23 #ifndef ETCPAL_CPP_UUID_H_
24 #define ETCPAL_CPP_UUID_H_
30 #include "etcpal/pack.h"
31 #include "etcpal/uuid.h"
32 #include "etcpal/cpp/hash.h"
101 const uint8_t*
data() const noexcept;
103 bool IsNull() const noexcept;
116 static
Uuid V1() noexcept;
117 static
Uuid V3(const
Uuid& ns, const
void* name,
size_t name_len) noexcept;
118 static
Uuid V3(const
Uuid& ns, const
char* name) noexcept;
119 static
Uuid V3(const
Uuid& ns, const std::
string& name) noexcept;
120 static
Uuid V4() noexcept;
121 static
Uuid V5(const
Uuid& ns, const
void* name,
size_t name_len) noexcept;
122 static
Uuid V5(const
Uuid& ns, const
char* name) noexcept;
123 static
Uuid V5(const
Uuid& ns, const std::
string& name) noexcept;
125 static
Uuid Device(const std::
string& device_str, const uint8_t* mac_addr, uint32_t uuid_num) noexcept;
126 static
Uuid Device(const std::
string& device_str, const std::array<uint8_t, 6>& mac_addr, uint32_t uuid_num) noexcept;
167 std::array<char, ETCPAL_UUID_STRING_BYTES> str_buf{};
169 return {str_buf.data()};
183 uint8_t vers_val = uuid_.
data[6] >> 4;
184 return (vers_val >= 1 && vers_val <= 5) ?
static_cast<UuidVersion>(vers_val) : UuidVersion::kUnknown;
209 std::array<uint8_t, 8> res{};
210 std::memcpy(res.data(), &uuid_.
data[8], 8);
227 return FromString(uuid_str.c_str());
257 return V3(ns, name, std::strlen(name));
267 return V3(ns, name.c_str(), name.length());
297 return V5(ns, name, std::strlen(name));
307 return V5(ns, name.c_str(), name.length());
324 inline Uuid Uuid::Device(
const std::string& device_str,
const uint8_t* mac_addr, uint32_t uuid_num) noexcept
335 const std::array<uint8_t, 6>& mac_addr,
336 uint32_t uuid_num) noexcept
351 inline bool operator==(
const EtcPalUuid& c_uuid,
const Uuid& uuid) noexcept
353 return c_uuid == uuid.get();
356 inline bool operator!=(
const EtcPalUuid& c_uuid,
const Uuid& uuid) noexcept
358 return !(c_uuid == uuid);
361 inline bool operator==(
const Uuid& uuid,
const EtcPalUuid& c_uuid) noexcept
363 return uuid.get() == c_uuid;
366 inline bool operator!=(
const Uuid& uuid,
const EtcPalUuid& c_uuid) noexcept
368 return !(uuid == c_uuid);
373 inline bool operator==(
const Uuid& a,
const Uuid& b) noexcept
375 return a.get() == b.get();
378 inline bool operator!=(
const Uuid& a,
const Uuid& b) noexcept
383 inline bool operator<(
const Uuid& a,
const Uuid& b) noexcept
385 return a.get() < b.get();
388 inline bool operator>(
const Uuid& a,
const Uuid& b) noexcept
393 inline bool operator<=(
const Uuid& a,
const Uuid& b) noexcept
398 inline bool operator>=(
const Uuid& a,
const Uuid& b) noexcept
419 std::size_t operator()(const ::etcpal::Uuid& uuid)
const noexcept
A wrapper class for the EtcPal UUID type.
Definition: uuid.h:88
static Uuid V1() noexcept
Generate and return a Version 1 UUID.
Definition: uuid.h:233
uint16_t time_hi_and_version() const noexcept
Get the time_hi_and_version portion of a UUID.
Definition: uuid.h:200
uint32_t time_low() const noexcept
Get the time_low portion of a UUID.
Definition: uuid.h:188
std::string ToString() const
Convert the UUID to a string representation formatted per RFC 4122.
Definition: uuid.h:165
const uint8_t * data() const noexcept
Get the raw data of a UUID.
Definition: uuid.h:159
bool IsNull() const noexcept
Check if a UUID is null (all 0's).
Definition: uuid.h:174
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:324
static Uuid V4() noexcept
Generate and return a Version 4 UUID.
Definition: uuid.h:273
uint16_t time_mid() const noexcept
Get the time_mid portion of a UUID.
Definition: uuid.h:194
constexpr const EtcPalUuid & get() const noexcept
Get a reference to the underlying C type.
Definition: uuid.h:152
static Uuid FromString(const char *uuid_str) noexcept
Create a UUID from a string representation.
Definition: uuid.h:216
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:243
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:207
UuidVersion version() const noexcept
Get the version type of a UUID.
Definition: uuid.h:181
Uuid & operator=(const EtcPalUuid &c_uuid) noexcept
Assign an instance of the C EtcPalUuid type to an instance of this class.
Definition: uuid.h:145
static Uuid OsPreferred() noexcept
Generate and return a UUID of the version preferred by the underlying OS.
Definition: uuid.h:314
static Uuid V5(const Uuid &ns, const void *name, size_t name_len) noexcept
Generate and return a Version 5 UUID.
Definition: uuid.h:283
UuidVersion
Represents a UUID version as defined in RFC 4122.
Definition: uuid.h:73
@ 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.
void HashCombine(size_t &seed, const T &val)
A function that combines the hash of a new value into an existing seed, based on boost::hash_combine.
Definition: hash.h:52
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:119
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:90
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:94
uint8_t data[ETCPAL_UUID_BYTES]
The 16-byte UUID data.
Definition: uuid.h:95