Skip to content

Commit 02719f5

Browse files
authored
Merge pull request ekxide#25 from orecham/rmw-iox-2-serialization-support
Add ROS message serialization
2 parents 1ffa00e + 025e297 commit 02719f5

File tree

19 files changed

+622
-275
lines changed

19 files changed

+622
-275
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ bindings to the Rust core.
3535
| Node | :white_check_mark: |
3636
| Guard Condition | :white_check_mark: |
3737
| Event | :construction: |
38-
| Publish-Subscribe (Copy) | :white_check_mark: |
39-
| Publish-Subscribe (Loan) | :white_check_mark: |
40-
| Publish-Subscribe (Serialized) | :construction: |
38+
| Publish-Subscribe | :white_check_mark: |
39+
| Message Serialization | :white_check_mark: |
4140
| Server-Client | :construction: |
4241
| Waitset | :white_check_mark: |
4342
| Graph | :construction: |

doc/release-notes/unreleased.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
conflicts when merging.
1212
-->
1313

14+
* Serialize/deserialized non-self-contained messages into `iceoryx2` payloads [#2](https://github.com/ekxide/rmw_iceoryx2/issues/2)
15+
1416
### Bugfixes
1517

1618
<!--

rmw_iceoryx2_cxx/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ set(AMENT_PACKAGES
4545
rosidl_typesupport_introspection_cpp
4646
rosidl_typesupport_fastrtps_c
4747
rosidl_typesupport_fastrtps_cpp
48-
fastrtps
48+
fastcdr
4949
)
5050
foreach(package ${AMENT_PACKAGES})
5151
find_package(${package} REQUIRED)
@@ -58,7 +58,6 @@ endforeach()
5858
add_library(${PROJECT_NAME} SHARED
5959
src/impl/common/names.cpp
6060
src/impl/message/introspection.cpp
61-
src/impl/message/serialization.cpp
6261
src/impl/middleware/iceoryx2.cpp
6362
src/impl/runtime/context.cpp
6463
src/impl/runtime/guard_condition.cpp

rmw_iceoryx2_cxx/include/rmw_iceoryx2_cxx/impl/message/serialization.hpp

Lines changed: 0 additions & 8 deletions
This file was deleted.

rmw_iceoryx2_cxx/include/rmw_iceoryx2_cxx/impl/runtime/publisher.hpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "rmw_iceoryx2_cxx/impl/middleware/iceoryx2.hpp"
1919
#include "rmw_iceoryx2_cxx/impl/runtime/node.hpp"
2020
#include "rmw_iceoryx2_cxx/impl/runtime/sample_registry.hpp"
21+
#include "rosidl_typesupport_cpp/message_type_support.hpp"
2122

2223
namespace rmw::iox2
2324
{
@@ -32,7 +33,6 @@ struct Error<Publisher>
3233

3334
/// @brief Implementation of the RMW publisher for iceoryx2
3435
///
35-
/// @details The publisher supports both zero-copy loans and copy-based publishing mechanisms.
3636
/// @details The implementation supports both copy and loan-based publishing mechanisms,
3737
/// allowing for efficient zero-copy communication when possible.
3838
///
@@ -59,14 +59,12 @@ class RMW_PUBLIC Publisher
5959
/// @param[out] error Optional error that is set if construction fails
6060
/// @param[in] node The node that owns this publisher
6161
/// @param[in] topic The topic name to publish to
62-
/// @param[in] type The message type name
63-
/// @param[in] size The maximum message payload size
62+
/// @param[in] typesupport The message typesupport
6463
Publisher(CreationLock,
6564
iox::optional<ErrorType>& error,
6665
Node& node,
6766
const char* topic,
68-
const char* type,
69-
const uint64_t size);
67+
const rosidl_message_type_support_t* type_support);
7068

7169
/// @brief Get the unique identifier of this publisher
7270
/// @return The unique id or empty optional if failing to retrieve it from iceoryx2
@@ -76,21 +74,21 @@ class RMW_PUBLIC Publisher
7674
/// @return The topic name as string
7775
auto topic() const -> const std::string&;
7876

79-
/// @brief Get the message type name
80-
/// @return The message type name as string
81-
auto type() const -> const std::string&;
77+
/// @brief Get the typesupport used by the publisher
78+
/// @return Pointer to the typesupport stored in the loaded typesupport library
79+
auto typesupport() const -> const rosidl_message_type_support_t*;
80+
81+
/// @brief Get the (unserialized) size of the message struct.
82+
/// @return Size of the message
83+
auto unserialized_size() const -> uint64_t;
8284

8385
/// @brief Get the service name used internally, required for matching via iceoryx2
8486
/// @return The service name as string
8587
auto service_name() const -> const std::string&;
8688

87-
/// @brief Get the maximum payload size
88-
/// @return The maximum payload size in bytes
89-
auto payload_size() const -> uint64_t;
90-
9189
/// @brief Loan memory for zero-copy publishing
9290
/// @return Expected containing pointer to loaned memory or error
93-
auto loan() -> iox::expected<void*, ErrorType>;
91+
auto loan(uint64_t number_of_bytes) -> iox::expected<void*, ErrorType>;
9492

9593
/// @brief Return previously loaned memory without publishing
9694
/// @param[in] loaned_memory Pointer to the loaned memory to return
@@ -107,13 +105,13 @@ class RMW_PUBLIC Publisher
107105
/// @param[in] msg Pointer to the message data to copy
108106
/// @param[in] size Size of the message data in bytes
109107
/// @return Expected containing void or error if publish failed
110-
auto publish_copy(const void* msg, uint64_t size) -> iox::expected<void, ErrorType>;
108+
auto publish_copy(const void* data, uint64_t number_of_bytes) -> iox::expected<void, ErrorType>;
111109

112110
private:
113111
const std::string m_topic;
114-
const std::string m_type;
112+
const rosidl_message_type_support_t* m_typesupport;
113+
const uint64_t m_unserialized_size;
115114
const std::string m_service_name;
116-
const uint64_t m_payload_size;
117115

118116
iox::optional<IdType> m_iox_unique_id;
119117
iox::optional<IceoryxNotifier> m_iox2_notifier;

rmw_iceoryx2_cxx/include/rmw_iceoryx2_cxx/impl/runtime/sample_registry.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class SampleRegistry
7070
/// @brief Remove and return a stored sample
7171
/// @param[in] loaned_memory Pointer to the payload data
7272
/// @return Expected containing the removed sample if found, error otherwise
73-
auto release(uint8_t* loaned_memory) -> iox::expected<SampleType, ErrorType> {
73+
auto release(const uint8_t* loaned_memory) -> iox::expected<SampleType, ErrorType> {
7474
using iox::err;
7575
using iox::ok;
7676
using iox::optional;
@@ -85,7 +85,7 @@ class SampleRegistry
8585
}
8686

8787
private:
88-
std::unordered_map<uint8_t*, SampleType> m_samples;
88+
std::unordered_map<const uint8_t*, SampleType> m_samples;
8989
};
9090

9191
} // namespace rmw::iox2

rmw_iceoryx2_cxx/include/rmw_iceoryx2_cxx/impl/runtime/subscriber.hpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "rmw_iceoryx2_cxx/impl/common/creation_lock.hpp"
1818
#include "rmw_iceoryx2_cxx/impl/runtime/node.hpp"
1919
#include "rmw_iceoryx2_cxx/impl/runtime/sample_registry.hpp"
20+
#include "rosidl_typesupport_cpp/message_type_support.hpp"
2021

2122
namespace rmw::iox2
2223
{
@@ -29,6 +30,12 @@ struct Error<Subscriber>
2930
using Type = SubscriberError;
3031
};
3132

33+
struct SubscriberLoan
34+
{
35+
uint8_t* bytes;
36+
size_t number_of_bytes;
37+
};
38+
3239
/// @brief Implementation of the RMW subscriber for iceoryx2
3340
/// @details The implementation supports both copy and loan-based data access patterns,
3441
/// allowing for efficient zero-copy communication when possible.
@@ -54,8 +61,12 @@ class RMW_PUBLIC Subscriber
5461
/// @param[out] error Optional error that is set if construction fails
5562
/// @param[in] node The node that owns this subscriber
5663
/// @param[in] topic The topic name to subscribe to
57-
/// @param[in] type The message type name
58-
Subscriber(CreationLock, iox::optional<ErrorType>& error, Node& node, const char* topic, const char* type);
64+
/// @param[in] typesupport The message typesupport
65+
Subscriber(CreationLock,
66+
iox::optional<ErrorType>& error,
67+
Node& node,
68+
const char* topic,
69+
const rosidl_message_type_support_t* type_support);
5970

6071
/// @brief Get the unique identifier of the subscriber
6172
/// @return Optional containing the raw ID of the subscriber
@@ -65,9 +76,9 @@ class RMW_PUBLIC Subscriber
6576
/// @return The topic name as string reference
6677
auto topic() const -> const std::string&;
6778

68-
/// @brief Get the message type name
69-
/// @return The type name as string reference
70-
auto type() const -> const std::string&;
79+
/// @brief Get the typesupport used by the publisher
80+
/// @return Pointer to the typesupport stored in the loaded typesupport library
81+
auto typesupport() const -> const rosidl_message_type_support_t*;
7182

7283
/// @brief Get the service name used internally, required for matching via iceoryx2
7384
/// @return The service name as string
@@ -80,16 +91,16 @@ class RMW_PUBLIC Subscriber
8091

8192
/// @brief Take a loaned message without copying
8293
/// @return Expected containing optional pointer to the loaned message memory
83-
auto take_loan() -> iox::expected<iox::optional<const void*>, ErrorType>;
94+
auto take_loan() -> iox::expected<iox::optional<SubscriberLoan>, ErrorType>;
8495

8596
/// @brief Return previously loaned message memory
8697
/// @param[in] loaned_memory Pointer to the loaned memory to return
8798
/// @return Expected containing void if successful
88-
auto return_loan(void* loaned_memory) -> iox::expected<void, ErrorType>;
99+
auto return_loan(void* loan) -> iox::expected<void, ErrorType>;
89100

90101
private:
91102
const std::string m_topic;
92-
const std::string m_type;
103+
const rosidl_message_type_support_t* m_typesupport;
93104
const std::string m_service_name;
94105

95106
iox::optional<IdType> m_iox2_unique_id;

rmw_iceoryx2_cxx/package.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<depend>rosidl_typesupport_fastrtps_cpp</depend>
2525
<depend>rosidl_typesupport_introspection_c</depend>
2626
<depend>rosidl_typesupport_introspection_cpp</depend>
27-
<depend>fastrtps</depend>
27+
<depend>fastcdr</depend>
2828

2929
<depend>iceoryx2_cxx</depend>
3030

rmw_iceoryx2_cxx/src/impl/message/serialization.cpp

Lines changed: 0 additions & 10 deletions
This file was deleted.

rmw_iceoryx2_cxx/src/impl/runtime/publisher.cpp

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "rmw_iceoryx2_cxx/impl/common/error_message.hpp"
1313
#include "rmw_iceoryx2_cxx/impl/common/names.hpp"
14+
#include "rmw_iceoryx2_cxx/impl/message/introspection.hpp"
1415
#include "rmw_iceoryx2_cxx/impl/middleware/iceoryx2.hpp"
1516

1617
namespace rmw::iox2
@@ -20,12 +21,11 @@ Publisher::Publisher(CreationLock,
2021
iox::optional<ErrorType>& error,
2122
Node& node,
2223
const char* topic,
23-
const char* type,
24-
const uint64_t payload_size)
24+
const rosidl_message_type_support_t* type_support)
2525
: m_topic{topic}
26-
, m_type{type}
27-
, m_service_name{::rmw::iox2::names::topic(topic)}
28-
, m_payload_size{payload_size} {
26+
, m_typesupport{type_support}
27+
, m_unserialized_size{::rmw::iox2::message_size(type_support)}
28+
, m_service_name{::rmw::iox2::names::topic(topic)} {
2929
auto iox2_service_name = Iceoryx2::ServiceName::create(m_service_name.c_str());
3030
if (iox2_service_name.has_error()) {
3131
RMW_IOX2_CHAIN_ERROR_MSG(::iox::into<const char*>(iox2_service_name.error()));
@@ -40,6 +40,8 @@ Publisher::Publisher(CreationLock,
4040
// TODO: make configurable
4141
.max_publishers(64)
4242
.max_subscribers(64)
43+
.history_size(10)
44+
.subscriber_max_buffer_size(10)
4345
.payload_alignment(8) // All ROS2 messages have alignment 8. Maybe?
4446
.open_or_create(); // TODO: set attribute for ROS typename
4547

@@ -49,7 +51,10 @@ Publisher::Publisher(CreationLock,
4951
return;
5052
}
5153

52-
auto publisher = iox2_pubsub_service.value().publisher_builder().initial_max_slice_len(m_payload_size).create();
54+
auto publisher = iox2_pubsub_service.value()
55+
.publisher_builder()
56+
.initial_max_slice_len(::rmw::iox2::message_size(m_typesupport))
57+
.create();
5358
if (publisher.has_error()) {
5459
RMW_IOX2_CHAIN_ERROR_MSG(::iox::into<const char*>(publisher.error()));
5560
error.emplace(ErrorType::PUBLISHER_CREATION_FAILURE);
@@ -84,23 +89,25 @@ auto Publisher::topic() const -> const std::string& {
8489
return m_topic;
8590
}
8691

87-
auto Publisher::type() const -> const std::string& {
88-
return m_type;
92+
auto Publisher::typesupport() const -> const rosidl_message_type_support_t* {
93+
return m_typesupport;
8994
}
9095

91-
auto Publisher::service_name() const -> const std::string& {
92-
return m_service_name;
96+
97+
auto Publisher::unserialized_size() const -> uint64_t {
98+
return m_unserialized_size;
9399
}
94100

95-
auto Publisher::payload_size() const -> uint64_t {
96-
return m_payload_size;
101+
auto Publisher::service_name() const -> const std::string& {
102+
return m_service_name;
97103
}
98104

99-
auto Publisher::loan() -> iox::expected<void*, ErrorType> {
105+
// TODO: Make return uint8_t
106+
auto Publisher::loan(uint64_t number_of_bytes) -> iox::expected<void*, ErrorType> {
100107
using iox::err;
101108
using iox::ok;
102109

103-
auto sample = m_iox2_publisher->loan_slice_uninit(m_payload_size);
110+
auto sample = m_iox2_publisher->loan_slice_uninit(number_of_bytes);
104111
if (sample.has_error()) {
105112
return err(ErrorType::LOAN_FAILURE);
106113
}
@@ -148,13 +155,13 @@ auto Publisher::publish_loan(void* loaned_memory) -> iox::expected<void, ErrorTy
148155
return ok();
149156
}
150157

151-
auto Publisher::publish_copy(const void* msg, uint64_t size) -> iox::expected<void, ErrorType> {
158+
auto Publisher::publish_copy(const void* data, uint64_t number_of_bytes) -> iox::expected<void, ErrorType> {
152159
using ::iox::err;
153160
using ::iox::ImmutableSlice;
154161
using ::iox::ok;
155162

156163
// Send
157-
auto payload = ImmutableSlice<uint8_t>{static_cast<const uint8_t*>(msg), size};
164+
auto payload = ImmutableSlice<uint8_t>{static_cast<const uint8_t*>(data), number_of_bytes};
158165

159166
if (auto result = m_iox2_publisher->send_slice_copy(payload); result.has_error()) {
160167
RMW_IOX2_CHAIN_ERROR_MSG(::iox::into<const char*>(result.error()));

0 commit comments

Comments
 (0)