diff --git a/samples/subsys/uuid/CMakeLists.txt b/samples/subsys/uuid/CMakeLists.txt new file mode 100644 index 00000000000..98b06b94203 --- /dev/null +++ b/samples/subsys/uuid/CMakeLists.txt @@ -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}) diff --git a/samples/subsys/uuid/README.rst b/samples/subsys/uuid/README.rst new file mode 100644 index 00000000000..3e399af2d55 --- /dev/null +++ b/samples/subsys/uuid/README.rst @@ -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 +``` diff --git a/samples/subsys/uuid/prj.conf b/samples/subsys/uuid/prj.conf new file mode 100644 index 00000000000..e68bba789fb --- /dev/null +++ b/samples/subsys/uuid/prj.conf @@ -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 diff --git a/samples/subsys/uuid/sample.yaml b/samples/subsys/uuid/sample.yaml new file mode 100644 index 00000000000..b8ec922c994 --- /dev/null +++ b/samples/subsys/uuid/sample.yaml @@ -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" diff --git a/samples/subsys/uuid/src/main.c b/samples/subsys/uuid/src/main.c new file mode 100644 index 00000000000..436e790e562 --- /dev/null +++ b/samples/subsys/uuid/src/main.c @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2025, SECO Mind Srl + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include +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; +} diff --git a/tests/lib/uuid/src/main.c b/tests/lib/uuid/src/main.c index 13fcaf6637b..b8607767e40 100644 --- a/tests/lib/uuid/src/main.c +++ b/tests/lib/uuid/src/main.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024, SECO Mind Srl + * Copyright (c) 2025, SECO Mind Srl * * SPDX-License-Identifier: Apache-2.0 */