|
| 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 | +#ifndef RCLCPP__CREATE_GENERIC_SERVICE_HPP_ |
| 16 | +#define RCLCPP__CREATE_GENERIC_SERVICE_HPP_ |
| 17 | + |
| 18 | +#include <memory> |
| 19 | +#include <string> |
| 20 | +#include <utility> |
| 21 | + |
| 22 | +#include "rclcpp/generic_service.hpp" |
| 23 | +#include "rclcpp/node_interfaces/get_node_base_interface.hpp" |
| 24 | +#include "rclcpp/node_interfaces/node_base_interface.hpp" |
| 25 | +#include "rclcpp/node_interfaces/get_node_services_interface.hpp" |
| 26 | +#include "rclcpp/node_interfaces/node_services_interface.hpp" |
| 27 | +#include "rclcpp/visibility_control.hpp" |
| 28 | +#include "rmw/rmw.h" |
| 29 | + |
| 30 | +namespace rclcpp |
| 31 | +{ |
| 32 | +/// Create a generic service with a given type. |
| 33 | +/** |
| 34 | + * \param[in] node_base NodeBaseInterface implementation of the node on which |
| 35 | + * to create the generic service. |
| 36 | + * \param[in] node_services NodeServicesInterface implementation of the node on |
| 37 | + * which to create the service. |
| 38 | + * \param[in] service_name The name on which the service is accessible. |
| 39 | + * \param[in] service_type The name of service type, e.g. "std_srvs/srv/SetBool". |
| 40 | + * \param[in] callback The callback to call when the service gets a request. |
| 41 | + * \param[in] qos Quality of service profile for the service. |
| 42 | + * \param[in] group Callback group to handle the reply to service calls. |
| 43 | + * \return Shared pointer to the created service. |
| 44 | + */ |
| 45 | +template<typename CallbackT> |
| 46 | +typename rclcpp::GenericService::SharedPtr |
| 47 | +create_generic_service( |
| 48 | + std::shared_ptr<node_interfaces::NodeBaseInterface> node_base, |
| 49 | + std::shared_ptr<node_interfaces::NodeServicesInterface> node_services, |
| 50 | + const std::string & service_name, |
| 51 | + const std::string & service_type, |
| 52 | + CallbackT && callback, |
| 53 | + const rclcpp::QoS & qos, |
| 54 | + rclcpp::CallbackGroup::SharedPtr group) |
| 55 | +{ |
| 56 | + rclcpp::GenericServiceCallback any_service_callback; |
| 57 | + any_service_callback.set(std::forward<CallbackT>(callback)); |
| 58 | + |
| 59 | + rcl_service_options_t service_options = rcl_service_get_default_options(); |
| 60 | + service_options.qos = qos.get_rmw_qos_profile(); |
| 61 | + |
| 62 | + auto serv = GenericService::make_shared( |
| 63 | + node_base->get_shared_rcl_node_handle(), |
| 64 | + service_name, service_type, any_service_callback, service_options); |
| 65 | + auto serv_base_ptr = std::dynamic_pointer_cast<ServiceBase>(serv); |
| 66 | + node_services->add_service(serv_base_ptr, group); |
| 67 | + return serv; |
| 68 | +} |
| 69 | + |
| 70 | +/// Create a generic service with a given type. |
| 71 | +/** |
| 72 | + * The NodeT type needs to have NodeBaseInterface implementation and NodeServicesInterface |
| 73 | + * implementation of the node which to create the generic service. |
| 74 | + * |
| 75 | + * \param[in] node The node on which to create the generic service. |
| 76 | + * \param[in] service_name The name on which the service is accessible. |
| 77 | + * \param[in] service_type The name of service type, e.g. "std_srvs/srv/SetBool". |
| 78 | + * \param[in] callback The callback to call when the service gets a request. |
| 79 | + * \param[in] qos Quality of service profile for the service. |
| 80 | + * \param[in] group Callback group to handle the reply to service calls. |
| 81 | + * \return Shared pointer to the created service. |
| 82 | + */ |
| 83 | +template<typename NodeT, typename CallbackT> |
| 84 | +typename rclcpp::GenericService::SharedPtr |
| 85 | +create_generic_service( |
| 86 | + NodeT node, |
| 87 | + const std::string & service_name, |
| 88 | + const std::string & service_type, |
| 89 | + CallbackT && callback, |
| 90 | + const rclcpp::QoS & qos, |
| 91 | + rclcpp::CallbackGroup::SharedPtr group) |
| 92 | +{ |
| 93 | + return create_generic_service<CallbackT>( |
| 94 | + rclcpp::node_interfaces::get_node_base_interface(node), |
| 95 | + rclcpp::node_interfaces::get_node_services_interface(node), |
| 96 | + service_name, |
| 97 | + service_type, |
| 98 | + std::forward<CallbackT>(callback), qos.get_rmw_qos_profile(), group); |
| 99 | +} |
| 100 | +} // namespace rclcpp |
| 101 | + |
| 102 | +#endif // RCLCPP__CREATE_GENERIC_SERVICE_HPP_ |
0 commit comments