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

Implementation of the MeshX logging system. More...

#include "meshx_log.h"
#include "interface/meshx_platform.h"
#include "module_id.h"
#include "interface/rtos/meshx_rtos_utils.h"
#include "interface/rtos/meshx_msg_q.h"
#include "interface/rtos/meshx_task.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>

Macros

#define CONFIG_MESHX_LOG_BUF_SIZE   128

Functions

static uint8_t calculate_xor_parity (const uint8_t *data, uint16_t len)
 Calculates XOR parity for a buffer.
meshx_err_t meshx_logging_init (const meshx_logging_t *config)
void meshx_module_set_log_level (module_id_t module_id, meshx_log_level_t log_level)
static void vmeshx_log_packet (module_id_t module_id, meshx_log_level_t log_level, const char *file, int line_no, const char *fmt, va_list args)
void meshx_log_packet (module_id_t module_id, meshx_log_level_t log_level, const char *file, int line_no, const char *fmt,...)
 Logs a packetized binary log message.
void meshx_log_printf (module_id_t module_id, meshx_log_level_t log_level, const char *func, int line_no, const char *fmt,...)
 Legacy printf-style logging for external components.

Variables

static meshx_log_level_t module_log_level [MODULE_ID_MAX]
static meshx_logging_t meshx_logging_ctrl

Detailed Description

Implementation of the MeshX logging system.

This file contains the implementation of the logging system for the MeshX application. It provides functions to initialize the logging system, set logging levels for specific modules, and log formatted messages with different log levels and colors.

Macro Definition Documentation

◆ CONFIG_MESHX_LOG_BUF_SIZE

#define CONFIG_MESHX_LOG_BUF_SIZE   128

Function Documentation

◆ calculate_xor_parity()

uint8_t calculate_xor_parity ( const uint8_t * data,
uint16_t len )
static

Calculates XOR parity for a buffer.

79{
80 uint8_t parity = 0;
81 for (uint16_t i = 0; i < len; i++)
82 {
83 parity ^= data[i];
84 }
85 return parity;
86}

◆ meshx_log_packet()

void meshx_log_packet ( module_id_t module_id,
meshx_log_level_t log_level,
const char * file,
int line_no,
const char * fmt,
... )

Logs a packetized binary log message.

Note
This is called by the MESHX_LOG macro.
223{
224 va_list args;
225 va_start(args, fmt);
226 vmeshx_log_packet(module_id, log_level, file, line_no, fmt, args);
227 va_end(args);
228}
static void vmeshx_log_packet(module_id_t module_id, meshx_log_level_t log_level, const char *file, int line_no, const char *fmt, va_list args)
Definition meshx_log.c:132

◆ meshx_log_printf()

void meshx_log_printf ( module_id_t module_id,
meshx_log_level_t log_level,
const char * func,
int line_no,
const char * fmt,
... )

Legacy printf-style logging for external components.

232{
233 va_list args;
234 va_start(args, fmt);
235 vmeshx_log_packet(module_id, log_level, func, line_no, fmt, args);
236 va_end(args);
237}

◆ meshx_logging_init()

meshx_err_t meshx_logging_init ( const meshx_logging_t * config)
89{
90 if (!config)
91 return MESHX_INVALID_ARG;
92
93 meshx_logging_ctrl.def_log_level = config->def_log_level;
94 for (size_t i = 0; i < MODULE_ID_MAX; i++)
95 {
97 }
98
99#if CONFIG_MESHX_LOG_THREADED
100 if (!log_threaded_init_done)
101 {
102 meshx_err_t err = meshx_msg_q_create(&log_msg_q);
103 if (err != MESHX_SUCCESS)
104 return err;
105
106 static meshx_task_t log_task;
107 log_task.task_cb = meshx_log_task_handler;
108 log_task.task_name = "meshx_log_task";
109 log_task.stack_size = CONFIG_MESHX_LOG_STACK_SIZE;
111 log_task.arg = NULL;
112
113 err = meshx_task_create(&log_task);
114 if (err != MESHX_SUCCESS)
115 return err;
116
117 log_threaded_init_done = true;
118 }
119#endif
120
121 return MESHX_SUCCESS;
122}
#define CONFIG_MESHX_LOG_TASK_PRIO
Logging task priority configuration.
Definition meshx_control_task.h:43
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
@ MESHX_INVALID_ARG
Definition meshx_err.h:46
static meshx_logging_t meshx_logging_ctrl
Definition meshx_log.c:73
static meshx_log_level_t module_log_level[MODULE_ID_MAX]
Definition meshx_log.c:72
**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
meshx_err_t meshx_msg_q_create(meshx_msg_q_t *msg_q_handle)
Create a MeshX Message Queue.
struct meshx_task meshx_task_t
MeshX Task Structure.
meshx_err_t meshx_task_create(meshx_task_t *task_handle)
Create a MeshX Task.
@ MODULE_ID_MAX
Definition module_id.h:51
meshx_log_level_t def_log_level
Definition meshx_log.h:38
const char * task_name
Definition meshx_task.h:34
size_t stack_size
Definition meshx_task.h:36
int priority
Definition meshx_task.h:37
void * arg
Definition meshx_task.h:35
meshx_task_cb_t task_cb
Definition meshx_task.h:38

◆ meshx_module_set_log_level()

void meshx_module_set_log_level ( module_id_t module_id,
meshx_log_level_t log_level )
125{
126 if (module_id < MODULE_ID_MAX)
127 {
128 module_log_level[module_id] = log_level;
129 }
130}

◆ vmeshx_log_packet()

void vmeshx_log_packet ( module_id_t module_id,
meshx_log_level_t log_level,
const char * file,
int line_no,
const char * fmt,
va_list args )
static
134{
135 /* Validate module ID and log level */
136 if (module_id >= MODULE_ID_MAX || log_level < meshx_logging_ctrl.def_log_level ||
137 module_log_level[module_id] < log_level)
138 {
139 return;
140 }
141
142#if CONFIG_MESHX_LOG_THREADED
143 meshx_log_msg_t msg;
144 memset(&msg, 0, sizeof(msg));
145 meshx_log_packet_t *pkt = (meshx_log_packet_t *)msg.data;
146 msg.len = sizeof(meshx_log_packet_t);
147#else
148 uint8_t buf[sizeof(meshx_log_packet_t)];
150 memset(buf, 0, sizeof(buf));
151#endif
152
153 unsigned int timestamp = 0;
154 meshx_rtos_get_sys_time(&timestamp);
155
156 pkt->sync = MESHX_LOG_SYNC_WORD;
157 pkt->level = (uint8_t)log_level;
158 pkt->module_id = (uint8_t)module_id;
159 pkt->timestamp = (uint32_t)timestamp;
160 pkt->fmt_addr = (uint32_t)fmt;
161 pkt->file_addr = (uint32_t)file;
162 pkt->line_no = (uint16_t)line_no;
163
164 /* Pack arguments */
165 va_list args_copy;
166 va_copy(args_copy, args);
167 for (int i = 0; i < 16; i++) {
168 pkt->args[i] = va_arg(args_copy, uint32_t);
169 }
170 va_end(args_copy);
171
172 /* Simple scan for %s to support dynamic string inlining */
173 const char *p = fmt;
174 int arg_idx = 0;
175 bool found_s = false;
176 while (*p) {
177 if (*p == '%' && *(p+1) != '%') {
178 p++;
179 // Basic skip of common modifiers
180 while (*p && (isdigit((int)*p) || strchr("-+ #.0123456789", *p))) p++;
181 while (*p && strchr("lhjzL", *p)) p++;
182
183 if (*p == 's') {
184 found_s = true;
185 break;
186 }
187 arg_idx++;
188 if (arg_idx >= 16) break;
189 }
190 p++;
191 }
192
193 if (found_s && arg_idx < 16) {
194 uint32_t str_ptr = pkt->args[arg_idx];
195 /* Check if pointer is likely in RAM (ESP32-C3 DRAM: 0x3FC80000 - 0x3FCE0000) */
196 /* Note: This is a heuristic. In a full system, use platform-specific RAM checks. */
197 if (str_ptr >= 0x3FC00000 && str_ptr < 0x40000000) {
198 strncpy(pkt->inline_str, (const char *)str_ptr, sizeof(pkt->inline_str) - 1);
199 pkt->inline_str[sizeof(pkt->inline_str) - 1] = '\0';
200 }
201 }
202
203 pkt->len = sizeof(meshx_log_packet_t) - 3; // Length excluding sync and len
204
205 pkt->parity = calculate_xor_parity((uint8_t *)pkt, sizeof(meshx_log_packet_t) - 1);
206
207#if CONFIG_MESHX_LOG_THREADED
208 if (log_threaded_init_done)
209 {
210 /* Use a small wait to avoid dropping logs if queue is momentarily full */
211 if (meshx_msg_q_send(&log_msg_q, &msg, sizeof(msg), 10) == MESHX_SUCCESS) {
212 return;
213 }
214 }
215#endif
216
217 /* Fallback: direct output if not threaded or queue full/not ready */
218 meshx_platform_console_write((const char *)pkt, sizeof(meshx_log_packet_t));
219}
static uint8_t calculate_xor_parity(const uint8_t *data, uint16_t len)
Calculates XOR parity for a buffer.
Definition meshx_log.c:78
#define MESHX_LOG_SYNC_WORD
Definition meshx_log.h:70
meshx_err_t meshx_msg_q_send(meshx_msg_q_t *msg_q_handle, void const *msg, size_t msg_len, uint32_t delay_ms)
Send a Message to a MeshX Message Queue Back.
void meshx_platform_console_write(const char *data, uint16_t len)
Writes data to the console interface.
Definition esp_platform.c:178
meshx_err_t meshx_rtos_get_sys_time(unsigned int *millis)
Retrieves the current system time in milliseconds.
TLV Log Packet structure.
Definition meshx_log.h:85

Variable Documentation

◆ meshx_logging_ctrl

meshx_logging_t meshx_logging_ctrl
static

◆ module_log_level

meshx_log_level_t module_log_level[MODULE_ID_MAX]
static