Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ble adv timeout #2744

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/include/dt-bindings/zmk/bt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
1 change: 1 addition & 0 deletions app/include/zmk/ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion app/src/behaviors/behavior_bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down
69 changes: 58 additions & 11 deletions app/src/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -210,6 +222,23 @@ 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 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) {
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);
Expand All @@ -221,6 +250,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();
};

Expand All @@ -234,6 +266,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();
};

Expand Down Expand Up @@ -277,6 +312,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();
Expand Down Expand Up @@ -316,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) {
Expand Down Expand Up @@ -666,6 +710,9 @@ static void zmk_ble_ready(int err) {
return;
}

#if IS_ENABLED(CONFIG_ZMK_BLE_ADVERTISING_TIMEOUT)
advertising_timeout_timer_start();
#endif
update_advertising();
}

Expand Down