Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
feat(autoware_utils): add functions to generate and convert uuids (#238)
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey Panferov <[email protected]>
  • Loading branch information
lexavtanke authored Feb 15, 2024
1 parent 119ea82 commit 7c55915
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
1 change: 1 addition & 0 deletions autoware_utils/include/autoware_utils/autoware_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_
72 changes: 72 additions & 0 deletions autoware_utils/include/autoware_utils/ros/uuid_helper.hpp
Original file line number Diff line number Diff line change
@@ -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 <unique_identifier_msgs/msg/uuid.hpp>

#include <boost/uuid/uuid.hpp>

#include <algorithm>
#include <random>
#include <string>

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<std::mt19937, 8, uint8_t> 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_
1 change: 1 addition & 0 deletions autoware_utils/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<depend>builtin_interfaces</depend>
<depend>rclcpp</depend>
<depend>unique_identifier_msgs</depend>

<test_depend>ament_cmake_ros</test_depend>

Expand Down
76 changes: 76 additions & 0 deletions autoware_utils/test/src/ros/test_uuid_helper.cpp
Original file line number Diff line number Diff line change
@@ -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 <boost/uuid/uuid_generators.hpp>

#include <gtest/gtest.h>

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()));
}

0 comments on commit 7c55915

Please sign in to comment.