Skip to content

Commit 598b0f9

Browse files
committed
Bluetooth: CSIP: Fix ntf issue to clients on reboot
On reboot, client list to notify is not updated properly. Fix is to check and add the reconnected clients on security changed cb. Subscription check is added before notify to clients. BT Enable check is added in the register function before adding bonded devices to client list. Also typo is corrected in add_bonded_addr_to_client_list in the second loop. Signed-off-by: Nithin Ramesh Myliattil <[email protected]>
1 parent 7abda99 commit 598b0f9

File tree

1 file changed

+52
-8
lines changed

1 file changed

+52
-8
lines changed

subsys/bluetooth/audio/csip_set_member.c

+52-8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <zephyr/sys/check.h>
3939

4040
#include "../host/conn_internal.h"
41+
#include "../host/hci_core.h"
4142
#include "../host/keys.h"
4243

4344
#include "common/bt_str.h"
@@ -83,6 +84,7 @@ struct bt_csip_set_member_svc_inst {
8384
static struct bt_csip_set_member_svc_inst svc_insts[CONFIG_BT_CSIP_SET_MEMBER_MAX_INSTANCE_COUNT];
8485

8586
static void deferred_nfy_work_handler(struct k_work *work);
87+
static void add_bonded_addr_to_client_list(const struct bt_bond_info *info, void *data);
8688

8789
static K_WORK_DELAYABLE_DEFINE(deferred_nfy_work, deferred_nfy_work_handler);
8890

@@ -460,24 +462,49 @@ static void set_lock_timer_handler(struct k_work *work)
460462
static void csip_security_changed(struct bt_conn *conn, bt_security_t level,
461463
enum bt_security_err err)
462464
{
465+
const bt_addr_le_t *peer_addr;
466+
463467
if (err != 0 || conn->encrypt == 0) {
464468
return;
465469
}
466470

471+
peer_addr = bt_conn_get_dst(conn);
472+
467473
if (!bt_le_bond_exists(conn->id, &conn->le.dst)) {
468474
return;
469475
}
470476

471477
for (size_t i = 0U; i < ARRAY_SIZE(svc_insts); i++) {
472478
struct bt_csip_set_member_svc_inst *svc_inst = &svc_insts[i];
473479

480+
bool found = false;
481+
482+
/* Check if client is already in the active list */
474483
for (size_t j = 0U; j < ARRAY_SIZE(svc_inst->clients); j++) {
475-
struct csip_client *client;
484+
struct csip_client *client = &svc_inst->clients[j];
485+
486+
if (atomic_test_bit(client->flags, FLAG_ACTIVE) &&
487+
bt_addr_le_eq(peer_addr, &client->addr)) {
488+
found = true;
489+
break;
490+
}
491+
}
492+
493+
/* If not found, add the bonded address to the client list */
494+
if (!found) {
495+
const struct bt_bond_info bond_info = {
496+
.addr = *peer_addr
497+
};
498+
499+
add_bonded_addr_to_client_list(&bond_info, NULL);
500+
}
476501

477-
client = &svc_inst->clients[i];
502+
/* Check for clients with FLAG_NOTIFY_LOCK */
503+
for (size_t j = 0U; j < ARRAY_SIZE(svc_inst->clients); j++) {
504+
struct csip_client *client = &svc_inst->clients[j];
478505

479506
if (atomic_test_bit(client->flags, FLAG_NOTIFY_LOCK) &&
480-
bt_addr_le_eq(bt_conn_get_dst(conn), &client->addr)) {
507+
bt_addr_le_eq(peer_addr, &client->addr)) {
481508
notify_work_reschedule(K_NO_WAIT);
482509
break;
483510
}
@@ -737,11 +764,26 @@ static void notify(struct bt_csip_set_member_svc_inst *svc_inst, struct bt_conn
737764
{
738765
int err;
739766

740-
if (svc_inst->service_p == NULL) {
767+
if (svc_inst->service_p == NULL || svc_inst->service_p->attrs == NULL) {
768+
return;
769+
}
770+
771+
const struct bt_gatt_attr *attr = bt_gatt_find_by_uuid(
772+
svc_inst->service_p->attrs,
773+
svc_inst->service_p->attr_count,
774+
uuid);
775+
776+
if (!attr) {
777+
LOG_WRN("Attribute for UUID %p not found", uuid);
741778
return;
742779
}
743780

744-
err = bt_gatt_notify_uuid(conn, uuid, svc_inst->service_p->attrs, data, len);
781+
if (!bt_gatt_is_subscribed(conn, attr, BT_GATT_CCC_NOTIFY)) {
782+
LOG_DBG("Connection not subscribed to UUID %p", uuid);
783+
return;
784+
}
785+
786+
err = bt_gatt_notify(conn, attr, data, len);
745787
if (err) {
746788
if (err == -ENOTCONN) {
747789
LOG_DBG("Notification error: ENOTCONN (%d)", err);
@@ -801,7 +843,7 @@ static void add_bonded_addr_to_client_list(const struct bt_bond_info *info, void
801843
for (size_t i = 0U; i < ARRAY_SIZE(svc_insts); i++) {
802844
struct bt_csip_set_member_svc_inst *svc_inst = &svc_insts[i];
803845

804-
for (size_t j = 1U; j < ARRAY_SIZE(svc_inst->clients); i++) {
846+
for (size_t j = 0U; j < ARRAY_SIZE(svc_inst->clients); j++) {
805847
/* Check if device is registered, it not, add it */
806848
if (!atomic_test_bit(svc_inst->clients[j].flags, FLAG_ACTIVE)) {
807849
char addr_str[BT_ADDR_LE_STR_LEN];
@@ -849,8 +891,10 @@ int bt_csip_set_member_register(const struct bt_csip_set_member_register_param *
849891
bt_conn_cb_register(&conn_callbacks);
850892
bt_conn_auth_info_cb_register(&auth_callbacks);
851893

852-
/* Restore bonding list */
853-
bt_foreach_bond(BT_ID_DEFAULT, add_bonded_addr_to_client_list, NULL);
894+
if (atomic_test_bit(bt_dev.flags, BT_DEV_ENABLE)) {
895+
/* Restore bonding list */
896+
bt_foreach_bond(BT_ID_DEFAULT, add_bonded_addr_to_client_list, NULL);
897+
}
854898

855899
first_register = true;
856900
}

0 commit comments

Comments
 (0)