From cf970efb98c5af97955bfffbcebb3b065b16edc4 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Fri, 28 Aug 2020 14:15:16 -0400 Subject: 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/src/ble.c | 335 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 306 insertions(+), 29 deletions(-) (limited to 'app/src/ble.c') diff --git a/app/src/ble.c b/app/src/ble.c index c4d3efd..97ff461 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) -- cgit v1.2.3 From fc0812bd2eb08d66819f38bafd1f5d00b933c87b Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Tue, 1 Sep 2020 23:22:30 -0400 Subject: 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/src/ble.c | 158 ++++++++-------------------------------------------------- 1 file changed, 22 insertions(+), 136 deletions(-) (limited to 'app/src/ble.c') diff --git a/app/src/ble.c b/app/src/ble.c index 97ff461..0aba16c 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_foreach_bond(active_identity, unpair_non_peripheral_bonds, NULL); + + bt_conn_foreach(BT_ID_DEFAULT, disconnect_host_connection, NULL); + bt_foreach_bond(BT_ID_DEFAULT, 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) -- cgit v1.2.3 From 13842a8a1e45b399864ba1ae5a759c614313b4db Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Thu, 3 Sep 2020 13:21:01 -0400 Subject: fix(bluetooth): Kconfig to clear bonds on start --- app/src/ble.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'app/src/ble.c') diff --git a/app/src/ble.c b/app/src/ble.c index 0aba16c..1e748ed 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); -- cgit v1.2.3 From b103eb4b059b60ebff81eb45db8405ed3a381257 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Thu, 3 Sep 2020 14:55:15 -0400 Subject: fix(bluetooth): Delete any previously stored name. --- app/src/ble.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'app/src/ble.c') diff --git a/app/src/ble.c b/app/src/ble.c index 1e748ed..f723762 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); } -- cgit v1.2.3 From e88d0833c5f8ddd2b8a9b93ab7b6d03c141f8463 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Thu, 3 Sep 2020 21:11:21 -0400 Subject: fix(bluetooth): Log when clearing on start. --- app/src/ble.c | 1 + 1 file changed, 1 insertion(+) (limited to 'app/src/ble.c') diff --git a/app/src/ble.c b/app/src/ble.c index f723762..7ef95ca 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++) { -- cgit v1.2.3 From 39f980a06dac1769e4f09abaf19d3ccbb4b34e67 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Tue, 8 Sep 2020 23:26:00 -0400 Subject: 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/src/ble.c | 238 ++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 175 insertions(+), 63 deletions(-) (limited to 'app/src/ble.c') diff --git a/app/src/ble.c b/app/src/ble.c index 7ef95ca..40e05a3 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) +int zmk_ble_clear_bonds() { - struct bt_conn_info info; - bt_conn_get_info(conn, &info); - - if (info.role != BT_CONN_ROLE_SLAVE) { - return; + LOG_DBG(""); + + 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); } - bt_conn_disconnect(conn, BT_HCI_ERR_LOCALHOST_TERM_CONN); + return 0; }; - -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; +int zmk_ble_prof_select(u8_t index) +{ + LOG_DBG("profile %d", index); + if (active_profile == index) { + return 0; } -#endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) */ - LOG_DBG("Unpairing %s", log_strdup(addr)); - bt_unpair(BT_ID_DEFAULT, &info->addr); -} + active_profile = index; + return settings_save_one("ble/active_profile", &active_profile, sizeof(active_profile)); -int zmk_ble_clear_bonds() + raise_profile_changed_event(); +}; + +int zmk_ble_prof_next() { LOG_DBG(""); - - bt_conn_foreach(BT_ID_DEFAULT, disconnect_host_connection, NULL); - bt_foreach_bond(BT_ID_DEFAULT, unpair_non_peripheral_bonds, NULL); + return zmk_ble_prof_select((active_profile + 1) % PROFILE_COUNT); +}; - return 0; +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); -- cgit v1.2.3 From 4658999e31865e54d02955c500c716385e6c69d8 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Sun, 13 Sep 2020 22:22:12 -0400 Subject: fix(bluetooth): Reject pairing to taken profiles. --- app/src/ble.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/src/ble.c') diff --git a/app/src/ble.c b/app/src/ble.c index 40e05a3..a2a8207 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; } -- cgit v1.2.3