25 #include "rcl/error_handling.h"
27 #include "rcl/node_type_cache.h"
31 #include "rcutils/logging_macros.h"
32 #include "rcutils/macros.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"
42 #include "./service_impl.h"
54 unconfigure_service_introspection(
59 if (service_impl == NULL) {
63 if (service_impl->service_event_publisher == NULL) {
67 rcl_ret_t ret = rcl_service_event_publisher_fini(service_impl->service_event_publisher, node);
69 allocator->deallocate(service_impl->service_event_publisher, allocator->state);
70 service_impl->service_event_publisher = NULL;
79 const rosidl_service_type_support_t * type_support,
80 const char * service_name,
101 RCUTILS_LOG_DEBUG_NAMED(
102 ROS_PACKAGE_NAME,
"Initializing service for service name '%s'", service_name);
104 RCL_SET_ERROR_MSG(
"service already initialized, or memory was unintialized");
111 RCL_CHECK_FOR_NULL_WITH_MSG(
112 service->
impl,
"allocating memory failed",
122 &service->
impl->remapped_service_name);
129 goto free_service_impl;
131 RCUTILS_LOG_DEBUG_NAMED(
132 ROS_PACKAGE_NAME,
"Expanded and remapped service name '%s'",
133 service->
impl->remapped_service_name);
135 if (RMW_QOS_POLICY_DURABILITY_TRANSIENT_LOCAL == options->
qos.durability) {
136 RCUTILS_LOG_WARN_NAMED(
138 "Warning: Setting QoS durability to 'transient local' for service servers "
139 "can cause them to receive requests from clients that have since terminated.");
144 service->
impl->rmw_handle = rmw_create_service(
147 service->
impl->remapped_service_name,
149 if (!service->
impl->rmw_handle) {
150 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
152 goto free_remapped_service_name;
156 rmw_ret_t rmw_ret = rmw_service_request_subscription_get_actual_qos(
157 service->
impl->rmw_handle,
158 &service->
impl->actual_request_subscription_qos);
159 if (RMW_RET_OK != rmw_ret) {
160 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
161 ret = rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);
162 goto destroy_service;
165 rmw_ret = rmw_service_response_publisher_get_actual_qos(
166 service->
impl->rmw_handle,
167 &service->
impl->actual_response_publisher_qos);
168 if (RMW_RET_OK != rmw_ret) {
169 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
170 ret = rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);
171 goto destroy_service;
175 service->
impl->actual_request_subscription_qos.avoid_ros_namespace_conventions =
176 options->
qos.avoid_ros_namespace_conventions;
177 service->
impl->actual_response_publisher_qos.avoid_ros_namespace_conventions =
178 options->
qos.avoid_ros_namespace_conventions;
181 service->
impl->options = *options;
182 service->
impl->in_use_by_waitset =
false;
184 if (
RCL_RET_OK != rcl_node_type_cache_register_type(
185 node, type_support->get_type_hash_func(type_support),
186 type_support->get_type_description_func(type_support),
187 type_support->get_type_description_sources_func(type_support)))
189 rcutils_reset_error();
190 RCL_SET_ERROR_MSG(
"Failed to register type for service");
192 goto destroy_service;
194 service->
impl->type_hash = *type_support->get_type_hash_func(type_support);
196 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Service initialized");
197 TRACETOOLS_TRACEPOINT(
199 (
const void *)service,
201 (
const void *)service->
impl->rmw_handle,
202 service->
impl->remapped_service_name);
208 if (RMW_RET_OK != rmw_ret) {
209 RCUTILS_SAFE_FWRITE_TO_STDERR(rmw_get_error_string().str);
210 RCUTILS_SAFE_FWRITE_TO_STDERR(
"\n");
213 free_remapped_service_name:
214 allocator->deallocate(service->
impl->remapped_service_name, allocator->state);
215 service->
impl->remapped_service_name = NULL;
218 allocator->deallocate(service->
impl, allocator->state);
219 service->
impl = NULL;
232 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Finalizing service");
246 rcl_ret_t rcl_ret = unconfigure_service_introspection(node, service->
impl, &allocator);
248 RCL_SET_ERROR_MSG(rcl_get_error_string().str);
252 rmw_ret_t ret = rmw_destroy_service(rmw_node, service->
impl->rmw_handle);
253 if (ret != RMW_RET_OK) {
254 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
259 ROSIDL_TYPE_HASH_VERSION_UNSET != service->
impl->type_hash.version &&
260 RCL_RET_OK != rcl_node_type_cache_unregister_type(node, &service->
impl->type_hash))
262 RCUTILS_SAFE_FWRITE_TO_STDERR(rcl_get_error_string().str);
266 allocator.deallocate(service->
impl->remapped_service_name, allocator.state);
267 service->
impl->remapped_service_name = NULL;
269 allocator.deallocate(service->
impl, allocator.state);
270 service->
impl = NULL;
272 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Service finalized");
282 default_options.
qos = rmw_qos_profile_services_default;
284 return default_options;
294 RCL_CHECK_FOR_NULL_WITH_MSG(service->
impl->rmw_handle,
"service is invalid",
return NULL);
295 return service->
impl->rmw_handle->service_name;
304 return &service->
impl->options;
313 return service->
impl->rmw_handle;
319 rmw_service_info_t * request_header,
322 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Service server taking service request");
329 RCL_CHECK_FOR_NULL_WITH_MSG(options,
"Failed to get service options",
return RCL_RET_ERROR);
332 rmw_ret_t ret = rmw_take_request(
333 service->
impl->rmw_handle, request_header, ros_request, &taken);
334 if (RMW_RET_OK != ret) {
335 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
336 if (RMW_RET_BAD_ALLOC == ret) {
341 RCUTILS_LOG_DEBUG_NAMED(
342 ROS_PACKAGE_NAME,
"Service take request succeeded: %s", taken ?
"true" :
"false");
346 if (service->
impl->service_event_publisher != NULL) {
347 rcl_ret_t rclret = rcl_send_service_event_message(
348 service->
impl->service_event_publisher,
349 service_msgs__msg__ServiceEventInfo__REQUEST_RECEIVED,
351 request_header->request_id.sequence_number,
352 request_header->request_id.writer_guid);
354 RCL_SET_ERROR_MSG(rcl_get_error_string().str);
364 rmw_request_id_t * request_header,
367 rmw_service_info_t header;
368 header.request_id = *request_header;
370 *request_header = header.request_id;
377 rmw_request_id_t * request_header,
381 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Sending service response");
388 RCL_CHECK_FOR_NULL_WITH_MSG(options,
"Failed to get service options",
return RCL_RET_ERROR);
390 ret = rmw_send_response(service->
impl->rmw_handle, request_header, ros_response);
391 if (ret != RMW_RET_OK) {
392 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
393 if (ret == RMW_RET_TIMEOUT) {
400 if (service->
impl->service_event_publisher != NULL) {
401 ret = rcl_send_service_event_message(
402 service->
impl->service_event_publisher,
403 service_msgs__msg__ServiceEventInfo__RESPONSE_SENT,
405 request_header->sequence_number,
406 request_header->writer_guid);
408 RCL_SET_ERROR_MSG(rcl_get_error_string().str);
418 RCL_CHECK_FOR_NULL_WITH_MSG(service,
"service pointer is invalid",
return false);
419 RCL_CHECK_FOR_NULL_WITH_MSG(
420 service->
impl,
"service's implementation is invalid",
return false);
421 RCL_CHECK_FOR_NULL_WITH_MSG(
422 service->
impl->rmw_handle,
"service's rmw handle is invalid",
return false);
426 const rmw_qos_profile_t *
432 return &service->
impl->actual_request_subscription_qos;
435 const rmw_qos_profile_t *
441 return &service->
impl->actual_response_publisher_qos;
447 rcl_event_callback_t callback,
448 const void * user_data)
455 return rmw_service_set_on_new_request_callback(
456 service->
impl->rmw_handle,
466 const rosidl_service_type_support_t * type_support,
468 rcl_service_introspection_state_t introspection_state)
479 if (introspection_state == RCL_SERVICE_INTROSPECTION_OFF) {
480 return unconfigure_service_introspection(node, service->
impl, &allocator);
483 if (service->
impl->service_event_publisher == NULL) {
486 service->
impl->service_event_publisher = allocator.allocate(
488 RCL_CHECK_FOR_NULL_WITH_MSG(
489 service->
impl->service_event_publisher,
"allocating memory failed",
492 *service->
impl->service_event_publisher = rcl_get_zero_initialized_service_event_publisher();
493 rcl_ret_t ret = rcl_service_event_publisher_init(
494 service->
impl->service_event_publisher, node, clock, publisher_options,
495 service->
impl->remapped_service_name, type_support);
497 allocator.deallocate(service->
impl->service_event_publisher, allocator.state);
498 service->
impl->service_event_publisher = NULL;
503 return rcl_service_event_publisher_change_state(
504 service->
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 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.
RCL_PUBLIC RCL_WARN_UNUSED const rmw_qos_profile_t * rcl_service_request_subscription_get_actual_qos(const rcl_service_t *service)
Get the actual qos settings of the service's request subscription.
RCL_PUBLIC RCL_WARN_UNUSED rmw_service_t * rcl_service_get_rmw_handle(const rcl_service_t *service)
Return the rmw service handle.
RCL_PUBLIC RCL_WARN_UNUSED const char * rcl_service_get_service_name(const rcl_service_t *service)
Get the topic name for the service.
RCL_PUBLIC RCL_WARN_UNUSED const rmw_qos_profile_t * rcl_service_response_publisher_get_actual_qos(const rcl_service_t *service)
Get the actual qos settings of the service's response publisher.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_service_init(rcl_service_t *service, const rcl_node_t *node, const rosidl_service_type_support_t *type_support, const char *service_name, const rcl_service_options_t *options)
Initialize a rcl service.
RCL_PUBLIC RCL_WARN_UNUSED const rcl_service_options_t * rcl_service_get_options(const rcl_service_t *service)
Return the rcl service options.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_service_configure_service_introspection(rcl_service_t *service, 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)
Configure service introspection features for the service.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_take_request_with_info(const rcl_service_t *service, rmw_service_info_t *request_header, void *ros_request)
Take a pending ROS request using a rcl service.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_service_set_on_new_request_callback(const rcl_service_t *service, rcl_event_callback_t callback, const void *user_data)
Set the on new request callback function for the service.
RCL_PUBLIC bool rcl_service_is_valid(const rcl_service_t *service)
Check that the service is valid.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_service_fini(rcl_service_t *service, rcl_node_t *node)
Finalize a rcl_service_t.
RCL_PUBLIC RCL_WARN_UNUSED rcl_service_options_t rcl_service_get_default_options(void)
Return the default service options in a rcl_service_options_t.
RCL_PUBLIC RCL_WARN_UNUSED rcl_service_t rcl_get_zero_initialized_service(void)
Return a rcl_service_t struct with members set to NULL.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_take_request(const rcl_service_t *service, rmw_request_id_t *request_header, void *ros_request)
Backwards compatibility function to take a pending ROS request using a rcl service.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_send_response(const rcl_service_t *service, rmw_request_id_t *response_header, void *ros_response)
Send a ROS response to a client using a service.
Encapsulation of a time source.
Structure which encapsulates a ROS Node.
Options available for a rcl publisher.
Options available for a rcl service.
rmw_qos_profile_t qos
Middleware quality of service settings for the service.
rcl_allocator_t allocator
Custom allocator for the service, used for incidental allocations.
Structure which encapsulates a ROS Service.
rcl_service_impl_t * impl
Pointer to the service implementation.
#define RCL_RET_SERVICE_INVALID
Invalid rcl_service_t given 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_SERVICE_TAKE_FAILED
Failed to take a request from the service return code.
#define RCL_RET_NODE_INVALID
Invalid rcl_node_t given return code.
#define RCL_RET_TIMEOUT
Timeout occurred return code.
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.