Skip to content

Commit cbf7576

Browse files
lylezhu2012kartben
authored andcommitted
Bluetooth: Classic: SDP: Use slist to manage SDP records
Use slist instead of custom singly linked list to manage SDP records. Check if the SDP record has been registered in the function `bt_sdp_register_service()`. Append the new SDP record to the tail of the SDP record list. Check if the index of SDP record is overflow in the function `bt_sdp_register_service()`. Signed-off-by: Lyle Zhu <[email protected]>
1 parent 7aff307 commit cbf7576

File tree

2 files changed

+29
-17
lines changed
  • include/zephyr/bluetooth/classic
  • subsys/bluetooth/host/classic

2 files changed

+29
-17
lines changed

include/zephyr/bluetooth/classic/sdp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ struct bt_sdp_record {
310310
struct bt_sdp_attribute *attrs; /**< Base addr of attr array */
311311
size_t attr_count; /**< Number of attributes */
312312
uint8_t index; /**< Index of the record in LL */
313-
struct bt_sdp_record *next; /**< Next service record */
313+
sys_snode_t node;
314314
};
315315

316316
/*

subsys/bluetooth/host/classic/sdp.c

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ struct bt_sdp {
6868
/* TODO: Allow more than one pending request */
6969
};
7070

71-
static struct bt_sdp_record *db;
71+
static sys_slist_t sdp_db = SYS_SLIST_STATIC_INIT(&sdp_db);
7272
static uint8_t num_services;
7373

7474
static struct bt_sdp bt_sdp_pool[CONFIG_BT_MAX_CONN];
@@ -437,17 +437,14 @@ static uint32_t search_uuid(struct bt_sdp_data_elem *elem, struct bt_uuid *uuid,
437437
* @return Pointer to the record where the iterator stopped, or NULL if all
438438
* records are covered
439439
*/
440-
static struct bt_sdp_record *bt_sdp_foreach_svc(bt_sdp_svc_func_t func,
441-
void *user_data)
440+
static struct bt_sdp_record *bt_sdp_foreach_svc(bt_sdp_svc_func_t func, void *user_data)
442441
{
443-
struct bt_sdp_record *rec = db;
442+
struct bt_sdp_record *rec, *next;
444443

445-
while (rec) {
444+
SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&sdp_db, rec, next, node) {
446445
if (func(rec, user_data) == BT_SDP_ITER_STOP) {
447446
break;
448447
}
449-
450-
rec = rec->next;
451448
}
452449
return rec;
453450
}
@@ -1655,29 +1652,44 @@ void bt_sdp_init(void)
16551652

16561653
int bt_sdp_register_service(struct bt_sdp_record *service)
16571654
{
1658-
uint32_t handle = SDP_SERVICE_HANDLE_BASE;
1655+
uint8_t index = 0;
16591656

16601657
if (!service) {
16611658
LOG_ERR("No service record specified");
16621659
return 0;
16631660
}
16641661

1662+
if (sys_slist_find(&sdp_db, &service->node, NULL)) {
1663+
LOG_ERR("Service already registered");
1664+
return -EEXIST;
1665+
}
1666+
16651667
if (num_services == BT_SDP_MAX_SERVICES) {
16661668
LOG_ERR("Reached max allowed registrations");
16671669
return -ENOMEM;
16681670
}
16691671

1670-
if (db) {
1671-
handle = db->handle + 1;
1672+
if (!sys_slist_is_empty(&sdp_db)) {
1673+
struct bt_sdp_record *last;
1674+
1675+
last = CONTAINER_OF(sys_slist_peek_tail(&sdp_db), struct bt_sdp_record, node);
1676+
index = last->index + 1;
1677+
1678+
if (last->index > index) {
1679+
LOG_ERR("Registered record is full");
1680+
return -EOVERFLOW;
1681+
}
16721682
}
16731683

1674-
service->next = db;
1675-
service->index = num_services++;
1676-
service->handle = handle;
1677-
*((uint32_t *)(service->attrs[0].val.data)) = handle;
1678-
db = service;
1684+
service->index = index;
1685+
service->handle = SDP_SERVICE_HANDLE_BASE + index;
1686+
*((uint32_t *)(service->attrs[0].val.data)) = service->handle;
1687+
1688+
sys_slist_append(&sdp_db, &service->node);
1689+
1690+
num_services++;
16791691

1680-
LOG_DBG("Service registered at %u", handle);
1692+
LOG_DBG("Service registered at %u", service->handle);
16811693

16821694
return 0;
16831695
}

0 commit comments

Comments
 (0)