ROS 2 rclcpp + rcl - rolling  rolling-a919a6e5
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  // All members are initialized to 0 or NULL by C99 6.7.8/10.
42  static rcl_event_t null_event;
43  return null_event;
44 }
45 
48  rcl_event_t * event,
49  const rcl_publisher_t * publisher,
50  const rcl_publisher_event_type_t event_type)
51 {
52  RCL_CHECK_ARGUMENT_FOR_NULL(event, RCL_RET_EVENT_INVALID);
53  // Check publisher and allocator first, so allocator can be used with errors.
54  RCL_CHECK_ARGUMENT_FOR_NULL(publisher, RCL_RET_INVALID_ARGUMENT);
55  rcl_allocator_t * allocator = &publisher->impl->options.allocator;
56  RCL_CHECK_ALLOCATOR_WITH_MSG(allocator, "invalid allocator", return RCL_RET_INVALID_ARGUMENT);
57  rmw_event_type_t rmw_event_type = RMW_EVENT_INVALID;
58  switch (event_type) {
59  case RCL_PUBLISHER_OFFERED_DEADLINE_MISSED:
60  rmw_event_type = RMW_EVENT_OFFERED_DEADLINE_MISSED;
61  break;
62  case RCL_PUBLISHER_LIVELINESS_LOST:
63  rmw_event_type = RMW_EVENT_LIVELINESS_LOST;
64  break;
65  case RCL_PUBLISHER_OFFERED_INCOMPATIBLE_QOS:
66  rmw_event_type = RMW_EVENT_OFFERED_QOS_INCOMPATIBLE;
67  break;
68  case RCL_PUBLISHER_INCOMPATIBLE_TYPE:
69  rmw_event_type = RMW_EVENT_PUBLISHER_INCOMPATIBLE_TYPE;
70  break;
71  case RCL_PUBLISHER_MATCHED:
72  rmw_event_type = RMW_EVENT_PUBLICATION_MATCHED;
73  break;
74  default:
75  RCL_SET_ERROR_MSG("Event type for publisher not supported");
77  }
78 
79  // Allocate space for the implementation struct.
80  event->impl = (rcl_event_impl_t *) allocator->allocate(
81  sizeof(rcl_event_impl_t), allocator->state);
82  RCL_CHECK_FOR_NULL_WITH_MSG(
83  event->impl, "allocating memory failed", return RCL_RET_BAD_ALLOC);
84 
85  event->impl->rmw_handle = rmw_get_zero_initialized_event();
86  event->impl->allocator = *allocator;
87 
88  rmw_ret_t ret = rmw_publisher_event_init(
89  &event->impl->rmw_handle,
90  publisher->impl->rmw_handle,
91  rmw_event_type);
92  if (ret != RMW_RET_OK) {
93  goto fail;
94  }
95 
96  return RCL_RET_OK;
97 fail:
98  allocator->deallocate(event->impl, allocator->state);
99  event->impl = NULL;
100  return rcl_convert_rmw_ret_to_rcl_ret(ret);
101 }
102 
103 rcl_ret_t
105  rcl_event_t * event,
106  const rcl_subscription_t * subscription,
107  const rcl_subscription_event_type_t event_type)
108 {
109  RCL_CHECK_ARGUMENT_FOR_NULL(event, RCL_RET_EVENT_INVALID);
110  // Check subscription and allocator first, so allocator can be used with errors.
111  RCL_CHECK_ARGUMENT_FOR_NULL(subscription, RCL_RET_INVALID_ARGUMENT);
112  rcl_allocator_t * allocator = &subscription->impl->options.allocator;
113  RCL_CHECK_ALLOCATOR_WITH_MSG(allocator, "invalid allocator", return RCL_RET_INVALID_ARGUMENT);
114  rmw_event_type_t rmw_event_type = RMW_EVENT_INVALID;
115  switch (event_type) {
116  case RCL_SUBSCRIPTION_REQUESTED_DEADLINE_MISSED:
117  rmw_event_type = RMW_EVENT_REQUESTED_DEADLINE_MISSED;
118  break;
119  case RCL_SUBSCRIPTION_LIVELINESS_CHANGED:
120  rmw_event_type = RMW_EVENT_LIVELINESS_CHANGED;
121  break;
122  case RCL_SUBSCRIPTION_REQUESTED_INCOMPATIBLE_QOS:
123  rmw_event_type = RMW_EVENT_REQUESTED_QOS_INCOMPATIBLE;
124  break;
125  case RCL_SUBSCRIPTION_MESSAGE_LOST:
126  rmw_event_type = RMW_EVENT_MESSAGE_LOST;
127  break;
128  case RCL_SUBSCRIPTION_INCOMPATIBLE_TYPE:
129  rmw_event_type = RMW_EVENT_SUBSCRIPTION_INCOMPATIBLE_TYPE;
130  break;
131  case RCL_SUBSCRIPTION_MATCHED:
132  rmw_event_type = RMW_EVENT_SUBSCRIPTION_MATCHED;
133  break;
134  default:
135  RCL_SET_ERROR_MSG("Event type for subscription not supported");
137  }
138 
139  // Allocate space for the implementation struct.
140  event->impl = (rcl_event_impl_t *) allocator->allocate(
141  sizeof(rcl_event_impl_t), allocator->state);
142  RCL_CHECK_FOR_NULL_WITH_MSG(
143  event->impl, "allocating memory failed", return RCL_RET_BAD_ALLOC);
144 
145  event->impl->rmw_handle = rmw_get_zero_initialized_event();
146  event->impl->allocator = *allocator;
147 
148  rmw_ret_t ret = rmw_subscription_event_init(
149  &event->impl->rmw_handle,
150  subscription->impl->rmw_handle,
151  rmw_event_type);
152  if (ret != RMW_RET_OK) {
153  goto fail;
154  }
155 
156  return RCL_RET_OK;
157 fail:
158  allocator->deallocate(event->impl, allocator->state);
159  event->impl = NULL;
160  return rcl_convert_rmw_ret_to_rcl_ret(ret);
161 }
162 
163 rcl_ret_t
165  const rcl_event_t * event,
166  void * event_info)
167 {
168  bool taken = false;
169  if (!rcl_event_is_valid(event)) {
170  return RCL_RET_EVENT_INVALID;
171  }
172  RCL_CHECK_ARGUMENT_FOR_NULL(event_info, RCL_RET_INVALID_ARGUMENT);
173  rmw_ret_t ret = rmw_take_event(&event->impl->rmw_handle, event_info, &taken);
174  if (RMW_RET_OK != ret) {
175  RCL_SET_ERROR_MSG(rmw_get_error_string().str);
176  return rcl_convert_rmw_ret_to_rcl_ret(ret);
177  }
178  if (!taken) {
179  RCUTILS_LOG_DEBUG_NAMED(
180  ROS_PACKAGE_NAME, "take_event request complete, unable to take event");
182  }
183  RCUTILS_LOG_DEBUG_NAMED(
184  ROS_PACKAGE_NAME, "take_event request success");
185  return rcl_convert_rmw_ret_to_rcl_ret(ret);
186 }
187 
188 rcl_ret_t
190 {
191  rcl_ret_t result = RCL_RET_OK;
192  RCL_CHECK_ARGUMENT_FOR_NULL(event, RCL_RET_EVENT_INVALID);
193 
194  RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME, "Finalizing event");
195  if (NULL != event->impl) {
196  rcl_allocator_t allocator = event->impl->allocator;
197  rmw_ret_t ret = rmw_event_fini(&event->impl->rmw_handle);
198  if (ret != RMW_RET_OK) {
199  RCL_SET_ERROR_MSG(rmw_get_error_string().str);
200  result = rcl_convert_rmw_ret_to_rcl_ret(ret);
201  }
202  allocator.deallocate(event->impl, allocator.state);
203  event->impl = NULL;
204  }
205  RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME, "Event finalized");
206 
207  return result;
208 }
209 
210 rmw_event_t *
212 {
213  if (!rcl_event_is_valid(event)) {
214  return NULL; // error already set
215  } else {
216  return &event->impl->rmw_handle;
217  }
218 }
219 
220 bool
222 {
223  RCL_CHECK_FOR_NULL_WITH_MSG(event, "event pointer is invalid", return false);
224  RCL_CHECK_FOR_NULL_WITH_MSG(event->impl, "event's implementation is invalid", return false);
225  if (event->impl->rmw_handle.event_type == RMW_EVENT_INVALID) {
226  RCUTILS_SET_ERROR_MSG("event's implementation not init");
227  return false;
228  }
229  RCUTILS_CHECK_ALLOCATOR_WITH_MSG(
230  &event->impl->allocator, "not valid allocator", return false);
231  return true;
232 }
233 
234 rcl_ret_t
236  const rcl_event_t * event,
237  rcl_event_callback_t callback,
238  const void * user_data)
239 {
240  if (!rcl_event_is_valid(event)) {
241  // error state already set
243  }
244 
245  return rmw_event_set_callback(
246  &event->impl->rmw_handle,
247  callback,
248  user_data);
249 }
250 
251 #ifdef __cplusplus
252 }
253 #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:164
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:235
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:211
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:104
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_event_fini(rcl_event_t *event)
Definition: event.c:189
RCL_PUBLIC bool rcl_event_is_valid(const rcl_event_t *event)
Check that the event is valid.
Definition: event.c:221
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:47
Structure which encapsulates a ROS QoS event handle.
Definition: event.h:61
rcl_event_impl_t * impl
Pointer to the event implementation.
Definition: event.h:63
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:52
Structure which encapsulates a ROS Subscription.
Definition: subscription.h:40
rcl_subscription_impl_t * impl
Pointer to the subscription implementation.
Definition: subscription.h:42
#define RCL_RET_EVENT_TAKE_FAILED
Failed to take an event from the event handle.
Definition: types.h:121
#define RCL_RET_OK
Success return code.
Definition: types.h:27
#define RCL_RET_BAD_ALLOC
Failed to allocate memory return code.
Definition: types.h:33
#define RCL_RET_INVALID_ARGUMENT
Invalid argument return code.
Definition: types.h:35
#define RCL_RET_EVENT_INVALID
Invalid rcl_event_t given return code.
Definition: types.h:119
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:24