From cf970efb98c5af97955bfffbcebb3b065b16edc4 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Fri, 28 Aug 2020 14:15:16 -0400 Subject: [PATCH 01/11] feat(bluetooth): Proper bond management, identity support for non-splits * Add `bt` behavior that can be used to perform certain actions, such as next/prev identity, reset identity, etc. NOTE: Multiple identities is only supported for non-split shields, due to missing Zephyr identity functionality for dual central/peripheral devices. * Proper bond reset tied to action, that honors peripheral bonds, so folks can reset and pair to other hosts, without breaking bonds between splt halves. --- app/CMakeLists.txt | 7 +- app/Kconfig | 29 +- app/dts/behaviors.dtsi | 3 +- app/dts/behaviors/bluetooth.dtsi | 9 + .../behaviors/zmk,behavior-bluetooth.yaml | 8 + app/include/dt-bindings/zmk/bt.h | 17 + app/include/zmk/ble.h | 13 + app/src/behaviors/behavior_bt.c | 62 ++++ app/src/ble.c | 335 ++++++++++++++++-- app/src/split/bluetooth/central.c | 49 ++- docs/docs/behavior/bluetooth.md | 82 +++++ docs/sidebars.js | 1 + 12 files changed, 563 insertions(+), 52 deletions(-) create mode 100644 app/dts/behaviors/bluetooth.dtsi create mode 100644 app/dts/bindings/behaviors/zmk,behavior-bluetooth.yaml create mode 100644 app/include/dt-bindings/zmk/bt.h create mode 100644 app/src/behaviors/behavior_bt.c create mode 100644 docs/docs/behavior/bluetooth.md diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 2e24fdc2..0f2d2ba7 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -41,8 +41,11 @@ target_sources(app PRIVATE src/behaviors/behavior_toggle_layer.c) target_sources(app PRIVATE src/behaviors/behavior_transparent.c) target_sources(app PRIVATE src/behaviors/behavior_none.c) target_sources(app PRIVATE src/behaviors/behavior_sensor_rotate_key_press.c) -target_sources(app PRIVATE src/keymap.c) +if(NOT CONFIG_ZMK_BLE_SPLIT_PERIPHERAL) + target_sources(app PRIVATE src/keymap.c) +endif() target_sources_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/behaviors/behavior_rgb_underglow.c) +target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/behaviors/behavior_bt.c) target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/ble.c) target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/ble_unpair_combo.c) target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL app PRIVATE src/split_listener.c) @@ -57,4 +60,4 @@ target_sources(app PRIVATE src/endpoints.c) target_sources(app PRIVATE src/hid_listener.c) target_sources(app PRIVATE src/main.c) -zephyr_cc_option(-Wfatal-errors) \ No newline at end of file +zephyr_cc_option(-Wfatal-errors) diff --git a/app/Kconfig b/app/Kconfig index 877fce43..b60db056 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -40,8 +40,6 @@ menuconfig ZMK_BLE select BT_PERIPHERAL select BT_GATT_DIS select BT_GATT_BAS - select SETTINGS - # select BT_SETTINGS if ZMK_BLE @@ -49,6 +47,12 @@ config ZMK_BLE_INIT_PRIORITY int "Init Priority" default 50 +config SETTINGS + default y + +config BT_SETTINGS + default y + config SYSTEM_WORKQUEUE_STACK_SIZE default 2048 @@ -103,8 +107,11 @@ config ZMK_SPLIT_BLE_ROLE_CENTRAL if ZMK_SPLIT_BLE_ROLE_CENTRAL +config BT_MAX_PAIRED + default 2 + config BT_MAX_CONN - default 5 + default 2 config BT_MAX_PAIRED # Bump this everywhere once we support switching active connections! @@ -120,15 +127,18 @@ if ZMK_SPLIT_BLE_ROLE_PERIPHERAL config ZMK_USB default n +config BT_MAX_PAIRED + default 1 config BT_MAX_CONN - default 5 + default 2 config BT_GAP_AUTO_UPDATE_CONN_PARAMS default n endif + endchoice endif @@ -138,6 +148,17 @@ endif endmenu +if ZMK_BLE && !ZMK_SPLIT_BLE + +config BT_ID_MAX + default 5 + +# Used to update the name to include the identity used +config BT_DEVICE_NAME_DYNAMIC + default y + +endif + config ZMK_KSCAN_MOCK_DRIVER bool "Enable mock kscan driver to simulate key presses" default n diff --git a/app/dts/behaviors.dtsi b/app/dts/behaviors.dtsi index ab70bcc7..202202b4 100644 --- a/app/dts/behaviors.dtsi +++ b/app/dts/behaviors.dtsi @@ -7,4 +7,5 @@ #include #include #include -#include \ No newline at end of file +#include +#include \ No newline at end of file diff --git a/app/dts/behaviors/bluetooth.dtsi b/app/dts/behaviors/bluetooth.dtsi new file mode 100644 index 00000000..ea09f4a2 --- /dev/null +++ b/app/dts/behaviors/bluetooth.dtsi @@ -0,0 +1,9 @@ +/ { + behaviors { + bt: behavior_bluetooth { + compatible = "zmk,behavior-bluetooth"; + label = "BLUETOOTH"; + #binding-cells = <2>; + }; + }; +}; diff --git a/app/dts/bindings/behaviors/zmk,behavior-bluetooth.yaml b/app/dts/bindings/behaviors/zmk,behavior-bluetooth.yaml new file mode 100644 index 00000000..127ebe0b --- /dev/null +++ b/app/dts/bindings/behaviors/zmk,behavior-bluetooth.yaml @@ -0,0 +1,8 @@ +# Copyright (c) 2020, Peter Johanson +# SPDX-License-Identifier: MIT + +description: Bluetooth Behavior + +compatible: "zmk,behavior-bluetooth" + +include: two_param.yaml diff --git a/app/include/dt-bindings/zmk/bt.h b/app/include/dt-bindings/zmk/bt.h new file mode 100644 index 00000000..6b05ff0c --- /dev/null +++ b/app/include/dt-bindings/zmk/bt.h @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#define BT_RST_CMD 0 +#define BT_IDENT_NEXT_CMD 1 +#define BT_IDENT_PREV_CMD 2 +#define BT_IDENT_SEL_CMD 3 +#define BT_IDENT_CLR_CMD 4 + +#define BT_RST BT_RST_CMD 0 +#define BT_IDENT_NEXT BT_IDENT_NEXT_CMD 0 +#define BT_IDENT_PREV BT_IDENT_PREV_CMD 0 +#define BT_IDENT_SEL BT_IDENT_SEL_CMD +#define BT_IDENT_CLR BT_IDENT_CLR_CMD 0 \ No newline at end of file diff --git a/app/include/zmk/ble.h b/app/include/zmk/ble.h index d0aaa96b..b3762cd4 100644 --- a/app/include/zmk/ble.h +++ b/app/include/zmk/ble.h @@ -6,7 +6,20 @@ #pragma once +#include #include +int zmk_ble_adv_pause(); +int zmk_ble_adv_resume(); + +int zmk_ble_identity_clear(); +int zmk_ble_identity_next(); +int zmk_ble_identity_prev(); +int zmk_ble_identity_select(u8_t index); + int zmk_ble_unpair_all(); bool zmk_ble_handle_key_user(struct zmk_key_event *key_event); + +#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) +void zmk_ble_set_peripheral_addr(bt_addr_le_t *addr); +#endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) */ \ No newline at end of file diff --git a/app/src/behaviors/behavior_bt.c b/app/src/behaviors/behavior_bt.c new file mode 100644 index 00000000..724d2457 --- /dev/null +++ b/app/src/behaviors/behavior_bt.c @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#define DT_DRV_COMPAT zmk_behavior_bluetooth + +#include +#include + +#include + +#include + +#include +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + +#include + +static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t command, u32_t arg) +{ + switch (command) + { + case BT_RST_CMD: + return zmk_ble_unpair_all(); + case BT_IDENT_CLR_CMD: + return zmk_ble_identity_clear(); +#if CONFIG_BT_ID_MAX != 1 + case BT_IDENT_NEXT_CMD: + return zmk_ble_identity_next(); + case BT_IDENT_PREV_CMD: + return zmk_ble_identity_prev(); + case BT_IDENT_SEL_CMD: + return zmk_ble_identity_select(arg); +#endif /* BT_ID_MAX != 1 */ + } + + return -ENOTSUP; +} + +static int behavior_bt_init(struct device *dev) +{ + return 0; +}; + +static int on_keymap_binding_released(struct device *dev, u32_t position, u32_t command, u32_t arg) +{ + return 0; +} + +static const struct behavior_driver_api behavior_bt_driver_api = { + .binding_pressed = on_keymap_binding_pressed, + .binding_released = on_keymap_binding_released, +}; + +DEVICE_AND_API_INIT(behavior_bt, DT_INST_LABEL(0), + behavior_bt_init, + NULL, + NULL, + APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, + &behavior_bt_driver_api); diff --git a/app/src/ble.c b/app/src/ble.c index c4d3efd5..97ff4619 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -8,6 +8,8 @@ #include #include +#include +#include #include #include @@ -15,6 +17,13 @@ #include #include #include +#include + +#if IS_ENABLED(CONFIG_SETTINGS) + +#include + +#endif #include @@ -28,16 +37,249 @@ static struct bt_conn *auth_passkey_entry_conn; static u8_t passkey_entries[6] = {0, 0, 0, 0, 0, 0}; static u8_t passkey_digit = 0; -#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) -#define ZMK_ADV_PARAMS BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE | \ - BT_LE_ADV_OPT_USE_NAME | \ - BT_LE_ADV_OPT_ONE_TIME, \ +#define ZMK_BT_LE_ADV_PARAM_INIT(_id, _options, _int_min, _int_max, _peer) \ +{ \ + .id = _id, \ + .sid = 0, \ + .secondary_max_skip = 0, \ + .options = (_options), \ + .interval_min = (_int_min), \ + .interval_max = (_int_max), \ + .peer = (_peer), \ +} + +#define ZMK_BT_LE_ADV_PARAM(_id, _options, _int_min, _int_max, _peer) \ + ((struct bt_le_adv_param[]) { \ + ZMK_BT_LE_ADV_PARAM_INIT(_id, _options, _int_min, _int_max, _peer) \ + }) + +#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) +#define ZMK_ADV_PARAMS(_id) ZMK_BT_LE_ADV_PARAM(_id, \ + BT_LE_ADV_OPT_CONNECTABLE | \ + BT_LE_ADV_OPT_ONE_TIME | \ + BT_LE_ADV_OPT_USE_NAME, \ BT_GAP_ADV_FAST_INT_MIN_2, \ BT_GAP_ADV_FAST_INT_MAX_2, NULL) #else -#define ZMK_ADV_PARAMS BT_LE_ADV_CONN_NAME +#define ZMK_ADV_PARAMS(_id) ZMK_BT_LE_ADV_PARAM(_id, \ + BT_LE_ADV_OPT_CONNECTABLE | \ + BT_LE_ADV_OPT_USE_NAME, \ + BT_GAP_ADV_FAST_INT_MIN_2, \ + BT_GAP_ADV_FAST_INT_MAX_2, NULL) #endif +static const struct bt_data zmk_ble_ad[] = { + BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), + BT_DATA_BYTES(BT_DATA_UUID16_SOME, +#if !IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) + 0x12, 0x18, /* HID Service */ +#endif + 0x0f, 0x18 /* Battery Service */ + ), +#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) + BT_DATA_BYTES(BT_DATA_UUID128_ALL, + ZMK_SPLIT_BT_SERVICE_UUID) +#endif +}; + +#define IDENTITY_COUNT CONFIG_BT_ID_MAX + +#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) + +static bt_addr_le_t peripheral_addr; + +#endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) */ + + +static u8_t active_identity = 0; + +int zmk_ble_adv_pause() +{ + int err = bt_le_adv_stop(); + if (err) { + LOG_ERR("Failed to stop advertising (err %d)", err); + return err; + } + + return 0; +}; + +int zmk_ble_adv_resume() +{ + struct bt_le_adv_param *adv_params = ZMK_ADV_PARAMS(active_identity); + + LOG_DBG(""); + int err = bt_le_adv_start(adv_params, zmk_ble_ad, ARRAY_SIZE(zmk_ble_ad), NULL, 0); + if (err) + { + LOG_ERR("Advertising failed to start (err %d)", err); + return err; + } + + return 0; +}; + +static void disconnect_host_connection(struct bt_conn *conn, void *arg) +{ + struct bt_conn_info info; + bt_conn_get_info(conn, &info); + + if (info.role != BT_CONN_ROLE_SLAVE) { + return; + } + + bt_conn_disconnect(conn, BT_HCI_ERR_LOCALHOST_TERM_CONN); +}; + +static int activate_profile(u8_t index) +{ + int err; + + if (index >= IDENTITY_COUNT) { + return -EINVAL; + } + + if (active_identity != index) { + LOG_DBG("Persisting new active identity"); + active_identity = index; + +#if IS_ENABLED(CONFIG_SETTINGS) + err = settings_save_one("ble/active_identity", &active_identity, sizeof(u8_t)); + if (err) { + LOG_WRN("Failed to persist active_identity (err %d)", err); + } +#endif + +#if IS_ENABLED(CONFIG_BT_DEVICE_NAME_DYNAMIC) + char name[CONFIG_BT_DEVICE_NAME_MAX]; + snprintf(name, sizeof(name), "%s (Profile %d)", CONFIG_ZMK_KEYBOARD_NAME, active_identity + 1); + bt_set_name(name); +#endif /* IS_ENABLED(CONFIG_BT_DEVICE_NAME_DYNAMIC) */ + } + + return zmk_ble_adv_resume(); +}; + +static int deactivate_profile(u8_t index) +{ + int err = zmk_ble_adv_pause(); + if (err) { + LOG_WRN("Failed to pause advertising %d", err); + } + + bt_conn_foreach(BT_CONN_TYPE_ALL, disconnect_host_connection, NULL); + + return 0; +}; + +int zmk_ble_identity_select(u8_t index) +{ + LOG_DBG("index %d", index); + if (index >= IDENTITY_COUNT) { + return -EINVAL; + } + + int err = deactivate_profile(active_identity); + if (err) { + LOG_WRN("Failed to deactivate profile"); + return err; + } + + return activate_profile(index); +}; + +static void unpair_non_peripheral_bonds(const struct bt_bond_info *info, void *user_data) { + char addr[BT_ADDR_LE_STR_LEN]; + bt_addr_le_to_str(&info->addr, addr, sizeof(addr)); + +#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) + if (!bt_addr_le_cmp(&info->addr, &peripheral_addr)) { + LOG_DBG("Skipping unpairing peripheral %s", log_strdup(addr)); + return; + } +#endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) */ + + LOG_DBG("Unpairing %s", log_strdup(addr)); + bt_unpair(active_identity, &info->addr); +} + +int zmk_ble_identity_clear() +{ + LOG_DBG(""); + int err = deactivate_profile(active_identity); + if (err) { + return err; + } + + bt_foreach_bond(active_identity, unpair_non_peripheral_bonds, NULL); + + return activate_profile(active_identity); +}; + +int zmk_ble_identity_next() +{ + LOG_DBG("active_identity %d IDENTITY_COUNT %d", active_identity, IDENTITY_COUNT); + return zmk_ble_identity_select((active_identity + 1) % IDENTITY_COUNT); +} + +int zmk_ble_identity_prev() +{ + LOG_DBG(""); + return zmk_ble_identity_select((active_identity + IDENTITY_COUNT - 1) % IDENTITY_COUNT); +} + +#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) + +void zmk_ble_set_peripheral_addr(bt_addr_le_t *addr) +{ + memcpy(&peripheral_addr, addr, sizeof(bt_addr_le_t)); + settings_save_one("ble/peripheral_address", addr, sizeof(bt_addr_le_t)); +} + +#endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) */ + +#if IS_ENABLED(CONFIG_SETTINGS) + +static int ble_profiles_handle_set(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg) +{ + const char *next; + + LOG_DBG("Setting BLE value %s", log_strdup(name)); + + if (settings_name_steq(name, "active_identity", &next) && !next) { + if (len != sizeof(active_identity)) { + return -EINVAL; + } + + int err = read_cb(cb_arg, &active_identity, sizeof(active_identity)); + if (err <= 0) { + LOG_ERR("Failed to handle profile from settings (err %d)", err); + return err; + } + LOG_DBG("Loaded active identity %d", active_identity); +#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) + } else if (settings_name_steq(name, "peripheral_address", &next) && !next) { + if (len != sizeof(bt_addr_le_t)) { + return -EINVAL; + } + + int err = read_cb(cb_arg, &peripheral_addr, sizeof(bt_addr_le_t)); + if (err <= 0) { + LOG_ERR("Failed to handle peripheral address from settings (err %d)", err); + return err; + } +#endif + } + + return 0; +}; + +struct settings_handler profiles_handler = { + .name = "ble", + .h_set = ble_profiles_handle_set +}; +#endif /* IS_ENABLED(CONFIG_SETTINGS) */ + static void connected(struct bt_conn *conn, u8_t err) { char addr[BT_ADDR_LE_STR_LEN]; @@ -71,6 +313,12 @@ static void disconnected(struct bt_conn *conn, u8_t reason) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); LOG_DBG("Disconnected from %s (reason 0x%02x)", log_strdup(addr), reason); + +#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) + if (bt_addr_le_cmp(&peripheral_addr, BT_ADDR_LE_ANY) && bt_addr_le_cmp(&peripheral_addr, bt_conn_get_dst(conn))) { + zmk_ble_adv_resume(); + } +#endif } static void security_changed(struct bt_conn *conn, bt_security_t level, @@ -146,19 +394,6 @@ static struct bt_conn_auth_cb zmk_ble_auth_cb_display = { .cancel = auth_cancel, }; -static const struct bt_data zmk_ble_ad[] = { - BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), - BT_DATA_BYTES(BT_DATA_UUID16_SOME, -#if !IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) - 0x12, 0x18, /* HID Service */ -#endif - 0x0f, 0x18 /* Battery Service */ - ), -#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) - BT_DATA_BYTES(BT_DATA_UUID128_ALL, - ZMK_SPLIT_BT_SERVICE_UUID) -#endif -}; static void zmk_ble_ready(int err) { @@ -169,17 +404,42 @@ static void zmk_ble_ready(int err) return; } - err = bt_le_adv_start(ZMK_ADV_PARAMS, zmk_ble_ad, ARRAY_SIZE(zmk_ble_ad), NULL, 0); - if (err) - { - LOG_ERR("Advertising failed to start (err %d)", err); - return; - } + zmk_ble_identity_select(active_identity); } +#if CONFIG_BT_ID_MAX != 1 +static int initialize_identities() +{ + bt_addr_le_t addrs[CONFIG_BT_ID_MAX]; + size_t count = CONFIG_BT_ID_MAX; + + LOG_DBG(""); + bt_id_get(addrs, &count); + + for (int i = 0; i < count; i++) { + char addr[BT_ADDR_LE_STR_LEN]; + bt_addr_le_to_str(&addrs[i], addr, sizeof(addr)); + LOG_DBG("Existing identity %s", log_strdup(addr)); + } + + for (int i = count; i < CONFIG_BT_ID_MAX; i++) { + LOG_DBG("Initializing identity %d", i); + int id = bt_id_create(NULL, NULL); + if (id < 0) { + LOG_ERR("Failed to create new identity with id %d", i); + return id; + } + } + + return 0; +}; +#endif /* CONFIG_BT_ID_MAX != 1 */ + static int zmk_ble_init(struct device *_arg) { - int err = bt_enable(NULL); + int err; + + err = bt_enable(NULL); if (err) { @@ -187,11 +447,23 @@ static int zmk_ble_init(struct device *_arg) return err; } - if (IS_ENABLED(CONFIG_BT_SETTINGS)) - { - settings_load(); +#if IS_ENABLED(CONFIG_SETTINGS) + settings_subsys_init(); + + err = settings_register(&profiles_handler); + if (err) { + LOG_ERR("Failed to setup the profile settings handler (err %d)", err); + return err; } + settings_load(); + +#endif + +#if CONFIG_BT_ID_MAX != 1 + initialize_identities(); +#endif /* CONFIG_BT_ID_MAX != 1 */ + bt_conn_cb_register(&conn_callbacks); bt_conn_auth_cb_register(&zmk_ble_auth_cb_display); @@ -203,7 +475,12 @@ static int zmk_ble_init(struct device *_arg) int zmk_ble_unpair_all() { LOG_DBG(""); - return bt_unpair(BT_ID_DEFAULT, NULL); + int err = bt_unpair(BT_ID_DEFAULT, NULL); + if (err) { + LOG_ERR("Failed to unpair devices (err %d)", err); + } + + return err; }; bool zmk_ble_handle_key_user(struct zmk_key_event *key_event) diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c index 9e67228e..df8f34ec 100644 --- a/app/src/split/bluetooth/central.c +++ b/app/src/split/bluetooth/central.c @@ -10,12 +10,14 @@ #include #include #include +#include #include #include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); +#include #include #include #include @@ -71,6 +73,18 @@ static u8_t split_central_notify_func(struct bt_conn *conn, return BT_GATT_ITER_CONTINUE; } +static int split_central_subscribe(struct bt_conn *conn) +{ + int err = bt_gatt_subscribe(conn, &subscribe_params); + if (err && err != -EALREADY) { + LOG_ERR("Subscribe failed (err %d)", err); + } else { + LOG_DBG("[SUBSCRIBED]"); + } + + return 0; +} + static u8_t split_central_discovery_func(struct bt_conn *conn, const struct bt_gatt_attr *attr, struct bt_gatt_discover_params *params) @@ -112,12 +126,7 @@ static u8_t split_central_discovery_func(struct bt_conn *conn, subscribe_params.value = BT_GATT_CCC_NOTIFY; subscribe_params.ccc_handle = attr->handle; - err = bt_gatt_subscribe(conn, &subscribe_params); - if (err && err != -EALREADY) { - LOG_ERR("Subscribe failed (err %d)", err); - } else { - LOG_DBG("[SUBSCRIBED]"); - } + split_central_subscribe(conn); return BT_GATT_ITER_STOP; } @@ -137,16 +146,20 @@ static void split_central_process_connection(struct bt_conn *conn) { } if (conn == default_conn) { - discover_params.uuid = &uuid.uuid; - discover_params.func = split_central_discovery_func; - discover_params.start_handle = 0x0001; - discover_params.end_handle = 0xffff; - discover_params.type = BT_GATT_DISCOVER_PRIMARY; + if (subscribe_params.value) { + split_central_subscribe(conn); + } else { + discover_params.uuid = &uuid.uuid; + discover_params.func = split_central_discovery_func; + discover_params.start_handle = 0x0001; + discover_params.end_handle = 0xffff; + discover_params.type = BT_GATT_DISCOVER_PRIMARY; - err = bt_gatt_discover(default_conn, &discover_params); - if (err) { - LOG_ERR("Discover failed(err %d)", err); - return; + err = bt_gatt_discover(default_conn, &discover_params); + if (err) { + LOG_ERR("Discover failed(err %d)", err); + return; + } } } @@ -194,6 +207,8 @@ static bool split_central_eir_found(struct bt_data *data, void *user_data) LOG_DBG("Found the split service"); + zmk_ble_set_peripheral_addr(addr); + err = bt_le_scan_stop(); if (err) { LOG_ERR("Stop LE scan failed (err %d)", err); @@ -206,10 +221,11 @@ static bool split_central_eir_found(struct bt_data *data, void *user_data) split_central_process_connection(default_conn); } else { param = BT_LE_CONN_PARAM(0x0006, 0x0006, 30, 400); + err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, param, &default_conn); if (err) { - LOG_ERR("Create conn failed (err %d)", err); + LOG_ERR("Create conn failed (err %d) (create conn? 0x%04x)", err, BT_HCI_OP_LE_CREATE_CONN); start_scan(); } @@ -263,6 +279,7 @@ static void split_central_connected(struct bt_conn *conn, u8_t conn_err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); + if (conn_err) { LOG_ERR("Failed to connect to %s (%u)", addr, conn_err); diff --git a/docs/docs/behavior/bluetooth.md b/docs/docs/behavior/bluetooth.md new file mode 100644 index 00000000..df8ec26b --- /dev/null +++ b/docs/docs/behavior/bluetooth.md @@ -0,0 +1,82 @@ +--- +title: Bluetooth Behavior +sidebar_label: Bluetooth +--- + +## Summary + +The bluetooth behavior allows management of various settings and states related to the bluetooth connection(s) +between the keyboard and the host. + +## Bluetooth Command Defines + +Bluetooth command defines are provided through the [`dt-bindings/zmk/bt.h`](https://github.com/zmkfirmware/zmk/blob/main/app/include/dt-bindings/zmk/bt.h) header, +which is added at the top of the keymap file: + +``` +#include +``` + +This will allow you to reference the actions defined in this header such as `BT_IDENT_CLR_CMD`. + +Here is a table describing the command for each define: + +| Define | Action | +| ------------------- | ----------------------------------------------------- | +| `BT_IDENT_CLR_CMD` | Clear paired keyboards (for the current identity)[^1] | +| `BT_IDENT_NEXT_CMD` | Switch to the next identity[^1] | +| `BT_IDENT_PREV_CMD` | Switch to the previous identity | +| `BT_IDENT_SEL_CMD` | Switch to a specific numbered identity | +| `BT_RST_CMD` | Hard reset of all bluetooth bonds | + +Because the `BT_IDENT_SEL_CMD` command takes an additional parameter, the numeric index of the identity to select, _all_ the commands for the bluetooth behavior must take that additional parameter, and ignore the value. To make this easier, +there are alias defines that add those default parameters for you: + +| Define | Action | +| --------------- | --------------------------------------------------------------------------------------- | +| `BT_IDENT_CLR` | Alias for `BT_IDENT_CLR_CMD 0` to clear paired keyboards (for the current identity)[^1] | +| `BT_IDENT_NEXT` | Alias for `BT_IDENT_NEXT_CMD 0` to switch to the next identity[^1] | +| `BT_IDENT_PREV` | Alias for `BT_IDENT_PREV_CMD 0` to switch to the previous identity | +| `BT_IDENT_SEL` | Alias for `BT_IDENT_SEL_CMD` to switch to a specific numbered identity | +| `BT_RST` | Alias for `BT_RST_CMD 0` to reset all bonds[^2] | + +[^1]: Multiple keyboard identities/profiles is only support on non-split keyboards as of this time. +[^2]: This may interrupt pairing between both halves of a split keyboard. For split keyboards, it is preferred to use the [/docs/bond-reset] combo to clear bonds on both devices + +## Bluetooth Behavior + +The bluetooth behavior completes an bluetooth action given on press. + +### Behavior Binding + +- Reference: `&bt` +- Parameter #1: The bluetooth command define, e.g. `BT_IDENT_CLR_CMD` or `BT_RST_CMD` +- Parameter #2: The parameter for the command, when used to identify an identity/profile to select, e.g. `0` + +### Examples + +1. Behavior to clear the paired host: + + ``` + &bt BT_IDENT_CLR + ``` + +1. Behavior to hard reset all bonded devices[^2]: + + ``` + &bt BT_IDENT_CLR + ``` + +Examples for non-split keyboards with multiple identities: + +1. Behavior to switch to the next identity: + + ``` + &bt BT_IDENT_NEXT + ``` + +1. Behavior to switch to the specific numbered identity: + + ``` + &bt BT_IDENT_SEL 1 + ``` diff --git a/docs/sidebars.js b/docs/sidebars.js index 6bd3aa40..ace7fa17 100644 --- a/docs/sidebars.js +++ b/docs/sidebars.js @@ -22,6 +22,7 @@ module.exports = { "behavior/hold-tap", "behavior/mod-tap", "behavior/reset", + "behavior/bluetooth", "behavior/lighting", ], Development: [ From a4d06f69ac9879618e91a9befac6f2e84ef37428 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Mon, 31 Aug 2020 16:56:05 -0400 Subject: [PATCH 02/11] fix(bluetooth): Fix max pair settings for non-split. --- app/Kconfig | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Kconfig b/app/Kconfig index b60db056..3640dc6b 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -107,9 +107,6 @@ config ZMK_SPLIT_BLE_ROLE_CENTRAL if ZMK_SPLIT_BLE_ROLE_CENTRAL -config BT_MAX_PAIRED - default 2 - config BT_MAX_CONN default 2 @@ -153,6 +150,9 @@ if ZMK_BLE && !ZMK_SPLIT_BLE config BT_ID_MAX default 5 +config BT_MAX_PAIRED + default 5 + # Used to update the name to include the identity used config BT_DEVICE_NAME_DYNAMIC default y From fc0812bd2eb08d66819f38bafd1f5d00b933c87b Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Tue, 1 Sep 2020 23:22:30 -0400 Subject: [PATCH 03/11] fix(bluetooth): Remove identity, minimal `bt`. * Simplify the `bt` behavior to one current command `BT_CLEAR_BONDS_CMD`. * Simplify BLE code for split and non-split keyboards. * Remove keymap processing from split peripheral side. --- app/CMakeLists.txt | 4 +- app/Kconfig | 23 +---- app/include/dt-bindings/zmk/bt.h | 17 ++-- app/include/zmk/ble.h | 7 +- app/src/behaviors/behavior_bt.c | 16 +--- app/src/ble.c | 158 +++++-------------------------- app/src/split_listener.c | 1 + docs/docs/behavior/bluetooth.md | 58 +++--------- 8 files changed, 52 insertions(+), 232 deletions(-) diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 0f2d2ba7..ca2767fd 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -41,9 +41,7 @@ target_sources(app PRIVATE src/behaviors/behavior_toggle_layer.c) target_sources(app PRIVATE src/behaviors/behavior_transparent.c) target_sources(app PRIVATE src/behaviors/behavior_none.c) target_sources(app PRIVATE src/behaviors/behavior_sensor_rotate_key_press.c) -if(NOT CONFIG_ZMK_BLE_SPLIT_PERIPHERAL) - target_sources(app PRIVATE src/keymap.c) -endif() +target_sources(app PRIVATE src/keymap.c) target_sources_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/behaviors/behavior_rgb_underglow.c) target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/behaviors/behavior_bt.c) target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/ble.c) diff --git a/app/Kconfig b/app/Kconfig index 3640dc6b..11a3cc05 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -40,18 +40,14 @@ menuconfig ZMK_BLE select BT_PERIPHERAL select BT_GATT_DIS select BT_GATT_BAS + select BT_SETTINGS + select SETTINGS if ZMK_BLE config ZMK_BLE_INIT_PRIORITY int "Init Priority" default 50 - -config SETTINGS - default y - -config BT_SETTINGS - default y config SYSTEM_WORKQUEUE_STACK_SIZE default 2048 @@ -144,21 +140,6 @@ endif endmenu - -if ZMK_BLE && !ZMK_SPLIT_BLE - -config BT_ID_MAX - default 5 - -config BT_MAX_PAIRED - default 5 - -# Used to update the name to include the identity used -config BT_DEVICE_NAME_DYNAMIC - default y - -endif - config ZMK_KSCAN_MOCK_DRIVER bool "Enable mock kscan driver to simulate key presses" default n diff --git a/app/include/dt-bindings/zmk/bt.h b/app/include/dt-bindings/zmk/bt.h index 6b05ff0c..24d27ae6 100644 --- a/app/include/dt-bindings/zmk/bt.h +++ b/app/include/dt-bindings/zmk/bt.h @@ -4,14 +4,11 @@ * SPDX-License-Identifier: MIT */ -#define BT_RST_CMD 0 -#define BT_IDENT_NEXT_CMD 1 -#define BT_IDENT_PREV_CMD 2 -#define BT_IDENT_SEL_CMD 3 -#define BT_IDENT_CLR_CMD 4 +#define BT_CLEAR_BONDS_CMD 0 -#define BT_RST BT_RST_CMD 0 -#define BT_IDENT_NEXT BT_IDENT_NEXT_CMD 0 -#define BT_IDENT_PREV BT_IDENT_PREV_CMD 0 -#define BT_IDENT_SEL BT_IDENT_SEL_CMD -#define BT_IDENT_CLR BT_IDENT_CLR_CMD 0 \ No newline at end of file +/* +Note: Some future commands will include additional parameters, so we +defines these aliases up front. +*/ + +#define BT_CLEAR_BONDS BT_CLEAR_BONDS_CMD 0 \ No newline at end of file diff --git a/app/include/zmk/ble.h b/app/include/zmk/ble.h index b3762cd4..62d476ed 100644 --- a/app/include/zmk/ble.h +++ b/app/include/zmk/ble.h @@ -9,13 +9,8 @@ #include #include -int zmk_ble_adv_pause(); -int zmk_ble_adv_resume(); -int zmk_ble_identity_clear(); -int zmk_ble_identity_next(); -int zmk_ble_identity_prev(); -int zmk_ble_identity_select(u8_t index); +int zmk_ble_clear_bonds(); int zmk_ble_unpair_all(); bool zmk_ble_handle_key_user(struct zmk_key_event *key_event); diff --git a/app/src/behaviors/behavior_bt.c b/app/src/behaviors/behavior_bt.c index 724d2457..63a1ef19 100644 --- a/app/src/behaviors/behavior_bt.c +++ b/app/src/behaviors/behavior_bt.c @@ -22,18 +22,10 @@ static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t c { switch (command) { - case BT_RST_CMD: - return zmk_ble_unpair_all(); - case BT_IDENT_CLR_CMD: - return zmk_ble_identity_clear(); -#if CONFIG_BT_ID_MAX != 1 - case BT_IDENT_NEXT_CMD: - return zmk_ble_identity_next(); - case BT_IDENT_PREV_CMD: - return zmk_ble_identity_prev(); - case BT_IDENT_SEL_CMD: - return zmk_ble_identity_select(arg); -#endif /* BT_ID_MAX != 1 */ + case BT_CLEAR_BONDS_CMD: + return zmk_ble_clear_bonds(); + default: + LOG_ERR("Unknown BT command: %d", command); } return -ENOTSUP; diff --git a/app/src/ble.c b/app/src/ble.c index 97ff4619..0aba16c5 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -82,8 +82,6 @@ static const struct bt_data zmk_ble_ad[] = { #endif }; -#define IDENTITY_COUNT CONFIG_BT_ID_MAX - #if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) static bt_addr_le_t peripheral_addr; @@ -91,8 +89,6 @@ static bt_addr_le_t peripheral_addr; #endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) */ -static u8_t active_identity = 0; - int zmk_ble_adv_pause() { int err = bt_le_adv_stop(); @@ -106,7 +102,7 @@ int zmk_ble_adv_pause() int zmk_ble_adv_resume() { - struct bt_le_adv_param *adv_params = ZMK_ADV_PARAMS(active_identity); + struct bt_le_adv_param *adv_params = ZMK_ADV_PARAMS(BT_ID_DEFAULT); LOG_DBG(""); int err = bt_le_adv_start(adv_params, zmk_ble_ad, ARRAY_SIZE(zmk_ble_ad), NULL, 0); @@ -131,62 +127,6 @@ static void disconnect_host_connection(struct bt_conn *conn, void *arg) bt_conn_disconnect(conn, BT_HCI_ERR_LOCALHOST_TERM_CONN); }; -static int activate_profile(u8_t index) -{ - int err; - - if (index >= IDENTITY_COUNT) { - return -EINVAL; - } - - if (active_identity != index) { - LOG_DBG("Persisting new active identity"); - active_identity = index; - -#if IS_ENABLED(CONFIG_SETTINGS) - err = settings_save_one("ble/active_identity", &active_identity, sizeof(u8_t)); - if (err) { - LOG_WRN("Failed to persist active_identity (err %d)", err); - } -#endif - -#if IS_ENABLED(CONFIG_BT_DEVICE_NAME_DYNAMIC) - char name[CONFIG_BT_DEVICE_NAME_MAX]; - snprintf(name, sizeof(name), "%s (Profile %d)", CONFIG_ZMK_KEYBOARD_NAME, active_identity + 1); - bt_set_name(name); -#endif /* IS_ENABLED(CONFIG_BT_DEVICE_NAME_DYNAMIC) */ - } - - return zmk_ble_adv_resume(); -}; - -static int deactivate_profile(u8_t index) -{ - int err = zmk_ble_adv_pause(); - if (err) { - LOG_WRN("Failed to pause advertising %d", err); - } - - bt_conn_foreach(BT_CONN_TYPE_ALL, disconnect_host_connection, NULL); - - return 0; -}; - -int zmk_ble_identity_select(u8_t index) -{ - LOG_DBG("index %d", index); - if (index >= IDENTITY_COUNT) { - return -EINVAL; - } - - int err = deactivate_profile(active_identity); - if (err) { - LOG_WRN("Failed to deactivate profile"); - return err; - } - - return activate_profile(index); -}; static void unpair_non_peripheral_bonds(const struct bt_bond_info *info, void *user_data) { char addr[BT_ADDR_LE_STR_LEN]; @@ -200,34 +140,19 @@ static void unpair_non_peripheral_bonds(const struct bt_bond_info *info, void *u #endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) */ LOG_DBG("Unpairing %s", log_strdup(addr)); - bt_unpair(active_identity, &info->addr); + bt_unpair(BT_ID_DEFAULT, &info->addr); } -int zmk_ble_identity_clear() +int zmk_ble_clear_bonds() { LOG_DBG(""); - int err = deactivate_profile(active_identity); - if (err) { - return err; - } + + bt_conn_foreach(BT_ID_DEFAULT, disconnect_host_connection, NULL); + bt_foreach_bond(BT_ID_DEFAULT, unpair_non_peripheral_bonds, NULL); - bt_foreach_bond(active_identity, unpair_non_peripheral_bonds, NULL); - - return activate_profile(active_identity); + return 0; }; -int zmk_ble_identity_next() -{ - LOG_DBG("active_identity %d IDENTITY_COUNT %d", active_identity, IDENTITY_COUNT); - return zmk_ble_identity_select((active_identity + 1) % IDENTITY_COUNT); -} - -int zmk_ble_identity_prev() -{ - LOG_DBG(""); - return zmk_ble_identity_select((active_identity + IDENTITY_COUNT - 1) % IDENTITY_COUNT); -} - #if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) void zmk_ble_set_peripheral_addr(bt_addr_le_t *addr) @@ -246,19 +171,8 @@ static int ble_profiles_handle_set(const char *name, size_t len, settings_read_c LOG_DBG("Setting BLE value %s", log_strdup(name)); - if (settings_name_steq(name, "active_identity", &next) && !next) { - if (len != sizeof(active_identity)) { - return -EINVAL; - } - - int err = read_cb(cb_arg, &active_identity, sizeof(active_identity)); - if (err <= 0) { - LOG_ERR("Failed to handle profile from settings (err %d)", err); - return err; - } - LOG_DBG("Loaded active identity %d", active_identity); #if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) - } else if (settings_name_steq(name, "peripheral_address", &next) && !next) { + if (settings_name_steq(name, "peripheral_address", &next) && !next) { if (len != sizeof(bt_addr_le_t)) { return -EINVAL; } @@ -268,8 +182,8 @@ static int ble_profiles_handle_set(const char *name, size_t len, settings_read_c LOG_ERR("Failed to handle peripheral address from settings (err %d)", err); return err; } -#endif } +#endif return 0; }; @@ -318,6 +232,8 @@ static void disconnected(struct bt_conn *conn, u8_t reason) if (bt_addr_le_cmp(&peripheral_addr, BT_ADDR_LE_ANY) && bt_addr_le_cmp(&peripheral_addr, bt_conn_get_dst(conn))) { zmk_ble_adv_resume(); } +#else + zmk_ble_adv_resume(); #endif } @@ -404,42 +320,12 @@ static void zmk_ble_ready(int err) return; } - zmk_ble_identity_select(active_identity); + zmk_ble_adv_resume(); } -#if CONFIG_BT_ID_MAX != 1 -static int initialize_identities() -{ - bt_addr_le_t addrs[CONFIG_BT_ID_MAX]; - size_t count = CONFIG_BT_ID_MAX; - - LOG_DBG(""); - bt_id_get(addrs, &count); - - for (int i = 0; i < count; i++) { - char addr[BT_ADDR_LE_STR_LEN]; - bt_addr_le_to_str(&addrs[i], addr, sizeof(addr)); - LOG_DBG("Existing identity %s", log_strdup(addr)); - } - - for (int i = count; i < CONFIG_BT_ID_MAX; i++) { - LOG_DBG("Initializing identity %d", i); - int id = bt_id_create(NULL, NULL); - if (id < 0) { - LOG_ERR("Failed to create new identity with id %d", i); - return id; - } - } - - return 0; -}; -#endif /* CONFIG_BT_ID_MAX != 1 */ - static int zmk_ble_init(struct device *_arg) { - int err; - - err = bt_enable(NULL); + int err = bt_enable(NULL); if (err) { @@ -460,10 +346,6 @@ static int zmk_ble_init(struct device *_arg) #endif -#if CONFIG_BT_ID_MAX != 1 - initialize_identities(); -#endif /* CONFIG_BT_ID_MAX != 1 */ - bt_conn_cb_register(&conn_callbacks); bt_conn_auth_cb_register(&zmk_ble_auth_cb_display); @@ -474,13 +356,17 @@ static int zmk_ble_init(struct device *_arg) int zmk_ble_unpair_all() { - LOG_DBG(""); - int err = bt_unpair(BT_ID_DEFAULT, NULL); - if (err) { - LOG_ERR("Failed to unpair devices (err %d)", err); + int resp = 0; + for (int i = BT_ID_DEFAULT; i < CONFIG_BT_ID_MAX; i++) { + + int err = bt_unpair(BT_ID_DEFAULT, NULL); + if (err) { + resp = err; + LOG_ERR("Failed to unpair devices (err %d)", err); + } } - return err; + return resp; }; bool zmk_ble_handle_key_user(struct zmk_key_event *key_event) diff --git a/app/src/split_listener.c b/app/src/split_listener.c index 46a95e14..ee59c484 100644 --- a/app/src/split_listener.c +++ b/app/src/split_listener.c @@ -21,6 +21,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); int split_listener(const struct zmk_event_header *eh) { + LOG_DBG(""); if (is_position_state_changed(eh)) { const struct position_state_changed *ev = cast_position_state_changed(eh); if (ev->state) { diff --git a/docs/docs/behavior/bluetooth.md b/docs/docs/behavior/bluetooth.md index df8ec26b..f605c962 100644 --- a/docs/docs/behavior/bluetooth.md +++ b/docs/docs/behavior/bluetooth.md @@ -6,7 +6,8 @@ sidebar_label: Bluetooth ## Summary The bluetooth behavior allows management of various settings and states related to the bluetooth connection(s) -between the keyboard and the host. +between the keyboard and the host. As of right now, there is only one such action support, but in the future +more will be added. ## Bluetooth Command Defines @@ -17,31 +18,20 @@ which is added at the top of the keymap file: #include ``` -This will allow you to reference the actions defined in this header such as `BT_IDENT_CLR_CMD`. +This will allow you to reference the actions defined in this header such as `BT_CLEAR_BONDS_CMD`. Here is a table describing the command for each define: -| Define | Action | -| ------------------- | ----------------------------------------------------- | -| `BT_IDENT_CLR_CMD` | Clear paired keyboards (for the current identity)[^1] | -| `BT_IDENT_NEXT_CMD` | Switch to the next identity[^1] | -| `BT_IDENT_PREV_CMD` | Switch to the previous identity | -| `BT_IDENT_SEL_CMD` | Switch to a specific numbered identity | -| `BT_RST_CMD` | Hard reset of all bluetooth bonds | +| Define | Action | +| -------------------- | --------------------------------------------------------- | +| `BT_CLEAR_BONDS_CMD` | Clear bond information between the keyboard and host [^1] | -Because the `BT_IDENT_SEL_CMD` command takes an additional parameter, the numeric index of the identity to select, _all_ the commands for the bluetooth behavior must take that additional parameter, and ignore the value. To make this easier, -there are alias defines that add those default parameters for you: +Because future bluetooth commands will take an additional parameter, it is recommended to use +the following alias in your keymap to avoid having to change it later. -| Define | Action | -| --------------- | --------------------------------------------------------------------------------------- | -| `BT_IDENT_CLR` | Alias for `BT_IDENT_CLR_CMD 0` to clear paired keyboards (for the current identity)[^1] | -| `BT_IDENT_NEXT` | Alias for `BT_IDENT_NEXT_CMD 0` to switch to the next identity[^1] | -| `BT_IDENT_PREV` | Alias for `BT_IDENT_PREV_CMD 0` to switch to the previous identity | -| `BT_IDENT_SEL` | Alias for `BT_IDENT_SEL_CMD` to switch to a specific numbered identity | -| `BT_RST` | Alias for `BT_RST_CMD 0` to reset all bonds[^2] | - -[^1]: Multiple keyboard identities/profiles is only support on non-split keyboards as of this time. -[^2]: This may interrupt pairing between both halves of a split keyboard. For split keyboards, it is preferred to use the [/docs/bond-reset] combo to clear bonds on both devices +| Define | Action | +| ---------------- | ---------------------------------------------------------------------- | +| `BT_CLEAR_BONDS` | Alias for `BT_CLEAR_BONDS_CMD 0` to clear the bond to the current host | ## Bluetooth Behavior @@ -50,33 +40,13 @@ The bluetooth behavior completes an bluetooth action given on press. ### Behavior Binding - Reference: `&bt` -- Parameter #1: The bluetooth command define, e.g. `BT_IDENT_CLR_CMD` or `BT_RST_CMD` -- Parameter #2: The parameter for the command, when used to identify an identity/profile to select, e.g. `0` +- Parameter #1: The bluetooth command define, e.g. `BT_CLEAR_BONDS_CMD` +- Parameter #2: (Reserved for future bluetooth command types) ### Examples 1. Behavior to clear the paired host: ``` - &bt BT_IDENT_CLR - ``` - -1. Behavior to hard reset all bonded devices[^2]: - - ``` - &bt BT_IDENT_CLR - ``` - -Examples for non-split keyboards with multiple identities: - -1. Behavior to switch to the next identity: - - ``` - &bt BT_IDENT_NEXT - ``` - -1. Behavior to switch to the specific numbered identity: - - ``` - &bt BT_IDENT_SEL 1 + &bt BT_CLEAR_BONDS ``` From 13842a8a1e45b399864ba1ae5a759c614313b4db Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Thu, 3 Sep 2020 13:21:01 -0400 Subject: [PATCH 04/11] fix(bluetooth): Kconfig to clear bonds on start --- app/Kconfig | 4 ++++ app/src/ble.c | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/app/Kconfig b/app/Kconfig index 11a3cc05..b9d05a99 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -52,6 +52,10 @@ config ZMK_BLE_INIT_PRIORITY config SYSTEM_WORKQUEUE_STACK_SIZE default 2048 +config ZMK_BLE_CLEAR_BONDS_ON_START + bool "Configuration that clears all bond information from the keyboard on startup." + default n + # HID GATT notifications sent this way are *not* picked up by Linux, and possibly others. config BT_GATT_NOTIFY_MULTIPLE default n diff --git a/app/src/ble.c b/app/src/ble.c index 0aba16c5..1e748edf 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -346,6 +346,12 @@ static int zmk_ble_init(struct device *_arg) #endif +#if IS_ENABLED(CONFIG_ZMK_BLE_CLEAR_BONDS_ON_START) + for (int i = 0; i < 10; i++) { + bt_unpair(i, NULL); + } +#endif + bt_conn_cb_register(&conn_callbacks); bt_conn_auth_cb_register(&zmk_ble_auth_cb_display); From b103eb4b059b60ebff81eb45db8405ed3a381257 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Thu, 3 Sep 2020 14:55:15 -0400 Subject: [PATCH 05/11] fix(bluetooth): Delete any previously stored name. --- app/src/ble.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/ble.c b/app/src/ble.c index 1e748edf..f7237625 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -347,6 +347,8 @@ static int zmk_ble_init(struct device *_arg) #endif #if IS_ENABLED(CONFIG_ZMK_BLE_CLEAR_BONDS_ON_START) + settings_delete("bt/name"); + for (int i = 0; i < 10; i++) { bt_unpair(i, NULL); } From e88d0833c5f8ddd2b8a9b93ab7b6d03c141f8463 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Thu, 3 Sep 2020 21:11:21 -0400 Subject: [PATCH 06/11] fix(bluetooth): Log when clearing on start. --- app/src/ble.c | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/ble.c b/app/src/ble.c index f7237625..7ef95ca0 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -347,6 +347,7 @@ static int zmk_ble_init(struct device *_arg) #endif #if IS_ENABLED(CONFIG_ZMK_BLE_CLEAR_BONDS_ON_START) + LOG_WRN("Clearing all existing BLE bond information from the keyboard"); settings_delete("bt/name"); for (int i = 0; i < 10; i++) { From 39f980a06dac1769e4f09abaf19d3ccbb4b34e67 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Tue, 8 Sep 2020 23:26:00 -0400 Subject: [PATCH 07/11] feat(bluetooth): Add back profiles, split fixes. * Add back in profiles, not using Zephyr BT identity infrastructure. * Restore additional `&bt` commands for profile operations. * Fix for split pairing and subscriptions, since Zephyr persists subscriptions across connects. * Remove keymap from peripheral builds, reduces firmware size, and avoids unneeded attempts to send HID data. --- app/CMakeLists.txt | 23 +- app/Kconfig | 26 +- app/include/dt-bindings/zmk/bt.h | 11 +- app/include/zmk/ble.h | 9 +- app/include/zmk/ble/profile.h | 16 ++ .../zmk/events/ble-active-profile-changed.h | 22 ++ app/src/behaviors/behavior_bt.c | 6 + app/src/ble.c | 248 +++++++++++++----- app/src/events/ble_active_profile_changed.c | 10 + app/src/hog.c | 36 ++- app/src/split/bluetooth/central.c | 41 +-- app/src/split/bluetooth/service.c | 6 + app/src/split_listener.c | 4 +- docs/docs/behavior/bluetooth.md | 46 +++- 14 files changed, 376 insertions(+), 128 deletions(-) create mode 100644 app/include/zmk/ble/profile.h create mode 100644 app/include/zmk/events/ble-active-profile-changed.h create mode 100644 app/src/events/ble_active_profile_changed.c diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index ca2767fd..3e0560b7 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -29,23 +29,26 @@ target_sources(app PRIVATE src/hid.c) target_sources(app PRIVATE src/sensors.c) target_sources_ifdef(CONFIG_ZMK_DISPLAY app PRIVATE src/display.c) target_sources(app PRIVATE src/event_manager.c) +target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/ble_unpair_combo.c) target_sources(app PRIVATE src/events/position_state_changed.c) target_sources(app PRIVATE src/events/keycode_state_changed.c) target_sources(app PRIVATE src/events/modifiers_state_changed.c) target_sources(app PRIVATE src/events/sensor_event.c) -target_sources(app PRIVATE src/behaviors/behavior_key_press.c) -target_sources(app PRIVATE src/behaviors/behavior_reset.c) -target_sources(app PRIVATE src/behaviors/behavior_hold_tap.c) -target_sources(app PRIVATE src/behaviors/behavior_momentary_layer.c) -target_sources(app PRIVATE src/behaviors/behavior_toggle_layer.c) -target_sources(app PRIVATE src/behaviors/behavior_transparent.c) -target_sources(app PRIVATE src/behaviors/behavior_none.c) -target_sources(app PRIVATE src/behaviors/behavior_sensor_rotate_key_press.c) -target_sources(app PRIVATE src/keymap.c) +target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/events/ble_active_profile_changed.c) +if (NOT CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) + target_sources(app PRIVATE src/behaviors/behavior_key_press.c) + target_sources(app PRIVATE src/behaviors/behavior_reset.c) + target_sources(app PRIVATE src/behaviors/behavior_hold_tap.c) + target_sources(app PRIVATE src/behaviors/behavior_momentary_layer.c) + target_sources(app PRIVATE src/behaviors/behavior_toggle_layer.c) + target_sources(app PRIVATE src/behaviors/behavior_transparent.c) + target_sources(app PRIVATE src/behaviors/behavior_none.c) + target_sources(app PRIVATE src/behaviors/behavior_sensor_rotate_key_press.c) + target_sources(app PRIVATE src/keymap.c) +endif() target_sources_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/behaviors/behavior_rgb_underglow.c) target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/behaviors/behavior_bt.c) target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/ble.c) -target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/ble_unpair_combo.c) target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL app PRIVATE src/split_listener.c) target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL app PRIVATE src/split/bluetooth/service.c) target_sources_ifdef(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL app PRIVATE src/split/bluetooth/central.c) diff --git a/app/Kconfig b/app/Kconfig index b9d05a99..917674d6 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -37,6 +37,7 @@ menuconfig ZMK_BLE select BT select BT_SMP select BT_SMP_SC_PAIR_ONLY + select BT_SMP_APP_PAIRING_ACCEPT select BT_PERIPHERAL select BT_GATT_DIS select BT_GATT_BAS @@ -105,19 +106,9 @@ config ZMK_SPLIT_BLE_ROLE_CENTRAL select BT_CENTRAL select BT_GATT_CLIENT -if ZMK_SPLIT_BLE_ROLE_CENTRAL - -config BT_MAX_CONN - default 2 - -config BT_MAX_PAIRED - # Bump this everywhere once we support switching active connections! - default 2 - -endif - config ZMK_SPLIT_BLE_ROLE_PERIPHERAL bool "Peripheral" + select BT_KEYS_OVERWRITE_OLDEST if ZMK_SPLIT_BLE_ROLE_PERIPHERAL @@ -128,16 +119,25 @@ config BT_MAX_PAIRED default 1 config BT_MAX_CONN - default 2 + default 1 config BT_GAP_AUTO_UPDATE_CONN_PARAMS default n endif - endchoice +if ZMK_BLE && (!ZMK_SPLIT_BLE || ZMK_SPLIT_BLE_ROLE_CENTRAL) + +config BT_MAX_CONN + default 6 + +config BT_MAX_PAIRED + default 5 + +endif + endif endif diff --git a/app/include/dt-bindings/zmk/bt.h b/app/include/dt-bindings/zmk/bt.h index 24d27ae6..b2899ab4 100644 --- a/app/include/dt-bindings/zmk/bt.h +++ b/app/include/dt-bindings/zmk/bt.h @@ -4,11 +4,18 @@ * SPDX-License-Identifier: MIT */ -#define BT_CLEAR_BONDS_CMD 0 +#define BT_CLEAR_BONDS_CMD 0 +#define BT_PROF_NEXT_CMD 1 +#define BT_PROF_PREV_CMD 2 +#define BT_PROF_SEL_CMD 3 +// #define BT_FULL_RESET_CMD 4 /* Note: Some future commands will include additional parameters, so we defines these aliases up front. */ -#define BT_CLEAR_BONDS BT_CLEAR_BONDS_CMD 0 \ No newline at end of file +#define BT_CLEAR_BONDS BT_CLEAR_BONDS_CMD 0 +#define BT_PROF_NEXT BT_PROF_NEXT_CMD 0 +#define BT_PROF_PREV BT_PROF_PREV_CMD 0 +#define BT_PROF_SEL BT_PROF_SEL_CMD \ No newline at end of file diff --git a/app/include/zmk/ble.h b/app/include/zmk/ble.h index 62d476ed..1cf71a77 100644 --- a/app/include/zmk/ble.h +++ b/app/include/zmk/ble.h @@ -6,11 +6,16 @@ #pragma once -#include #include - +#include int zmk_ble_clear_bonds(); +int zmk_ble_prof_next(); +int zmk_ble_prof_prev(); +int zmk_ble_prof_select(u8_t index); + +bt_addr_le_t *zmk_ble_active_profile_addr(); +char *zmk_ble_active_profile_name(); int zmk_ble_unpair_all(); bool zmk_ble_handle_key_user(struct zmk_key_event *key_event); diff --git a/app/include/zmk/ble/profile.h b/app/include/zmk/ble/profile.h new file mode 100644 index 00000000..9a79c6d3 --- /dev/null +++ b/app/include/zmk/ble/profile.h @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include + +#define ZMK_BLE_PROFILE_NAME_MAX 15 + +struct zmk_ble_profile { + char name[ZMK_BLE_PROFILE_NAME_MAX]; + bt_addr_le_t peer; +}; diff --git a/app/include/zmk/events/ble-active-profile-changed.h b/app/include/zmk/events/ble-active-profile-changed.h new file mode 100644 index 00000000..c464236f --- /dev/null +++ b/app/include/zmk/events/ble-active-profile-changed.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#pragma once + +#include +#include +#include + +#include + + +struct ble_active_profile_changed { + struct zmk_event_header header; + u8_t index; + struct zmk_ble_profile *profile; +}; + +ZMK_EVENT_DECLARE(ble_active_profile_changed); diff --git a/app/src/behaviors/behavior_bt.c b/app/src/behaviors/behavior_bt.c index 63a1ef19..bf156833 100644 --- a/app/src/behaviors/behavior_bt.c +++ b/app/src/behaviors/behavior_bt.c @@ -24,6 +24,12 @@ static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t c { case BT_CLEAR_BONDS_CMD: return zmk_ble_clear_bonds(); + case BT_PROF_NEXT_CMD: + return zmk_ble_prof_next(); + case BT_PROF_PREV_CMD: + return zmk_ble_prof_prev(); + case BT_PROF_SEL_CMD: + return zmk_ble_prof_select(arg); default: LOG_ERR("Unknown BT command: %d", command); } diff --git a/app/src/ble.c b/app/src/ble.c index 7ef95ca0..40e05a3a 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -25,49 +25,30 @@ #endif - #include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); +#include #include #include +#include +#include static struct bt_conn *auth_passkey_entry_conn; static u8_t passkey_entries[6] = {0, 0, 0, 0, 0, 0}; static u8_t passkey_digit = 0; -#define ZMK_BT_LE_ADV_PARAM_INIT(_id, _options, _int_min, _int_max, _peer) \ -{ \ - .id = _id, \ - .sid = 0, \ - .secondary_max_skip = 0, \ - .options = (_options), \ - .interval_min = (_int_min), \ - .interval_max = (_int_max), \ - .peer = (_peer), \ -} - -#define ZMK_BT_LE_ADV_PARAM(_id, _options, _int_min, _int_max, _peer) \ - ((struct bt_le_adv_param[]) { \ - ZMK_BT_LE_ADV_PARAM_INIT(_id, _options, _int_min, _int_max, _peer) \ - }) - #if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) -#define ZMK_ADV_PARAMS(_id) ZMK_BT_LE_ADV_PARAM(_id, \ - BT_LE_ADV_OPT_CONNECTABLE | \ - BT_LE_ADV_OPT_ONE_TIME | \ - BT_LE_ADV_OPT_USE_NAME, \ - BT_GAP_ADV_FAST_INT_MIN_2, \ - BT_GAP_ADV_FAST_INT_MAX_2, NULL) +#define PROFILE_COUNT (CONFIG_BT_MAX_PAIRED - 1) #else -#define ZMK_ADV_PARAMS(_id) ZMK_BT_LE_ADV_PARAM(_id, \ - BT_LE_ADV_OPT_CONNECTABLE | \ - BT_LE_ADV_OPT_USE_NAME, \ - BT_GAP_ADV_FAST_INT_MIN_2, \ - BT_GAP_ADV_FAST_INT_MAX_2, NULL) +#define PROFILE_COUNT CONFIG_BT_MAX_PAIRED #endif + +static struct zmk_ble_profile profiles[PROFILE_COUNT]; +static u8_t active_profile; + static const struct bt_data zmk_ble_ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), BT_DATA_BYTES(BT_DATA_UUID16_SOME, @@ -88,6 +69,33 @@ static bt_addr_le_t peripheral_addr; #endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) */ +static void raise_profile_changed_event() +{ + struct ble_active_profile_changed *ev = new_ble_active_profile_changed(); + ev->index = active_profile; + ev->profile = &profiles[active_profile]; + + ZMK_EVENT_RAISE(ev); +} + +static bool active_profile_is_open() +{ + return !bt_addr_le_cmp(&profiles[active_profile].peer, BT_ADDR_LE_ANY); +} + +void set_profile_address(u8_t index, const bt_addr_le_t *addr) +{ + char setting_name[15]; + char addr_str[BT_ADDR_LE_STR_LEN]; + + bt_addr_le_to_str(addr, addr_str, sizeof(addr_str)); + + memcpy(&profiles[index].peer, addr, sizeof(bt_addr_le_t)); + sprintf(setting_name, "ble/profiles/%d", index); + LOG_DBG("Setting profile addr for %s to %s", log_strdup(setting_name), log_strdup(addr_str)); + settings_save_one(setting_name, &profiles[index], sizeof(struct zmk_ble_profile)); + raise_profile_changed_event(); +} int zmk_ble_adv_pause() { @@ -102,10 +110,12 @@ int zmk_ble_adv_pause() int zmk_ble_adv_resume() { - struct bt_le_adv_param *adv_params = ZMK_ADV_PARAMS(BT_ID_DEFAULT); + LOG_DBG("active_profile %d, directed? %s", active_profile, active_profile_is_open() ? "no" : "yes"); - LOG_DBG(""); - int err = bt_le_adv_start(adv_params, zmk_ble_ad, ARRAY_SIZE(zmk_ble_ad), NULL, 0); + int err = bt_le_adv_start( + BT_LE_ADV_CONN_NAME, + zmk_ble_ad, ARRAY_SIZE(zmk_ble_ad), + NULL, 0); if (err) { LOG_ERR("Advertising failed to start (err %d)", err); @@ -115,44 +125,54 @@ int zmk_ble_adv_resume() return 0; }; -static void disconnect_host_connection(struct bt_conn *conn, void *arg) -{ - struct bt_conn_info info; - bt_conn_get_info(conn, &info); - - if (info.role != BT_CONN_ROLE_SLAVE) { - return; - } - - bt_conn_disconnect(conn, BT_HCI_ERR_LOCALHOST_TERM_CONN); -}; - - -static void unpair_non_peripheral_bonds(const struct bt_bond_info *info, void *user_data) { - char addr[BT_ADDR_LE_STR_LEN]; - bt_addr_le_to_str(&info->addr, addr, sizeof(addr)); - -#if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) - if (!bt_addr_le_cmp(&info->addr, &peripheral_addr)) { - LOG_DBG("Skipping unpairing peripheral %s", log_strdup(addr)); - return; - } -#endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) */ - - LOG_DBG("Unpairing %s", log_strdup(addr)); - bt_unpair(BT_ID_DEFAULT, &info->addr); -} - int zmk_ble_clear_bonds() { LOG_DBG(""); - bt_conn_foreach(BT_ID_DEFAULT, disconnect_host_connection, NULL); - bt_foreach_bond(BT_ID_DEFAULT, unpair_non_peripheral_bonds, NULL); + if (bt_addr_le_cmp(&profiles[active_profile].peer, BT_ADDR_LE_ANY)) { + LOG_DBG("Unpairing!"); + bt_unpair(BT_ID_DEFAULT, &profiles[active_profile].peer); + set_profile_address(active_profile, BT_ADDR_LE_ANY); + } return 0; }; +int zmk_ble_prof_select(u8_t index) +{ + LOG_DBG("profile %d", index); + if (active_profile == index) { + return 0; + } + + active_profile = index; + return settings_save_one("ble/active_profile", &active_profile, sizeof(active_profile)); + + raise_profile_changed_event(); +}; + +int zmk_ble_prof_next() +{ + LOG_DBG(""); + return zmk_ble_prof_select((active_profile + 1) % PROFILE_COUNT); +}; + +int zmk_ble_prof_prev() +{ + LOG_DBG(""); + return zmk_ble_prof_select((active_profile + PROFILE_COUNT - 1) % PROFILE_COUNT); +}; + +bt_addr_le_t *zmk_ble_active_profile_addr() +{ + return &profiles[active_profile].peer; +} + +char *zmk_ble_active_profile_name() +{ + return profiles[active_profile].name; +} + #if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) void zmk_ble_set_peripheral_addr(bt_addr_le_t *addr) @@ -171,8 +191,47 @@ static int ble_profiles_handle_set(const char *name, size_t len, settings_read_c LOG_DBG("Setting BLE value %s", log_strdup(name)); + if (settings_name_steq(name, "profiles", &next) && next) { + char *endptr; + u8_t idx = strtoul(next, &endptr, 10); + if (*endptr != '\0') { + LOG_WRN("Invalid profile index: %s", log_strdup(next)); + return -EINVAL; + } + + if (len != sizeof(struct zmk_ble_profile)) { + LOG_ERR("Invalid profile size (got %d expected %d)", len, sizeof(struct zmk_ble_profile)); + return -EINVAL; + } + + if (idx >= PROFILE_COUNT) { + LOG_WRN("Profile address for index %d is larger than max of %d", idx, PROFILE_COUNT); + return -EINVAL; + } + + int err = read_cb(cb_arg, &profiles[idx], sizeof(struct zmk_ble_profile)); + if (err <= 0) { + LOG_ERR("Failed to handle profile address from settings (err %d)", err); + return err; + } + + char addr_str[BT_ADDR_LE_STR_LEN]; + bt_addr_le_to_str(&profiles[idx].peer, addr_str, sizeof(addr_str)); + + LOG_DBG("Loaded %s address for profile %d", log_strdup(addr_str), idx); + } else if (settings_name_steq(name, "active_profile", &next) && !next) { + if (len != sizeof(active_profile)) { + return -EINVAL; + } + + int err = read_cb(cb_arg, &active_profile, sizeof(active_profile)); + if (err <= 0) { + LOG_ERR("Failed to handle active profile from settings (err %d)", err); + return err; + } + } #if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) - if (settings_name_steq(name, "peripheral_address", &next) && !next) { + else if (settings_name_steq(name, "peripheral_address", &next) && !next) { if (len != sizeof(bt_addr_le_t)) { return -EINVAL; } @@ -197,7 +256,6 @@ struct settings_handler profiles_handler = { static void connected(struct bt_conn *conn, u8_t err) { char addr[BT_ADDR_LE_STR_LEN]; - bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (err) @@ -229,11 +287,11 @@ static void disconnected(struct bt_conn *conn, u8_t reason) LOG_DBG("Disconnected from %s (reason 0x%02x)", log_strdup(addr), reason); #if IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) - if (bt_addr_le_cmp(&peripheral_addr, BT_ADDR_LE_ANY) && bt_addr_le_cmp(&peripheral_addr, bt_conn_get_dst(conn))) { - zmk_ble_adv_resume(); - } + // if (bt_addr_le_cmp(&peripheral_addr, BT_ADDR_LE_ANY) && bt_addr_le_cmp(&peripheral_addr, bt_conn_get_dst(conn))) { + // zmk_ble_adv_resume(); + // } #else - zmk_ble_adv_resume(); + // zmk_ble_adv_resume(); #endif } @@ -301,7 +359,52 @@ static void auth_cancel(struct bt_conn *conn) LOG_DBG("Pairing cancelled: %s", log_strdup(addr)); } +#if !IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) +static enum bt_security_err auth_pairing_accept(struct bt_conn *conn, const struct bt_conn_pairing_feat *const feat) +{ + struct bt_conn_info info; + bt_conn_get_info(conn, &info); + + LOG_DBG("role %d, open? %s", info.role, active_profile_is_open() ? "yes" : "no"); + if (info.role != BT_CONN_ROLE_SLAVE && !active_profile_is_open()) { + LOG_WRN("Rejecting pairing request to taken profile %d", active_profile); + return BT_SECURITY_ERR_PAIR_NOT_ALLOWED; + } + + return BT_SECURITY_ERR_SUCCESS; +}; +#endif /* !IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) */ + +static void auth_pairing_complete(struct bt_conn *conn, bool bonded) +{ + struct bt_conn_info info; + char addr[BT_ADDR_LE_STR_LEN]; + const bt_addr_le_t *dst = bt_conn_get_dst(conn); + + bt_addr_le_to_str(dst, addr, sizeof(addr)); + bt_conn_get_info(conn, &info); + + if (info.role != BT_CONN_ROLE_SLAVE) { + LOG_DBG("SKIPPING FOR ROLE %d", info.role); + return; + } + +#if !IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) + if (!active_profile_is_open()) { + LOG_ERR("Pairing completed but current profile is not open: %s", log_strdup(addr)); + bt_unpair(BT_ID_DEFAULT, dst); + return; + } +#endif /* !IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) */ + + set_profile_address(active_profile, dst); +}; + static struct bt_conn_auth_cb zmk_ble_auth_cb_display = { +#if !IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) + .pairing_accept = auth_pairing_accept, +#endif /* !IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_PERIPHERAL) */ + .pairing_complete = auth_pairing_complete, // .passkey_display = auth_passkey_display, #ifdef CONFIG_ZMK_BLE_PASSKEY_ENTRY @@ -348,11 +451,20 @@ static int zmk_ble_init(struct device *_arg) #if IS_ENABLED(CONFIG_ZMK_BLE_CLEAR_BONDS_ON_START) LOG_WRN("Clearing all existing BLE bond information from the keyboard"); - settings_delete("bt/name"); for (int i = 0; i < 10; i++) { bt_unpair(i, NULL); } + + for (int i = 0; i < PROFILE_COUNT; i++) { + char setting_name[15]; + sprintf(setting_name, "ble/profiles/%d", i); + + err = settings_delete(setting_name); + if (err) { + LOG_ERR("Failed to delete setting: %d", err); + } + } #endif bt_conn_cb_register(&conn_callbacks); diff --git a/app/src/events/ble_active_profile_changed.c b/app/src/events/ble_active_profile_changed.c new file mode 100644 index 00000000..a270a144 --- /dev/null +++ b/app/src/events/ble_active_profile_changed.c @@ -0,0 +1,10 @@ +/* + * Copyright (c) 2020 Peter Johanson + * + * SPDX-License-Identifier: MIT + */ + +#include +#include + +ZMK_EVENT_IMPL(ble_active_profile_changed); \ No newline at end of file diff --git a/app/src/hog.c b/app/src/hog.c index 92858d56..93e6d9b3 100644 --- a/app/src/hog.c +++ b/app/src/hog.c @@ -6,6 +6,10 @@ #include +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + #include #include @@ -148,12 +152,40 @@ BT_GATT_SERVICE_DEFINE(hog_svc, BT_GATT_PERM_WRITE, NULL, write_ctrl_point, &ctrl_point)); +struct bt_conn *destination_connection() { + struct bt_conn *conn; + bt_addr_le_t *addr = zmk_ble_active_profile_addr(); + LOG_DBG("Address pointer %p", addr); + if (!bt_addr_le_cmp(addr, BT_ADDR_LE_ANY)) { + LOG_WRN("Not sending, no active address for current profile"); + return NULL; + } else if ((conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, addr)) == NULL) { + LOG_WRN("Not sending, not connected to active profile"); + return NULL; + } + + return conn; + +} + int zmk_hog_send_keypad_report(struct zmk_hid_keypad_report_body *report) { - return bt_gatt_notify(NULL, &hog_svc.attrs[5], report, sizeof(struct zmk_hid_keypad_report_body)); + struct bt_conn *conn = destination_connection(); + if (conn == NULL) { + return -ENOTCONN; + } + + LOG_DBG("Sending to NULL? %s", conn == NULL ? "yes" : "no"); + + return bt_gatt_notify(conn, &hog_svc.attrs[5], report, sizeof(struct zmk_hid_keypad_report_body)); }; int zmk_hog_send_consumer_report(struct zmk_hid_consumer_report_body *report) { - return bt_gatt_notify(NULL, &hog_svc.attrs[10], report, sizeof(struct zmk_hid_consumer_report_body)); + struct bt_conn *conn = destination_connection(); + if (conn == NULL) { + return -ENOTCONN; + } + + return bt_gatt_notify(conn, &hog_svc.attrs[10], report, sizeof(struct zmk_hid_consumer_report_body)); }; diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c index df8f34ec..6d8b4357 100644 --- a/app/src/split/bluetooth/central.c +++ b/app/src/split/bluetooth/central.c @@ -76,10 +76,19 @@ static u8_t split_central_notify_func(struct bt_conn *conn, static int split_central_subscribe(struct bt_conn *conn) { int err = bt_gatt_subscribe(conn, &subscribe_params); - if (err && err != -EALREADY) { - LOG_ERR("Subscribe failed (err %d)", err); - } else { + switch (err) { + case -EALREADY: + LOG_DBG("[ALREADY SUBSCRIBED]"); + break; + // break; + // bt_gatt_unsubscribe(conn, &subscribe_params); + // return split_central_subscribe(conn); + case 0: LOG_DBG("[SUBSCRIBED]"); + break; + default: + LOG_ERR("Subscribe failed (err %d)", err); + break; } return 0; @@ -145,21 +154,17 @@ static void split_central_process_connection(struct bt_conn *conn) { return; } - if (conn == default_conn) { - if (subscribe_params.value) { - split_central_subscribe(conn); - } else { - discover_params.uuid = &uuid.uuid; - discover_params.func = split_central_discovery_func; - discover_params.start_handle = 0x0001; - discover_params.end_handle = 0xffff; - discover_params.type = BT_GATT_DISCOVER_PRIMARY; + if (conn == default_conn && !subscribe_params.value) { + discover_params.uuid = &uuid.uuid; + discover_params.func = split_central_discovery_func; + discover_params.start_handle = 0x0001; + discover_params.end_handle = 0xffff; + discover_params.type = BT_GATT_DISCOVER_PRIMARY; - err = bt_gatt_discover(default_conn, &discover_params); - if (err) { - LOG_ERR("Discover failed(err %d)", err); - return; - } + err = bt_gatt_discover(default_conn, &discover_params); + if (err) { + LOG_ERR("Discover failed(err %d)", err); + return; } } @@ -281,7 +286,7 @@ static void split_central_connected(struct bt_conn *conn, u8_t conn_err) if (conn_err) { - LOG_ERR("Failed to connect to %s (%u)", addr, conn_err); + LOG_ERR("Failed to connect to %s (%u)", log_strdup(addr), conn_err); bt_conn_unref(default_conn); default_conn = NULL; diff --git a/app/src/split/bluetooth/service.c b/app/src/split/bluetooth/service.c index 0a5ddb7a..c2f65d28 100644 --- a/app/src/split/bluetooth/service.c +++ b/app/src/split/bluetooth/service.c @@ -6,6 +6,11 @@ #include #include + +#include + +LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); + #include #include @@ -28,6 +33,7 @@ static ssize_t split_svc_num_of_positions(struct bt_conn *conn, const struct bt_ static void split_svc_pos_state_ccc(const struct bt_gatt_attr *attr, u16_t value) { + LOG_DBG("value %d", value); } diff --git a/app/src/split_listener.c b/app/src/split_listener.c index ee59c484..12638073 100644 --- a/app/src/split_listener.c +++ b/app/src/split_listener.c @@ -25,9 +25,9 @@ int split_listener(const struct zmk_event_header *eh) if (is_position_state_changed(eh)) { const struct position_state_changed *ev = cast_position_state_changed(eh); if (ev->state) { - zmk_split_bt_position_pressed(ev->position); + return zmk_split_bt_position_pressed(ev->position); } else { - zmk_split_bt_position_released(ev->position); + return zmk_split_bt_position_released(ev->position); } } return 0; diff --git a/docs/docs/behavior/bluetooth.md b/docs/docs/behavior/bluetooth.md index f605c962..4a121a31 100644 --- a/docs/docs/behavior/bluetooth.md +++ b/docs/docs/behavior/bluetooth.md @@ -6,8 +6,8 @@ sidebar_label: Bluetooth ## Summary The bluetooth behavior allows management of various settings and states related to the bluetooth connection(s) -between the keyboard and the host. As of right now, there is only one such action support, but in the future -more will be added. +between the keyboard and the host. By default, ZMK supports five "profiles" for selecting which bonded host +computer/laptop/keyboard should receive the keyboard input; many of the commands here operation on those profiles. ## Bluetooth Command Defines @@ -22,16 +22,22 @@ This will allow you to reference the actions defined in this header such as `BT_ Here is a table describing the command for each define: -| Define | Action | -| -------------------- | --------------------------------------------------------- | -| `BT_CLEAR_BONDS_CMD` | Clear bond information between the keyboard and host [^1] | +| Define | Action | +| -------------------- | ---------------------------------------------------------------------------------------------- | +| `BT_CLEAR_BONDS_CMD` | Clear bond information between the keyboard and host for the selected profile [^1] | +| `BT_PROF_NEXT_CMD` | Switch to the next profile, cycling through to the first one when the end is reached. | +| `BT_PROF_PREV_CMD` | Switch to the previous profile, cycling through to the last one when the beginning is reached. | +| `BT_PROF_SEL_CMD` | Select the 0-indexed profile by number. | -Because future bluetooth commands will take an additional parameter, it is recommended to use -the following alias in your keymap to avoid having to change it later. +Because at least one bluetooth commands takes an additional parameter, it is recommended to use +the following aliases in your keymap to avoid having to specify an ignored second parameter: -| Define | Action | -| ---------------- | ---------------------------------------------------------------------- | -| `BT_CLEAR_BONDS` | Alias for `BT_CLEAR_BONDS_CMD 0` to clear the bond to the current host | +| Define | Action | +| ---------------- | ---------------------------------------------------------------------------------------- | +| `BT_CLEAR_BONDS` | Alias for `BT_CLEAR_BONDS_CMD 0` to clear the current profile's bond to the current host | +| `BT_PROF_NEXT` | Alias for `BT_PROF_NEXT_CMD 0` to select the next profile | +| `BT_PROF_PREV` | Alias for `BT_PROF_PREV_CMD 0` to select the previous profile | +| `BT_PROF_SEL` | Alias for `BT_PROF_SEL_CMD` to select the given profile, e.g. `&bt BT_PROF_SEL 1` | ## Bluetooth Behavior @@ -45,8 +51,26 @@ The bluetooth behavior completes an bluetooth action given on press. ### Examples -1. Behavior to clear the paired host: +1. Behavior binding to clear the paired host for the selected profile: ``` &bt BT_CLEAR_BONDS ``` + +1. Behavior binding to select the next profile: + + ``` + &bt BT_PROF_NEXT + ``` + +1. Behavior binding to select the previous profile: + + ``` + &bt BT_PROF_NEXT + ``` + +1. Behavior binding to select the 2nd profile (passed parameters are [zero based](https://en.wikipedia.org/wiki/Zero-based_numbering)): + + ``` + &bt BT_PROF_SEL 1 + ``` From fb91be5164a5b6ba78b19c7a2597c13ddd992f81 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Wed, 9 Sep 2020 14:33:09 -0400 Subject: [PATCH 08/11] fix(bluetooth): Configs for non-splits. --- app/Kconfig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/Kconfig b/app/Kconfig index 917674d6..61805656 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -128,6 +128,10 @@ endif endchoice +endif + +endif + if ZMK_BLE && (!ZMK_SPLIT_BLE || ZMK_SPLIT_BLE_ROLE_CENTRAL) config BT_MAX_CONN @@ -138,10 +142,6 @@ config BT_MAX_PAIRED endif -endif - -endif - endmenu config ZMK_KSCAN_MOCK_DRIVER From 652bb6ce05254f8168f279396a50b92d7a2182c3 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Sat, 12 Sep 2020 23:48:44 -0400 Subject: [PATCH 09/11] feat(bluetooth): Add new settings_reset shield. * Easy to build board/image that will clear saved profile/bond information for a fresh start for keyboards. --- .github/workflows/build.yml | 1 + .../shields/settings_reset/Kconfig.defconfig | 10 ++++++++ .../shields/settings_reset/Kconfig.shield | 5 ++++ .../settings_reset/settings_reset.conf | 1 + .../settings_reset/settings_reset.keymap | 22 +++++++++++++++++ .../settings_reset/settings_reset.overlay | 24 +++++++++++++++++++ 6 files changed, 63 insertions(+) create mode 100644 app/boards/shields/settings_reset/Kconfig.defconfig create mode 100644 app/boards/shields/settings_reset/Kconfig.shield create mode 100644 app/boards/shields/settings_reset/settings_reset.conf create mode 100644 app/boards/shields/settings_reset/settings_reset.keymap create mode 100644 app/boards/shields/settings_reset/settings_reset.overlay diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c8a711c1..02657c0c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -17,6 +17,7 @@ jobs: - lily58_left - lily58_right - romac + - settings_reset include: - board: proton_c shield: clueboard_california diff --git a/app/boards/shields/settings_reset/Kconfig.defconfig b/app/boards/shields/settings_reset/Kconfig.defconfig new file mode 100644 index 00000000..6d050cb4 --- /dev/null +++ b/app/boards/shields/settings_reset/Kconfig.defconfig @@ -0,0 +1,10 @@ +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT + +if SHIELD_SETTINGS_RESET + +config ZMK_KEYBOARD_NAME + default "SETTINGS RESET" + +endif + diff --git a/app/boards/shields/settings_reset/Kconfig.shield b/app/boards/shields/settings_reset/Kconfig.shield new file mode 100644 index 00000000..b5ce97f9 --- /dev/null +++ b/app/boards/shields/settings_reset/Kconfig.shield @@ -0,0 +1,5 @@ +# Copyright (c) 2020 The ZMK Contributors +# SPDX-License-Identifier: MIT + +config SHIELD_SETTINGS_RESET + def_bool $(shields_list_contains,settings_reset) diff --git a/app/boards/shields/settings_reset/settings_reset.conf b/app/boards/shields/settings_reset/settings_reset.conf new file mode 100644 index 00000000..8052a6cf --- /dev/null +++ b/app/boards/shields/settings_reset/settings_reset.conf @@ -0,0 +1 @@ +CONFIG_ZMK_BLE_CLEAR_BONDS_ON_START=y diff --git a/app/boards/shields/settings_reset/settings_reset.keymap b/app/boards/shields/settings_reset/settings_reset.keymap new file mode 100644 index 00000000..05236445 --- /dev/null +++ b/app/boards/shields/settings_reset/settings_reset.keymap @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include +#include + +/ { + keymap { + compatible = "zmk,keymap"; + + default_layer { + bindings = < + &reset + >; + }; + }; +}; + + diff --git a/app/boards/shields/settings_reset/settings_reset.overlay b/app/boards/shields/settings_reset/settings_reset.overlay new file mode 100644 index 00000000..a2b5799b --- /dev/null +++ b/app/boards/shields/settings_reset/settings_reset.overlay @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2020 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +#include + +/ { + chosen { + zmk,kscan = &kscan0; + }; + + kscan0: kscan { + compatible = "zmk,kscan-gpio-direct"; + label = "KSCAN"; + + input-gpios + = <&pro_micro_d 4 (GPIO_ACTIVE_LOW | GPIO_PULL_UP)> + ; + }; + +}; + From 6c8b0b53f0dbec695ab967ee947916875ed49352 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Sun, 13 Sep 2020 22:20:25 -0400 Subject: [PATCH 10/11] refactor(bluetooth): More concise names. --- app/include/dt-bindings/zmk/bt.h | 16 +++++++------- app/src/behaviors/behavior_bt.c | 8 +++---- docs/docs/behavior/bluetooth.md | 36 ++++++++++++++++---------------- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/app/include/dt-bindings/zmk/bt.h b/app/include/dt-bindings/zmk/bt.h index b2899ab4..bf8b4f53 100644 --- a/app/include/dt-bindings/zmk/bt.h +++ b/app/include/dt-bindings/zmk/bt.h @@ -4,10 +4,10 @@ * SPDX-License-Identifier: MIT */ -#define BT_CLEAR_BONDS_CMD 0 -#define BT_PROF_NEXT_CMD 1 -#define BT_PROF_PREV_CMD 2 -#define BT_PROF_SEL_CMD 3 +#define BT_CLR_CMD 0 +#define BT_NXT_CMD 1 +#define BT_PRV_CMD 2 +#define BT_SEL_CMD 3 // #define BT_FULL_RESET_CMD 4 /* @@ -15,7 +15,7 @@ Note: Some future commands will include additional parameters, so we defines these aliases up front. */ -#define BT_CLEAR_BONDS BT_CLEAR_BONDS_CMD 0 -#define BT_PROF_NEXT BT_PROF_NEXT_CMD 0 -#define BT_PROF_PREV BT_PROF_PREV_CMD 0 -#define BT_PROF_SEL BT_PROF_SEL_CMD \ No newline at end of file +#define BT_CLR BT_CLR_CMD 0 +#define BT_NXT BT_NXT_CMD 0 +#define BT_PRV BT_PRV_CMD 0 +#define BT_SEL BT_SEL_CMD \ No newline at end of file diff --git a/app/src/behaviors/behavior_bt.c b/app/src/behaviors/behavior_bt.c index bf156833..3a5fbfbf 100644 --- a/app/src/behaviors/behavior_bt.c +++ b/app/src/behaviors/behavior_bt.c @@ -22,13 +22,13 @@ static int on_keymap_binding_pressed(struct device *dev, u32_t position, u32_t c { switch (command) { - case BT_CLEAR_BONDS_CMD: + case BT_CLR_CMD: return zmk_ble_clear_bonds(); - case BT_PROF_NEXT_CMD: + case BT_NXT_CMD: return zmk_ble_prof_next(); - case BT_PROF_PREV_CMD: + case BT_PRV_CMD: return zmk_ble_prof_prev(); - case BT_PROF_SEL_CMD: + case BT_SEL_CMD: return zmk_ble_prof_select(arg); default: LOG_ERR("Unknown BT command: %d", command); diff --git a/docs/docs/behavior/bluetooth.md b/docs/docs/behavior/bluetooth.md index 4a121a31..f802a9a2 100644 --- a/docs/docs/behavior/bluetooth.md +++ b/docs/docs/behavior/bluetooth.md @@ -18,26 +18,26 @@ which is added at the top of the keymap file: #include ``` -This will allow you to reference the actions defined in this header such as `BT_CLEAR_BONDS_CMD`. +This will allow you to reference the actions defined in this header such as `BT_CLR_CMD`. Here is a table describing the command for each define: -| Define | Action | -| -------------------- | ---------------------------------------------------------------------------------------------- | -| `BT_CLEAR_BONDS_CMD` | Clear bond information between the keyboard and host for the selected profile [^1] | -| `BT_PROF_NEXT_CMD` | Switch to the next profile, cycling through to the first one when the end is reached. | -| `BT_PROF_PREV_CMD` | Switch to the previous profile, cycling through to the last one when the beginning is reached. | -| `BT_PROF_SEL_CMD` | Select the 0-indexed profile by number. | +| Define | Action | +| ------------ | ---------------------------------------------------------------------------------------------- | +| `BT_CLR_CMD` | Clear bond information between the keyboard and host for the selected profile [^1] | +| `BT_NXT_CMD` | Switch to the next profile, cycling through to the first one when the end is reached. | +| `BT_PRV_CMD` | Switch to the previous profile, cycling through to the last one when the beginning is reached. | +| `BT_SEL_CMD` | Select the 0-indexed profile by number. | Because at least one bluetooth commands takes an additional parameter, it is recommended to use the following aliases in your keymap to avoid having to specify an ignored second parameter: -| Define | Action | -| ---------------- | ---------------------------------------------------------------------------------------- | -| `BT_CLEAR_BONDS` | Alias for `BT_CLEAR_BONDS_CMD 0` to clear the current profile's bond to the current host | -| `BT_PROF_NEXT` | Alias for `BT_PROF_NEXT_CMD 0` to select the next profile | -| `BT_PROF_PREV` | Alias for `BT_PROF_PREV_CMD 0` to select the previous profile | -| `BT_PROF_SEL` | Alias for `BT_PROF_SEL_CMD` to select the given profile, e.g. `&bt BT_PROF_SEL 1` | +| Define | Action | +| -------- | -------------------------------------------------------------------------------- | +| `BT_CLR` | Alias for `BT_CLR_CMD 0` to clear the current profile's bond to the current host | +| `BT_NXT` | Alias for `BT_NXT_CMD 0` to select the next profile | +| `BT_PRV` | Alias for `BT_PRV_CMD 0` to select the previous profile | +| `BT_SEL` | Alias for `BT_SEL_CMD` to select the given profile, e.g. `&bt BT_SEL 1` | ## Bluetooth Behavior @@ -46,7 +46,7 @@ The bluetooth behavior completes an bluetooth action given on press. ### Behavior Binding - Reference: `&bt` -- Parameter #1: The bluetooth command define, e.g. `BT_CLEAR_BONDS_CMD` +- Parameter #1: The bluetooth command define, e.g. `BT_CLR_CMD` - Parameter #2: (Reserved for future bluetooth command types) ### Examples @@ -54,23 +54,23 @@ The bluetooth behavior completes an bluetooth action given on press. 1. Behavior binding to clear the paired host for the selected profile: ``` - &bt BT_CLEAR_BONDS + &bt BT_CLR ``` 1. Behavior binding to select the next profile: ``` - &bt BT_PROF_NEXT + &bt BT_NXT ``` 1. Behavior binding to select the previous profile: ``` - &bt BT_PROF_NEXT + &bt BT_NXT ``` 1. Behavior binding to select the 2nd profile (passed parameters are [zero based](https://en.wikipedia.org/wiki/Zero-based_numbering)): ``` - &bt BT_PROF_SEL 1 + &bt BT_SEL 1 ``` From 4658999e31865e54d02955c500c716385e6c69d8 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Sun, 13 Sep 2020 22:22:12 -0400 Subject: [PATCH 11/11] fix(bluetooth): Reject pairing to taken profiles. --- app/src/ble.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/ble.c b/app/src/ble.c index 40e05a3a..a2a8207a 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -366,7 +366,7 @@ static enum bt_security_err auth_pairing_accept(struct bt_conn *conn, const stru bt_conn_get_info(conn, &info); LOG_DBG("role %d, open? %s", info.role, active_profile_is_open() ? "yes" : "no"); - if (info.role != BT_CONN_ROLE_SLAVE && !active_profile_is_open()) { + if (info.role == BT_CONN_ROLE_SLAVE && !active_profile_is_open()) { LOG_WRN("Rejecting pairing request to taken profile %d", active_profile); return BT_SECURITY_ERR_PAIR_NOT_ALLOWED; }