Uuid.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * Uuid.h - Universal Unique Identifier
8  *
9  * @author mikee47 <mike@sillyhouse.net>
10  *
11  ****/
12 
13 #pragma once
14 
15 #include <WString.h>
16 #include <MacAddress.h>
17 
26 struct Uuid {
27  uint32_t time_low{0}; // 0-3
28  uint16_t time_mid{0}; // 4-5
29  uint16_t time_hi_and_version{0}; // 6-7, version = top 4 bits
30  uint8_t clock_seq_hi_and_reserved{0}; // 8, variant = top 2 bits
31  uint8_t clock_seq_low{0}; // 9
32  uint8_t node[6]{}; // 10-15
33 
37  static constexpr size_t stringSize = 36;
38 
39  constexpr Uuid()
40  {
41  }
42 
43  explicit Uuid(const char* s)
44  {
45  decompose(s);
46  }
47 
48  explicit Uuid(const char* s, size_t len)
49  {
50  decompose(s, len);
51  }
52 
53  explicit Uuid(const String& s) : Uuid(s.c_str(), s.length())
54  {
55  }
56 
57  explicit Uuid(const FlashString& s) : Uuid(String(s))
58  {
59  }
60 
61  explicit constexpr Uuid(uint32_t time_low, uint16_t time_mid, uint16_t time_hi_and_version,
62  uint8_t clock_seq_hi_and_reserved, uint8_t clock_seq_low, uint8_t n1, uint8_t n2,
63  uint8_t n3, uint8_t n4, uint8_t n5, uint8_t n6)
66  clock_seq_low(clock_seq_low), node{n1, n2, n3, n4, n5, n6}
67  {
68  }
69 
70  explicit operator bool() const
71  {
72  return *this != Uuid{};
73  }
74 
75  bool operator==(const Uuid& other) const;
76 
77  bool operator!=(const Uuid& other) const
78  {
79  return !operator==(other);
80  }
81 
87  bool generate(MacAddress mac);
88 
95  bool generate();
96 
101  bool decompose(const char* s, size_t len);
102 
103  bool decompose(const char* s)
104  {
105  return s ? decompose(s, strlen(s)) : false;
106  }
107 
108  bool decompose(const String& s)
109  {
110  return decompose(s.c_str(), s.length());
111  }
130  size_t toString(char* buffer, size_t bufSize) const;
131 
132  String toString() const;
133 
134  operator String() const
135  {
136  return toString();
137  }
138 
140 };
141 
142 static_assert(sizeof(Uuid) == 16, "Bad Uuid");
143 
144 inline String toString(const Uuid& uuid)
145 {
146  return uuid.toString();
147 }
148 
149 inline bool fromString(const char* s, Uuid& uuid)
150 {
151  return uuid.decompose(s);
152 }
153 
154 inline bool fromString(const String& s, Uuid& uuid)
155 {
156  return uuid.decompose(s);
157 }
158 
String toString(const Uuid &uuid)
Definition: Uuid.h:144
bool fromString(const char *s, Uuid &uuid)
Definition: Uuid.h:149
describes a counted string stored in flash memory
Definition: String.hpp:174
A network hardware (MAC) address.
Definition: MacAddress.h:39
The String class.
Definition: WString.h:137
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:616
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:243
#define SMING_DEPRECATED
Definition: sming_attr.h:36
Class for manipulating UUID (aka GUID) entities.
Definition: Uuid.h:26
bool decompose(const char *s)
Definition: Uuid.h:103
constexpr Uuid()
Definition: Uuid.h:39
bool operator!=(const Uuid &other) const
Definition: Uuid.h:77
Uuid(const FlashString &s)
Definition: Uuid.h:57
constexpr Uuid(uint32_t time_low, uint16_t time_mid, uint16_t time_hi_and_version, uint8_t clock_seq_hi_and_reserved, uint8_t clock_seq_low, uint8_t n1, uint8_t n2, uint8_t n3, uint8_t n4, uint8_t n5, uint8_t n6)
Definition: Uuid.h:61
bool generate(MacAddress mac)
Generate a UUID using a MAC node address.
uint16_t time_mid
Definition: Uuid.h:28
uint32_t time_low
Definition: Uuid.h:27
bool decompose(const char *s, size_t len)
Uuid(const char *s)
Definition: Uuid.h:43
bool operator==(const Uuid &other) const
static constexpr size_t stringSize
Number of characters in a UUID string (excluding NUL terminator)
Definition: Uuid.h:37
bool generate()
Generate UUID using random number instead of MAC.
Uuid(const String &s)
Definition: Uuid.h:53
uint8_t clock_seq_low
Definition: Uuid.h:31
uint8_t clock_seq_hi_and_reserved
Definition: Uuid.h:30
uint8_t node[6]
Definition: Uuid.h:32
bool decompose(const String &s)
Definition: Uuid.h:108
uint16_t time_hi_and_version
Definition: Uuid.h:29
String toString() const
Uuid(const char *s, size_t len)
Definition: Uuid.h:48
size_t toString(char *buffer, size_t bufSize) const