GCC Code Coverage Report


Directory: ./
File: include/powertask/scheduler.h
Date: 2025-01-29 11:09:34
Exec Total Coverage
Lines: 2 2 100.0%
Functions: 1 1 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 #ifndef POWERTASK_SCHEDULER_H
2 #define POWERTASK_SCHEDULER_H
3
4 #include <stdbool.h>
5 #include <powertask/energy.h>
6
7 /** @brief Task */
8 typedef struct powertask_task_s {
9 void (*action)(void); /**< Action to be executed. */
10 bool (*condition)(void); /**< Condition that allows execution of the task. */
11 int required_energy; /**< Required energy (in Joules) to run the task. */
12 bool complete; /**< Indicates if the task was already executed. */
13 } powertask_task;
14
15 /** @brief Scheduler */
16 typedef struct powertask_scheduler_s {
17 powertask_task **list_of_tasks; /**< List with scheduled tasks. */
18 int number_of_tasks; /**< Current number of scheduled tasks in the list. */
19 int _list_of_tasks_len; /**< Maximum number of tasks allowed in the list. */
20 } powertask_scheduler;
21
22 /**
23 * @brief Runs the scheduled tasks
24 *
25 * @param[in] sched Scheduler instance
26 * @param[in] energy_source Energy source used to run scheduled tasks
27 */
28 void powertask_run_scheduler(powertask_scheduler *sched, powertask_energy_source_t *energy_source);
29
30 /**
31 * @brief Add task to a scheduler
32 *
33 * @param[in] sched Scheduler to which the task will be added.
34 * @param[in] task Task to be added to the scheduler.
35 */
36 void powertask_add(powertask_scheduler *sched, powertask_task *task);
37
38 /**
39 * @brief Initialize scheduler
40 *
41 * @param[in] _name Name to be given to the scheduler.
42 * @param[in] _number_of_tasks Maximum number of tasks to be allowed on the scheduler.
43 */
44 #define POWERTASK_INIT(_name, _number_of_tasks) \
45 static powertask_task * _name##_list_of_tasks[_number_of_tasks]; \
46 static struct powertask_scheduler_s _name = { \
47 .list_of_tasks = _name##_list_of_tasks , \
48 ._list_of_tasks_len = _number_of_tasks, \
49 }
50
51 /** @brief Implementation of run always macro. */
52 6 static bool _run_always(void)
53 {
54 6 return true;
55 }
56
57 /** @brief Always execute the action (when energy is available). */
58 #define POWERTASK_RUN_ALWAYS _run_always
59
60 /**
61 * @brief Wait for a task is finished to continue.
62 *
63 * @param[in] _task Task to wait for.
64 */
65 #define POWERTASK_WAIT_FOR(_task) _is_##_task##_complete
66
67 /**
68 * @brief Declares a task.
69 *
70 * @param[in] _name Name to be given to the task.
71 */
72 #define POWERTASK_DECLARE(_name) \
73 static powertask_task task_##_name = { \
74 .complete = false, \
75 }; \
76 static bool _is_##_name##_complete() { \
77 return task_##_name.complete; \
78 }
79
80 /**
81 * @brief Declare task
82 *
83 * @param[in] _scheduler Scheduler to which task should be added.
84 * @param[in] _name Name used to identify the task.
85 * @param[in] _action Action to be executed.
86 * @param[in] _condition Function defining in which condition the action
87 * will be executed.
88 * @param[in] _required_energy Minimum amount of energy (in Joules) required to
89 * execute the action.
90 */
91 #define POWERTASK_TASK(_scheduler, _name, _action, _condition, _required_energy) \
92 task_##_name = (powertask_task){ \
93 .action = _action, \
94 .condition = _condition, \
95 .required_energy = _required_energy, \
96 }; \
97 powertask_add(&_scheduler, &task_##_name);
98
99 #endif /* POWERTASK_SCHEDULER_H */
100