Move uuid to //aos:uuid
It is getting usage significantly further outside the logger. Time to
promote it somewhere better. I only see it getting used more.
Change-Id: I754739347212f5a3154f68c7b7fc84b9a9074d90
diff --git a/aos/events/logging/BUILD b/aos/events/logging/BUILD
index a3513d1..0b6adeb 100644
--- a/aos/events/logging/BUILD
+++ b/aos/events/logging/BUILD
@@ -27,7 +27,7 @@
deps = [
":buffer_encoder",
":logger_fbs",
- ":uuid",
+ "//aos:uuid",
"//aos:configuration",
"//aos:flatbuffer_merge",
"//aos:flatbuffers",
@@ -176,7 +176,7 @@
deps = [
":logfile_utils",
":logger_fbs",
- ":uuid",
+ "//aos:uuid",
"@com_github_google_flatbuffers//:flatbuffers",
],
)
@@ -214,7 +214,7 @@
":log_writer",
":logfile_utils",
":logger_fbs",
- ":uuid",
+ "//aos:uuid",
"//aos/events:event_loop",
"//aos/events:simulated_event_loop",
"//aos/network:message_bridge_server_fbs",
@@ -377,29 +377,6 @@
],
)
-cc_library(
- name = "uuid",
- srcs = ["uuid.cc"],
- hdrs = ["uuid.h"],
- target_compatible_with = ["@platforms//os:linux"],
- visibility = ["//visibility:public"],
- deps = [
- "@com_github_google_flatbuffers//:flatbuffers",
- "@com_github_google_glog//:glog",
- "@com_google_absl//absl/types:span",
- ],
-)
-
-cc_test(
- name = "uuid_test",
- srcs = ["uuid_test.cc"],
- target_compatible_with = ["@platforms//os:linux"],
- deps = [
- ":uuid",
- "//aos/testing:googletest",
- ],
-)
-
cc_test(
name = "logfile_utils_test",
srcs = ["logfile_utils_test.cc"],
diff --git a/aos/events/logging/log_namer.cc b/aos/events/logging/log_namer.cc
index 18138d9..a0625d0 100644
--- a/aos/events/logging/log_namer.cc
+++ b/aos/events/logging/log_namer.cc
@@ -9,7 +9,7 @@
#include "absl/strings/str_cat.h"
#include "aos/events/logging/logfile_utils.h"
#include "aos/events/logging/logger_generated.h"
-#include "aos/events/logging/uuid.h"
+#include "aos/uuid.h"
#include "flatbuffers/flatbuffers.h"
#include "glog/logging.h"
diff --git a/aos/events/logging/log_namer.h b/aos/events/logging/log_namer.h
index 2d4c23e..8f3f712 100644
--- a/aos/events/logging/log_namer.h
+++ b/aos/events/logging/log_namer.h
@@ -9,7 +9,7 @@
#include "aos/events/logging/logfile_utils.h"
#include "aos/events/logging/logger_generated.h"
-#include "aos/events/logging/uuid.h"
+#include "aos/uuid.h"
#include "flatbuffers/flatbuffers.h"
namespace aos {
diff --git a/aos/events/logging/log_reader.cc b/aos/events/logging/log_reader.cc
index 881f9c1..07b423f 100644
--- a/aos/events/logging/log_reader.cc
+++ b/aos/events/logging/log_reader.cc
@@ -13,7 +13,6 @@
#include "aos/events/event_loop.h"
#include "aos/events/logging/logfile_sorting.h"
#include "aos/events/logging/logger_generated.h"
-#include "aos/events/logging/uuid.h"
#include "aos/flatbuffer_merge.h"
#include "aos/network/multinode_timestamp_filter.h"
#include "aos/network/remote_message_generated.h"
@@ -22,6 +21,7 @@
#include "aos/network/timestamp_channel.h"
#include "aos/time/time.h"
#include "aos/util/file.h"
+#include "aos/uuid.h"
#include "flatbuffers/flatbuffers.h"
#include "openssl/sha.h"
diff --git a/aos/events/logging/log_reader.h b/aos/events/logging/log_reader.h
index 24ac5c9..37163c1 100644
--- a/aos/events/logging/log_reader.h
+++ b/aos/events/logging/log_reader.h
@@ -11,13 +11,13 @@
#include "aos/events/logging/logfile_sorting.h"
#include "aos/events/logging/logfile_utils.h"
#include "aos/events/logging/logger_generated.h"
-#include "aos/events/logging/uuid.h"
#include "aos/events/simulated_event_loop.h"
#include "aos/network/message_bridge_server_generated.h"
#include "aos/network/multinode_timestamp_filter.h"
#include "aos/network/remote_message_generated.h"
#include "aos/network/timestamp_filter.h"
#include "aos/time/time.h"
+#include "aos/uuid.h"
#include "flatbuffers/flatbuffers.h"
namespace aos {
diff --git a/aos/events/logging/log_writer.h b/aos/events/logging/log_writer.h
index eb08bb7..0e88b4f 100644
--- a/aos/events/logging/log_writer.h
+++ b/aos/events/logging/log_writer.h
@@ -9,11 +9,11 @@
#include "aos/events/logging/log_namer.h"
#include "aos/events/logging/logfile_utils.h"
#include "aos/events/logging/logger_generated.h"
-#include "aos/events/logging/uuid.h"
#include "aos/events/simulated_event_loop.h"
#include "aos/network/message_bridge_server_generated.h"
#include "aos/network/remote_message_generated.h"
#include "aos/time/time.h"
+#include "aos/uuid.h"
#include "flatbuffers/flatbuffers.h"
namespace aos {
diff --git a/aos/events/logging/logfile_sorting.h b/aos/events/logging/logfile_sorting.h
index f479d62..95a6431 100644
--- a/aos/events/logging/logfile_sorting.h
+++ b/aos/events/logging/logfile_sorting.h
@@ -6,8 +6,8 @@
#include <vector>
#include "aos/configuration.h"
-#include "aos/events/logging/uuid.h"
#include "aos/time/time.h"
+#include "aos/uuid.h"
namespace aos {
namespace logger {
diff --git a/aos/events/logging/uuid.cc b/aos/events/logging/uuid.cc
deleted file mode 100644
index ce3411d..0000000
--- a/aos/events/logging/uuid.cc
+++ /dev/null
@@ -1,164 +0,0 @@
-#include "aos/events/logging/uuid.h"
-
-#include <fcntl.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <array>
-#include <random>
-#include <string_view>
-
-#include "glog/logging.h"
-
-namespace aos {
-namespace {
-void ToHex(const uint8_t *val, char *result, size_t count) {
- while (count > 0) {
- int upper = ((*val) >> 4) & 0xf;
- if (upper < 10) {
- result[0] = upper + '0';
- } else {
- result[0] = upper - 10 + 'a';
- }
-
- int lower = (*val) & 0xf;
- if (lower < 10) {
- result[1] = lower + '0';
- } else {
- result[1] = lower - 10 + 'a';
- }
-
- ++val;
- result += 2;
- --count;
- }
-}
-
-void FromHex(const char *val, uint8_t *result, size_t count) {
- while (count > 0) {
- CHECK((val[0] >= '0' && val[0] <= '9') || (val[0] >= 'a' && val[0] <= 'f'))
- << ": Invalid hex '" << val[0] << "'";
- CHECK((val[1] >= '0' && val[1] <= '9') || (val[1] >= 'a' && val[1] <= 'f'))
- << ": Invalid hex '" << val[1] << "'";
-
- uint8_t converted = 0;
- if (val[0] < 'a') {
- converted |= static_cast<uint8_t>(val[0] - '0') << 4;
- } else {
- converted |= (static_cast<uint8_t>(val[0] - 'a') + 0xa) << 4;
- }
- if (val[1] < 'a') {
- converted |= static_cast<uint8_t>(val[1] - '0');
- } else {
- converted |= (static_cast<uint8_t>(val[1] - 'a') + 0xa);
- }
- *result = converted;
-
- val += 2;
- ++result;
- --count;
- }
-}
-
-} // namespace
-
-UUID UUID::Random() {
- std::random_device rd;
- std::mt19937 gen(rd());
-
- std::uniform_int_distribution<> dis(0, 255);
- std::uniform_int_distribution<> dis2(8, 11);
- UUID result;
- for (size_t i = 0; i < kDataSize; ++i) {
- result.data_[i] = dis(gen);
- }
-
- // Mark the reserved bits in the data that this is a uuid4, a random UUID.
- result.data_[6] = (result.data_[6] & 0x0f) | 0x40;
- result.data_[8] = (result.data_[6] & 0x3f) | 0x80;
-
- return result;
-}
-
-std::string UUID::ToString() const {
- std::string out;
- out.resize(UUID::kStringSize);
- CopyTo(out.data());
- return out;
-}
-
-std::ostream &operator<<(std::ostream &os, const UUID &uuid) {
- return os << uuid.ToString();
-}
-
-flatbuffers::Offset<flatbuffers::String> UUID::PackString(
- flatbuffers::FlatBufferBuilder *fbb) const {
- std::array<char, kStringSize> data;
- CopyTo(data.data());
-
- return fbb->CreateString(data.data(), data.size());
-}
-
-flatbuffers::Offset<flatbuffers::Vector<uint8_t>> UUID::PackVector(
- flatbuffers::FlatBufferBuilder *fbb) const {
- return fbb->CreateVector(data_.data(), data_.size());
-}
-
-void UUID::CopyTo(char *result) const {
- ToHex(&data_[0], result, 4);
- result[8] = '-';
- ToHex(&data_[4], result + 9, 2);
- result[13] = '-';
- ToHex(&data_[6], result + 14, 2);
- result[18] = '-';
- ToHex(&data_[8], result + 19, 2);
- result[23] = '-';
- ToHex(&data_[10], result + 24, 6);
-}
-
-UUID UUID::Zero() {
- UUID result;
- std::memset(result.data_.data(), 0, result.data_.size());
- return result;
-}
-
-UUID UUID::FromString(const flatbuffers::String *str) {
- return FromString(str->string_view());
-}
-
-UUID UUID::FromVector(const flatbuffers::Vector<uint8_t> *data) {
- CHECK(data != nullptr);
- CHECK_EQ(data->size(), kDataSize);
-
- UUID result;
- std::memcpy(result.data_.data(), data->Data(), kDataSize);
- return result;
-}
-
-UUID UUID::FromString(std::string_view str) {
- CHECK_EQ(str.size(), kStringSize);
-
- UUID result;
- FromHex(str.data(), result.data_.data(), 4);
- CHECK(str.data()[8] == '-' && str.data()[13] == '-' &&
- str.data()[18] == '-' && str.data()[23] == '-')
- << ": Invalid uuid.";
- FromHex(str.data() + 9, result.data_.data() + 4, 2);
- FromHex(str.data() + 14, result.data_.data() + 6, 2);
- FromHex(str.data() + 19, result.data_.data() + 8, 2);
- FromHex(str.data() + 24, result.data_.data() + 10, 6);
- return result;
-}
-
-UUID UUID::BootUUID() {
- int fd = open("/proc/sys/kernel/random/boot_id", O_RDONLY);
- PCHECK(fd != -1);
-
- std::array<char, kStringSize> data;
- CHECK_EQ(static_cast<ssize_t>(kStringSize),
- read(fd, data.begin(), kStringSize));
- close(fd);
-
- return UUID::FromString(std::string_view(data.data(), data.size()));
-}
-
-} // namespace aos
diff --git a/aos/events/logging/uuid.h b/aos/events/logging/uuid.h
deleted file mode 100644
index 1379369..0000000
--- a/aos/events/logging/uuid.h
+++ /dev/null
@@ -1,78 +0,0 @@
-#ifndef AOS_EVENTS_LOGGING_UUID_H_
-#define AOS_EVENTS_LOGGING_UUID_H_
-
-#include <array>
-#include <ostream>
-#include <string>
-
-#include "absl/types/span.h"
-#include "flatbuffers/flatbuffers.h"
-
-namespace aos {
-
-// Class to generate and hold a UUID.
-class UUID {
- public:
- // Size of a UUID both as a string and the raw data.
- static constexpr size_t kStringSize = 36;
- static constexpr size_t kDataSize = 16;
-
- // Returns a randomly generated UUID. This is known as a UUID4.
- static UUID Random();
-
- // Returns a uuid with all '0's.
- static UUID Zero();
-
- // Converts a string UUID of the form 00000000-0000-0000-0000-000000000000 to
- // a UUID.
- static UUID FromString(std::string_view string);
- static UUID FromString(const flatbuffers::String *string);
-
- // Converts a 16 byte vector (128 bits) to a UUID. This requires no
- // transformation.
- static UUID FromVector(const flatbuffers::Vector<uint8_t> *data);
-
- // Returns the boot UUID for the current linux computer.
- static UUID BootUUID();
-
- // Default constructor which builds an uninitialized UUID. Use one of the
- // static methods if you want something more useful.
- UUID() {}
-
- // Packs this UUID into a flatbuffer as a string.
- flatbuffers::Offset<flatbuffers::String> PackString(
- flatbuffers::FlatBufferBuilder *fbb) const;
- // Copies this UUID as a string into the memory pointed by result. Result
- // must be at least kStringSize long.
- void CopyTo(char *result) const;
- // Returns this UUID as a string.
- std::string ToString() const;
-
- // Packs the UUID bytes directly into a vector.
- flatbuffers::Offset<flatbuffers::Vector<uint8_t>> PackVector(
- flatbuffers::FlatBufferBuilder *fbb) const;
-
- // Returns the underlying UUID data.
- absl::Span<const uint8_t> span() const {
- return absl::Span<const uint8_t>(data_.data(), data_.size());
- }
-
- bool operator==(const UUID &other) const {
- return other.span() == span();
- }
- bool operator!=(const UUID &other) const {
- return other.span() != span();
- }
-
- private:
- friend std::ostream &operator<<(std::ostream &os, const UUID &uuid);
-
- // Encoded storage for the data.
- std::array<uint8_t, kDataSize> data_;
-};
-
-std::ostream &operator<<(std::ostream &os, const UUID &uuid);
-
-} // namespace aos
-
-#endif // AOS_EVENTS_LOGGING_UUID_H_
diff --git a/aos/events/logging/uuid_test.cc b/aos/events/logging/uuid_test.cc
deleted file mode 100644
index 5ec8f83..0000000
--- a/aos/events/logging/uuid_test.cc
+++ /dev/null
@@ -1,52 +0,0 @@
-#include "aos/events/logging/uuid.h"
-
-#include "glog/logging.h"
-#include "gtest/gtest.h"
-
-namespace aos {
-namespace testing {
-
-// Tests that random UUIDs are actually random, and we can convert them to a
-// string. Not very exhaustive, but it is a good smoke test.
-TEST(UUIDTest, GetOne) {
- LOG(INFO) << UUID::Random();
-
- UUID r = UUID::Random();
-
- std::stringstream ss;
- ss << r;
-
- UUID r2 = UUID::FromString(ss.str());
- EXPECT_EQ(r2, r);
-
- EXPECT_NE(UUID::Random(), UUID::Random());
- EXPECT_NE(UUID::Random(), UUID::Zero());
- EXPECT_EQ(UUID::Zero(), UUID::Zero());
-}
-
-// Tests that converting to and from various formats produces the same UUID.
-TEST(UUIDTest, FromString) {
- std::string_view str = "4b88ab00-556a-455b-a395-17d1a0c6f906";
- std::array<uint8_t, UUID::kDataSize> data = {
- 0x4b, 0x88, 0xab, 0x00, 0x55, 0x6a, 0x45, 0x5b,
- 0xa3, 0x95, 0x17, 0xd1, 0xa0, 0xc6, 0xf9, 0x06};
-
- const UUID u = UUID::FromString(str);
-
- EXPECT_EQ(u.span(), absl::Span<uint8_t>(data.data(), data.size()));
- EXPECT_EQ(u.ToString(), str);
-
- flatbuffers::FlatBufferBuilder fbb;
- flatbuffers::Offset<flatbuffers::Vector<uint8_t>> data_offset =
- fbb.CreateVector(data.data(), data.size());
-
- const flatbuffers::Vector<uint8_t> *data_vector =
- flatbuffers::GetTemporaryPointer(fbb, data_offset);
-
- const UUID u2 = UUID::FromVector(data_vector);
-
- EXPECT_EQ(u, u2);
-}
-
-} // namespace testing
-} // namespace aos