ROS 2 rclcpp + rcl - rolling  rolling-a919a6e5
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 "rclcpp/qos.hpp"
26 #include "rmw/error_handling.h"
27 #include "rmw/rmw.h"
28 
30 
31 ServiceBase::ServiceBase(std::shared_ptr<rcl_node_t> node_handle)
32 : node_handle_(node_handle),
33  node_logger_(rclcpp::get_node_logger(node_handle_.get()))
34 {}
35 
36 
37 bool
38 ServiceBase::take_type_erased_request(void * request_out, rmw_request_id_t & request_id_out)
39 {
41  this->get_service_handle().get(),
42  &request_id_out,
43  request_out);
44  if (RCL_RET_SERVICE_TAKE_FAILED == ret) {
45  return false;
46  } else if (RCL_RET_OK != ret) {
47  rclcpp::exceptions::throw_from_rcl_error(ret);
48  }
49  return true;
50 }
51 
52 const char *
54 {
56 }
57 
58 std::shared_ptr<rcl_service_t>
60 {
61  return service_handle_;
62 }
63 
64 std::shared_ptr<const rcl_service_t>
66 {
67  return service_handle_;
68 }
69 
70 rcl_node_t *
71 ServiceBase::get_rcl_node_handle()
72 {
73  return node_handle_.get();
74 }
75 
76 const rcl_node_t *
77 ServiceBase::get_rcl_node_handle() const
78 {
79  return node_handle_.get();
80 }
81 
82 bool
84 {
85  return in_use_by_wait_set_.exchange(in_use_state);
86 }
87 
90 {
91  const rmw_qos_profile_t * qos =
92  rcl_service_response_publisher_get_actual_qos(service_handle_.get());
93  if (!qos) {
94  auto msg =
95  std::string("failed to get service's response publisher qos settings: ") +
96  rcl_get_error_string().str;
97  rcl_reset_error();
98  throw std::runtime_error(msg);
99  }
100 
101  rclcpp::QoS response_publisher_qos =
103 
104  return response_publisher_qos;
105 }
106 
109 {
110  const rmw_qos_profile_t * qos =
112  if (!qos) {
113  auto msg =
114  std::string("failed to get service's request subscription qos settings: ") +
115  rcl_get_error_string().str;
116  rcl_reset_error();
117  throw std::runtime_error(msg);
118  }
119 
120  rclcpp::QoS request_subscription_qos =
122 
123  return request_subscription_qos;
124 }
125 
126 void
127 ServiceBase::set_on_new_request_callback(rcl_event_callback_t callback, const void * user_data)
128 {
130  service_handle_.get(),
131  callback,
132  user_data);
133 
134  if (RCL_RET_OK != ret) {
135  rclcpp::exceptions::throw_from_rcl_error(
136  ret, "failed to set the on new request callback for service");
137  }
138 }
Encapsulation of Quality of Service settings.
Definition: qos.hpp:116
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:83
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:108
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:89
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:193
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 const char * get_service_name()
Return the name of the service.
Definition: service.cpp:53
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:45
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:436
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:297
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:445
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:454
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:371
Structure which encapsulates a ROS Node.
Definition: node.h:45
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:63
#define RCL_RET_OK
Success return code.
Definition: types.h:27
#define RCL_RET_SERVICE_TAKE_FAILED
Failed to take a request from the service return code.
Definition: types.h:87
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:24