ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
service.cpp
1 // Copyright 2015 Open Source Robotics Foundation, Inc.
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/service.hpp"
16 
17 #include <functional>
18 #include <iostream>
19 #include <memory>
20 #include <sstream>
21 #include <string>
22 
23 #include "rclcpp/any_service_callback.hpp"
24 #include "rclcpp/macros.hpp"
25 #include "rmw/error_handling.h"
26 #include "rmw/rmw.h"
27 
29 
30 ServiceBase::ServiceBase(std::shared_ptr<rcl_node_t> node_handle)
31 : node_handle_(node_handle),
32  node_logger_(rclcpp::get_node_logger(node_handle_.get()))
33 {}
34 
35 ServiceBase::~ServiceBase()
36 {
38 }
39 
40 bool
41 ServiceBase::take_type_erased_request(void * request_out, rmw_request_id_t & request_id_out)
42 {
44  this->get_service_handle().get(),
45  &request_id_out,
46  request_out);
47  if (RCL_RET_SERVICE_TAKE_FAILED == ret) {
48  return false;
49  } else if (RCL_RET_OK != ret) {
50  rclcpp::exceptions::throw_from_rcl_error(ret);
51  }
52  return true;
53 }
54 
55 const char *
57 {
59 }
60 
61 std::shared_ptr<rcl_service_t>
63 {
64  return service_handle_;
65 }
66 
67 std::shared_ptr<const rcl_service_t>
69 {
70  return service_handle_;
71 }
72 
73 rcl_node_t *
74 ServiceBase::get_rcl_node_handle()
75 {
76  return node_handle_.get();
77 }
78 
79 const rcl_node_t *
80 ServiceBase::get_rcl_node_handle() const
81 {
82  return node_handle_.get();
83 }
84 
85 bool
87 {
88  return in_use_by_wait_set_.exchange(in_use_state);
89 }
90 
93 {
94  const rmw_qos_profile_t * qos =
95  rcl_service_response_publisher_get_actual_qos(service_handle_.get());
96  if (!qos) {
97  auto msg =
98  std::string("failed to get service's response publisher qos settings: ") +
99  rcl_get_error_string().str;
100  rcl_reset_error();
101  throw std::runtime_error(msg);
102  }
103 
104  rclcpp::QoS response_publisher_qos =
106 
107  return response_publisher_qos;
108 }
109 
112 {
113  const rmw_qos_profile_t * qos =
115  if (!qos) {
116  auto msg =
117  std::string("failed to get service's request subscription qos settings: ") +
118  rcl_get_error_string().str;
119  rcl_reset_error();
120  throw std::runtime_error(msg);
121  }
122 
123  rclcpp::QoS request_subscription_qos =
125 
126  return request_subscription_qos;
127 }
128 
129 void
130 ServiceBase::set_on_new_request_callback(rcl_event_callback_t callback, const void * user_data)
131 {
133  service_handle_.get(),
134  callback,
135  user_data);
136 
137  if (RCL_RET_OK != ret) {
138  using rclcpp::exceptions::throw_from_rcl_error;
139  throw_from_rcl_error(ret, "failed to set the on new request callback for service");
140  }
141 }
Encapsulation of Quality of Service settings.
Definition: qos.hpp:111
RCLCPP_PUBLIC bool exchange_in_use_by_wait_set_state(bool in_use_state)
Exchange the "in use by wait set" state for this service.
Definition: service.cpp:86
RCLCPP_PUBLIC rclcpp::QoS get_request_subscription_actual_qos() const
Get the actual request subscription QoS settings, after the defaults have been determined.
Definition: service.cpp:111
RCLCPP_PUBLIC rclcpp::QoS get_response_publisher_actual_qos() const
Get the actual response publisher QoS settings, after the defaults have been determined.
Definition: service.cpp:92
void set_on_new_request_callback(std::function< void(size_t)> callback)
Set a callback to be called when each new request is received.
Definition: service.hpp:191
void clear_on_new_request_callback()
Unset the callback registered for new requests, if any.
Definition: service.hpp:239
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:62
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:41
RCLCPP_PUBLIC const char * get_service_name()
Return the name of the service.
Definition: service.cpp:56
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC Logger get_node_logger(const rcl_node_t *node)
Return a named logger using an rcl_node_t.
Definition: logger.cpp:38
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.
Definition: service.c:335
RCL_PUBLIC RCL_WARN_UNUSED const char * rcl_service_get_service_name(const rcl_service_t *service)
Get the topic name for the service.
Definition: service.c:220
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.
Definition: service.c:344
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.
Definition: service.c:353
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.
Definition: service.c:284
Structure which encapsulates a ROS Node.
Definition: node.h:42
static QoSInitialization from_rmw(const rmw_qos_profile_t &rmw_qos)
Create a QoSInitialization from an existing rmw_qos_profile_t, using its history and depth.
Definition: qos.cpp:51
#define RCL_RET_OK
Success return code.
Definition: types.h:26
#define RCL_RET_SERVICE_TAKE_FAILED
Failed to take a request from the service return code.
Definition: types.h:84
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:23