EtcPal  HEAD (unstable)
ETC Platform Abstraction Layer (EtcPal)
View other versions:
uuid.h
1 /******************************************************************************
2  * Copyright 2022 ETC Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************
16  * This file is a part of EtcPal. For more information, go to:
17  * https://github.com/ETCLabs/EtcPal
18  ******************************************************************************/
19 
20 /* etcpal/uuid.h: A type and helper functions for a Universally Unique Identifier (UUID) */
21 
22 #ifndef ETCPAL_UUID_H_
23 #define ETCPAL_UUID_H_
24 
25 #include <stdbool.h>
26 #include <stdint.h>
27 #include <string.h>
28 #include "etcpal/error.h"
29 
85 #ifdef __cplusplus
86 extern "C" {
87 #endif
88 
90 #define ETCPAL_UUID_BYTES 16u
91 
93 typedef struct EtcPalUuid
94 {
97 
106 #define ETCPAL_UUID_CMP(uuid1ptr, uuid2ptr) memcmp((uuid1ptr)->data, (uuid2ptr)->data, ETCPAL_UUID_BYTES)
107 
109 extern const EtcPalUuid kEtcPalNullUuid;
110 
119 #define ETCPAL_UUID_IS_NULL(uuidptr) (memcmp((uuidptr)->data, kEtcPalNullUuid.data, ETCPAL_UUID_BYTES) == 0)
120 
122 #define ETCPAL_UUID_STRING_BYTES 37
123 
125 #define ETCPAL_UUID_DEV_STR_MAX_LEN 32
126 
127 bool etcpal_uuid_to_string(const EtcPalUuid* uuid, char* buf);
128 bool etcpal_string_to_uuid(const char* str, EtcPalUuid* uuid);
129 
130 /************************ UUID Generation Functions **************************/
131 
133 etcpal_error_t etcpal_generate_v3_uuid(const EtcPalUuid* ns, const void* name, size_t name_len, EtcPalUuid* uuid);
135 etcpal_error_t etcpal_generate_v5_uuid(const EtcPalUuid* ns, const void* name, size_t name_len, EtcPalUuid* uuid);
137 etcpal_error_t etcpal_generate_device_uuid(const char* dev_str,
138  const uint8_t* mac_addr,
139  uint32_t uuid_num,
140  EtcPalUuid* uuid);
141 
142 #ifdef __cplusplus
143 }
144 #endif
145 
146 #ifdef __cplusplus
147 
148 /* C++ utilities */
149 
150 /* Comparison operators for UUIDs */
151 
152 inline bool operator<(const EtcPalUuid& a, const EtcPalUuid& b)
153 {
154  return (ETCPAL_UUID_CMP(&a, &b) < 0);
155 }
156 
157 inline bool operator==(const EtcPalUuid& a, const EtcPalUuid& b)
158 {
159  return (ETCPAL_UUID_CMP(&a, &b) == 0);
160 }
161 
162 #endif
163 
168 #endif /* ETCPAL_UUID_H_ */
etcpal_error_t
A set of error codes that can be returned by library functions.
Definition: error.h:49
etcpal_error_t etcpal_generate_device_uuid(const char *dev_str, const uint8_t *mac_addr, uint32_t uuid_num, EtcPalUuid *uuid)
Generate a UUID from a combination of a custom string and MAC address.
Definition: uuid.c:309
etcpal_error_t etcpal_generate_v4_uuid(EtcPalUuid *uuid)
Generate a Version 4 UUID.
Definition: uuid.c:216
struct EtcPalUuid EtcPalUuid
The UUID type.
bool etcpal_uuid_to_string(const EtcPalUuid *uuid, char *buf)
Create a string representation of a UUID.
Definition: uuid.c:61
#define ETCPAL_UUID_CMP(uuid1ptr, uuid2ptr)
Compare two UUIDs.
Definition: uuid.h:106
etcpal_error_t etcpal_generate_v1_uuid(EtcPalUuid *uuid)
Generate a Version 1 UUID.
Definition: uuid.c:152
bool etcpal_string_to_uuid(const char *str, EtcPalUuid *uuid)
Create a UUID from a string representation.
Definition: uuid.c:85
etcpal_error_t etcpal_generate_os_preferred_uuid(EtcPalUuid *uuid)
Generate the preferred UUID version of the underlying OS.
Definition: uuid.c:282
etcpal_error_t etcpal_generate_v5_uuid(const EtcPalUuid *ns, const void *name, size_t name_len, EtcPalUuid *uuid)
Generate a Version 5 UUID.
Definition: uuid.c:243
etcpal_error_t etcpal_generate_v3_uuid(const EtcPalUuid *ns, const void *name, size_t name_len, EtcPalUuid *uuid)
Generate a Version 3 UUID.
Definition: uuid.c:178
#define ETCPAL_UUID_BYTES
The number of bytes that make up a UUID.
Definition: uuid.h:90
const EtcPalUuid kEtcPalNullUuid
A null (all 0's) UUID, used by ETCPAL_UUID_IS_NULL() for comparison.
Definition: uuid.c:46
The UUID type.
Definition: uuid.h:94
uint8_t data[ETCPAL_UUID_BYTES]
The 16-byte UUID data.
Definition: uuid.h:95