Skip to content

Commit

Permalink
[#500] Expose UniquePublisherId bytes in CXX API
Browse files Browse the repository at this point in the history
  • Loading branch information
orecham committed Nov 2, 2024
1 parent 29c71af commit a5df017
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
9 changes: 8 additions & 1 deletion iceoryx2-ffi/cxx/include/iox2/unique_port_id.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@
#ifndef IOX2_UNIQUE_PORT_ID_HPP
#define IOX2_UNIQUE_PORT_ID_HPP

#include "iox/optional.hpp"
#include "iox2/internal/iceoryx2.hpp"

#include <array>

namespace iox2 {

constexpr uint64_t UNIQUE_PORT_ID_LENGTH = 128;

/// The system-wide unique id of a [`Publisher`].
class UniquePublisherId {
public:
Expand All @@ -25,6 +31,8 @@ class UniquePublisherId {
auto operator=(UniquePublisherId&& rhs) noexcept -> UniquePublisherId&;
~UniquePublisherId();

auto bytes() -> iox::optional<std::array<uint8_t, UNIQUE_PORT_ID_LENGTH>>;

private:
template <ServiceType, typename, typename>
friend class Publisher;
Expand All @@ -38,7 +46,6 @@ class UniquePublisherId {
iox2_unique_publisher_id_h m_handle = nullptr;
};


/// The system-wide unique id of a [`Subscriber`].
class UniqueSubscriberId {
public:
Expand Down
9 changes: 9 additions & 0 deletions iceoryx2-ffi/cxx/src/unique_port_id.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ UniquePublisherId::UniquePublisherId(iox2_unique_publisher_id_h handle)
: m_handle { handle } {
}

auto UniquePublisherId::bytes() -> iox::optional<std::array<uint8_t, UNIQUE_PORT_ID_LENGTH>> {
if (m_handle != nullptr) {
std::array<uint8_t, UNIQUE_PORT_ID_LENGTH> unique_port_id {};
iox2_unique_publisher_id_value(m_handle, unique_port_id.data());
return unique_port_id;
}
return iox::nullopt;
};

void UniquePublisherId::drop() {
if (m_handle != nullptr) {
iox2_unique_publisher_id_drop(m_handle);
Expand Down
9 changes: 9 additions & 0 deletions iceoryx2-ffi/cxx/tests/src/unique_port_id_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
#include "iox2/publisher.hpp"
#include "iox2/service_name.hpp"
#include "iox2/subscriber.hpp"
#include "iox2/unique_port_id.hpp"

#include "test.hpp"

#include <atomic>
#include <gtest/gtest.h>

namespace {
using namespace iox2;
Expand Down Expand Up @@ -63,6 +65,13 @@ struct UniquePortIdTest : public ::testing::Test {

TYPED_TEST_SUITE(UniquePortIdTest, iox2_testing::ServiceTypes);

TYPED_TEST(UniquePortIdTest, unique_publisher_id_value) {
auto null_id = std::array<uint8_t, iox2::UNIQUE_PORT_ID_LENGTH> {};
auto unique_port_id = this->publisher_1.id();
ASSERT_TRUE(unique_port_id.bytes().has_value());
ASSERT_NE(unique_port_id.bytes().value(), null_id);
}

TYPED_TEST(UniquePortIdTest, unique_port_id_from_same_port_is_equal) {
ASSERT_TRUE(this->listener_1.id() == this->listener_1.id());
ASSERT_TRUE(this->notifier_1.id() == this->notifier_1.id());
Expand Down
18 changes: 18 additions & 0 deletions iceoryx2-ffi/ffi/src/api/unique_publisher_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ impl HandleToType for iox2_unique_publisher_id_h_ref {

// BEGIN C API

#[no_mangle]
unsafe extern "C" fn iox2_unique_publisher_id_value(
handle: iox2_unique_publisher_id_h,
id_ptr: *mut u8,
) {
handle.assert_non_null();

let h = &mut *handle.as_type();

if let Some(Some(id)) = (h.value.internal.as_ptr() as *const Option<UniquePublisherId>).as_ref()
{
let bytes = id.value().to_ne_bytes();
unsafe {
std::ptr::copy_nonoverlapping(bytes.as_ptr(), id_ptr, bytes.len());
}
}
}

/// This function needs to be called to destroy the unique publisher id!
///
/// # Arguments
Expand Down
5 changes: 5 additions & 0 deletions iceoryx2/src/port/port_identifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ macro_rules! generate_id {
pub fn new() -> Self {
Self::default()
}

/// Returns the underlying raw value of the ID
pub fn value(&self) -> u128 {
self.0.value()
}
}
};
}
Expand Down

0 comments on commit a5df017

Please sign in to comment.