24 #include "rcl/error_handling.h"
27 #include "rcutils/allocator.h"
28 #include "rcutils/logging_macros.h"
29 #include "rmw/error_handling.h"
30 #include "rmw/validate_full_topic_name.h"
31 #include "rmw/event.h"
34 #include "./event_impl.h"
35 #include "./publisher_impl.h"
36 #include "./subscription_impl.h"
57 rmw_event_type_t rmw_event_type = RMW_EVENT_INVALID;
59 case RCL_PUBLISHER_OFFERED_DEADLINE_MISSED:
60 rmw_event_type = RMW_EVENT_OFFERED_DEADLINE_MISSED;
62 case RCL_PUBLISHER_LIVELINESS_LOST:
63 rmw_event_type = RMW_EVENT_LIVELINESS_LOST;
65 case RCL_PUBLISHER_OFFERED_INCOMPATIBLE_QOS:
66 rmw_event_type = RMW_EVENT_OFFERED_QOS_INCOMPATIBLE;
68 case RCL_PUBLISHER_INCOMPATIBLE_TYPE:
69 rmw_event_type = RMW_EVENT_PUBLISHER_INCOMPATIBLE_TYPE;
71 case RCL_PUBLISHER_MATCHED:
72 rmw_event_type = RMW_EVENT_PUBLICATION_MATCHED;
75 if (rmw_event_type == RMW_EVENT_INVALID) {
76 RCL_SET_ERROR_MSG(
"Event type for publisher not supported");
83 RCL_CHECK_FOR_NULL_WITH_MSG(
86 event->impl->rmw_handle = rmw_get_zero_initialized_event();
87 event->impl->allocator = *allocator;
89 rmw_ret_t ret = rmw_publisher_event_init(
90 &event->
impl->rmw_handle,
91 publisher->
impl->rmw_handle,
93 if (ret != RMW_RET_OK) {
99 allocator->deallocate(event->
impl, allocator->state);
101 return rcl_convert_rmw_ret_to_rcl_ret(ret);
115 rmw_event_type_t rmw_event_type = RMW_EVENT_INVALID;
116 switch (event_type) {
117 case RCL_SUBSCRIPTION_REQUESTED_DEADLINE_MISSED:
118 rmw_event_type = RMW_EVENT_REQUESTED_DEADLINE_MISSED;
120 case RCL_SUBSCRIPTION_LIVELINESS_CHANGED:
121 rmw_event_type = RMW_EVENT_LIVELINESS_CHANGED;
123 case RCL_SUBSCRIPTION_REQUESTED_INCOMPATIBLE_QOS:
124 rmw_event_type = RMW_EVENT_REQUESTED_QOS_INCOMPATIBLE;
126 case RCL_SUBSCRIPTION_MESSAGE_LOST:
127 rmw_event_type = RMW_EVENT_MESSAGE_LOST;
129 case RCL_SUBSCRIPTION_INCOMPATIBLE_TYPE:
130 rmw_event_type = RMW_EVENT_SUBSCRIPTION_INCOMPATIBLE_TYPE;
132 case RCL_SUBSCRIPTION_MATCHED:
133 rmw_event_type = RMW_EVENT_SUBSCRIPTION_MATCHED;
136 if (rmw_event_type == RMW_EVENT_INVALID) {
137 RCL_SET_ERROR_MSG(
"Event type for subscription not supported");
144 RCL_CHECK_FOR_NULL_WITH_MSG(
147 event->impl->rmw_handle = rmw_get_zero_initialized_event();
148 event->impl->allocator = *allocator;
150 rmw_ret_t ret = rmw_subscription_event_init(
151 &event->
impl->rmw_handle,
152 subscription->
impl->rmw_handle,
154 if (ret != RMW_RET_OK) {
160 allocator->deallocate(event->
impl, allocator->state);
162 return rcl_convert_rmw_ret_to_rcl_ret(ret);
175 rmw_ret_t ret = rmw_take_event(&event->
impl->rmw_handle, event_info, &taken);
176 if (RMW_RET_OK != ret) {
177 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
178 return rcl_convert_rmw_ret_to_rcl_ret(ret);
181 RCUTILS_LOG_DEBUG_NAMED(
182 ROS_PACKAGE_NAME,
"take_event request complete, unable to take event");
185 RCUTILS_LOG_DEBUG_NAMED(
186 ROS_PACKAGE_NAME,
"take_event request success");
187 return rcl_convert_rmw_ret_to_rcl_ret(ret);
196 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Finalizing event");
197 if (NULL != event->
impl) {
199 rmw_ret_t ret = rmw_event_fini(&event->
impl->rmw_handle);
200 if (ret != RMW_RET_OK) {
201 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
202 result = rcl_convert_rmw_ret_to_rcl_ret(ret);
204 allocator.deallocate(event->
impl, allocator.state);
207 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Event finalized");
218 return &
event->impl->rmw_handle;
225 RCL_CHECK_FOR_NULL_WITH_MSG(event,
"event pointer is invalid",
return false);
226 RCL_CHECK_FOR_NULL_WITH_MSG(event->
impl,
"event's implementation is invalid",
return false);
227 if (event->
impl->rmw_handle.event_type == RMW_EVENT_INVALID) {
228 RCUTILS_SET_ERROR_MSG(
"event's implementation not init");
231 RCUTILS_CHECK_ALLOCATOR_WITH_MSG(
232 &event->
impl->allocator,
"not valid allocator",
return false);
239 rcl_event_callback_t callback,
240 const void * user_data)
247 return rmw_event_set_callback(
248 &event->
impl->rmw_handle,
#define RCL_CHECK_ALLOCATOR_WITH_MSG(allocator, msg, fail_statement)
Check that the given allocator is initialized, or fail with a message.
rcutils_allocator_t rcl_allocator_t
Encapsulation of an allocator.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_take_event(const rcl_event_t *event, void *event_info)
enum rcl_publisher_event_type_e rcl_publisher_event_type_t
Enumeration of all of the publisher events that may fire.
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.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_event_set_callback(const rcl_event_t *event, rcl_event_callback_t callback, const void *user_data)
Set the callback function for the event.
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 rmw_event_t * rcl_event_get_rmw_handle(const rcl_event_t *event)
Return the rmw event handle.
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.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_event_fini(rcl_event_t *event)
RCL_PUBLIC bool rcl_event_is_valid(const rcl_event_t *event)
Check that the event is valid.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_publisher_event_init(rcl_event_t *event, const rcl_publisher_t *publisher, const rcl_publisher_event_type_t event_type)
Initialize an rcl_event_t with a publisher.
Structure which encapsulates a ROS QoS event handle.
rcl_event_impl_t * impl
Pointer to the event implementation.
rcl_allocator_t allocator
Custom allocator for the publisher, used for incidental allocations.
Structure which encapsulates a ROS Publisher.
rcl_publisher_impl_t * impl
Pointer to the publisher implementation.
rcl_allocator_t allocator
Custom allocator for the subscription, used for incidental allocations.
Structure which encapsulates a ROS Subscription.
rcl_subscription_impl_t * impl
Pointer to the subscription implementation.
#define RCL_RET_EVENT_TAKE_FAILED
Failed to take an event from the event handle.
#define RCL_RET_OK
Success return code.
#define RCL_RET_BAD_ALLOC
Failed to allocate memory return code.
#define RCL_RET_INVALID_ARGUMENT
Invalid argument return code.
#define RCL_RET_EVENT_INVALID
Invalid rcl_event_t given return code.
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.