EtcPal  0.4.1
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 
84 #ifdef __cplusplus
85 extern "C" {
86 #endif
87 
89 #define ETCPAL_UUID_BYTES 16u
90 
92 typedef struct EtcPalUuid
93 {
96 
105 #define ETCPAL_UUID_CMP(uuid1ptr, uuid2ptr) memcmp((uuid1ptr)->data, (uuid2ptr)->data, ETCPAL_UUID_BYTES)
106 
108 extern const EtcPalUuid kEtcPalNullUuid;
109 
118 #define ETCPAL_UUID_IS_NULL(uuidptr) (memcmp((uuidptr)->data, kEtcPalNullUuid.data, ETCPAL_UUID_BYTES) == 0)
119 
121 #define ETCPAL_UUID_STRING_BYTES 37
122 
124 #define ETCPAL_UUID_DEV_STR_MAX_LEN 32
125 
126 bool etcpal_uuid_to_string(const EtcPalUuid* uuid, char* buf);
127 bool etcpal_string_to_uuid(const char* str, EtcPalUuid* uuid);
128 
129 /************************ UUID Generation Functions **************************/
130 
132 etcpal_error_t etcpal_generate_v3_uuid(const EtcPalUuid* ns, const void* name, size_t name_len, EtcPalUuid* uuid);
134 etcpal_error_t etcpal_generate_v5_uuid(const EtcPalUuid* ns, const void* name, size_t name_len, EtcPalUuid* uuid);
136 etcpal_error_t etcpal_generate_device_uuid(const char* dev_str,
137  const uint8_t* mac_addr,
138  uint32_t uuid_num,
139  EtcPalUuid* uuid);
140 
141 #ifdef __cplusplus
142 }
143 #endif
144 
145 #ifdef __cplusplus
146 
147 /* C++ utilities */
148 
149 /* Comparison operators for UUIDs */
150 
151 inline bool operator<(const EtcPalUuid& a, const EtcPalUuid& b)
152 {
153  return (ETCPAL_UUID_CMP(&a, &b) < 0);
154 }
155 
156 inline bool operator==(const EtcPalUuid& a, const EtcPalUuid& b)
157 {
158  return (ETCPAL_UUID_CMP(&a, &b) == 0);
159 }
160 
161 #endif
162 
167 #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:105
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:89
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:93
uint8_t data[ETCPAL_UUID_BYTES]
The 16-byte UUID data.
Definition: uuid.h:94