-
Notifications
You must be signed in to change notification settings - Fork 4
/
ESPRandom.h
132 lines (115 loc) · 3.33 KB
/
ESPRandom.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* ESPRandom: Generate random numbers and UUIDs on the ESP8266 and ESP32
*/
#ifndef ESPRandom_h
#define ESPRandom_h
#include <Arduino.h>
#include <WiFi.h>
#include <chrono>
class ESPRandom {
public:
/**
* Generate a random number
*
* \return Returns a 32 bit random number
*/
static uint32_t get();
/**
* Generate a v4 UUID in the buffer provided
*
* \deprecated Use version specified functions
*
* \param buffer A 16 byte array where the UUID will be saved in
*/
static void uuid(uint8_t buffer[16]);
/**
* Generate a v4 UUID in the buffer provided
*
* \param buffer A 16 byte array where the UUID will be saved in
*/
static void uuid4(uint8_t buffer[16]);
/**
* Generate a UUID in the buffer provided
*
* To be compliant, the time has to be set
*
* \param buffer A 16 byte array where the UUID will be saved in
*/
static void uuid1(uint8_t buffer[16]);
/**
* Generate a v4 UUID
*
* \deprecated Use uuid function with version number
* \see uuid1()
* \see uuid4()
*
* \return A vector with the generated UUID
*/
static std::vector<uint8_t> uuid();
/**
* Generate a v1 UUID
*
* \return A vector with the generated UUID
*/
static std::vector<uint8_t> uuid1();
/**
* Generate a v4 UUID
*
* \return A vector with the generated UUID
*/
static std::vector<uint8_t> uuid4();
/**
* Convert a binary UUID into a String
*
* \param buffer A 16 byte array containing a UUID
* \return A string containing the canonical textual representation, empty on
* error
*/
static String uuidToString(const uint8_t buffer[16]);
/**
* Identical to uuidToString(uint8_t*) but with range check
*
* \param buffer A 16 byte vector containing a UUID
* \return A string containing the canonical textual representation
*/
static String uuidToString(const std::vector<uint8_t>& buffer);
/**
* Checks if the UUID is a valid v4 UUID
*
* xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx where x is any hexadecimal digit and y
* is one of 8, 9, A, or B
*
* \param buffer A 16 byte buffer containing the UUID
* \return True if it is valid
*/
static bool isValidV4Uuid(const uint8_t* buffer);
/**
* Checks if the UUID is a valid v4 UUID
*
* xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx where x is any hexadecimal digit and y
* is one of 8, 9, A, or B
*
* \param buffer A vector containing the UUID
* \return True if it is valid
*/
static bool isValidV4Uuid(const std::vector<uint8_t>& buffer);
private:
/**
* The ESP's RNG only receives entropy when the WiFi / Bluetooth radio is on
*
* The esp_random() function will not block but act as a pseudo-RNG if there
* is not entropy source. Tests have shown that even without the radio,
* dieharder checks will confirm the generated numbers as random. This is
* problematic if generating UUIDs. Arduino's built-in random function is
* purely a pseudo-RNG and will generate the same numbers upon restart.
*
* \see
* https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/system.html#_CPPv410esp_randomv
* \see
* https://www.arduino.cc/reference/en/language/functions/random-numbers/random/
*/
static void enableRadio();
/// Counter to create unique v1 uuids
static uint16_t clock_sequence_;
};
#endif