15 #ifndef RCLCPP__SUBSCRIPTION_BASE_HPP_
16 #define RCLCPP__SUBSCRIPTION_BASE_HPP_
22 #include <unordered_map>
26 #include "rcl/event_callback.h"
31 #include "rclcpp/any_subscription_callback.hpp"
32 #include "rclcpp/dynamic_typesupport/dynamic_message.hpp"
33 #include "rclcpp/dynamic_typesupport/dynamic_message_type.hpp"
34 #include "rclcpp/dynamic_typesupport/dynamic_serialization_support.hpp"
35 #include "rclcpp/experimental/intra_process_manager.hpp"
36 #include "rclcpp/experimental/subscription_intra_process_base.hpp"
37 #include "rclcpp/logger.hpp"
38 #include "rclcpp/macros.hpp"
39 #include "rclcpp/message_info.hpp"
40 #include "rclcpp/network_flow_endpoint.hpp"
41 #include "rclcpp/qos.hpp"
42 #include "rclcpp/event_handler.hpp"
43 #include "rclcpp/serialized_message.hpp"
44 #include "rclcpp/subscription_content_filter_options.hpp"
45 #include "rclcpp/type_support_decl.hpp"
46 #include "rclcpp/visibility_control.hpp"
51 namespace node_interfaces
53 class NodeBaseInterface;
56 namespace experimental
62 class IntraProcessManager;
82 SERIALIZED_MESSAGE = 2,
108 const rosidl_message_type_support_t & type_support_handle,
109 const std::string & topic_name,
112 bool use_default_callbacks,
146 std::shared_ptr<rcl_subscription_t>
147 get_subscription_handle();
150 std::shared_ptr<const rcl_subscription_t>
151 get_subscription_handle()
const;
157 std::unordered_map<rcl_subscription_event_type_t, std::shared_ptr<rclcpp::EventHandlerBase>> &
219 std::shared_ptr<void>
226 std::shared_ptr<rclcpp::SerializedMessage>
259 handle_serialized_message(
260 const std::shared_ptr<rclcpp::SerializedMessage> & serialized_message,
266 handle_loaned_message(
void * loaned_message,
const rclcpp::MessageInfo & message_info) = 0;
283 const rosidl_message_type_support_t &
284 get_message_type_support_handle()
const;
319 using IntraProcessManagerWeakPtr =
320 std::weak_ptr<rclcpp::experimental::IntraProcessManager>;
326 uint64_t intra_process_subscription_id,
327 IntraProcessManagerWeakPtr weak_ipm);
335 rclcpp::Waitable::SharedPtr
361 std::vector<rclcpp::NetworkFlowEndpoint>
453 const std::function<
void(
size_t)> & callback,
491 const std::string & filter_expression,
492 const std::vector<std::string> & expression_parameters = {});
508 rclcpp::dynamic_typesupport::DynamicMessageType::SharedPtr
509 get_shared_dynamic_message_type() = 0;
513 rclcpp::dynamic_typesupport::DynamicMessage::SharedPtr
514 get_shared_dynamic_message() = 0;
518 rclcpp::dynamic_typesupport::DynamicSerializationSupport::SharedPtr
519 get_shared_dynamic_serialization_support() = 0;
525 rclcpp::dynamic_typesupport::DynamicMessage::SharedPtr
531 return_dynamic_message(rclcpp::dynamic_typesupport::DynamicMessage::SharedPtr & message) = 0;
536 handle_dynamic_message(
537 const rclcpp::dynamic_typesupport::DynamicMessage::SharedPtr & message,
542 take_dynamic_message(
548 template<
typename EventCallbackT>
551 const EventCallbackT & callback,
554 auto handler = std::make_shared<
EventHandler<EventCallbackT,
555 std::shared_ptr<rcl_subscription_t>>>(
558 get_subscription_handle(),
560 qos_events_in_use_by_wait_set_.insert(std::make_pair(handler.get(),
false));
561 event_handlers_.insert(std::make_pair(event_type, handler));
565 void default_incompatible_qos_callback(QOSRequestedIncompatibleQoSInfo & info)
const;
568 void default_incompatible_type_callback(IncompatibleTypeInfo & info)
const;
572 matches_any_intra_process_publishers(
const rmw_gid_t * sender_gid)
const;
580 std::shared_ptr<rcl_node_t> node_handle_;
582 std::recursive_mutex on_new_message_callback_mutex_;
587 std::function<void(
size_t)> on_new_message_callback_{
nullptr};
589 std::shared_ptr<rcl_subscription_t> subscription_handle_;
590 std::shared_ptr<rcl_subscription_t> intra_process_subscription_handle_;
594 std::shared_ptr<rclcpp::EventHandlerBase>> event_handlers_;
596 bool use_intra_process_;
597 IntraProcessManagerWeakPtr weak_ipm_;
598 uint64_t intra_process_subscription_id_;
599 std::shared_ptr<rclcpp::experimental::SubscriptionIntraProcessBase> subscription_intra_process_;
601 const SubscriptionEventCallbacks event_callbacks_;
606 rosidl_message_type_support_t type_support_;
609 std::atomic<bool> subscription_in_use_by_wait_set_{
false};
610 std::atomic<bool> intra_process_subscription_waitable_in_use_by_wait_set_{
false};
612 std::atomic<bool>> qos_events_in_use_by_wait_set_;
Additional meta data about messages taken from subscriptions.
Encapsulation of Quality of Service settings.
Object oriented version of rcl_serialized_message_t with destructor to avoid memory leaks.
virtual RCLCPP_PUBLIC std::shared_ptr< void > create_message()=0
Borrow a new message.
RCLCPP_PUBLIC size_t get_publisher_count() const
Get matching publisher count.
virtual RCLCPP_PUBLIC rclcpp::dynamic_typesupport::DynamicMessage::SharedPtr create_dynamic_message()=0
Borrow a new serialized message (this clones!)
RCLCPP_PUBLIC rclcpp::QoS get_actual_qos() const
Get the actual QoS settings, after the defaults have been determined.
RCLCPP_PUBLIC void set_on_new_message_callback(const std::function< void(size_t)> &callback)
Set a callback to be called when each new message is received.
RCLCPP_PUBLIC void clear_on_new_message_callback()
Unset the callback registered for new messages, if any.
virtual RCLCPP_PUBLIC void handle_message(std::shared_ptr< void > &message, const rclcpp::MessageInfo &message_info)=0
Check if we need to handle the message, and execute the callback if we do.
RCLCPP_PUBLIC void set_on_new_qos_event_callback(const std::function< void(size_t)> &callback, rcl_subscription_event_type_t event_type)
Set a callback to be called when each new qos event instance occurs.
virtual RCLCPP_PUBLIC void enable_callbacks()
Enable the callbacks to be called.
RCLCPP_PUBLIC bool can_loan_messages() const
Check if subscription instance can loan messages.
static RCLCPP_PUBLIC bool event_type_is_supported(const rcl_subscription_event_type_t event_type)
Check if a subscription event type is supported by the active RMW implementation.
RCLCPP_PUBLIC bool is_cft_supported() const
Check if content filtered topic feature of the subscription instance is supported.
RCLCPP_PUBLIC rclcpp::Waitable::SharedPtr get_intra_process_waitable() const
Return the waitable for intra-process.
RCLCPP_PUBLIC std::vector< rclcpp::NetworkFlowEndpoint > get_network_flow_endpoints() const
Get network flow endpoints.
RCLCPP_PUBLIC void setup_intra_process(uint64_t intra_process_subscription_id, IntraProcessManagerWeakPtr weak_ipm)
Implemenation detail.
RCLCPP_PUBLIC DeliveredMessageKind get_delivered_message_kind() const
Return the delivered message kind.
virtual RCLCPP_PUBLIC void return_message(std::shared_ptr< void > &message)=0
Return the message borrowed in create_message.
RCLCPP_PUBLIC void set_on_new_intra_process_message_callback(const std::function< void(size_t)> &callback)
Set a callback to be called when each new intra-process message is received.
virtual RCLCPP_PUBLIC void disable_callbacks()
Disable callbacks from being called.
virtual RCLCPP_PUBLIC std::shared_ptr< rclcpp::SerializedMessage > create_serialized_message()=0
Borrow a new serialized message.
RCLCPP_PUBLIC void clear_on_new_qos_event_callback(rcl_subscription_event_type_t event_type)
Unset the callback registered for new qos events, if any.
RCLCPP_PUBLIC bool take_serialized(rclcpp::SerializedMessage &message_out, rclcpp::MessageInfo &message_info_out)
Take the next inter-process message, in its serialized form, from the subscription.
RCLCPP_PUBLIC bool take_type_erased(void *message_out, rclcpp::MessageInfo &message_info_out)
Take the next inter-process message from the subscription as a type erased pointer.
RCLCPP_PUBLIC bool exchange_in_use_by_wait_set_state(void *pointer_to_subscription_part, bool in_use_state)
Exchange state of whether or not a part of the subscription is used by a wait set.
RCLCPP_PUBLIC void set_content_filter(const std::string &filter_expression, const std::vector< std::string > &expression_parameters={})
Set the filter expression and expression parameters for the subscription.
virtual RCLCPP_PUBLIC ~SubscriptionBase()
Destructor.
RCLCPP_PUBLIC SubscriptionBase(rclcpp::node_interfaces::NodeBaseInterface *node_base, const rosidl_message_type_support_t &type_support_handle, const std::string &topic_name, const rcl_subscription_options_t &subscription_options, const SubscriptionEventCallbacks &event_callbacks, bool use_default_callbacks, DeliveredMessageKind delivered_message_kind=DeliveredMessageKind::ROS_MESSAGE)
Constructor.
virtual RCLCPP_PUBLIC void return_serialized_message(std::shared_ptr< rclcpp::SerializedMessage > &message)=0
Return the message borrowed in create_serialized_message.
RCLCPP_PUBLIC bool is_cft_enabled() const
Check if content filtered topic feature of the subscription instance is enabled.
RCLCPP_PUBLIC void bind_event_callbacks(const SubscriptionEventCallbacks &event_callbacks, bool use_default_callbacks)
Add event handlers for passed in event_callbacks.
RCLCPP_PUBLIC const std::unordered_map< rcl_subscription_event_type_t, std::shared_ptr< rclcpp::EventHandlerBase > > & get_event_handlers() const
Get all the QoS event handlers associated with this subscription.
RCLCPP_PUBLIC void clear_on_new_intra_process_message_callback()
Unset the callback registered for new intra-process messages, if any.
RCLCPP_PUBLIC rclcpp::ContentFilterOptions get_content_filter() const
Get the filter expression and expression parameters for the subscription.
RCLCPP_PUBLIC bool is_serialized() const
Return if the subscription is serialized.
RCLCPP_PUBLIC const char * get_topic_name() const
Get the topic that this subscription is subscribed on.
Pure virtual interface class for the NodeBase part of the Node API.
enum rcl_subscription_event_type_e rcl_subscription_event_type_t
Enumeration of all of the subscription events that may fire.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_subscription_event_init(rcl_event_t *event, const rcl_subscription_t *subscription, const rcl_subscription_event_type_t event_type)
Initialize an rcl_event_t with a subscription.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
DeliveredMessageKind
The kind of message that the subscription delivers in its callback, used by the executor.
Options available for a rcl subscription.
Options to configure content filtered topic in the subscription.
Contains callbacks for non-message events that a Subscription can receive from the middleware.