From 18e072b03270436fcd58312217fa8f362abb5ccd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Narajowski?= Date: Wed, 25 Aug 2021 14:29:08 +0200 Subject: [PATCH] [nrf fromtree] Bluetooth: host: Cancel limited adv timeout when advertising stopped MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Advertising might stop when: - it was stopped by application - device connected to a peer - extended advertising reached stop condition defined in BT_LE_EXT_ADV_START_PARAM - this is handled in ll Signed-off-by: MichaƂ Narajowski (cherry picked from commit a9db9a3aa80f2dcdd06809747b16568c5093acc7) Signed-off-by: Martin Tverdal --- subsys/bluetooth/host/adv.c | 18 ++++++++++++++---- subsys/bluetooth/host/adv.h | 1 + subsys/bluetooth/host/hci_core.c | 6 ++++++ subsys/bluetooth/host/hci_core.h | 2 +- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/subsys/bluetooth/host/adv.c b/subsys/bluetooth/host/adv.c index 43ebbe2e0b4..06e28bd9a2f 100644 --- a/subsys/bluetooth/host/adv.c +++ b/subsys/bluetooth/host/adv.c @@ -997,6 +997,11 @@ int bt_le_adv_start_ext(struct bt_le_ext_adv *adv, static void adv_timeout(struct k_work *work); +int bt_le_lim_adv_cancel_timeout(struct bt_le_ext_adv *adv) +{ + return k_work_cancel_delayable(&adv->lim_adv_timeout_work); +} + int bt_le_adv_start(const struct bt_le_adv_param *param, const struct bt_data *ad, size_t ad_len, const struct bt_data *sd, size_t sd_len) @@ -1020,8 +1025,8 @@ int bt_le_adv_start(const struct bt_le_adv_param *param, } if (ad_is_limited(ad, ad_len)) { - k_work_init_delayable(&adv->timeout_work, adv_timeout); - k_work_reschedule(&adv->timeout_work, + k_work_init_delayable(&adv->lim_adv_timeout_work, adv_timeout); + k_work_reschedule(&adv->lim_adv_timeout_work, K_SECONDS(CONFIG_BT_LIM_ADV_TIMEOUT)); } @@ -1033,6 +1038,8 @@ int bt_le_adv_stop(void) struct bt_le_ext_adv *adv = bt_le_adv_lookup_legacy(); int err; + (void)bt_le_lim_adv_cancel_timeout(adv); + if (!adv) { BT_ERR("No valid legacy adv"); return 0; @@ -1284,6 +1291,8 @@ int bt_le_ext_adv_start(struct bt_le_ext_adv *adv, int bt_le_ext_adv_stop(struct bt_le_ext_adv *adv) { + (void)bt_le_lim_adv_cancel_timeout(adv); + atomic_clear_bit(adv->flags, BT_ADV_PERSIST); if (!atomic_test_bit(adv->flags, BT_ADV_ENABLED)) { @@ -1360,13 +1369,13 @@ int bt_le_ext_adv_delete(struct bt_le_ext_adv *adv) static void adv_timeout(struct k_work *work) { int err = 0; -#if defined(CONFIG_BT_EXT_ADV) struct k_work_delayable *dwork; struct bt_le_ext_adv *adv; dwork = k_work_delayable_from_work(work); - adv = CONTAINER_OF(dwork, struct bt_le_ext_adv, timeout_work); + adv = CONTAINER_OF(dwork, struct bt_le_ext_adv, lim_adv_timeout_work); +#if defined(CONFIG_BT_EXT_ADV) if (adv == bt_dev.adv) { err = bt_le_adv_stop(); } else { @@ -1582,6 +1591,7 @@ void bt_hci_le_adv_set_terminated(struct net_buf *buf) adv = bt_adv_lookup_handle(evt->adv_handle); conn_handle = sys_le16_to_cpu(evt->conn_handle); + (void)bt_le_lim_adv_cancel_timeout(adv); #if (CONFIG_BT_ID_MAX > 1) && (CONFIG_BT_EXT_ADV_MAX_ADV_SET > 1) bt_dev.adv_conn_id = adv->id; for (int i = 0; i < ARRAY_SIZE(bt_dev.cached_conn_complete); i++) { diff --git a/subsys/bluetooth/host/adv.h b/subsys/bluetooth/host/adv.h index 28d7a626792..852f8678930 100644 --- a/subsys/bluetooth/host/adv.h +++ b/subsys/bluetooth/host/adv.h @@ -19,3 +19,4 @@ int bt_le_adv_set_enable_ext(struct bt_le_ext_adv *adv, bool enable, const struct bt_le_ext_adv_start_param *param); int bt_le_adv_set_enable_legacy(struct bt_le_ext_adv *adv, bool enable); +int bt_le_lim_adv_cancel_timeout(struct bt_le_ext_adv *adv); diff --git a/subsys/bluetooth/host/hci_core.c b/subsys/bluetooth/host/hci_core.c index d5abad64742..dae8b4b23ad 100644 --- a/subsys/bluetooth/host/hci_core.c +++ b/subsys/bluetooth/host/hci_core.c @@ -1323,6 +1323,12 @@ static void le_legacy_conn_complete(struct net_buf *buf) struct bt_hci_evt_le_conn_complete *evt = (void *)buf->data; struct bt_hci_evt_le_enh_conn_complete enh; +#if defined(CONFIG_BT_BROADCASTER) + struct bt_le_ext_adv *adv = bt_le_adv_lookup_legacy(); + + (void)bt_le_lim_adv_cancel_timeout(adv); +#endif + BT_DBG("status 0x%02x role %u %s", evt->status, evt->role, bt_addr_le_str(&evt->peer_addr)); diff --git a/subsys/bluetooth/host/hci_core.h b/subsys/bluetooth/host/hci_core.h index 89cd9419c65..c6525005a64 100644 --- a/subsys/bluetooth/host/hci_core.h +++ b/subsys/bluetooth/host/hci_core.h @@ -151,7 +151,7 @@ struct bt_le_ext_adv { int8_t tx_power; #endif /* defined(CONFIG_BT_EXT_ADV) */ - struct k_work_delayable timeout_work; + struct k_work_delayable lim_adv_timeout_work; }; enum {