Skip to content
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
2 changes: 2 additions & 0 deletions rosidl_typesupport_c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ endif()

find_package(ament_cmake_ros REQUIRED)
find_package(rcpputils REQUIRED)
find_package(rcutils REQUIRED)
find_package(rosidl_runtime_c REQUIRED)

ament_export_dependencies(rcpputils)
Expand All @@ -42,6 +43,7 @@ target_include_directories(${PROJECT_NAME} PUBLIC

ament_target_dependencies(${PROJECT_NAME}
"rcpputils"
"rcutils"
"rosidl_runtime_c"
)
ament_export_libraries(${PROJECT_NAME})
Expand Down
1 change: 1 addition & 0 deletions rosidl_typesupport_c/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<buildtool_depend>ament_cmake_ros</buildtool_depend>

<depend>rcpputils</depend>
<depend>rcutils</depend>

<build_depend>rosidl_runtime_c</build_depend>
<!--
Expand Down
22 changes: 14 additions & 8 deletions rosidl_typesupport_c/src/type_support_dispatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "rcpputils/find_library.hpp"
#include "rcpputils/shared_library.hpp"
#include "rcutils/error_handling.h"
#include "rosidl_typesupport_c/identifier.h"
#include "rosidl_typesupport_c/type_support_map.h"

Expand Down Expand Up @@ -59,27 +60,29 @@ get_typesupport_handle_function(
map->package_name, identifier);
std::string library_path = rcpputils::find_library_path(library_name);
if (library_path.empty()) {
fprintf(stderr, "Failed to find library '%s'\n", library_name);
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
"Failed to find library '%s'\n", library_name);
return nullptr;
}

try {
lib = new rcpputils::SharedLibrary(library_path.c_str());
} catch (const std::runtime_error & e) {
throw std::runtime_error(
"Could not load library " + library_path + ": " +
std::string(e.what()));
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
"Could not load library %s: %s\n", library_path.c_str(), e.what());
return nullptr;
} catch (const std::bad_alloc & e) {
throw std::runtime_error(
"Could not load library " + library_path + ": " +
std::string(e.what()));
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
"Could not load library %s: %s\n", library_path.c_str(), e.what());
return nullptr;
}
map->data[i] = lib;
}
auto clib = static_cast<const rcpputils::SharedLibrary *>(map->data[i]);
lib = const_cast<rcpputils::SharedLibrary *>(clib);
if (!lib->has_symbol(map->symbol_name[i])) {
fprintf(stderr, "Failed to find symbol '%s' in library\n", map->symbol_name[i]);
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
"Failed to find symbol '%s' in library\n", map->symbol_name[i]);
return nullptr;
}
void * sym = lib->get_symbol(map->symbol_name[i]);
Expand All @@ -90,6 +93,9 @@ get_typesupport_handle_function(
return ts;
}
}
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
"Handle's typesupport identifier (%s) is not supported by this library\n",
handle->typesupport_identifier);
return nullptr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <gtest/gtest.h>
#include "rcpputils/shared_library.hpp"
#include "rcutils/error_handling.h"
#include "rosidl_typesupport_c/identifier.h"
#include "rosidl_typesupport_c/message_type_support_dispatch.h"
#include "rosidl_typesupport_c/type_support_map.h"
Expand Down Expand Up @@ -83,6 +84,8 @@ TEST(TestMessageTypeSupportDispatch, get_handle_function) {
rosidl_typesupport_c__get_message_typesupport_handle_function(
&type_support,
"different_identifier"), nullptr);
EXPECT_TRUE(rcutils_error_is_set());
rcutils_reset_error();

rosidl_message_type_support_t type_support_c_identifier =
get_rosidl_message_type_support(rosidl_typesupport_c__typesupport_identifier);
Expand All @@ -108,16 +111,22 @@ TEST(TestMessageTypeSupportDispatch, get_handle_function) {
rosidl_typesupport_c__get_message_typesupport_handle_function(
&type_support_c_identifier,
"test_type_support2"), nullptr);
EXPECT_TRUE(rcutils_error_is_set());
rcutils_reset_error();

// Library file exists, but loading shared library fails
EXPECT_THROW(
EXPECT_EQ(
rosidl_typesupport_c__get_message_typesupport_handle_function(
&type_support_c_identifier,
"test_type_support3"), std::runtime_error);
"test_type_support3"), nullptr);
EXPECT_TRUE(rcutils_error_is_set());
rcutils_reset_error();

// Library doesn't exist
EXPECT_EQ(
rosidl_typesupport_c__get_message_typesupport_handle_function(
&type_support_c_identifier,
"test_type_support4"), nullptr);
EXPECT_TRUE(rcutils_error_is_set());
rcutils_reset_error();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <gtest/gtest.h>
#include "rcpputils/shared_library.hpp"
#include "rcutils/error_handling.h"
#include "rosidl_typesupport_c/identifier.h"
#include "rosidl_typesupport_c/service_type_support_dispatch.h"
#include "rosidl_typesupport_c/type_support_map.h"
Expand Down Expand Up @@ -83,6 +84,8 @@ TEST(TestServiceTypeSupportDispatch, get_handle_function) {
rosidl_typesupport_c__get_service_typesupport_handle_function(
&type_support,
"different_identifier"), nullptr);
EXPECT_TRUE(rcutils_error_is_set());
rcutils_reset_error();

rosidl_service_type_support_t type_support_c_identifier =
get_rosidl_service_type_support(rosidl_typesupport_c__typesupport_identifier);
Expand All @@ -108,16 +111,22 @@ TEST(TestServiceTypeSupportDispatch, get_handle_function) {
rosidl_typesupport_c__get_service_typesupport_handle_function(
&type_support_c_identifier,
"test_type_support2"), nullptr);
EXPECT_TRUE(rcutils_error_is_set());
rcutils_reset_error();

// Library file exists, but loading shared library fails
EXPECT_THROW(
EXPECT_EQ(
rosidl_typesupport_c__get_service_typesupport_handle_function(
&type_support_c_identifier,
"test_type_support3"), std::runtime_error);
"test_type_support3"), nullptr);
EXPECT_TRUE(rcutils_error_is_set());
rcutils_reset_error();

// Library doesn't exist
EXPECT_EQ(
rosidl_typesupport_c__get_service_typesupport_handle_function(
&type_support_c_identifier,
"test_type_support4"), nullptr);
EXPECT_TRUE(rcutils_error_is_set());
rcutils_reset_error();
}