From f3a9efa8df6e95f69c21f3997bf9dc3ca8280b1a Mon Sep 17 00:00:00 2001 From: moergo-sc Date: Sat, 11 Jun 2022 18:22:18 +1200 Subject: [PATCH] bt: add BT_CLR_ALL_CMD behaviour --- app/include/dt-bindings/zmk/bt.h | 3 ++- app/include/zmk/ble.h | 1 + app/src/behaviors/behavior_bt.c | 2 ++ app/src/ble.c | 17 +++++++++++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/app/include/dt-bindings/zmk/bt.h b/app/include/dt-bindings/zmk/bt.h index 8ca10606..437b7e7c 100644 --- a/app/include/dt-bindings/zmk/bt.h +++ b/app/include/dt-bindings/zmk/bt.h @@ -8,7 +8,7 @@ #define BT_NXT_CMD 1 #define BT_PRV_CMD 2 #define BT_SEL_CMD 3 -// #define BT_FULL_RESET_CMD 4 +#define BT_CLR_ALL_CMD 4 /* Note: Some future commands will include additional parameters, so we @@ -19,3 +19,4 @@ defines these aliases up front. #define BT_NXT BT_NXT_CMD 0 #define BT_PRV BT_PRV_CMD 0 #define BT_SEL BT_SEL_CMD +#define BT_CLR_ALL BT_CLR_ALL_CMD 0 diff --git a/app/include/zmk/ble.h b/app/include/zmk/ble.h index 9b962bc2..05a5bee6 100644 --- a/app/include/zmk/ble.h +++ b/app/include/zmk/ble.h @@ -24,6 +24,7 @@ int zmk_ble_clear_bonds(); int zmk_ble_prof_next(); int zmk_ble_prof_prev(); int zmk_ble_prof_select(uint8_t index); +int zmk_ble_clear_all_bonds(); int zmk_ble_active_profile_index(); int zmk_ble_profile_index(const bt_addr_le_t *addr); diff --git a/app/src/behaviors/behavior_bt.c b/app/src/behaviors/behavior_bt.c index 79b805b6..17b000a0 100644 --- a/app/src/behaviors/behavior_bt.c +++ b/app/src/behaviors/behavior_bt.c @@ -30,6 +30,8 @@ static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, return zmk_ble_prof_prev(); case BT_SEL_CMD: return zmk_ble_prof_select(binding->param2); + case BT_CLR_ALL_CMD: + return zmk_ble_clear_all_bonds(); default: LOG_ERR("Unknown BT command: %d", binding->param1); } diff --git a/app/src/ble.c b/app/src/ble.c index 8cbb4064..50a5b616 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -225,6 +225,23 @@ int zmk_ble_clear_bonds() { return 0; }; +int zmk_ble_clear_all_bonds() { + LOG_DBG("zmk_ble_clear_all_bonds()"); + + // Unpair all profiles + for (uint8_t i = 0; i < ZMK_BLE_PROFILE_COUNT; i++) { + if (bt_addr_le_cmp(&profiles[i].peer, BT_ADDR_LE_ANY)) { + bt_unpair(BT_ID_DEFAULT, &profiles[i].peer); + set_profile_address(i, BT_ADDR_LE_ANY); + } + } + + // Automatically switch to profile 0 + zmk_ble_prof_select(0); + + return 0; +}; + int zmk_ble_active_profile_index() { return active_profile; } int zmk_ble_profile_index(const bt_addr_le_t *addr) {