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_utils.c File Reference

Utility functions for integrating MeshX with FreeRTOS. This file provides implementations for memory management, system time retrieval, and heap monitoring using FreeRTOS APIs. More...

#include "interface/rtos/meshx_rtos_utils.h"
#include "interface/logging/meshx_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/portmacro.h"
#include <string.h>
Include dependency graph for meshx_utils.c:

Go to the source code of this file.

Functions

meshx_err_t meshx_rtos_get_sys_time (unsigned int *millis)
 Retrieves the current system time in milliseconds.
 
meshx_err_t meshx_rtos_malloc (void **ptr, size_t size)
 Allocates memory dynamically in a thread-safe manner using FreeRTOS.
 
meshx_err_t meshx_rtos_calloc (void **ptr, size_t num, size_t size)
 Allocates memory for an array of elements and initializes it to zero.
 
meshx_err_t meshx_rtos_free (void **ptr)
 Frees memory allocated to a pointer and sets it to NULL.
 
size_t meshx_rtos_get_free_heap (void)
 Retrieves the amount of free heap memory available in the system.
 
meshx_err_t meshx_rtos_get_curr_task_id_prio (unsigned int *task_id)
 Retrieves the current task ID.
 

Detailed Description

Utility functions for integrating MeshX with FreeRTOS. This file provides implementations for memory management, system time retrieval, and heap monitoring using FreeRTOS APIs.

Copyright (c) 2024 - 2025 MeshX

Author
Pranjal Chanda

Definition in file meshx_utils.c.

Function Documentation

◆ meshx_rtos_calloc()

meshx_err_t meshx_rtos_calloc ( void ** ptr,
size_t num,
size_t size )

Allocates memory for an array of elements and initializes it to zero.

This function allocates memory for an array of num elements, each of size size, and initializes all bytes in the allocated memory to zero. The allocated memory pointer is returned via the ptr parameter.

Parameters
[out]ptrPointer to the allocated memory. This will be set to NULL if the allocation fails.
[in]numNumber of elements to allocate.
[in]sizeSize of each element in bytes.
Returns
  • MESHX_SUCCESS: Memory allocation was successful.
  • MESHX_ERR_NO_MEM: Memory allocation failed due to insufficient memory.

Definition at line 96 of file meshx_utils.c.

97{
98 if (!ptr || num == 0 || size == 0)
99 {
100 return MESHX_INVALID_ARG; // Invalid input
101 }
102
103 *ptr = pvPortMalloc(size);
104 if (*ptr == NULL)
105 {
106 return MESHX_NO_MEM; // Memory allocation failed
107 }
108
109 memset(*ptr, 0, num * size); // Initialize allocated memory to zero
110
111 return MESHX_SUCCESS;
112}
@ MESHX_SUCCESS
Definition meshx_err.h:40
@ MESHX_INVALID_ARG
Definition meshx_err.h:42
@ MESHX_NO_MEM
Definition meshx_err.h:44

◆ meshx_rtos_free()

meshx_err_t meshx_rtos_free ( void ** ptr)

Frees memory allocated to a pointer and sets it to NULL.

This function is used to safely deallocate memory that was previously allocated and ensures that the pointer is set to NULL to avoid dangling pointer issues.

Parameters
[in,out]ptrA double pointer to the memory to be freed. After the memory is freed, the pointer is set to NULL.
Returns
  • MESHX_SUCCESS on successful deallocation.
  • MESHX_ERR_INVALID_ARG if the provided pointer is NULL or invalid.
  • Other error codes depending on the implementation.

Definition at line 129 of file meshx_utils.c.

130{
131 if (ptr && *ptr)
132 { // Ensure pointer is valid
133 vPortFree(*ptr); // Free the actual allocated memory
134 *ptr = NULL; // Set pointer to NULL to avoid dangling reference
135 }
136 MESHX_LOGD(MODULE_ID_COMMON, "ESP Heap available: %d", meshx_rtos_get_free_heap());
137 return MESHX_SUCCESS;
138}
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:113
size_t meshx_rtos_get_free_heap(void)
Retrieves the amount of free heap memory available in the system.
@ MODULE_ID_COMMON
Definition module_id.h:32
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_rtos_get_curr_task_id_prio()

meshx_err_t meshx_rtos_get_curr_task_id_prio ( unsigned int * task_id)

Retrieves the current task ID.

This function retrieves the current task ID using FreeRTOS APIs. The task ID is stored in the variable pointed to by the task_id parameter.

Parameters
[out]task_idPointer to an unsigned integer where the task ID will be stored.
Returns
  • MESHX_SUCCESS: If the task ID was successfully retrieved.
  • MESHX_ERR_INVALID_ARG: If the provided pointer is NULL or invalid.
Note
Ensure that the task_id pointer is valid and not NULL before calling this function.

Definition at line 170 of file meshx_utils.c.

171{
172
173#ifdef configUSE_TRACE_FACILITY
174 TaskStatus_t xTaskDetails;
175 vTaskGetInfo(NULL, &xTaskDetails, pdTRUE, eInvalid);
176 *task_id = xTaskDetails.xTaskNumber << 8 | xTaskDetails.uxCurrentPriority;
177#else
178 *task_id = 0x5A; /* Dummy Task ID*/
179#endif
180 return MESHX_SUCCESS;
181
182}
Here is the caller graph for this function:

◆ meshx_rtos_get_free_heap()

size_t meshx_rtos_get_free_heap ( void )

Retrieves the amount of free heap memory available in the system.

This function is used to query the current amount of free heap memory available in the system. It is useful for monitoring memory usage and ensuring that the system has sufficient resources for dynamic memory allocation.

Returns
size_t The amount of free heap memory in bytes.

Definition at line 150 of file meshx_utils.c.

151{
152 return xPortGetFreeHeapSize();
153}
Here is the caller graph for this function:

◆ meshx_rtos_get_sys_time()

meshx_err_t meshx_rtos_get_sys_time ( unsigned int * millis)

Retrieves the current system time in milliseconds.

This function calculates the system time in milliseconds based on the FreeRTOS tick count and the configured tick rate. The result is stored in the variable pointed to by the millis parameter.

Parameters
[out]millisPointer to an unsigned integer where the system time in milliseconds will be stored.
Returns
  • MESHX_SUCCESS: If the system time was successfully retrieved.
Note
Ensure that the millis pointer is valid and not NULL before calling this function.

Definition at line 36 of file meshx_utils.c.

37{
38 if(xPortInIsrContext() == pdFALSE)
39 {
40 *millis = xTaskGetTickCount() * (1000 / configTICK_RATE_HZ);
41 }
42 else
43 {
44 *millis = xTaskGetTickCountFromISR() * (1000 / configTICK_RATE_HZ);
45 }
46 return MESHX_SUCCESS;
47}
Here is the caller graph for this function:

◆ meshx_rtos_malloc()

meshx_err_t meshx_rtos_malloc ( void ** ptr,
size_t size )

Allocates memory dynamically in a thread-safe manner using FreeRTOS.

This function wraps the memory allocation process to ensure compatibility with the FreeRTOS environment. It allocates a block of memory of the specified size and assigns the pointer to the provided pointer variable.

Parameters
[out]ptrPointer to the memory location where the allocated memory address will be stored. Must not be NULL.
[in]sizeThe size of the memory block to allocate, in bytes.
Returns
  • MESHX_SUCCESS on successful memory allocation.
  • MESHX_ERR_NO_MEM if memory allocation fails.
  • Other error codes as defined in the meshx_err_t enumeration.

Definition at line 65 of file meshx_utils.c.

66{
67 if (!ptr || size == 0)
68 {
69 return MESHX_INVALID_ARG; // Invalid input
70 }
71
72 *ptr = pvPortMalloc(size);
73 if (*ptr == NULL)
74 {
75 return MESHX_NO_MEM; // Memory allocation failed
76 }
77
78 return MESHX_SUCCESS;
79}
Here is the caller graph for this function: