25 #include "rcl/error_handling.h"
27 #include "rcl/node_type_cache.h"
30 #include "rcutils/logging_macros.h"
31 #include "rcutils/macros.h"
32 #include "rcutils/stdatomic_helper.h"
33 #include "rmw/error_handling.h"
35 #include "service_msgs/msg/service_event_info.h"
36 #include "tracetools/tracetools.h"
38 #include "rosidl_runtime_c/service_type_support_struct.h"
41 #include "./service_event_publisher.h"
46 rmw_qos_profile_t actual_request_publisher_qos;
47 rmw_qos_profile_t actual_response_subscription_qos;
48 rmw_client_t * rmw_handle;
49 atomic_int_least64_t sequence_number;
51 char * remapped_service_name;
52 rosidl_type_hash_t type_hash;
64 unconfigure_service_introspection(
69 if (client_impl == NULL) {
73 if (client_impl->service_event_publisher == NULL) {
77 rcl_ret_t ret = rcl_service_event_publisher_fini(client_impl->service_event_publisher, node);
79 allocator->deallocate(client_impl->service_event_publisher, allocator->state);
80 client_impl->service_event_publisher = NULL;
89 const rosidl_service_type_support_t * type_support,
90 const char * service_name,
103 RCUTILS_LOG_DEBUG_NAMED(
104 ROS_PACKAGE_NAME,
"Initializing client for service name '%s'", service_name);
106 RCL_SET_ERROR_MSG(
"client already initialized, or memory was unintialized");
113 RCL_CHECK_FOR_NULL_WITH_MSG(
114 client->
impl,
"allocating memory failed",
124 &client->
impl->remapped_service_name);
131 goto free_client_impl;
133 RCUTILS_LOG_DEBUG_NAMED(
134 ROS_PACKAGE_NAME,
"Expanded and remapped service name '%s'",
135 client->
impl->remapped_service_name);
140 client->
impl->rmw_handle = rmw_create_client(
143 client->
impl->remapped_service_name,
145 if (!client->
impl->rmw_handle) {
146 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
148 goto free_remapped_service_name;
152 rmw_ret_t rmw_ret = rmw_client_request_publisher_get_actual_qos(
153 client->
impl->rmw_handle,
154 &client->
impl->actual_request_publisher_qos);
155 if (RMW_RET_OK != rmw_ret) {
156 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
157 ret = rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);
161 rmw_ret = rmw_client_response_subscription_get_actual_qos(
162 client->
impl->rmw_handle,
163 &client->
impl->actual_response_subscription_qos);
164 if (RMW_RET_OK != rmw_ret) {
165 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
166 ret = rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);
172 client->
impl->actual_request_publisher_qos.avoid_ros_namespace_conventions =
173 options->
qos.avoid_ros_namespace_conventions;
174 client->
impl->actual_response_subscription_qos.avoid_ros_namespace_conventions =
175 options->
qos.avoid_ros_namespace_conventions;
178 client->
impl->options = *options;
179 atomic_init(&client->
impl->sequence_number, 0);
181 const rosidl_type_hash_t * hash = type_support->get_type_hash_func(type_support);
183 RCL_SET_ERROR_MSG(
"Failed to get the type hash");
188 if (
RCL_RET_OK != rcl_node_type_cache_register_type(
189 node, hash, type_support->get_type_description_func(type_support),
190 type_support->get_type_description_sources_func(type_support)))
192 rcutils_reset_error();
193 RCL_SET_ERROR_MSG(
"Failed to register type for client");
197 client->
impl->type_hash = *hash;
199 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Client initialized");
200 TRACETOOLS_TRACEPOINT(
202 (
const void *)client,
204 (
const void *)client->
impl->rmw_handle,
205 client->
impl->remapped_service_name);
211 if (RMW_RET_OK != rmw_ret) {
212 RCUTILS_SAFE_FWRITE_TO_STDERR(rmw_get_error_string().str);
213 RCUTILS_SAFE_FWRITE_TO_STDERR(
"\n");
216 free_remapped_service_name:
217 allocator->deallocate(client->
impl->remapped_service_name, allocator->state);
218 client->
impl->remapped_service_name = NULL;
221 allocator->deallocate(client->
impl, allocator->state);
234 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Finalizing client");
248 rcl_ret_t rcl_ret = unconfigure_service_introspection(node, client->
impl, &allocator);
250 RCL_SET_ERROR_MSG(rcl_get_error_string().str);
254 rmw_ret_t ret = rmw_destroy_client(rmw_node, client->
impl->rmw_handle);
255 if (ret != RMW_RET_OK) {
256 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
261 ROSIDL_TYPE_HASH_VERSION_UNSET != client->
impl->type_hash.version &&
262 RCL_RET_OK != rcl_node_type_cache_unregister_type(node, &client->
impl->type_hash))
264 RCUTILS_SAFE_FWRITE_TO_STDERR(rcl_get_error_string().str);
268 allocator.deallocate(client->
impl->remapped_service_name, allocator.state);
269 client->
impl->remapped_service_name = NULL;
271 allocator.deallocate(client->
impl, allocator.state);
274 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Client finalized");
284 default_options.
qos = rmw_qos_profile_services_default;
286 return default_options;
295 return client->
impl->rmw_handle->service_name;
304 return &client->
impl->options;
313 return client->
impl->rmw_handle;
319 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Client sending service request");
325 *sequence_number = rcutils_atomic_load_int64_t(&client->
impl->sequence_number);
326 if (rmw_send_request(
327 client->
impl->rmw_handle, ros_request, sequence_number) != RMW_RET_OK)
329 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
332 rcutils_atomic_exchange_int64_t(&client->
impl->sequence_number, *sequence_number);
334 if (client->
impl->service_event_publisher != NULL) {
336 rmw_ret_t rmw_ret = rmw_get_gid_for_client(client->
impl->rmw_handle, &gid);
337 if (rmw_ret != RMW_RET_OK) {
338 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
339 return rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);
341 rcl_ret_t ret = rcl_send_service_event_message(
342 client->
impl->service_event_publisher,
343 service_msgs__msg__ServiceEventInfo__REQUEST_SENT,
348 RCL_SET_ERROR_MSG(rcl_get_error_string().str);
358 rmw_service_info_t * request_header,
361 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Client taking service response");
370 request_header->source_timestamp = 0;
371 request_header->received_timestamp = 0;
372 if (rmw_take_response(
373 client->
impl->rmw_handle, request_header, ros_response, &taken) != RMW_RET_OK)
375 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
378 RCUTILS_LOG_DEBUG_NAMED(
379 ROS_PACKAGE_NAME,
"Client take response succeeded: %s", taken ?
"true" :
"false");
384 if (client->
impl->service_event_publisher != NULL) {
386 rmw_ret_t rmw_ret = rmw_get_gid_for_client(client->
impl->rmw_handle, &gid);
387 if (rmw_ret != RMW_RET_OK) {
388 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
389 return rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);
391 rcl_ret_t ret = rcl_send_service_event_message(
392 client->
impl->service_event_publisher,
393 service_msgs__msg__ServiceEventInfo__RESPONSE_RECEIVED,
395 request_header->request_id.sequence_number,
398 RCL_SET_ERROR_MSG(rcl_get_error_string().str);
408 rmw_request_id_t * request_header,
411 rmw_service_info_t header;
412 header.request_id = *request_header;
414 *request_header = header.request_id;
421 RCL_CHECK_FOR_NULL_WITH_MSG(client,
"client pointer is invalid",
return false);
422 RCL_CHECK_FOR_NULL_WITH_MSG(
423 client->
impl,
"client's rmw implementation is invalid",
return false);
424 RCL_CHECK_FOR_NULL_WITH_MSG(
425 client->
impl->rmw_handle,
"client's rmw handle is invalid",
return false);
429 const rmw_qos_profile_t *
435 return &client->
impl->actual_request_publisher_qos;
438 const rmw_qos_profile_t *
444 return &client->
impl->actual_response_subscription_qos;
450 rcl_event_callback_t callback,
451 const void * user_data)
458 return rmw_client_set_on_new_response_callback(
459 client->
impl->rmw_handle,
469 const rosidl_service_type_support_t * type_support,
471 rcl_service_introspection_state_t introspection_state)
482 if (introspection_state == RCL_SERVICE_INTROSPECTION_OFF) {
483 return unconfigure_service_introspection(node, client->
impl, &allocator);
486 if (client->
impl->service_event_publisher == NULL) {
489 client->
impl->service_event_publisher = allocator.allocate(
491 RCL_CHECK_FOR_NULL_WITH_MSG(
494 *client->
impl->service_event_publisher = rcl_get_zero_initialized_service_event_publisher();
495 rcl_ret_t ret = rcl_service_event_publisher_init(
496 client->
impl->service_event_publisher, node, clock, publisher_options,
497 client->
impl->remapped_service_name, type_support);
499 allocator.deallocate(client->
impl->service_event_publisher, allocator.state);
500 client->
impl->service_event_publisher = NULL;
505 return rcl_service_event_publisher_change_state(
506 client->
impl->service_event_publisher, introspection_state);
#define rcl_get_default_allocator
Return a properly initialized rcl_allocator_t with default values.
#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_response_with_info(const rcl_client_t *client, rmw_service_info_t *request_header, void *ros_response)
Take a ROS response using a client.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_client_configure_service_introspection(rcl_client_t *client, rcl_node_t *node, rcl_clock_t *clock, const rosidl_service_type_support_t *type_support, const rcl_publisher_options_t publisher_options, rcl_service_introspection_state_t introspection_state)
Configures service introspection features for the client.
RCL_PUBLIC RCL_WARN_UNUSED const rmw_qos_profile_t * rcl_client_request_publisher_get_actual_qos(const rcl_client_t *client)
Get the actual qos settings of the client's request publisher.
RCL_PUBLIC RCL_WARN_UNUSED const char * rcl_client_get_service_name(const rcl_client_t *client)
Get the name of the service that this client will request a response from.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_client_fini(rcl_client_t *client, rcl_node_t *node)
Finalize a rcl_client_t.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_client_init(rcl_client_t *client, const rcl_node_t *node, const rosidl_service_type_support_t *type_support, const char *service_name, const rcl_client_options_t *options)
Initialize a rcl client.
RCL_PUBLIC RCL_WARN_UNUSED rcl_client_options_t rcl_client_get_default_options(void)
Return the default client options in a rcl_client_options_t.
RCL_PUBLIC RCL_WARN_UNUSED rmw_client_t * rcl_client_get_rmw_handle(const rcl_client_t *client)
Return the rmw client handle.
RCL_PUBLIC RCL_WARN_UNUSED rcl_client_t rcl_get_zero_initialized_client(void)
Return a rcl_client_t struct with members set to NULL.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_take_response(const rcl_client_t *client, rmw_request_id_t *request_header, void *ros_response)
backwards compatibility function that takes a rmw_request_id_t only
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_client_set_on_new_response_callback(const rcl_client_t *client, rcl_event_callback_t callback, const void *user_data)
Set the on new response callback function for the client.
RCL_PUBLIC bool rcl_client_is_valid(const rcl_client_t *client)
Check that the client is valid.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_send_request(const rcl_client_t *client, const void *ros_request, int64_t *sequence_number)
Send a ROS request using a client.
RCL_PUBLIC RCL_WARN_UNUSED const rmw_qos_profile_t * rcl_client_response_subscription_get_actual_qos(const rcl_client_t *client)
Get the actual qos settings of the client's response subscription.
RCL_PUBLIC RCL_WARN_UNUSED const rcl_client_options_t * rcl_client_get_options(const rcl_client_t *client)
Return the rcl client options.
RCL_PUBLIC bool rcl_node_is_valid(const rcl_node_t *node)
Return true if the node is valid, else false.
RCL_PUBLIC RCL_WARN_UNUSED rmw_node_t * rcl_node_get_rmw_handle(const rcl_node_t *node)
Return the rmw node handle.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_node_resolve_name(const rcl_node_t *node, const char *input_name, rcl_allocator_t allocator, bool is_service, bool only_expand, char **output_name)
Expand a given name into a fully-qualified topic name and apply remapping rules.
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.
Options available for a rcl_client_t.
rcl_allocator_t allocator
Custom allocator for the client, used for incidental allocations.
rmw_qos_profile_t qos
Middleware quality of service settings for the client.
Structure which encapsulates a ROS Client.
rcl_client_impl_t * impl
Pointer to the client implementation.
Encapsulation of a time source.
Structure which encapsulates a ROS Node.
Options available for a rcl publisher.
#define RCL_RET_CLIENT_INVALID
Invalid rcl_client_t given return code.
#define RCL_RET_CLIENT_TAKE_FAILED
Failed to take a response from the client return code.
#define RCL_RET_UNKNOWN_SUBSTITUTION
Topic name substitution is unknown.
#define RCL_RET_SERVICE_NAME_INVALID
Service name (same as topic name) does not pass validation.
#define RCL_RET_ALREADY_INIT
rcl_init() already called return code.
#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_ERROR
Unspecified error return code.
#define RCL_RET_NODE_INVALID
Invalid rcl_node_t given return code.
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.