Skip to content

Commit 9e93214

Browse files
committed
feat(LibCarla/ros2): add GenericCdrPubSubType, wire FastDDS middleware to CDR, add 6 tests
Replace 30 fastddsgen-generated PubSubType classes (and FastDDSTypeMap.h) with a single GenericCdrPubSubType<T> template that implements TopicDataType by delegating serialize()/deserialize() to CdrSerialization.h, and type_name()/ m_typeSize to CdrTopicInfo<T>. Rewrite FastDDSPublisherMiddleware and FastDDSSubscriberMiddleware to use GenericCdrPubSubType instead of FastDDSTypeMap. Remove to_fastdds()/from_fastdds() conversion calls. Publishers now pass the POD msg pointer directly to DataWriter::write(); subscribers receive CDR payloads directly into _message_ptr. Add 6 generic_cdr_pubsubtype tests covering: type name contract, m_typeSize, serialize/deserialize round-trip via SerializedPayload_t (fixed-size and string types), createData/deleteData, and getKey(). Update cmake/test/CMakeLists.txt to also link libfastrtps.a and libfoonathan_memory (required by TopicDataType static initializers). Server test count: 111 to 117.
1 parent 5a9dd3b commit 9e93214

File tree

5 files changed

+118
-239
lines changed

5 files changed

+118
-239
lines changed

LibCarla/cmake/test/CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ foreach(target ${build_targets})
5454
target_include_directories(${target} PRIVATE
5555
"${libcarla_source_path}/test")
5656

57-
# Server tests exercise CDR serialization (CdrSerialization.h) which
58-
# requires Fast-CDR headers and the libfastcdr runtime. Enable exceptions
59-
# for this target because Fast-CDR templates use try/catch internally.
57+
# Server tests exercise CDR serialization (CdrSerialization.h) and
58+
# GenericCdrPubSubType (inherits TopicDataType from fastrtps).
59+
# Enable exceptions because Fast-CDR templates use try/catch internally.
6060
if (CMAKE_BUILD_TYPE STREQUAL "Server")
6161
target_include_directories(${target} SYSTEM PRIVATE "${FASTDDS_INCLUDE_PATH}")
6262
target_compile_options(${target} PRIVATE -fexceptions)
63+
target_link_libraries(${target} "${FASTDDS_LIB_PATH}/libfastrtps.a")
6364
target_link_libraries(${target} "${FASTDDS_LIB_PATH}/libfastcdr.a")
65+
target_link_libraries(${target} "${FASTDDS_LIB_PATH}/libfoonathan_memory-0.7.3.a")
6466
endif()
6567

6668
if (WIN32)

LibCarla/source/carla/ros2/dds/fastdds/FastDDSPublisherMiddleware.h

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#pragma once
66

77
#include "carla/ros2/dds/IDDSPublisherMiddleware.h"
8-
#include "carla/ros2/dds/fastdds/FastDDSTypeMap.h"
8+
#include "carla/ros2/dds/fastdds/GenericCdrPubSubType.h"
99
#include "carla/Logging.h"
1010

1111
#include <fastdds/dds/domain/DomainParticipant.hpp>
@@ -30,17 +30,14 @@ using erc = eprosima::fastrtps::types::ReturnCode_t;
3030

3131
/// FastDDS implementation of IDDSPublisherMiddleware.
3232
/// Parameterized on a traits type T that provides:
33-
/// T::msg_type — the message type
34-
/// Native FastDDS types are resolved via FastDDSTypeMap<T::msg_type>.
33+
/// T::msg_type — the message type (a carla::ros2::msg::* POD struct)
34+
/// Serialization is handled by GenericCdrPubSubType<msg_type> via CdrSerialization.h.
3535
template<typename T>
3636
class FastDDSPublisherMiddleware
3737
: public IDDSPublisherMiddleware,
3838
public eprosima::fastdds::dds::DataWriterListener {
3939
public:
4040
using msg_type = typename T::msg_type;
41-
using type_map = FastDDSTypeMap<msg_type>;
42-
using fastdds_type = typename type_map::fastdds_type;
43-
using fastdds_pubsub_type = typename type_map::fastdds_pubsub_type;
4441

4542
void on_publication_matched(
4643
efd::DataWriter* writer,
@@ -108,10 +105,8 @@ class FastDDSPublisherMiddleware
108105
}
109106

110107
bool Publish(void* message_data) override {
111-
auto* msg = static_cast<msg_type*>(message_data);
112-
to_fastdds(*msg, _fastdds_msg);
113108
eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
114-
erc rcode = _datawriter->write(&_fastdds_msg, instance_handle);
109+
erc rcode = _datawriter->write(message_data, instance_handle);
115110
if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
116111
return true;
117112
}
@@ -133,11 +128,10 @@ class FastDDSPublisherMiddleware
133128
efd::Publisher* _publisher { nullptr };
134129
efd::Topic* _topic { nullptr };
135130
efd::DataWriter* _datawriter { nullptr };
136-
efd::TypeSupport _type { new fastdds_pubsub_type() };
131+
efd::TypeSupport _type { new GenericCdrPubSubType<msg_type>() };
137132

138-
fastdds_type _fastdds_msg;
139-
std::string _topic_name;
140-
bool _alive { false };
133+
std::string _topic_name;
134+
bool _alive { false };
141135
};
142136

143137
} // namespace ros2

LibCarla/source/carla/ros2/dds/fastdds/FastDDSSubscriberMiddleware.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#pragma once
66

77
#include "carla/ros2/dds/IDDSSubscriberMiddleware.h"
8-
#include "carla/ros2/dds/fastdds/FastDDSTypeMap.h"
8+
#include "carla/ros2/dds/fastdds/GenericCdrPubSubType.h"
99
#include "carla/Logging.h"
1010

1111
#include <fastdds/dds/domain/DomainParticipant.hpp>
@@ -31,17 +31,14 @@ using erc = eprosima::fastrtps::types::ReturnCode_t;
3131

3232
/// FastDDS implementation of IDDSSubscriberMiddleware.
3333
/// Parameterized on traits type S that provides:
34-
/// S::msg_type — the message type
35-
/// Native FastDDS types are resolved via FastDDSTypeMap<S::msg_type>.
34+
/// S::msg_type — the message type (a carla::ros2::msg::* POD struct)
35+
/// Deserialization is handled by GenericCdrPubSubType<msg_type> via CdrSerialization.h.
3636
template<typename S>
3737
class FastDDSSubscriberMiddleware
3838
: public IDDSSubscriberMiddleware,
3939
public eprosima::fastdds::dds::DataReaderListener {
4040
public:
4141
using msg_type = typename S::msg_type;
42-
using type_map = FastDDSTypeMap<msg_type>;
43-
using fastdds_type = typename type_map::fastdds_type;
44-
using fastdds_pubsub_type = typename type_map::fastdds_pubsub_type;
4542

4643
void on_subscription_matched(
4744
efd::DataReader* reader,
@@ -51,9 +48,8 @@ class FastDDSSubscriberMiddleware
5148

5249
void on_data_available(efd::DataReader* reader) override {
5350
efd::SampleInfo info;
54-
erc rcode = reader->take_next_sample(&_fastdds_msg, &info);
51+
erc rcode = reader->take_next_sample(_message_ptr, &info);
5552
if (rcode == erc::ReturnCodeValue::RETCODE_OK) {
56-
from_fastdds(_fastdds_msg, *_message_ptr);
5753
*_new_message_ptr = true;
5854
} else {
5955
log_error("FastDDSSubscriberMiddleware::on_data_available (",
@@ -137,11 +133,10 @@ class FastDDSSubscriberMiddleware
137133
efd::Subscriber* _subscriber { nullptr };
138134
efd::Topic* _topic { nullptr };
139135
efd::DataReader* _datareader { nullptr };
140-
efd::TypeSupport _type { new fastdds_pubsub_type() };
136+
efd::TypeSupport _type { new GenericCdrPubSubType<msg_type>() };
141137

142-
fastdds_type _fastdds_msg;
143-
msg_type* _message_ptr { nullptr };
144-
bool* _new_message_ptr { nullptr };
138+
msg_type* _message_ptr { nullptr };
139+
bool* _new_message_ptr { nullptr };
145140

146141
std::string _topic_name;
147142
bool _alive { false };

LibCarla/source/carla/ros2/dds/fastdds/FastDDSTypeMap.h

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

0 commit comments

Comments
 (0)