REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
uuid.cpp
Go to the documentation of this file.
1#include "utils/uuid.h"
2
3// TODO: Should unit test whether this is correct
4FLASHMEM
5void utils::UUID::toCharArray(char *_buffer) const {
6 // process 16 bytes build up the char array.
7 for (uint8_t i = 0, j = 0; i < 16; i++) {
8 // multiples of 4 between 8 and 20 get a -.
9 // note we are processing 2 digits in one loop.
10 if ((i & 0x1) == 0) {
11 if ((4 <= i) && (i <= 10)) {
12 _buffer[j++] = '-';
13 }
14 }
15
17 // uint8_t nr = i / 4;
18 uint8_t xx = ar[i];
19 uint8_t ch = xx & 0x0F;
20 _buffer[j++] = (ch < 10) ? '0' + ch : ('a' - 10) + ch;
21
22 ch = (xx >> 4) & 0x0F;
23 // ar[nr] >>= 8;
24 _buffer[j++] = (ch < 10) ? '0' + ch : ('a' - 10) + ch;
25 }
26
27 // if (_upperCase)
28 // {
29 // for (int i = 0; i < 37; i++)
30 // {
31 // _buffer[i] = toUpper(_buffer[i]);
32 // }
33 // }
34 _buffer[36] = 0;
35}
36
37FLASHMEM
38String utils::UUID::toString() const {
39 char _buffer[37];
40 toCharArray(_buffer);
41 return String(_buffer);
42}
43
44FLASHMEM
45size_t utils::UUID::printTo(Print &p) const {
46 // UUID in string format
47 char _buffer[37];
48 toCharArray(_buffer);
49 return p.print(_buffer);
50}
51
52FLASHMEM
53uint8_t chartoi(char X) {
54 if ('0' <= X && X <= '9')
55 return X - '0';
56 if ('a' <= X && X <= 'f')
57 return X - 'a' + 10;
58 if ('A' <= X && X <= 'F')
59 return X - 'A' + 10;
60 return 0;
61}
62
63// TODO: Should unit test whether this is correct
64FLASHMEM
65utils::UUID utils::UUID::fromString(const char *str) {
66 UUID data; // all zero by default
67
68 if (strlen(str) != 36)
69 return data;
70
71 // implementation follows
72 // https://github.com/mariusbancila/stduuid/blob/3afe7193facd5d674de709fccc44d5055e144d7a/include/uuid.h#L503
73 bool firstDigit = true;
74 size_t index = 0;
75 for (size_t i = 0; i < 36; i++) {
76 if (str[i] == '-')
77 continue;
78 if (firstDigit) {
79 data.ar[index] = chartoi(str[i]) << 4;
80 firstDigit = false;
81 } else {
82 data.ar[index] |= chartoi(str[i]);
83 index++;
84 firstDigit = true;
85 }
86 }
87 return data;
88}
uint32_t ch(uint32_t x, uint32_t y, uint32_t z)
Definition dcp.cpp:25
FLASHMEM uint8_t chartoi(char X)
Definition uuid.cpp:53
FLASHMEM void index(awot::Request &req, awot::Response &res)
Definition server.cpp:120