15 #ifndef RCLCPP__EVENT_HANDLER_HPP_
16 #define RCLCPP__EVENT_HANDLER_HPP_
26 #include "rcl/error_handling.h"
27 #include "rcl/event_callback.h"
28 #include "rmw/incompatible_qos_events_statuses.h"
29 #include "rmw/events_statuses/incompatible_type.h"
31 #include "rcutils/logging_macros.h"
33 #include "rclcpp/detail/cpp_callback_trampoline.hpp"
34 #include "rclcpp/exceptions.hpp"
35 #include "rclcpp/function_traits.hpp"
36 #include "rclcpp/waitable.hpp"
41 using QOSDeadlineRequestedInfo = rmw_requested_deadline_missed_status_t;
42 using QOSDeadlineOfferedInfo = rmw_offered_deadline_missed_status_t;
43 using QOSLivelinessChangedInfo = rmw_liveliness_changed_status_t;
44 using QOSLivelinessLostInfo = rmw_liveliness_lost_status_t;
45 using QOSMessageLostInfo = rmw_message_lost_status_t;
46 using QOSOfferedIncompatibleQoSInfo = rmw_offered_qos_incompatible_event_status_t;
47 using QOSRequestedIncompatibleQoSInfo = rmw_requested_qos_incompatible_event_status_t;
49 using IncompatibleTypeInfo = rmw_incompatible_type_status_t;
50 using MatchedInfo = rmw_matched_status_t;
52 using QOSDeadlineRequestedCallbackType = std::function<void (QOSDeadlineRequestedInfo &)>;
53 using QOSDeadlineOfferedCallbackType = std::function<void (QOSDeadlineOfferedInfo &)>;
54 using QOSLivelinessChangedCallbackType = std::function<void (QOSLivelinessChangedInfo &)>;
55 using QOSLivelinessLostCallbackType = std::function<void (QOSLivelinessLostInfo &)>;
56 using QOSMessageLostCallbackType = std::function<void (QOSMessageLostInfo &)>;
57 using QOSOfferedIncompatibleQoSCallbackType = std::function<void (QOSOfferedIncompatibleQoSInfo &)>;
58 using QOSRequestedIncompatibleQoSCallbackType =
59 std::function<void (QOSRequestedIncompatibleQoSInfo &)>;
61 using IncompatibleTypeCallbackType = std::function<void (IncompatibleTypeInfo &)>;
62 using PublisherMatchedCallbackType = std::function<void (MatchedInfo &)>;
63 using SubscriptionMatchedCallbackType = std::function<void (MatchedInfo &)>;
68 QOSDeadlineOfferedCallbackType deadline_callback;
69 QOSLivelinessLostCallbackType liveliness_callback;
70 QOSOfferedIncompatibleQoSCallbackType incompatible_qos_callback;
71 IncompatibleTypeCallbackType incompatible_type_callback;
72 PublisherMatchedCallbackType matched_callback;
78 QOSDeadlineRequestedCallbackType deadline_callback;
79 QOSLivelinessChangedCallbackType liveliness_callback;
80 QOSRequestedIncompatibleQoSCallbackType incompatible_qos_callback;
81 QOSMessageLostCallbackType message_lost_callback;
82 IncompatibleTypeCallbackType incompatible_type_callback;
83 SubscriptionMatchedCallbackType matched_callback;
92 const rcl_error_state_t * error_state,
93 const std::string & prefix);
98 const std::string & prefix);
104 enum class EntityType : std::size_t
181 std::vector<std::shared_ptr<rclcpp::TimerBase>>
187 set_on_new_event_callback(rcl_event_callback_t callback,
const void * user_data);
189 std::recursive_mutex on_new_event_callback_mutex_;
190 std::function<void(
size_t)> on_new_event_callback_{
nullptr};
193 size_t wait_set_event_index_;
196 template<
typename EventCallbackT,
typename ParentHandleT>
200 template<
typename InitFuncT,
typename EventTypeEnum>
202 const EventCallbackT & callback,
204 ParentHandleT parent_handle,
205 EventTypeEnum event_type)
206 : parent_handle_(parent_handle), event_callback_(callback)
209 rcl_ret_t ret = init_func(&event_handle_, parent_handle.get(), event_type);
216 rclcpp::exceptions::throw_from_rcl_error(ret,
"Failed to initialize event");
232 std::shared_ptr<void>
235 EventCallbackInfoT callback_info;
238 RCUTILS_LOG_ERROR_NAMED(
240 "Couldn't take event info: %s", rcl_get_error_string().str);
244 return std::static_pointer_cast<void>(std::make_shared<EventCallbackInfoT>(callback_info));
247 std::shared_ptr<void>
248 take_data_by_entity_id([[maybe_unused]]
size_t id)
override
255 execute(
const std::shared_ptr<void> & data)
override
257 std::unique_lock<std::mutex> event_callback_lock(event_callback_mutex_);
258 if (disabled_.load()) {
262 throw std::runtime_error(
"'data' is empty");
264 auto callback_ptr = std::static_pointer_cast<EventCallbackInfoT>(data);
265 event_callback_(*callback_ptr);
266 callback_ptr.reset();
278 std::lock_guard<std::recursive_mutex> on_new_event_lock(on_new_event_callback_mutex_);
279 if (on_new_event_callback_) {
280 set_on_new_event_callback(
nullptr,
nullptr);
283 std::lock_guard<std::mutex> event_callback_lock(event_callback_mutex_);
284 disabled_.store(
true);
296 std::lock_guard<std::recursive_mutex> on_new_event_lock(on_new_event_callback_mutex_);
297 if (on_new_event_callback_) {
298 set_on_new_event_callback(
299 rclcpp::detail::cpp_callback_trampoline<
300 decltype(on_new_event_callback_),
const void *,
size_t>,
301 static_cast<const void *
>(&on_new_event_callback_));
304 std::lock_guard<std::mutex> event_callback_lock(event_callback_mutex_);
305 disabled_.store(
false);
309 using EventCallbackInfoT =
typename std::remove_reference<
typename
312 ParentHandleT parent_handle_;
313 EventCallbackT event_callback_;
314 std::mutex event_callback_mutex_;
315 std::atomic_bool disabled_{
false};
RCLCPP_PUBLIC size_t get_number_of_ready_events() override
Get the number of ready events.
RCLCPP_PUBLIC void add_to_wait_set(rcl_wait_set_t &wait_set) override
Add the Waitable to a wait set.
RCLCPP_PUBLIC void set_on_ready_callback(std::function< void(size_t, int)> callback) override
Set a callback to be called when each new event instance occurs.
RCLCPP_PUBLIC bool is_ready(const rcl_wait_set_t &wait_set) override
Check if the Waitable is ready.
RCLCPP_PUBLIC void clear_on_ready_callback() override
Unset the callback registered for new events, if any.
RCLCPP_PUBLIC std::vector< std::shared_ptr< rclcpp::TimerBase > > get_timers() const override
Returns all timers used by this waitable.
void disable() override
Disable the event callback from being called when execute(..) invoked.
void enable() override
Enable the event callback to be called when execute(..) invoked.
void execute(const std::shared_ptr< void > &data) override
Execute any entities of the Waitable that are ready.
std::shared_ptr< void > take_data() override
Take data so that the callback cannot be scheduled again.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_take_event(const rcl_event_t *event, void *event_info)
RCL_PUBLIC RCL_WARN_UNUSED rcl_event_t rcl_get_zero_initialized_event(void)
Return a rcl_event_t struct with members set to NULL.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
Structure which encapsulates a ROS QoS event handle.
Container for subscription's, guard condition's, etc to be waited on.
Contains callbacks for various types of events a Publisher can receive from the middleware.
Contains callbacks for non-message events that a Subscription can receive from the middleware.
#define RCL_RET_UNSUPPORTED
Unsupported return code.
#define RCL_RET_OK
Success return code.
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.