ROS 2 rclcpp + rcl - rolling  rolling-a919a6e5
ROS 2 C++ Client Library with ROS Client Library
generic_service.cpp
1 // Copyright 2024 Sony Group Corporation.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "rclcpp/generic_service.hpp"
16 
17 namespace rclcpp
18 {
19 GenericService::GenericService(
20  std::shared_ptr<rcl_node_t> node_handle,
21  const std::string & service_name,
22  const std::string & service_type,
23  GenericServiceCallback any_callback,
24  rcl_service_options_t & service_options)
25 : ServiceBase(node_handle),
26  any_callback_(any_callback)
27 {
28  const rosidl_service_type_support_t * service_ts;
29  try {
30  ts_lib_ = get_typesupport_library(
31  service_type, "rosidl_typesupport_cpp");
32 
33  service_ts = get_service_typesupport_handle(
34  service_type, "rosidl_typesupport_cpp", *ts_lib_);
35 
36  auto request_type_support_intro = get_message_typesupport_handle(
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);
41 
42  auto response_type_support_intro = get_message_typesupport_handle(
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) {
48  RCLCPP_ERROR(
49  rclcpp::get_node_logger(node_handle_.get()).get_child("rclcpp"),
50  "Invalid service type: %s",
51  err.what());
53  }
54 
55  // rcl does the static memory allocation here
56  service_handle_ = std::shared_ptr<rcl_service_t>(
57  new rcl_service_t, [handle = node_handle_, service_name](rcl_service_t * service)
58  {
59  if (rcl_service_fini(service, handle.get()) != RCL_RET_OK) {
60  RCLCPP_ERROR(
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);
64  rcl_reset_error();
65  }
66  delete service;
67  });
68  *service_handle_.get() = rcl_get_zero_initialized_service();
69 
71  service_handle_.get(),
72  node_handle.get(),
73  service_ts,
74  service_name.c_str(),
75  &service_options);
76  if (ret != RCL_RET_OK) {
77  if (ret == RCL_RET_SERVICE_NAME_INVALID) {
78  auto rcl_node_handle = get_rcl_node_handle();
79  // this will throw on any validation problem
80  rcl_reset_error();
82  service_name,
83  rcl_node_get_name(rcl_node_handle),
84  rcl_node_get_namespace(rcl_node_handle),
85  true);
86  }
87 
88  rclcpp::exceptions::throw_from_rcl_error(ret, "could not create service");
89  }
90  TRACETOOLS_TRACEPOINT(
91  rclcpp_service_callback_added,
92  static_cast<const void *>(get_service_handle().get()),
93  static_cast<const void *>(&any_callback_));
94 #ifndef TRACETOOLS_DISABLED
95  any_callback_.register_callback_for_tracing();
96 #endif
97 }
98 
99 bool
101  SharedRequest request_out,
102  rmw_request_id_t & request_id_out)
103 {
104  request_out = create_request();
105  return this->take_type_erased_request(request_out.get(), request_id_out);
106 }
107 
108 std::shared_ptr<void>
109 GenericService::create_request()
110 {
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>(
114  request,
115  [this](void * p)
116  {
117  request_members_->fini_function(p);
118  delete[] reinterpret_cast<uint8_t *>(p);
119  });
120 }
121 
122 std::shared_ptr<void>
123 GenericService::create_response()
124 {
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>(
128  response,
129  [this](void * p)
130  {
131  response_members_->fini_function(p);
132  delete[] reinterpret_cast<uint8_t *>(p);
133  });
134 }
135 
136 std::shared_ptr<rmw_request_id_t>
137 GenericService::create_request_header()
138 {
139  return std::make_shared<rmw_request_id_t>();
140 }
141 
142 void
143 GenericService::handle_request(
144  std::shared_ptr<rmw_request_id_t> request_header,
145  std::shared_ptr<void> request)
146 {
147  auto response = any_callback_.dispatch(
148  this->shared_from_this(), request_header, request, create_response());
149  if (response) {
150  send_response(*request_header, response);
151  }
152 }
153 
154 void
155 GenericService::send_response(rmw_request_id_t & req_id, SharedResponse & response)
156 {
157  rcl_ret_t ret = rcl_send_response(get_service_handle().get(), &req_id, response.get());
158 
159  if (ret == RCL_RET_TIMEOUT) {
160  RCLCPP_WARN(
161  node_logger_.get_child("rclcpp"),
162  "failed to send response to %s (timeout): %s",
163  this->get_service_name(), rcl_get_error_string().str);
164  rcl_reset_error();
165  return;
166  }
167  if (ret != RCL_RET_OK) {
168  rclcpp::exceptions::throw_from_rcl_error(ret, "failed to send response");
169  }
170 }
171 
172 } // namespace rclcpp
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.
Definition: logger.cpp:73
RCLCPP_PUBLIC std::shared_ptr< rcl_service_t > get_service_handle()
Return the rcl_service_t service handle in a std::shared_ptr.
Definition: service.cpp:59
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.
Definition: service.cpp:38
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.
Definition: logger.cpp:45
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.
Definition: node.c:408
RCL_PUBLIC RCL_WARN_UNUSED const char * rcl_node_get_namespace(const rcl_node_t *node)
Return the namespace of the node.
Definition: node.c:417
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.
Definition: service.c:86
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_service_fini(rcl_service_t *service, rcl_node_t *node)
Finalize a rcl_service_t.
Definition: service.c:234
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.
Definition: service.c:55
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.
Definition: service.c:384
Options available for a rcl service.
Definition: service.h:50
Structure which encapsulates a ROS Service.
Definition: service.h:43
#define RCL_RET_SERVICE_NAME_INVALID
Service name (same as topic name) does not pass validation.
Definition: types.h:49
#define RCL_RET_OK
Success return code.
Definition: types.h:27
#define RCL_RET_TIMEOUT
Timeout occurred return code.
Definition: types.h:31
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:24