From 5e1613b9e40589d80b133287129c69289eb27ac4 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Wed, 17 Jan 2024 14:47:40 -0800 Subject: [PATCH] fix(split): Fix for reconnecting to splits * Tweak GATT resubscribes on reconnects. --- app/src/split/bluetooth/Kconfig | 3 +++ app/src/split/bluetooth/central.c | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/src/split/bluetooth/Kconfig b/app/src/split/bluetooth/Kconfig index 4da50528343f..00e35245fc47 100644 --- a/app/src/split/bluetooth/Kconfig +++ b/app/src/split/bluetooth/Kconfig @@ -20,6 +20,9 @@ config ZMK_SPLIT_ROLE_CENTRAL config BT_L2CAP_TX_BUF_COUNT default 5 if ZMK_SPLIT_ROLE_CENTRAL +config BT_GATT_AUTO_RESUBSCRIBE + default n if ZMK_SPLIT_ROLE_CENTRAL + if ZMK_SPLIT_ROLE_CENTRAL config ZMK_SPLIT_BLE_CENTRAL_PERIPHERALS diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c index abb37a0b91ce..eed20bf5ba16 100644 --- a/app/src/split/bluetooth/central.c +++ b/app/src/split/bluetooth/central.c @@ -41,6 +41,7 @@ enum peripheral_slot_state { }; struct peripheral_slot { + bool have_existing_bond; enum peripheral_slot_state state; struct bt_conn *conn; struct bt_gatt_discover_params discover_params; @@ -115,6 +116,7 @@ int release_peripheral_slot(int index) { slot->conn = NULL; } slot->state = PERIPHERAL_SLOT_STATE_OPEN; + slot->have_existing_bond = false; // Raise events releasing any active positions from this peripheral for (int i = 0; i < POSITION_STATE_DATA_LEN; i++) { @@ -375,7 +377,11 @@ static uint8_t split_central_battery_level_read_func(struct bt_conn *conn, uint8 #endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_CENTRAL_BATTERY_LEVEL_FETCHING) */ static int split_central_subscribe(struct bt_conn *conn, struct bt_gatt_subscribe_params *params) { - int err = bt_gatt_subscribe(conn, params); + int err; + + err = peripheral_slot_for_conn(conn)->have_existing_bond + ? bt_gatt_resubscribe(BT_ID_DEFAULT, bt_conn_get_dst(conn), params) + : bt_gatt_subscribe(conn, params); switch (err) { case -EALREADY: LOG_DBG("[ALREADY SUBSCRIBED]"); @@ -686,6 +692,14 @@ static int start_scanning(void) { return 0; } +static void check_slot_for_bonded(const struct bt_bond_info *info, void *user_data) { + struct peripheral_slot *slot = (struct peripheral_slot *)user_data; + + if (bt_addr_le_cmp(&info->addr, bt_conn_get_dst(slot->conn)) == 0) { + slot->have_existing_bond = true; + } +} + static void split_central_connected(struct bt_conn *conn, uint8_t conn_err) { char addr[BT_ADDR_LE_STR_LEN]; struct bt_conn_info info; @@ -711,6 +725,7 @@ static void split_central_connected(struct bt_conn *conn, uint8_t conn_err) { LOG_DBG("Connected: %s", addr); confirm_peripheral_slot_conn(conn); + bt_foreach_bond(BT_ID_DEFAULT, check_slot_for_bonded, peripheral_slot_for_conn(conn)); split_central_process_connection(conn); }