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;
64 unconfigure_service_introspection(
69 if (service_impl == NULL) {
73 if (service_impl->service_event_publisher == NULL) {
77 rcl_ret_t ret = rcl_service_event_publisher_fini(service_impl->service_event_publisher, node);
79 allocator->deallocate(service_impl->service_event_publisher, allocator->state);
80 service_impl->service_event_publisher = NULL;
89 const rosidl_service_type_support_t * type_support,
90 const char * service_name,
111 RCUTILS_LOG_DEBUG_NAMED(
112 ROS_PACKAGE_NAME,
"Initializing service for service name '%s'", service_name);
114 RCL_SET_ERROR_MSG(
"service already initialized, or memory was unintialized");
121 RCL_CHECK_FOR_NULL_WITH_MSG(
122 service->
impl,
"allocating memory failed",
132 &service->
impl->remapped_service_name);
139 goto free_service_impl;
141 RCUTILS_LOG_DEBUG_NAMED(
142 ROS_PACKAGE_NAME,
"Expanded and remapped service name '%s'",
143 service->
impl->remapped_service_name);
145 if (RMW_QOS_POLICY_DURABILITY_TRANSIENT_LOCAL == options->
qos.durability) {
146 RCUTILS_LOG_WARN_NAMED(
148 "Warning: Setting QoS durability to 'transient local' for service servers "
149 "can cause them to receive requests from clients that have since terminated.");
154 service->
impl->rmw_handle = rmw_create_service(
157 service->
impl->remapped_service_name,
159 if (!service->
impl->rmw_handle) {
160 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
162 goto free_remapped_service_name;
166 rmw_ret_t rmw_ret = rmw_service_request_subscription_get_actual_qos(
167 service->
impl->rmw_handle,
168 &service->
impl->actual_request_subscription_qos);
169 if (RMW_RET_OK != rmw_ret) {
170 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
171 ret = rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);
172 goto destroy_service;
175 rmw_ret = rmw_service_response_publisher_get_actual_qos(
176 service->
impl->rmw_handle,
177 &service->
impl->actual_response_publisher_qos);
178 if (RMW_RET_OK != rmw_ret) {
179 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
180 ret = rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);
181 goto destroy_service;
185 service->
impl->actual_request_subscription_qos.avoid_ros_namespace_conventions =
186 options->
qos.avoid_ros_namespace_conventions;
187 service->
impl->actual_response_publisher_qos.avoid_ros_namespace_conventions =
188 options->
qos.avoid_ros_namespace_conventions;
191 service->
impl->options = *options;
193 if (
RCL_RET_OK != rcl_node_type_cache_register_type(
194 node, type_support->get_type_hash_func(type_support),
195 type_support->get_type_description_func(type_support),
196 type_support->get_type_description_sources_func(type_support)))
198 rcutils_reset_error();
199 RCL_SET_ERROR_MSG(
"Failed to register type for service");
201 goto destroy_service;
203 service->
impl->type_hash = *type_support->get_type_hash_func(type_support);
205 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Service initialized");
206 TRACETOOLS_TRACEPOINT(
208 (
const void *)service,
210 (
const void *)service->
impl->rmw_handle,
211 service->
impl->remapped_service_name);
217 if (RMW_RET_OK != rmw_ret) {
218 RCUTILS_SAFE_FWRITE_TO_STDERR(rmw_get_error_string().str);
219 RCUTILS_SAFE_FWRITE_TO_STDERR(
"\n");
222 free_remapped_service_name:
223 allocator->deallocate(service->
impl->remapped_service_name, allocator->state);
224 service->
impl->remapped_service_name = NULL;
227 allocator->deallocate(service->
impl, allocator->state);
228 service->
impl = NULL;
241 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Finalizing service");
255 rcl_ret_t rcl_ret = unconfigure_service_introspection(node, service->
impl, &allocator);
257 RCL_SET_ERROR_MSG(rcl_get_error_string().str);
261 rmw_ret_t ret = rmw_destroy_service(rmw_node, service->
impl->rmw_handle);
262 if (ret != RMW_RET_OK) {
263 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
268 ROSIDL_TYPE_HASH_VERSION_UNSET != service->
impl->type_hash.version &&
269 RCL_RET_OK != rcl_node_type_cache_unregister_type(node, &service->
impl->type_hash))
271 RCUTILS_SAFE_FWRITE_TO_STDERR(rcl_get_error_string().str);
275 allocator.deallocate(service->
impl->remapped_service_name, allocator.state);
276 service->
impl->remapped_service_name = NULL;
278 allocator.deallocate(service->
impl, allocator.state);
279 service->
impl = NULL;
281 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Service finalized");
291 default_options.
qos = rmw_qos_profile_services_default;
293 return default_options;
303 RCL_CHECK_FOR_NULL_WITH_MSG(service->
impl->rmw_handle,
"service is invalid",
return NULL);
304 return service->
impl->rmw_handle->service_name;
313 return &service->
impl->options;
322 return service->
impl->rmw_handle;
328 rmw_service_info_t * request_header,
331 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Service server taking service request");
338 RCL_CHECK_FOR_NULL_WITH_MSG(options,
"Failed to get service options",
return RCL_RET_ERROR);
341 rmw_ret_t ret = rmw_take_request(
342 service->
impl->rmw_handle, request_header, ros_request, &taken);
343 if (RMW_RET_OK != ret) {
344 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
345 if (RMW_RET_BAD_ALLOC == ret) {
350 RCUTILS_LOG_DEBUG_NAMED(
351 ROS_PACKAGE_NAME,
"Service take request succeeded: %s", taken ?
"true" :
"false");
355 if (service->
impl->service_event_publisher != NULL) {
356 rcl_ret_t rclret = rcl_send_service_event_message(
357 service->
impl->service_event_publisher,
358 service_msgs__msg__ServiceEventInfo__REQUEST_RECEIVED,
360 request_header->request_id.sequence_number,
361 request_header->request_id.writer_guid);
363 RCL_SET_ERROR_MSG(rcl_get_error_string().str);
373 rmw_request_id_t * request_header,
376 rmw_service_info_t header;
377 header.request_id = *request_header;
379 *request_header = header.request_id;
386 rmw_request_id_t * request_header,
390 RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME,
"Sending service response");
397 RCL_CHECK_FOR_NULL_WITH_MSG(options,
"Failed to get service options",
return RCL_RET_ERROR);
399 ret = rmw_send_response(service->
impl->rmw_handle, request_header, ros_response);
400 if (ret != RMW_RET_OK) {
401 RCL_SET_ERROR_MSG(rmw_get_error_string().str);
402 if (ret == RMW_RET_TIMEOUT) {
409 if (service->
impl->service_event_publisher != NULL) {
410 ret = rcl_send_service_event_message(
411 service->
impl->service_event_publisher,
412 service_msgs__msg__ServiceEventInfo__RESPONSE_SENT,
414 request_header->sequence_number,
415 request_header->writer_guid);
417 RCL_SET_ERROR_MSG(rcl_get_error_string().str);
427 RCL_CHECK_FOR_NULL_WITH_MSG(service,
"service pointer is invalid",
return false);
428 RCL_CHECK_FOR_NULL_WITH_MSG(
429 service->
impl,
"service's implementation is invalid",
return false);
430 RCL_CHECK_FOR_NULL_WITH_MSG(
431 service->
impl->rmw_handle,
"service's rmw handle is invalid",
return false);
435 const rmw_qos_profile_t *
441 return &service->
impl->actual_request_subscription_qos;
444 const rmw_qos_profile_t *
450 return &service->
impl->actual_response_publisher_qos;
456 rcl_event_callback_t callback,
457 const void * user_data)
464 return rmw_service_set_on_new_request_callback(
465 service->
impl->rmw_handle,
475 const rosidl_service_type_support_t * type_support,
477 rcl_service_introspection_state_t introspection_state)
488 if (introspection_state == RCL_SERVICE_INTROSPECTION_OFF) {
489 return unconfigure_service_introspection(node, service->
impl, &allocator);
492 if (service->
impl->service_event_publisher == NULL) {
495 service->
impl->service_event_publisher = allocator.allocate(
497 RCL_CHECK_FOR_NULL_WITH_MSG(
498 service->
impl->service_event_publisher,
"allocating memory failed",
501 *service->
impl->service_event_publisher = rcl_get_zero_initialized_service_event_publisher();
502 rcl_ret_t ret = rcl_service_event_publisher_init(
503 service->
impl->service_event_publisher, node, clock, publisher_options,
504 service->
impl->remapped_service_name, type_support);
506 allocator.deallocate(service->
impl->service_event_publisher, allocator.state);
507 service->
impl->service_event_publisher = NULL;
512 return rcl_service_event_publisher_change_state(
513 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.