Skip to content

Commit 3c717d8

Browse files
committed
samples: Bluetooth: Broadcast multiple legacy and extended advertising
Update broadcaster_multiple sample to start multiple advertising sets of type legacy and extended advertising. Signed-off-by: Vinayak Kariappa Chettimada <[email protected]>
1 parent b46070f commit 3c717d8

File tree

7 files changed

+180
-49
lines changed

7 files changed

+180
-49
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Increased to 4 gives one each of legacy, 2M, 1M and Coded PHY advertising sets
2+
CONFIG_BT_EXT_ADV_MAX_ADV_SET=4
3+
4+
# Use Zephyr Bluetooth Low Energy Controller
5+
CONFIG_BT_LL_SW_SPLIT=y
6+
7+
# Enable Coded PHY support in Zephyr Controller, if testing 4 advertising sets
8+
CONFIG_BT_CTLR_PHY_CODED=y
9+
10+
# Zephyr Bluetooth LE Controller will need to use chain PDUs when AD data
11+
# length > 191 bytes
12+
# - 31 bytes will use 22 bytes for the default name in this sample plus 9 bytes
13+
# for manufacturer data
14+
# - 191 bytes will use 22 bytes for the default name in this sample plus 169
15+
# bytes for manufacturer data
16+
# - 277 bytes will use 22 bytes for the default name in this sample plus 255
17+
# bytes for manufacturer data
18+
CONFIG_BT_CTLR_ADV_DATA_LEN_MAX=192
19+
20+
# Increase Advertising PDU buffers to number of advertising sets times the
21+
# number of chain PDUs per advertising set when using Zephyr Bluetooth LE
22+
# Controller
23+
CONFIG_BT_CTLR_ADVANCED_FEATURES=y
24+
CONFIG_BT_CTLR_ADV_RESERVE_MAX=n
25+
CONFIG_BT_CTLR_ADV_DATA_BUF_MAX=6
26+
CONFIG_BT_CTLR_ADV_DATA_CHAIN=y
Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
11
CONFIG_BT=y
22
CONFIG_BT_BROADCASTER=y
3-
CONFIG_BT_EXT_ADV=y
4-
CONFIG_BT_EXT_ADV_MAX_ADV_SET=2
53
CONFIG_BT_DEVICE_NAME="Broadcaster Multiple"
6-
74
CONFIG_LOG=y
85

9-
# Zephyr Bluetooth LE Controller will need to use chain PDUs when AD data
10-
# length > 191 bytes
11-
# - 31 bytes will use 22 bytes for the default name in this sample plus 9 bytes
12-
# for manufacturer data
13-
# - 191 bytes will use 22 bytes for the default name in this sample plus 169
14-
# bytes for manufacturer data
15-
# - 277 bytes will use 22 bytes for the default name in this sample plus 255
16-
# bytes for manufacturer data
17-
# CONFIG_BT_CTLR_ADV_DATA_LEN_MAX=192
6+
# Require Bluetooth Advertising Extensions Feature
7+
CONFIG_BT_EXT_ADV=y
188

19-
# Increase Advertising PDU buffers to number of advertising sets times the
20-
# number of chain PDUs per advertising set when using Zephyr Bluetooth LE
21-
# Controller
22-
# CONFIG_BT_CTLR_ADVANCED_FEATURES=y
23-
# CONFIG_BT_CTLR_ADV_DATA_BUF_MAX=2
9+
# Increased to 4 gives one each of legacy, 2M, 1M and Coded PHY advertising sets
10+
CONFIG_BT_EXT_ADV_MAX_ADV_SET=2
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
sample:
22
name: Bluetooth Multiple Extended Advertising Broadcaster
33
tests:
4-
sample.bluetooth.multiple_broadcast:
4+
sample.bluetooth.broadcaster_multiple:
55
harness: bluetooth
6+
tags: bluetooth
67
platform_allow:
78
- qemu_cortex_m3
89
- qemu_x86
10+
integration_platforms:
11+
- qemu_cortex_m3
12+
sample.bluetooth.broadcaster_multiple.bt_ll_sw_split:
13+
harness: bluetooth
14+
tags: bluetooth
15+
extra_args:
16+
- EXTRA_CONF_FILE=overlay-bt_ll_sw_split.conf
17+
platform_allow:
918
- nrf51dk/nrf51822
1019
- nrf52_bsim
1120
- nrf52dk/nrf52832
12-
tags: bluetooth
21+
- nrf52840dk/nrf52840
1322
integration_platforms:
14-
- qemu_cortex_m3
23+
- nrf52dk/nrf52832
24+
- nrf52840dk/nrf52840

samples/bluetooth/broadcaster_multiple/src/broadcaster_multiple.c

Lines changed: 69 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,50 +54,105 @@
5454
*/
5555
static uint8_t mfg_data[BT_MFG_DATA_LEN] = { 0xFF, 0xFF, };
5656

57-
static const struct bt_data ad[] = {
57+
static const struct bt_data ad_long[] = {
5858
BT_DATA(BT_DATA_MANUFACTURER_DATA, mfg_data, sizeof(mfg_data)),
5959
#if CONFIG_BT_CTLR_ADV_DATA_LEN_MAX > 255
6060
BT_DATA(BT_DATA_MANUFACTURER_DATA, mfg_data, sizeof(mfg_data)),
6161
#endif
6262
BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
6363
};
6464

65+
static const struct bt_data ad_short[] = {
66+
BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
67+
};
68+
6569
static struct bt_le_ext_adv *adv[CONFIG_BT_EXT_ADV_MAX_ADV_SET];
6670

6771
int broadcaster_multiple(void)
6872
{
69-
struct bt_le_adv_param adv_param = {
70-
.id = BT_ID_DEFAULT,
71-
.sid = 0U, /* Supply unique SID when creating advertising set */
72-
.secondary_max_skip = 0U,
73-
.options = BT_LE_ADV_OPT_EXT_ADV,
74-
.interval_min = BT_GAP_ADV_FAST_INT_MIN_2,
75-
.interval_max = BT_GAP_ADV_FAST_INT_MAX_2,
76-
.peer = NULL,
77-
};
7873
int err;
7974

75+
/* Create Advertising Sets */
8076
for (int index = 0; index < CONFIG_BT_EXT_ADV_MAX_ADV_SET; index++) {
77+
struct bt_le_adv_param adv_param = {
78+
.id = BT_ID_DEFAULT,
79+
.sid = 0U, /* Supply unique SID when creating advertising set */
80+
.secondary_max_skip = 0U,
81+
.options = BT_LE_ADV_OPT_EXT_ADV,
82+
.interval_min = BT_GAP_ADV_FAST_INT_MIN_2,
83+
.interval_max = BT_GAP_ADV_FAST_INT_MAX_2,
84+
.peer = NULL,
85+
};
86+
const struct adv_param_config {
87+
uint32_t options;
88+
const struct bt_data *ad;
89+
size_t ad_size;
90+
} param_config[] = {
91+
/* Use 1M legacy PDU */
92+
{.options = BT_LE_ADV_OPT_NONE,
93+
.ad = ad_short,
94+
.ad_size = ARRAY_SIZE(ad_short),
95+
},
96+
/* Use 2M auxiliary PDU */
97+
{.options = BT_LE_ADV_OPT_EXT_ADV,
98+
.ad = ad_long,
99+
.ad_size = ARRAY_SIZE(ad_long),
100+
},
101+
/* Use 1M auxiliary PDU */
102+
{.options = BT_LE_ADV_OPT_EXT_ADV | BT_LE_ADV_OPT_NO_2M,
103+
.ad = ad_long,
104+
.ad_size = ARRAY_SIZE(ad_long),
105+
},
106+
/* Use Coded PHY */
107+
{.options = BT_LE_ADV_OPT_EXT_ADV | BT_LE_ADV_OPT_CODED,
108+
.ad = ad_long,
109+
.ad_size = ARRAY_SIZE(ad_long),
110+
},
111+
};
112+
const struct bt_data *ad;
113+
size_t ad_size;
114+
81115
/* Use advertising set instance index as SID */
82116
adv_param.sid = index;
83117

118+
/* Advertising set options, AD and AD array size */
119+
const struct adv_param_config *config =
120+
&param_config[index % ARRAY_SIZE(param_config)];
121+
122+
adv_param.options = config->options;
123+
ad = config->ad;
124+
ad_size = config->ad_size;
125+
126+
ext_adv_create_retry:
84127
/* Create a non-connectable advertising set */
85128
err = bt_le_ext_adv_create(&adv_param, NULL, &adv[index]);
86129
if (err) {
87-
printk("Failed to create advertising set %d (err %d)\n",
88-
index, err);
130+
/* Failed creating Coded PHY advertising set? */
131+
if ((adv_param.options & BT_LE_ADV_OPT_CODED) != 0U) {
132+
printk("Failed to create advertising set %d with Coded PHY "
133+
"(err %d), retry without...\n", index, err);
134+
135+
/* Retry with non-Coded PHY advertising set */
136+
adv_param.options &= ~BT_LE_ADV_OPT_CODED;
137+
138+
goto ext_adv_create_retry;
139+
}
140+
141+
printk("Failed to create advertising set %d (err %d)\n", index, err);
89142
return err;
90143
}
91144

92145
/* Set extended advertising data */
93-
err = bt_le_ext_adv_set_data(adv[index], ad, ARRAY_SIZE(ad),
94-
NULL, 0);
146+
err = bt_le_ext_adv_set_data(adv[index], ad, ad_size, NULL, 0);
95147
if (err) {
96148
printk("Failed to set advertising data for set %d "
97149
"(err %d)\n", index, err);
98150
return err;
99151
}
152+
}
100153

154+
/* Start Advertising Sets */
155+
for (int index = 0; index < CONFIG_BT_EXT_ADV_MAX_ADV_SET; index++) {
101156
/* Start extended advertising set */
102157
err = bt_le_ext_adv_start(adv[index],
103158
BT_LE_EXT_ADV_START_DEFAULT);

samples/bluetooth/observer/src/observer.c

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#include <zephyr/bluetooth/bluetooth.h>
1010
#include <zephyr/bluetooth/hci.h>
1111

12-
#define NAME_LEN 30
13-
1412
static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
1513
struct net_buf_simple *ad)
1614
{
@@ -22,6 +20,8 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
2220
}
2321

2422
#if defined(CONFIG_BT_EXT_ADV)
23+
#define NAME_LEN 30
24+
2525
static bool data_cb(struct bt_data *data, void *user_data)
2626
{
2727
char *name = user_data;
@@ -86,27 +86,56 @@ static struct bt_le_scan_cb scan_callbacks = {
8686
};
8787
#endif /* CONFIG_BT_EXT_ADV */
8888

89-
int observer_start(void)
89+
static int scan_start(void)
9090
{
9191
struct bt_le_scan_param scan_param = {
92-
.type = BT_LE_SCAN_TYPE_PASSIVE,
92+
.type = BT_LE_SCAN_TYPE_ACTIVE,
9393
.options = BT_LE_SCAN_OPT_FILTER_DUPLICATE,
9494
.interval = BT_GAP_SCAN_FAST_INTERVAL,
9595
.window = BT_GAP_SCAN_FAST_WINDOW,
9696
};
9797
int err;
9898

9999
#if defined(CONFIG_BT_EXT_ADV)
100-
bt_le_scan_cb_register(&scan_callbacks);
101-
printk("Registered scan callbacks\n");
100+
scan_param.options |= BT_LE_SCAN_OPT_CODED;
102101
#endif /* CONFIG_BT_EXT_ADV */
103102

103+
scan_start_retry:
104+
printk("Starting scanning...\n");
104105
err = bt_le_scan_start(&scan_param, device_found);
105106
if (err) {
107+
if ((scan_param.options & BT_LE_SCAN_OPT_CODED) != 0U) {
108+
printk("Failed to start scanning with Coded PHY (err %d), retrying "
109+
"without...\n", err);
110+
111+
scan_param.options &= ~BT_LE_SCAN_OPT_CODED;
112+
113+
goto scan_start_retry;
114+
}
115+
106116
printk("Start scanning failed (err %d)\n", err);
117+
118+
return err;
119+
}
120+
121+
printk("success.\n");
122+
123+
return 0;
124+
}
125+
126+
int observer_start(void)
127+
{
128+
int err;
129+
130+
#if defined(CONFIG_BT_EXT_ADV)
131+
bt_le_scan_cb_register(&scan_callbacks);
132+
printk("Registered scan callbacks\n");
133+
#endif /* CONFIG_BT_EXT_ADV */
134+
135+
err = scan_start();
136+
if (err != 0) {
107137
return err;
108138
}
109-
printk("Started scanning...\n");
110139

111140
return 0;
112141
}

tests/bsim/bluetooth/host/adv/chain/prj.conf

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
CONFIG_BT=y
2+
3+
# Broadcaster
24
CONFIG_BT_BROADCASTER=y
3-
CONFIG_BT_OBSERVER=y
45
CONFIG_BT_EXT_ADV=y
5-
CONFIG_BT_EXT_ADV_MAX_ADV_SET=2
6+
CONFIG_BT_EXT_ADV_MAX_ADV_SET=4
67
CONFIG_BT_DEVICE_NAME="Broadcaster Multiple"
78

89
# Enable Advertising Data chaining in Zephyr Bluetooth LE Controller
910
CONFIG_BT_CTLR_ADVANCED_FEATURES=y
11+
CONFIG_BT_CTLR_ADV_RESERVE_MAX=n
1012
CONFIG_BT_CTLR_ADV_DATA_CHAIN=y
1113

1214
# Zephyr Bluetooth LE Controller will need to use chain PDUs when AD data
@@ -22,19 +24,36 @@ CONFIG_BT_CTLR_ADV_DATA_LEN_MAX=1650
2224
# Increase Advertising PDU buffers to number of advertising sets times the
2325
# number of chain PDUs per advertising set when using Zephyr Bluetooth LE
2426
# Controller
25-
CONFIG_BT_CTLR_ADV_DATA_BUF_MAX=2
27+
CONFIG_BT_CTLR_ADV_DATA_BUF_MAX=6
28+
29+
# Enable Coded PHY
30+
CONFIG_BT_CTLR_PHY_CODED=y
31+
32+
# Observer
33+
CONFIG_BT_OBSERVER=y
34+
CONFIG_BT_EXT_ADV=y
2635

2736
# Maximum Extended Scanning buffer size
2837
CONFIG_BT_EXT_SCAN_BUF_SIZE=1650
2938

30-
# Set maximum scan data length for Extended Scanning in Bluetooth LE Controller
31-
CONFIG_BT_CTLR_SCAN_DATA_LEN_MAX=1650
32-
3339
# The Zephyr Controller does not combine all the 1650 bytes before
3440
# fragmenting into 8 HCI reports, if a PDU has 255 bytes,
3541
# it will generate 2 HCI reports and so we need to reserve 16 buffers
3642
CONFIG_BT_BUF_EVT_RX_COUNT=16
3743

44+
# Set maximum scan data length for Extended Scanning in Bluetooth LE Controller
45+
CONFIG_BT_CTLR_SCAN_DATA_LEN_MAX=1650
46+
3847
# Increase Zephyr Bluetooth LE Controller Rx buffer to receive complete chain
3948
# of PDUs
4049
CONFIG_BT_CTLR_RX_BUFFERS=9
50+
51+
# Enable scanning interleaved extended advertising in Zephyr Bluetooth LE
52+
# Controller
53+
CONFIG_BT_CTLR_ADVANCED_FEATURES=y
54+
CONFIG_BT_CTLR_SCAN_AUX_SET=3
55+
CONFIG_BT_CTLR_LOW_LAT_ULL=y
56+
# CONFIG_BT_CTLR_SCAN_AUX_USE_CHAINS=y
57+
# CONFIG_BT_CTLR_SCAN_AUX_CHAIN_COUNT=3
58+
CONFIG_BT_CTLR_SCAN_UNRESERVED=y
59+
# CONFIG_BT_TICKER_EXT_EXPIRE_INFO=y

tests/bsim/bluetooth/host/adv/chain/src/main.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
BT_AD_DATA_MFG_DATA_SIZE + BT_AD_DATA_MFG_DATA_SIZE), \
2828
CONFIG_BT_CTLR_ADV_DATA_LEN_MAX)
2929

30+
/* One less extended advertising set as first one is legacy advertising in the broadcaster_multiple
31+
* sample.
32+
*/
33+
#define BT_EXT_ADV_MAX_ADV_SET (CONFIG_BT_EXT_ADV_MAX_ADV_SET - 1)
34+
3035
static K_SEM_DEFINE(sem_recv, 0, 1);
3136

3237
static void test_adv_main(void)
@@ -76,7 +81,7 @@ static bool data_cb(struct bt_data *data, void *user_data)
7681
static void scan_recv(const struct bt_le_scan_recv_info *info,
7782
struct net_buf_simple *buf)
7883
{
79-
static uint8_t sid[CONFIG_BT_EXT_ADV_MAX_ADV_SET];
84+
static uint8_t sid[BT_EXT_ADV_MAX_ADV_SET];
8085
static uint8_t sid_count;
8186
char name[NAME_LEN];
8287
uint8_t data_status;
@@ -110,7 +115,7 @@ static void scan_recv(const struct bt_le_scan_recv_info *info,
110115

111116
sid[sid_count++] = info->sid;
112117

113-
if (sid_count < CONFIG_BT_EXT_ADV_MAX_ADV_SET) {
118+
if (sid_count < BT_EXT_ADV_MAX_ADV_SET) {
114119
printk("Received advertising sets: %d\n", sid_count);
115120
return;
116121
}

0 commit comments

Comments
 (0)