blob: a2cf8ae8eda61d11be1c6f69bfefd4fb1a0cf044 [file] [log] [blame]
Austin Schuh64fab802020-09-09 22:47:47 -07001#ifndef AOS_EVENTS_LOGGING_UUID_H_
2#define AOS_EVENTS_LOGGING_UUID_H_
3
4#include <array>
Austin Schuh5e2bfb82021-03-13 22:46:55 -08005#include <ostream>
James Kuszmaula791b762023-07-13 14:56:21 -07006#include <random>
Austin Schuh5e2bfb82021-03-13 22:46:55 -08007#include <string>
8
9#include "absl/types/span.h"
10#include "flatbuffers/flatbuffers.h"
James Kuszmaul55844a72023-12-20 11:57:01 -080011#include "glog/logging.h"
Austin Schuh64fab802020-09-09 22:47:47 -070012
13namespace aos {
14
15// Class to generate and hold a UUID.
16class UUID {
17 public:
Austin Schuh5e2bfb82021-03-13 22:46:55 -080018 // 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 Schuh64fab802020-09-09 22:47:47 -070022 // Returns a randomly generated UUID. This is known as a UUID4.
James Kuszmaula791b762023-07-13 14:56:21 -070023 // The first Random() call in a thread will tend to be slightly slower than
24 // the rest so that it can seed the pseudo-random number generator used
25 // internally.
Austin Schuh64fab802020-09-09 22:47:47 -070026 static UUID Random();
27
Austin Schuh5e2bfb82021-03-13 22:46:55 -080028 // Returns a uuid with all '0's.
Austin Schuh81d0fe42022-08-17 16:29:23 -070029 static constexpr UUID Zero() {
30 UUID result;
31 std::memset(result.data_.data(), 0, result.data_.size());
32 return result;
33 }
Brian Silverman1f345222020-09-24 21:14:48 -070034
Austin Schuh5e2bfb82021-03-13 22:46:55 -080035 // Converts a string UUID of the form 00000000-0000-0000-0000-000000000000 to
36 // a UUID.
37 static UUID FromString(std::string_view string);
38 static UUID FromString(const flatbuffers::String *string);
Austin Schuh20ac95d2020-12-05 17:24:19 -080039
Austin Schuh5e2bfb82021-03-13 22:46:55 -080040 // Converts a 16 byte vector (128 bits) to a UUID. This requires no
41 // transformation.
42 static UUID FromVector(const flatbuffers::Vector<uint8_t> *data);
43
Alexei Strots72060d62022-10-10 19:23:53 -070044 // Initializes new UUID from data.
45 static UUID FromSpan(absl::Span<const uint8_t> data);
46
Austin Schuh5e2bfb82021-03-13 22:46:55 -080047 // Returns the boot UUID for the current linux computer.
Austin Schuh20ac95d2020-12-05 17:24:19 -080048 static UUID BootUUID();
49
Austin Schuh5e2bfb82021-03-13 22:46:55 -080050 // Default constructor which builds an uninitialized UUID. Use one of the
51 // static methods if you want something more useful.
Austin Schuh81d0fe42022-08-17 16:29:23 -070052 constexpr UUID() : data_() {}
Brian Silverman25969762022-08-24 19:54:00 -070053 constexpr UUID(const UUID &uuid) = default;
Austin Schuh81d0fe42022-08-17 16:29:23 -070054
Brian Silverman25969762022-08-24 19:54:00 -070055 constexpr UUID &operator=(const UUID &other) = default;
Austin Schuh20ac95d2020-12-05 17:24:19 -080056
Austin Schuh5e2bfb82021-03-13 22:46:55 -080057 // Packs this UUID into a flatbuffer as a string.
58 flatbuffers::Offset<flatbuffers::String> PackString(
59 flatbuffers::FlatBufferBuilder *fbb) const;
60 // Copies this UUID as a string into the memory pointed by result. Result
61 // must be at least kStringSize long.
62 void CopyTo(char *result) const;
63 // Returns this UUID as a string.
64 std::string ToString() const;
65
66 // Packs the UUID bytes directly into a vector.
67 flatbuffers::Offset<flatbuffers::Vector<uint8_t>> PackVector(
68 flatbuffers::FlatBufferBuilder *fbb) const;
69
James Kuszmaul55844a72023-12-20 11:57:01 -080070 template <typename T>
71 void PackStaticVector(T *static_vector) const {
72 CHECK(static_vector->FromData(data_.data(), data_.size()));
73 }
74
Austin Schuh81d0fe42022-08-17 16:29:23 -070075 // Returns a human-readable string representing this UUID.
76 //
77 // This is done without any memory allocation, which means it's returned in a
78 // thread-local buffer.
79 //
80 // Be careful using this. It's mostly useful for low-level tracing of UUIDs
81 // through the system.
82 const char *thread_local_string() const {
Austin Schuhf7bfb652023-08-25 14:22:50 -070083 thread_local char buffer[kStringSize + 1];
Austin Schuh81d0fe42022-08-17 16:29:23 -070084 CopyTo(buffer);
85 return buffer;
86 }
87
Austin Schuh5e2bfb82021-03-13 22:46:55 -080088 // Returns the underlying UUID data.
89 absl::Span<const uint8_t> span() const {
90 return absl::Span<const uint8_t>(data_.data(), data_.size());
Austin Schuh64fab802020-09-09 22:47:47 -070091 }
92
Austin Schuh4385b142021-03-14 21:31:13 -070093 bool operator==(const UUID &other) const { return other.span() == span(); }
Austin Schuh58646e22021-08-23 23:51:46 -070094 bool operator<(const UUID &other) const { return other.span() < span(); }
Austin Schuh4385b142021-03-14 21:31:13 -070095 bool operator!=(const UUID &other) const { return other.span() != span(); }
Austin Schuh64fab802020-09-09 22:47:47 -070096
97 private:
Austin Schuh5e2bfb82021-03-13 22:46:55 -080098 friend std::ostream &operator<<(std::ostream &os, const UUID &uuid);
Austin Schuh64fab802020-09-09 22:47:47 -070099
Austin Schuh5e2bfb82021-03-13 22:46:55 -0800100 // Encoded storage for the data.
101 std::array<uint8_t, kDataSize> data_;
Austin Schuh64fab802020-09-09 22:47:47 -0700102};
103
Austin Schuh5e2bfb82021-03-13 22:46:55 -0800104std::ostream &operator<<(std::ostream &os, const UUID &uuid);
105
James Kuszmaula791b762023-07-13 14:56:21 -0700106namespace internal {
107// Initializes a mt19937 with as much entropy as it can take (rather than just a
108// 32-bit value from std::random_device).
109// Exposed for testing purposes.
110std::mt19937 FullySeededRandomGenerator();
111} // namespace internal
112
Austin Schuh64fab802020-09-09 22:47:47 -0700113} // namespace aos
114
115#endif // AOS_EVENTS_LOGGING_UUID_H_