Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type hash to rmw_topic_endpoint_info_t (rep2011) #348

Merged
merged 6 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 49 additions & 11 deletions rmw/include/rmw/topic_endpoint_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extern "C"
#endif

#include "rcutils/allocator.h"
#include "rosidl_runtime_c/type_hash.h"
#include "rmw/types.h"
#include "rmw/visibility_control.h"

Expand All @@ -33,8 +34,10 @@ typedef struct RMW_PUBLIC_TYPE rmw_topic_endpoint_info_s
const char * node_name;
/// Namespace of the node
const char * node_namespace;
/// The associated topic type
/// The associated topic type's name
const char * topic_type;
/// Hashed value for topic type's description
rosidl_type_hash_t topic_type_hash;
/// The endpoint type
rmw_endpoint_type_t endpoint_type;
/// The GID of the endpoint
Expand All @@ -55,7 +58,7 @@ rmw_get_zero_initialized_topic_endpoint_info(void);

/// Finalize a topic endpoint info data structure.
/**
* This function deallocates all allocated members of the given data structure,
* Deallocates all allocated members of the given data structure,
* and then zero initializes it.
* If a logical error, such as `RMW_RET_INVALID_ARGUMENT`, ensues, this function
* will return early, leaving the given data structure unchanged.
Expand Down Expand Up @@ -94,8 +97,8 @@ rmw_topic_endpoint_info_fini(

/// Set the topic type in the given topic endpoint info data structure.
/**
* This functions allocates memory and copies the value of the `topic_type`
* argument to set the data structure `topic_type` member.
* Allocates memory and copies the value of the `topic_type`
* argument to set the data structure's `topic_type` member.
*
* <hr>
* Attribute | Adherence
Expand Down Expand Up @@ -135,10 +138,45 @@ rmw_topic_endpoint_info_set_topic_type(
const char * topic_type,
rcutils_allocator_t * allocator);

/// Set the topic type hash in the given topic endpoint info data structure.
/**
* Assigns the value of the `topic_type_hash` argument to the data structure's
* `topic_type_hash` member.
*
* <hr>
* Attribute | Adherence
* ------------------ | -------------
* Allocates Memory | No
* Thread-Safe | No
* Uses Atomics | No
* Lock-Free | Yes
*
* \par Thread-safety
* Setting a member is a reentrant procedure, but:
* - Access to the topic endpoint info data structure is not synchronized.
* It is not safe to read or write the `topic_type_hash` member of the given `topic_endpoint`
* while setting it.
* Concurrent `topic_type_hash` reads are safe, but concurrent reads and writes are not.
*
* \param[inout] topic_endpoint_info Data structure to be populated.
* \param[in] topic_type_hash Topic type hash to be copied.
* \returns `RMW_RET_OK` if successful, or
* \returns `RMW_RET_INVALID_ARGUMENT` if `topic_endpoint_info` is NULL, or
* \returns `RMW_RET_INVALID_ARGUMENT` if `topic_type_hash` is NULL, or
* \returns `RMW_RET_ERROR` when an unspecified error occurs.
* \remark This function sets the RMW error state on failure.
*/
RMW_PUBLIC
RMW_WARN_UNUSED
rmw_ret_t
rmw_topic_endpoint_info_set_topic_type_hash(
rmw_topic_endpoint_info_t * topic_endpoint_info,
const rosidl_type_hash_t * type_hash);

/// Set the node name in the given topic endpoint info data structure.
/**
* This functions allocates memory and copies the value of the `node_name`
* argument to set the data structure `node_name` member.
* Allocates memory and copies the value of the `node_name`
* argument to set the data structure's `node_name` member.
*
* <hr>
* Attribute | Adherence
Expand Down Expand Up @@ -180,8 +218,8 @@ rmw_topic_endpoint_info_set_node_name(

/// Set the node namespace in the given topic endpoint info data structure.
/**
* This functions allocates memory and copies the value of the `node_namespace`
* argument to set the data structure `node_namespace` member.
* Allocates memory and copies the value of the `node_namespace`
* argument to set the data structure's `node_namespace` member.
*
* <hr>
* Attribute | Adherence
Expand Down Expand Up @@ -223,7 +261,7 @@ rmw_topic_endpoint_info_set_node_namespace(

/// Set the endpoint type in the given topic endpoint info data structure.
/**
* This functions assigns the value of the `type` argument to the data structure
* Assigns the value of the `type` argument to the data structure's
* `endpoint_type` member.
*
* <hr>
Expand Down Expand Up @@ -256,7 +294,7 @@ rmw_topic_endpoint_info_set_endpoint_type(

/// Set the endpoint gid in the given topic endpoint info data structure.
/**
* This functions copies the value of the `gid` argument to the data structure
* Copies the value of the `gid` argument to the data structure's
* `endpoint_gid` member.
*
* <hr>
Expand Down Expand Up @@ -293,7 +331,7 @@ rmw_topic_endpoint_info_set_gid(

/// Set the endpoint QoS profile in the given topic endpoint info data structure.
/**
* This functions assigns the value of the `qos_profile` argument to the data structure
* Assigns the value of the `qos_profile` argument to the data structure's
* `qos_profile` member.
*
* <hr>
Expand Down
12 changes: 12 additions & 0 deletions rmw/src/topic_endpoint_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ rmw_topic_endpoint_info_set_topic_type(
return _rmw_topic_endpoint_info_copy_str(&topic_endpoint_info->topic_type, topic_type, allocator);
}

rmw_ret_t
rmw_topic_endpoint_info_set_topic_type_hash(
rmw_topic_endpoint_info_t * topic_endpoint_info,
const rosidl_type_hash_t * type_hash)
{
RCUTILS_CAN_RETURN_WITH_ERROR_OF(RMW_RET_INVALID_ARGUMENT);
RCUTILS_CHECK_ARGUMENT_FOR_NULL(topic_endpoint_info, RMW_RET_INVALID_ARGUMENT);
RCUTILS_CHECK_ARGUMENT_FOR_NULL(type_hash, RMW_RET_INVALID_ARGUMENT);
topic_endpoint_info->topic_type_hash = *type_hash;
return RMW_RET_OK;
}

rmw_ret_t
rmw_topic_endpoint_info_set_node_name(
rmw_topic_endpoint_info_t * topic_endpoint_info,
Expand Down
26 changes: 26 additions & 0 deletions rmw/test/test_topic_endpoint_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,32 @@ TEST(test_topic_endpoint_info, set_topic_type) {
"test_topic_type") << "Topic Type value is not as expected";
}

TEST(test_topic_endpoint_info, set_topic_type_hash) {
rmw_topic_endpoint_info_t topic_endpoint_info = rmw_get_zero_initialized_topic_endpoint_info();
rosidl_type_hash_t type_hash = rosidl_get_zero_initialized_type_hash();
type_hash.version = 22;
for (uint8_t i = 0; i < ROSIDL_TYPE_HASH_SIZE; i++) {
type_hash.value[i] = i;
}

rmw_ret_t ret = rmw_topic_endpoint_info_set_topic_type_hash(nullptr, &type_hash);
EXPECT_EQ(ret, RMW_RET_INVALID_ARGUMENT) <<
"Expected invalid argument for null topic_endpoint_info";
rmw_reset_error();

ret = rmw_topic_endpoint_info_set_topic_type_hash(&topic_endpoint_info, nullptr);
EXPECT_EQ(ret, RMW_RET_INVALID_ARGUMENT) << "Expected invalid argument for null type_hash";
rmw_reset_error();

ret = rmw_topic_endpoint_info_set_topic_type_hash(&topic_endpoint_info, &type_hash);
EXPECT_EQ(ret, RMW_RET_OK) << "Expected OK for valid arguments";

EXPECT_EQ(topic_endpoint_info.topic_type_hash.version, 22);
for (size_t i = 0; i < ROSIDL_TYPE_HASH_SIZE; i++) {
EXPECT_EQ(i, topic_endpoint_info.topic_type_hash.value[i]);
}
}

TEST(test_topic_endpoint_info, set_node_name) {
rmw_topic_endpoint_info_t topic_endpoint_info = rmw_get_zero_initialized_topic_endpoint_info();
rcutils_allocator_t allocator = rcutils_get_default_allocator();
Expand Down