Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions components/utilities/esp_uuid/include/uuid.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ extern "C" {
typedef enum uuid_modes_e {
UUID_MODE_VARIANT4 = 0, /*!< Variant-4 UUID */
UUID_MODE_RANDOM = 1, /*!< Random UUID */
UUID_MODE_VARIANT7 = 2, /*!< Variant-7 UUID */
} uuid_modes_t;

/**
Expand Down
27 changes: 23 additions & 4 deletions components/utilities/esp_uuid/uuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
#include <stdio.h>
#include <stdarg.h>
#include <esp_timer.h>

#include <sys/time.h>

/* constant definitions */
#define UUID_RANDOM_SIZE 4
#define UUID_BUFFER_SIZE 37
Expand Down Expand Up @@ -117,10 +118,28 @@

// Apply RFC-4122 version and variant bits
if (uuid_mode == UUID_MODE_VARIANT4) {
ar[1] = (ar[1] & 0xFFF0FFFF) | 0x00040000; // Version 4
ar[2] = (ar[2] & 0x3FFFFFFF) | 0x80000000; // Variant 10xx
ar[1] = (ar[1] & 0xFF0FFFFF) | 0x00400000; // Version 4
ar[2] = (ar[2] & 0xFFFFFF3F) | 0x00000080; // Variant 10xx
}


// Apply Unix timestamp on 48-bits, RFC-9562 version and variant bits
if (uuid_mode == UUID_MODE_VARIANT7){
/* Get timestamp UNIX on 48 bits */
struct timeval tv;
gettimeofday(&tv, NULL);
uint64_t unix_ts_ms = (uint64_t) (tv.tv_sec * 1000ULL) + (tv.tv_usec / 1000ULL);
/* Chunk of timestamp for manipulation */
char t1 = unix_ts_ms >> 40;
char t2 = unix_ts_ms >> 32;
char t3 = unix_ts_ms >> 24;
char t4 = unix_ts_ms >> 16;
char t5 = unix_ts_ms >> 8;
char t6 = unix_ts_ms;
ar[0] = (ar[0] & 0x00000000) | ((t1)) | (t2) << 8 | (t3) << 16 | (t4) << 24;

Check warning on line 138 in components/utilities/esp_uuid/uuid.c

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

components/utilities/esp_uuid/uuid.c#L138

Operator '|' with one operand equal to zero is redundant.
ar[1] = (ar[1] & 0xFF0F0000) | (0x00700000 | ((t5)) | (t6) << 8); // Version 7
ar[2] = (ar[2] & 0xFFFFFF3F) | 0x00000080; // Variant 10xx
}

// Build the UUID string
int j = 0;
for (int i = 0; i < 16; i++) {
Expand Down