2022-06-14 09:20:04 -04:00
|
|
|
//
|
|
|
|
// FILE: UUID.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2022-11-27 03:39:34 -05:00
|
|
|
// VERSION: 0.1.4
|
2022-06-14 09:20:04 -04:00
|
|
|
// DATE: 2022-06-14
|
|
|
|
// PURPOSE: Arduino Library for generating UUID's
|
|
|
|
// URL: https://github.com/RobTillaart/UUID
|
2022-06-15 01:34:09 -04:00
|
|
|
// https://en.wikipedia.org/wiki/Universally_unique_identifier
|
2022-06-14 09:20:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
#include "UUID.h"
|
|
|
|
|
|
|
|
|
|
|
|
UUID::UUID()
|
|
|
|
{
|
|
|
|
seed(1, 2);
|
2022-06-15 01:34:09 -04:00
|
|
|
setVariant4Mode();
|
2022-06-14 09:20:04 -04:00
|
|
|
generate();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UUID::seed(uint32_t s1, uint32_t s2)
|
|
|
|
{
|
2022-11-27 03:39:34 -05:00
|
|
|
// set Marsaglia constants, prevent 0 as value
|
2022-06-14 09:20:04 -04:00
|
|
|
if (s1 == 0) s1 = 1;
|
|
|
|
if (s2 == 0) s2 = 2;
|
|
|
|
_m_w = s1;
|
|
|
|
_m_z = s2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-15 04:31:57 -04:00
|
|
|
// check version 0.1.1 for more readable code
|
2022-06-14 09:20:04 -04:00
|
|
|
void UUID::generate()
|
|
|
|
{
|
2022-11-27 03:39:34 -05:00
|
|
|
uint32_t ar[4];
|
2022-06-14 09:20:04 -04:00
|
|
|
for (uint8_t i = 0; i < 4; i++)
|
|
|
|
{
|
2022-11-27 03:39:34 -05:00
|
|
|
ar[i] = _random();
|
|
|
|
// store binary version globally ?
|
|
|
|
// _ar[i] = ar[i];
|
2022-06-14 09:20:04 -04:00
|
|
|
}
|
2022-11-27 03:39:34 -05:00
|
|
|
|
|
|
|
// 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}.
|
|
|
|
//
|
2022-06-15 01:34:09 -04:00
|
|
|
// patch bits for version 1 and variant 4 here
|
|
|
|
if (_mode == UUID_MODE_VARIANT4)
|
|
|
|
{
|
2022-11-27 03:39:34 -05:00
|
|
|
ar[1] &= 0xFFF0FFFF; // remove 4 bits.
|
|
|
|
ar[1] |= 0x00040000; // variant 4
|
|
|
|
ar[2] &= 0xFFFFFFF3; // remove 2 bits
|
|
|
|
ar[2] |= 0x00000008; // version 1
|
2022-06-15 01:34:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-15 04:31:57 -04:00
|
|
|
// process 16 bytes build up the char array.
|
|
|
|
for (uint8_t i = 0, j = 0; i < 16; i++)
|
2022-06-14 09:20:04 -04:00
|
|
|
{
|
2022-06-15 04:31:57 -04:00
|
|
|
// multiples of 4 between 8 and 20 get a -.
|
|
|
|
// but note we are doing 2 digits in one loop.
|
|
|
|
if ((i & 0x1) == 0)
|
|
|
|
{
|
|
|
|
if ((4 <= i) && (i <= 10)) _buffer[j++] = '-';
|
|
|
|
}
|
|
|
|
|
|
|
|
// process one byte at the time instead of a nibble
|
|
|
|
uint8_t nr = i / 4;
|
2022-11-27 03:39:34 -05:00
|
|
|
uint8_t xx = ar[nr];
|
|
|
|
uint8_t ch = xx & 0x0F;
|
2022-06-16 03:37:59 -04:00
|
|
|
_buffer[j++] = (ch < 10) ? '0' + ch : ('a' - 10) + ch;
|
2022-11-27 03:39:34 -05:00
|
|
|
|
|
|
|
ch = (xx >> 4) & 0x0F;
|
|
|
|
ar[nr] >>= 8;
|
2022-06-16 03:37:59 -04:00
|
|
|
_buffer[j++] = (ch < 10) ? '0' + ch : ('a' - 10) + ch;
|
2022-06-14 09:20:04 -04:00
|
|
|
}
|
|
|
|
_buffer[36] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char * UUID::toCharArray()
|
|
|
|
{
|
|
|
|
return _buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-27 03:39:34 -05:00
|
|
|
//////////////////////////////////////////////////
|
|
|
|
//
|
2022-06-14 09:20:04 -04:00
|
|
|
// PRINTING
|
2022-11-27 03:39:34 -05:00
|
|
|
//
|
2022-06-14 09:20:04 -04:00
|
|
|
size_t UUID::printTo(Print& p) const
|
|
|
|
{
|
|
|
|
return p.print(_buffer);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-11-27 03:39:34 -05:00
|
|
|
//////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// RANDOM GENERATOR MARSAGLIA
|
|
|
|
//
|
|
|
|
// An example of a simple pseudo-random number generator is the
|
|
|
|
// Multiply-with-carry method invented by George Marsaglia.
|
|
|
|
// two initializers (not null)
|
2022-06-14 09:20:04 -04:00
|
|
|
uint32_t UUID::_random()
|
|
|
|
{
|
|
|
|
_m_z = 36969L * (_m_z & 65535L) + (_m_z >> 16);
|
|
|
|
_m_w = 18000L * (_m_w & 65535L) + (_m_w >> 16);
|
|
|
|
return (_m_z << 16) + _m_w; /* 32-bit result */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -- END OF FILE --
|
|
|
|
|