|
| 1 | +.. _uuid_api: |
| 2 | + |
| 3 | +UUID |
| 4 | +#### |
| 5 | + |
| 6 | +Overview |
| 7 | +******** |
| 8 | + |
| 9 | +Universally Unique Identifiers (UUID), also known as Globally Unique IDentifiers (GUIDs), are |
| 10 | +128-bit identifiers intended to guarantee uniqueness across space and time. They are defined by |
| 11 | +:rfc:`9562`. |
| 12 | + |
| 13 | +The UUID subsystem provides utility functions for the generation, parsing, and manipulation of |
| 14 | +UUIDs. It supports generating version 4 (random) and version 5 (name-based) UUIDs. |
| 15 | + |
| 16 | +Usage |
| 17 | +===== |
| 18 | + |
| 19 | +To use the UUID API, include the header file: |
| 20 | + |
| 21 | +.. code-block:: c |
| 22 | +
|
| 23 | + #include <zephyr/sys/uuid.h> |
| 24 | +
|
| 25 | +Generating a UUIDv4 |
| 26 | +------------------- |
| 27 | + |
| 28 | +UUIDv4 is based on random numbers (hence requires an entropy generator), and can be generated using |
| 29 | +the :c:func:`uuid_generate_v4` function. :kconfig:option:`CONFIG_UUID_V4` must also be enabled. |
| 30 | + |
| 31 | +.. code-block:: c |
| 32 | +
|
| 33 | + struct uuid my_uuid; |
| 34 | + char uuid_str[UUID_STR_LEN]; |
| 35 | +
|
| 36 | + int ret = uuid_generate_v4(&my_uuid); |
| 37 | +
|
| 38 | + if (ret != 0) { |
| 39 | + printk("Failed to generate UUID v4 (err %d)\n", ret); |
| 40 | + return ret; |
| 41 | + } |
| 42 | +
|
| 43 | + ret = uuid_to_string(&my_uuid, uuid_str); |
| 44 | + if (ret != 0) { |
| 45 | + printk("Failed to convert UUID to string (err %d)\n", ret); |
| 46 | + return ret; |
| 47 | + } |
| 48 | +
|
| 49 | + printk("UUID: %s\n", uuid_str); |
| 50 | +
|
| 51 | +Generating a UUIDv5 |
| 52 | +------------------- |
| 53 | + |
| 54 | +UUIDv5 is generated from a namespace UUID and a name (binary data), and can be generated using the |
| 55 | +:c:func:`uuid_generate_v5` function. :kconfig:option:`CONFIG_UUID_V5` must also be enabled. |
| 56 | + |
| 57 | +UUIDv5 is deterministic: the same namespace and name will always result in the same UUID. |
| 58 | + |
| 59 | +.. code-block:: c |
| 60 | +
|
| 61 | + struct uuid ns_uuid; |
| 62 | + struct uuid my_uuid; |
| 63 | + char uuid_str[UUID_STR_LEN]; |
| 64 | +
|
| 65 | + /* Well-known namespace ID for URLs (RFC 9562) */ |
| 66 | + uuid_from_string("6ba7b811-9dad-11d1-80b4-00c04fd430c8", &ns_uuid); |
| 67 | +
|
| 68 | + const char *name = "https://zephyrproject.org/"; |
| 69 | + int ret = uuid_generate_v5(&ns_uuid, name, strlen(name), &my_uuid); |
| 70 | +
|
| 71 | + if (ret != 0) { |
| 72 | + printk("Failed to generate UUID v5 (err %d)\n", ret); |
| 73 | + return ret; |
| 74 | + } |
| 75 | +
|
| 76 | + ret = uuid_to_string(&my_uuid, uuid_str); |
| 77 | + if (ret != 0) { |
| 78 | + printk("Failed to convert UUID to string (err %d)\n", ret); |
| 79 | + return ret; |
| 80 | + } |
| 81 | +
|
| 82 | + /* outputs: "UUID: abdb72b5-b342-5882-925b-4ce0e3cb4790" */ |
| 83 | + printk("UUID: %s\n", uuid_str); |
| 84 | +
|
| 85 | +Configuration |
| 86 | +************* |
| 87 | + |
| 88 | +Related configuration options: |
| 89 | + |
| 90 | +* :kconfig:option:`CONFIG_UUID` |
| 91 | +* :kconfig:option:`CONFIG_UUID_V4` |
| 92 | +* :kconfig:option:`CONFIG_UUID_V5` |
| 93 | +* :kconfig:option:`CONFIG_UUID_BASE64` |
| 94 | + |
| 95 | +API Reference |
| 96 | +************* |
| 97 | + |
| 98 | +.. doxygengroup:: uuid |
0 commit comments