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"
46 rmw_qos_profile_t actual_request_subscription_qos;
47 rmw_qos_profile_t actual_response_publisher_qos;
48 rmw_service_t * rmw_handle;
50 char * remapped_service_name;
51 rosidl_type_hash_t type_hash;
63 unconfigure_service_introspection(
68 if (service_impl == NULL) {
72 if (service_impl->service_event_publisher == NULL) {
76 rcl_ret_t ret = rcl_service_event_publisher_fini(service_impl->service_event_publisher, node);
78 allocator->deallocate(service_impl->service_event_publisher, allocator->state);
79 service_impl->service_event_publisher = NULL;
88 const rosidl_service_type_support_t * type_support,
89 const char * service_name,
110 RCUTILS_LOG_DEBUG_NAMED(
111 ROS_PACKAGE_NAME,
"Initializing service for service name '%s'", service_name);
113 RCL_SET_ERROR_MSG(
"service already initialized, or memory was unintialized");
120 RCL_CHECK_FOR_NULL_WITH_MSG(
121 service->
impl,
"allocating memory failed",
131 &service->
impl->remapped_service_name);
138 goto free_service_impl;
140 RCUTILS_LOG_DEBUG_NAMED(
141 ROS_PACKAGE_NAME,
"Expanded and remapped service name '%s'",
142 service->
impl->remapped_service_name);
144 if (RMW_QOS_POLICY_DURABILITY_TRANSIENT_LOCAL == options->
qos.durability) {
145 RCUTILS_LOG_WARN_NAMED(
147 "Warning: Setting QoS durability to 'transient local' for service servers "
148 "can cause them to receive requests from clients that have since terminated.");
153 service->
impl->rmw_handle = rmw_create_service(
156 service->
impl->remapped_service_name,
158 if (!service->
impl->rmw_handle) {
159 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
161 goto free_remapped_service_name;
165 rmw_ret_t rmw_ret = rmw_service_request_subscription_get_actual_qos(
166 service->
impl->rmw_handle,
167 &service->
impl->actual_request_subscription_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;
174 rmw_ret = rmw_service_response_publisher_get_actual_qos(
175 service->
impl->rmw_handle,
176 &service->
impl->actual_response_publisher_qos);
177 if (RMW_RET_OK != rmw_ret) {
178 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
179 ret = rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);
180 goto destroy_service;
184 service->
impl->actual_request_subscription_qos.avoid_ros_namespace_conventions =
185 options->
qos.avoid_ros_namespace_conventions;
186 service->
impl->actual_response_publisher_qos.avoid_ros_namespace_conventions =
187 options->
qos.avoid_ros_namespace_conventions;
190 service->
impl->options = *options;
192 if (
RCL_RET_OK != rcl_node_type_cache_register_type(
193 node, type_support->get_type_hash_func(type_support),
194 type_support->get_type_description_func(type_support),
195 type_support->get_type_description_sources_func(type_support)))
197 rcutils_reset_error();
198 RCL_SET_ERROR_MSG(
"Failed to register type for service");
200 goto destroy_service;
202 service->
impl->type_hash = *type_support->get_type_hash_func(type_support);
204 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Service initialized");
205 TRACETOOLS_TRACEPOINT(
207 (
const void *)service,
209 (
const void *)service->
impl->rmw_handle,
210 service->
impl->remapped_service_name);
216 if (RMW_RET_OK != rmw_ret) {
217 RCUTILS_SAFE_FWRITE_TO_STDERR(rmw_get_error_string().str);
218 RCUTILS_SAFE_FWRITE_TO_STDERR(
"\n");
221 free_remapped_service_name:
222 allocator->deallocate(service->
impl->remapped_service_name, allocator->state);
223 service->
impl->remapped_service_name = NULL;
226 allocator->deallocate(service->
impl, allocator->state);
227 service->
impl = NULL;
240 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Finalizing service");
254 rcl_ret_t rcl_ret = unconfigure_service_introspection(node, service->
impl, &allocator);
256 RCL_SET_ERROR_MSG(rcl_get_error_string().str);
260 rmw_ret_t ret = rmw_destroy_service(rmw_node, service->
impl->rmw_handle);
261 if (ret != RMW_RET_OK) {
262 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
267 ROSIDL_TYPE_HASH_VERSION_UNSET != service->
impl->type_hash.version &&
268 RCL_RET_OK != rcl_node_type_cache_unregister_type(node, &service->
impl->type_hash))
270 RCUTILS_SAFE_FWRITE_TO_STDERR(rcl_get_error_string().str);
274 allocator.deallocate(service->
impl->remapped_service_name, allocator.state);
275 service->
impl->remapped_service_name = NULL;
277 allocator.deallocate(service->
impl, allocator.state);
278 service->
impl = NULL;
280 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Service finalized");
290 default_options.
qos = rmw_qos_profile_services_default;
292 return default_options;
302 RCL_CHECK_FOR_NULL_WITH_MSG(service->
impl->rmw_handle,
"service is invalid",
return NULL);
303 return service->
impl->rmw_handle->service_name;
312 return &service->
impl->options;
321 return service->
impl->rmw_handle;
327 rmw_service_info_t * request_header,
330 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Service server taking service request");
337 RCL_CHECK_FOR_NULL_WITH_MSG(options,
"Failed to get service options",
return RCL_RET_ERROR);
340 rmw_ret_t ret = rmw_take_request(
341 service->
impl->rmw_handle, request_header, ros_request, &taken);
342 if (RMW_RET_OK != ret) {
343 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
344 if (RMW_RET_BAD_ALLOC == ret) {
349 RCUTILS_LOG_DEBUG_NAMED(
350 ROS_PACKAGE_NAME,
"Service take request succeeded: %s", taken ?
"true" :
"false");
354 if (service->
impl->service_event_publisher != NULL) {
355 rcl_ret_t rclret = rcl_send_service_event_message(
356 service->
impl->service_event_publisher,
357 service_msgs__msg__ServiceEventInfo__REQUEST_RECEIVED,
359 request_header->request_id.sequence_number,
360 request_header->request_id.writer_guid);
362 RCL_SET_ERROR_MSG(rcl_get_error_string().str);
372 rmw_request_id_t * request_header,
375 rmw_service_info_t header;
376 header.request_id = *request_header;
378 *request_header = header.request_id;
385 rmw_request_id_t * request_header,
389 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Sending service response");
396 RCL_CHECK_FOR_NULL_WITH_MSG(options,
"Failed to get service options",
return RCL_RET_ERROR);
398 ret = rmw_send_response(service->
impl->rmw_handle, request_header, ros_response);
399 if (ret != RMW_RET_OK) {
400 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
401 if (ret == RMW_RET_TIMEOUT) {
408 if (service->
impl->service_event_publisher != NULL) {
409 ret = rcl_send_service_event_message(
410 service->
impl->service_event_publisher,
411 service_msgs__msg__ServiceEventInfo__RESPONSE_SENT,
413 request_header->sequence_number,
414 request_header->writer_guid);
416 RCL_SET_ERROR_MSG(rcl_get_error_string().str);
426 RCL_CHECK_FOR_NULL_WITH_MSG(service,
"service pointer is invalid",
return false);
427 RCL_CHECK_FOR_NULL_WITH_MSG(
428 service->
impl,
"service's implementation is invalid",
return false);
429 RCL_CHECK_FOR_NULL_WITH_MSG(
430 service->
impl->rmw_handle,
"service's rmw handle is invalid",
return false);
434 const rmw_qos_profile_t *
440 return &service->
impl->actual_request_subscription_qos;
443 const rmw_qos_profile_t *
449 return &service->
impl->actual_response_publisher_qos;
455 rcl_event_callback_t callback,
456 const void * user_data)
463 return rmw_service_set_on_new_request_callback(
464 service->
impl->rmw_handle,
474 const rosidl_service_type_support_t * type_support,
476 rcl_service_introspection_state_t introspection_state)
487 if (introspection_state == RCL_SERVICE_INTROSPECTION_OFF) {
488 return unconfigure_service_introspection(node, service->
impl, &allocator);
491 if (service->
impl->service_event_publisher == NULL) {
494 service->
impl->service_event_publisher = allocator.allocate(
496 RCL_CHECK_FOR_NULL_WITH_MSG(
497 service->
impl->service_event_publisher,
"allocating memory failed",
500 *service->
impl->service_event_publisher = rcl_get_zero_initialized_service_event_publisher();
501 rcl_ret_t ret = rcl_service_event_publisher_init(
502 service->
impl->service_event_publisher, node, clock, publisher_options,
503 service->
impl->remapped_service_name, type_support);
505 allocator.deallocate(service->
impl->service_event_publisher, allocator.state);
506 service->
impl->service_event_publisher = NULL;
511 return rcl_service_event_publisher_change_state(
512 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.