Skip to content

Commit af42036

Browse files
lylezhu2012fabiobaltieri
authored andcommitted
Bluetooth: Classic: SDP: Improve UUID 128 support
In current implementation, the UUID 128 is not well supported. The found issue includes, Issue 1: No clear byte order of the UUID 128 in local SDP record. Issue 2: No clear byte order of the UUID 128 of the SDP discover parameter. For issue 1, Add the description to note that if the SDP attribute type is `BT_SDP_UINT128`, `BT_SDP_INT128`, and `BT_SDP_UUID128`, the byte order should be little-endian. And swap the 128bit from little-endian to big-endian when responding the peer SDP discovery request. For issue 2, Add the description to note that if the SDP discovery type is `Service Search` and `Service Search Attribute`, and UUID is UUID 128, the passed UUID data of the discovery request parameter should be represented as the little-endian byte-order sequence. And swap the 128bit from little-endian to big-endian when packing the SDP discovery packet. Signed-off-by: Lyle Zhu <[email protected]>
1 parent a128f55 commit af42036

File tree

4 files changed

+66
-17
lines changed

4 files changed

+66
-17
lines changed

include/zephyr/bluetooth/classic/sdp.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,13 @@ extern "C" {
241241
* TextString and URLString can be of size 2^{8, 16, 32} bytes
242242
* DataSequence and DataSequenceAlternates can be of size 2^{8, 16, 32}
243243
* The size are computed post-facto in the API and are not known apriori.
244+
*
245+
* For the type BT_SDP_UINT128, BT_SDP_INT128, and BT_SDP_UUID128, the
246+
* byteorder of data should be little-endian. Such as, SPP UUID128:
247+
* `00001101-0000-1000-8000-00805F9B34FB` will be represented as
248+
* {0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00,
249+
* 0x01, 0x11, 0x00, 0x00}
250+
* For UUID 128, @ref BT_SDP_ARRAY_UUID_128 is used to declare the array.
244251
* @{
245252
*/
246253
#define BT_SDP_DATA_NIL 0x00 /**< Nil, the null type */
@@ -327,6 +334,31 @@ struct bt_sdp_record {
327334
*/
328335
#define BT_SDP_ARRAY_32(...) ((uint32_t[]) {__VA_ARGS__})
329336

337+
/**
338+
* @brief Declare a UUID 128 in little-endian format in an attribute.
339+
*
340+
* Helper macro to initialize a 128-bit UUID array value from the readable form
341+
* of UUIDs.
342+
*
343+
* Example of how to declare the UUID `00001101-0000-1000-8000-00805F9B34FB`
344+
*
345+
* @code
346+
* BT_SDP_ARRAY_UUID_128(0x6E400001, 0xB5A3, 0xF393, 0xE0A9, 0xE50E24DCCA9E)
347+
* @endcode
348+
*
349+
* Just replace the hyphen by the comma and add `0x` prefixes.
350+
*
351+
* @param w32 First part of the UUID (32 bits)
352+
* @param w1 Second part of the UUID (16 bits)
353+
* @param w2 Third part of the UUID (16 bits)
354+
* @param w3 Fourth part of the UUID (16 bits)
355+
* @param w48 Fifth part of the UUID (48 bits)
356+
*
357+
* @return The comma separated values for UUID 128.
358+
*/
359+
#define BT_SDP_ARRAY_UUID_128(w32, w1, w2, w3, w48) \
360+
BT_SDP_ARRAY_8(BT_UUID_128_ENCODE(w32, w1, w2, w3, w48))
361+
330362
/**
331363
* @brief Declare a fixed-size data element header.
332364
*
@@ -560,6 +592,10 @@ struct bt_sdp_discover_params {
560592
* On the service discovery completion the callback function will be
561593
* called to get feedback to user about findings.
562594
*
595+
* If the UUID is UUID 128 for discovery type `Service Search` and
596+
* `Service Search Attribute`, the UUID data should be represented as the
597+
* little-endian byteorder sequence.
598+
*
563599
* Service Search: The SDP Client generates an
564600
* SDP_SERVICE_SEARCH_REQ to locate service
565601
* records that match the service search

subsys/bluetooth/host/classic/sdp.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -778,9 +778,15 @@ static uint32_t copy_attribute(struct bt_sdp_data_elem *elem,
778778
net_buf_add_be16(buf, *((uint16_t *)elem->data));
779779
} else if (seq_size == 4U) {
780780
net_buf_add_be32(buf, *((uint32_t *)elem->data));
781+
} else if (seq_size == 8U) {
782+
net_buf_add_be64(buf, *((uint64_t *)elem->data));
781783
} else {
782-
/* TODO: Convert 32bit and 128bit values to big-endian*/
783-
net_buf_add_mem(buf, elem->data, seq_size);
784+
__ASSERT(seq_size == 0x10, "Invalid sequence size");
785+
786+
uint8_t val[seq_size];
787+
788+
sys_memcpy_swap(val, elem->data, sizeof(val));
789+
net_buf_add_mem(buf, val, seq_size);
784790
}
785791
} else {
786792
net_buf_add_mem(buf, elem->data, seq_size);
@@ -1504,6 +1510,7 @@ static int sdp_client_ss_search(struct bt_sdp_client *session,
15041510
const struct bt_sdp_discover_params *param)
15051511
{
15061512
struct net_buf *buf;
1513+
uint8_t uuid128[BT_UUID_SIZE_128];
15071514

15081515
/* Update context param directly. */
15091516
session->param = param;
@@ -1530,8 +1537,8 @@ static int sdp_client_ss_search(struct bt_sdp_client *session,
15301537
case BT_UUID_TYPE_128:
15311538
net_buf_add_u8(buf, 0x11);
15321539
net_buf_add_u8(buf, BT_SDP_UUID128);
1533-
net_buf_add_mem(buf, BT_UUID_128(param->uuid)->val,
1534-
ARRAY_SIZE(BT_UUID_128(param->uuid)->val));
1540+
sys_memcpy_swap(uuid128, BT_UUID_128(param->uuid)->val, sizeof(uuid128));
1541+
net_buf_add_mem(buf, uuid128, sizeof(uuid128));
15351542
break;
15361543
default:
15371544
LOG_ERR("Unknown UUID type %u", param->uuid->type);
@@ -1610,6 +1617,7 @@ static int sdp_client_ssa_search(struct bt_sdp_client *session,
16101617
const struct bt_sdp_discover_params *param)
16111618
{
16121619
struct net_buf *buf;
1620+
uint8_t uuid128[BT_UUID_SIZE_128];
16131621

16141622
/* Update context param directly. */
16151623
session->param = param;
@@ -1636,8 +1644,8 @@ static int sdp_client_ssa_search(struct bt_sdp_client *session,
16361644
case BT_UUID_TYPE_128:
16371645
net_buf_add_u8(buf, 0x11);
16381646
net_buf_add_u8(buf, BT_SDP_UUID128);
1639-
net_buf_add_mem(buf, BT_UUID_128(param->uuid)->val,
1640-
ARRAY_SIZE(BT_UUID_128(param->uuid)->val));
1647+
sys_memcpy_swap(uuid128, BT_UUID_128(param->uuid)->val, sizeof(uuid128));
1648+
net_buf_add_mem(buf, uuid128, sizeof(uuid128));
16411649
break;
16421650
default:
16431651
LOG_ERR("Unknown UUID type %u", param->uuid->type);
@@ -2498,6 +2506,11 @@ static inline ssize_t sdp_get_uuid_len(const uint8_t *data, size_t len)
24982506

24992507
return 5;
25002508
case BT_SDP_UUID128:
2509+
if (len < (BT_UUID_SIZE_128 + sizeof(uint8_t))) {
2510+
break;
2511+
}
2512+
2513+
return BT_UUID_SIZE_128 + sizeof(uint8_t);
25012514
default:
25022515
LOG_ERR("Invalid/unhandled DTD 0x%02x", data[0]);
25032516
return -EINVAL;

tests/bluetooth/classic/sdp_c/src/sdp_client.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ static int cmd_ssa_discovery(const struct shell *sh, size_t argc, char *argv[])
170170
{
171171
int err;
172172
size_t len;
173+
uint8_t uuid128[BT_UUID_SIZE_128];
173174

174175
len = strlen(argv[1]);
175176

@@ -189,8 +190,8 @@ static int cmd_ssa_discovery(const struct shell *sh, size_t argc, char *argv[])
189190
sdp_discover.uuid = &sdp_discover_uuid.u32.uuid;
190191
} else if (len == (BT_UUID_SIZE_128 * 2)) {
191192
sdp_discover_uuid.u128.uuid.type = BT_UUID_TYPE_128;
192-
hex2bin(argv[1], len, &sdp_discover_uuid.u128.val[0],
193-
sizeof(sdp_discover_uuid.u128.val));
193+
hex2bin(argv[1], len, &uuid128[0], sizeof(uuid128));
194+
sys_memcpy_swap(sdp_discover_uuid.u128.val, uuid128, sizeof(uuid128));
194195
sdp_discover.uuid = &sdp_discover_uuid.u128.uuid;
195196
} else {
196197
shell_error(sh, "Invalid UUID");
@@ -213,6 +214,7 @@ static int cmd_ss_discovery(const struct shell *sh, size_t argc, char *argv[])
213214
{
214215
int err;
215216
size_t len;
217+
uint8_t uuid128[BT_UUID_SIZE_128];
216218

217219
len = strlen(argv[1]);
218220

@@ -232,8 +234,8 @@ static int cmd_ss_discovery(const struct shell *sh, size_t argc, char *argv[])
232234
sdp_discover.uuid = &sdp_discover_uuid.u32.uuid;
233235
} else if (len == (BT_UUID_SIZE_128 * 2)) {
234236
sdp_discover_uuid.u128.uuid.type = BT_UUID_TYPE_128;
235-
hex2bin(argv[1], len, &sdp_discover_uuid.u128.val[0],
236-
sizeof(sdp_discover_uuid.u128.val));
237+
hex2bin(argv[1], len, &uuid128[0], sizeof(uuid128));
238+
sys_memcpy_swap(sdp_discover_uuid.u128.val, uuid128, sizeof(uuid128));
237239
sdp_discover.uuid = &sdp_discover_uuid.u128.uuid;
238240
} else {
239241
shell_error(sh, "Invalid UUID");
@@ -310,9 +312,9 @@ static int cmd_ssa_discovery_fail(const struct shell *sh, size_t argc, char *arg
310312
}
311313

312314
SHELL_STATIC_SUBCMD_SET_CREATE(sdp_client_cmds,
313-
SHELL_CMD_ARG(ss_discovery, NULL, "<UUID>", cmd_ss_discovery, 2, 0),
315+
SHELL_CMD_ARG(ss_discovery, NULL, "<Big endian UUID>", cmd_ss_discovery, 2, 0),
314316
SHELL_CMD_ARG(sa_discovery, NULL, "<Service Record Handle>", cmd_sa_discovery, 2, 0),
315-
SHELL_CMD_ARG(ssa_discovery, NULL, "<UUID>", cmd_ssa_discovery, 2, 0),
317+
SHELL_CMD_ARG(ssa_discovery, NULL, "<Big endian UUID>", cmd_ssa_discovery, 2, 0),
316318
SHELL_CMD_ARG(ssa_discovery_fail, NULL, "", cmd_ssa_discovery_fail, 1, 0),
317319
SHELL_SUBCMD_SET_END
318320
);

tests/bluetooth/classic/sdp_s/src/sdp_server.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,6 @@ static int cmd_register_sdp_large_valid(const struct shell *sh, size_t argc, cha
296296
return 0;
297297
}
298298

299-
uint8_t serial_port_svclass_uuid128[16] = {0x00, 0x00, 0x11, 0x01, 0x00, 0x00, 0x10, 0x00,
300-
0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};
301-
302299
static struct bt_sdp_attribute spp_attrs_uuid128[] = {
303300
BT_SDP_NEW_SERVICE,
304301
BT_SDP_LIST(
@@ -307,7 +304,7 @@ static struct bt_sdp_attribute spp_attrs_uuid128[] = {
307304
BT_SDP_DATA_ELEM_LIST(
308305
{
309306
BT_SDP_TYPE_SIZE(BT_SDP_UUID128),
310-
serial_port_svclass_uuid128
307+
BT_SDP_ARRAY_UUID_128(0x00001101, 0x0000, 0x1000, 0x8000, 0x00805F9B34FB),
311308
},
312309
)
313310
),
@@ -348,7 +345,8 @@ static struct bt_sdp_attribute spp_attrs_uuid128[] = {
348345
BT_SDP_DATA_ELEM_LIST(
349346
{
350347
BT_SDP_TYPE_SIZE(BT_SDP_UUID128),
351-
serial_port_svclass_uuid128
348+
BT_SDP_ARRAY_UUID_128(0x00001101, 0x0000, 0x1000, 0x8000,
349+
0x00805F9B34FB),
352350
},
353351
{
354352
BT_SDP_TYPE_SIZE(BT_SDP_UINT16),

0 commit comments

Comments
 (0)