Infuse-IoT SDK API 0.0.1
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
schedule.h
Go to the documentation of this file.
1
9
10#ifndef INFUSE_SDK_INCLUDE_INFUSE_TASK_RUNNER_SCHEDULE_H_
11#define INFUSE_SDK_INCLUDE_INFUSE_TASK_RUNNER_SCHEDULE_H_
12
13#include <stdint.h>
14#include <stdbool.h>
15
16#include <zephyr/toolchain.h>
17#include <zephyr/sys/atomic.h>
18
20
21#ifdef CONFIG_TASK_RUNNER_CUSTOM_TASK_DEFINITIONS
22#include CONFIG_TASK_RUNNER_CUSTOM_TASK_DEFINITIONS_PATH
23#endif /* CONFIG_TASK_RUNNER_CUSTOM_TASK_DEFINITIONS */
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
34
47 _TASK_VALID_END,
48 _TASK_VALID_MASK = 0x7F,
50};
51
65
67#define TR_NOT 0x100
69#define TR_OR 0x200
71#define TR_AND 0x00
72
74
75#define _TR_STATE_BASE 0xFF
76#define _TR_NOT_BIT_COMMON(state, idx) ((state) & TR_NOT ? BIT(idx) : 0)
77#define _TR_OR_BIT_COMMON(state, idx) ((state) & TR_OR ? BIT(idx) : 0)
78#define _TR_STATES_DEFINE_ALL(s0, s1, s2, s3) \
79 { \
80 .metadata = _TR_NOT_BIT_COMMON(s0, 0) | _TR_NOT_BIT_COMMON(s1, 1) | \
81 _TR_NOT_BIT_COMMON(s2, 2) | _TR_NOT_BIT_COMMON(s3, 3) | \
82 _TR_OR_BIT_COMMON(s0, 4) | _TR_OR_BIT_COMMON(s1, 5) | \
83 _TR_OR_BIT_COMMON(s2, 6) | _TR_OR_BIT_COMMON(s3, 7), \
84 .states = { \
85 (s0) & _TR_STATE_BASE, \
86 (s1) & _TR_STATE_BASE, \
87 (s2) & _TR_STATE_BASE, \
88 (s3) & _TR_STATE_BASE, \
89 } \
90 }
91
92#define _TR_GET_MACRO(_1, _2, _3, _4, NAME, ...) NAME
93#define _TR_STATES_1(arg1) _TR_STATES_DEFINE_ALL(arg1, 0, 0, 0)
94#define _TR_STATES_2(arg1, arg2) _TR_STATES_DEFINE_ALL(arg1, arg2, 0, 0)
95#define _TR_STATES_3(arg1, arg2, arg3) _TR_STATES_DEFINE_ALL(arg1, arg2, arg3, 0)
96#define _TR_STATES_4(arg1, arg2, arg3, arg4) _TR_STATES_DEFINE_ALL(arg1, arg2, arg3, arg4)
97
99
118#define TASK_STATES_DEFINE(...) \
119 _TR_GET_MACRO(__VA_ARGS__, _TR_STATES_4, _TR_STATES_3, _TR_STATES_2, _TR_STATES_1) \
120 (__VA_ARGS__)
121
131#define TASK_STATES_START_TIMEOUT(seconds) ((seconds) / 2)
132
138 uint8_t loggers;
140 uint8_t tdf_mask;
141} __packed;
142
150 /* Metadata associated with states (inversion) */
151 uint8_t metadata;
153 uint8_t states[4];
154} __packed;
155
161#define TASK_RUNNER_LOCKOUT_IGNORE_FIRST BIT(31)
162
169 uint8_t task_id;
171 uint8_t validity;
177 uint32_t timeout_s;
181 uint8_t lower;
183 uint8_t upper;
184 } __packed battery_start;
232 union task_args {
233 uint8_t raw[16];
235#ifdef CONFIG_TASK_RUNNER_CUSTOM_TASK_DEFINITIONS
236 union custom_task_arguments custom;
237#endif /* CONFIG_TASK_RUNNER_CUSTOM_TASK_DEFINITIONS */
239} __packed;
240
250
260typedef void (*task_schedule_event_cb_t)(const struct task_schedule *schedule,
261 enum task_schedule_event event);
262
274 uint32_t last_run;
276 uint32_t runtime;
280 uint8_t task_idx;
281#ifdef CONFIG_TASK_RUNNER_PER_SCHEDULE_STORAGE
283 uint8_t runtime_state[CONFIG_TASK_RUNNER_PER_SCHEDULE_STORAGE];
284#endif /* CONFIG_TASK_RUNNER_PER_SCHEDULE_STORAGE */
285};
286
288#define _TASK_SCHEDULE_MAX_SCHEDULES(schedules) \
289 COND_CODE_1(CONFIG_KV_STORE_KEY_TASK_SCHEDULES, \
290 (CONFIG_KV_STORE_KEY_TASK_SCHEDULES_RANGE), (ARRAY_SIZE(schedules)))
292
302#define TASK_SCHEDULE_STATES_DEFINE(name, schedules) \
303 static struct task_schedule_state name[_TASK_SCHEDULE_MAX_SCHEDULES(schedules)]
304
316bool task_schedule_validate(const struct task_schedule *schedule);
317
331bool task_schedule_should_start(const struct task_schedule *schedule,
332 struct task_schedule_state *state, atomic_t *app_states,
333 uint32_t uptime, uint32_t epoch_time, uint8_t battery);
334
349 struct task_schedule_state *state, atomic_t *app_states,
350 uint32_t uptime, uint32_t epoch_time, uint8_t battery);
351
355
356#ifdef __cplusplus
357}
358#endif
359
360#endif /* INFUSE_SDK_INCLUDE_INFUSE_TASK_RUNNER_SCHEDULE_H_ */
bool task_schedule_should_start(const struct task_schedule *schedule, struct task_schedule_state *state, atomic_t *app_states, uint32_t uptime, uint32_t epoch_time, uint8_t battery)
Determine whether a task should start executing.
void(* task_schedule_event_cb_t)(const struct task_schedule *schedule, enum task_schedule_event event)
Callback notifying that an event has occurred on a schedule.
Definition schedule.h:260
task_runner_valid_type
Definition schedule.h:35
bool task_schedule_validate(const struct task_schedule *schedule)
Basic validity checking on task schedules.
bool task_schedule_should_terminate(const struct task_schedule *schedule, struct task_schedule_state *state, atomic_t *app_states, uint32_t uptime, uint32_t epoch_time, uint8_t battery)
Determine whether a task should be terminated.
task_runner_periodicity_type
Definition schedule.h:52
task_schedule_event
Events that can trigger callbacks.
Definition schedule.h:242
@ TASK_VALID_ACTIVE
Task is only valid when INFUSE_STATE_APPLICATION_ACTIVE is set.
Definition schedule.h:41
@ TASK_VALID_ALWAYS
Task is always valid.
Definition schedule.h:39
@ TASK_VALID_PERMANENTLY_RUNS
Entry and exit conditions never checked, task is rebooted if it terminates.
Definition schedule.h:45
@ TASK_LOCKED
Do not update definition from KV store.
Definition schedule.h:37
@ TASK_VALID_INACTIVE
Task is only valid when INFUSE_STATE_APPLICATION_ACTIVE is not set.
Definition schedule.h:43
@ TASK_PERIODICITY_FIXED
Task can only run on N second boundaries.
Definition schedule.h:54
@ TASK_PERIODICITY_LOCKOUT_DYNAMIC_BATTERY
TASK_PERIODICITY_LOCKOUT with a dynamic lockout based on battery charge
Definition schedule.h:60
@ TASK_PERIODICITY_AFTER
Task can only run N seconds after another schedule terminates.
Definition schedule.h:58
@ TASK_PERIODICITY_LOCKOUT
Task can only run N seconds after previous run started.
Definition schedule.h:56
@ TASK_SCHEDULE_STARTED
Task associated with the schedule has been started.
Definition schedule.h:244
@ TASK_SCHEDULE_STOPPED
Task associated with the schedule has stopped.
Definition schedule.h:248
@ TASK_SCHEDULE_TERMINATE_REQUEST
Task associated with the schedule has been requested to terminate.
Definition schedule.h:246
Task Runner Infuse-IoT task arguments.
Battery charge thresholds to start the task.
Definition schedule.h:179
uint8_t upper
Start task if <= this charge.
Definition schedule.h:183
uint8_t lower
Start task if >= this charge.
Definition schedule.h:181
Battery charge thresholds to terminate the task.
Definition schedule.h:186
uint8_t lower
Terminate task if <= this charge.
Definition schedule.h:188
uint8_t upper
Terminate task if >= this charge.
Definition schedule.h:190
Start N seconds after another task finishes.
Definition schedule.h:203
uint16_t duration_s
Definition schedule.h:205
uint8_t schedule_idx
Definition schedule.h:204
Lockout for a variable duration based on battery state.
Definition schedule.h:212
uint8_t battery_max
Upper battery threshold.
Definition schedule.h:220
uint16_t lockout_min
Lockout between runs at <= battery_min.
Definition schedule.h:214
uint8_t battery_min
Lower battery threshold.
Definition schedule.h:218
uint16_t lockout_max
Lockout between runs at >= battery_max.
Definition schedule.h:216
Start N seconds after task last started.
Definition schedule.h:199
uint32_t lockout_s
Definition schedule.h:200
Start on global N second boundaries.
Definition schedule.h:195
uint32_t period_s
Definition schedule.h:196
Schedule state conditions.
Definition schedule.h:149
uint8_t metadata
Definition schedule.h:151
uint8_t states[4]
Array of states to test.
Definition schedule.h:153
State for a given task schedule.
Definition schedule.h:268
uint32_t last_run
System uptime that started the last run of this schedule.
Definition schedule.h:274
uint32_t last_terminate
System uptime at termination of last run of this schedule.
Definition schedule.h:278
uint32_t runtime
Duration of current run.
Definition schedule.h:276
uint8_t task_idx
Index into task array that corresponds with schedule task_id.
Definition schedule.h:280
struct task_schedule_state * linked
Linked schedule for TASK_PERIODICITY_AFTER.
Definition schedule.h:270
task_schedule_event_cb_t event_cb
Optional callback to be notified of schedule events.
Definition schedule.h:272
Control TDF logging output of a task.
Definition schedule.h:136
uint8_t loggers
TDF loggers to log to.
Definition schedule.h:138
uint8_t tdf_mask
TDFs to log (bitmask defined by the activity)
Definition schedule.h:140
Schedule for a given task.
Definition schedule.h:168
struct task_schedule::battery_start_thresholds battery_start
struct task_schedule_tdf_logging task_logging[2]
Task logging configuration.
Definition schedule.h:230
uint8_t task_id
Definition schedule.h:169
struct task_schedule_state_conditions states_terminate
Task termination state conditions.
Definition schedule.h:228
uint8_t periodicity_type
TASK_PERIODICITY_* value.
Definition schedule.h:173
uint16_t states_start_timeout_2x_s
states_start will evaluate as true 2x this many seconds after last run started
Definition schedule.h:224
uint8_t boot_lockout_minutes
Task will not start for the first N minutes after boot.
Definition schedule.h:175
uint32_t timeout_s
Duration after which task is requested to terminate.
Definition schedule.h:177
uint8_t validity
TASK_VALID_* value.
Definition schedule.h:171
union task_schedule::task_args task_args
union task_schedule::periodicity_args periodicity
struct task_schedule::battery_terminate_thresholds battery_terminate
struct task_schedule_state_conditions states_start
Task start state conditions.
Definition schedule.h:226
Definition infuse_task_args.h:33
Periodicity parameters.
Definition schedule.h:193
struct task_schedule::periodicity_args::periodicity_lockout lockout
struct task_schedule::periodicity_args::periodicity_periodic fixed
struct task_schedule::periodicity_args::periodicity_after after
struct task_schedule::periodicity_args::periodicity_lockout_dynamic_battery lockout_dynamic_battery
Task specific arguments.
Definition schedule.h:232
union infuse_task_arguments infuse
Definition schedule.h:234
uint8_t raw[16]
Definition schedule.h:233