Use

The UUID data type is used to model any kind of Identification number (ID). IDs are often key members in a struct.

Description

UUID consists of two 64 bit integers called "upper" and "lower".

QDM

The QDM description of UUID is shown in the QDM snippet below:

(...)
    <struct name="UUID">       
       <doc>A generic 128-bit UUID.</doc>
       <member name="upper" type="int64">
           <doc>Upper 64-bit of identifier.</doc>
       </member>
       <member name="lower" type="int64">
           <doc>Lower 64-bit of identifier.</doc>
       </member>
    </struct>
(...)

Example of Use: Generating a UUID on Android

The code snippet below shows how to generate a random UUID for Qeo:

java.util.UUID jid = java.util.UUID.randomUUID();
org.qeo.UUID qid = new org.qeo.UUID();
qid.upper = jid.getMostSignificantBits();
qid.lower = jid.getLeastSignificantBits();

Example of Use: Generating a UUID  on Unix/Linux in C

The code snippet below shows how to generate a random UUID for Qeo:

/*
* To get random character value of UUID string from Unix/Linux OS 
* - input: UUID generator path e.g. /proc/sys/kernel/random/uuid
* - output: a pointer to allocated memory with the UUID string, 
* an empty string if method is failed
* NOTE: memory has to be released later on
*/
char* get_uuid_string_from_sys(const char* path)
* get_uuid_string_from_sys(const char* path){
FILE* file = NULL;
char temp[128]; /* e9cabebc-923b-4932-a534-9578a6904bf8 */
char* result = NULL;
if (path == NULL){
/* error – invalid input argument */
return strdup("");
}
file = fopen(path, "rb");
if (file == NULL ) {
/* error – file is not opened */
return strdup("");
}
if (fgets(temp, sizeof(temp), file) == NULL){
fclose(file);
/* error - Could not get UUID */
return strdup("");
}
fclose(file);
result = strdup(temp);
if (result == NULL ) {
/* error - memory allocation call is failed */;
return strdup("");
}
return result;
}
/*
* To get qeo UUID structure from Unix/Linux OS based UUID string
* - input: UUID string, format is "e9cabebc-923b-4932-a534-9578a6904bf8"
* - output: qeo UUID structure 
*/
org_qeo_UUID_t uuid_from_str(char* string)
{
char uuid_parsed[34];
char upperId[17];
char lowerId[17];
uint64_t upper = 0;
uint64_t lower = 0;
org_qeo_UUID_t qeoUUID;
if (string == NULL ) {
/* error - Invalid input argument */
qeoUUID.upper = upper;
qeoUUID.lower = lower;
return qeoUUID;
}
/* Remove all '-' characters */
char *src = string;
char *dst = uuid_parsed;
while ((*dst++ = *src++)) {
if (*src == '-') {
src++;
}
}
/* Split in 2 and convert from HEX to uint64_t */
snprintf(upperId, sizeof(upperId), "%s", uuid_parsed);
snprintf(lowerId, sizeof(lowerId), "%s", uuid_parsed + 16);
upper = strtoull(upperId, NULL, 16);
lower = strtoull(lowerId, NULL, 16);
qeoUUID.upper = upper;
qeoUUID.lower = lower;
return qeoUUID;
}
/*
* To get a random UUID qeo structure from Unix/Linux based system
* - input: a pointer to qeo structure to be filled in
* - output: 0 a qeo UUID
* -1 function is failed 
*/
int get_uuid_from_sys(org_qeo_UUID_t * pUUID)
{
char* result_uuid = NULL;
if(!pUUID){
/* error - Invalid input argument ! */
return -1;
}
result_uuid = get_uuid_string_from_sys("/proc/sys/kernel/random/uuid");
if (!result_uuid) {
/* error - uuid_string_from_sys call is failed ! */
return -1;
} else if (strcmp(result_uuid,"") == 0){
free(result_uuid);
/* error - get_uuid_string_from_sys is failed ! */
return -1;
}
*pUUID = uuid_from_str(result_uuid);
free(result_uuid);
return 0; 
}