ROS 2 rclcpp + rcl - jazzy  jazzy
ROS 2 C++ Client Library with ROS Client Library
service_event_publisher.c
1 // Copyright 2022 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 #include "rcl/service_event_publisher.h"
16 
17 #include <string.h>
18 
19 #include "rcl/allocator.h"
20 #include "rcl/macros.h"
21 #include "rcl/error_handling.h"
22 #include "rcl/publisher.h"
23 #include "rcl/node.h"
24 #include "rcl/service_introspection.h"
25 #include "rcl/time.h"
26 #include "rcl/types.h"
27 #include "rcutils/logging_macros.h"
28 #include "rcutils/macros.h"
29 #include "rmw/error_handling.h"
30 #include "service_msgs/msg/service_event_info.h"
31 
32 rcl_service_event_publisher_t rcl_get_zero_initialized_service_event_publisher(void)
33 {
34  static rcl_service_event_publisher_t zero_service_event_publisher = {0};
35  return zero_service_event_publisher;
36 }
37 
38 bool
39 rcl_service_event_publisher_is_valid(const rcl_service_event_publisher_t * service_event_publisher)
40 {
41  RCL_CHECK_FOR_NULL_WITH_MSG(
42  service_event_publisher, "service_event_publisher is invalid", return false);
43  RCL_CHECK_FOR_NULL_WITH_MSG(
44  service_event_publisher->service_type_support,
45  "service_event_publisher's service type support is invalid", return false);
46  if (!rcl_clock_valid(service_event_publisher->clock)) {
47  RCL_SET_ERROR_MSG("service_event_publisher's clock is invalid");
48  return false;
49  }
50  return true;
51 }
52 
53 static rcl_ret_t introspection_create_publisher(
54  rcl_service_event_publisher_t * service_event_publisher,
55  const rcl_node_t * node)
56 {
57  rcl_allocator_t allocator = service_event_publisher->publisher_options.allocator;
58  RCL_CHECK_ALLOCATOR_WITH_MSG(&allocator, "allocator is invalid", return RCL_RET_ERROR);
59 
60  service_event_publisher->publisher = allocator.allocate(
61  sizeof(rcl_publisher_t), allocator.state);
62  RCL_CHECK_FOR_NULL_WITH_MSG(
63  service_event_publisher->publisher,
64  "allocate service_event_publisher failed in enable", return RCL_RET_BAD_ALLOC);
65  *service_event_publisher->publisher = rcl_get_zero_initialized_publisher();
67  service_event_publisher->publisher, node,
68  service_event_publisher->service_type_support->event_typesupport,
69  service_event_publisher->service_event_topic_name,
70  &service_event_publisher->publisher_options);
71  if (RCL_RET_OK != ret) {
72  allocator.deallocate(service_event_publisher->publisher, allocator.state);
73  service_event_publisher->publisher = NULL;
74  rcutils_reset_error();
75  RCL_SET_ERROR_MSG(rcl_get_error_string().str);
76  return ret;
77  }
78 
79  return RCL_RET_OK;
80 }
81 
82 rcl_ret_t rcl_service_event_publisher_init(
83  rcl_service_event_publisher_t * service_event_publisher,
84  const rcl_node_t * node,
85  rcl_clock_t * clock,
86  const rcl_publisher_options_t publisher_options,
87  const char * service_name,
88  const rosidl_service_type_support_t * service_type_support)
89 {
90  RCUTILS_CAN_RETURN_WITH_ERROR_OF(RCL_RET_INVALID_ARGUMENT);
91  RCUTILS_CAN_RETURN_WITH_ERROR_OF(RCL_RET_ALREADY_INIT);
92  RCUTILS_CAN_RETURN_WITH_ERROR_OF(RCL_RET_NODE_INVALID);
93  RCUTILS_CAN_RETURN_WITH_ERROR_OF(RCL_RET_BAD_ALLOC);
94  RCUTILS_CAN_RETURN_WITH_ERROR_OF(RCL_RET_ERROR);
95  RCUTILS_CAN_RETURN_WITH_ERROR_OF(RCL_RET_TOPIC_NAME_INVALID);
96  RCUTILS_CAN_RETURN_WITH_ERROR_OF(RCL_RET_PUBLISHER_INVALID);
97 
98  RCL_CHECK_ARGUMENT_FOR_NULL(service_event_publisher, RCL_RET_INVALID_ARGUMENT);
99  RCL_CHECK_ARGUMENT_FOR_NULL(node, RCL_RET_INVALID_ARGUMENT);
100  RCL_CHECK_ARGUMENT_FOR_NULL(service_name, RCL_RET_INVALID_ARGUMENT);
101  RCL_CHECK_ARGUMENT_FOR_NULL(service_type_support, RCL_RET_INVALID_ARGUMENT);
102 
104  &publisher_options.allocator,
105  "allocator is invalid", return RCL_RET_ERROR);
106 
107  rcl_allocator_t allocator = publisher_options.allocator;
108 
109  rcl_ret_t ret = RCL_RET_OK;
110 
111  if (!rcl_node_is_valid(node)) {
112  return RCL_RET_NODE_INVALID;
113  }
114 
115  if (!rcl_clock_valid(clock)) {
116  rcutils_reset_error();
117  RCL_SET_ERROR_MSG("clock is invalid");
118  return RCL_RET_ERROR;
119  }
120 
121  RCUTILS_LOG_DEBUG_NAMED(
122  ROS_PACKAGE_NAME, "Initializing service introspection for service name '%s'", service_name);
123 
124  // Typesupports have static lifetimes
125  service_event_publisher->service_type_support = service_type_support;
126  service_event_publisher->clock = clock;
127  service_event_publisher->publisher_options = publisher_options;
128 
129  size_t topic_length = strlen(service_name) + strlen(RCL_SERVICE_INTROSPECTION_TOPIC_POSTFIX) + 1;
130  service_event_publisher->service_event_topic_name = (char *) allocator.allocate(
131  topic_length, allocator.state);
132  RCL_CHECK_FOR_NULL_WITH_MSG(
133  service_event_publisher->service_event_topic_name,
134  "allocating memory for service introspection topic name failed",
135  return RCL_RET_BAD_ALLOC;);
136 
137  snprintf(
138  service_event_publisher->service_event_topic_name,
139  topic_length,
140  "%s%s", service_name, RCL_SERVICE_INTROSPECTION_TOPIC_POSTFIX);
141 
142  ret = introspection_create_publisher(service_event_publisher, node);
143  if (ret != RCL_RET_OK) {
144  goto free_topic_name;
145  }
146 
147  RCUTILS_LOG_DEBUG_NAMED(
148  ROS_PACKAGE_NAME, "Service introspection for service '%s' initialized", service_name);
149 
150  return RCL_RET_OK;
151 
152 free_topic_name:
153  allocator.deallocate(service_event_publisher->service_event_topic_name, allocator.state);
154 
155  return ret;
156 }
157 
158 rcl_ret_t rcl_service_event_publisher_fini(
159  rcl_service_event_publisher_t * service_event_publisher,
160  rcl_node_t * node)
161 {
162  RCUTILS_CAN_RETURN_WITH_ERROR_OF(RCL_RET_INVALID_ARGUMENT);
163  RCUTILS_CAN_RETURN_WITH_ERROR_OF(RCL_RET_ALREADY_SHUTDOWN);
164  RCUTILS_CAN_RETURN_WITH_ERROR_OF(RCL_RET_NODE_INVALID);
165  RCUTILS_CAN_RETURN_WITH_ERROR_OF(RCL_RET_PUBLISHER_INVALID);
166  RCUTILS_CAN_RETURN_WITH_ERROR_OF(RCL_RET_ERROR);
167  RCUTILS_CAN_RETURN_WITH_ERROR_OF(RCL_RET_BAD_ALLOC);
168 
169  if (!rcl_service_event_publisher_is_valid(service_event_publisher)) {
170  return RCL_RET_ERROR;
171  }
172 
174  return RCL_RET_NODE_INVALID;
175  }
176 
177  rcl_allocator_t allocator = service_event_publisher->publisher_options.allocator;
178  RCL_CHECK_ALLOCATOR_WITH_MSG(&allocator, "allocator is invalid", return RCL_RET_ERROR);
179 
180  if (service_event_publisher->publisher) {
181  rcl_ret_t ret = rcl_publisher_fini(service_event_publisher->publisher, node);
182  allocator.deallocate(service_event_publisher->publisher, allocator.state);
183  service_event_publisher->publisher = NULL;
184  if (RCL_RET_OK != ret) {
185  return ret;
186  }
187  }
188 
189  allocator.deallocate(service_event_publisher->service_event_topic_name, allocator.state);
190  service_event_publisher->service_event_topic_name = NULL;
191 
192  return RCL_RET_OK;
193 }
194 
195 rcl_ret_t rcl_send_service_event_message(
196  const rcl_service_event_publisher_t * service_event_publisher,
197  const uint8_t event_type,
198  const void * ros_response_request,
199  const int64_t sequence_number,
200  const uint8_t guid[16])
201 {
202  RCUTILS_CAN_RETURN_WITH_ERROR_OF(RCL_RET_INVALID_ARGUMENT);
203  RCUTILS_CAN_RETURN_WITH_ERROR_OF(RCL_RET_PUBLISHER_INVALID);
204  RCUTILS_CAN_RETURN_WITH_ERROR_OF(RCL_RET_ERROR);
205  RCUTILS_CAN_RETURN_WITH_ERROR_OF(RCL_RET_BAD_ALLOC);
206 
207  RCL_CHECK_ARGUMENT_FOR_NULL(ros_response_request, RCL_RET_INVALID_ARGUMENT);
208  RCL_CHECK_FOR_NULL_WITH_MSG(guid, "guid is NULL", return RCL_RET_INVALID_ARGUMENT);
209 
210  if (!rcl_service_event_publisher_is_valid(service_event_publisher)) {
211  return RCL_RET_ERROR;
212  }
213 
214  if (service_event_publisher->introspection_state == RCL_SERVICE_INTROSPECTION_OFF) {
215  return RCL_RET_ERROR;
216  }
217 
218  rcl_allocator_t allocator = service_event_publisher->publisher_options.allocator;
219  RCL_CHECK_ALLOCATOR_WITH_MSG(&allocator, "invalid allocator", return RCL_RET_INVALID_ARGUMENT);
220 
221  if (!rcl_publisher_is_valid(service_event_publisher->publisher)) {
223  }
224 
225  rcl_ret_t ret;
226 
228  ret = rcl_clock_get_now(service_event_publisher->clock, &now);
229  if (RMW_RET_OK != ret) {
230  rcutils_reset_error();
231  RCL_SET_ERROR_MSG(rmw_get_error_string().str);
232  return RCL_RET_ERROR;
233  }
234 
235  rosidl_service_introspection_info_t info = {
236  .event_type = event_type,
237  .stamp_sec = (int32_t)RCL_NS_TO_S(now),
238  .stamp_nanosec = now % (1000LL * 1000LL * 1000LL),
239  .sequence_number = sequence_number,
240  };
241 
242  memcpy(info.client_gid, guid, 16);
243 
244  void * service_introspection_message;
245  if (service_event_publisher->introspection_state == RCL_SERVICE_INTROSPECTION_METADATA) {
246  ros_response_request = NULL;
247  }
248  switch (event_type) {
249  case service_msgs__msg__ServiceEventInfo__REQUEST_RECEIVED:
250  case service_msgs__msg__ServiceEventInfo__REQUEST_SENT:
251  service_introspection_message =
252  service_event_publisher->service_type_support->event_message_create_handle_function(
253  &info, &allocator, ros_response_request, NULL);
254  break;
255  case service_msgs__msg__ServiceEventInfo__RESPONSE_RECEIVED:
256  case service_msgs__msg__ServiceEventInfo__RESPONSE_SENT:
257  service_introspection_message =
258  service_event_publisher->service_type_support->event_message_create_handle_function(
259  &info, &allocator, NULL, ros_response_request);
260  break;
261  default:
262  rcutils_reset_error();
263  RCL_SET_ERROR_MSG("unsupported event type");
264  return RCL_RET_ERROR;
265  }
266  RCL_CHECK_FOR_NULL_WITH_MSG(
267  service_introspection_message, "service_introspection_message is NULL", return RCL_RET_ERROR);
268 
269  // and publish it out!
270  ret = rcl_publish(service_event_publisher->publisher, service_introspection_message, NULL);
271  // clean up before error checking
272  service_event_publisher->service_type_support->event_message_destroy_handle_function(
273  service_introspection_message, &allocator);
274  if (RCL_RET_OK != ret) {
275  rcutils_reset_error();
276  RCL_SET_ERROR_MSG(rmw_get_error_string().str);
277  }
278 
279  return ret;
280 }
281 
282 rcl_ret_t
283 rcl_service_event_publisher_change_state(
284  rcl_service_event_publisher_t * service_event_publisher,
285  rcl_service_introspection_state_t introspection_state)
286 {
287  if (!rcl_service_event_publisher_is_valid(service_event_publisher)) {
288  return RCL_RET_ERROR;
289  }
290 
291  service_event_publisher->introspection_state = introspection_state;
292 
293  return RCL_RET_OK;
294 }
#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 bool rcl_node_is_valid(const rcl_node_t *node)
Return true if the node is valid, else false.
Definition: node.c:397
RCL_PUBLIC bool rcl_node_is_valid_except_context(const rcl_node_t *node)
Return true if node is valid, except for the context being valid.
Definition: node.c:387
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_publisher_init(rcl_publisher_t *publisher, const rcl_node_t *node, const rosidl_message_type_support_t *type_support, const char *topic_name, const rcl_publisher_options_t *options)
Initialize a rcl publisher.
Definition: publisher.c:47
RCL_PUBLIC bool rcl_publisher_is_valid(const rcl_publisher_t *publisher)
Return true if the publisher is valid, otherwise false.
Definition: publisher.c:417
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_publish(const rcl_publisher_t *publisher, const void *ros_message, rmw_publisher_allocation_t *allocation)
Publish a ROS message on a topic using a publisher.
Definition: publisher.c:270
RCL_PUBLIC RCL_WARN_UNUSED rcl_publisher_t rcl_get_zero_initialized_publisher(void)
Return a rcl_publisher_t struct with members set to NULL.
Definition: publisher.c:40
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_publisher_fini(rcl_publisher_t *publisher, rcl_node_t *node)
Finalize a rcl_publisher_t.
Definition: publisher.c:178
Encapsulation of a time source.
Definition: time.h:138
Structure which encapsulates a ROS Node.
Definition: node.h:45
Options available for a rcl publisher.
Definition: publisher.h:44
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
char * service_event_topic_name
Name of service introspection topic: <service_name>/<RCL_SERVICE_INTROSPECTION_TOPIC_POSTFIX>
const rosidl_service_type_support_t * service_type_support
Handle to service typesupport.
rcl_publisher_options_t publisher_options
Publisher options for service event publisher.
rcl_service_introspection_state_t introspection_state
Current state of introspection; off, metadata, or contents.
rcl_publisher_t * publisher
Handle to publisher for publishing service events.
rcl_clock_t * clock
Handle to clock for timestamping service events.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_clock_get_now(rcl_clock_t *clock, rcl_time_point_value_t *time_point_value)
Fill the time point value with the current value of the associated clock.
Definition: time.c:263
#define RCL_NS_TO_S
Convenience macro to convert nanoseconds to seconds.
Definition: time.h:39
rcutils_time_point_value_t rcl_time_point_value_t
A single point in time, measured in nanoseconds since the Unix epoch.
Definition: time.h:46
RCL_PUBLIC RCL_WARN_UNUSED bool rcl_clock_valid(rcl_clock_t *clock)
Check if the clock has valid values.
Definition: time.c:86
#define RCL_RET_ALREADY_INIT
rcl_init() already called return code.
Definition: types.h:41
#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_ERROR
Unspecified error return code.
Definition: types.h:29
#define RCL_RET_NODE_INVALID
Invalid rcl_node_t given return code.
Definition: types.h:59
#define RCL_RET_TOPIC_NAME_INVALID
Topic name does not pass validation.
Definition: types.h:47
#define RCL_RET_ALREADY_SHUTDOWN
rcl_shutdown() already called return code.
Definition: types.h:53
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:24
#define RCL_RET_PUBLISHER_INVALID
Invalid rcl_publisher_t given return code.
Definition: types.h:69