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 "./client_impl.h"
42 #include "./service_event_publisher.h"
54 unconfigure_service_introspection(
59 if (client_impl == NULL) {
63 if (client_impl->service_event_publisher == NULL) {
67 rcl_ret_t ret = rcl_service_event_publisher_fini(client_impl->service_event_publisher, node);
69 allocator->deallocate(client_impl->service_event_publisher, allocator->state);
70 client_impl->service_event_publisher = NULL;
79 const rosidl_service_type_support_t * type_support,
80 const char * service_name,
93 RCUTILS_LOG_DEBUG_NAMED(
94 ROS_PACKAGE_NAME,
"Initializing client for service name '%s'", service_name);
96 RCL_SET_ERROR_MSG(
"client already initialized, or memory was unintialized");
103 RCL_CHECK_FOR_NULL_WITH_MSG(
104 client->
impl,
"allocating memory failed",
114 &client->
impl->remapped_service_name);
121 goto free_client_impl;
123 RCUTILS_LOG_DEBUG_NAMED(
124 ROS_PACKAGE_NAME,
"Expanded and remapped service name '%s'",
125 client->
impl->remapped_service_name);
130 client->
impl->rmw_handle = rmw_create_client(
133 client->
impl->remapped_service_name,
135 if (!client->
impl->rmw_handle) {
136 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
138 goto free_remapped_service_name;
142 rmw_ret_t rmw_ret = rmw_client_request_publisher_get_actual_qos(
143 client->
impl->rmw_handle,
144 &client->
impl->actual_request_publisher_qos);
145 if (RMW_RET_OK != rmw_ret) {
146 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
147 ret = rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);
151 rmw_ret = rmw_client_response_subscription_get_actual_qos(
152 client->
impl->rmw_handle,
153 &client->
impl->actual_response_subscription_qos);
154 if (RMW_RET_OK != rmw_ret) {
155 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
156 ret = rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);
162 client->
impl->actual_request_publisher_qos.avoid_ros_namespace_conventions =
163 options->
qos.avoid_ros_namespace_conventions;
164 client->
impl->actual_response_subscription_qos.avoid_ros_namespace_conventions =
165 options->
qos.avoid_ros_namespace_conventions;
168 client->
impl->options = *options;
169 atomic_init(&client->
impl->sequence_number, 0);
170 client->
impl->in_use_by_waitset =
false;
172 const rosidl_type_hash_t * hash = type_support->get_type_hash_func(type_support);
174 RCL_SET_ERROR_MSG(
"Failed to get the type hash");
179 if (
RCL_RET_OK != rcl_node_type_cache_register_type(
180 node, hash, type_support->get_type_description_func(type_support),
181 type_support->get_type_description_sources_func(type_support)))
183 rcutils_reset_error();
184 RCL_SET_ERROR_MSG(
"Failed to register type for client");
188 client->
impl->type_hash = *hash;
190 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Client initialized");
191 TRACETOOLS_TRACEPOINT(
193 (
const void *)client,
195 (
const void *)client->
impl->rmw_handle,
196 client->
impl->remapped_service_name);
202 if (RMW_RET_OK != rmw_ret) {
203 RCUTILS_SAFE_FWRITE_TO_STDERR(rmw_get_error_string().str);
204 RCUTILS_SAFE_FWRITE_TO_STDERR(
"\n");
207 free_remapped_service_name:
208 allocator->deallocate(client->
impl->remapped_service_name, allocator->state);
209 client->
impl->remapped_service_name = NULL;
212 allocator->deallocate(client->
impl, allocator->state);
225 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Finalizing client");
239 rcl_ret_t rcl_ret = unconfigure_service_introspection(node, client->
impl, &allocator);
241 RCL_SET_ERROR_MSG(rcl_get_error_string().str);
245 rmw_ret_t ret = rmw_destroy_client(rmw_node, client->
impl->rmw_handle);
246 if (ret != RMW_RET_OK) {
247 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
252 ROSIDL_TYPE_HASH_VERSION_UNSET != client->
impl->type_hash.version &&
253 RCL_RET_OK != rcl_node_type_cache_unregister_type(node, &client->
impl->type_hash))
255 RCUTILS_SAFE_FWRITE_TO_STDERR(rcl_get_error_string().str);
259 allocator.deallocate(client->
impl->remapped_service_name, allocator.state);
260 client->
impl->remapped_service_name = NULL;
262 allocator.deallocate(client->
impl, allocator.state);
265 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Client finalized");
275 default_options.
qos = rmw_qos_profile_services_default;
277 return default_options;
286 return client->
impl->rmw_handle->service_name;
295 return &client->
impl->options;
304 return client->
impl->rmw_handle;
310 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Client sending service request");
316 *sequence_number = rcutils_atomic_load_int64_t(&client->
impl->sequence_number);
317 if (rmw_send_request(
318 client->
impl->rmw_handle, ros_request, sequence_number) != RMW_RET_OK)
320 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
323 rcutils_atomic_exchange_int64_t(&client->
impl->sequence_number, *sequence_number);
325 if (client->
impl->service_event_publisher != NULL) {
327 rmw_ret_t rmw_ret = rmw_get_gid_for_client(client->
impl->rmw_handle, &gid);
328 if (rmw_ret != RMW_RET_OK) {
329 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
330 return rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);
332 rcl_ret_t ret = rcl_send_service_event_message(
333 client->
impl->service_event_publisher,
334 service_msgs__msg__ServiceEventInfo__REQUEST_SENT,
339 RCL_SET_ERROR_MSG(rcl_get_error_string().str);
349 rmw_service_info_t * request_header,
352 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Client taking service response");
361 request_header->source_timestamp = 0;
362 request_header->received_timestamp = 0;
363 if (rmw_take_response(
364 client->
impl->rmw_handle, request_header, ros_response, &taken) != RMW_RET_OK)
366 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
369 RCUTILS_LOG_DEBUG_NAMED(
370 ROS_PACKAGE_NAME,
"Client take response succeeded: %s", taken ?
"true" :
"false");
375 if (client->
impl->service_event_publisher != NULL) {
377 rmw_ret_t rmw_ret = rmw_get_gid_for_client(client->
impl->rmw_handle, &gid);
378 if (rmw_ret != RMW_RET_OK) {
379 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
380 return rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);
382 rcl_ret_t ret = rcl_send_service_event_message(
383 client->
impl->service_event_publisher,
384 service_msgs__msg__ServiceEventInfo__RESPONSE_RECEIVED,
386 request_header->request_id.sequence_number,
389 RCL_SET_ERROR_MSG(rcl_get_error_string().str);
399 rmw_request_id_t * request_header,
402 rmw_service_info_t header;
403 header.request_id = *request_header;
405 *request_header = header.request_id;
412 RCL_CHECK_FOR_NULL_WITH_MSG(client,
"client pointer is invalid",
return false);
413 RCL_CHECK_FOR_NULL_WITH_MSG(
414 client->
impl,
"client's rmw implementation is invalid",
return false);
415 RCL_CHECK_FOR_NULL_WITH_MSG(
416 client->
impl->rmw_handle,
"client's rmw handle is invalid",
return false);
420 const rmw_qos_profile_t *
426 return &client->
impl->actual_request_publisher_qos;
429 const rmw_qos_profile_t *
435 return &client->
impl->actual_response_subscription_qos;
441 rcl_event_callback_t callback,
442 const void * user_data)
449 return rmw_client_set_on_new_response_callback(
450 client->
impl->rmw_handle,
460 const rosidl_service_type_support_t * type_support,
462 rcl_service_introspection_state_t introspection_state)
473 if (introspection_state == RCL_SERVICE_INTROSPECTION_OFF) {
474 return unconfigure_service_introspection(node, client->
impl, &allocator);
477 if (client->
impl->service_event_publisher == NULL) {
480 client->
impl->service_event_publisher = allocator.allocate(
482 RCL_CHECK_FOR_NULL_WITH_MSG(
485 *client->
impl->service_event_publisher = rcl_get_zero_initialized_service_event_publisher();
486 rcl_ret_t ret = rcl_service_event_publisher_init(
487 client->
impl->service_event_publisher, node, clock, publisher_options,
488 client->
impl->remapped_service_name, type_support);
490 allocator.deallocate(client->
impl->service_event_publisher, allocator.state);
491 client->
impl->service_event_publisher = NULL;
496 return rcl_service_event_publisher_change_state(
497 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.