ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
event.c
1 // Copyright 2019 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifdef __cplusplus
16 extern "C"
17 {
18 #endif
19 
20 #include "rcl/event.h"
21 
22 #include <stdio.h>
23 
24 #include "rcl/error_handling.h"
25 #include "rcl/expand_topic_name.h"
26 #include "rcl/remap.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"
32 
33 #include "./common.h"
34 #include "./event_impl.h"
35 #include "./publisher_impl.h"
36 #include "./subscription_impl.h"
37 
40 {
41  static rcl_event_t null_event = {0};
42  return null_event;
43 }
44 
47  rcl_event_t * event,
48  const rcl_publisher_t * publisher,
49  const rcl_publisher_event_type_t event_type)
50 {
51  RCL_CHECK_ARGUMENT_FOR_NULL(event, RCL_RET_EVENT_INVALID);
52  // Check publisher and allocator first, so allocator can be used with errors.
53  RCL_CHECK_ARGUMENT_FOR_NULL(publisher, RCL_RET_INVALID_ARGUMENT);
54  rcl_allocator_t * allocator = &publisher->impl->options.allocator;
55  RCL_CHECK_ALLOCATOR_WITH_MSG(allocator, "invalid allocator", return RCL_RET_INVALID_ARGUMENT);
56  rmw_event_type_t rmw_event_type = RMW_EVENT_INVALID;
57  switch (event_type) {
58  case RCL_PUBLISHER_OFFERED_DEADLINE_MISSED:
59  rmw_event_type = RMW_EVENT_OFFERED_DEADLINE_MISSED;
60  break;
61  case RCL_PUBLISHER_LIVELINESS_LOST:
62  rmw_event_type = RMW_EVENT_LIVELINESS_LOST;
63  break;
64  case RCL_PUBLISHER_OFFERED_INCOMPATIBLE_QOS:
65  rmw_event_type = RMW_EVENT_OFFERED_QOS_INCOMPATIBLE;
66  break;
67  default:
68  RCL_SET_ERROR_MSG("Event type for publisher not supported");
70  }
71 
72  // Allocate space for the implementation struct.
73  event->impl = (rcl_event_impl_t *) allocator->allocate(
74  sizeof(rcl_event_impl_t), allocator->state);
75  RCL_CHECK_FOR_NULL_WITH_MSG(
76  event->impl, "allocating memory failed", return RCL_RET_BAD_ALLOC);
77 
78  event->impl->rmw_handle = rmw_get_zero_initialized_event();
79  event->impl->allocator = *allocator;
80 
81  rmw_ret_t ret = rmw_publisher_event_init(
82  &event->impl->rmw_handle,
83  publisher->impl->rmw_handle,
84  rmw_event_type);
85  if (ret != RMW_RET_OK) {
86  goto fail;
87  }
88 
89  return RCL_RET_OK;
90 fail:
91  allocator->deallocate(event->impl, allocator->state);
92  event->impl = NULL;
93  return rcl_convert_rmw_ret_to_rcl_ret(ret);
94 }
95 
98  rcl_event_t * event,
99  const rcl_subscription_t * subscription,
100  const rcl_subscription_event_type_t event_type)
101 {
102  RCL_CHECK_ARGUMENT_FOR_NULL(event, RCL_RET_EVENT_INVALID);
103  // Check subscription and allocator first, so allocator can be used with errors.
104  RCL_CHECK_ARGUMENT_FOR_NULL(subscription, RCL_RET_INVALID_ARGUMENT);
105  rcl_allocator_t * allocator = &subscription->impl->options.allocator;
106  RCL_CHECK_ALLOCATOR_WITH_MSG(allocator, "invalid allocator", return RCL_RET_INVALID_ARGUMENT);
107  rmw_event_type_t rmw_event_type = RMW_EVENT_INVALID;
108  switch (event_type) {
109  case RCL_SUBSCRIPTION_REQUESTED_DEADLINE_MISSED:
110  rmw_event_type = RMW_EVENT_REQUESTED_DEADLINE_MISSED;
111  break;
112  case RCL_SUBSCRIPTION_LIVELINESS_CHANGED:
113  rmw_event_type = RMW_EVENT_LIVELINESS_CHANGED;
114  break;
115  case RCL_SUBSCRIPTION_REQUESTED_INCOMPATIBLE_QOS:
116  rmw_event_type = RMW_EVENT_REQUESTED_QOS_INCOMPATIBLE;
117  break;
118  case RCL_SUBSCRIPTION_MESSAGE_LOST:
119  rmw_event_type = RMW_EVENT_MESSAGE_LOST;
120  break;
121  default:
122  RCL_SET_ERROR_MSG("Event type for subscription not supported");
124  }
125 
126  // Allocate space for the implementation struct.
127  event->impl = (rcl_event_impl_t *) allocator->allocate(
128  sizeof(rcl_event_impl_t), allocator->state);
129  RCL_CHECK_FOR_NULL_WITH_MSG(
130  event->impl, "allocating memory failed", return RCL_RET_BAD_ALLOC);
131 
132  event->impl->rmw_handle = rmw_get_zero_initialized_event();
133  event->impl->allocator = *allocator;
134 
135  rmw_ret_t ret = rmw_subscription_event_init(
136  &event->impl->rmw_handle,
137  subscription->impl->rmw_handle,
138  rmw_event_type);
139  if (ret != RMW_RET_OK) {
140  goto fail;
141  }
142 
143  return RCL_RET_OK;
144 fail:
145  allocator->deallocate(event->impl, allocator->state);
146  event->impl = NULL;
147  return rcl_convert_rmw_ret_to_rcl_ret(ret);
148 }
149 
150 rcl_ret_t
152  const rcl_event_t * event,
153  void * event_info)
154 {
155  bool taken = false;
156  if (!rcl_event_is_valid(event)) {
157  return RCL_RET_EVENT_INVALID;
158  }
159  RCL_CHECK_ARGUMENT_FOR_NULL(event_info, RCL_RET_INVALID_ARGUMENT);
160  rmw_ret_t ret = rmw_take_event(&event->impl->rmw_handle, event_info, &taken);
161  if (RMW_RET_OK != ret) {
162  RCL_SET_ERROR_MSG(rmw_get_error_string().str);
163  return rcl_convert_rmw_ret_to_rcl_ret(ret);
164  }
165  if (!taken) {
166  RCUTILS_LOG_DEBUG_NAMED(
167  ROS_PACKAGE_NAME, "take_event request complete, unable to take event");
169  }
170  RCUTILS_LOG_DEBUG_NAMED(
171  ROS_PACKAGE_NAME, "take_event request success");
172  return rcl_convert_rmw_ret_to_rcl_ret(ret);
173 }
174 
175 rcl_ret_t
177 {
178  rcl_ret_t result = RCL_RET_OK;
179  RCL_CHECK_ARGUMENT_FOR_NULL(event, RCL_RET_EVENT_INVALID);
180 
181  RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME, "Finalizing event");
182  if (NULL != event->impl) {
183  rcl_allocator_t allocator = event->impl->allocator;
184  rmw_ret_t ret = rmw_event_fini(&event->impl->rmw_handle);
185  if (ret != RMW_RET_OK) {
186  RCL_SET_ERROR_MSG(rmw_get_error_string().str);
187  result = rcl_convert_rmw_ret_to_rcl_ret(ret);
188  }
189  allocator.deallocate(event->impl, allocator.state);
190  event->impl = NULL;
191  }
192  RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME, "Event finalized");
193 
194  return result;
195 }
196 
197 rmw_event_t *
199 {
200  if (!rcl_event_is_valid(event)) {
201  return NULL; // error already set
202  } else {
203  return &event->impl->rmw_handle;
204  }
205 }
206 
207 bool
209 {
210  RCL_CHECK_FOR_NULL_WITH_MSG(event, "event pointer is invalid", return false);
211  RCL_CHECK_FOR_NULL_WITH_MSG(event->impl, "event's implementation is invalid", return false);
212  if (event->impl->rmw_handle.event_type == RMW_EVENT_INVALID) {
213  RCUTILS_SET_ERROR_MSG("event's implementation not init");
214  return false;
215  }
216  RCUTILS_CHECK_ALLOCATOR_WITH_MSG(
217  &event->impl->allocator, "not valid allocator", return false);
218  return true;
219 }
220 
221 rcl_ret_t
223  const rcl_event_t * event,
224  rcl_event_callback_t callback,
225  const void * user_data)
226 {
227  if (!rcl_event_is_valid(event)) {
228  // error state already set
230  }
231 
232  return rmw_event_set_callback(
233  &event->impl->rmw_handle,
234  callback,
235  user_data);
236 }
237 
238 #ifdef __cplusplus
239 }
240 #endif
#define RCL_CHECK_ALLOCATOR_WITH_MSG(allocator, msg, fail_statement)
Check that the given allocator is initialized, or fail with a message.
Definition: allocator.h:56
rcutils_allocator_t rcl_allocator_t
Encapsulation of an allocator.
Definition: allocator.h:31
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_take_event(const rcl_event_t *event, void *event_info)
Definition: event.c:151
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.
Definition: event.c:39
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.
Definition: event.c:222
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.
Definition: event.c:198
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.
Definition: event.c:97
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_event_fini(rcl_event_t *event)
Definition: event.c:176
RCL_PUBLIC bool rcl_event_is_valid(const rcl_event_t *event)
Check that the event is valid.
Definition: event.c:208
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.
Definition: event.c:46
Structure which encapsulates a ROS QoS event handle.
Definition: event.h:57
rcl_event_impl_t * impl
Pointer to the event implementation.
Definition: event.h:59
rcl_allocator_t allocator
Custom allocator for the publisher, used for incidental allocations.
Definition: publisher.h:49
Structure which encapsulates a ROS Publisher.
Definition: publisher.h:37
rcl_publisher_impl_t * impl
Pointer to the publisher implementation.
Definition: publisher.h:39
rcl_allocator_t allocator
Custom allocator for the subscription, used for incidental allocations.
Definition: subscription.h:51
Structure which encapsulates a ROS Subscription.
Definition: subscription.h:39
rcl_subscription_impl_t * impl
Pointer to the subscription implementation.
Definition: subscription.h:41
#define RCL_RET_EVENT_TAKE_FAILED
Failed to take an event from the event handle.
Definition: types.h:118
#define RCL_RET_OK
Success return code.
Definition: types.h:26
#define RCL_RET_BAD_ALLOC
Failed to allocate memory return code.
Definition: types.h:32
#define RCL_RET_INVALID_ARGUMENT
Invalid argument return code.
Definition: types.h:34
#define RCL_RET_EVENT_INVALID
Invalid rcl_event_t given return code.
Definition: types.h:116
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:23