- Notifications
You must be signed in to change notification settings - Fork 8.2k
Blueototh: UUID: add string to uuid #98784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| | @@ -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 | ||||||
| * 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 | ||||||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested change
All uses of UUID should be in caps | ||||||
| * bt_uuid_16 type. If the string uuid is 32 bt uuid, the converted bt_uuid | ||||||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested change
or did you mean to use | ||||||
| * 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The downcasts ( But I would instead prefer three type-safe variants of this function, one for each 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 and help note updated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested change
| ||||||
| * @param str pointer to string to convert | ||||||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested change
| ||||||
| * | ||||||
| * @return 0 if the string was converted successfully, otherwise negative error | ||||||
| */ | ||||||
| int bt_uuid_from_str(struct bt_uuid *uuid, const char *str); | ||||||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider switching the order, so it's Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ( | ||||||
| | ||||||
| #ifdef __cplusplus | ||||||
| } | ||||||
| #endif | ||||||
| | ||||||
| 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); | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Basically if you have a UUID and put it through | ||
| | ||
| 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"); | ||
| } | ||
There was a problem hiding this comment.
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_stralso does.