ROS 2 rclcpp + rcl - kilted  kilted
ROS 2 C++ Client Library with ROS Client Library
generic_publisher.cpp
1 // Copyright 2018, Bosch Software Innovations GmbH.
2 // Copyright 2021, Apex.AI Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include "rclcpp/generic_publisher.hpp"
17 
18 #include <memory>
19 #include <string>
20 
21 namespace rclcpp
22 {
23 
25 {
26  TRACETOOLS_TRACEPOINT(
27  rclcpp_publish,
28  nullptr,
29  static_cast<const void *>(&message.get_rcl_serialized_message()));
30  auto return_code = rcl_publish_serialized_message(
31  get_publisher_handle().get(), &message.get_rcl_serialized_message(), NULL);
32 
33  if (return_code != RCL_RET_OK) {
34  rclcpp::exceptions::throw_from_rcl_error(return_code, "failed to publish serialized message");
35  }
36 }
37 
39 {
40  auto loaned_message = borrow_loaned_message();
41  deserialize_message(message.get_rcl_serialized_message(), loaned_message);
42  publish_loaned_message(loaned_message);
43 }
44 
45 void * GenericPublisher::borrow_loaned_message()
46 {
47  void * loaned_message = nullptr;
48  auto return_code = rcl_borrow_loaned_message(
49  get_publisher_handle().get(), &type_support_, &loaned_message);
50 
51  if (return_code != RMW_RET_OK) {
52  if (return_code == RCL_RET_UNSUPPORTED) {
53  rclcpp::exceptions::throw_from_rcl_error(
54  return_code,
55  "current middleware cannot support loan messages");
56  } else {
57  rclcpp::exceptions::throw_from_rcl_error(return_code, "failed to borrow loaned msg");
58  }
59  }
60  return loaned_message;
61 }
62 
63 void GenericPublisher::deserialize_message(
64  const rmw_serialized_message_t & serialized_message,
65  void * deserialized_msg)
66 {
67  auto return_code = rmw_deserialize(&serialized_message, &type_support_, deserialized_msg);
68  if (return_code != RMW_RET_OK) {
69  rclcpp::exceptions::throw_from_rcl_error(return_code, "failed to deserialize msg");
70  }
71 }
72 
73 void GenericPublisher::publish_loaned_message(void * loaned_message)
74 {
75  TRACETOOLS_TRACEPOINT(rclcpp_publish, nullptr, static_cast<const void *>(loaned_message));
76  auto return_code = rcl_publish_loaned_message(
77  get_publisher_handle().get(), loaned_message, NULL);
78 
79  if (return_code != RCL_RET_OK) {
80  rclcpp::exceptions::throw_from_rcl_error(return_code, "failed to publish loaned message");
81  }
82 }
83 
84 } // namespace rclcpp
RCLCPP_PUBLIC void publish(const rclcpp::SerializedMessage &message)
Publish a rclcpp::SerializedMessage.
RCLCPP_PUBLIC void publish_as_loaned_msg(const rclcpp::SerializedMessage &message)
RCLCPP_PUBLIC std::shared_ptr< rcl_publisher_t > get_publisher_handle()
Get the rcl publisher handle.
Object oriented version of rcl_serialized_message_t with destructor to avoid memory leaks.
rcl_serialized_message_t & get_rcl_serialized_message()
Get the underlying rcl_serialized_t handle.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_publish_loaned_message(const rcl_publisher_t *publisher, void *ros_message, rmw_publisher_allocation_t *allocation)
Publish a loaned message on a topic using a publisher.
Definition: publisher.c:315
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_publish_serialized_message(const rcl_publisher_t *publisher, const rcl_serialized_message_t *serialized_message, rmw_publisher_allocation_t *allocation)
Publish a serialized message on a topic using a publisher.
Definition: publisher.c:292
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_borrow_loaned_message(const rcl_publisher_t *publisher, const rosidl_message_type_support_t *type_support, void **ros_message)
Borrow a loaned message.
Definition: publisher.c:245
#define RCL_RET_UNSUPPORTED
Unsupported return code.
Definition: types.h:37
#define RCL_RET_OK
Success return code.
Definition: types.h:27