15 #include "rclcpp/generic_service.hpp"
19 GenericService::GenericService(
20 std::shared_ptr<rcl_node_t> node_handle,
21 const std::string & service_name,
22 const std::string & service_type,
26 any_callback_(any_callback)
28 const rosidl_service_type_support_t * service_ts;
31 service_type,
"rosidl_typesupport_cpp");
34 service_type,
"rosidl_typesupport_cpp", *ts_lib_);
37 service_ts->request_typesupport,
38 rosidl_typesupport_introspection_cpp::typesupport_identifier);
39 request_members_ =
static_cast<const rosidl_typesupport_introspection_cpp::MessageMembers *
>(
40 request_type_support_intro->data);
43 service_ts->response_typesupport,
44 rosidl_typesupport_introspection_cpp::typesupport_identifier);
45 response_members_ =
static_cast<const rosidl_typesupport_introspection_cpp::MessageMembers *
>(
46 response_type_support_intro->data);
47 }
catch (std::runtime_error & err) {
50 "Invalid service type: %s",
56 service_handle_ = std::shared_ptr<rcl_service_t>(
61 rclcpp::get_node_logger(handle.get()).get_child(
"rclcpp"),
62 "Error in destruction of rcl service handle: %s",
63 rcl_get_error_string().str);
71 service_handle_.get(),
78 auto rcl_node_handle = get_rcl_node_handle();
88 rclcpp::exceptions::throw_from_rcl_error(ret,
"could not create service");
90 TRACETOOLS_TRACEPOINT(
91 rclcpp_service_callback_added,
93 static_cast<const void *
>(&any_callback_));
94 #ifndef TRACETOOLS_DISABLED
95 any_callback_.register_callback_for_tracing();
101 SharedRequest request_out,
102 rmw_request_id_t & request_id_out)
104 request_out = create_request();
108 std::shared_ptr<void>
109 GenericService::create_request()
111 Request request =
new uint8_t[request_members_->size_of_];
112 request_members_->init_function(request, rosidl_runtime_cpp::MessageInitialization::ZERO);
113 return std::shared_ptr<void>(
117 request_members_->fini_function(p);
118 delete[]
reinterpret_cast<uint8_t *
>(p);
122 std::shared_ptr<void>
123 GenericService::create_response()
125 Response response =
new uint8_t[response_members_->size_of_];
126 response_members_->init_function(response, rosidl_runtime_cpp::MessageInitialization::ZERO);
127 return std::shared_ptr<void>(
131 response_members_->fini_function(p);
132 delete[]
reinterpret_cast<uint8_t *
>(p);
136 std::shared_ptr<rmw_request_id_t>
137 GenericService::create_request_header()
139 return std::make_shared<rmw_request_id_t>();
143 GenericService::handle_request(
144 std::shared_ptr<rmw_request_id_t> request_header,
145 std::shared_ptr<void> request)
147 auto response = any_callback_.dispatch(
148 this->shared_from_this(), request_header, request, create_response());
150 send_response(*request_header, response);
155 GenericService::send_response(rmw_request_id_t & req_id, SharedResponse & response)
162 "failed to send response to %s (timeout): %s",
163 this->get_service_name(), rcl_get_error_string().str);
168 rclcpp::exceptions::throw_from_rcl_error(ret,
"failed to send response");
RCLCPP_PUBLIC bool take_request(SharedRequest request_out, rmw_request_id_t &request_id_out)
Take the next request from the service.
RCLCPP_PUBLIC Logger get_child(const std::string &suffix)
Return a logger that is a descendant of this logger.
RCLCPP_PUBLIC std::shared_ptr< rcl_service_t > get_service_handle()
Return the rcl_service_t service handle in a std::shared_ptr.
RCLCPP_PUBLIC bool take_type_erased_request(void *request_out, rmw_request_id_t &request_id_out)
Take the next request from the service as a type erased pointer.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC std::string expand_topic_or_service_name(const std::string &name, const std::string &node_name, const std::string &namespace_, bool is_service=false)
Expand a topic or service name and throw if it is not valid.
RCLCPP_PUBLIC Logger get_node_logger(const rcl_node_t *node)
Return a named logger using an rcl_node_t.
RCLCPP_PUBLIC std::shared_ptr< rcpputils::SharedLibrary > get_typesupport_library(const std::string &type, const std::string &typesupport_identifier)
Load the type support library for the given type.
RCLCPP_PUBLIC const rosidl_message_type_support_t * get_message_typesupport_handle(const std::string &type, const std::string &typesupport_identifier, rcpputils::SharedLibrary &library)
Extracts the message type support handle from the library.
RCLCPP_PUBLIC const rosidl_service_type_support_t * get_service_typesupport_handle(const std::string &type, const std::string &typesupport_identifier, rcpputils::SharedLibrary &library)
Extracts the service type support handle from the library.
RCL_PUBLIC RCL_WARN_UNUSED const char * rcl_node_get_name(const rcl_node_t *node)
Return the name of the node.
RCL_PUBLIC RCL_WARN_UNUSED const char * rcl_node_get_namespace(const rcl_node_t *node)
Return the namespace of the node.
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 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_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_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.
Options available for a rcl service.
Structure which encapsulates a ROS Service.
#define RCL_RET_SERVICE_NAME_INVALID
Service name (same as topic name) does not pass validation.
#define RCL_RET_OK
Success return code.
#define RCL_RET_TIMEOUT
Timeout occurred return code.
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.