Skip to content

Commit

Permalink
[nrf fromtree] Bluetooth: host: Cancel limited adv timeout when adver…
Browse files Browse the repository at this point in the history
…tising stopped

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 <[email protected]>
(cherry picked from commit a9db9a3)
Signed-off-by: Martin Tverdal <[email protected]>
  • Loading branch information
Michał Narajowski authored and carlescufi committed Oct 26, 2021
1 parent 553ff26 commit 18e072b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
18 changes: 14 additions & 4 deletions subsys/bluetooth/host/adv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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));
}

Expand All @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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++) {
Expand Down
1 change: 1 addition & 0 deletions subsys/bluetooth/host/adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
6 changes: 6 additions & 0 deletions subsys/bluetooth/host/hci_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
2 changes: 1 addition & 1 deletion subsys/bluetooth/host/hci_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 18e072b

Please sign in to comment.