From 585366d2bc4035831105746bc35374fd9f7aef58 Mon Sep 17 00:00:00 2001 From: Michel Hidalgo Date: Tue, 22 Sep 2020 16:54:22 -0300 Subject: [PATCH] Update graph API return codes. (#436) This patch affects: - rmw_get_node_names() - rmw_get_node_names_with_enclaves() - rmw_get_topic_names_and_types() - rmw_get_service_names_and_types() - rmw_get_publishers_info_by_topic() - rmw_get_subscriptions_info_by_topic() - rmw_get_subscriber_names_and_types_by_node() - rmw_get_publisher_names_and_types_by_node() - rmw_get_service_names_and_types_by_node() - rmw_get_client_names_and_types_by_node() - rmw_count_publishers() - rmw_count_subscribers() Signed-off-by: Michel Hidalgo --- rmw_fastrtps_shared_cpp/src/rmw_count.cpp | 42 +++++++--- .../src/rmw_get_topic_endpoint_info.cpp | 50 ++++------- .../src/rmw_node_info_and_types.cpp | 84 ++++++++----------- .../src/rmw_node_names.cpp | 49 +++++------ .../src/rmw_service_names_and_types.cpp | 24 +++--- .../src/rmw_topic_names_and_types.cpp | 23 ++--- 6 files changed, 122 insertions(+), 150 deletions(-) diff --git a/rmw_fastrtps_shared_cpp/src/rmw_count.cpp b/rmw_fastrtps_shared_cpp/src/rmw_count.cpp index a30d21989..59b5bd962 100644 --- a/rmw_fastrtps_shared_cpp/src/rmw_count.cpp +++ b/rmw_fastrtps_shared_cpp/src/rmw_count.cpp @@ -21,8 +21,10 @@ #include "rcutils/logging_macros.h" #include "rmw/error_handling.h" +#include "rmw/impl/cpp/macros.hpp" #include "rmw/rmw.h" #include "rmw/types.h" +#include "rmw/validate_full_topic_name.h" #include "rmw_dds_common/context.hpp" @@ -41,14 +43,24 @@ __rmw_count_publishers( const char * topic_name, size_t * count) { - if (!node) { - RMW_SET_ERROR_MSG("null node handle"); - return RMW_RET_INVALID_ARGUMENT; + RMW_CHECK_ARGUMENT_FOR_NULL(node, RMW_RET_INVALID_ARGUMENT); + RMW_CHECK_TYPE_IDENTIFIERS_MATCH( + node, + node->implementation_identifier, + identifier, + return RMW_RET_INCORRECT_RMW_IMPLEMENTATION); + RMW_CHECK_ARGUMENT_FOR_NULL(topic_name, RMW_RET_INVALID_ARGUMENT); + int validation_result = RMW_TOPIC_VALID; + rmw_ret_t ret = rmw_validate_full_topic_name(topic_name, &validation_result, nullptr); + if (RMW_RET_OK != ret) { + return ret; } - if (node->implementation_identifier != identifier) { - RMW_SET_ERROR_MSG("node handle not from this implementation"); + if (RMW_TOPIC_VALID != validation_result) { + const char * reason = rmw_full_topic_name_validation_result_string(validation_result); + RMW_SET_ERROR_MSG_WITH_FORMAT_STRING("topic_name argument is invalid: %s", reason); return RMW_RET_INVALID_ARGUMENT; } + RMW_CHECK_ARGUMENT_FOR_NULL(count, RMW_RET_INVALID_ARGUMENT); auto common_context = static_cast(node->context->impl->common); const std::string mangled_topic_name = _mangle_topic_name(ros_topic_prefix, topic_name).to_string(); @@ -62,14 +74,24 @@ __rmw_count_subscribers( const char * topic_name, size_t * count) { - if (!node) { - RMW_SET_ERROR_MSG("null node handle"); - return RMW_RET_INVALID_ARGUMENT; + RMW_CHECK_ARGUMENT_FOR_NULL(node, RMW_RET_INVALID_ARGUMENT); + RMW_CHECK_TYPE_IDENTIFIERS_MATCH( + node, + node->implementation_identifier, + identifier, + return RMW_RET_INCORRECT_RMW_IMPLEMENTATION); + RMW_CHECK_ARGUMENT_FOR_NULL(topic_name, RMW_RET_INVALID_ARGUMENT); + int validation_result = RMW_TOPIC_VALID; + rmw_ret_t ret = rmw_validate_full_topic_name(topic_name, &validation_result, nullptr); + if (RMW_RET_OK != ret) { + return ret; } - if (node->implementation_identifier != identifier) { - RMW_SET_ERROR_MSG("node handle not from this implementation"); + if (RMW_TOPIC_VALID != validation_result) { + const char * reason = rmw_full_topic_name_validation_result_string(validation_result); + RMW_SET_ERROR_MSG_WITH_FORMAT_STRING("topic_name argument is invalid: %s", reason); return RMW_RET_INVALID_ARGUMENT; } + RMW_CHECK_ARGUMENT_FOR_NULL(count, RMW_RET_INVALID_ARGUMENT); auto common_context = static_cast(node->context->impl->common); const std::string mangled_topic_name = _mangle_topic_name(ros_topic_prefix, topic_name).to_string(); diff --git a/rmw_fastrtps_shared_cpp/src/rmw_get_topic_endpoint_info.cpp b/rmw_fastrtps_shared_cpp/src/rmw_get_topic_endpoint_info.cpp index c6cd2bbda..45d4b8e61 100644 --- a/rmw_fastrtps_shared_cpp/src/rmw_get_topic_endpoint_info.cpp +++ b/rmw_fastrtps_shared_cpp/src/rmw_get_topic_endpoint_info.cpp @@ -18,6 +18,7 @@ #include #include +#include "rmw/impl/cpp/macros.hpp" #include "rmw/rmw.h" #include "rmw/types.h" #include "rmw/topic_endpoint_info_array.h" @@ -35,49 +36,28 @@ namespace rmw_fastrtps_shared_cpp { -rmw_ret_t -_validate_params( +static rmw_ret_t __validate_arguments( const char * identifier, const rmw_node_t * node, rcutils_allocator_t * allocator, const char * topic_name, rmw_topic_endpoint_info_array_t * participants_info) { - if (!identifier) { - RMW_SET_ERROR_MSG("null implementation identifier provided"); - return RMW_RET_ERROR; - } - - if (!topic_name) { - RMW_SET_ERROR_MSG("null topic_name provided"); - return RMW_RET_ERROR; - } - - if (!allocator) { - RMW_SET_ERROR_MSG("null allocator provided"); - return RMW_RET_ERROR; - } - - if (!node) { - RMW_SET_ERROR_MSG("null node handle"); - return RMW_RET_ERROR; - } - - // Get participant pointer from node - if (node->implementation_identifier != identifier) { - RMW_SET_ERROR_MSG("node handle not from this implementation"); - return RMW_RET_ERROR; - } - - if (!participants_info) { - RMW_SET_ERROR_MSG("null participants_info provided"); - return RMW_RET_ERROR; + RMW_CHECK_ARGUMENT_FOR_NULL(node, RMW_RET_INVALID_ARGUMENT); + RMW_CHECK_TYPE_IDENTIFIERS_MATCH( + node, + node->implementation_identifier, + identifier, + return RMW_RET_INCORRECT_RMW_IMPLEMENTATION); + RCUTILS_CHECK_ALLOCATOR_WITH_MSG( + allocator, "allocator argument is invalid", return RMW_RET_INVALID_ARGUMENT); + RMW_CHECK_ARGUMENT_FOR_NULL(topic_name, RMW_RET_INVALID_ARGUMENT); + if (RMW_RET_OK != rmw_topic_endpoint_info_array_check_zero(participants_info)) { + return RMW_RET_INVALID_ARGUMENT; } - return RMW_RET_OK; } - rmw_ret_t __rmw_get_publishers_info_by_topic( const char * identifier, @@ -87,7 +67,7 @@ __rmw_get_publishers_info_by_topic( bool no_mangle, rmw_topic_endpoint_info_array_t * publishers_info) { - rmw_ret_t ret = _validate_params( + rmw_ret_t ret = __validate_arguments( identifier, node, allocator, @@ -120,7 +100,7 @@ __rmw_get_subscriptions_info_by_topic( bool no_mangle, rmw_topic_endpoint_info_array_t * subscriptions_info) { - rmw_ret_t ret = _validate_params( + rmw_ret_t ret = __validate_arguments( identifier, node, allocator, diff --git a/rmw_fastrtps_shared_cpp/src/rmw_node_info_and_types.cpp b/rmw_fastrtps_shared_cpp/src/rmw_node_info_and_types.cpp index 4d95448dd..12e90b136 100644 --- a/rmw_fastrtps_shared_cpp/src/rmw_node_info_and_types.cpp +++ b/rmw_fastrtps_shared_cpp/src/rmw_node_info_and_types.cpp @@ -32,6 +32,8 @@ #include "rmw/impl/cpp/macros.hpp" #include "rmw/names_and_types.h" #include "rmw/rmw.h" +#include "rmw/validate_namespace.h" +#include "rmw/validate_node_name.h" #include "rmw_dds_common/context.hpp" @@ -45,53 +47,6 @@ namespace rmw_fastrtps_shared_cpp { -/** - * Validate the input data of node_info_and_types functions. - * - * \return RMW_RET_INVALID_ARGUMENT for null input args - * \return RMW_RET_ERROR if identifier is not the same as the input node - * \return RMW_RET_OK if all input is valid - */ -rmw_ret_t __validate_input( - const char * identifier, - const rmw_node_t * node, - rcutils_allocator_t * allocator, - const char * node_name, - const char * node_namespace, - rmw_names_and_types_t * topic_names_and_types) -{ - if (!allocator) { - RMW_SET_ERROR_MSG("allocator is null"); - return RMW_RET_INVALID_ARGUMENT; - } - if (!node) { - RMW_SET_ERROR_MSG("null node handle"); - return RMW_RET_INVALID_ARGUMENT; - } - - if (!node_name) { - RMW_SET_ERROR_MSG("null node name"); - return RMW_RET_INVALID_ARGUMENT; - } - - if (!node_namespace) { - RMW_SET_ERROR_MSG("null node namespace"); - return RMW_RET_INVALID_ARGUMENT; - } - - rmw_ret_t ret = rmw_names_and_types_check_zero(topic_names_and_types); - if (ret != RMW_RET_OK) { - return ret; - } - - // Get participant pointer from node - if (node->implementation_identifier != identifier) { - RMW_SET_ERROR_MSG("node handle not from this implementation"); - return RMW_RET_ERROR; - } - return RMW_RET_OK; -} - using GetNamesAndTypesByNodeFunction = rmw_ret_t (*)( rmw_dds_common::Context *, const std::string &, @@ -114,10 +69,37 @@ __rmw_get_topic_names_and_types_by_node( GetNamesAndTypesByNodeFunction get_names_and_types_by_node, rmw_names_and_types_t * topic_names_and_types) { - rmw_ret_t valid_input = __validate_input( - identifier, node, allocator, node_name, node_namespace, topic_names_and_types); - if (valid_input != RMW_RET_OK) { - return valid_input; + RMW_CHECK_ARGUMENT_FOR_NULL(node, RMW_RET_INVALID_ARGUMENT); + RMW_CHECK_TYPE_IDENTIFIERS_MATCH( + node, + node->implementation_identifier, + identifier, + return RMW_RET_INCORRECT_RMW_IMPLEMENTATION); + RCUTILS_CHECK_ALLOCATOR_WITH_MSG( + allocator, "allocator argument is invalid", return RMW_RET_INVALID_ARGUMENT); + int validation_result = RMW_NODE_NAME_VALID; + rmw_ret_t ret = rmw_validate_node_name(node_name, &validation_result, nullptr); + if (RMW_RET_OK != ret) { + return ret; + } + if (RMW_NODE_NAME_VALID != validation_result) { + const char * reason = rmw_node_name_validation_result_string(validation_result); + RMW_SET_ERROR_MSG_WITH_FORMAT_STRING("node_name argument is invalid: %s", reason); + return RMW_RET_INVALID_ARGUMENT; + } + validation_result = RMW_NAMESPACE_VALID; + ret = rmw_validate_namespace(node_namespace, &validation_result, nullptr); + if (RMW_RET_OK != ret) { + return ret; + } + if (RMW_NAMESPACE_VALID != validation_result) { + const char * reason = rmw_namespace_validation_result_string(validation_result); + RMW_SET_ERROR_MSG_WITH_FORMAT_STRING("node_namespace argument is invalid: %s", reason); + return RMW_RET_INVALID_ARGUMENT; + } + ret = rmw_names_and_types_check_zero(topic_names_and_types); + if (RMW_RET_OK != ret) { + return ret; } auto common_context = static_cast(node->context->impl->common); diff --git a/rmw_fastrtps_shared_cpp/src/rmw_node_names.cpp b/rmw_fastrtps_shared_cpp/src/rmw_node_names.cpp index 5b4324dc2..3a4f9bffb 100644 --- a/rmw_fastrtps_shared_cpp/src/rmw_node_names.cpp +++ b/rmw_fastrtps_shared_cpp/src/rmw_node_names.cpp @@ -24,6 +24,7 @@ #include "rmw/allocators.h" #include "rmw/convert_rcutils_ret_to_rmw_ret.h" #include "rmw/error_handling.h" +#include "rmw/impl/cpp/macros.hpp" #include "rmw/rmw.h" #include "rmw/sanity_checks.h" @@ -44,19 +45,17 @@ __rmw_get_node_names( rcutils_string_array_t * node_names, rcutils_string_array_t * node_namespaces) { - if (!node) { - RMW_SET_ERROR_MSG("null node handle"); - return RMW_RET_ERROR; + RMW_CHECK_ARGUMENT_FOR_NULL(node, RMW_RET_INVALID_ARGUMENT); + RMW_CHECK_TYPE_IDENTIFIERS_MATCH( + node, + node->implementation_identifier, + identifier, + return RMW_RET_INCORRECT_RMW_IMPLEMENTATION); + if (RMW_RET_OK != rmw_check_zero_rmw_string_array(node_names)) { + return RMW_RET_INVALID_ARGUMENT; } - if (rmw_check_zero_rmw_string_array(node_names) != RMW_RET_OK) { - return RMW_RET_ERROR; - } - if (rmw_check_zero_rmw_string_array(node_namespaces) != RMW_RET_OK) { - return RMW_RET_ERROR; - } - if (node->implementation_identifier != identifier) { - RMW_SET_ERROR_MSG("node handle not from this implementation"); - return RMW_RET_ERROR; + if (RMW_RET_OK != rmw_check_zero_rmw_string_array(node_namespaces)) { + return RMW_RET_INVALID_ARGUMENT; } auto common_context = static_cast(node->context->impl->common); @@ -76,22 +75,20 @@ __rmw_get_node_names_with_enclaves( rcutils_string_array_t * node_namespaces, rcutils_string_array_t * enclaves) { - if (!node) { - RMW_SET_ERROR_MSG("null node handle"); - return RMW_RET_ERROR; - } - if (rmw_check_zero_rmw_string_array(node_names) != RMW_RET_OK) { - return RMW_RET_ERROR; - } - if (rmw_check_zero_rmw_string_array(node_namespaces) != RMW_RET_OK) { - return RMW_RET_ERROR; + RMW_CHECK_ARGUMENT_FOR_NULL(node, RMW_RET_INVALID_ARGUMENT); + RMW_CHECK_TYPE_IDENTIFIERS_MATCH( + node, + node->implementation_identifier, + identifier, + return RMW_RET_INCORRECT_RMW_IMPLEMENTATION); + if (RMW_RET_OK != rmw_check_zero_rmw_string_array(node_names)) { + return RMW_RET_INVALID_ARGUMENT; } - if (rmw_check_zero_rmw_string_array(enclaves) != RMW_RET_OK) { - return RMW_RET_ERROR; + if (RMW_RET_OK != rmw_check_zero_rmw_string_array(node_namespaces)) { + return RMW_RET_INVALID_ARGUMENT; } - if (node->implementation_identifier != identifier) { - RMW_SET_ERROR_MSG("node handle not from this implementation"); - return RMW_RET_ERROR; + if (RMW_RET_OK != rmw_check_zero_rmw_string_array(enclaves)) { + return RMW_RET_INVALID_ARGUMENT; } auto common_context = static_cast(node->context->impl->common); diff --git a/rmw_fastrtps_shared_cpp/src/rmw_service_names_and_types.cpp b/rmw_fastrtps_shared_cpp/src/rmw_service_names_and_types.cpp index a35520657..f008ecdc1 100644 --- a/rmw_fastrtps_shared_cpp/src/rmw_service_names_and_types.cpp +++ b/rmw_fastrtps_shared_cpp/src/rmw_service_names_and_types.cpp @@ -20,6 +20,7 @@ #include "rmw/allocators.h" #include "rmw/error_handling.h" #include "rmw/get_service_names_and_types.h" +#include "rmw/impl/cpp/macros.hpp" #include "rmw/names_and_types.h" #include "rmw/types.h" @@ -40,22 +41,17 @@ __rmw_get_service_names_and_types( rcutils_allocator_t * allocator, rmw_names_and_types_t * service_names_and_types) { - if (!allocator) { - RMW_SET_ERROR_MSG("allocator is null"); + RMW_CHECK_ARGUMENT_FOR_NULL(node, RMW_RET_INVALID_ARGUMENT); + RMW_CHECK_TYPE_IDENTIFIERS_MATCH( + node, + node->implementation_identifier, + identifier, + return RMW_RET_INCORRECT_RMW_IMPLEMENTATION); + RCUTILS_CHECK_ALLOCATOR_WITH_MSG( + allocator, "allocator argument is invalid", return RMW_RET_INVALID_ARGUMENT); + if (RMW_RET_OK != rmw_names_and_types_check_zero(service_names_and_types)) { return RMW_RET_INVALID_ARGUMENT; } - if (!node) { - RMW_SET_ERROR_MSG("null node handle"); - return RMW_RET_INVALID_ARGUMENT; - } - rmw_ret_t ret = rmw_names_and_types_check_zero(service_names_and_types); - if (ret != RMW_RET_OK) { - return ret; - } - if (node->implementation_identifier != identifier) { - RMW_SET_ERROR_MSG("node handle not from this implementation"); - return RMW_RET_ERROR; - } auto common_context = static_cast(node->context->impl->common); diff --git a/rmw_fastrtps_shared_cpp/src/rmw_topic_names_and_types.cpp b/rmw_fastrtps_shared_cpp/src/rmw_topic_names_and_types.cpp index 761a73143..5ac6dc446 100644 --- a/rmw_fastrtps_shared_cpp/src/rmw_topic_names_and_types.cpp +++ b/rmw_fastrtps_shared_cpp/src/rmw_topic_names_and_types.cpp @@ -42,22 +42,17 @@ __rmw_get_topic_names_and_types( bool no_demangle, rmw_names_and_types_t * topic_names_and_types) { - if (!allocator) { - RMW_SET_ERROR_MSG("allocator is null"); + RMW_CHECK_ARGUMENT_FOR_NULL(node, RMW_RET_INVALID_ARGUMENT); + RMW_CHECK_TYPE_IDENTIFIERS_MATCH( + node, + node->implementation_identifier, + identifier, + return RMW_RET_INCORRECT_RMW_IMPLEMENTATION); + RCUTILS_CHECK_ALLOCATOR_WITH_MSG( + allocator, "allocator argument is invalid", return RMW_RET_INVALID_ARGUMENT); + if (RMW_RET_OK != rmw_names_and_types_check_zero(topic_names_and_types)) { return RMW_RET_INVALID_ARGUMENT; } - if (!node) { - RMW_SET_ERROR_MSG("null node handle"); - return RMW_RET_INVALID_ARGUMENT; - } - rmw_ret_t ret = rmw_names_and_types_check_zero(topic_names_and_types); - if (ret != RMW_RET_OK) { - return ret; - } - if (node->implementation_identifier != identifier) { - RMW_SET_ERROR_MSG("node handle not from this implementation"); - return RMW_RET_ERROR; - } DemangleFunction demangle_topic = _demangle_ros_topic_from_topic; DemangleFunction demangle_type = _demangle_if_ros_type;