Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bluetooth: Mesh: fix NO_ACTIONS scheduling #20054

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions subsys/bluetooth/mesh/scheduler_srv.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ static void run_scheduler(struct bt_mesh_scheduler_srv *srv)
uint8_t planned_idx = get_least_time_index(srv);

if (planned_idx == BT_MESH_SCHEDULER_ACTION_ENTRY_COUNT) {
k_work_cancel_delayable(&srv->delayed_work);
return;
}

Expand Down Expand Up @@ -639,6 +640,23 @@ static int action_set(const struct bt_mesh_model *model, struct bt_mesh_msg_ctx
run_scheduler(srv);
}

if ((srv->sch_reg[idx].action == BT_MESH_SCHEDULER_NO_ACTIONS) &&
(srv->active_bitmap & BIT(idx))) {
alxelax marked this conversation as resolved.
Show resolved Hide resolved
WRITE_BIT(srv->active_bitmap, idx, 0);

bool reschedule = srv->idx == idx;

if (srv->active_bitmap == 0) {
srv->idx = BT_MESH_SCHEDULER_ACTION_ENTRY_COUNT;
srv->last_idx = BT_MESH_SCHEDULER_ACTION_ENTRY_COUNT;
}

/* If the current action is set to NO_ACTIONS, schedule the next in line, if any */
if (reschedule) {
run_scheduler(srv);
alxelax marked this conversation as resolved.
Show resolved Hide resolved
}
}

if (srv->action_set_cb) {
srv->action_set_cb(srv, ctx, idx, &srv->sch_reg[idx]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,4 +655,56 @@ ZTEST(scheduler_action_planning, test_second_sched_turn_off_recurring)
expected_tm_check(fired_tm, expected, 8);
}

ZTEST(scheduler_action_planning, test_sched_cancel_imminent_action)
{
/** Send a TURN_ON action to the sched srv in
* element 1 of the mocked composition data.
* Cancel the action before it is triggered by
* sending a new action with the same parameters
* but with the action set to NO_ACTIONS.
*
* Expect no events to be triggered.
*/

const struct bt_mesh_schedule_entry test_action = {
.year = BT_MESH_SCHEDULER_ANY_YEAR,
.month = ANY_MONTH,
.day = BT_MESH_SCHEDULER_ANY_DAY,
.hour = 0,
.minute = BT_MESH_SCHEDULER_ANY_MINUTE,
.second = BT_MESH_SCHEDULER_EVERY_15_SECONDS,
.day_of_week = ANY_DAY_OF_WEEK,
.action = BT_MESH_SCHEDULER_TURN_ON,
.transition_time = model_transition_encode(2500),
.scene_number = 1
};

struct sched_evt evt_list[] = {
{ .elem_idx = 0, .evt = BT_MESH_SCHEDULER_TURN_ON, .transition_time = 2500 },
{ .elem_idx = 1, .evt = BT_MESH_SCHEDULER_TURN_ON, .transition_time = 2500 },
{ .elem_idx = 2, .evt = BT_MESH_SCHEDULER_TURN_ON, .transition_time = 2500 },
};

sched_evt_ctx_init(evt_list, ARRAY_SIZE(evt_list));
action_put(&test_action, sched_mod_elem1);
k_sleep(K_SECONDS(1));

const struct bt_mesh_schedule_entry test_action_inactive = {
.year = BT_MESH_SCHEDULER_ANY_YEAR,
.month = ANY_MONTH,
.day = BT_MESH_SCHEDULER_ANY_DAY,
.hour = 0,
.minute = BT_MESH_SCHEDULER_ANY_MINUTE,
.second = BT_MESH_SCHEDULER_EVERY_15_SECONDS,
.day_of_week = ANY_DAY_OF_WEEK,
.action = BT_MESH_SCHEDULER_NO_ACTIONS,
.transition_time = model_transition_encode(2500),
.scene_number = 1
};

action_put(&test_action_inactive, sched_mod_elem1);
zassert_equal(k_sem_take(&action_fired, K_SECONDS(60)), -EAGAIN,
"Unexpected event was triggered");
}

ZTEST_SUITE(scheduler_action_planning, NULL, NULL, tc_setup, tc_teardown, NULL);