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
9
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
29
30enum {
35};
36
37enum {
40};
41
47#define TASK_RUNNER_TASK_U32_ID(task_id) (0x7A5A6600 | (task_id))
48
56typedef void (*task_runner_task_fn)(const struct task_schedule *schedule,
57 struct k_poll_signal *terminate, void *arg);
58
64 const char *name;
66 uint8_t task_id;
68 uint8_t exec_type;
70 uint8_t flags;
72 union {
74 const struct device *dev;
76 const void *const_arg;
78 void *arg;
81 union {
83 struct {
85 struct k_thread *thread;
89 k_thread_stack_t *stack;
91 size_t stack_size;
92 } thread;
94 struct {
96 k_work_handler_t worker_fn;
98 void *state;
99 } workqueue;
101};
102
106struct task_data {
108 union {
110 struct {
112 struct k_work_delayable work;
116 union {
118 const void *const_arg;
120 void *arg;
125 struct k_poll_signal terminate_signal;
131 bool skip;
132};
133
140#define ___TASK_VAR_DEFINE(macro, ...) macro(1, 0, __VA_ARGS__)
141#define __TASK_VAR_DEFINE(...) ___TASK_VAR_DEFINE(__VA_ARGS__)
142#define _TASK_VAR_DEFINE(task) __TASK_VAR_DEFINE(__DEBRACKET task)
149#define ___TASK_CONFIG_DEFINE(macro, ...) macro(0, 1, __VA_ARGS__)
150#define __TASK_CONFIG_DEFINE(...) ___TASK_CONFIG_DEFINE(__VA_ARGS__)
151#define _TASK_CONFIG_DEFINE(task) __TASK_CONFIG_DEFINE(__DEBRACKET task)
152
153/* clang-format off */
154
205#define TASK_RUNNER_TASKS_DEFINE(config_name, data_name, ...) \
206 /* Define task specific variables */ \
207 FOR_EACH_NONEMPTY_TERM(_TASK_VAR_DEFINE, (;), __VA_ARGS__); \
208 /* Define the configurations for each task */ \
209 static const struct task_config config_name[] = { \
210 FOR_EACH_NONEMPTY_TERM(_TASK_CONFIG_DEFINE, (,), __VA_ARGS__) \
211 }; \
212 /* Define the runtime task data array */ \
213 static struct task_data data_name[ARRAY_SIZE(config_name)]
214
215/* clang-format on */
216
224static inline struct task_data *task_data_from_work(struct k_work *work)
225{
226 struct k_work_delayable *dwork = k_work_delayable_from_work(work);
227
228 return CONTAINER_OF(dwork, struct task_data, executor.workqueue.work);
229}
230
239
248
256void task_workqueue_reschedule(struct task_data *task, k_timeout_t delay);
257
267static inline int task_runner_task_block(struct k_poll_signal *terminate_signal,
268 k_timeout_t timeout)
269{
270 struct k_poll_event events[1] = {
271 K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_SIGNAL, K_POLL_MODE_NOTIFY_ONLY,
272 terminate_signal),
273 };
274 int signaled;
275 int result;
276
277 /* Block until requested timeout */
278 (void)k_poll(events, 1, timeout);
279 /* Determine if we have been requested to terminate */
280 k_poll_signal_check(terminate_signal, &signaled, &result);
281 return signaled ? 1 : 0;
282}
283
293static inline bool task_schedule_tdf_requested(const struct task_schedule *schedule,
294 uint8_t tdf_mask)
295{
296 return (schedule->task_logging[0].tdf_mask & tdf_mask) ||
297 (schedule->task_logging[1].tdf_mask & tdf_mask);
298}
299
314static inline void task_schedule_tdf_log_core(const struct task_schedule *schedule,
315 uint8_t tdf_mask, uint16_t tdf_id, uint8_t tdf_len,
316 uint8_t tdf_num, enum tdf_data_format format,
317 uint64_t time, uint32_t idx_period, const void *data)
318{
319 if (schedule->task_logging[0].tdf_mask & tdf_mask) {
320 tdf_data_logger_log_core(schedule->task_logging[0].loggers, tdf_id, tdf_len,
321 tdf_num, format, time, idx_period, data);
322 }
323 if (schedule->task_logging[1].tdf_mask & tdf_mask) {
324 tdf_data_logger_log_core(schedule->task_logging[1].loggers, tdf_id, tdf_len,
325 tdf_num, format, time, idx_period, data);
326 }
327}
328
341static inline void task_schedule_tdf_log_array(const struct task_schedule *schedule,
342 uint8_t tdf_mask, uint16_t tdf_id, uint8_t tdf_len,
343 uint8_t tdf_num, uint64_t time, uint32_t period,
344 const void *data)
345{
346 task_schedule_tdf_log_core(schedule, tdf_mask, tdf_id, tdf_len, tdf_num,
347 TDF_DATA_FORMAT_TIME_ARRAY, time, period, data);
348}
349
360static inline void task_schedule_tdf_log(const struct task_schedule *schedule, uint8_t tdf_mask,
361 uint16_t tdf_id, uint8_t tdf_len, uint64_t time,
362 const void *data)
363{
364 task_schedule_tdf_log_core(schedule, tdf_mask, tdf_id, tdf_len, 1, TDF_DATA_FORMAT_SINGLE,
365 time, 0, data);
366}
367
384#define TASK_SCHEDULE_TDF_LOG_ARRAY(schedule, tdf_mask, tdf_id, tdf_num, tdf_time, period, data) \
385 task_schedule_tdf_log_array(schedule, tdf_mask, tdf_id, sizeof(TDF_TYPE(tdf_id)), tdf_num, \
386 tdf_time, period, data); \
387 do { \
388 __maybe_unused const TDF_TYPE(tdf_id) *_data = data; \
389 } while (0)
390
405#define TASK_SCHEDULE_TDF_LOG(schedule, tdf_mask, tdf_id, tdf_time, data) \
406 task_schedule_tdf_log(schedule, tdf_mask, tdf_id, sizeof(TDF_TYPE(tdf_id)), tdf_time, \
407 data); \
408 do { \
409 __maybe_unused const TDF_TYPE(tdf_id) *_data = data; \
410 } while (0)
411
415
416#ifdef __cplusplus
417}
418#endif
419
420#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:293
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:314
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:360
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:341
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)
Function run by a thread executor task.
Definition task.h:56
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:267
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:224
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:39
@ TASK_EXECUTOR_WORKQUEUE
Task runs on the system workqueue.
Definition task.h:34
@ TASK_EXECUTOR_THREAD
Task runs on its own thread.
Definition task.h:32
tdf_data_format
Format of the data in a TDF.
Definition tdf.h:42
@ TDF_DATA_FORMAT_TIME_ARRAY
Time array with period.
Definition tdf.h:46
@ TDF_DATA_FORMAT_SINGLE
Single sample.
Definition tdf.h:44
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:62
const struct device * dev
Argument as a device pointer (TASK_FLAG_ARG_IS_DEVICE)
Definition task.h:74
union task_config::@225067150370361177166166351327340264154013335120 task_arg
Task specific argument.
uint8_t task_id
Task identifier.
Definition task.h:66
const void * const_arg
Argument as a constant pointer.
Definition task.h:76
void * arg
Argument as a mutable pointer.
Definition task.h:78
uint8_t exec_type
Execution context TASK_EXECUTOR_*
Definition task.h:68
k_work_handler_t worker_fn
Handler function.
Definition task.h:96
size_t stack_size
Size of stack memory.
Definition task.h:91
union task_config::@246102301160106360140230353205072307124062022247 executor
Executor specific configuration.
struct k_thread * thread
Thread state storage.
Definition task.h:85
k_thread_stack_t * stack
Pointer to stack memory for thread.
Definition task.h:89
uint8_t flags
Task flags of type TASK_FLAG_*
Definition task.h:70
void * state
Persistent state.
Definition task.h:98
const char * name
Task name.
Definition task.h:64
task_runner_task_fn task_fn
Thread function.
Definition task.h:87
Task runtime state.
Definition task.h:106
struct task_data::@174101042313227056365276014012106006134230234354::@163121324307354322136206211317316074310326313007 workqueue
Workqueue state storage.
union task_data::@174101042313227056365276014012106006134230234354::@163121324307354322136206211317316074310326313007::@120365071263030125005141070164045340126227245342 task_arg
Compile-time argument.
struct k_poll_signal terminate_signal
Thread termination signal.
Definition task.h:125
void * arg
Argument as a mutable pointer.
Definition task.h:120
struct k_work_delayable work
Workqueue item.
Definition task.h:112
bool skip
Skip evaluation of task.
Definition task.h:131
int reschedule_counter
Number of times the work has been rescheduled this run.
Definition task.h:114
bool running
Task is currently running.
Definition task.h:129
uint8_t schedule_idx
Schedule that triggered the task to run.
Definition task.h:127
const void * const_arg
Argument as a constant pointer.
Definition task.h:118
union task_data::@174101042313227056365276014012106006134230234354 executor
Executor specific state.
uint8_t loggers
TDF loggers to log to.
Definition schedule.h:139
uint8_t tdf_mask
TDFs to log (bitmask defined by the activity)
Definition schedule.h:141
Schedule for a given task.
Definition schedule.h:169
struct task_schedule_tdf_logging task_logging[2]
Task logging configuration.
Definition schedule.h:236