Skip to content

Commit 75b72eb

Browse files
committed
Bluetooth: Mesh: Convert beacon timer to delayable work
Moves the beacon_enabled check in the beacon work handler to check the beacon flag before sending anything, in case a cancel call fails. Split out from zephyrproject-rtos#33782. Signed-off-by: Trond Einar Snekvik <[email protected]>
1 parent e4a349f commit 75b72eb

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

subsys/bluetooth/mesh/beacon.c

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
/* 1 transmission, 20ms interval */
3939
#define PROV_XMIT BT_MESH_TRANSMIT(0, 20)
4040

41-
static struct k_delayed_work beacon_timer;
41+
static struct k_work_delayable beacon_timer;
4242

4343
static int cache_check(struct bt_mesh_subnet *sub, void *beacon_data)
4444
{
@@ -232,32 +232,30 @@ static void update_beacon_observation(void)
232232

233233
static void beacon_send(struct k_work *work)
234234
{
235-
/* Don't send anything if we have an active provisioning link */
236-
if (IS_ENABLED(CONFIG_BT_MESH_PB_ADV) && bt_mesh_prov_active()) {
237-
k_delayed_work_submit(&beacon_timer, K_SECONDS(CONFIG_BT_MESH_UNPROV_BEACON_INT));
238-
return;
239-
}
240-
241235
BT_DBG("");
242236

243237
if (bt_mesh_is_provisioned()) {
238+
if (!bt_mesh_beacon_enabled() &&
239+
!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR)) {
240+
return;
241+
}
242+
244243
update_beacon_observation();
245244
(void)bt_mesh_subnet_find(secure_beacon_send, NULL);
246245

247-
/* Only resubmit if beaconing is still enabled */
248-
if (bt_mesh_beacon_enabled() ||
249-
atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR)) {
250-
k_delayed_work_submit(&beacon_timer,
251-
PROVISIONED_INTERVAL);
252-
}
253-
246+
k_work_schedule(&beacon_timer, PROVISIONED_INTERVAL);
254247
return;
255248
}
256249

257250
if (IS_ENABLED(CONFIG_BT_MESH_PB_ADV)) {
258-
unprovisioned_beacon_send();
259-
k_delayed_work_submit(&beacon_timer, K_SECONDS(CONFIG_BT_MESH_UNPROV_BEACON_INT));
251+
/* Don't send anything if we have an active provisioning link */
252+
if (!bt_mesh_prov_active()) {
253+
unprovisioned_beacon_send();
254+
}
255+
256+
k_work_schedule(&beacon_timer, K_SECONDS(CONFIG_BT_MESH_UNPROV_BEACON_INT));
260257
}
258+
261259
}
262260

263261
struct beacon_params {
@@ -430,18 +428,23 @@ BT_MESH_SUBNET_CB_DEFINE(subnet_evt);
430428

431429
void bt_mesh_beacon_init(void)
432430
{
433-
k_delayed_work_init(&beacon_timer, beacon_send);
431+
k_work_init_delayable(&beacon_timer, beacon_send);
434432
}
435433

436434
void bt_mesh_beacon_ivu_initiator(bool enable)
437435
{
438436
atomic_set_bit_to(bt_mesh.flags, BT_MESH_IVU_INITIATOR, enable);
439437

440-
if (enable) {
441-
k_delayed_work_submit(&beacon_timer, K_NO_WAIT);
442-
} else if (!bt_mesh_beacon_enabled()) {
443-
k_delayed_work_cancel(&beacon_timer);
444-
}
438+
/* Fire the beacon handler straight away if it's not already pending -
439+
* in which case we'll fire according to the ongoing periodic sending.
440+
* If beacons are disabled, the handler will exit early.
441+
*
442+
* An alternative solution would be to check whether beacons are enabled
443+
* here, and cancel if not. As the cancel operation may fail, we would
444+
* still have to implement an early exit mechanism, so we might as well
445+
* just use this every time.
446+
*/
447+
k_work_schedule(&beacon_timer, K_NO_WAIT);
445448
}
446449

447450
static void subnet_beacon_enable(struct bt_mesh_subnet *sub)
@@ -454,19 +457,17 @@ static void subnet_beacon_enable(struct bt_mesh_subnet *sub)
454457

455458
void bt_mesh_beacon_enable(void)
456459
{
457-
if (!bt_mesh_is_provisioned()) {
458-
k_delayed_work_submit(&beacon_timer, K_NO_WAIT);
459-
return;
460+
if (bt_mesh_is_provisioned()) {
461+
bt_mesh_subnet_foreach(subnet_beacon_enable);
460462
}
461463

462-
bt_mesh_subnet_foreach(subnet_beacon_enable);
463-
464-
k_delayed_work_submit(&beacon_timer, K_NO_WAIT);
464+
k_work_reschedule(&beacon_timer, K_NO_WAIT);
465465
}
466466

467467
void bt_mesh_beacon_disable(void)
468468
{
469469
if (!atomic_test_bit(bt_mesh.flags, BT_MESH_IVU_INITIATOR)) {
470-
k_delayed_work_cancel(&beacon_timer);
470+
/* If this fails, we'll do an early exit in the work handler. */
471+
(void)k_work_cancel_delayable(&beacon_timer);
471472
}
472473
}

0 commit comments

Comments
 (0)