MeshX 0.4
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"

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

Macro Definition Documentation

◆ OS_TIMER_SIZE

#define OS_TIMER_SIZE   sizeof(meshx_os_timer_t)

return meshx_os_timer_t size

◆ OS_TMER_GET_TIMER_NAME

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

return timer registered name pointer

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.

◆ 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.

◆ 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.

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:43
struct meshx_os_timer meshx_os_timer_t
Alias for the meshx_os_timer structure.
Definition meshx_os_timer.h:47
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.
Definition meshx_os_timer.c:264
Returns
MESHX_SUCCESS on success, or an error code on failure.
270{
271 if (timer_handle == NULL)
272 {
273 return MESHX_INVALID_STATE;
274 }
275
276 if ((*timer_handle) != NULL && (*timer_handle)->init == OS_TIMER_INIT_MAGIC)
277 return MESHX_INVALID_STATE;
278
280
281 *timer_handle = (meshx_os_timer_t *)MESHX_MALLOC(OS_TIMER_SIZE);
282 if (*timer_handle == NULL)
283 return MESHX_NO_MEM;
284
285 (*timer_handle)->cb = cb;
286 (*timer_handle)->period = period;
287
288 err = meshx_rtos_timer_create(&(*timer_handle)->timer_handle,
289 name,
291 *timer_handle,
292 period,
293 reload);
294 if (err)
295 {
296 MESHX_FREE(*timer_handle);
297 return err;
298 }
299
300 SLIST_INSERT_HEAD(&meshx_os_timer_reg_table_head, (*timer_handle), next);
301 (*timer_handle)->init = OS_TIMER_INIT_MAGIC;
302
303 return err;
304}
@ MESHX_INVALID_STATE
Definition meshx_err.h:49
@ MESHX_NO_MEM
Definition meshx_err.h:48
#define MESHX_MALLOC
Definition meshx_err.h:28
#define MESHX_FREE
Definition meshx_err.h:36
**This function is called by the parent element when a state change request *is received It validates the request and returns a result to the element not the model layer **return * MESHX_SUCCESS
Definition meshx_model_level.cpp:385
#define OS_TIMER_INIT_MAGIC
Definition meshx_os_timer.c:16
static struct meshx_os_timer_reg_head meshx_os_timer_reg_table_head
Head of the OS timer registration table.
Definition meshx_os_timer.c:44
#define OS_TIMER_SIZE
return meshx_os_timer_t size
Definition meshx_os_timer.h:24
void(* meshx_rtos_timer_callback_t)(void *)
Definition meshx_rtos_timer.h:24
void meshx_os_timer_fire_cb(const void *timer_handle)
Definition meshx_os_timer.c:149
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.

◆ 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.
420{
421 meshx_err_t err;
422
423 if (timer_handle == NULL)
424 {
425 return MESHX_INVALID_STATE;
426 }
427
428 if (*timer_handle == NULL || (*timer_handle)->init != OS_TIMER_INIT_MAGIC)
429 return MESHX_INVALID_STATE;
430
431 MESHX_LOGI(MODULE_ID_COMPONENT_OS_TIMER, "Deleting timer %s", OS_TMER_GET_TIMER_NAME((*timer_handle)));
432
433 err = meshx_rtos_timer_delete(&(*timer_handle)->timer_handle);
434 if (err)
435 return err;
436
437 (*timer_handle)->init = 0;
438
439 SLIST_REMOVE(&meshx_os_timer_reg_table_head, *timer_handle, meshx_os_timer, next);
440 MESHX_FREE(*timer_handle);
441
442 return MESHX_SUCCESS;
443}
#define MESHX_LOGI(module_id, format,...)
Definition meshx_log.h:126
#define OS_TMER_GET_TIMER_NAME(timer)
return timer registered name pointer
Definition meshx_os_timer.h:29
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:30
Structure to hold parameters for the OS timer control task message.
Definition meshx_os_timer.h:66

◆ 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.
228{
229 meshx_err_t err;
230#if CONFIG_ENABLE_UNIT_TEST
232 if (err)
233 {
234 MESHX_LOGE(MODULE_ID_COMPONENT_OS_TIMER, "unit_test reg failed: (%d)", err);
235 return err;
236 }
237#endif /* CONFIG_ENABLE_UNIT_TEST */
242
243 return err;
244}
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.
Definition meshx_control_task.h:188
@ CONTROL_TASK_MSG_CODE_SYSTEM
Definition meshx_control_task.h:66
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.
Definition meshx_control_task.c:142
#define MESHX_LOGE(module_id, format,...)
Definition meshx_log.h:114
static meshx_err_t meshx_os_timer_control_task_cb(const dev_struct_t *pdev, control_task_msg_evt_t evt, void *params)
Definition meshx_os_timer.c:176
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.
Definition meshx_os_timer.c:86
#define OS_TIMER_CONTROL_TASK_EVT_MASK
Mask for OS timer control task events.
Definition meshx_os_timer.c:26
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:137

◆ 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.
341{
342 if (timer_handle == NULL)
343 {
344 return MESHX_INVALID_STATE;
345 }
346 if (timer_handle->init != OS_TIMER_INIT_MAGIC)
347 return MESHX_INVALID_STATE;
348
352 timer_handle,
354}
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.
Definition meshx_control_task.c:84
CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_REARM
Definition meshx_control_task.h:127
uint16_t init
Definition meshx_os_timer.h:67

◆ 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.
367{
368 if (timer_handle == NULL)
369 {
370 return MESHX_INVALID_STATE;
371 }
372 if (timer_handle->init != OS_TIMER_INIT_MAGIC)
373 return MESHX_INVALID_STATE;
374
375 timer_handle->period = period_ms;
376
380 timer_handle,
382}
CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_PERIOD
Definition meshx_control_task.h:130
uint32_t period
Definition meshx_os_timer.h:68

◆ 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.
316{
317 if (timer_handle == NULL)
318 {
319 return MESHX_INVALID_STATE;
320 }
321 if (timer_handle->init != OS_TIMER_INIT_MAGIC)
322 return MESHX_INVALID_STATE;
323
327 timer_handle,
329}
CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_ARM
Definition meshx_control_task.h:126

◆ 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.
394{
395 if (timer_handle == NULL)
396 {
397 return MESHX_INVALID_STATE;
398 }
399 if (timer_handle->init != OS_TIMER_INIT_MAGIC)
400 return MESHX_INVALID_STATE;
401
405 timer_handle,
407}
CONTROL_TASK_MSG_EVT_SYSTEM_TIMER_DISARM
Definition meshx_control_task.h:128