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

Header file for OS timer utilities. More...

#include <stdint.h>
#include "sys/queue.h"
#include "meshx_control_task.h"
#include "interface/rtos/meshx_rtos_timer.h"
Include dependency graph for meshx_os_timer.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  meshx_os_timer
 Structure to hold parameters for the OS timer control task message. More...
 

Macros

#define OS_TIMER_SIZE   sizeof(meshx_os_timer_t)
 return meshx_os_timer_t size
 
#define OS_TMER_GET_TIMER_NAME(timer)
 return timer registered name pointer
 

Typedefs

typedef meshx_rtos_timer_t meshx_os_timer_handle_t
 Alias for the meshx_rtos_timer_t type.
 
typedef struct meshx_os_timer meshx_os_timer_t
 Alias for the meshx_os_timer structure.
 
typedef void(* meshx_os_timer_cb_t) (const meshx_os_timer_t *p_timer)
 Timer callback function prototype.
 

Functions

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.
 

Detailed Description

Header file for OS timer utilities.

Copyright © 2024 - 2025 MeshX

This file contains the definitions and includes necessary for working with OS timers in the ESP32 BLE mesh node application.

Author
Pranjal Chanda

Definition in file meshx_os_timer.h.

Macro Definition Documentation

◆ OS_TIMER_SIZE

#define OS_TIMER_SIZE   sizeof(meshx_os_timer_t)

return meshx_os_timer_t size

Definition at line 24 of file meshx_os_timer.h.

◆ OS_TMER_GET_TIMER_NAME

#define OS_TMER_GET_TIMER_NAME ( timer)
Value:
(timer->timer_handle.timer_name)

return timer registered name pointer

Definition at line 29 of file meshx_os_timer.h.

Typedef Documentation

◆ meshx_os_timer_cb_t

typedef void(* meshx_os_timer_cb_t) (const meshx_os_timer_t *p_timer)

Timer callback function prototype.

This function is called when the timer expires.

Parameters
[in]p_timerThe timer handle.

Definition at line 55 of file meshx_os_timer.h.

◆ meshx_os_timer_handle_t

Alias for the meshx_rtos_timer_t type.

This typedef provides a more convenient name for the FreeRTOS timer handle type, used for creating and managing timers.

Definition at line 38 of file meshx_os_timer.h.

◆ meshx_os_timer_t

Alias for the meshx_os_timer structure.

This typedef provides a more convenient name for the meshx_os_timer structure, which holds the parameters for the OS timer control task message.

Definition at line 47 of file meshx_os_timer.h.

Function Documentation

◆ 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 Error Codes.
Definition meshx_err.h:39
struct meshx_os_timer meshx_os_timer_t
Alias for the meshx_os_timer structure.
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_SUCCESS
Definition meshx_err.h:40
@ 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
#define OS_TMER_GET_TIMER_NAME(timer)
return timer registered name pointer
meshx_err_t meshx_rtos_timer_delete(meshx_rtos_timer_t *timer)
Deletes the RTOS timer.
@ MODULE_ID_COMPONENT_OS_TIMER
Definition module_id.h:26
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_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.
@ CONTROL_TASK_MSG_CODE_SYSTEM
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}
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
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}
CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_PERIOD
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}
CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_ARM
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}
CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_DISARM
Here is the call graph for this function:
Here is the caller graph for this function: