23 #ifndef ETCPAL_CPP_UUID_H_
24 #define ETCPAL_CPP_UUID_H_
30 #include "etcpal/pack.h"
31 #include "etcpal/uuid.h"
105 const uint8_t*
data() const noexcept;
107 bool IsNull() const noexcept;
120 static
Uuid V1() noexcept;
121 static
Uuid V3(const
Uuid& ns, const
void* name,
size_t name_len) noexcept;
122 static
Uuid V3(const
Uuid& ns, const
char* name) noexcept;
123 static
Uuid V3(const
Uuid& ns, const std::
string& name) noexcept;
124 static
Uuid V4() noexcept;
125 static
Uuid V5(const
Uuid& ns, const
void* name,
size_t name_len) noexcept;
126 static
Uuid V5(const
Uuid& ns, const
char* name) noexcept;
127 static
Uuid V5(const
Uuid& ns, const std::
string& name) noexcept;
129 static
Uuid Device(const std::
string& device_str, const uint8_t* mac_addr, uint32_t uuid_num) noexcept;
130 static
Uuid Device(const std::
string& device_str, const std::array<uint8_t, 6>& mac_addr, uint32_t uuid_num) noexcept;
155 uint16_t time_hi_and_version,
156 const uint8_t clock_seq_and_node[8])
171 uint16_t time_hi_and_version,
172 const std::array<uint8_t, 8>& clock_seq_and_node)
173 :
Uuid(time_low, time_mid, time_hi_and_version, clock_seq_and_node.data())
204 return std::string();
217 uint8_t vers_val = uuid_.
data[6] >> 4;
218 return (vers_val >= 1 && vers_val <= 5) ?
static_cast<UuidVersion>(vers_val) : UuidVersion::Unknown;
243 std::array<uint8_t, 8> res;
244 std::memcpy(res.data(), &uuid_.
data[8], 8);
261 return FromString(uuid_str.c_str());
291 return V3(ns, name, std::strlen(name));
301 return V3(ns, name.c_str(), name.length());
331 return V5(ns, name, std::strlen(name));
341 return V5(ns, name.c_str(), name.length());
358 inline Uuid Uuid::Device(
const std::string& device_str,
const uint8_t* mac_addr, uint32_t uuid_num) noexcept
369 const std::array<uint8_t, 6>& mac_addr,
370 uint32_t uuid_num) noexcept
385 inline bool operator==(
const EtcPalUuid& c_uuid,
const Uuid& uuid) noexcept
387 return c_uuid == uuid.get();
390 inline bool operator!=(
const EtcPalUuid& c_uuid,
const Uuid& uuid) noexcept
392 return !(c_uuid == uuid);
395 inline bool operator==(
const Uuid& uuid,
const EtcPalUuid& c_uuid) noexcept
397 return uuid.get() == c_uuid;
400 inline bool operator!=(
const Uuid& uuid,
const EtcPalUuid& c_uuid) noexcept
402 return !(uuid == c_uuid);
407 inline bool operator==(
const Uuid& a,
const Uuid& b) noexcept
409 return a.get() == b.get();
412 inline bool operator!=(
const Uuid& a,
const Uuid& b) noexcept
417 inline bool operator<(
const Uuid& a,
const Uuid& b) noexcept
419 return a.get() < b.get();
422 inline bool operator>(
const Uuid& a,
const Uuid& b) noexcept
427 inline bool operator<=(
const Uuid& a,
const Uuid& b) noexcept
432 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:267
uint16_t time_hi_and_version() const noexcept
Get the time_hi_and_version portion of a UUID.
Definition: uuid.h:234
uint32_t time_low() const noexcept
Get the time_low portion of a UUID.
Definition: uuid.h:222
std::string ToString() const
Convert the UUID to a string representation formatted per RFC 4122.
Definition: uuid.h:198
const uint8_t * data() const noexcept
Get the raw data of a UUID.
Definition: uuid.h:192
bool IsNull() const noexcept
Check if a UUID is null (all 0's).
Definition: uuid.h:208
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:358
static Uuid V4() noexcept
Generate and return a Version 4 UUID.
Definition: uuid.h:307
uint16_t time_mid() const noexcept
Get the time_mid portion of a UUID.
Definition: uuid.h:228
constexpr const EtcPalUuid & get() const noexcept
Get a reference to the underlying C type.
Definition: uuid.h:185
static Uuid FromString(const char *uuid_str) noexcept
Create a UUID from a string representation.
Definition: uuid.h:250
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:277
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:241
UuidVersion version() const noexcept
Get the version type of a UUID.
Definition: uuid.h:215
Uuid & operator=(const EtcPalUuid &c_uuid) noexcept
Assign an instance of the C EtcPalUuid type to an instance of this class.
Definition: uuid.h:178
static Uuid OsPreferred() noexcept
Generate and return a UUID of the version preferred by the underlying OS.
Definition: uuid.h:348
static Uuid V5(const Uuid &ns, const void *name, size_t name_len) noexcept
Generate and return a Version 5 UUID.
Definition: uuid.h:317
UuidVersion
Represents a UUID version as defined in RFC 4122.
Definition: uuid.h:72
@ Unknown
Unknown UUID version.
@ V5
Version 5 UUID: Namespace-based, SHA-1.
@ V1
Version 1 UUID: Date-time and MAC address.
@ V3
Version 3 UUID: Namespace-based, MD5.
@ V2
Version 2 UUID: DCE Security version (rarely used)
uint32_t etcpal_unpack_u32b(const uint8_t *buf)
Unpack a uint32_t from a known big-endian buffer.
Definition: pack.c:174
void etcpal_pack_u32b(uint8_t *buf, uint32_t val)
Pack a uint32_t to a known big-endian buffer.
Definition: pack.c:192
void etcpal_pack_u16b(uint8_t *buf, uint16_t val)
Pack a uint16_t to a known big-endian buffer.
Definition: pack.c:90
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
#define ETCPAL_UUID_STRING_BYTES
The maximum number of bytes required to hold an ASCII string representation of a UUID.
Definition: uuid.h:121
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