SensESP 3.3.0
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
uuid.cpp
Go to the documentation of this file.
1#include "uuid.h"
2
3namespace sensesp {
4
5String generate_uuid4() {
6 // implementation copied from https://github.com/protohaus/ESP_UUID
7 // to avoid additional external dependencies.
8 uint8_t buffer_[16];
9 String uuid_str;
10
11 // Generate 16 random bytes. On ESP32, these are actually random if the
12 // radio is enabled.
13 for (int i = 0; i < 16; i += 4) {
14 uint32_t random = esp_random();
15 memcpy(&buffer_[i], &random, 4);
16 }
17
18 buffer_[6] = (buffer_[6] & 0x0F) | 0x40; // version 4
19 buffer_[8] = (buffer_[8] & 0x3F) | 0x80; // variant 1
20
21 uuid_str.reserve(36 + 1); // Include NULL / terminator byte
22
23 for (int i = 0; i < 16; i++) {
24 if (i == 4 || i == 6 || i == 8 || i == 10) {
25 uuid_str += "-";
26 }
27 uuid_str += String(buffer_[i] >> 4, HEX);
28 uuid_str += String(buffer_[i] & 0x0F, HEX);
29 }
30 return uuid_str;
31}
32
33} // namespace sensesp
String generate_uuid4()
Generate a random UUIDv4 string.
Definition uuid.cpp:5