Skip to content

Commit

Permalink
Implement converter plugin for CDR format and add converter plugins p…
Browse files Browse the repository at this point in the history
…ackage (ros2#48)

* ros2GH-111 Add package for converter plugins

* ros2GH-111 Add CDR converter plugin

* ros2GH-111 Add test for more primitives types

* ros2GH-116 Fix cdr converter after rebase on new converters interface

* ros2GH-116 Use rmw_serialize/rmw_deserialize directly in converter and link against rmw_fastrtps_cpp

* Fix converter package.xml

* Fix clang warnings

* ros2GH-30 Change interface to the same convention as rmw_(de)serialize

* comply to new rcutils error handling API

* use poco to load fastrtps

* Update rosbag2_converter_default_plugins/src/rosbag2_converter_default_plugins/cdr/cdr_converter.cpp

Co-Authored-By: Karsten1987 <karsten@osrfoundation.org>
  • Loading branch information
botteroa-si and Karsten1987 committed Nov 8, 2018
1 parent f37ffa3 commit 1737d24
Show file tree
Hide file tree
Showing 16 changed files with 626 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ class SerializationFormatConverterInterface
virtual ~SerializationFormatConverterInterface() = default;

virtual void deserialize(
std::shared_ptr<rosbag2_ros2_message_t> ros_message,
std::shared_ptr<const SerializedBagMessage> serialized_message,
const rosidl_message_type_support_t * type_support) = 0;
std::shared_ptr<const rosbag2::SerializedBagMessage> serialized_message,
const rosidl_message_type_support_t * type_support,
std::shared_ptr<rosbag2_ros2_message_t> ros_message) = 0;

virtual void serialize(
std::shared_ptr<SerializedBagMessage> serialized_message,
std::shared_ptr<const rosbag2_ros2_message_t> ros_message,
const rosidl_message_type_support_t * type_support) = 0;
const rosidl_message_type_support_t * type_support,
std::shared_ptr<rosbag2::SerializedBagMessage> serialized_message) = 0;
};

} // namespace rosbag2
Expand Down
9 changes: 4 additions & 5 deletions rosbag2/test/rosbag2/converter_test_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@
#include "converter_test_plugin.hpp"

void ConverterTestPlugin::deserialize(

std::shared_ptr<rosbag2_ros2_message_t> ros_message,
const std::shared_ptr<const rosbag2::SerializedBagMessage> serialized_message,
const rosidl_message_type_support_t * type_support)
const rosidl_message_type_support_t * type_support,
std::shared_ptr<rosbag2_ros2_message_t> ros_message)
{
(void) ros_message;
(void) serialized_message;
(void) type_support;
}

void ConverterTestPlugin::serialize(
std::shared_ptr<rosbag2::SerializedBagMessage> serialized_message,
const std::shared_ptr<const rosbag2_ros2_message_t> ros_message,
const rosidl_message_type_support_t * type_support)
const rosidl_message_type_support_t * type_support,
std::shared_ptr<rosbag2::SerializedBagMessage> serialized_message)
{
(void) serialized_message;
(void) ros_message;
Expand Down
8 changes: 4 additions & 4 deletions rosbag2/test/rosbag2/converter_test_plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ class ConverterTestPlugin : public rosbag2::SerializationFormatConverterInterfac
{
public:
void deserialize(
std::shared_ptr<rosbag2_ros2_message_t> ros_message,
std::shared_ptr<const rosbag2::SerializedBagMessage> serialized_message,
const rosidl_message_type_support_t * type_support) override;
const rosidl_message_type_support_t * type_support,
std::shared_ptr<rosbag2_ros2_message_t> ros_message) override;

void serialize(
std::shared_ptr<rosbag2::SerializedBagMessage> serialized_message,
std::shared_ptr<const rosbag2_ros2_message_t> ros_message,
const rosidl_message_type_support_t * type_support) override;
const rosidl_message_type_support_t * type_support,
std::shared_ptr<rosbag2::SerializedBagMessage> serialized_message) override;
};

#endif // ROSBAG2__CONVERTER_TEST_PLUGIN_HPP_
81 changes: 81 additions & 0 deletions rosbag2_converter_default_plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
cmake_minimum_required(VERSION 3.5)
project(rosbag2_converter_default_plugins)

# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

find_package(ament_cmake REQUIRED)
find_package(ament_index_cpp REQUIRED)
find_package(poco_vendor REQUIRED)
find_package(Poco COMPONENTS Foundation)
find_package(pluginlib REQUIRED)
find_package(rosbag2 REQUIRED)
find_package(rosidl_generator_cpp REQUIRED)
find_package(rcutils REQUIRED)
find_package(rmw REQUIRED)

add_library(${PROJECT_NAME} SHARED
src/rosbag2_converter_default_plugins/cdr/cdr_converter.cpp)

ament_target_dependencies(${PROJECT_NAME}
ament_index_cpp
pluginlib
Poco
rosbag2
rcutils
rmw
rosidl_generator_cpp)

target_include_directories(${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)

pluginlib_export_plugin_description_file(rosbag2 plugin_description.xml)

install(
TARGETS ${PROJECT_NAME}
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin)

ament_export_include_directories(include)
ament_export_libraries(${PROJECT_NAME})
ament_export_dependencies(rosbag2)

if(BUILD_TESTING)
find_package(ament_cmake_gmock REQUIRED)
find_package(ament_lint_auto REQUIRED)
find_package(rcutils REQUIRED)
find_package(rosbag2 REQUIRED)
find_package(rosbag2_test_common REQUIRED)
find_package(test_msgs REQUIRED)

ament_lint_auto_find_test_dependencies()

ament_add_gmock(test_cdr_converter
test/rosbag2_converter_default_plugins/cdr/test_cdr_converter.cpp
src/rosbag2_converter_default_plugins/cdr/cdr_converter.cpp
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
if(TARGET test_cdr_converter)
ament_target_dependencies(test_cdr_converter
pluginlib
rosbag2
rosbag2_test_common
rcutils
test_msgs)
endif()
endif()

ament_package()
31 changes: 31 additions & 0 deletions rosbag2_converter_default_plugins/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="2">
<name>rosbag2_converter_default_plugins</name>
<version>0.0.0</version>
<description>Package containing default plugins for format converters</description>
<maintainer email="karsten.knese@googlemail.com">Karsten Knese</maintainer>
<license>Apache License 2.0</license>

<buildtool_depend>ament_cmake</buildtool_depend>

<depend>ament_index_cpp</depend>
<depend>pluginlib</depend>
<depend>poco_vendor</depend>
<depend>rosidl_generator_cpp</depend>
<depend>rcutils</depend>
<depend>rmw</depend>
<depend>rosbag2</depend>

<test_depend>ament_cmake_gmock</test_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<test_depend>rcutils</test_depend>
<test_depend>rosbag2</test_depend>
<test_depend>rosbag2_test_common</test_depend>
<test_depend>test_msgs</test_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
9 changes: 9 additions & 0 deletions rosbag2_converter_default_plugins/plugin_description.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<library path="rosbag2_converter_default_plugins">
<class
name="cdr_converter"
type="CdrConverterPlugin"
base_class_type="rosbag2::FormatConverterInterface"
>
<description>This is a converter plugin for cdr format.</description>
</class>
</library>
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright 2018, Bosch Software Innovations GmbH.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "cdr_converter.hpp"

#include <memory>
#include <string>

#include "ament_index_cpp/get_resources.hpp"
#include "ament_index_cpp/get_package_prefix.hpp"

#include "Poco/SharedLibrary.h"

#include "rcutils/strdup.h"

#include "../logging.hpp"

namespace rosbag2_converter_default_plugins
{

inline
std::string get_package_library_path(const std::string & package_name)
{
const char * filename_prefix;
const char * filename_extension;
const char * dynamic_library_folder;
#ifdef _WIN32
filename_prefix = "";
filename_extension = ".dll";
dynamic_library_folder = "/bin/";
#elif __APPLE__
filename_prefix = "lib";
filename_extension = ".dylib";
dynamic_library_folder = "/lib/";
#else
filename_prefix = "lib";
filename_extension = ".so";
dynamic_library_folder = "/lib/";
#endif

auto package_prefix = ament_index_cpp::get_package_prefix(package_name);
auto library_path = package_prefix + dynamic_library_folder + filename_prefix +
package_name + filename_extension;

return library_path;
}

CdrConverter::CdrConverter()
{
auto library_path = get_package_library_path("rmw_fastrtps_dynamic_cpp");
std::shared_ptr<Poco::SharedLibrary> library;
try {
library = std::make_shared<Poco::SharedLibrary>(library_path);
} catch (Poco::LibraryLoadException &) {
throw std::runtime_error(
std::string("poco exception: library could not be found:") + library_path);
}

std::string serialize_symbol = "rmw_serialize";
std::string deserialize_symbol = "rmw_deserialize";

if (!library->hasSymbol(serialize_symbol)) {
throw std::runtime_error(
std::string("poco exception: symbol not found: ") + serialize_symbol);
}

if (!library->hasSymbol(deserialize_symbol)) {
throw std::runtime_error(
std::string("poco exception: symbol not found: ") + deserialize_symbol);
}

serialize_fcn_ = (decltype(serialize_fcn_))library->getSymbol(serialize_symbol);
if (!serialize_fcn_) {
throw std::runtime_error(
std::string("poco exception: symbol of wrong type: ") + serialize_symbol);
}

deserialize_fcn_ = (decltype(deserialize_fcn_))library->getSymbol(deserialize_symbol);
if (!deserialize_fcn_) {
throw std::runtime_error(
std::string("poco exception: symbol of wrong type: ") + deserialize_symbol);
}
}

void CdrConverter::deserialize(
const std::shared_ptr<const rosbag2::SerializedBagMessage> serialized_message,
const rosidl_message_type_support_t * type_support,
std::shared_ptr<rosbag2_ros2_message_t> ros_message)
{
ros_message->topic_name = serialized_message->topic_name.c_str();
ros_message->timestamp = serialized_message->time_stamp;

printf("this should be a call to FASTRTPS\n");
auto ret =
deserialize_fcn_(serialized_message->serialized_data.get(), type_support, ros_message->message);
if (ret != RMW_RET_OK) {
ROSBAG2_CONVERTER_DEFAULT_PLUGINS_LOG_ERROR("Failed to deserialize message.");
}
}

void CdrConverter::serialize(
const std::shared_ptr<const rosbag2_ros2_message_t> ros_message,
const rosidl_message_type_support_t * type_support,
std::shared_ptr<rosbag2::SerializedBagMessage> serialized_message)
{
serialized_message->topic_name = std::string(ros_message->topic_name);
serialized_message->time_stamp = ros_message->timestamp;

auto ret = serialize_fcn_(
ros_message->message, type_support, serialized_message->serialized_data.get());
if (ret != RMW_RET_OK) {
ROSBAG2_CONVERTER_DEFAULT_PLUGINS_LOG_ERROR("Failed to serialize message.");
}
}

} // namespace rosbag2_converter_default_plugins

#include "pluginlib/class_list_macros.hpp" // NOLINT
PLUGINLIB_EXPORT_CLASS(rosbag2_converter_default_plugins::CdrConverter,
rosbag2::SerializationFormatConverterInterface)
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2018, Bosch Software Innovations GmbH.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef ROSBAG2_CONVERTER_DEFAULT_PLUGINS__CDR__CDR_CONVERTER_HPP_
#define ROSBAG2_CONVERTER_DEFAULT_PLUGINS__CDR__CDR_CONVERTER_HPP_

#include <memory>
#include <string>

#include "rmw/types.h"

#include "rosbag2/serialization_format_converter_interface.hpp"

namespace rosbag2_converter_default_plugins
{

class CdrConverter : public rosbag2::SerializationFormatConverterInterface
{
public:
CdrConverter();

void deserialize(
std::shared_ptr<const rosbag2::SerializedBagMessage> serialized_message,
const rosidl_message_type_support_t * type_support,
std::shared_ptr<rosbag2_ros2_message_t> ros_message) override;

void serialize(
std::shared_ptr<const rosbag2_ros2_message_t> ros_message,
const rosidl_message_type_support_t * type_support,
std::shared_ptr<rosbag2::SerializedBagMessage> serialized_message) override;

protected:
rmw_ret_t (* serialize_fcn_)(
const void *,
const rosidl_message_type_support_t *,
rmw_serialized_message_t *) = nullptr;

rmw_ret_t (* deserialize_fcn_)(
const rmw_serialized_message_t *,
const rosidl_message_type_support_t *,
void *) = nullptr;
};

} // namespace rosbag2_converter_default_plugins

#endif // ROSBAG2_CONVERTER_DEFAULT_PLUGINS__CDR__CDR_CONVERTER_HPP_
Loading

0 comments on commit 1737d24

Please sign in to comment.