Skip to content

Commit 52eeb6a

Browse files
maje-embnordic-piks
authored andcommitted
samples: bluetooth: Fix resume advertising
Making Bluetooth API calls in the `recycled` callback context is error-prone. The changes add the use of workqueue to resume advertising. Signed-off-by: Marcin Jelinski <[email protected]>
1 parent f3951ce commit 52eeb6a

File tree

11 files changed

+89
-12
lines changed

11 files changed

+89
-12
lines changed

samples/bluetooth/central_and_peripheral_hr/src/main.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ K_MSGQ_DEFINE(hrs_queue, sizeof(struct bt_hrs_client_measurement), HRS_QUEUE_SIZ
5050

5151
static struct bt_hrs_client hrs_c;
5252
static struct bt_conn *central_conn;
53+
static struct k_work adv_work;
5354

5455
static const char * const sensor_location_str[] = {
5556
"Other",
@@ -219,7 +220,7 @@ static int scan_start(void)
219220
return err;
220221
}
221222

222-
static void advertising_start(void)
223+
static void adv_work_handler(struct k_work *work)
223224
{
224225
int err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_2, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
225226

@@ -231,6 +232,11 @@ static void advertising_start(void)
231232
printk("Advertising successfully started\n");
232233
}
233234

235+
static void advertising_start(void)
236+
{
237+
k_work_submit(&adv_work);
238+
}
239+
234240
static void connected(struct bt_conn *conn, uint8_t conn_err)
235241
{
236242
int err;
@@ -444,6 +450,7 @@ int main(void)
444450

445451
printk("Scanning started\n");
446452

453+
k_work_init(&adv_work, adv_work_handler);
447454
advertising_start();
448455

449456
for (;;) {

samples/bluetooth/peripheral_bms/src/main.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* In hex: {0x41, 0x42, 0x43, 0x44}
3939
*/
4040
static const uint8_t bms_auth_code[] = {'A', 'B', 'C', 'D'};
41+
static struct k_work adv_work;
4142

4243
static const struct bt_data ad[] = {
4344
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
@@ -48,7 +49,7 @@ static const struct bt_data sd[] = {
4849
BT_DATA_BYTES(BT_DATA_UUID16_ALL, BT_UUID_16_ENCODE(BT_UUID_BMS_VAL)),
4950
};
5051

51-
static void advertising_start(void)
52+
static void adv_work_handler(struct k_work *work)
5253
{
5354
int err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_2, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
5455

@@ -60,6 +61,11 @@ static void advertising_start(void)
6061
printk("Advertising successfully started\n");
6162
}
6263

64+
static void advertising_start(void)
65+
{
66+
k_work_submit(&adv_work);
67+
}
68+
6369
static void connected(struct bt_conn *conn, uint8_t err)
6470
{
6571
if (err) {
@@ -236,6 +242,7 @@ int main(void)
236242
return 0;
237243
}
238244

245+
k_work_init(&adv_work, adv_work_handler);
239246
advertising_start();
240247

241248
for (;;) {

samples/bluetooth/peripheral_cgms/src/main.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define APP_GLUCOSE_STEP 0.1f
2727

2828
static bool session_state;
29+
static struct k_work adv_work;
2930

3031
static const struct bt_data ad[] = {
3132
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
@@ -38,7 +39,7 @@ static const struct bt_data sd[] = {
3839
BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
3940
};
4041

41-
static void advertising_start(void)
42+
static void adv_work_handler(struct k_work *work)
4243
{
4344
int err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_2, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
4445

@@ -50,6 +51,11 @@ static void advertising_start(void)
5051
printk("Advertising successfully started\n");
5152
}
5253

54+
static void advertising_start(void)
55+
{
56+
k_work_submit(&adv_work);
57+
}
58+
5359
static void connected(struct bt_conn *conn, uint8_t err)
5460
{
5561
char addr[BT_ADDR_LE_STR_LEN];
@@ -171,6 +177,7 @@ int main(void)
171177
return 0;
172178
}
173179

180+
k_work_init(&adv_work, adv_work_handler);
174181
advertising_start();
175182

176183
/* Submit the measured glucose result in main loop. */

samples/bluetooth/peripheral_cts_client/src/main.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#define KEY_READ_TIME DK_BTN1_MSK
3131

3232
static struct bt_cts_client cts_c;
33-
33+
static struct k_work adv_work;
3434
static bool has_cts;
3535

3636
static const struct bt_data ad[] = {
@@ -163,7 +163,7 @@ static const struct bt_gatt_dm_cb discover_cb = {
163163
};
164164

165165

166-
static void advertising_start(void)
166+
static void adv_work_handler(struct k_work *work)
167167
{
168168
int err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_2, ad, ARRAY_SIZE(ad), NULL, 0);
169169

@@ -175,6 +175,11 @@ static void advertising_start(void)
175175
printk("Advertising successfully started\n");
176176
}
177177

178+
static void advertising_start(void)
179+
{
180+
k_work_submit(&adv_work);
181+
}
182+
178183
static void connected(struct bt_conn *conn, uint8_t err)
179184
{
180185
char addr[BT_ADDR_LE_STR_LEN];
@@ -366,6 +371,7 @@ int main(void)
366371
return 0;
367372
}
368373

374+
k_work_init(&adv_work, adv_work_handler);
369375
advertising_start();
370376

371377
for (;;) {

samples/bluetooth/peripheral_gatt_dm/src/main.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#define KEY_PAIRING_ACCEPT DK_BTN1_MSK
3030
#define KEY_PAIRING_REJECT DK_BTN2_MSK
3131

32+
static struct k_work adv_work;
33+
3234
static const struct bt_data ad[] = {
3335
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
3436
BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
@@ -71,7 +73,7 @@ static struct bt_gatt_dm_cb discover_all_cb = {
7173
.error_found = discover_all_error_found,
7274
};
7375

74-
static void advertising_start(void)
76+
static void adv_work_handler(struct k_work *work)
7577
{
7678
int err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_2, ad, ARRAY_SIZE(ad), NULL, 0);
7779

@@ -83,6 +85,11 @@ static void advertising_start(void)
8385
printk("Advertising successfully started\n");
8486
}
8587

88+
static void advertising_start(void)
89+
{
90+
k_work_submit(&adv_work);
91+
}
92+
8693
static void connected(struct bt_conn *conn, uint8_t err)
8794
{
8895
char addr[BT_ADDR_LE_STR_LEN];
@@ -197,6 +204,7 @@ int main(void)
197204
return 0;
198205
}
199206

207+
k_work_init(&adv_work, adv_work_handler);
200208
advertising_start();
201209

202210
return 0;

samples/bluetooth/peripheral_lbs/src/main.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#define USER_BUTTON DK_BTN1_MSK
4040

4141
static bool app_button_state;
42+
static struct k_work adv_work;
4243

4344
static const struct bt_data ad[] = {
4445
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
@@ -49,7 +50,7 @@ static const struct bt_data sd[] = {
4950
BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_LBS_VAL),
5051
};
5152

52-
static void advertising_start(void)
53+
static void adv_work_handler(struct k_work *work)
5354
{
5455
int err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_2, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
5556

@@ -61,6 +62,11 @@ static void advertising_start(void)
6162
printk("Advertising successfully started\n");
6263
}
6364

65+
static void advertising_start(void)
66+
{
67+
k_work_submit(&adv_work);
68+
}
69+
6470
static void connected(struct bt_conn *conn, uint8_t err)
6571
{
6672
if (err) {
@@ -252,6 +258,7 @@ int main(void)
252258
return 0;
253259
}
254260

261+
k_work_init(&adv_work, adv_work_handler);
255262
advertising_start();
256263

257264
for (;;) {

samples/bluetooth/peripheral_mds/src/main.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ static const struct bt_data sd[] = {
3838
};
3939

4040
static struct bt_conn *mds_conn;
41+
static struct k_work adv_work;
4142

4243
static void bas_work_handler(struct k_work *work);
4344

@@ -64,7 +65,7 @@ static void security_changed(struct bt_conn *conn, bt_security_t level,
6465
}
6566
}
6667

67-
static void advertising_start(void)
68+
static void adv_work_handler(struct k_work *work)
6869
{
6970
int err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_2, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
7071

@@ -76,6 +77,11 @@ static void advertising_start(void)
7677
printk("Advertising successfully started\n");
7778
}
7879

80+
static void advertising_start(void)
81+
{
82+
k_work_submit(&adv_work);
83+
}
84+
7985
static void connected(struct bt_conn *conn, uint8_t conn_err)
8086
{
8187
char addr[BT_ADDR_LE_STR_LEN];
@@ -302,6 +308,7 @@ int main(void)
302308
}
303309
}
304310

311+
k_work_init(&adv_work, adv_work_handler);
305312
advertising_start();
306313

307314
k_work_schedule(&bas_work, K_SECONDS(1));

samples/bluetooth/peripheral_rscs/src/main.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
static struct bt_rscs_measurement measurement;
3232
static struct bt_conn *current_conn;
33+
static struct k_work adv_work;
3334

3435
static const struct bt_data ad[] = {
3536
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
@@ -123,7 +124,7 @@ static const struct bt_rscs_cp_cb control_point_cb = {
123124
.update_loc = update_loc,
124125
};
125126

126-
static void advertising_start(void)
127+
static void adv_work_handler(struct k_work *work)
127128
{
128129
int err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_2, ad, ARRAY_SIZE(ad), NULL, 0);
129130

@@ -135,6 +136,11 @@ static void advertising_start(void)
135136
printk("Advertising successfully started\n");
136137
}
137138

139+
static void advertising_start(void)
140+
{
141+
k_work_submit(&adv_work);
142+
}
143+
138144
static void connected(struct bt_conn *conn, uint8_t err)
139145
{
140146
if (err) {
@@ -306,6 +312,7 @@ int main(void)
306312
return 0;
307313
}
308314

315+
k_work_init(&adv_work, adv_work_handler);
309316
advertising_start();
310317

311318
for (;;) {

samples/bluetooth/peripheral_status/src/main.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#define STATUS1_BUTTON DK_BTN1_MSK
3838
#define STATUS2_BUTTON DK_BTN2_MSK
3939

40+
static struct k_work adv_work;
4041

4142
/* Implementation of two status characteristics */
4243
BT_NSMS_DEF(nsms_btn1, "Button 1", false, "Unknown", 20);
@@ -52,7 +53,7 @@ static const struct bt_data sd[] = {
5253
BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_NSMS_VAL),
5354
};
5455

55-
static void advertising_start(void)
56+
static void adv_work_handler(struct k_work *work)
5657
{
5758
int err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_2, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
5859

@@ -64,6 +65,11 @@ static void advertising_start(void)
6465
printk("Advertising successfully started\n");
6566
}
6667

68+
static void advertising_start(void)
69+
{
70+
k_work_submit(&adv_work);
71+
}
72+
6773
static void connected(struct bt_conn *conn, uint8_t err)
6874
{
6975
if (err) {
@@ -237,6 +243,7 @@ int main(void)
237243
settings_load();
238244
}
239245

246+
k_work_init(&adv_work, adv_work_handler);
240247
advertising_start();
241248

242249
for (;;) {

samples/bluetooth/peripheral_uart/src/main.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ static K_SEM_DEFINE(ble_init_ok, 0, 1);
5959

6060
static struct bt_conn *current_conn;
6161
static struct bt_conn *auth_conn;
62+
static struct k_work adv_work;
6263

6364
static const struct device *uart = DEVICE_DT_GET(DT_CHOSEN(nordic_nus_uart));
6465
static struct k_work_delayable uart_work;
@@ -333,7 +334,7 @@ static int uart_init(void)
333334
return err;
334335
}
335336

336-
static void advertising_start(void)
337+
static void adv_work_handler(struct k_work *work)
337338
{
338339
int err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_2, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
339340

@@ -345,6 +346,11 @@ static void advertising_start(void)
345346
printk("Advertising successfully started\n");
346347
}
347348

349+
static void advertising_start(void)
350+
{
351+
k_work_submit(&adv_work);
352+
}
353+
348354
static void connected(struct bt_conn *conn, uint8_t err)
349355
{
350356
char addr[BT_ADDR_LE_STR_LEN];
@@ -640,6 +646,7 @@ int main(void)
640646
return 0;
641647
}
642648

649+
k_work_init(&adv_work, adv_work_handler);
643650
advertising_start();
644651

645652
for (;;) {

0 commit comments

Comments
 (0)