Skip to content

Commit 107895c

Browse files
committed
Blueototh: UUID: add string to uuid
add string to uuid Signed-off-by: Kai Cheng <chengkai@xiaomi.com>
1 parent 2526911 commit 107895c

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

include/zephyr/bluetooth/uuid.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5249,6 +5249,27 @@ bool bt_uuid_create(struct bt_uuid *uuid, const uint8_t *data, uint8_t data_len)
52495249
*/
52505250
void bt_uuid_to_str(const struct bt_uuid *uuid, char *str, size_t len);
52515251

5252+
/** @brief Convert string to Bluetooth UUID.
5253+
*
5254+
* Converts string to Bluetooth UUID. The string must be in the standard
5255+
* UUID format, which could be 16 bit, 32 bit or 128 bit format. and the
5256+
* uuid pointer must point to a bt_uuid large enough to hold the converted
5257+
* UUID. if the string uuid is 16 bit, the converted bt_uuid will be
5258+
* bt_uuid_16 type. if the string uuid is 32 bt uuid, the converted bt_uuid
5259+
* will be bt_uuid_32 type. if the string uuid is 128 bt uuid, the converted
5260+
* bt_uuid will be bt_uuid_128 type.
5261+
*
5262+
* @note The string uuid must be in the standard UUID format, which could be
5263+
* 16 bit, 32 bit or 128 bit format.
5264+
* For example: 128 bit format "00001101-0000-1000-8000-00805F9B34FB"
5265+
*
5266+
* @param uuid pointer where to put converted UUID
5267+
* @param str pointer to string to convert
5268+
*
5269+
* @return 0 if the string was converted successfully, otherwise negative error
5270+
*/
5271+
int bt_uuid_from_str(struct bt_uuid *uuid, const char *str);
5272+
52525273
#ifdef __cplusplus
52535274
}
52545275
#endif

subsys/bluetooth/host/uuid.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <errno.h>
1010
#include <stdbool.h>
1111
#include <stdint.h>
12+
#include <stdio.h>
1213
#include <string.h>
1314

1415
#include <zephyr/bluetooth/uuid.h>
@@ -130,3 +131,49 @@ void bt_uuid_to_str(const struct bt_uuid *uuid, char *str, size_t len)
130131
return;
131132
}
132133
}
134+
135+
int bt_uuid_from_str(struct bt_uuid *uuid, const char *str);
136+
{
137+
if (uuid == NULL || str == NULL) {
138+
return -EINVAL;
139+
}
140+
141+
switch (strlen(str)) {
142+
case BT_UUID_SIZE_16 * 2: {
143+
uint16_t *p = &BT_UUID_16(uuid)->val;
144+
145+
if (sscanf(str, "%04hx", p) != 1) {
146+
return -EINVAL;
147+
}
148+
149+
uuid->type = BT_UUID_TYPE_16;
150+
return 0;
151+
}
152+
case BT_UUID_SIZE_32 * 2: {
153+
uint32_t *p = &BT_UUID_32(uuid)->val;
154+
155+
if (sscanf(str, "%08x", p) != 1) {
156+
return -EINVAL;
157+
}
158+
159+
uuid->type = BT_UUID_TYPE_32;
160+
return 0;
161+
}
162+
case BT_UUID_STR_LEN - 1: {
163+
uint8_t *p = BT_UUID_128(uuid)->val;
164+
165+
if (sscanf(str,
166+
"%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx"
167+
"-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
168+
&p[0], &p[1], &p[2], &p[3], &p[4], &p[5], &p[6], &p[7], &p[8], &p[9],
169+
&p[10], &p[11], &p[12], &p[13], &p[14], &p[15]) != BT_UUID_SIZE_128) {
170+
return -EINVAL;
171+
}
172+
173+
uuid->type = BT_UUID_TYPE_128;
174+
return 0;
175+
}
176+
default:
177+
return -EINVAL;
178+
}
179+
}

0 commit comments

Comments
 (0)