Infuse-IoT SDK API 0.0.1
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
task.h
Go to the documentation of this file.
1
10#ifndef INFUSE_SDK_INCLUDE_INFUSE_TASK_RUNNER_TASK_H_
11#define INFUSE_SDK_INCLUDE_INFUSE_TASK_RUNNER_TASK_H_
12
13#include <zephyr/kernel.h>
14#include <zephyr/device.h>
15
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
29enum {
34};
35
36enum {
39};
40
41typedef void (*task_runner_task_fn)(const struct task_schedule *schedule,
42 struct k_poll_signal *terminate, void *arg);
43
49 const char *name;
51 uint8_t task_id;
53 uint8_t exec_type;
55 uint8_t flags;
56 /* Task specific argument */
57 union {
58 const struct device *dev;
59 const void *const_arg;
60 void *arg;
62 union {
63 struct {
65 struct k_thread *thread;
69 k_thread_stack_t *stack;
71 size_t stack_size;
72 } thread;
73 struct {
75 k_work_handler_t worker_fn;
77 void *state;
78 } workqueue;
80};
81
85struct task_data {
86 union {
88 struct {
89 /* Workqueue item */
90 struct k_work_delayable work;
91 /* Counter for the number of times the work has been rescheduled this run */
93 /* Compile-time argument */
94 union {
95 const void *const_arg;
96 void *arg;
101 struct k_poll_signal terminate_signal;
107 bool skip;
108};
109
116#define ___TASK_VAR_DEFINE(macro, ...) macro(1, 0, __VA_ARGS__)
117#define __TASK_VAR_DEFINE(...) ___TASK_VAR_DEFINE(__VA_ARGS__)
118#define _TASK_VAR_DEFINE(task) __TASK_VAR_DEFINE(__DEBRACKET task)
125#define ___TASK_CONFIG_DEFINE(macro, ...) macro(0, 1, __VA_ARGS__)
126#define __TASK_CONFIG_DEFINE(...) ___TASK_CONFIG_DEFINE(__VA_ARGS__)
127#define _TASK_CONFIG_DEFINE(task) __TASK_CONFIG_DEFINE(__DEBRACKET task)
128
129/* clang-format off */
130
181#define TASK_RUNNER_TASKS_DEFINE(config_name, data_name, ...) \
182 /* Define task specific variables */ \
183 FOR_EACH_NONEMPTY_TERM(_TASK_VAR_DEFINE, (;), __VA_ARGS__); \
184 /* Define the configurations for each task */ \
185 static const struct task_config config_name[] = { \
186 FOR_EACH_NONEMPTY_TERM(_TASK_CONFIG_DEFINE, (,), __VA_ARGS__) \
187 }; \
188 /* Define the runtime task data array */ \
189 static struct task_data data_name[ARRAY_SIZE(config_name)]
190
191/* clang-format on */
192
200static inline struct task_data *task_data_from_work(struct k_work *work)
201{
202 struct k_work_delayable *dwork = k_work_delayable_from_work(work);
203
204 return CONTAINER_OF(dwork, struct task_data, executor.workqueue.work);
205}
206
215
224
232void task_workqueue_reschedule(struct task_data *task, k_timeout_t delay);
233
243static inline int task_runner_task_block(struct k_poll_signal *terminate_signal,
244 k_timeout_t timeout)
245{
246 struct k_poll_event events[1] = {
247 K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_SIGNAL, K_POLL_MODE_NOTIFY_ONLY,
248 terminate_signal),
249 };
250 int signaled;
251 int result;
252
253 /* Block until requested timeout */
254 (void)k_poll(events, 1, timeout);
255 /* Determine if we have been requested to terminate */
256 k_poll_signal_check(terminate_signal, &signaled, &result);
257 return signaled ? 1 : 0;
258}
259
269static inline bool task_schedule_tdf_requested(const struct task_schedule *schedule,
270 uint8_t tdf_mask)
271{
272 return (schedule->task_logging[0].tdf_mask & tdf_mask) ||
273 (schedule->task_logging[1].tdf_mask & tdf_mask);
274}
275
290static inline void task_schedule_tdf_log_core(const struct task_schedule *schedule,
291 uint8_t tdf_mask, uint16_t tdf_id, uint8_t tdf_len,
292 uint8_t tdf_num, enum tdf_data_format format,
293 uint64_t time, uint32_t idx_period, const void *data)
294{
295 if (schedule->task_logging[0].tdf_mask & tdf_mask) {
296 tdf_data_logger_log_core(schedule->task_logging[0].loggers, tdf_id, tdf_len,
297 tdf_num, format, time, idx_period, data);
298 }
299 if (schedule->task_logging[1].tdf_mask & tdf_mask) {
300 tdf_data_logger_log_core(schedule->task_logging[1].loggers, tdf_id, tdf_len,
301 tdf_num, format, time, idx_period, data);
302 }
303}
304
317static inline void task_schedule_tdf_log_array(const struct task_schedule *schedule,
318 uint8_t tdf_mask, uint16_t tdf_id, uint8_t tdf_len,
319 uint8_t tdf_num, uint64_t time, uint32_t period,
320 const void *data)
321{
322 task_schedule_tdf_log_core(schedule, tdf_mask, tdf_id, tdf_len, tdf_num,
323 TDF_DATA_FORMAT_TIME_ARRAY, time, period, data);
324}
325
336static inline void task_schedule_tdf_log(const struct task_schedule *schedule, uint8_t tdf_mask,
337 uint16_t tdf_id, uint8_t tdf_len, uint64_t time,
338 const void *data)
339{
340 task_schedule_tdf_log_core(schedule, tdf_mask, tdf_id, tdf_len, 1, TDF_DATA_FORMAT_SINGLE,
341 time, 0, data);
342}
343
360#define TASK_SCHEDULE_TDF_LOG_ARRAY(schedule, tdf_mask, tdf_id, tdf_num, tdf_time, period, data) \
361 task_schedule_tdf_log_array(schedule, tdf_mask, tdf_id, sizeof(TDF_TYPE(tdf_id)), tdf_num, \
362 tdf_time, period, data); \
363 do { \
364 __maybe_unused const TDF_TYPE(tdf_id) *_data = data; \
365 } while (0)
366
381#define TASK_SCHEDULE_TDF_LOG(schedule, tdf_mask, tdf_id, tdf_time, data) \
382 task_schedule_tdf_log(schedule, tdf_mask, tdf_id, sizeof(TDF_TYPE(tdf_id)), tdf_time, \
383 data); \
384 do { \
385 __maybe_unused const TDF_TYPE(tdf_id) *_data = data; \
386 } while (0)
387
392#ifdef __cplusplus
393}
394#endif
395
396#endif /* INFUSE_SDK_INCLUDE_INFUSE_TASK_RUNNER_TASK_H_ */
TDF Data Logger.
static bool task_schedule_tdf_requested(const struct task_schedule *schedule, uint8_t tdf_mask)
Determine if a given TDF was requested by the schedule.
Definition task.h:269
static void task_schedule_tdf_log_core(const struct task_schedule *schedule, uint8_t tdf_mask, uint16_t tdf_id, uint8_t tdf_len, uint8_t tdf_num, enum tdf_data_format format, uint64_t time, uint32_t idx_period, const void *data)
Log an array of TDFs as requested by a schedule as a diff array.
Definition task.h:290
static void task_schedule_tdf_log(const struct task_schedule *schedule, uint8_t tdf_mask, uint16_t tdf_id, uint8_t tdf_len, uint64_t time, const void *data)
Log a single TDF as requested by a schedule.
Definition task.h:336
static void task_schedule_tdf_log_array(const struct task_schedule *schedule, uint8_t tdf_mask, uint16_t tdf_id, uint8_t tdf_len, uint8_t tdf_num, uint64_t time, uint32_t period, const void *data)
Log an array of TDFs as requested by a schedule.
Definition task.h:317
uint8_t * task_schedule_persistent_storage(struct task_data *data)
Retrieve per-schedule persistent memory.
void(* task_runner_task_fn)(const struct task_schedule *schedule, struct k_poll_signal *terminate, void *arg)
Definition task.h:41
void task_workqueue_reschedule(struct task_data *task, k_timeout_t delay)
Reschedule the task to run again after a delay.
static int task_runner_task_block(struct k_poll_signal *terminate_signal, k_timeout_t timeout)
Block on the termination signal for a duration.
Definition task.h:243
static struct task_data * task_data_from_work(struct k_work *work)
Get the parent task_data struct from the work pointer.
Definition task.h:200
const struct task_schedule * task_schedule_from_data(struct task_data *data)
Retrieve the schedule associated with a task.
@ TASK_FLAG_ARG_IS_DEVICE
task_arg union is a device pointer
Definition task.h:38
@ TASK_EXECUTOR_WORKQUEUE
Task runs on the system workqueue.
Definition task.h:33
@ TASK_EXECUTOR_THREAD
Task runs on its own thread.
Definition task.h:31
tdf_data_format
Definition tdf.h:39
@ TDF_DATA_FORMAT_TIME_ARRAY
Time array with period.
Definition tdf.h:43
@ TDF_DATA_FORMAT_SINGLE
Single sample.
Definition tdf.h:41
void tdf_data_logger_log_core(uint8_t logger_mask, uint16_t tdf_id, uint8_t tdf_len, uint8_t tdf_num, enum tdf_data_format format, uint64_t time, uint32_t idx_period, const void *data)
Add multiple TDFs to multiple data loggers.
Task Runner task scheduling.
Constant task configuration.
Definition task.h:47
const struct device * dev
Definition task.h:58
uint8_t task_id
Task identifier.
Definition task.h:51
const void * const_arg
Definition task.h:59
void * arg
Definition task.h:60
union task_config::@38 task_arg
uint8_t exec_type
Execution context TASK_EXECUTOR_*
Definition task.h:53
k_work_handler_t worker_fn
Handler function.
Definition task.h:75
size_t stack_size
Size of stack memory.
Definition task.h:71
struct k_thread * thread
Thread state storage.
Definition task.h:65
k_thread_stack_t * stack
Pointer to stack memory for thread.
Definition task.h:69
uint8_t flags
Task flags of type TASK_FLAG_*
Definition task.h:55
void * state
Persistent state.
Definition task.h:77
const char * name
Task name.
Definition task.h:49
union task_config::@39 executor
task_runner_task_fn task_fn
Thread function.
Definition task.h:67
Task runtime state.
Definition task.h:85
union task_data::@42 executor
struct k_poll_signal terminate_signal
Thread termination signal.
Definition task.h:101
void * arg
Definition task.h:96
struct k_work_delayable work
Definition task.h:90
bool skip
Skip evaluation of task.
Definition task.h:107
int reschedule_counter
Definition task.h:92
struct task_data::@42::@43 workqueue
Workqueue state storage.
bool running
Task is currently running.
Definition task.h:105
uint8_t schedule_idx
Schedule that triggered the task to run.
Definition task.h:103
const void * const_arg
Definition task.h:95
union task_data::@42::@43::@44 task_arg
uint8_t loggers
TDF loggers to log to.
Definition schedule.h:113
uint8_t tdf_mask
TDFs to log (bitmask defined by the activity)
Definition schedule.h:115
Schedule for a given task.
Definition schedule.h:143
struct task_schedule_tdf_logging task_logging[2]
Task logging configuration.
Definition schedule.h:185