blob: 1b63ac3bb168ddcdd98b6ada4eecef10c9c01cb8 [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>
6#include <string>
7
8#include "absl/types/span.h"
9#include "flatbuffers/flatbuffers.h"
Austin Schuh64fab802020-09-09 22:47:47 -070010
Philipp Schrader790cb542023-07-05 21:06:52 -070011#include "aos/thread_local.h"
12
Austin Schuh64fab802020-09-09 22:47:47 -070013namespace 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.
23 static UUID Random();
24
Austin Schuh5e2bfb82021-03-13 22:46:55 -080025 // Returns a uuid with all '0's.
Austin Schuh81d0fe42022-08-17 16:29:23 -070026 static constexpr UUID Zero() {
27 UUID result;
28 std::memset(result.data_.data(), 0, result.data_.size());
29 return result;
30 }
Brian Silverman1f345222020-09-24 21:14:48 -070031
Austin Schuh5e2bfb82021-03-13 22:46:55 -080032 // 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 Schuh20ac95d2020-12-05 17:24:19 -080036
Austin Schuh5e2bfb82021-03-13 22:46:55 -080037 // 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 Strots72060d62022-10-10 19:23:53 -070041 // Initializes new UUID from data.
42 static UUID FromSpan(absl::Span<const uint8_t> data);
43
Austin Schuh5e2bfb82021-03-13 22:46:55 -080044 // Returns the boot UUID for the current linux computer.
Austin Schuh20ac95d2020-12-05 17:24:19 -080045 static UUID BootUUID();
46
Austin Schuh5e2bfb82021-03-13 22:46:55 -080047 // Default constructor which builds an uninitialized UUID. Use one of the
48 // static methods if you want something more useful.
Austin Schuh81d0fe42022-08-17 16:29:23 -070049 constexpr UUID() : data_() {}
Brian Silverman25969762022-08-24 19:54:00 -070050 constexpr UUID(const UUID &uuid) = default;
Austin Schuh81d0fe42022-08-17 16:29:23 -070051
Brian Silverman25969762022-08-24 19:54:00 -070052 constexpr UUID &operator=(const UUID &other) = default;
Austin Schuh20ac95d2020-12-05 17:24:19 -080053
Austin Schuh5e2bfb82021-03-13 22:46:55 -080054 // 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 Schuh81d0fe42022-08-17 16:29:23 -070067 // 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 Schuh5e2bfb82021-03-13 22:46:55 -080080 // 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 Schuh64fab802020-09-09 22:47:47 -070083 }
84
Austin Schuh4385b142021-03-14 21:31:13 -070085 bool operator==(const UUID &other) const { return other.span() == span(); }
Austin Schuh58646e22021-08-23 23:51:46 -070086 bool operator<(const UUID &other) const { return other.span() < span(); }
Austin Schuh4385b142021-03-14 21:31:13 -070087 bool operator!=(const UUID &other) const { return other.span() != span(); }
Austin Schuh64fab802020-09-09 22:47:47 -070088
89 private:
Austin Schuh5e2bfb82021-03-13 22:46:55 -080090 friend std::ostream &operator<<(std::ostream &os, const UUID &uuid);
Austin Schuh64fab802020-09-09 22:47:47 -070091
Austin Schuh5e2bfb82021-03-13 22:46:55 -080092 // Encoded storage for the data.
93 std::array<uint8_t, kDataSize> data_;
Austin Schuh64fab802020-09-09 22:47:47 -070094};
95
Austin Schuh5e2bfb82021-03-13 22:46:55 -080096std::ostream &operator<<(std::ostream &os, const UUID &uuid);
97
Austin Schuh64fab802020-09-09 22:47:47 -070098} // namespace aos
99
100#endif // AOS_EVENTS_LOGGING_UUID_H_