Skip to content

Commit

Permalink
fix(split): Fix for reconnecting to splits
Browse files Browse the repository at this point in the history
* Tweak GATT resubscribes on reconnects.
  • Loading branch information
petejohanson committed Jan 26, 2024
1 parent a2d3583 commit 5e1613b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/src/split/bluetooth/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion app/src/split/bluetooth/central.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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]");
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down

0 comments on commit 5e1613b

Please sign in to comment.