forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUuidUtils.cpp
More file actions
56 lines (45 loc) · 1.35 KB
/
UuidUtils.cpp
File metadata and controls
56 lines (45 loc) · 1.35 KB
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
#include <MicroOcpp/Core/UuidUtils.h>
#include <MicroOcpp/Platform.h>
#include <stdint.h>
namespace MicroOcpp {
#define UUID_STR_LEN 36
bool generateUUID(char *uuidBuffer, size_t len) {
if (len < UUID_STR_LEN + 1)
{
return false;
}
uint32_t ar[4];
for (uint8_t i = 0; i < 4; i++) {
ar[i] = mocpp_rng();
}
// Conforming to RFC 4122 Specification
// - byte 7: four most significant bits ==> 0100 --> always 4
// - byte 9: two most significant bits ==> 10 --> always {8, 9, A, B}.
//
// patch bits for version 1 and variant 4 here
ar[1] &= 0xFFF0FFFF; // remove 4 bits.
ar[1] |= 0x00040000; // variant 4
ar[2] &= 0xFFFFFFF3; // remove 2 bits
ar[2] |= 0x00000008; // version 1
// loop through the random 16 byte array
for (uint8_t i = 0, j = 0; i < 16; i++) {
// multiples of 4 between 8 and 20 get a -.
// note we are processing 2 digits in one loop.
if ((i & 0x1) == 0) {
if ((4 <= i) && (i <= 10)) {
uuidBuffer[j++] = '-';
}
}
// encode the byte as two hex characters
uint8_t nr = i / 4;
uint8_t xx = ar[nr];
uint8_t ch = xx & 0x0F;
uuidBuffer[j++] = (ch < 10)? '0' + ch : ('a' - 10) + ch;
ch = (xx >> 4) & 0x0F;
ar[nr] >>= 8;
uuidBuffer[j++] = (ch < 10)? '0' + ch : ('a' - 10) + ch;
}
uuidBuffer[UUID_STR_LEN] = 0;
return true;
}
}