Giter Club home page Giter Club logo

uuid's Introduction

Arduino CI Arduino-lint JSON check GitHub issues

License: MIT GitHub release PlatformIO Registry

UUID

Arduino library for generating UUID strings.

Description

This library provides a UUID generator class. A UUID is an Universally Unique IDentifier of 128 bits. These are typically written in the following format, defined in RFC-4122.

    0ac82d02-002b-4ccb-b96c-1c7839cbc4c0
                  ^    ^

The length is 32 hexadecimal digits + four hyphens = 36 characters. Note that the hexadecimal digits are all lower case.

The 0.1.1 version of the lib tries to follow the RFC-4122, for version 4 (random generated) and variant 1. In the format above the version 4 is indicated by the first arrow and must be 4. The variant 1 is at the position of the second arrow. This nibble must be 8, 9, A or B. All the remaining bits are random.

The basis for the UUID class is a Marsaglia pseudo random number generator (PRNG). Note: This PRNG must be seeded with two real random uint32_t to get real random UUID's. Regular reseeding with external entropy improves randomness. Default it will generate the same sequence as it uses the same seeds. This is useful for testing.

Often one sees also the term GUID = Globally Unique Identifier.

Tested on Arduino UNO + ESP32.

Interface

#include "UUID.h"

UUID class

The UUID class has only a few methods.

  • UUID() Constructor, initializes internals an generates a default UUID.
  • void seed(uint32_t s1, uint32_t s2 = 0) reseeds the internal pseudo random number generator. It is mandatory to set s1 while s2 is optional. The combination {0, 0} is not allowed and overruled in software.
  • void generate() generates a new UUID depending on the mode.
    • UUID_MODE_RANDOM: all UUID bits are random.
    • UUID_MODE_VARIANT4: the UUID (tries to) conform to version 4 variant 1. See above. This is the default.
  • char * toCharArray() returns a pointer to a char buffer representing the last generated UUID. Multiple subsequent calls to toCharArray() gives the same UUID until generate() is called again.

Mode

Only two modi are supported, default is the UUID_MODE_VARIANT4. This is conform RFC-4122.

  • void setVariant4Mode() set mode to UUID_MODE_VARIANT4.
  • void setRandomMode() set mode to UUID_MODE_RANDOM.
  • uint8_t getMode() returns mode set.

Printable

The UUID class implements the Printable interface. This allows one to print the UUID object directly over Serial and any other stream implementing the Print interface. Think Ethernet or SD card.

UUID uuid;

Serial.println(uuid);

//  gives same output as

Serial.println(uuid.toCharArray());

Performance

Performance measured with UUID_test.ino shows the following times:

microseconds per byte

Version Function UNO 16 MHz ESP32 240 MHz
0.1.0 seed 4 us
0.1.0 generate 412 us
0.1.0 toCharArray 4 us
0.1.1 seed 4 us
0.1.1 generate 248 us
0.1.1 toCharArray 4 us
0.1.2 generate 156 us
0.1.3 generate 120 us
- -
0.1.4 seed 4 us 3 us
0.1.4 generate 120 us 14 us
0.1.4 toCharArray 4 us 0 us
0.1.5 seed 4 us 4 us
0.1.5 generate 120 us 15 us
0.1.5 toCharArray 4 us 1 us

Note: generating the 16 random bytes already takes ~40 us (UNO).

UUID's per second

indicative maximum performance (see example sketch)

Version UNO 16 MHz ESP32 240 MHz notes
0.1.0 2000++ both modi
0.1.1 4000++ both modi
0.1.2 6400++ both modi
0.1.3 8200++ both modi
0.1.4 8268 31970 VARIANT4
0.1.4 8418 34687 RANDOM
0.1.5 8268 31969 VARIANT4
0.1.5 8418 34689 RANDOM

Note that these maxima are not realistic e.g. for a server. Other tasks need to be done too (listening, transfer etc.).

Future

Must

  • improve documentation
    • background
  • test other platforms

Should

  • optimize
    • reduce footprint
    • can the buffer be reduced?
    • buffer as static char in generate is ~2% faster on AVR (not shocking, impact ?)
    • smaller / faster random generator?

Could

  • investigate entropy harvesting
    • micros() between calls.
    • freeRAM, timers, RAM, USB-ID, ...
    • compile constants DATE and TIME
    • see example
  • auto reseed function?
    • e.g. micros() between calls.
  • GUID as derived class?
    • (further identical?)
  • add setUpperCase() and setLowerCase(), isUpperCase()
    • one bool flag
  • binary output in a byte array
    • getBinary(uint8_t * array)
    • need to store them from generate.
  • add examples
    • ESP32 UUID server => using timing of the calls as entropy !
    • RTC for entropy
    • EEPROM to store last seeds? (n)

Won't

  • support for { and }
  • add setSeparator(char) and getSeparator() ?
    • minus is the RFC specification.
  • move code to .h so compiler can optimize more?
  • next() add 1 to UUID to generate a continuous sequence ?

Support

If you appreciate my libraries, you can support the development and maintenance. Improve the quality of the libraries by providing issues and Pull Requests, or donate through PayPal or GitHub sponsors.

Thank you,

uuid's People

Contributors

robtillaart avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

uuid's Issues

Can't generate true random uuids. I get allways the same

I have tried to reseed the random generator, but I get the same sequences of UUIDs. What I´m doing wrong?
Regards
this is the result:


19:13:41.602 -> UUID_performance.ino
19:13:41.648 -> UUID_LIB_VERSION: 0.1.3
19:13:42.632 -> UUID: 08476dee-80a7-bb4c-f727-e2fe433a9177
19:13:42.678 -> 16808
19:13:42.678 -> 23710
19:13:44.640 -> UUID: 023b7641-a48d-f2cb-2540-208180101ca6
19:13:44.687 -> 28234
19:13:44.687 -> 33173
19:13:46.651 -> UUID: 0322cbbb-d80e-a415-373c-b6b0c93c4913
19:13:46.651 -> 16359
19:13:46.698 -> 4823
19:13:48.944 -> 
19:13:48.944 -> UUID_performance.ino
19:13:48.944 -> UUID_LIB_VERSION: 0.1.3
19:13:49.921 -> UUID: 08476dee-80a7-bb4c-f727-e2fe433a9177
19:13:49.968 -> 16808
19:13:49.968 -> 23710
19:13:51.933 -> UUID: 023b7641-a48d-f2cb-2540-208180101ca6
19:13:51.981 -> 28234
19:13:51.981 -> 33173
19:13:53.946 -> UUID: 0322cbbb-d80e-a415-373c-b6b0c93c4913

This is the sketch

//
//    FILE: UUID_performance.ino
//  AUTHOR: Rob Tillaart
// PURPOSE: demo

#include "Arduino.h"
#include "UUID.h"

UUID uuid;

void setup()
{
  Serial.begin(9600);
  while (!Serial);

  Serial.println();
  Serial.println("UUID_performance.ino");
  Serial.print("UUID_LIB_VERSION: ");
  Serial.println(UUID_LIB_VERSION);
}


void loop()
{
  uuid.setRandomMode();
unsigned int s1 = random(1,65535);
unsigned int s2 = random(1,65535);
    uuid.seed(s1,s2);
  delay(1000);
    
    uuid.generate();
  Serial.print("UUID: ");
  Serial.println(uuid);
  Serial.println(s1);
  Serial.println(s2);
  delay(1000);
}


// -- END OF FILE --

improve performance generate()

especially for AVR

  • check loop iterators
  • less operators if possible
  • faster random generator - how much time takes current?
  • if success improve documentation

Failing to generate Unique UIDs

Recently I am experimenting with UUID generation on Arduino framework esp32. I came across this repo and tried using it. But it seems like this library is unable to produce UUIDs and always producing the same UUID when I am using the code show below:

UUID uuid;
char *cNewUUID = (char *)malloc(37);
uuid.generate();
strcpy(cNewUUID, uuid.toCharArray());

Is there something I am particularly doing wrong or missing? Please help me out. Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.