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
Go to the documentation of this file.
1
12
15#include "freertos/FreeRTOS.h"
16#include "freertos/task.h"
17#include "freertos/portmacro.h"
18#include <string.h>
19
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}
48
65meshx_err_t meshx_rtos_malloc(void **ptr, size_t size)
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}
80
96meshx_err_t meshx_rtos_calloc(void **ptr, size_t num, size_t size)
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}
113
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}
139
151{
152 return xPortGetFreeHeapSize();
153}
154
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}
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_NO_MEM
Definition meshx_err.h:44
Logging interface for MeshX with color-coded output.
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:113
Utility functions for RTOS operations in the MeshX framework.
meshx_err_t meshx_rtos_get_sys_time(unsigned int *millis)
Retrieves the current system time in milliseconds.
Definition meshx_utils.c:36
meshx_err_t meshx_rtos_get_curr_task_id_prio(unsigned int *task_id)
Retrieves the current task ID.
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.
Definition meshx_utils.c:96
meshx_err_t meshx_rtos_free(void **ptr)
Frees memory allocated to a pointer and sets it to NULL.
meshx_err_t meshx_rtos_malloc(void **ptr, size_t size)
Allocates memory dynamically in a thread-safe manner using FreeRTOS.
Definition meshx_utils.c:65
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