diff --git a/rmw_fastrtps_cpp/CMakeLists.txt b/rmw_fastrtps_cpp/CMakeLists.txt index 0b233c3fb..c80818421 100644 --- a/rmw_fastrtps_cpp/CMakeLists.txt +++ b/rmw_fastrtps_cpp/CMakeLists.txt @@ -72,6 +72,7 @@ add_library(rmw_fastrtps_cpp src/rmw_get_topic_endpoint_info.cpp src/rmw_guard_condition.cpp src/rmw_init.cpp + src/rmw_is_loaned_message_valid.cpp src/rmw_node.cpp src/rmw_node_info_and_types.cpp src/rmw_node_names.cpp diff --git a/rmw_fastrtps_cpp/src/rmw_is_loaned_message_valid.cpp b/rmw_fastrtps_cpp/src/rmw_is_loaned_message_valid.cpp new file mode 100644 index 000000000..a5d1586f0 --- /dev/null +++ b/rmw_fastrtps_cpp/src/rmw_is_loaned_message_valid.cpp @@ -0,0 +1,28 @@ +// Copyright 2026 Open Source Robotics Foundation, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include "rmw/rmw.h" +#include "rmw_fastrtps_shared_cpp/rmw_is_loaned_message_valid.hpp" +#include "rmw_fastrtps_cpp/identifier.hpp" +extern "C" { +rmw_ret_t +rmw_is_loaned_message_valid( + const rmw_subscription_t * subscription, + const void * loaned_message, + const rmw_message_info_t * message_info, + bool * is_valid) +{ + return rmw_fastrtps_shared_cpp::rmw_is_loaned_message_valid( + subscription, loaned_message, message_info, is_valid); +} +} // extern "C" diff --git a/rmw_fastrtps_dynamic_cpp/CMakeLists.txt b/rmw_fastrtps_dynamic_cpp/CMakeLists.txt index b8cfe25b5..2b9b115fe 100644 --- a/rmw_fastrtps_dynamic_cpp/CMakeLists.txt +++ b/rmw_fastrtps_dynamic_cpp/CMakeLists.txt @@ -70,6 +70,7 @@ add_library(rmw_fastrtps_dynamic_cpp src/rmw_get_topic_endpoint_info.cpp src/rmw_guard_condition.cpp src/rmw_init.cpp + src/rmw_is_loaned_message_valid.cpp src/rmw_logging.cpp src/rmw_node.cpp src/rmw_node_info_and_types.cpp diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_is_loaned_message_valid.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_is_loaned_message_valid.cpp new file mode 100644 index 000000000..964010f2f --- /dev/null +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_is_loaned_message_valid.cpp @@ -0,0 +1,28 @@ +// Copyright 2026 Open Source Robotics Foundation, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include "rmw/rmw.h" +#include "rmw_fastrtps_shared_cpp/rmw_is_loaned_message_valid.hpp" +#include "rmw_fastrtps_dynamic_cpp/identifier.hpp" +extern "C" { +rmw_ret_t +rmw_is_loaned_message_valid( + const rmw_subscription_t * subscription, + const void * loaned_message, + const rmw_message_info_t * message_info, + bool * is_valid) +{ + return rmw_fastrtps_shared_cpp::rmw_is_loaned_message_valid( + subscription, loaned_message, message_info, is_valid); +} +} // extern "C" diff --git a/rmw_fastrtps_shared_cpp/CMakeLists.txt b/rmw_fastrtps_shared_cpp/CMakeLists.txt index 2475015ec..32f3ed8f7 100644 --- a/rmw_fastrtps_shared_cpp/CMakeLists.txt +++ b/rmw_fastrtps_shared_cpp/CMakeLists.txt @@ -75,6 +75,7 @@ add_library(rmw_fastrtps_shared_cpp src/rmw_get_topic_endpoint_info.cpp src/rmw_guard_condition.cpp src/rmw_init.cpp + src/rmw_is_loaned_message_valid.cpp src/rmw_logging.cpp src/rmw_node.cpp src/rmw_node_info_and_types.cpp diff --git a/rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/rmw_is_loaned_message_valid.hpp b/rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/rmw_is_loaned_message_valid.hpp new file mode 100644 index 000000000..17a69e410 --- /dev/null +++ b/rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/rmw_is_loaned_message_valid.hpp @@ -0,0 +1,44 @@ +// Copyright 2026 Open Source Robotics Foundation, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef RMW_FASTRTPS_SHARED_CPP__RMW_IS_LOANED_MESSAGE_VALID_HPP_ +#define RMW_FASTRTPS_SHARED_CPP__RMW_IS_LOANED_MESSAGE_VALID_HPP_ + +#include "rmw_fastrtps_shared_cpp/visibility_control.h" +#include "rmw/rmw.h" + +namespace rmw_fastrtps_shared_cpp +{ +/** + * Check if a loaned message is still valid (not overwritten by middleware). + * + * This function checks if the loaned message memory has been invalidated + * by the middleware (e.g., when using shared memory with Fast-DDS data-sharing). + * + * \param[in] subscription The rmw subscription handle + * \param[in] loaned_message Pointer to the loaned message data + * \param[in] message_info Message info containing publisher_gid and sequence_number + * \param[out] is_valid Output parameter indicating if the message is valid + * \return RMW_RET_OK if successful, RMW_RET_INVALID_ARGUMENT if parameters are invalid + */ +RMW_FASTRTPS_SHARED_CPP_PUBLIC +rmw_ret_t +rmw_is_loaned_message_valid( + const rmw_subscription_t * subscription, + const void * loaned_message, + const rmw_message_info_t * message_info, + bool * is_valid); +} // namespace rmw_fastrtps_shared_cpp + +#endif // RMW_FASTRTPS_SHARED_CPP__RMW_IS_LOANED_MESSAGE_VALID_HPP_ diff --git a/rmw_fastrtps_shared_cpp/src/rmw_is_loaned_message_valid.cpp b/rmw_fastrtps_shared_cpp/src/rmw_is_loaned_message_valid.cpp new file mode 100644 index 000000000..763cb9fb3 --- /dev/null +++ b/rmw_fastrtps_shared_cpp/src/rmw_is_loaned_message_valid.cpp @@ -0,0 +1,62 @@ +// Copyright 2026 Open Source Robotics Foundation, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "rmw_fastrtps_shared_cpp/rmw_is_loaned_message_valid.hpp" +#include + +#include "fastdds/dds/subscriber/DataReader.hpp" +#include "fastdds/dds/subscriber/SampleInfo.hpp" +#include "fastdds/rtps/common/Guid.hpp" +#include "fastdds/rtps/common/SequenceNumber.hpp" + +#include "rmw_fastrtps_shared_cpp/custom_subscriber_info.hpp" +#include "rmw_fastrtps_shared_cpp/guid_utils.hpp" + +namespace rmw_fastrtps_shared_cpp +{ + +rmw_ret_t +rmw_is_loaned_message_valid( + const rmw_subscription_t * subscription, + const void * loaned_message, + const rmw_message_info_t * message_info, + bool * is_valid) +{ + if (!subscription || !loaned_message || !message_info || !is_valid) { + return RMW_RET_INVALID_ARGUMENT; + } + + *is_valid = true; + auto * info = static_cast(subscription->data); + if (!info || !info->data_reader_) { + return RMW_RET_OK; + } + + eprosima::fastdds::dds::SampleInfo sample_info{}; + eprosima::fastdds::rtps::GUID_t writer_guid; + copy_from_byte_array_to_fastdds_guid( + message_info->publisher_gid.data, &writer_guid); + uint64_t seq = message_info->publication_sequence_number; + eprosima::fastdds::rtps::SequenceNumber_t seq_num; + seq_num.high = static_cast(seq >> 32); + seq_num.low = static_cast(seq & 0xFFFFFFFF); + sample_info.sample_identity.writer_guid(writer_guid); + sample_info.sample_identity.sequence_number(seq_num); + + *is_valid = info->data_reader_->is_sample_valid(loaned_message, &sample_info); + + return RMW_RET_OK; +} + +} // namespace rmw_fastrtps_shared_cpp