uuid: Add sample for UUID utilities
Add a sample that showcases the usqge of the UUID utilities. Signed-off-by: Simone Orru <simone.orru@secomind.com>
This commit is contained in:
parent
c82bf03e6d
commit
36ec017a38
6 changed files with 151 additions and 1 deletions
9
samples/subsys/uuid/CMakeLists.txt
Normal file
9
samples/subsys/uuid/CMakeLists.txt
Normal file
|
@ -0,0 +1,9 @@
|
|||
# Copyright (c) 2025, SECO Mind Srl
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
cmake_minimum_required(VERSION 3.20.0)
|
||||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||
project(dap)
|
||||
|
||||
FILE(GLOB app_sources src/*.c)
|
||||
target_sources(app PRIVATE ${app_sources})
|
36
samples/subsys/uuid/README.rst
Normal file
36
samples/subsys/uuid/README.rst
Normal file
|
@ -0,0 +1,36 @@
|
|||
.. zephyr:code-sample:: uuid
|
||||
:name: UUID
|
||||
|
||||
Manipulate UUID v4 and v5 compliant with IETF RFC 9562.
|
||||
|
||||
Overview
|
||||
********
|
||||
|
||||
This sample app demonstrates the use of the UUID utilities to generate and manipulate UUIDs
|
||||
accordingly to IETF RFC 9562.
|
||||
|
||||
The following functionality is demonstrated:
|
||||
- UUIDv4 generation
|
||||
- UUIDv5 generation from namespace and data
|
||||
- UUID conversion from/to string and to base64 and base64 URL safe formats
|
||||
|
||||
Requirements
|
||||
************
|
||||
|
||||
This sample relies on the following modules:
|
||||
- MbedTLS for the UUIDv5 hash functions
|
||||
- Base64 for the base64 encoding of UUIDs
|
||||
- Entropy source for the pseudo-random generation of UUIDv4
|
||||
|
||||
Building and Running
|
||||
********************
|
||||
|
||||
Use the standard ``west`` commands to build and flash this application.
|
||||
For example for ``native_sim`` build with:
|
||||
```
|
||||
west build -p -b native_sim samples/subsys/uuid
|
||||
```
|
||||
Then run with:
|
||||
```
|
||||
west build -t run
|
||||
```
|
13
samples/subsys/uuid/prj.conf
Normal file
13
samples/subsys/uuid/prj.conf
Normal file
|
@ -0,0 +1,13 @@
|
|||
CONFIG_UUID=y
|
||||
CONFIG_UUID_V5=y
|
||||
CONFIG_UUID_V4=y
|
||||
CONFIG_UUID_BASE64=y
|
||||
|
||||
CONFIG_ENTROPY_GENERATOR=y
|
||||
|
||||
CONFIG_MBEDTLS=y
|
||||
CONFIG_MBEDTLS_MD=y
|
||||
CONFIG_MBEDTLS_SHA1=y
|
||||
CONFIG_BASE64=y
|
||||
|
||||
CONFIG_LOG=y
|
17
samples/subsys/uuid/sample.yaml
Normal file
17
samples/subsys/uuid/sample.yaml
Normal file
|
@ -0,0 +1,17 @@
|
|||
sample:
|
||||
name: UUID
|
||||
tests:
|
||||
sample.subsys.uuid:
|
||||
tags: uuid
|
||||
platform_allow:
|
||||
- qemu_x86
|
||||
- native_sim
|
||||
- native_sim/native/64
|
||||
integration_platforms:
|
||||
- native_sim
|
||||
filter: CONFIG_ENTROPY_HAS_DRIVER
|
||||
harness: console
|
||||
harness_config:
|
||||
type: one_line
|
||||
regex:
|
||||
- "UUID sample completed successfully"
|
75
samples/subsys/uuid/src/main.c
Normal file
75
samples/subsys/uuid/src/main.c
Normal file
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
* Copyright (c) 2025, SECO Mind Srl
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/sys/uuid.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
LOG_MODULE_REGISTER(uuid_sample, LOG_LEVEL_INF);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int result;
|
||||
struct uuid uuid_v4 = {0};
|
||||
struct uuid uuid_v5_namespace = {0};
|
||||
struct uuid uuid_v5 = {0};
|
||||
char uuid_v4_str[UUID_STR_LEN] = {0};
|
||||
char uuid_v5_str[UUID_STR_LEN] = {0};
|
||||
char uuid_v4_base64[UUID_BASE64_LEN] = {0};
|
||||
char uuid_v4_base64url[UUID_BASE64URL_LEN] = {0};
|
||||
|
||||
/* Generate an UUID v4 from pseudo-random data */
|
||||
result = uuid_generate_v4(&uuid_v4);
|
||||
if (result != 0) {
|
||||
LOG_ERR("UUID v4 generation failed, error: %s (%d)", strerror(result), result);
|
||||
return -1;
|
||||
}
|
||||
/* Convert the UUID to string and to its base 64 and base 64 URL safe formats */
|
||||
result = uuid_to_string(&uuid_v4, uuid_v4_str);
|
||||
if (result != 0) {
|
||||
LOG_ERR("UUID v4 to string failed, error: %s (%d)", strerror(result), result);
|
||||
return -1;
|
||||
}
|
||||
LOG_INF("UUID v4: '%s'", uuid_v4_str);
|
||||
result = uuid_to_base64(&uuid_v4, uuid_v4_base64);
|
||||
if (result != 0) {
|
||||
LOG_ERR("UUID v4 to base 64 failed, error: %s (%d)", strerror(result), result);
|
||||
return -1;
|
||||
}
|
||||
LOG_INF("UUID v4 base 64: '%s'", uuid_v4_base64);
|
||||
result = uuid_to_base64url(&uuid_v4, uuid_v4_base64url);
|
||||
if (result != 0) {
|
||||
LOG_ERR("UUID v4 to base 64 URL safe failed, error: %s (%d)", strerror(result),
|
||||
result);
|
||||
return -1;
|
||||
}
|
||||
LOG_INF("UUID v4 base 64 URL safe: '%s'", uuid_v4_base64url);
|
||||
|
||||
/* Generate an UUID v5 */
|
||||
/* This UUID is the same as in RFC 9562 Appendix A.4: "Example of a UUIDv5 Value" */
|
||||
result = uuid_from_string("6ba7b810-9dad-11d1-80b4-00c04fd430c8", &uuid_v5_namespace);
|
||||
if (result != 0) {
|
||||
LOG_ERR("Namespace string to UUID failed, error: %s (%d)", strerror(result),
|
||||
result);
|
||||
return -1;
|
||||
}
|
||||
result = uuid_generate_v5(&uuid_v5_namespace, "www.example.com", strlen("www.example.com"),
|
||||
&uuid_v5);
|
||||
if (result != 0) {
|
||||
LOG_ERR("UUID v5 generation failed, error: %s (%d)", strerror(result), result);
|
||||
return -1;
|
||||
}
|
||||
result = uuid_to_string(&uuid_v5, uuid_v5_str);
|
||||
if (result != 0) {
|
||||
LOG_ERR("UUID v4 to string failed, error: %s (%d)", strerror(result), result);
|
||||
return -1;
|
||||
}
|
||||
LOG_INF("UUID v5: '%s'", uuid_v5_str);
|
||||
|
||||
LOG_INF("UUID sample completed successfully");
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2024, SECO Mind Srl
|
||||
* Copyright (c) 2025, SECO Mind Srl
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue