From 7e65e4c5c458423813a3825cd34a405a9961c75d Mon Sep 17 00:00:00 2001 From: elagil Date: Mon, 18 Jan 2021 19:46:28 +0100 Subject: [PATCH] Implemented bluetooth disable key feature --- app/include/dt-bindings/zmk/bt.h | 2 ++ app/include/zmk/ble.h | 1 + app/src/behaviors/behavior_bt.c | 2 ++ app/src/ble.c | 17 +++++++++++++++++ docs/docs/behaviors/bluetooth.md | 7 +++++++ 5 files changed, 29 insertions(+) diff --git a/app/include/dt-bindings/zmk/bt.h b/app/include/dt-bindings/zmk/bt.h index 8ca10606..d239ad47 100644 --- a/app/include/dt-bindings/zmk/bt.h +++ b/app/include/dt-bindings/zmk/bt.h @@ -9,6 +9,7 @@ #define BT_PRV_CMD 2 #define BT_SEL_CMD 3 // #define BT_FULL_RESET_CMD 4 +#define BT_DIS_CMD 5 /* Note: Some future commands will include additional parameters, so we @@ -18,4 +19,5 @@ defines these aliases up front. #define BT_CLR BT_CLR_CMD 0 #define BT_NXT BT_NXT_CMD 0 #define BT_PRV BT_PRV_CMD 0 +#define BT_DIS BT_DIS_CMD 0 #define BT_SEL BT_SEL_CMD diff --git a/app/include/zmk/ble.h b/app/include/zmk/ble.h index f6f6e191..d5cb70b3 100644 --- a/app/include/zmk/ble.h +++ b/app/include/zmk/ble.h @@ -13,6 +13,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_disconnect(); int zmk_ble_active_profile_index(); bt_addr_le_t *zmk_ble_active_profile_addr(); diff --git a/app/src/behaviors/behavior_bt.c b/app/src/behaviors/behavior_bt.c index 3149c8ce..42096445 100644 --- a/app/src/behaviors/behavior_bt.c +++ b/app/src/behaviors/behavior_bt.c @@ -28,6 +28,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_DIS_CMD: + return zmk_ble_disconnect(); default: LOG_ERR("Unknown BT command: %d", binding->param1); } diff --git a/app/src/ble.c b/app/src/ble.c index 3a115cf4..ce6f2861 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -594,6 +594,23 @@ static int zmk_ble_init(const struct device *_arg) { return 0; } +int zmk_ble_disconnect() { + LOG_DBG(""); + + for (uint8_t profile_index = 0; profile_index < PROFILE_COUNT; profile_index++) { + struct zmk_ble_profile *profile = &profiles[profile_index]; + + if (bt_addr_le_cmp(&(profile->peer), BT_ADDR_LE_ANY)) { + LOG_DBG("Disconnecting profile %d!", profile_index); + + struct bt_conn *conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &(profile->peer)); + bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); + } + } + + return 0; +} + int zmk_ble_unpair_all() { int resp = 0; for (int i = BT_ID_DEFAULT; i < CONFIG_BT_ID_MAX; i++) { diff --git a/docs/docs/behaviors/bluetooth.md b/docs/docs/behaviors/bluetooth.md index 0b66a4ca..a4242ef0 100644 --- a/docs/docs/behaviors/bluetooth.md +++ b/docs/docs/behaviors/bluetooth.md @@ -32,6 +32,7 @@ Here is a table describing the command for each define: | `BT_NXT` | Switch to the next profile, cycling through to the first one when the end is reached. | | `BT_PRV` | Switch to the previous profile, cycling through to the last one when the beginning is reached. | | `BT_SEL` | Select the 0-indexed profile by number. Please note: this definition must include a number as an argument in the keymap to work correctly. eg. `BT_SEL 0` | +| `BT_DIS` | Disconnects all bluetooth profiles. This command disables bluetooth advertising until a board reset is issued. | ## Bluetooth Behavior @@ -69,6 +70,12 @@ The bluetooth behavior completes an bluetooth action given on press. &bt BT_SEL 1 ``` +1. Behavior binding to disconnect bluetooth: + + ``` + &bt BT_DIS + ``` + ## Bluetooth Pairing and Profiles ZMK support bluetooth “profiles” which allows connection to multiple devices (5 by default, or 4 if you are using split keyboards). Each profile stores the bluetooth MAC address of a peer, which can be empty if a profile has not been paired with a device yet. Upon switching to a profile, ZMK does the following: