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() = default;
40 
41  explicit Uuid(const char* s)
42  {
43  decompose(s);
44  }
45 
46  explicit Uuid(const char* s, size_t len)
47  {
48  decompose(s, len);
49  }
50 
51  explicit Uuid(const String& s) : Uuid(s.c_str(), s.length())
52  {
53  }
54 
55  explicit Uuid(const FlashString& s) : Uuid(String(s))
56  {
57  }
58 
59  explicit constexpr Uuid(uint32_t time_low, uint16_t time_mid, uint16_t time_hi_and_version,
60  uint8_t clock_seq_hi_and_reserved, uint8_t clock_seq_low, uint8_t n1, uint8_t n2,
61  uint8_t n3, uint8_t n4, uint8_t n5, uint8_t n6)
64  clock_seq_low(clock_seq_low), node{n1, n2, n3, n4, n5, n6}
65  {
66  }
67 
68  explicit operator bool() const
69  {
70  return *this != Uuid{};
71  }
72 
73  bool operator==(const Uuid& other) const;
74 
75  bool operator!=(const Uuid& other) const
76  {
77  return !operator==(other);
78  }
79 
85  bool generate(MacAddress mac);
86 
93  bool generate();
94 
99  bool decompose(const char* s, size_t len);
100 
101  bool decompose(const char* s)
102  {
103  return s ? decompose(s, strlen(s)) : false;
104  }
105 
106  bool decompose(const String& s)
107  {
108  return decompose(s.c_str(), s.length());
109  }
128  size_t toString(char* buffer, size_t bufSize) const;
129 
130  String toString() const;
131 
132  operator String() const
133  {
134  return toString();
135  }
136 
138 };
139 
140 static_assert(sizeof(Uuid) == 16, "Bad Uuid");
141 
142 inline String toString(const Uuid& uuid)
143 {
144  return uuid.toString();
145 }
146 
147 inline bool fromString(const char* s, Uuid& uuid)
148 {
149  return uuid.decompose(s);
150 }
151 
152 inline bool fromString(const String& s, Uuid& uuid)
153 {
154  return uuid.decompose(s);
155 }
156 
String toString(const Uuid &uuid)
Definition: Uuid.h:142
bool fromString(const char *s, Uuid &uuid)
Definition: Uuid.h:147
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:133
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:609
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:238
#define SMING_DEPRECATED
Definition: sming_attr.h:37
Class for manipulating UUID (aka GUID) entities.
Definition: Uuid.h:26
bool decompose(const char *s)
Definition: Uuid.h:101
bool operator!=(const Uuid &other) const
Definition: Uuid.h:75
Uuid(const FlashString &s)
Definition: Uuid.h:55
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:59
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:41
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
constexpr Uuid()=default
bool generate()
Generate UUID using random number instead of MAC.
Uuid(const String &s)
Definition: Uuid.h:51
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:106
uint16_t time_hi_and_version
Definition: Uuid.h:29
String toString() const
Uuid(const char *s, size_t len)
Definition: Uuid.h:46
size_t toString(char *buffer, size_t bufSize) const