- 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?
Conversation
include/zephyr/bluetooth/uuid.h Outdated
| * | ||
| * @return true if the string was valid and the UUID was successfully created. | ||
| */ | ||
| bool bt_str_to_uuid(const char *str, struct bt_uuid *uuid); |
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.
I feel like this should use the bt_uuid prefix, but
| bool bt_str_to_uuid(const char *str, struct bt_uuid *uuid); | |
| bool bt_uuid_str_to_uuid(const char *str, struct bt_uuid *uuid); |
Also sounds kinda off
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.
I suggest using from and also reversing the parameters. That way, we both get bt_uuid as a namespace, and we get bt_uuid as a self (to borrow terminology from Python).
| bool bt_str_to_uuid(const char *str, struct bt_uuid *uuid); | |
| bool bt_uuid_from_str(struct bt_uuid *uuid, const char *str); |
| Please expand https://github.com/zephyrproject-rtos/zephyr/tree/main/tests/bluetooth/uuid to test this new function :) |
| * UUID can be in any format, 16-bit, 32-bit or 128-bit. | ||
| * | ||
| * @param str pointer to string to convert | ||
| * @param uuid pointer where to put converted UUID |
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.
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.
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.
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 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
I add it with 4ed0d20 However, I'm not sure how to set up the test environment. |
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.
Pull Request Overview
This PR adds a new function bt_uuid_from_str() to convert string representations of UUIDs into bt_uuid structures. The function supports 16-bit, 32-bit, and 128-bit UUID formats.
- Implements
bt_uuid_from_str()function insubsys/bluetooth/host/uuid.c - Adds function declaration and documentation to
include/zephyr/bluetooth/uuid.h - Adds comprehensive test suite in
tests/bluetooth/uuid/src/test_bt_uuid_from_str.c
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| subsys/bluetooth/host/uuid.c | Implements the bt_uuid_from_str() function to parse UUID strings |
| include/zephyr/bluetooth/uuid.h | Adds function declaration and documentation |
| tests/bluetooth/uuid/src/test_bt_uuid_from_str.c | Adds test cases for 16-bit, 32-bit, 128-bit, and invalid UUID parsing |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Implement bt_uuid_from_str() function to convert string representations to Bluetooth UUID structures. This provides bidirectional conversion between UUID structures and their string representations, complementing the existing bt_uuid_to_str() function. Features: - Supports 16-bit UUID format (4 hex characters, e.g., "180d") - Supports 32-bit UUID format (8 hex characters, e.g., "abcdef12") - Supports 128-bit UUID standard format (with hyphens) - Comprehensive input validation and error handling - Automatic UUID type detection based on string length The function automatically detects the UUID type (16/32/128-bit) based on the input string length and format, setting the appropriate type in the output UUID structure. Signed-off-by: Kai Cheng <chengkai@xiaomi.com>
Add comprehensive unit tests for the new bt_str_to_uuid function to ensure proper conversion of string representations to Bluetooth UUID structures. Tests cover: - 16-bit UUID string conversion (e.g., "180d") - 32-bit UUID string conversion (e.g., "abcdef12") - 128-bit UUID string conversion with standard format - Invalid input cases (malformed strings, empty strings, wrong lengths) Includes proper validation of converted UUID values against expected results using bt_uuid_cmp(). Signed-off-by: Kai Cheng <chengkai@xiaomi.com>
|
Try this: /zephyrproject$ uv venv --clear && uv pip install -r zephyr/scripts/requirements.txt && uv run -- bash -c 'source zephyr/zephyr-env.sh && west twister -ciT zephyr/tests/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 |
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_str also does.
| * 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 | ||
| * 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 comment
The reason will be displayed to describe this comment to others. Learn more.
| * 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?
| * 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 comment
The reason will be displayed to describe this comment to others. Learn more.
| * 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
| * @note The string uuid must be in the standard UUID format, which could be | ||
| * 16 bit, 32 bit or 128 bit format. |
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.
This seems like duplicate text of previous text
| * 16 bit, 32 bit or 128 bit format. | ||
| * 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 comment
The reason will be displayed to describe this comment to others. Learn more.
| * @param uuid pointer where to put converted UUID | |
| * @param uuid Pointer where to put converted UUID |
| * For example: 128 bit format "00001101-0000-1000-8000-00805F9B34FB" | ||
| * | ||
| * @param uuid pointer where to put converted UUID | ||
| * @param str pointer to string to convert |
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.
| * @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); |
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.
Consider switching the order, so it's input, output
| int bt_uuid_from_str(struct bt_uuid *uuid, const char *str); | |
| int bt_uuid_from_str(const char *str, struct bt_uuid *uuid); |
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.
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.
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.
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)
| #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 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



Bluetooth: UUID: add string to UUID conversion function
Summary
This PR adds a new
bt_str_to_uuid()function to convert string representations to Bluetooth UUID structures, complementing the existingbt_uuid_to_str()function. This provides bidirectional conversion between UUID structures and their string representations.Changes
bt_str_to_uuid()function inuuid.hheaderuuid.cTechnical Details
trueon success,falseon invalid input formatFeatures
bt_uuid_to_str()functionsscanfwith exact format matching for reliable parsingUsage Example
Testing
bt_uuid_to_str()Impact
This function fills an important gap in the Bluetooth UUID utilities, enabling applications to easily convert user-input or configuration strings into proper UUID structures for Bluetooth operations.