Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_LOGGING_UUID_H_ |
| 2 | #define AOS_EVENTS_LOGGING_UUID_H_ |
| 3 | |
| 4 | #include <array> |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 5 | #include <ostream> |
| 6 | #include <string> |
| 7 | |
| 8 | #include "absl/types/span.h" |
| 9 | #include "flatbuffers/flatbuffers.h" |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 10 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 11 | #include "aos/thread_local.h" |
| 12 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 13 | namespace aos { |
| 14 | |
| 15 | // Class to generate and hold a UUID. |
| 16 | class UUID { |
| 17 | public: |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 18 | // Size of a UUID both as a string and the raw data. |
| 19 | static constexpr size_t kStringSize = 36; |
| 20 | static constexpr size_t kDataSize = 16; |
| 21 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 22 | // Returns a randomly generated UUID. This is known as a UUID4. |
| 23 | static UUID Random(); |
| 24 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 25 | // Returns a uuid with all '0's. |
Austin Schuh | 81d0fe4 | 2022-08-17 16:29:23 -0700 | [diff] [blame] | 26 | static constexpr UUID Zero() { |
| 27 | UUID result; |
| 28 | std::memset(result.data_.data(), 0, result.data_.size()); |
| 29 | return result; |
| 30 | } |
Brian Silverman | 1f34522 | 2020-09-24 21:14:48 -0700 | [diff] [blame] | 31 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 32 | // Converts a string UUID of the form 00000000-0000-0000-0000-000000000000 to |
| 33 | // a UUID. |
| 34 | static UUID FromString(std::string_view string); |
| 35 | static UUID FromString(const flatbuffers::String *string); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 36 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 37 | // Converts a 16 byte vector (128 bits) to a UUID. This requires no |
| 38 | // transformation. |
| 39 | static UUID FromVector(const flatbuffers::Vector<uint8_t> *data); |
| 40 | |
Alexei Strots | 72060d6 | 2022-10-10 19:23:53 -0700 | [diff] [blame] | 41 | // Initializes new UUID from data. |
| 42 | static UUID FromSpan(absl::Span<const uint8_t> data); |
| 43 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 44 | // Returns the boot UUID for the current linux computer. |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 45 | static UUID BootUUID(); |
| 46 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 47 | // Default constructor which builds an uninitialized UUID. Use one of the |
| 48 | // static methods if you want something more useful. |
Austin Schuh | 81d0fe4 | 2022-08-17 16:29:23 -0700 | [diff] [blame] | 49 | constexpr UUID() : data_() {} |
Brian Silverman | 2596976 | 2022-08-24 19:54:00 -0700 | [diff] [blame] | 50 | constexpr UUID(const UUID &uuid) = default; |
Austin Schuh | 81d0fe4 | 2022-08-17 16:29:23 -0700 | [diff] [blame] | 51 | |
Brian Silverman | 2596976 | 2022-08-24 19:54:00 -0700 | [diff] [blame] | 52 | constexpr UUID &operator=(const UUID &other) = default; |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 53 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 54 | // Packs this UUID into a flatbuffer as a string. |
| 55 | flatbuffers::Offset<flatbuffers::String> PackString( |
| 56 | flatbuffers::FlatBufferBuilder *fbb) const; |
| 57 | // Copies this UUID as a string into the memory pointed by result. Result |
| 58 | // must be at least kStringSize long. |
| 59 | void CopyTo(char *result) const; |
| 60 | // Returns this UUID as a string. |
| 61 | std::string ToString() const; |
| 62 | |
| 63 | // Packs the UUID bytes directly into a vector. |
| 64 | flatbuffers::Offset<flatbuffers::Vector<uint8_t>> PackVector( |
| 65 | flatbuffers::FlatBufferBuilder *fbb) const; |
| 66 | |
Austin Schuh | 81d0fe4 | 2022-08-17 16:29:23 -0700 | [diff] [blame] | 67 | // Returns a human-readable string representing this UUID. |
| 68 | // |
| 69 | // This is done without any memory allocation, which means it's returned in a |
| 70 | // thread-local buffer. |
| 71 | // |
| 72 | // Be careful using this. It's mostly useful for low-level tracing of UUIDs |
| 73 | // through the system. |
| 74 | const char *thread_local_string() const { |
| 75 | AOS_THREAD_LOCAL char buffer[kStringSize + 1]; |
| 76 | CopyTo(buffer); |
| 77 | return buffer; |
| 78 | } |
| 79 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 80 | // Returns the underlying UUID data. |
| 81 | absl::Span<const uint8_t> span() const { |
| 82 | return absl::Span<const uint8_t>(data_.data(), data_.size()); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame] | 85 | bool operator==(const UUID &other) const { return other.span() == span(); } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 86 | bool operator<(const UUID &other) const { return other.span() < span(); } |
Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame] | 87 | bool operator!=(const UUID &other) const { return other.span() != span(); } |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 88 | |
| 89 | private: |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 90 | friend std::ostream &operator<<(std::ostream &os, const UUID &uuid); |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 91 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 92 | // Encoded storage for the data. |
| 93 | std::array<uint8_t, kDataSize> data_; |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 96 | std::ostream &operator<<(std::ostream &os, const UUID &uuid); |
| 97 | |
Austin Schuh | 64fab80 | 2020-09-09 22:47:47 -0700 | [diff] [blame] | 98 | } // namespace aos |
| 99 | |
| 100 | #endif // AOS_EVENTS_LOGGING_UUID_H_ |