blob: aabca379def3e541dfa0abfabfc7a0ad5658ac4f [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"
Austin Schuh81d0fe42022-08-17 16:29:23 -07009#include "aos/thread_local.h"
Austin Schuh5e2bfb82021-03-13 22:46:55 -080010#include "flatbuffers/flatbuffers.h"
Austin Schuh64fab802020-09-09 22:47:47 -070011
12namespace aos {
13
14// Class to generate and hold a UUID.
15class UUID {
16 public:
Austin Schuh5e2bfb82021-03-13 22:46:55 -080017 // Size of a UUID both as a string and the raw data.
18 static constexpr size_t kStringSize = 36;
19 static constexpr size_t kDataSize = 16;
20
Austin Schuh64fab802020-09-09 22:47:47 -070021 // Returns a randomly generated UUID. This is known as a UUID4.
22 static UUID Random();
23
Austin Schuh5e2bfb82021-03-13 22:46:55 -080024 // Returns a uuid with all '0's.
Austin Schuh81d0fe42022-08-17 16:29:23 -070025 static constexpr UUID Zero() {
26 UUID result;
27 std::memset(result.data_.data(), 0, result.data_.size());
28 return result;
29 }
Brian Silverman1f345222020-09-24 21:14:48 -070030
Austin Schuh5e2bfb82021-03-13 22:46:55 -080031 // Converts a string UUID of the form 00000000-0000-0000-0000-000000000000 to
32 // a UUID.
33 static UUID FromString(std::string_view string);
34 static UUID FromString(const flatbuffers::String *string);
Austin Schuh20ac95d2020-12-05 17:24:19 -080035
Austin Schuh5e2bfb82021-03-13 22:46:55 -080036 // Converts a 16 byte vector (128 bits) to a UUID. This requires no
37 // transformation.
38 static UUID FromVector(const flatbuffers::Vector<uint8_t> *data);
39
Alexei Strots72060d62022-10-10 19:23:53 -070040 // Initializes new UUID from data.
41 static UUID FromSpan(absl::Span<const uint8_t> data);
42
Austin Schuh5e2bfb82021-03-13 22:46:55 -080043 // Returns the boot UUID for the current linux computer.
Austin Schuh20ac95d2020-12-05 17:24:19 -080044 static UUID BootUUID();
45
Austin Schuh5e2bfb82021-03-13 22:46:55 -080046 // Default constructor which builds an uninitialized UUID. Use one of the
47 // static methods if you want something more useful.
Austin Schuh81d0fe42022-08-17 16:29:23 -070048 constexpr UUID() : data_() {}
Brian Silverman25969762022-08-24 19:54:00 -070049 constexpr UUID(const UUID &uuid) = default;
Austin Schuh81d0fe42022-08-17 16:29:23 -070050
Brian Silverman25969762022-08-24 19:54:00 -070051 constexpr UUID &operator=(const UUID &other) = default;
Austin Schuh20ac95d2020-12-05 17:24:19 -080052
Austin Schuh5e2bfb82021-03-13 22:46:55 -080053 // Packs this UUID into a flatbuffer as a string.
54 flatbuffers::Offset<flatbuffers::String> PackString(
55 flatbuffers::FlatBufferBuilder *fbb) const;
56 // Copies this UUID as a string into the memory pointed by result. Result
57 // must be at least kStringSize long.
58 void CopyTo(char *result) const;
59 // Returns this UUID as a string.
60 std::string ToString() const;
61
62 // Packs the UUID bytes directly into a vector.
63 flatbuffers::Offset<flatbuffers::Vector<uint8_t>> PackVector(
64 flatbuffers::FlatBufferBuilder *fbb) const;
65
Austin Schuh81d0fe42022-08-17 16:29:23 -070066 // Returns a human-readable string representing this UUID.
67 //
68 // This is done without any memory allocation, which means it's returned in a
69 // thread-local buffer.
70 //
71 // Be careful using this. It's mostly useful for low-level tracing of UUIDs
72 // through the system.
73 const char *thread_local_string() const {
74 AOS_THREAD_LOCAL char buffer[kStringSize + 1];
75 CopyTo(buffer);
76 return buffer;
77 }
78
Austin Schuh5e2bfb82021-03-13 22:46:55 -080079 // Returns the underlying UUID data.
80 absl::Span<const uint8_t> span() const {
81 return absl::Span<const uint8_t>(data_.data(), data_.size());
Austin Schuh64fab802020-09-09 22:47:47 -070082 }
83
Austin Schuh4385b142021-03-14 21:31:13 -070084 bool operator==(const UUID &other) const { return other.span() == span(); }
Austin Schuh58646e22021-08-23 23:51:46 -070085 bool operator<(const UUID &other) const { return other.span() < span(); }
Austin Schuh4385b142021-03-14 21:31:13 -070086 bool operator!=(const UUID &other) const { return other.span() != span(); }
Austin Schuh64fab802020-09-09 22:47:47 -070087
88 private:
Austin Schuh5e2bfb82021-03-13 22:46:55 -080089 friend std::ostream &operator<<(std::ostream &os, const UUID &uuid);
Austin Schuh64fab802020-09-09 22:47:47 -070090
Austin Schuh5e2bfb82021-03-13 22:46:55 -080091 // Encoded storage for the data.
92 std::array<uint8_t, kDataSize> data_;
Austin Schuh64fab802020-09-09 22:47:47 -070093};
94
Austin Schuh5e2bfb82021-03-13 22:46:55 -080095std::ostream &operator<<(std::ostream &os, const UUID &uuid);
96
Austin Schuh64fab802020-09-09 22:47:47 -070097} // namespace aos
98
99#endif // AOS_EVENTS_LOGGING_UUID_H_