Skip to content

Commit 7593213

Browse files
KKopyscinskicarlescufi
authored andcommitted
[nrf fromtree] Bluetooth: host: introduce timeout in limited advertising
In limited advertising advertising should end after certain timeout. Previously, limited advertising was just general advertising with BT_LE_AD_LIMITED flag set. Now, if this flag is set the work is scheduled, that will disable advertising after timeout. This affects tests GAP/DISC/LIMM/BV-03-C and GAP/DISC/LIMM/BV-04-C Signed-off-by: Krzysztof Kopyściński <[email protected]> (cherry picked from commit 4f80fe8) Signed-off-by: Martin Tverdal <[email protected]>
1 parent 9efd3d5 commit 7593213

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

subsys/bluetooth/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ config BT_BROADCASTER
106106

107107
endmenu
108108

109+
config BT_LIM_ADV_TIMEOUT
110+
int "Timeout for limited advertising in 1s units"
111+
default 30
112+
range 1 180
113+
help
114+
After this timeout is reached, advertisement with BT_LE_AD_LIMITED flag
115+
set shall be terminated. As per BT Core Spec 5.2, Vol 3, Part C,
116+
Appendix A (NORMATIVE): TIMERS AND CONSTANTS it's required to be no more
117+
than 180s.
118+
109119
config BT_EXT_ADV
110120
bool "Extended Advertising and Scanning support"
111121
help

subsys/bluetooth/host/adv.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,23 @@ static inline bool ad_has_name(const struct bt_data *ad, size_t ad_len)
430430
return false;
431431
}
432432

433+
static bool ad_is_limited(const struct bt_data *ad, size_t ad_len)
434+
{
435+
size_t i;
436+
437+
for (i = 0; i < ad_len; i++) {
438+
if (ad[i].type == BT_DATA_FLAGS &&
439+
ad[i].data_len == sizeof(uint8_t) &&
440+
ad[i].data != NULL) {
441+
if (ad[i].data[0] & BT_LE_AD_LIMITED) {
442+
return true;
443+
}
444+
}
445+
}
446+
447+
return false;
448+
}
449+
433450
static int le_adv_update(struct bt_le_ext_adv *adv,
434451
const struct bt_data *ad, size_t ad_len,
435452
const struct bt_data *sd, size_t sd_len,
@@ -978,6 +995,8 @@ int bt_le_adv_start_ext(struct bt_le_ext_adv *adv,
978995
return 0;
979996
}
980997

998+
static void adv_timeout(struct k_work *work);
999+
9811000
int bt_le_adv_start(const struct bt_le_adv_param *param,
9821001
const struct bt_data *ad, size_t ad_len,
9831002
const struct bt_data *sd, size_t sd_len)
@@ -1000,6 +1019,12 @@ int bt_le_adv_start(const struct bt_le_adv_param *param,
10001019
bt_le_adv_delete_legacy();
10011020
}
10021021

1022+
if (ad_is_limited(ad, ad_len)) {
1023+
k_work_init_delayable(&adv->timeout_work, adv_timeout);
1024+
k_work_reschedule(&adv->timeout_work,
1025+
K_SECONDS(CONFIG_BT_LIM_ADV_TIMEOUT));
1026+
}
1027+
10031028
return err;
10041029
}
10051030

@@ -1332,6 +1357,26 @@ int bt_le_ext_adv_delete(struct bt_le_ext_adv *adv)
13321357
#endif /* defined(CONFIG_BT_EXT_ADV) */
13331358

13341359

1360+
static void adv_timeout(struct k_work *work)
1361+
{
1362+
int err = 0;
1363+
#if defined(CONFIG_BT_EXT_ADV)
1364+
struct k_work_delayable *dwork;
1365+
struct bt_le_ext_adv *adv;
1366+
1367+
dwork = k_work_delayable_from_work(work);
1368+
adv = CONTAINER_OF(dwork, struct bt_le_ext_adv, timeout_work);
1369+
1370+
if (atomic_test_bit(adv->flags, BT_ADV_EXT_ADV)) {
1371+
err = bt_le_ext_adv_stop(adv);
1372+
}
1373+
#else
1374+
err = bt_le_adv_stop();
1375+
#endif
1376+
__ASSERT(err != 0, "Limited Advertising timeout reached, "
1377+
"failed to stop advertising");
1378+
}
1379+
13351380
#if defined(CONFIG_BT_PER_ADV)
13361381
int bt_le_per_adv_set_param(struct bt_le_ext_adv *adv,
13371382
const struct bt_le_per_adv_param *param)

subsys/bluetooth/host/hci_core.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ struct bt_le_ext_adv {
150150
/* TX Power in use by the controller */
151151
int8_t tx_power;
152152
#endif /* defined(CONFIG_BT_EXT_ADV) */
153+
154+
struct k_work_delayable timeout_work;
153155
};
154156

155157
enum {

0 commit comments

Comments
 (0)