From be9eb76067e70577eb61135b0fc43e490c061fec Mon Sep 17 00:00:00 2001 From: rate Date: Thu, 2 Jan 2025 09:57:57 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E3=81=8A=E8=A9=A6=E3=81=97=E5=AE=9F?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Kconfig | 12 ++++++++++ app/src/ble.c | 61 +++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 62 insertions(+), 11 deletions(-) diff --git a/app/Kconfig b/app/Kconfig index 971c4991c7c..47934cf76df 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -185,6 +185,18 @@ config BT_SMP_ALLOW_UNAUTH_OVERWRITE config BT_CTLR_PHY_2M default n if ZMK_BLE_EXPERIMENTAL_CONN +config ZMK_BLE_ADVERTISING_TIMEOUT + bool "Enable BLE advertising timeout" + default n + +if ZMK_BLE_ADVERTISING_TIMEOUT + +config ZMK_BLE_ADVERTISING_TIMEOUT_SECONDS + int "BLE advertising timeout in seconds" + default 30 + +endif + # BT_TINYCRYPT_ECC is required for BT_SMP_SC_PAIR_ONLY when using HCI config BT_TINYCRYPT_ECC default y if BT_HCI && !BT_CTLR diff --git a/app/src/ble.c b/app/src/ble.c index 02d8901b982..bdd3cd40414 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -161,24 +161,36 @@ bool zmk_ble_active_profile_is_connected(void) { } \ advertising_status = ZMK_ADV_CONN; +#if IS_ENABLED(CONFIG_ZMK_BLE_ADVERTISING_TIMEOUT) +static bool advertising_inhibit; +#endif + int update_advertising(void) { int err = 0; bt_addr_le_t *addr; struct bt_conn *conn; enum advertising_type desired_adv = ZMK_ADV_NONE; - if (zmk_ble_active_profile_is_open()) { - desired_adv = ZMK_ADV_CONN; - } else if (!zmk_ble_active_profile_is_connected()) { - desired_adv = ZMK_ADV_CONN; - // Need to fix directed advertising for privacy centrals. See - // https://github.com/zephyrproject-rtos/zephyr/pull/14984 char - // addr_str[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(zmk_ble_active_profile_addr(), addr_str, - // sizeof(addr_str)); - - // LOG_DBG("Directed advertising to %s", addr_str); - // desired_adv = ZMK_ADV_DIR; +#if IS_ENABLED(CONFIG_ZMK_BLE_ADVERTISING_TIMEOUT) + // If the advertising timeout is enabled, inhibit advertising if the timeout is active + if (!advertising_inhibit) { +#endif + if (zmk_ble_active_profile_is_open()) { + desired_adv = ZMK_ADV_CONN; + } else if (!zmk_ble_active_profile_is_connected()) { + desired_adv = ZMK_ADV_CONN; + // Need to fix directed advertising for privacy centrals. See + // https://github.com/zephyrproject-rtos/zephyr/pull/14984 char + // addr_str[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(zmk_ble_active_profile_addr(), + // addr_str, sizeof(addr_str)); + + // LOG_DBG("Directed advertising to %s", addr_str); + // desired_adv = ZMK_ADV_DIR; + } +#if IS_ENABLED(CONFIG_ZMK_BLE_ADVERTISING_TIMEOUT) } +#endif + LOG_DBG("advertising from %d to %d", advertising_status, desired_adv); switch (desired_adv + CURR_ADV(advertising_status)) { @@ -210,6 +222,21 @@ static void update_advertising_callback(struct k_work *work) { update_advertisin K_WORK_DEFINE(update_advertising_work, update_advertising_callback); +#if IS_ENABLED(CONFIG_ZMK_BLE_ADVERTISING_TIMEOUT) +void advertising_timeout(struct k_timer *timer) { + advertising_inhibit = true; + k_work_submit(&update_advertising_work); +} + +K_TIMER_DEFINE(advertising_timer, advertising_timeout, NULL); + +void advertising_timeout_timer_start(void) { + advertising_inhibit = false; + k_timer_start(&advertising_timer, K_SECONDS(CONFIG_ZMK_BLE_ADVERTISING_TIMEOUT_SECONDS), + K_NO_WAIT); +} +#endif + static void clear_profile_bond(uint8_t profile) { if (bt_addr_le_cmp(&profiles[profile].peer, BT_ADDR_LE_ANY)) { bt_unpair(BT_ID_DEFAULT, &profiles[profile].peer); @@ -221,6 +248,9 @@ void zmk_ble_clear_bonds(void) { LOG_DBG("zmk_ble_clear_bonds()"); clear_profile_bond(active_profile); +#if IS_ENABLED(CONFIG_ZMK_BLE_ADVERTISING_TIMEOUT) + advertising_timeout_timer_start(); +#endif update_advertising(); }; @@ -234,6 +264,9 @@ void zmk_ble_clear_all_bonds(void) { // Automatically switch to profile 0 zmk_ble_prof_select(0); +#if IS_ENABLED(CONFIG_ZMK_BLE_ADVERTISING_TIMEOUT) + advertising_timeout_timer_start(); +#endif update_advertising(); }; @@ -277,6 +310,9 @@ int zmk_ble_prof_select(uint8_t index) { active_profile = index; ble_save_profile(); +#if IS_ENABLED(CONFIG_ZMK_BLE_ADVERTISING_TIMEOUT) + advertising_timeout_timer_start(); +#endif update_advertising(); raise_profile_changed_event(); @@ -666,6 +702,9 @@ static void zmk_ble_ready(int err) { return; } +#if IS_ENABLED(CONFIG_ZMK_BLE_ADVERTISING_TIMEOUT) + advertising_timeout_timer_start(); +#endif update_advertising(); } From dbc911b98c6630d0c8491d52918a6488a0f6ebba Mon Sep 17 00:00:00 2001 From: rate Date: Thu, 2 Jan 2025 21:56:54 +0000 Subject: [PATCH 2/2] feat: Added stop advertise --- app/include/dt-bindings/zmk/bt.h | 2 ++ app/include/zmk/ble.h | 1 + app/src/behaviors/behavior_bt.c | 9 ++++++++- app/src/ble.c | 10 +++++++++- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/include/dt-bindings/zmk/bt.h b/app/include/dt-bindings/zmk/bt.h index aaad4dc5b8b..3797d332def 100644 --- a/app/include/dt-bindings/zmk/bt.h +++ b/app/include/dt-bindings/zmk/bt.h @@ -10,6 +10,7 @@ #define BT_SEL_CMD 3 #define BT_CLR_ALL_CMD 4 #define BT_DISC_CMD 5 +#define BT_STOP_ADV_CMD 6 /* Note: Some future commands will include additional parameters, so we @@ -22,3 +23,4 @@ defines these aliases up front. #define BT_SEL BT_SEL_CMD #define BT_CLR_ALL BT_CLR_ALL_CMD 0 #define BT_DISC BT_DISC_CMD +#define BT_STOP_ADV BT_STOP_ADV_CMD 0 diff --git a/app/include/zmk/ble.h b/app/include/zmk/ble.h index c44c5e16869..85b4785ebb9 100644 --- a/app/include/zmk/ble.h +++ b/app/include/zmk/ble.h @@ -26,6 +26,7 @@ int zmk_ble_prof_prev(void); int zmk_ble_prof_select(uint8_t index); void zmk_ble_clear_all_bonds(void); int zmk_ble_prof_disconnect(uint8_t index); +void zmk_ble_stop_advertise(void); int zmk_ble_active_profile_index(void); int zmk_ble_profile_index(const bt_addr_le_t *addr); diff --git a/app/src/behaviors/behavior_bt.c b/app/src/behaviors/behavior_bt.c index f439e49b1cf..34a99186b00 100644 --- a/app/src/behaviors/behavior_bt.c +++ b/app/src/behaviors/behavior_bt.c @@ -43,7 +43,11 @@ static const struct behavior_parameter_value_metadata no_arg_values[] = { .type = BEHAVIOR_PARAMETER_VALUE_TYPE_VALUE, .value = BT_CLR_CMD, }, -}; + { + .display_name = "Stop Advertising", + .type = BEHAVIOR_PARAMETER_VALUE_TYPE_VALUE, + .value = BT_STOP_ADV_CMD, + }}; static const struct behavior_parameter_metadata_set no_args_set = { .param1_values = no_arg_values, @@ -105,6 +109,9 @@ static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, return 0; case BT_DISC_CMD: return zmk_ble_prof_disconnect(binding->param2); + case BT_STOP_ADV_CMD: + zmk_ble_stop_advertise(); + return 0; default: LOG_ERR("Unknown BT command: %d", binding->param1); } diff --git a/app/src/ble.c b/app/src/ble.c index bdd3cd40414..5c57fe6526e 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -223,11 +223,13 @@ static void update_advertising_callback(struct k_work *work) { update_advertisin K_WORK_DEFINE(update_advertising_work, update_advertising_callback); #if IS_ENABLED(CONFIG_ZMK_BLE_ADVERTISING_TIMEOUT) -void advertising_timeout(struct k_timer *timer) { +void set_advertising_inhibit(void) { advertising_inhibit = true; k_work_submit(&update_advertising_work); } +void advertising_timeout(struct k_timer *timer) { set_advertising_inhibit(); } + K_TIMER_DEFINE(advertising_timer, advertising_timeout, NULL); void advertising_timeout_timer_start(void) { @@ -352,6 +354,12 @@ int zmk_ble_prof_disconnect(uint8_t index) { return result; } +void zmk_ble_stop_advertise(void) { +#if IS_ENABLED(CONFIG_ZMK_BLE_ADVERTISING_TIMEOUT) + set_advertising_inhibit(); +#endif +} + bt_addr_le_t *zmk_ble_active_profile_addr(void) { return &profiles[active_profile].peer; } struct bt_conn *zmk_ble_active_profile_conn(void) {