MeshX 0.3
This repository provides an implementation for Bluetooth Low Energy (BLE) Mesh network nodes. The project allows you to create BLE mesh nodes that can communicate with each other, enabling the development of smart home solutions or other IoT-based applications.
Loading...
Searching...
No Matches
meshx_os_timer.c
Go to the documentation of this file.
1
12
13#include "meshx_os_timer.h"
14
15#define OS_TIMER_INIT_MAGIC 0x3892
25#define OS_TIMER_CONTROL_TASK_EVT_MASK (CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_DISARM | \
26 CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_ARM | \
27 CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_REARM | \
28 CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_PERIOD | \
29 CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_FIRE)
30
36
43static struct meshx_os_timer_reg_head meshx_os_timer_reg_table_head = SLIST_HEAD_INITIALIZER(os_timer_reg_table_head);
44
45#if CONFIG_ENABLE_UNIT_TEST
46
60
66{
68}
69
85static meshx_err_t meshx_os_timer_unit_test_cb_handler(int cmd_id, int argc, char **argv)
86{
89
90 uint32_t ut_period = 0;
91 bool ut_reload = false;
92 static meshx_os_timer_t *ut_os_timer;
93
94 MESHX_LOGD(MODULE_ID_COMPONENT_OS_TIMER, "argc|cmd_id: %d|%d", argc, cmd_id);
95 if (cmd_id >= OS_TIMER_CLI_CMD_MAX)
96 {
97 MESHX_LOGE(MODULE_ID_COMPONENT_OS_TIMER, "Invalid number of arguments");
98 return MESHX_INVALID_ARG;
99 }
100
101 switch (cmd)
102 {
104 /* ut 2 0 2 [period_ms] [reload]*/
105 ut_period = UT_GET_ARG(0, uint32_t, argv);
106 ut_reload = UT_GET_ARG(1, uint32_t, argv) == 0 ? false : true;
107 err = meshx_os_timer_create("OS_TIMER_UT", ut_period, ut_reload, meshx_os_timer_ut_cb_handler, &ut_os_timer);
108 break;
110 /* ut 2 1 0 */
111 err = meshx_os_timer_start(ut_os_timer);
112 break;
114 /* ut 2 2 0 */
115 err = meshx_os_timer_restart(ut_os_timer);
116 break;
118 /* ut 2 3 0 */
119 err = meshx_os_timer_stop(ut_os_timer);
120 break;
122 /* ut 2 4 0 */
123 err = meshx_os_timer_delete(&ut_os_timer);
124 break;
126 /* ut 2 5 1 [new period ms] */
127 ut_period = UT_GET_ARG(0, uint32_t, argv);
128 err = meshx_os_timer_set_period(ut_os_timer, ut_period);
129 break;
130 default:
131 break;
132 }
133 if (err)
134 MESHX_LOGE(MODULE_ID_COMPONENT_OS_TIMER, "err: 0x%x", err);
135
136 return err;
137}
138
139#endif /* CONFIG_ENABLE_UNIT_TEST */
140
141/*
142 * @brief Callback function for the OS timer to control task.
143 *
144 * This function is called whenever a OS timer timeout occurs.
145 *
146 * @param timer_handle Timer haandle callback param
147 */
148void meshx_os_timer_fire_cb(const void* timer_handle)
149{
150 meshx_os_timer_t *msg_params;
151 SLIST_FOREACH(msg_params, &meshx_os_timer_reg_table_head, next)
152 {
153 if (msg_params->timer_handle.__timer_handle == timer_handle)
154 {
158 msg_params,
160 break;
161 }
162 }
163}
164
165/*
166 * @brief Callback function for the OS timer control task.
167 *
168 * This function is called whenever a control task message is received.
169 *
170 * @param pdev Pointer to the device structure.
171 * @param evt Event code.
172 * @param params Pointer to the event parameters.
173 * @return MESHX_SUCCESS on success, or an error code on failure.
174 */
176{
177 meshx_os_timer_t *msg_params = (meshx_os_timer_t *)params;
179
180 switch (evt)
181 {
183 MESHX_LOGD(MODULE_ID_COMPONENT_OS_TIMER, "Starting timer %s", OS_TMER_GET_TIMER_NAME(msg_params));
184 err = meshx_rtos_timer_start(&msg_params->timer_handle);
185 break;
186
188 MESHX_LOGD(MODULE_ID_COMPONENT_OS_TIMER, "Rearming timer %s", OS_TMER_GET_TIMER_NAME(msg_params));
189 err = meshx_rtos_timer_reset(&msg_params->timer_handle);
190 break;
191
193 MESHX_LOGD(MODULE_ID_COMPONENT_OS_TIMER, "Stopping timer %s", OS_TMER_GET_TIMER_NAME(msg_params));
194 err = meshx_rtos_timer_stop(&msg_params->timer_handle);
195 break;
196
198 MESHX_LOGD(MODULE_ID_COMPONENT_OS_TIMER, "Timer %s period set: %ld", OS_TMER_GET_TIMER_NAME(msg_params), msg_params->period);
199 err = meshx_rtos_timer_change_period(&msg_params->timer_handle, msg_params->period);
200 break;
201
204 /* call respective callback */
205 if (msg_params->cb)
206 msg_params->cb(msg_params);
207 break;
208
209 default:
210 err = MESHX_INVALID_ARG;
211 break;
212 }
213
214 MESHX_UNUSED(pdev);
215
216 return err;
217}
218
227{
228 meshx_err_t err;
229#if CONFIG_ENABLE_UNIT_TEST
231 if (err)
232 {
233 MESHX_LOGE(MODULE_ID_COMPONENT_OS_TIMER, "unit_test reg failed: (%d)", err);
234 return err;
235 }
236#endif /* CONFIG_ENABLE_UNIT_TEST */
241
242 return err;
243}
244
264 const char *name,
265 uint32_t period,
266 bool reload,
268 meshx_os_timer_t **timer_handle)
269{
270 if (timer_handle == NULL)
271 {
272 return MESHX_INVALID_STATE;
273 }
274
275 if ((*timer_handle) != NULL && (*timer_handle)->init == OS_TIMER_INIT_MAGIC)
276 return MESHX_INVALID_STATE;
277
279
280 *timer_handle = (meshx_os_timer_t *)MESHX_MALLOC(OS_TIMER_SIZE);
281 if (*timer_handle == NULL)
282 return MESHX_NO_MEM;
283
284 (*timer_handle)->cb = cb;
285 (*timer_handle)->period = period;
286
287 err = meshx_rtos_timer_create(&(*timer_handle)->timer_handle,
288 name,
290 *timer_handle,
291 period,
292 reload);
293 if (err)
294 {
295 MESHX_FREE(*timer_handle);
296 return err;
297 }
298
299 SLIST_INSERT_HEAD(&meshx_os_timer_reg_table_head, (*timer_handle), next);
300 (*timer_handle)->init = OS_TIMER_INIT_MAGIC;
301
302 return err;
303}
304
315{
316 if (timer_handle == NULL)
317 {
318 return MESHX_INVALID_STATE;
319 }
320 if (timer_handle->init != OS_TIMER_INIT_MAGIC)
321 return MESHX_INVALID_STATE;
322
326 timer_handle,
328}
329
340{
341 if (timer_handle == NULL)
342 {
343 return MESHX_INVALID_STATE;
344 }
345 if (timer_handle->init != OS_TIMER_INIT_MAGIC)
346 return MESHX_INVALID_STATE;
347
351 timer_handle,
353}
354
365meshx_err_t meshx_os_timer_set_period(meshx_os_timer_t *timer_handle, const uint32_t period_ms)
366{
367 if (timer_handle == NULL)
368 {
369 return MESHX_INVALID_STATE;
370 }
371 if (timer_handle->init != OS_TIMER_INIT_MAGIC)
372 return MESHX_INVALID_STATE;
373
374 timer_handle->period = period_ms;
375
379 timer_handle,
381}
382
393{
394 if (timer_handle == NULL)
395 {
396 return MESHX_INVALID_STATE;
397 }
398 if (timer_handle->init != OS_TIMER_INIT_MAGIC)
399 return MESHX_INVALID_STATE;
400
404 timer_handle,
406}
407
417
419{
420 meshx_err_t err;
421
422 if (timer_handle == NULL)
423 {
424 return MESHX_INVALID_STATE;
425 }
426
427 if (*timer_handle == NULL || (*timer_handle)->init != OS_TIMER_INIT_MAGIC)
428 return MESHX_INVALID_STATE;
429
430 MESHX_LOGI(MODULE_ID_COMPONENT_OS_TIMER, "Deleting timer %s", OS_TMER_GET_TIMER_NAME((*timer_handle)));
431
432 err = meshx_rtos_timer_delete(&(*timer_handle)->timer_handle);
433 if (err)
434 return err;
435
436 (*timer_handle)->init = 0;
437
438 SLIST_REMOVE(&meshx_os_timer_reg_table_head, *timer_handle, meshx_os_timer, next);
439 MESHX_FREE(*timer_handle);
440
441 return MESHX_SUCCESS;
442}
struct dev_struct dev_struct_t
Structure representing the device composition and elements.
CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_FIRE
CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_PERIOD
uint32_t control_task_msg_evt_t
Type definition for control task message event.
meshx_err_t(* control_task_msg_handle_t)(dev_struct_t *pdev, control_task_msg_evt_t evt, void *params)
Function pointer type for control task message handler.
@ CONTROL_TASK_MSG_CODE_SYSTEM
CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_DISARM
CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_ARM
meshx_err_t control_task_msg_publish(control_task_msg_code_t msg_code, control_task_msg_evt_t msg_evt, const void *msg_evt_params, size_t sizeof_msg_evt_params)
Publish a control task message.
CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_REARM
meshx_err_t control_task_msg_subscribe(control_task_msg_code_t msg_code, control_task_msg_evt_t evt_bmap, control_task_msg_handle_t callback)
Subscribe to a control task message.
#define MESHX_UNUSED(x)
Definition meshx_err.h:15
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:39
@ MESHX_SUCCESS
Definition meshx_err.h:40
@ MESHX_INVALID_ARG
Definition meshx_err.h:42
@ MESHX_INVALID_STATE
Definition meshx_err.h:45
@ MESHX_NO_MEM
Definition meshx_err.h:44
#define MESHX_MALLOC
Definition meshx_err.h:24
#define MESHX_FREE
Definition meshx_err.h:32
#define MESHX_LOGI(module_id, format,...)
Definition meshx_log.h:100
#define MESHX_LOGE(module_id, format,...)
Definition meshx_log.h:73
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:113
meshx_err_t meshx_os_timer_start(const meshx_os_timer_t *timer_handle)
Start a timer.
static meshx_err_t meshx_os_timer_control_task_cb(const dev_struct_t *pdev, control_task_msg_evt_t evt, void *params)
static meshx_err_t meshx_os_timer_unit_test_cb_handler(int cmd_id, int argc, char **argv)
Callback handler for OS Timer unit test command.
#define OS_TIMER_INIT_MAGIC
meshx_err_t meshx_os_timer_init(void)
Initialize the OS timer module.
meshx_err_t meshx_os_timer_create(const char *name, uint32_t period, bool reload, meshx_os_timer_cb_t cb, meshx_os_timer_t **timer_handle)
Create a timer.
meshx_err_t meshx_os_timer_restart(const meshx_os_timer_t *timer_handle)
Restart a timer.
meshx_err_t meshx_os_timer_set_period(meshx_os_timer_t *timer_handle, const uint32_t period_ms)
Set period on an initialised timer.
#define OS_TIMER_CONTROL_TASK_EVT_MASK
Mask for OS timer control task events.
void meshx_os_timer_fire_cb(const void *timer_handle)
static void meshx_os_timer_ut_cb_handler(const meshx_os_timer_t *p_timer)
OS Timer Unit Test Callback handler.
meshx_err_t meshx_os_timer_stop(const meshx_os_timer_t *timer_handle)
Stop a timer.
static struct meshx_os_timer_reg_head meshx_os_timer_reg_table_head
Head of the OS timer registration table.
meshx_err_t meshx_os_timer_delete(meshx_os_timer_t **timer_handle)
Delete a timer.
meshx_os_timer_cli_cmd_t
OS Timer CLI command enumeration.
@ OS_TIMER_CLI_CMD_CREATE
@ OS_TIMER_CLI_CMD_MAX
@ OS_TIMER_CLI_CMD_PERIOD_SET
@ OS_TIMER_CLI_CMD_DISARM
@ OS_TIMER_CLI_CMD_DELETE
@ OS_TIMER_CLI_CMD_ARM
@ OS_TIMER_CLI_CMD_REARM
SLIST_HEAD(meshx_os_timer_reg_head, meshx_os_timer)
Header file for OS timer utilities.
struct meshx_os_timer meshx_os_timer_t
Alias for the meshx_os_timer structure.
void(* meshx_os_timer_cb_t)(const meshx_os_timer_t *p_timer)
Timer callback function prototype.
#define OS_TIMER_SIZE
return meshx_os_timer_t size
#define OS_TMER_GET_TIMER_NAME(timer)
return timer registered name pointer
meshx_err_t meshx_rtos_timer_stop(meshx_rtos_timer_t *timer)
Stops the RTOS timer.
meshx_err_t meshx_rtos_timer_start(meshx_rtos_timer_t *timer)
Starts the RTOS timer.
Definition meshx_timer.c:83
meshx_err_t meshx_rtos_timer_delete(meshx_rtos_timer_t *timer)
Deletes the RTOS timer.
void(* meshx_rtos_timer_callback_t)(void *)
void meshx_os_timer_fire_cb(const void *timer_handle)
meshx_err_t meshx_rtos_timer_change_period(meshx_rtos_timer_t *timer, uint32_t new_period_ms)
Changes the period of the RTOS timer.
meshx_err_t meshx_rtos_timer_reset(meshx_rtos_timer_t *timer)
Resets the RTOS timer.
meshx_err_t meshx_rtos_timer_create(meshx_rtos_timer_t *timer, const char *name, meshx_rtos_timer_callback_t cb, void *arg, uint32_t period_ms, bool reload)
Creates a new RTOS timer.
Definition meshx_timer.c:46
@ MODULE_ID_COMPONENT_OS_TIMER
Definition module_id.h:26
Head of the singly linked list for OS timer control task message parameters.
Structure to hold parameters for the OS timer control task message.
meshx_os_timer_cb_t cb
meshx_os_timer_handle_t timer_handle
meshx_err_t register_unit_test(module_id_t module_id, module_callback_t callback)
Register a unit test for a specific module.
Definition unit_test.c:149
#define UT_GET_ARG(_x, _type, _argv)
Macro to extract an argument from the argument list.
Definition unit_test.h:27