EtcPal  0.4.1
ETC Platform Abstraction Layer (EtcPal)
View other versions:
uuid.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2022 ETC Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************
16  * This file is a part of EtcPal. For more information, go to:
17  * https://github.com/ETCLabs/EtcPal
18  ******************************************************************************/
19 
22 
23 #ifndef ETCPAL_CPP_UUID_H_
24 #define ETCPAL_CPP_UUID_H_
25 
26 #include <cstdint>
27 #include <cstring>
28 #include <array>
29 #include <string>
30 #include "etcpal/pack.h"
31 #include "etcpal/uuid.h"
32 
33 namespace etcpal
34 {
35 // clang-format off
68 // clang-format on
69 
71 enum class UuidVersion
72 {
73  kV1 = 1,
74  kV2 = 2,
75  kV3 = 3,
76  kV4 = 4,
77  kV5 = 5,
78  kUnknown
79 };
80 
86 class Uuid
87 {
88 public:
90  Uuid() = default;
91 
92  Uuid(const uint8_t* data) noexcept;
93 
94  // Note: this constructor is not explicit by design, to allow implicit conversions e.g.
95  // etcpal::Uuid uuid = etcpal_c_function_that_returns_uuid();
96  constexpr Uuid(const EtcPalUuid& c_uuid) noexcept;
97  Uuid& operator=(const EtcPalUuid& c_uuid) noexcept;
98 
99  constexpr const EtcPalUuid& get() const noexcept;
100  const uint8_t* data() const noexcept;
101  std::string ToString() const;
102  bool IsNull() const noexcept;
103  UuidVersion version() const noexcept;
104 
107  uint32_t time_low() const noexcept;
108  uint16_t time_mid() const noexcept;
109  uint16_t time_hi_and_version() const noexcept;
110  std::array<uint8_t, 8> clock_seq_and_node() const noexcept;
112 
113  static Uuid FromString(const char* uuid_str) noexcept;
114  static Uuid FromString(const std::string& uuid_str) 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;
123  static Uuid OsPreferred() 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;
126 
127 private:
129 };
130 
132 constexpr Uuid::Uuid(const EtcPalUuid& c_uuid) noexcept : uuid_(c_uuid)
133 {
134 }
135 
138 inline Uuid::Uuid(const uint8_t* data) noexcept
139 {
140  std::memcpy(uuid_.data, data, ETCPAL_UUID_BYTES);
141 }
142 
144 inline Uuid& Uuid::operator=(const EtcPalUuid& c_uuid) noexcept
145 {
146  uuid_ = c_uuid;
147  return *this;
148 }
149 
151 constexpr const EtcPalUuid& Uuid::get() const noexcept
152 {
153  return uuid_;
154 }
155 
158 inline const uint8_t* Uuid::data() const noexcept
159 {
160  return uuid_.data;
161 }
162 
164 inline std::string Uuid::ToString() const
165 {
166  std::array<char, ETCPAL_UUID_STRING_BYTES> str_buf{};
167  if (etcpal_uuid_to_string(&uuid_, str_buf.data()))
168  return {str_buf.data()};
169  return {};
170 }
171 
173 inline bool Uuid::IsNull() const noexcept
174 {
175  return ETCPAL_UUID_IS_NULL(&uuid_);
176 }
177 
180 inline UuidVersion Uuid::version() const noexcept
181 {
182  uint8_t vers_val = uuid_.data[6] >> 4;
183  return (vers_val >= 1 && vers_val <= 5) ? static_cast<UuidVersion>(vers_val) : UuidVersion::kUnknown;
184 }
185 
187 inline uint32_t Uuid::time_low() const noexcept
188 {
189  return etcpal_unpack_u32b(&uuid_.data[0]);
190 }
191 
193 inline uint16_t Uuid::time_mid() const noexcept
194 {
195  return etcpal_unpack_u16b(&uuid_.data[4]);
196 }
197 
199 inline uint16_t Uuid::time_hi_and_version() const noexcept
200 {
201  return etcpal_unpack_u16b(&uuid_.data[6]);
202 }
203 
206 inline std::array<uint8_t, 8> Uuid::clock_seq_and_node() const noexcept
207 {
208  std::array<uint8_t, 8> res{};
209  std::memcpy(res.data(), &uuid_.data[8], 8);
210  return res;
211 }
212 
215 inline Uuid Uuid::FromString(const char* uuid_str) noexcept
216 {
217  Uuid uuid;
218  etcpal_string_to_uuid(uuid_str, &uuid.uuid_);
219  return uuid;
220 }
221 
224 inline Uuid Uuid::FromString(const std::string& uuid_str) noexcept
225 {
226  return FromString(uuid_str.c_str());
227 }
228 
232 inline Uuid Uuid::V1() noexcept
233 {
234  Uuid uuid;
235  etcpal_generate_v1_uuid(&uuid.uuid_);
236  return uuid;
237 }
238 
242 inline Uuid Uuid::V3(const Uuid& ns, const void* name, size_t name_len) noexcept
243 {
244  Uuid uuid;
245  etcpal_generate_v3_uuid(&ns.get(), name, name_len, &uuid.uuid_);
246  return uuid;
247 }
248 
254 inline Uuid Uuid::V3(const Uuid& ns, const char* name) noexcept
255 {
256  return V3(ns, name, std::strlen(name));
257 }
258 
264 inline Uuid Uuid::V3(const Uuid& ns, const std::string& name) noexcept
265 {
266  return V3(ns, name.c_str(), name.length());
267 }
268 
272 inline Uuid Uuid::V4() noexcept
273 {
274  Uuid uuid;
275  etcpal_generate_v4_uuid(&uuid.uuid_);
276  return uuid;
277 }
278 
282 inline Uuid Uuid::V5(const Uuid& ns, const void* name, size_t name_len) noexcept
283 {
284  Uuid uuid;
285  etcpal_generate_v5_uuid(&ns.get(), name, name_len, &uuid.uuid_);
286  return uuid;
287 }
288 
294 inline Uuid Uuid::V5(const Uuid& ns, const char* name) noexcept
295 {
296  return V5(ns, name, std::strlen(name));
297 }
298 
304 inline Uuid Uuid::V5(const Uuid& ns, const std::string& name) noexcept
305 {
306  return V5(ns, name.c_str(), name.length());
307 }
308 
313 inline Uuid Uuid::OsPreferred() noexcept
314 {
315  Uuid uuid;
317  return uuid;
318 }
319 
323 inline Uuid Uuid::Device(const std::string& device_str, const uint8_t* mac_addr, uint32_t uuid_num) noexcept
324 {
325  Uuid uuid;
326  etcpal_generate_device_uuid(device_str.c_str(), mac_addr, uuid_num, &uuid.uuid_);
327  return uuid;
328 }
329 
333 inline Uuid Uuid::Device(const std::string& device_str,
334  const std::array<uint8_t, 6>& mac_addr,
335  uint32_t uuid_num) noexcept
336 {
337  Uuid uuid;
338  etcpal_generate_device_uuid(device_str.c_str(), mac_addr.data(), uuid_num, &uuid.uuid_);
339  return uuid;
340 }
341 
344 
347 
348 // Special operators for comparing with EtcPalUuids
349 
350 inline bool operator==(const EtcPalUuid& c_uuid, const Uuid& uuid) noexcept
351 {
352  return c_uuid == uuid.get();
353 }
354 
355 inline bool operator!=(const EtcPalUuid& c_uuid, const Uuid& uuid) noexcept
356 {
357  return !(c_uuid == uuid);
358 }
359 
360 inline bool operator==(const Uuid& uuid, const EtcPalUuid& c_uuid) noexcept
361 {
362  return uuid.get() == c_uuid;
363 }
364 
365 inline bool operator!=(const Uuid& uuid, const EtcPalUuid& c_uuid) noexcept
366 {
367  return !(uuid == c_uuid);
368 }
369 
370 // Standard operators
371 
372 inline bool operator==(const Uuid& a, const Uuid& b) noexcept
373 {
374  return a.get() == b.get();
375 }
376 
377 inline bool operator!=(const Uuid& a, const Uuid& b) noexcept
378 {
379  return !(a == b);
380 }
381 
382 inline bool operator<(const Uuid& a, const Uuid& b) noexcept
383 {
384  return a.get() < b.get();
385 }
386 
387 inline bool operator>(const Uuid& a, const Uuid& b) noexcept
388 {
389  return b < a;
390 }
391 
392 inline bool operator<=(const Uuid& a, const Uuid& b) noexcept
393 {
394  return !(b < a);
395 }
396 
397 inline bool operator>=(const Uuid& a, const Uuid& b) noexcept
398 {
399  return !(a < b);
400 }
401 
404 
405 }; // namespace etcpal
406 
407 #endif // ETCPAL_CPP_UUID_H_
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