23 #ifndef ETCPAL_CPP_UUID_H_
24 #define ETCPAL_CPP_UUID_H_
30 #include "etcpal/pack.h"
31 #include "etcpal/uuid.h"
100 const uint8_t*
data() const noexcept;
102 bool IsNull() const noexcept;
115 static
Uuid V1() noexcept;
116 static
Uuid V3(const
Uuid& ns, const
void* name,
size_t name_len) noexcept;
117 static
Uuid V3(const
Uuid& ns, const
char* name) noexcept;
118 static
Uuid V3(const
Uuid& ns, const std::
string& name) noexcept;
119 static
Uuid V4() noexcept;
120 static
Uuid V5(const
Uuid& ns, const
void* name,
size_t name_len) noexcept;
121 static
Uuid V5(const
Uuid& ns, const
char* name) noexcept;
122 static
Uuid V5(const
Uuid& ns, const std::
string& name) noexcept;
124 static
Uuid Device(const std::
string& device_str, const uint8_t* mac_addr, uint32_t uuid_num) noexcept;
125 static
Uuid Device(const std::
string& device_str, const std::array<uint8_t, 6>& mac_addr, uint32_t uuid_num) noexcept;
166 std::array<char, ETCPAL_UUID_STRING_BYTES> str_buf{};
168 return {str_buf.data()};
182 uint8_t vers_val = uuid_.
data[6] >> 4;
183 return (vers_val >= 1 && vers_val <= 5) ?
static_cast<UuidVersion>(vers_val) : UuidVersion::kUnknown;
208 std::array<uint8_t, 8> res{};
209 std::memcpy(res.data(), &uuid_.
data[8], 8);
226 return FromString(uuid_str.c_str());
256 return V3(ns, name, std::strlen(name));
266 return V3(ns, name.c_str(), name.length());
296 return V5(ns, name, std::strlen(name));
306 return V5(ns, name.c_str(), name.length());
323 inline Uuid Uuid::Device(
const std::string& device_str,
const uint8_t* mac_addr, uint32_t uuid_num) noexcept
334 const std::array<uint8_t, 6>& mac_addr,
335 uint32_t uuid_num) noexcept
350 inline bool operator==(
const EtcPalUuid& c_uuid,
const Uuid& uuid) noexcept
352 return c_uuid == uuid.get();
355 inline bool operator!=(
const EtcPalUuid& c_uuid,
const Uuid& uuid) noexcept
357 return !(c_uuid == uuid);
360 inline bool operator==(
const Uuid& uuid,
const EtcPalUuid& c_uuid) noexcept
362 return uuid.get() == c_uuid;
365 inline bool operator!=(
const Uuid& uuid,
const EtcPalUuid& c_uuid) noexcept
367 return !(uuid == c_uuid);
372 inline bool operator==(
const Uuid& a,
const Uuid& b) noexcept
374 return a.get() == b.get();
377 inline bool operator!=(
const Uuid& a,
const Uuid& b) noexcept
382 inline bool operator<(
const Uuid& a,
const Uuid& b) noexcept
384 return a.get() < b.get();
387 inline bool operator>(
const Uuid& a,
const Uuid& b) noexcept
392 inline bool operator<=(
const Uuid& a,
const Uuid& b) noexcept
397 inline bool operator>=(
const Uuid& a,
const Uuid& b) noexcept
A wrapper class for the EtcPal UUID type.
Definition: uuid.h:87
static Uuid V1() noexcept
Generate and return a Version 1 UUID.
Definition: uuid.h:232
uint16_t time_hi_and_version() const noexcept
Get the time_hi_and_version portion of a UUID.
Definition: uuid.h:199
uint32_t time_low() const noexcept
Get the time_low portion of a UUID.
Definition: uuid.h:187
std::string ToString() const
Convert the UUID to a string representation formatted per RFC 4122.
Definition: uuid.h:164
const uint8_t * data() const noexcept
Get the raw data of a UUID.
Definition: uuid.h:158
bool IsNull() const noexcept
Check if a UUID is null (all 0's).
Definition: uuid.h:173
static Uuid Device(const std::string &device_str, const uint8_t *mac_addr, uint32_t uuid_num) noexcept
Generate and return a Device UUID.
Definition: uuid.h:323
static Uuid V4() noexcept
Generate and return a Version 4 UUID.
Definition: uuid.h:272
uint16_t time_mid() const noexcept
Get the time_mid portion of a UUID.
Definition: uuid.h:193
constexpr const EtcPalUuid & get() const noexcept
Get a reference to the underlying C type.
Definition: uuid.h:151
static Uuid FromString(const char *uuid_str) noexcept
Create a UUID from a string representation.
Definition: uuid.h:215
Uuid()=default
Constructs a null UUID by default.
static Uuid V3(const Uuid &ns, const void *name, size_t name_len) noexcept
Generate and return a Version 3 UUID.
Definition: uuid.h:242
std::array< uint8_t, 8 > clock_seq_and_node() const noexcept
Get the remaining portion of a UUID, including clock_seq_hi_and_res, clock_seq_low,...
Definition: uuid.h:206
UuidVersion version() const noexcept
Get the version type of a UUID.
Definition: uuid.h:180
Uuid & operator=(const EtcPalUuid &c_uuid) noexcept
Assign an instance of the C EtcPalUuid type to an instance of this class.
Definition: uuid.h:144
static Uuid OsPreferred() noexcept
Generate and return a UUID of the version preferred by the underlying OS.
Definition: uuid.h:313
static Uuid V5(const Uuid &ns, const void *name, size_t name_len) noexcept
Generate and return a Version 5 UUID.
Definition: uuid.h:282
UuidVersion
Represents a UUID version as defined in RFC 4122.
Definition: uuid.h:72
@ kUnknown
Unknown UUID version.
@ kV3
Version 3 UUID: Namespace-based, MD5.
@ kV1
Version 1 UUID: Date-time and MAC address.
@ kV2
Version 2 UUID: DCE Security version (rarely used)
@ kV5
Version 5 UUID: Namespace-based, SHA-1.
uint32_t etcpal_unpack_u32b(const uint8_t *buf)
Unpack a uint32_t from a known big-endian buffer.
Definition: pack.c:174
uint16_t etcpal_unpack_u16b(const uint8_t *buf)
Unpack a uint16_t from a known big-endian buffer.
Definition: pack.c:74
etcpal_error_t etcpal_generate_device_uuid(const char *dev_str, const uint8_t *mac_addr, uint32_t uuid_num, EtcPalUuid *uuid)
Generate a UUID from a combination of a custom string and MAC address.
Definition: uuid.c:309
etcpal_error_t etcpal_generate_v4_uuid(EtcPalUuid *uuid)
Generate a Version 4 UUID.
Definition: uuid.c:216
#define ETCPAL_UUID_IS_NULL(uuidptr)
Determine if a UUID is null.
Definition: uuid.h:118
bool etcpal_uuid_to_string(const EtcPalUuid *uuid, char *buf)
Create a string representation of a UUID.
Definition: uuid.c:61
etcpal_error_t etcpal_generate_v1_uuid(EtcPalUuid *uuid)
Generate a Version 1 UUID.
Definition: uuid.c:152
bool etcpal_string_to_uuid(const char *str, EtcPalUuid *uuid)
Create a UUID from a string representation.
Definition: uuid.c:85
etcpal_error_t etcpal_generate_os_preferred_uuid(EtcPalUuid *uuid)
Generate the preferred UUID version of the underlying OS.
Definition: uuid.c:282
etcpal_error_t etcpal_generate_v5_uuid(const EtcPalUuid *ns, const void *name, size_t name_len, EtcPalUuid *uuid)
Generate a Version 5 UUID.
Definition: uuid.c:243
etcpal_error_t etcpal_generate_v3_uuid(const EtcPalUuid *ns, const void *name, size_t name_len, EtcPalUuid *uuid)
Generate a Version 3 UUID.
Definition: uuid.c:178
#define ETCPAL_UUID_BYTES
The number of bytes that make up a UUID.
Definition: uuid.h:89
const EtcPalUuid kEtcPalNullUuid
A null (all 0's) UUID, used by ETCPAL_UUID_IS_NULL() for comparison.
Definition: uuid.c:46
The UUID type.
Definition: uuid.h:93
uint8_t data[ETCPAL_UUID_BYTES]
The 16-byte UUID data.
Definition: uuid.h:94