|
38 | 38 | #include <zephyr/sys/check.h>
|
39 | 39 |
|
40 | 40 | #include "../host/conn_internal.h"
|
| 41 | +#include "../host/hci_core.h" |
41 | 42 | #include "../host/keys.h"
|
42 | 43 |
|
43 | 44 | #include "common/bt_str.h"
|
@@ -83,6 +84,7 @@ struct bt_csip_set_member_svc_inst {
|
83 | 84 | static struct bt_csip_set_member_svc_inst svc_insts[CONFIG_BT_CSIP_SET_MEMBER_MAX_INSTANCE_COUNT];
|
84 | 85 |
|
85 | 86 | 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); |
86 | 88 |
|
87 | 89 | static K_WORK_DELAYABLE_DEFINE(deferred_nfy_work, deferred_nfy_work_handler);
|
88 | 90 |
|
@@ -460,24 +462,49 @@ static void set_lock_timer_handler(struct k_work *work)
|
460 | 462 | static void csip_security_changed(struct bt_conn *conn, bt_security_t level,
|
461 | 463 | enum bt_security_err err)
|
462 | 464 | {
|
| 465 | + const bt_addr_le_t *peer_addr; |
| 466 | + |
463 | 467 | if (err != 0 || conn->encrypt == 0) {
|
464 | 468 | return;
|
465 | 469 | }
|
466 | 470 |
|
| 471 | + peer_addr = bt_conn_get_dst(conn); |
| 472 | + |
467 | 473 | if (!bt_le_bond_exists(conn->id, &conn->le.dst)) {
|
468 | 474 | return;
|
469 | 475 | }
|
470 | 476 |
|
471 | 477 | for (size_t i = 0U; i < ARRAY_SIZE(svc_insts); i++) {
|
472 | 478 | struct bt_csip_set_member_svc_inst *svc_inst = &svc_insts[i];
|
473 | 479 |
|
| 480 | + bool found = false; |
| 481 | + |
| 482 | + /* Check if client is already in the active list */ |
474 | 483 | 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 | + } |
476 | 501 |
|
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]; |
478 | 505 |
|
479 | 506 | 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)) { |
481 | 508 | notify_work_reschedule(K_NO_WAIT);
|
482 | 509 | break;
|
483 | 510 | }
|
@@ -737,11 +764,26 @@ static void notify(struct bt_csip_set_member_svc_inst *svc_inst, struct bt_conn
|
737 | 764 | {
|
738 | 765 | int err;
|
739 | 766 |
|
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); |
741 | 778 | return;
|
742 | 779 | }
|
743 | 780 |
|
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); |
745 | 787 | if (err) {
|
746 | 788 | if (err == -ENOTCONN) {
|
747 | 789 | 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
|
801 | 843 | for (size_t i = 0U; i < ARRAY_SIZE(svc_insts); i++) {
|
802 | 844 | struct bt_csip_set_member_svc_inst *svc_inst = &svc_insts[i];
|
803 | 845 |
|
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++) { |
805 | 847 | /* Check if device is registered, it not, add it */
|
806 | 848 | if (!atomic_test_bit(svc_inst->clients[j].flags, FLAG_ACTIVE)) {
|
807 | 849 | 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 *
|
849 | 891 | bt_conn_cb_register(&conn_callbacks);
|
850 | 892 | bt_conn_auth_info_cb_register(&auth_callbacks);
|
851 | 893 |
|
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 | + } |
854 | 898 |
|
855 | 899 | first_register = true;
|
856 | 900 | }
|
|
0 commit comments