ROS 2 rclcpp + rcl - jazzy  jazzy
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  case RCL_PUBLISHER_INCOMPATIBLE_TYPE:
68  rmw_event_type = RMW_EVENT_PUBLISHER_INCOMPATIBLE_TYPE;
69  break;
70  case RCL_PUBLISHER_MATCHED:
71  rmw_event_type = RMW_EVENT_PUBLICATION_MATCHED;
72  break;
73  default:
74  RCL_SET_ERROR_MSG("Event type for publisher not supported");
76  }
77 
78  // Allocate space for the implementation struct.
79  event->impl = (rcl_event_impl_t *) allocator->allocate(
80  sizeof(rcl_event_impl_t), allocator->state);
81  RCL_CHECK_FOR_NULL_WITH_MSG(
82  event->impl, "allocating memory failed", return RCL_RET_BAD_ALLOC);
83 
84  event->impl->rmw_handle = rmw_get_zero_initialized_event();
85  event->impl->allocator = *allocator;
86 
87  rmw_ret_t ret = rmw_publisher_event_init(
88  &event->impl->rmw_handle,
89  publisher->impl->rmw_handle,
90  rmw_event_type);
91  if (ret != RMW_RET_OK) {
92  goto fail;
93  }
94 
95  return RCL_RET_OK;
96 fail:
97  allocator->deallocate(event->impl, allocator->state);
98  event->impl = NULL;
99  return rcl_convert_rmw_ret_to_rcl_ret(ret);
100 }
101 
102 rcl_ret_t
104  rcl_event_t * event,
105  const rcl_subscription_t * subscription,
106  const rcl_subscription_event_type_t event_type)
107 {
108  RCL_CHECK_ARGUMENT_FOR_NULL(event, RCL_RET_EVENT_INVALID);
109  // Check subscription and allocator first, so allocator can be used with errors.
110  RCL_CHECK_ARGUMENT_FOR_NULL(subscription, RCL_RET_INVALID_ARGUMENT);
111  rcl_allocator_t * allocator = &subscription->impl->options.allocator;
112  RCL_CHECK_ALLOCATOR_WITH_MSG(allocator, "invalid allocator", return RCL_RET_INVALID_ARGUMENT);
113  rmw_event_type_t rmw_event_type = RMW_EVENT_INVALID;
114  switch (event_type) {
115  case RCL_SUBSCRIPTION_REQUESTED_DEADLINE_MISSED:
116  rmw_event_type = RMW_EVENT_REQUESTED_DEADLINE_MISSED;
117  break;
118  case RCL_SUBSCRIPTION_LIVELINESS_CHANGED:
119  rmw_event_type = RMW_EVENT_LIVELINESS_CHANGED;
120  break;
121  case RCL_SUBSCRIPTION_REQUESTED_INCOMPATIBLE_QOS:
122  rmw_event_type = RMW_EVENT_REQUESTED_QOS_INCOMPATIBLE;
123  break;
124  case RCL_SUBSCRIPTION_MESSAGE_LOST:
125  rmw_event_type = RMW_EVENT_MESSAGE_LOST;
126  break;
127  case RCL_SUBSCRIPTION_INCOMPATIBLE_TYPE:
128  rmw_event_type = RMW_EVENT_SUBSCRIPTION_INCOMPATIBLE_TYPE;
129  break;
130  case RCL_SUBSCRIPTION_MATCHED:
131  rmw_event_type = RMW_EVENT_SUBSCRIPTION_MATCHED;
132  break;
133  default:
134  RCL_SET_ERROR_MSG("Event type for subscription not supported");
136  }
137 
138  // Allocate space for the implementation struct.
139  event->impl = (rcl_event_impl_t *) allocator->allocate(
140  sizeof(rcl_event_impl_t), allocator->state);
141  RCL_CHECK_FOR_NULL_WITH_MSG(
142  event->impl, "allocating memory failed", return RCL_RET_BAD_ALLOC);
143 
144  event->impl->rmw_handle = rmw_get_zero_initialized_event();
145  event->impl->allocator = *allocator;
146 
147  rmw_ret_t ret = rmw_subscription_event_init(
148  &event->impl->rmw_handle,
149  subscription->impl->rmw_handle,
150  rmw_event_type);
151  if (ret != RMW_RET_OK) {
152  goto fail;
153  }
154 
155  return RCL_RET_OK;
156 fail:
157  allocator->deallocate(event->impl, allocator->state);
158  event->impl = NULL;
159  return rcl_convert_rmw_ret_to_rcl_ret(ret);
160 }
161 
162 rcl_ret_t
164  const rcl_event_t * event,
165  void * event_info)
166 {
167  bool taken = false;
168  if (!rcl_event_is_valid(event)) {
169  return RCL_RET_EVENT_INVALID;
170  }
171  RCL_CHECK_ARGUMENT_FOR_NULL(event_info, RCL_RET_INVALID_ARGUMENT);
172  rmw_ret_t ret = rmw_take_event(&event->impl->rmw_handle, event_info, &taken);
173  if (RMW_RET_OK != ret) {
174  RCL_SET_ERROR_MSG(rmw_get_error_string().str);
175  return rcl_convert_rmw_ret_to_rcl_ret(ret);
176  }
177  if (!taken) {
178  RCUTILS_LOG_DEBUG_NAMED(
179  ROS_PACKAGE_NAME, "take_event request complete, unable to take event");
181  }
182  RCUTILS_LOG_DEBUG_NAMED(
183  ROS_PACKAGE_NAME, "take_event request success");
184  return rcl_convert_rmw_ret_to_rcl_ret(ret);
185 }
186 
187 rcl_ret_t
189 {
190  rcl_ret_t result = RCL_RET_OK;
191  RCL_CHECK_ARGUMENT_FOR_NULL(event, RCL_RET_EVENT_INVALID);
192 
193  RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME, "Finalizing event");
194  if (NULL != event->impl) {
195  rcl_allocator_t allocator = event->impl->allocator;
196  rmw_ret_t ret = rmw_event_fini(&event->impl->rmw_handle);
197  if (ret != RMW_RET_OK) {
198  RCL_SET_ERROR_MSG(rmw_get_error_string().str);
199  result = rcl_convert_rmw_ret_to_rcl_ret(ret);
200  }
201  allocator.deallocate(event->impl, allocator.state);
202  event->impl = NULL;
203  }
204  RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME, "Event finalized");
205 
206  return result;
207 }
208 
209 rmw_event_t *
211 {
212  if (!rcl_event_is_valid(event)) {
213  return NULL; // error already set
214  } else {
215  return &event->impl->rmw_handle;
216  }
217 }
218 
219 bool
221 {
222  RCL_CHECK_FOR_NULL_WITH_MSG(event, "event pointer is invalid", return false);
223  RCL_CHECK_FOR_NULL_WITH_MSG(event->impl, "event's implementation is invalid", return false);
224  if (event->impl->rmw_handle.event_type == RMW_EVENT_INVALID) {
225  RCUTILS_SET_ERROR_MSG("event's implementation not init");
226  return false;
227  }
228  RCUTILS_CHECK_ALLOCATOR_WITH_MSG(
229  &event->impl->allocator, "not valid allocator", return false);
230  return true;
231 }
232 
233 rcl_ret_t
235  const rcl_event_t * event,
236  rcl_event_callback_t callback,
237  const void * user_data)
238 {
239  if (!rcl_event_is_valid(event)) {
240  // error state already set
242  }
243 
244  return rmw_event_set_callback(
245  &event->impl->rmw_handle,
246  callback,
247  user_data);
248 }
249 
250 #ifdef __cplusplus
251 }
252 #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:163
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:234
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:210
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:103
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_event_fini(rcl_event_t *event)
Definition: event.c:188
RCL_PUBLIC bool rcl_event_is_valid(const rcl_event_t *event)
Check that the event is valid.
Definition: event.c:220
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: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