Disconnect on any connection

This commit is contained in:
Justin Ridgewell 2023-12-21 12:45:40 -05:00
parent aaf973efa2
commit 8d0474267b

View file

@ -424,13 +424,15 @@ static int ble_profiles_handle_set(const char *name, size_t len, settings_read_c
struct settings_handler profiles_handler = {.name = "ble", .h_set = ble_profiles_handle_set}; struct settings_handler profiles_handler = {.name = "ble", .h_set = ble_profiles_handle_set};
#endif /* IS_ENABLED(CONFIG_SETTINGS) */ #endif /* IS_ENABLED(CONFIG_SETTINGS) */
static bool is_conn_active_profile(const struct bt_conn *conn) { static bool is_addr_active_profile(const bt_addr_le_t *addr) {
return bt_addr_le_cmp(bt_conn_get_dst(conn), &profiles[active_profile].peer) == 0; return bt_addr_le_cmp(addr, &profiles[active_profile].peer) == 0;
} }
static void connected(struct bt_conn *conn, uint8_t err) { static void connected(struct bt_conn *conn, uint8_t err) {
char addr[BT_ADDR_LE_STR_LEN]; char addr[BT_ADDR_LE_STR_LEN];
struct bt_conn_info info; struct bt_conn_info info;
const bt_addr_le_t *dst = bt_conn_get_dst(conn);
LOG_DBG("Connected thread: %p", k_current_get()); LOG_DBG("Connected thread: %p", k_current_get());
bt_conn_get_info(conn, &info); bt_conn_get_info(conn, &info);
@ -440,7 +442,7 @@ static void connected(struct bt_conn *conn, uint8_t err) {
return; return;
} }
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); bt_addr_le_to_str(dst, addr, sizeof(addr));
advertising_status = ZMK_ADV_NONE; advertising_status = ZMK_ADV_NONE;
if (err) { if (err) {
@ -457,19 +459,14 @@ static void connected(struct bt_conn *conn, uint8_t err) {
} }
#endif // !IS_ENABLED(CONFIG_BT_GATT_AUTO_SEC_REQ) #endif // !IS_ENABLED(CONFIG_BT_GATT_AUTO_SEC_REQ)
if (is_conn_active_profile(conn)) { if (is_addr_active_profile(dst)) {
LOG_DBG("Active profile connected"); LOG_DBG("Active profile connected");
k_work_submit(&raise_profile_changed_event_work); k_work_submit(&raise_profile_changed_event_work);
} else {
#if !IS_ENABLED(CONFIG_ZMK_BLE_FAST_SWITCHING) #if !IS_ENABLED(CONFIG_ZMK_BLE_FAST_SWITCHING)
// Due to a bug with directed advertising, we had to use open int index = zmk_ble_profile_index(dst);
// advertising and probably connected to multiple profiles. if (index >= 0) {
// Now that we have the desired active connection, we disconnect zmk_ble_prof_disconnect(index);
// everything else.
for (int i = 0; i < ZMK_BLE_PROFILE_COUNT; i++) {
if (i != active_profile && bt_addr_le_cmp(&profiles[i].peer, BT_ADDR_LE_ANY)) {
zmk_ble_prof_disconnect(i);
}
} }
#endif #endif
} }
@ -480,8 +477,9 @@ static void connected(struct bt_conn *conn, uint8_t err) {
static void disconnected(struct bt_conn *conn, uint8_t reason) { static void disconnected(struct bt_conn *conn, uint8_t reason) {
char addr[BT_ADDR_LE_STR_LEN]; char addr[BT_ADDR_LE_STR_LEN];
struct bt_conn_info info; struct bt_conn_info info;
const bt_addr_le_t *dst = bt_conn_get_dst(conn);
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); bt_addr_le_to_str(dst, addr, sizeof(addr));
LOG_DBG("Disconnected from %s (reason 0x%02x)", addr, reason); LOG_DBG("Disconnected from %s (reason 0x%02x)", addr, reason);
@ -496,7 +494,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason) {
// connection for a profile as active, and not start advertising yet. // connection for a profile as active, and not start advertising yet.
k_work_submit(&update_advertising_work); k_work_submit(&update_advertising_work);
if (is_conn_active_profile(conn)) { if (is_addr_active_profile(dst)) {
LOG_DBG("Active profile disconnected"); LOG_DBG("Active profile disconnected");
k_work_submit(&raise_profile_changed_event_work); k_work_submit(&raise_profile_changed_event_work);
} }