Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions include/zephyr/bluetooth/uuid.h
Original file line number Diff line number Diff line change
Expand Up @@ -5249,6 +5249,27 @@ bool bt_uuid_create(struct bt_uuid *uuid, const uint8_t *data, uint8_t data_len)
*/
void bt_uuid_to_str(const struct bt_uuid *uuid, char *str, size_t len);

/** @brief Convert string to Bluetooth UUID.
*
* Converts string to Bluetooth UUID. The string must be in the standard
* UUID format, which could be 16 bit, 32 bit or 128 bit format. And the
Comment on lines +5254 to +5255
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be a bit tricky to say "standard UUID format", as there are several ways to represent a UUID as a string. Bluetooth seemingly uses the 8-4-4-4-12 format from https://www.rfc-editor.org/rfc/rfc9562.html, so we could possibly refer to that RFC for the string format, or simply omit this part as bt_uuid_to_str also does.

* uuid pointer must point to a bt_uuid large enough to hold the converted
* UUID. If the string uuid is 16 bit, the converted bt_uuid will be
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* UUID. If the string uuid is 16 bit, the converted bt_uuid will be
* UUID. If the string UUID is 16 bit, the converted bt_uuid will be

All uses of UUID should be in caps

* bt_uuid_16 type. If the string uuid is 32 bt uuid, the converted bt_uuid
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* bt_uuid_16 type. If the string uuid is 32 bt uuid, the converted bt_uuid
* bt_uuid_16 type. If the string uuid is 32 bit uuid, the converted bt_uuid

or did you mean to use BT/Bluetooth?

* will be bt_uuid_32 type. If the string uuid is 128 bt uuid, the converted
* bt_uuid will be bt_uuid_128 type.
*
* @note The string uuid must be in the standard UUID format, which could be
* 16 bit, 32 bit or 128 bit format.
Comment on lines +5262 to +5263
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like duplicate text of previous text

* For example: 128 bit format "00001101-0000-1000-8000-00805F9B34FB"
*
* @param uuid pointer where to put converted UUID
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The downcasts (BT_UUID_16, BT_UUID_32, BT_UUID_128) are not safe in the implementation. The documentation here should at least mention this, e.g. specify that uuid parameter is reinterpreted to one of bt_uuid_16, bt_uuid_32, bt_uuid_128, matching the string format.

But I would instead prefer three type-safe variants of this function, one for each bt_uuid subtype.

We can optionally have a C11 _Generic-based macro that automatically selects between the functions.

This makes even more sense considering that a 16-bit BT UUID is representable as an 128-bit UUID.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also create a type compatible with all the subtypes and make a parsing function that selects the variant tag corresponding to the string format, or to the smallest type that can represent the BT UUID. Maybe one function for each of those options.

Copy link
Contributor Author

@chengkai15 chengkai15 Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your suggestion. Perhaps this is a good practice to keep the function and its implementation consistent with bt_uuid_to_str. Use a single function that is compatible with all types and prompt the user to note the length of the output type.

and help note updated

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param uuid pointer where to put converted UUID
* @param uuid Pointer where to put converted UUID
* @param str pointer to string to convert
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param str pointer to string to convert
* @param str Pointer to string to convert
*
* @return 0 if the string was converted successfully, otherwise negative error
*/
int bt_uuid_from_str(struct bt_uuid *uuid, const char *str);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider switching the order, so it's input, output

Suggested change
int bt_uuid_from_str(struct bt_uuid *uuid, const char *str);
int bt_uuid_from_str(const char *str, struct bt_uuid *uuid);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you gave a thumbs-up on this: #98784 (comment)

The reasoning was to prefer a self-like order. It's OK if we change our minds, but maybe we should write something in the style guide.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, missed that it used to be like that. I don't have a strong opinion about this, and I guess it's also a widely debated topic (src, dst or dst, src)


#ifdef __cplusplus
}
#endif
Expand Down
47 changes: 47 additions & 0 deletions subsys/bluetooth/host/uuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include <zephyr/bluetooth/uuid.h>
Expand Down Expand Up @@ -130,3 +131,49 @@ void bt_uuid_to_str(const struct bt_uuid *uuid, char *str, size_t len)
return;
}
}

int bt_uuid_from_str(struct bt_uuid *uuid, const char *str)
{
if (uuid == NULL || str == NULL) {
return -EINVAL;
}

switch (strlen(str)) {
case BT_UUID_SIZE_16 * 2: {
uint16_t *p = &BT_UUID_16(uuid)->val;

if (sscanf(str, "%04hx", p) != 1) {
return -EINVAL;
}

uuid->type = BT_UUID_TYPE_16;
return 0;
}
case BT_UUID_SIZE_32 * 2: {
uint32_t *p = &BT_UUID_32(uuid)->val;

if (sscanf(str, "%08x", p) != 1) {
return -EINVAL;
}

uuid->type = BT_UUID_TYPE_32;
return 0;
}
case BT_UUID_STR_LEN - 1: {
uint8_t *p = BT_UUID_128(uuid)->val;

if (sscanf(str,
"%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx"
"-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
&p[15], &p[14], &p[13], &p[12], &p[11], &p[10], &p[9], &p[8], &p[7],
&p[6], &p[5], &p[4], &p[3], &p[2], &p[1], &p[0]) != BT_UUID_SIZE_128) {
return -EINVAL;
}

uuid->type = BT_UUID_TYPE_128;
return 0;
}
default:
return -EINVAL;
}
}
64 changes: 64 additions & 0 deletions tests/bluetooth/uuid/src/test_bt_uuid_from_str.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* Copyright (c) 2025 Xiaomi Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdint.h>
#include <zephyr/ztest.h>
#include <zephyr/bluetooth/uuid.h>

ZTEST_SUITE(bt_uuid_from_str, NULL, NULL, NULL, NULL, NULL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding one or more tests where you compare the output from both bt_uuid_from_str and bt_uuid_to_str

Basically if you have a UUID and put it through bt_uuid_to_str and then through bt_uuid_from_str you should get the same UUID, and vice versa


ZTEST(bt_uuid_from_str, test_uuid_from_str_16)
{
struct bt_uuid_16 uuid = {0};
int ret;

ret = bt_uuid_from_str(&uuid.uuid, "180d");
zassert_true(ret == 0, "Failed to parse 16-bit UUID");
zassert_true(bt_uuid_cmp(&uuid.uuid, BT_UUID_DECLARE_16(0x180d)) == 0,
"Parsed UUID does not match expected 16-bit UUID");
}

ZTEST(bt_uuid_from_str, test_uuid_from_str_32)
{
struct bt_uuid_32 uuid = {0};
int ret;

ret = bt_uuid_from_str(&uuid.uuid, "abcdef12");
zassert_true(ret == 0, "Failed to parse 32-bit UUID");
zassert_true(bt_uuid_cmp(&uuid.uuid, BT_UUID_DECLARE_32(0xabcdef12)) == 0,
"Parsed UUID does not match expected 32-bit UUID");
}

ZTEST(bt_uuid_from_str, test_uuid_from_str_128)
{
struct bt_uuid_128 uuid = {0};
int ret;

ret = bt_uuid_from_str(&uuid.uuid, "00001101-0000-1000-8000-00805f9b34fb");
zassert_true(ret == 0, "Failed to parse 128-bit UUID");
struct bt_uuid_128 expected =
BT_UUID_INIT_128(0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00,
0x00, 0x01, 0x11, 0x00, 0x00);
zassert_true(bt_uuid_cmp(&uuid.uuid, &expected.uuid) == 0,
"Parsed UUID does not match expected 128-bit UUID");
}

ZTEST(bt_uuid_from_str, test_uuid_from_str_invalid)
{
struct bt_uuid_128 uuid = {0};
int ret;

ret = bt_uuid_from_str(&uuid.uuid, "not-a-uuid");
zassert_true(ret < 0, "Invalid UUID string should fail");

ret = bt_uuid_from_str(&uuid.uuid, "");
zassert_true(ret < 0, "Empty string should fail");

ret = bt_uuid_from_str(&uuid.uuid, "123");
zassert_true(ret < 0, "Too short string should fail");

ret = bt_uuid_from_str(&uuid.uuid, "00001101-0000-1000-8000-00805f9b34fb00");
zassert_true(ret < 0, "Too long 128-bit string should fail");
}