diff --git a/autoware_utils/include/autoware_utils/autoware_utils.hpp b/autoware_utils/include/autoware_utils/autoware_utils.hpp index a9fa824e..77d2fecc 100644 --- a/autoware_utils/include/autoware_utils/autoware_utils.hpp +++ b/autoware_utils/include/autoware_utils/autoware_utils.hpp @@ -19,5 +19,6 @@ #include "autoware_utils/math/normalization.hpp" #include "autoware_utils/math/range.hpp" #include "autoware_utils/math/unit_conversion.hpp" +#include "autoware_utils/ros/uuid_helper.hpp" #endif // AUTOWARE_UTILS__AUTOWARE_UTILS_HPP_ diff --git a/autoware_utils/include/autoware_utils/ros/uuid_helper.hpp b/autoware_utils/include/autoware_utils/ros/uuid_helper.hpp new file mode 100644 index 00000000..8e27b4cf --- /dev/null +++ b/autoware_utils/include/autoware_utils/ros/uuid_helper.hpp @@ -0,0 +1,72 @@ +// Copyright 2022 Tier IV, Inc. +// +// 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 AUTOWARE_UTILS__ROS__UUID_HELPER_HPP_ +#define AUTOWARE_UTILS__ROS__UUID_HELPER_HPP_ + +#include + +#include + +#include +#include +#include + +namespace autoware_utils +{ +inline unique_identifier_msgs::msg::UUID generate_uuid() +{ + // Generate random number + unique_identifier_msgs::msg::UUID uuid; + std::mt19937 gen(std::random_device{}()); + std::independent_bits_engine bit_eng(gen); + std::generate(uuid.uuid.begin(), uuid.uuid.end(), bit_eng); + + return uuid; +} +inline unique_identifier_msgs::msg::UUID generate_default_uuid() +{ + // Generate UUID with all zeros + unique_identifier_msgs::msg::UUID default_uuid; + // Use std::generate to fill the UUID with zeros + std::generate(default_uuid.uuid.begin(), default_uuid.uuid.end(), []() { return 0; }); + + return default_uuid; +} +inline std::string to_hex_string(const unique_identifier_msgs::msg::UUID & id) +{ + std::stringstream ss; + for (auto i = 0; i < 16; ++i) { + ss << std::hex << std::setfill('0') << std::setw(2) << +id.uuid[i]; + } + return ss.str(); +} + +inline boost::uuids::uuid to_boost_uuid(const unique_identifier_msgs::msg::UUID & id) +{ + boost::uuids::uuid boost_uuid{}; + std::copy(id.uuid.begin(), id.uuid.end(), boost_uuid.begin()); + return boost_uuid; +} + +inline unique_identifier_msgs::msg::UUID to_uuid_msg(const boost::uuids::uuid & id) +{ + unique_identifier_msgs::msg::UUID ros_uuid{}; + std::copy(id.begin(), id.end(), ros_uuid.uuid.begin()); + return ros_uuid; +} + +} // namespace autoware_utils + +#endif // AUTOWARE_UTILS__ROS__UUID_HELPER_HPP_ diff --git a/autoware_utils/package.xml b/autoware_utils/package.xml index 7186fd99..694c275e 100644 --- a/autoware_utils/package.xml +++ b/autoware_utils/package.xml @@ -13,6 +13,7 @@ builtin_interfaces rclcpp + unique_identifier_msgs ament_cmake_ros diff --git a/autoware_utils/test/src/ros/test_uuid_helper.cpp b/autoware_utils/test/src/ros/test_uuid_helper.cpp new file mode 100644 index 00000000..05434fbb --- /dev/null +++ b/autoware_utils/test/src/ros/test_uuid_helper.cpp @@ -0,0 +1,76 @@ +// Copyright 2024 TIER IV, Inc. +// +// 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 "autoware_utils/ros/uuid_helper.hpp" + +#include + +#include + +TEST(UUIDHelperTest, generate_uuid) +{ + // Generate two UUIDs and ensure they are all different + + unique_identifier_msgs::msg::UUID uuid1 = autoware_utils::generate_uuid(); + unique_identifier_msgs::msg::UUID uuid2 = autoware_utils::generate_uuid(); + + EXPECT_FALSE(uuid1 == uuid2) << "Duplicate UUID generated: " + << autoware_utils::to_hex_string(uuid2); +} + +TEST(UUIDHelperTest, generate_default_uuid) +{ + // Generate two UUIDs and ensure they are all different + + unique_identifier_msgs::msg::UUID default_uuid = autoware_utils::generate_default_uuid(); + unique_identifier_msgs::msg::UUID zero_uuid; + std::fill(zero_uuid.uuid.begin(), zero_uuid.uuid.end(), 0x00); + + EXPECT_EQ(default_uuid, zero_uuid); +} + +TEST(UUIDHelperTest, to_hex_string) +{ + unique_identifier_msgs::msg::UUID uuid; + // Populate the UUID with some values + std::fill(uuid.uuid.begin(), uuid.uuid.end(), 0x42); + + std::string hex_string = autoware_utils::to_hex_string(uuid); + + // Check if the generated hex string is correct + EXPECT_EQ(hex_string, "42424242424242424242424242424242"); +} + +TEST(UUIDHelperTest, to_boost_uuid) +{ + unique_identifier_msgs::msg::UUID uuid; + // Populate the UUID with some values + std::fill(uuid.uuid.begin(), uuid.uuid.end(), 0x42); + + boost::uuids::uuid boost_uuid{}; + std::fill(boost_uuid.begin(), boost_uuid.end(), 0x42); + + // Check if the conversion from ROS UUID to Boost UUID is correct + EXPECT_TRUE(boost_uuid == autoware_utils::to_boost_uuid(uuid)); +} + +TEST(UUIDHelperTest, to_uuid_msg) +{ + boost::uuids::random_generator generator; + boost::uuids::uuid boost_uuid = generator(); + unique_identifier_msgs::msg::UUID ros_uuid = autoware_utils::to_uuid_msg(boost_uuid); + + // Check if the conversion from Boost UUID to ROS UUID is correct + EXPECT_TRUE(std::equal(boost_uuid.begin(), boost_uuid.end(), ros_uuid.uuid.begin())); +}