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 File Reference

Implementation of OS timer functionalities for BLE mesh node. More...

#include "meshx_os_timer.h"
Include dependency graph for meshx_os_timer.c:

Go to the source code of this file.

Macros

#define OS_TIMER_INIT_MAGIC   0x3892
 
#define OS_TIMER_CONTROL_TASK_EVT_MASK
 Mask for OS timer control task events.
 

Enumerations

enum  meshx_os_timer_cli_cmd_t {
  OS_TIMER_CLI_CMD_CREATE ,
  OS_TIMER_CLI_CMD_ARM ,
  OS_TIMER_CLI_CMD_REARM ,
  OS_TIMER_CLI_CMD_DISARM ,
  OS_TIMER_CLI_CMD_DELETE ,
  OS_TIMER_CLI_CMD_PERIOD_SET ,
  OS_TIMER_CLI_CMD_MAX
}
 OS Timer CLI command enumeration. More...
 

Functions

 SLIST_HEAD (meshx_os_timer_reg_head, meshx_os_timer)
 
static void meshx_os_timer_ut_cb_handler (const meshx_os_timer_t *p_timer)
 OS Timer Unit Test Callback handler.
 
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.
 
void meshx_os_timer_fire_cb (const void *timer_handle)
 
static meshx_err_t meshx_os_timer_control_task_cb (const dev_struct_t *pdev, control_task_msg_evt_t evt, void *params)
 
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_start (const meshx_os_timer_t *timer_handle)
 Start 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.
 
meshx_err_t meshx_os_timer_stop (const meshx_os_timer_t *timer_handle)
 Stop a timer.
 
meshx_err_t meshx_os_timer_delete (meshx_os_timer_t **timer_handle)
 Delete a timer.
 

Variables

static struct meshx_os_timer_reg_head meshx_os_timer_reg_table_head = SLIST_HEAD_INITIALIZER(os_timer_reg_table_head)
 Head of the OS timer registration table.
 

Detailed Description

Implementation of OS timer functionalities for BLE mesh node.

Copyright © 2024 - 2025 MeshX

This file contains the implementation of the OS timer functionalities used in the BLE mesh node application. It includes the necessary includes, definitions, and initialization of the timer registration table.

Author
Pranjal Chanda

Definition in file meshx_os_timer.c.

Macro Definition Documentation

◆ OS_TIMER_CONTROL_TASK_EVT_MASK

#define OS_TIMER_CONTROL_TASK_EVT_MASK
Value:

Mask for OS timer control task events.

This mask includes the following events:

  • CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_ARM
  • CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_DISARM
  • CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_FIRE

Definition at line 25 of file meshx_os_timer.c.

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)

◆ OS_TIMER_INIT_MAGIC

#define OS_TIMER_INIT_MAGIC   0x3892

Definition at line 15 of file meshx_os_timer.c.

Enumeration Type Documentation

◆ meshx_os_timer_cli_cmd_t

OS Timer CLI command enumeration.

Enumerator
OS_TIMER_CLI_CMD_CREATE 
OS_TIMER_CLI_CMD_ARM 
OS_TIMER_CLI_CMD_REARM 
OS_TIMER_CLI_CMD_DISARM 
OS_TIMER_CLI_CMD_DELETE 
OS_TIMER_CLI_CMD_PERIOD_SET 
OS_TIMER_CLI_CMD_MAX 

Definition at line 50 of file meshx_os_timer.c.

51{
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

Function Documentation

◆ meshx_os_timer_control_task_cb()

static meshx_err_t meshx_os_timer_control_task_cb ( const dev_struct_t * pdev,
control_task_msg_evt_t evt,
void * params )
static

Definition at line 175 of file meshx_os_timer.c.

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}
#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
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:113
struct meshx_os_timer meshx_os_timer_t
Alias for the meshx_os_timer structure.
#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_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.
@ MODULE_ID_COMPONENT_OS_TIMER
Definition module_id.h:26
meshx_os_timer_cb_t cb
meshx_os_timer_handle_t timer_handle
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_os_timer_create()

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.

This function creates a timer with the given period and callback function.

Parameters
[in]nameThe name of the timer.
[in]periodThe period of the timer in milliseconds.
[in]reloadIf true, the timer will automatically reload after expiring.
[in]cbThe callback function to be called when the timer expires.
[in,out]timer_handleThe timer handle.

Example:

meshx_os_timer_t * meshx_os_timer_inst;
meshx_err_t err = meshx_os_timer_create("Example_Timer", 1000, 1, &example_os_timer_cb, &os_timer_inst);
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.
Returns
MESHX_SUCCESS on success, or an error code on failure.

Definition at line 263 of file meshx_os_timer.c.

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}
@ 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 OS_TIMER_INIT_MAGIC
static struct meshx_os_timer_reg_head meshx_os_timer_reg_table_head
Head of the OS timer registration table.
#define OS_TIMER_SIZE
return meshx_os_timer_t size
void(* meshx_rtos_timer_callback_t)(void *)
void meshx_os_timer_fire_cb(const void *timer_handle)
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_os_timer_delete()

meshx_err_t meshx_os_timer_delete ( meshx_os_timer_t ** timer_handle)

Delete a timer.

This function deletes the given timer.

Parameters
timer_handleThe timer handle.
Returns
MESHX_SUCCESS on success, or an error code on failure.

Definition at line 418 of file meshx_os_timer.c.

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}
#define MESHX_LOGI(module_id, format,...)
Definition meshx_log.h:100
meshx_err_t meshx_rtos_timer_delete(meshx_rtos_timer_t *timer)
Deletes the RTOS timer.
Structure to hold parameters for the OS timer control task message.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_os_timer_fire_cb()

void meshx_os_timer_fire_cb ( const void * timer_handle)

Definition at line 148 of file meshx_os_timer.c.

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}
@ CONTROL_TASK_MSG_CODE_SYSTEM
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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_os_timer_init()

meshx_err_t meshx_os_timer_init ( void )

Initialize the OS timer module.

This function initializes the OS timer module.

Returns
MESHX_SUCCESS on success, or an error code on failure.

Definition at line 226 of file meshx_os_timer.c.

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}
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.
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_LOGE(module_id, format,...)
Definition meshx_log.h:73
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_CONTROL_TASK_EVT_MASK
Mask for OS timer control task events.
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_os_timer_restart()

meshx_err_t meshx_os_timer_restart ( const meshx_os_timer_t * timer_handle)

Restart a timer.

This function re-starts the given timer.

Parameters
timer_handleThe timer handle.
Returns
MESHX_SUCCESS on success, or an error code on failure.

Definition at line 339 of file meshx_os_timer.c.

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}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_os_timer_set_period()

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.

This function reset period of initialised timer.

Parameters
timer_handleThe timer handle.
period_msNew period in ms
Returns
MESHX_SUCCESS on success, or an error code on failure.

Definition at line 365 of file meshx_os_timer.c.

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}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_os_timer_start()

meshx_err_t meshx_os_timer_start ( const meshx_os_timer_t * timer_handle)

Start a timer.

This function starts the given timer.

Parameters
timer_handleThe timer handle.
Returns
MESHX_SUCCESS on success, or an error code on failure.

Definition at line 314 of file meshx_os_timer.c.

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}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_os_timer_stop()

meshx_err_t meshx_os_timer_stop ( const meshx_os_timer_t * timer_handle)

Stop a timer.

This function stops the given timer.

Parameters
timer_handleThe timer handle.
Returns
MESHX_SUCCESS on success, or an error code on failure.

Definition at line 392 of file meshx_os_timer.c.

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}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_os_timer_unit_test_cb_handler()

static meshx_err_t meshx_os_timer_unit_test_cb_handler ( int cmd_id,
int argc,
char ** argv )
static

Callback handler for OS Timer unit test command.

This function handles OS Timer unit test command by processing the provided command ID and arguments.

Parameters
[in]cmd_idThe command ID to be processed.
[in]argcThe number of arguments provided.
[in]argvThe array of arguments.
Returns
  • MESHX_SUCCESS: Success
  • MESHX_INVALID_ARG: Invalid arguments
  • Other error codes depending on the implementation

Definition at line 85 of file meshx_os_timer.c.

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}
meshx_err_t meshx_os_timer_start(const meshx_os_timer_t *timer_handle)
Start a timer.
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.
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.
meshx_err_t meshx_os_timer_delete(meshx_os_timer_t **timer_handle)
Delete a timer.
#define UT_GET_ARG(_x, _type, _argv)
Macro to extract an argument from the argument list.
Definition unit_test.h:27
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_os_timer_ut_cb_handler()

static void meshx_os_timer_ut_cb_handler ( const meshx_os_timer_t * p_timer)
static

OS Timer Unit Test Callback handler.

Parameters
[in]p_timerCallback params

Definition at line 65 of file meshx_os_timer.c.

66{
68}
Here is the caller graph for this function:

◆ SLIST_HEAD()

Variable Documentation

◆ meshx_os_timer_reg_table_head

struct meshx_os_timer_reg_head meshx_os_timer_reg_table_head = SLIST_HEAD_INITIALIZER(os_timer_reg_table_head)
static

Head of the OS timer registration table.

This is initialized using the SLIST_HEAD_INITIALIZER macro.

Definition at line 43 of file meshx_os_timer.c.