Skip to content

Conversation

@chengkai15
Copy link
Contributor

@chengkai15 chengkai15 commented Nov 3, 2025

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 existing bt_uuid_to_str() function. This provides bidirectional conversion between UUID structures and their string representations.

Changes

  • New API: Add bt_str_to_uuid() function in uuid.h header
  • Implementation: Implement string parsing for all UUID formats in uuid.c
  • Format Support: Handle 16-bit, 32-bit, and 128-bit UUID string formats

Technical Details

  • 16-bit UUID: Parses 4-character hexadecimal strings (e.g., "1800")
  • 32-bit UUID: Parses 8-character hexadecimal strings (e.g., "00001800")
  • 128-bit UUID: Parses standard UUID format (e.g., "00001800-0000-1000-8000-00805f9b34fb")
  • Validation: Returns true on success, false on invalid input format
  • Error Handling: Comprehensive format validation with proper return codes

Features

  • Bidirectional Conversion: Complements existing bt_uuid_to_str() function
  • Flexible Input: Supports all standard UUID string formats
  • Type Detection: Automatically detects and sets appropriate UUID type (16/32/128-bit)
  • Robust Parsing: Uses sscanf with exact format matching for reliable parsing

Usage Example

struct bt_uuid uuid; const char *uuid_str = "1800"; if (bt_str_to_uuid(uuid_str, &uuid)) { // Successfully converted string to UUID // uuid.type will be BT_UUID_TYPE_16 // BT_UUID_16(&uuid)->val will be 0x1800 }

Testing

  • Verified conversion of 16-bit, 32-bit, and 128-bit UUID strings
  • Tested edge cases and invalid input formats
  • Confirmed proper type detection and value assignment
  • Validated round-trip conversion with bt_uuid_to_str()

Impact

  • New Feature: Adds missing string-to-UUID conversion capability
  • Backward Compatible: No changes to existing APIs
  • Minimal Overhead: Small footprint with comprehensive functionality
  • Utility Enhancement: Useful for configuration, debugging, and dynamic UUID handling

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.

*
* @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);
Copy link
Contributor

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

Suggested change
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

Copy link
Contributor

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).

Suggested change
bool bt_str_to_uuid(const char *str, struct bt_uuid *uuid);
bool bt_uuid_from_str(struct bt_uuid *uuid, const char *str);
@Thalley
Copy link
Contributor

Thalley commented Nov 4, 2025

* 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
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

@zephyrbot zephyrbot added the area: Tests Issues related to a particular existing or missing test label Nov 4, 2025
@zephyrbot zephyrbot requested a review from nashif November 4, 2025 13:38
@chengkai15
Copy link
Contributor Author

Please expand https://github.com/zephyrproject-rtos/zephyr/tree/main/tests/bluetooth/uuid to test this new function :)

I add it with 4ed0d20

However, I'm not sure how to set up the test environment.

Copy link

Copilot AI left a 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 in subsys/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>
@alwa-nordic
Copy link
Contributor

I'm not sure how to set up the test environment.

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/'
@alwa-nordic alwa-nordic added the Bluetooth Review Discussion in the Bluetooth WG meeting required label Nov 4, 2025
Comment on lines +5254 to +5255
* 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
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 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
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?

* 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
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

Comment on lines +5262 to +5263
* @note The string uuid must be in the standard UUID format, which could be
* 16 bit, 32 bit or 128 bit format.
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

* 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
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
* 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
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)

#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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: Bluetooth Host Bluetooth Host (excluding BR/EDR) area: Bluetooth area: Tests Issues related to a particular existing or missing test Bluetooth Review Discussion in the Bluetooth WG meeting required

6 participants