Skip to content

Commit 36ec017

Browse files
sorru94kartben
authored andcommitted
uuid: Add sample for UUID utilities
Add a sample that showcases the usqge of the UUID utilities. Signed-off-by: Simone Orru <[email protected]>
1 parent c82bf03 commit 36ec017

File tree

6 files changed

+151
-1
lines changed

6 files changed

+151
-1
lines changed

samples/subsys/uuid/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) 2025, SECO Mind Srl
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(dap)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})

samples/subsys/uuid/README.rst

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
.. zephyr:code-sample:: uuid
2+
:name: UUID
3+
4+
Manipulate UUID v4 and v5 compliant with IETF RFC 9562.
5+
6+
Overview
7+
********
8+
9+
This sample app demonstrates the use of the UUID utilities to generate and manipulate UUIDs
10+
accordingly to IETF RFC 9562.
11+
12+
The following functionality is demonstrated:
13+
- UUIDv4 generation
14+
- UUIDv5 generation from namespace and data
15+
- UUID conversion from/to string and to base64 and base64 URL safe formats
16+
17+
Requirements
18+
************
19+
20+
This sample relies on the following modules:
21+
- MbedTLS for the UUIDv5 hash functions
22+
- Base64 for the base64 encoding of UUIDs
23+
- Entropy source for the pseudo-random generation of UUIDv4
24+
25+
Building and Running
26+
********************
27+
28+
Use the standard ``west`` commands to build and flash this application.
29+
For example for ``native_sim`` build with:
30+
```
31+
west build -p -b native_sim samples/subsys/uuid
32+
```
33+
Then run with:
34+
```
35+
west build -t run
36+
```

samples/subsys/uuid/prj.conf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CONFIG_UUID=y
2+
CONFIG_UUID_V5=y
3+
CONFIG_UUID_V4=y
4+
CONFIG_UUID_BASE64=y
5+
6+
CONFIG_ENTROPY_GENERATOR=y
7+
8+
CONFIG_MBEDTLS=y
9+
CONFIG_MBEDTLS_MD=y
10+
CONFIG_MBEDTLS_SHA1=y
11+
CONFIG_BASE64=y
12+
13+
CONFIG_LOG=y

samples/subsys/uuid/sample.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
sample:
2+
name: UUID
3+
tests:
4+
sample.subsys.uuid:
5+
tags: uuid
6+
platform_allow:
7+
- qemu_x86
8+
- native_sim
9+
- native_sim/native/64
10+
integration_platforms:
11+
- native_sim
12+
filter: CONFIG_ENTROPY_HAS_DRIVER
13+
harness: console
14+
harness_config:
15+
type: one_line
16+
regex:
17+
- "UUID sample completed successfully"

samples/subsys/uuid/src/main.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2025, SECO Mind Srl
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/sys/uuid.h>
9+
10+
#include <zephyr/logging/log.h>
11+
LOG_MODULE_REGISTER(uuid_sample, LOG_LEVEL_INF);
12+
13+
int main(void)
14+
{
15+
int result;
16+
struct uuid uuid_v4 = {0};
17+
struct uuid uuid_v5_namespace = {0};
18+
struct uuid uuid_v5 = {0};
19+
char uuid_v4_str[UUID_STR_LEN] = {0};
20+
char uuid_v5_str[UUID_STR_LEN] = {0};
21+
char uuid_v4_base64[UUID_BASE64_LEN] = {0};
22+
char uuid_v4_base64url[UUID_BASE64URL_LEN] = {0};
23+
24+
/* Generate an UUID v4 from pseudo-random data */
25+
result = uuid_generate_v4(&uuid_v4);
26+
if (result != 0) {
27+
LOG_ERR("UUID v4 generation failed, error: %s (%d)", strerror(result), result);
28+
return -1;
29+
}
30+
/* Convert the UUID to string and to its base 64 and base 64 URL safe formats */
31+
result = uuid_to_string(&uuid_v4, uuid_v4_str);
32+
if (result != 0) {
33+
LOG_ERR("UUID v4 to string failed, error: %s (%d)", strerror(result), result);
34+
return -1;
35+
}
36+
LOG_INF("UUID v4: '%s'", uuid_v4_str);
37+
result = uuid_to_base64(&uuid_v4, uuid_v4_base64);
38+
if (result != 0) {
39+
LOG_ERR("UUID v4 to base 64 failed, error: %s (%d)", strerror(result), result);
40+
return -1;
41+
}
42+
LOG_INF("UUID v4 base 64: '%s'", uuid_v4_base64);
43+
result = uuid_to_base64url(&uuid_v4, uuid_v4_base64url);
44+
if (result != 0) {
45+
LOG_ERR("UUID v4 to base 64 URL safe failed, error: %s (%d)", strerror(result),
46+
result);
47+
return -1;
48+
}
49+
LOG_INF("UUID v4 base 64 URL safe: '%s'", uuid_v4_base64url);
50+
51+
/* Generate an UUID v5 */
52+
/* This UUID is the same as in RFC 9562 Appendix A.4: "Example of a UUIDv5 Value" */
53+
result = uuid_from_string("6ba7b810-9dad-11d1-80b4-00c04fd430c8", &uuid_v5_namespace);
54+
if (result != 0) {
55+
LOG_ERR("Namespace string to UUID failed, error: %s (%d)", strerror(result),
56+
result);
57+
return -1;
58+
}
59+
result = uuid_generate_v5(&uuid_v5_namespace, "www.example.com", strlen("www.example.com"),
60+
&uuid_v5);
61+
if (result != 0) {
62+
LOG_ERR("UUID v5 generation failed, error: %s (%d)", strerror(result), result);
63+
return -1;
64+
}
65+
result = uuid_to_string(&uuid_v5, uuid_v5_str);
66+
if (result != 0) {
67+
LOG_ERR("UUID v4 to string failed, error: %s (%d)", strerror(result), result);
68+
return -1;
69+
}
70+
LOG_INF("UUID v5: '%s'", uuid_v5_str);
71+
72+
LOG_INF("UUID sample completed successfully");
73+
74+
return 0;
75+
}

tests/lib/uuid/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, SECO Mind Srl
2+
* Copyright (c) 2025, SECO Mind Srl
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/

0 commit comments

Comments
 (0)