Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 23 additions & 0 deletions rcl/include/rcl/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,29 @@ RCL_WARN_UNUSED
const char *
rcl_node_get_namespace(const rcl_node_t * node);

/// Return the fully qualified name of the node.
/**
* This function returns the node's internal namespace and name combined string.
* This function can fail, and therefore return `NULL`, if:
* - node is `NULL`
* - node has not been initialized (the implementation is invalid)
*
* <hr>
* Attribute | Adherence
* ------------------ | -------------
* Allocates Memory | Yes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to be able to use this in applications that require allocation free operations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.
name and namespace are stored in the specific rmw implementation (if I understand correctly) and I hesitated from making changes across many repos. Thats why I decided to store the computed fq_name in the rcl_node_impl_t structure.

* Thread-Safe | No
* Uses Atomics | No
* Lock-Free | Yes
*
* \param[in] node pointer to the node
* \return fully qualified name string if successful, otherwise `NULL`
*/
RCL_PUBLIC
RCL_WARN_UNUSED
const char *
rcl_node_get_fully_qualified_name(const rcl_node_t * node);

/// Return the rcl node options.
/**
* This function returns the node's internal options struct.
Expand Down
25 changes: 25 additions & 0 deletions rcl/src/rcl/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,31 @@ rcl_node_get_namespace(const rcl_node_t * node)
return node->impl->rmw_node_handle->namespace_;
}

const char *
rcl_node_get_fully_qualified_name(const rcl_node_t * node)
{
if (!rcl_node_is_valid_except_context(node)) {
return NULL; // error already set
}

const char * name = rcl_node_get_name(node);
const char * ns = rcl_node_get_namespace(node);
char * fq_name = NULL;

if ('/' == ns[strlen(ns) - 1]) {
fq_name = (char *)calloc(strlen(ns) + strlen(name), sizeof(char));
strcpy(fq_name, ns);
strcat(fq_name, name);
}
else {
fq_name = (char *)calloc(strlen(ns) + strlen(name) + 1, sizeof(char));
strcpy(fq_name, ns);
strcat(fq_name, "/");
strcat(fq_name, name);
}
return fq_name;
}

const rcl_node_options_t *
rcl_node_get_options(const rcl_node_t * node)
{
Expand Down
23 changes: 23 additions & 0 deletions rcl/test/rcl/test_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <regex>
#include <string>
#include <cstdlib>

#include "rcl/rcl.h"
#include "rcl/node.h"
Expand Down Expand Up @@ -99,6 +100,7 @@ TEST_F(CLASSNAME(TestNodeFixture, RMW_IMPLEMENTATION), test_rcl_node_accessors)
rcl_node_t invalid_node = rcl_get_zero_initialized_node();
const char * name = "test_rcl_node_accessors_node";
const char * namespace_ = "/ns";
const char * fq_name = "/ns/test_rcl_node_accessors_node";
rcl_node_options_t default_options = rcl_node_get_default_options();
default_options.domain_id = 42; // Set the domain id to something explicit.
ret = rcl_node_init(&invalid_node, name, namespace_, &invalid_context, &default_options);
Expand Down Expand Up @@ -204,6 +206,27 @@ TEST_F(CLASSNAME(TestNodeFixture, RMW_IMPLEMENTATION), test_rcl_node_accessors)
if (actual_node_namespace) {
EXPECT_EQ(std::string(namespace_), std::string(actual_node_namespace));
}
// Test rcl_node_get_fully_qualified_name().
const char * actual_fq_node_name;
actual_fq_node_name = rcl_node_get_fully_qualified_name(nullptr);
EXPECT_EQ(nullptr, actual_fq_node_name);
rcl_reset_error();
actual_fq_node_name = rcl_node_get_fully_qualified_name(&zero_node);
EXPECT_EQ(nullptr, actual_fq_node_name);
rcl_reset_error();
actual_fq_node_name = rcl_node_get_fully_qualified_name(&invalid_node);
EXPECT_TRUE(actual_fq_node_name ? true : false);
if (actual_fq_node_name) {
EXPECT_STREQ(fq_name, actual_fq_node_name);
free((char *)actual_fq_node_name);
}
rcl_reset_error();
actual_fq_node_name = rcl_node_get_fully_qualified_name(&node);
EXPECT_TRUE(actual_fq_node_name ? true : false);
if (actual_fq_node_name) {
EXPECT_EQ(std::string(fq_name), std::string(actual_fq_node_name));
free((char *)actual_fq_node_name);
}
// Test rcl_node_get_logger_name().
const char * actual_node_logger_name;
actual_node_logger_name = rcl_node_get_logger_name(nullptr);
Expand Down