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

MeshX Serial Protocol (MXSP) Implementation. More...

Macros

#define MESHX_UART_RX_TASK_STACK_SIZE   2048
#define MESHX_UART_RX_TASK_PRIO   5
 Priority level for the UART RX task.

Enumerations

enum  {
  STATE_SOF ,
  STATE_LEN ,
  STATE_TYPE ,
  STATE_PAYLOAD ,
  STATE_CHECKSUM ,
  STATE_EOF
}

Functions

static uint8_t calculate_checksum (uint8_t len, uint8_t type, const uint8_t *payload)
 Calculate checksum for a given frame.
void meshx_platform_serial_write (const uint8_t *data, uint16_t len)
 Writes data to the serial interface.
int32_t meshx_platform_serial_read (uint8_t *data, uint16_t len)
 Reads data from the serial interface.
void meshx_platform_reset (void)
 Resets the MeshX platform. This function performs a system reset, restarting the platform.
static void mxsp_handle_gpio_cmd (const uint8_t *payload, uint8_t len)
 Handle GPIO command from host.
static void mxsp_handle_gpio_evt_from_host (const uint8_t *payload, uint8_t len)
 Handle GPIO event from host (interrupt notification).
static uint32_t meshx_get_sys_time_ms (void)
 Get system time in milliseconds.
meshx_err_t mxsp_send_frame (mxsp_msg_type_t type, const uint8_t *payload, uint8_t len)
meshx_err_t mxsp_send_ctrl_event (const meshx_ctrl_msg_header_t *evt_header, const meshx_ctrl_payload_t *payload)
 Send a control event via MXSP.
meshx_err_t mxsp_send_data_event (const meshx_app_element_msg_header_t *msg_hdr, const meshx_data_payload_t *payload)
 Send data event via MXSP.
meshx_err_t mxsp_send_gpio_rsp (uint8_t cmd, uint8_t logical_pin, uint8_t status, const uint8_t *response, uint8_t response_len)
 Send GPIO response via MXSP.
meshx_err_t mxsp_send_gpio_evt (uint8_t event_type, uint8_t logical_pin, uint8_t value)
 Send GPIO async event via MXSP.
void meshx_serial_parse_byte (uint8_t data)
 Parse incoming byte stream for MXSP frames.
void meshx_serial_set_hosted_mode (bool enabled)
 Set the hosted mode state.
bool meshx_serial_is_hosted_mode_enabled (void)
 Check if hosted mode is enabled.
static void mxsp_uart_rx_task (void *pvParameters)
 UART RX task to parse incoming MXSP frames.
static void meshx_gpio_hosted_event_handler (const meshx_gpio_hosted_event_t *event)
 GPIO hosted mode event callback.
meshx_err_t meshx_serial_init (void)
 Initialize serial communication.

Variables

struct { 
   uint8_t   state 
   mxsp_frame_t   rx_frame 
   uint8_t   rx_ptr 
   bool   hosted_mode_enabled 
mxsp_ctx

Detailed Description

MeshX Serial Protocol (MXSP) Implementation.

The MeshX Serial Protocol (MXSP) is a lightweight binary protocol used for communication between a MeshX Controller and a MeshX Engine over a serial interface.

Frame Format:

Byte Field
0 Start Of Frame (SOF)
1 Length
2 Type
3..N Payload
N+1 Checksum
N+2 End Of Frame (EOF)
Author
Pranjal Chanda
Date
2026-05-04

Macro Definition Documentation

◆ MESHX_UART_RX_TASK_PRIO

#define MESHX_UART_RX_TASK_PRIO   5

Priority level for the UART RX task.

◆ MESHX_UART_RX_TASK_STACK_SIZE

#define MESHX_UART_RX_TASK_STACK_SIZE   2048

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
STATE_SOF 
STATE_LEN 
STATE_TYPE 
STATE_PAYLOAD 
STATE_CHECKSUM 
STATE_EOF 
45 {
52};
@ STATE_CHECKSUM
Definition meshx_serial.c:50
@ STATE_PAYLOAD
Definition meshx_serial.c:49
@ STATE_LEN
Definition meshx_serial.c:47
@ STATE_TYPE
Definition meshx_serial.c:48
@ STATE_SOF
Definition meshx_serial.c:46
@ STATE_EOF
Definition meshx_serial.c:51

Function Documentation

◆ calculate_checksum()

uint8_t calculate_checksum ( uint8_t len,
uint8_t type,
const uint8_t * payload )
static

Calculate checksum for a given frame.

Parameters
lenLength of the payload.
typeType of the frame.
payloadPayload of the frame.
Returns
uint8_t Checksum of the frame.
64{
65 uint8_t checksum = len ^ type;
66 for (uint8_t i = 0; i < len; i++) {
67 checksum ^= payload[i];
68 }
69 return checksum;
70}

◆ meshx_get_sys_time_ms()

uint32_t meshx_get_sys_time_ms ( void )
static

Get system time in milliseconds.

Returns
uint32_t System time in milliseconds
93{
94 unsigned int millis = 0;
96 return (uint32_t)millis;
97}
meshx_err_t meshx_rtos_get_sys_time(unsigned int *millis)
Retrieves the current system time in milliseconds.

◆ meshx_gpio_hosted_event_handler()

void meshx_gpio_hosted_event_handler ( const meshx_gpio_hosted_event_t * event)
static

GPIO hosted mode event callback.

This callback is invoked by the GPIO subsystem when events need to be sent to the host MCU in hosted mode.

Parameters
eventGPIO hosted mode event
482{
483 if (!mxsp_ctx.hosted_mode_enabled) {
484 return;
485 }
486
487 if (event == NULL) {
488 MESHX_LOGE(MODULE_ID_COMMON, "NULL GPIO hosted event");
489 return;
490 }
491
492 /* Map GPIO event type to MXSP event type */
493 uint8_t mxsp_evt_type;
494 switch (event->event_type) {
495 case 0: /* Level change */
496 mxsp_evt_type = MXSP_GPIO_EVT_LEVEL_CHANGE;
497 break;
498 case 1: /* Interrupt */
499 mxsp_evt_type = MXSP_GPIO_EVT_INTERRUPT;
500 break;
501 default:
502 mxsp_evt_type = event->event_type;
503 break;
504 }
505
506 /* Send GPIO event via MXSP using the dedicated GPIO event message type */
507 mxsp_send_gpio_evt(mxsp_evt_type, event->logical_pin, event->value);
508
509 MESHX_LOGD(MODULE_ID_COMMON, "GPIO hosted event sent: type=%d, pin=%u, value=%u",
510 event->event_type, event->logical_pin, event->value);
511}
#define MESHX_LOGE(module_id, format,...)
Definition meshx_log.h:114
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:132
static struct @217313375257260000277136332076024345071273230342 mxsp_ctx
meshx_err_t mxsp_send_gpio_evt(uint8_t event_type, uint8_t logical_pin, uint8_t value)
Send GPIO async event via MXSP.
Definition meshx_serial.c:174
@ MXSP_GPIO_EVT_INTERRUPT
Definition meshx_serial.h:70
@ MXSP_GPIO_EVT_LEVEL_CHANGE
Definition meshx_serial.h:69
@ MODULE_ID_COMMON
Definition module_id.h:36
uint8_t logical_pin
Definition meshx_gpio_types.h:182
uint8_t event_type
Definition meshx_gpio_types.h:181
uint8_t value
Definition meshx_gpio_types.h:183

◆ meshx_platform_reset()

void meshx_platform_reset ( void )
extern

Resets the MeshX platform. This function performs a system reset, restarting the platform.

90{
91 esp_restart();
92}

◆ meshx_platform_serial_read()

int32_t meshx_platform_serial_read ( uint8_t * data,
uint16_t len )
extern

Reads data from the serial interface.

140{
141 return uart_read_bytes(CONFIG_MXSP_UART_PORT, data, len, 20 / portTICK_PERIOD_MS);
142}
#define CONFIG_MXSP_UART_PORT
Definition esp_platform.c:30

◆ meshx_platform_serial_write()

void meshx_platform_serial_write ( const uint8_t * data,
uint16_t len )
extern

Writes data to the serial interface.

132{
133 uart_write_bytes(CONFIG_MXSP_UART_PORT, (const char *)data, len);
134}

◆ meshx_serial_init()

meshx_err_t meshx_serial_init ( void )

Initialize serial communication.

Initialize the MeshX Serial Protocol handler.

Returns
MESHX_SUCCESS on success, error code otherwise
518{
519 memset(&mxsp_ctx, 0, sizeof(mxsp_ctx));
520 mxsp_ctx.state = STATE_SOF;
521 mxsp_ctx.hosted_mode_enabled = false;
522
523 meshx_task_t uart_rx_task;
524 uart_rx_task.task_cb = mxsp_uart_rx_task;
525 uart_rx_task.task_name = "mxsp_uart_rx";
527 uart_rx_task.priority = MESHX_UART_RX_TASK_PRIO;
528 uart_rx_task.arg = NULL;
529 /* Create UART RX Task */
530 meshx_task_create(&uart_rx_task);
531
532 /* Register GPIO hosted event callback */
534
535 return MESHX_SUCCESS;
536}
meshx_err_t meshx_gpio_register_hosted_event_cb(meshx_gpio_hosted_event_cb_t callback)
Register callback function for hosted mode events.
Definition meshx_gpio.c:802
**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
static void meshx_gpio_hosted_event_handler(const meshx_gpio_hosted_event_t *event)
GPIO hosted mode event callback.
Definition meshx_serial.c:481
#define MESHX_UART_RX_TASK_STACK_SIZE
Definition meshx_serial.c:30
static void mxsp_uart_rx_task(void *pvParameters)
UART RX task to parse incoming MXSP frames.
Definition meshx_serial.c:462
#define MESHX_UART_RX_TASK_PRIO
Priority level for the UART RX task.
Definition meshx_serial.c:35
struct meshx_task meshx_task_t
MeshX Task Structure.
meshx_err_t meshx_task_create(meshx_task_t *task_handle)
Create a MeshX Task.
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_serial_is_hosted_mode_enabled()

bool meshx_serial_is_hosted_mode_enabled ( void )

Check if hosted mode is enabled.

Returns
true if enabled, false otherwise
455{
456 return mxsp_ctx.hosted_mode_enabled;
457}

◆ meshx_serial_parse_byte()

void meshx_serial_parse_byte ( uint8_t data)

Parse incoming byte stream for MXSP frames.

Parameters
dataByte to parse
363{
364 switch (mxsp_ctx.state) {
365 case STATE_SOF:
366 if (data == MXSP_SOF) {
367 mxsp_ctx.state = STATE_LEN;
368 mxsp_ctx.rx_ptr = 0;
369 }
370 break;
371 case STATE_LEN:
372 mxsp_ctx.rx_frame.len = data;
373 mxsp_ctx.state = STATE_TYPE;
374 break;
375 case STATE_TYPE:
376 mxsp_ctx.rx_frame.type = data;
377 if (mxsp_ctx.rx_frame.len == 0) {
378 mxsp_ctx.state = STATE_CHECKSUM;
379 } else {
380 mxsp_ctx.state = STATE_PAYLOAD;
381 }
382 break;
383 case STATE_PAYLOAD:
384 mxsp_ctx.rx_frame.payload[mxsp_ctx.rx_ptr++] = data;
385 if (mxsp_ctx.rx_ptr >= mxsp_ctx.rx_frame.len) {
386 mxsp_ctx.state = STATE_CHECKSUM;
387 }
388 break;
389 case STATE_CHECKSUM:
390 mxsp_ctx.rx_frame.checksum = data;
391 mxsp_ctx.state = STATE_EOF;
392 break;
393 case STATE_EOF:
394 if (data == MXSP_EOF) {
395 if (calculate_checksum(mxsp_ctx.rx_frame.len, mxsp_ctx.rx_frame.type, mxsp_ctx.rx_frame.payload) == mxsp_ctx.rx_frame.checksum) {
396 if (mxsp_ctx.rx_frame.type == MXSP_MSG_TYPE_EL_CMD_SEND) {
398 if (mxsp_ctx.rx_frame.len >= sizeof(hdr)) {
399 memcpy(&hdr, mxsp_ctx.rx_frame.payload, sizeof(hdr));
401 mxsp_ctx.rx_frame.len - sizeof(hdr),
402 &mxsp_ctx.rx_frame.payload[sizeof(hdr)]);
403 }
404 } else if (mxsp_ctx.rx_frame.type == MXSP_MSG_TYPE_SYS_CMD_SEND) {
406 if (mxsp_ctx.rx_frame.len >= sizeof(hdr)) {
407 memcpy(&hdr, mxsp_ctx.rx_frame.payload, sizeof(hdr));
410 }
411 }
412 } else if (mxsp_ctx.rx_frame.type == MXSP_MSG_TYPE_HOSTED_MODE) {
413 if (mxsp_ctx.rx_frame.len == 1) {
414 bool enabled = (mxsp_ctx.rx_frame.payload[0] != 0);
415 mxsp_ctx.hosted_mode_enabled = enabled;
416
417 /* Notify GPIO subsystem about hosted mode change */
418 meshx_gpio_hosted_mode_t gpio_mode = enabled ?
421 if (err != MESHX_SUCCESS) {
422 MESHX_LOGE(MODULE_ID_COMMON, "Failed to set GPIO hosted mode: %d", err);
423 }
424
425 MESHX_LOGI(MODULE_ID_COMMON, "Hosted mode %s via API TLV command",
426 enabled ? "enabled" : "disabled");
427 }
428 } else if (mxsp_ctx.rx_frame.type == MXSP_MSG_TYPE_GPIO_CMD) {
429 /* Handle GPIO command from host */
430 mxsp_handle_gpio_cmd(mxsp_ctx.rx_frame.payload, mxsp_ctx.rx_frame.len);
431 } else if (mxsp_ctx.rx_frame.type == MXSP_MSG_TYPE_GPIO_EVT) {
432 /* Handle GPIO event from host (interrupt notification) */
433 mxsp_handle_gpio_evt_from_host(mxsp_ctx.rx_frame.payload, mxsp_ctx.rx_frame.len);
434 }
435 } else {
436 MESHX_LOGE(MODULE_ID_COMMON, "Invalid MXSP frame checksum");
437 }
438 }
439 mxsp_ctx.state = STATE_SOF;
440 break;
441 }
442}
meshx_err_t meshx_send_msg_to_element(uint16_t element_id, uint16_t element_type, uint16_t func_id, uint16_t msg_len, const void *msg)
Sends a message to the element.
Definition meshx_api.c:182
struct meshx_ctrl_msg_header meshx_ctrl_msg_header_t
Structure for the BLE Mesh application control message header.
struct meshx_app_element_msg_header meshx_app_element_msg_header_t
Structure for the BLE Mesh application element message header.
@ MESHX_CTRL_EVT_NODE_RESET
Definition meshx_common.h:32
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
meshx_err_t meshx_gpio_set_hosted_mode(meshx_gpio_hosted_mode_t mode)
Set hosted mode for GPIO subsystem.
Definition meshx_gpio.c:711
meshx_gpio_hosted_mode_t
Hosted Mode State.
Definition meshx_gpio_types.h:169
@ MESHX_GPIO_MODE_NON_HOSTED
Definition meshx_gpio_types.h:170
@ MESHX_GPIO_MODE_HOSTED
Definition meshx_gpio_types.h:171
#define MESHX_LOGI(module_id, format,...)
Definition meshx_log.h:126
static uint8_t calculate_checksum(uint8_t len, uint8_t type, const uint8_t *payload)
Calculate checksum for a given frame.
Definition meshx_serial.c:63
static void mxsp_handle_gpio_cmd(const uint8_t *payload, uint8_t len)
Handle GPIO command from host.
Definition meshx_serial.c:198
static void mxsp_handle_gpio_evt_from_host(const uint8_t *payload, uint8_t len)
Handle GPIO event from host (interrupt notification).
Definition meshx_serial.c:345
void meshx_platform_reset(void)
Resets the MeshX platform. This function performs a system reset, restarting the platform.
Definition esp_platform.c:89
@ MXSP_MSG_TYPE_GPIO_EVT
Definition meshx_serial.h:33
@ MXSP_MSG_TYPE_GPIO_CMD
Definition meshx_serial.h:31
@ MXSP_MSG_TYPE_SYS_CMD_SEND
Definition meshx_serial.h:28
@ MXSP_MSG_TYPE_EL_CMD_SEND
Definition meshx_serial.h:27
@ MXSP_MSG_TYPE_HOSTED_MODE
Definition meshx_serial.h:29
#define MXSP_SOF
Definition meshx_serial.h:19
#define MXSP_EOF
Definition meshx_serial.h:20
uint16_t element_id
Definition meshx_api.h:184
uint16_t func_id
Definition meshx_api.h:186
uint16_t element_type
Definition meshx_api.h:185
uint16_t evt_id
Definition meshx_api.h:198

◆ meshx_serial_set_hosted_mode()

void meshx_serial_set_hosted_mode ( bool enabled)

Set the hosted mode state.

Parameters
enabledTrue to enable, false to disable
445{
446 mxsp_ctx.hosted_mode_enabled = enabled;
447
448 /* Also update GPIO subsystem hosted mode */
449 meshx_gpio_hosted_mode_t gpio_mode = enabled ?
452}

◆ mxsp_handle_gpio_cmd()

void mxsp_handle_gpio_cmd ( const uint8_t * payload,
uint8_t len )
static

Handle GPIO command from host.

Parameters
payloadCommand payload
lenPayload length
199{
200 if (payload == NULL || len < sizeof(mxsp_gpio_cmd_payload_t)) {
201 MESHX_LOGE(MODULE_ID_COMMON, "Invalid GPIO command payload");
202 return;
203 }
204
205 const mxsp_gpio_cmd_payload_t *cmd = (const mxsp_gpio_cmd_payload_t *)payload;
207 uint8_t response[8] = {0};
208 uint8_t response_len = 0;
209
210 MESHX_LOGD(MODULE_ID_COMMON, "GPIO cmd received: cmd=%d, pin=%u", cmd->cmd, cmd->logical_pin);
211
212 switch (cmd->cmd) {
214 /* Set GPIO pin level */
215 if (cmd->payload_len >= 1) {
216 uint8_t level = cmd->payload[0];
217 err = meshx_gpio_set_level(cmd->logical_pin, level);
218 if (err == MESHX_SUCCESS) {
219 MESHX_LOGD(MODULE_ID_COMMON, "GPIO pin %u set to %u (hosted cmd)",
220 cmd->logical_pin, level);
221 }
222 } else {
223 err = MESHX_INVALID_ARG;
224 }
225 break;
226
228 /* Get GPIO pin level */
229 {
230 uint8_t level = 0;
231 err = meshx_gpio_get_level(cmd->logical_pin, &level);
232 if (err == MESHX_SUCCESS) {
233 response[0] = level;
234 response_len = 1;
235 }
236 }
237 break;
238
240 /* Toggle GPIO pin */
241 err = meshx_gpio_toggle(cmd->logical_pin);
242 if (err == MESHX_SUCCESS) {
243 /* Get the new level after toggle */
244 uint8_t level = 0;
245 meshx_gpio_get_level(cmd->logical_pin, &level);
246 response[0] = level;
247 response_len = 1;
248 }
249 break;
250
252 /* Set PWM duty cycle - using execute_function */
253 if (cmd->payload_len >= 1) {
254 uint32_t args[1] = { cmd->payload[0] };
257 args, 1);
258 } else {
259 err = MESHX_INVALID_ARG;
260 }
261 break;
262
264 /* Set PWM frequency - using execute_function */
265 if (cmd->payload_len >= 4) {
266 uint32_t frequency = (cmd->payload[0]) |
267 (cmd->payload[1] << 8) |
268 (cmd->payload[2] << 16) |
269 (cmd->payload[3] << 24);
270 uint32_t args[1] = { frequency };
273 args, 1);
274 } else {
275 err = MESHX_INVALID_ARG;
276 }
277 break;
278
280 /* Enable GPIO interrupt */
281 if (cmd->payload_len >= 1) {
282 bool enable = (cmd->payload[0] != 0);
283 err = meshx_gpio_intr_enable(cmd->logical_pin, enable);
284 } else {
285 err = MESHX_INVALID_ARG;
286 }
287 break;
288
290 /* Disable GPIO interrupt */
291 err = meshx_gpio_intr_enable(cmd->logical_pin, false);
292 break;
293
295 /* Get pin configuration */
296 {
298 err = meshx_gpio_get_pin_config(cmd->logical_pin, &config);
299 if (err == MESHX_SUCCESS) {
300 /* Serialize config into response */
301 response[0] = config.mode;
302 response[1] = config.pull;
303 response[2] = config.drive_strength;
304 response[3] = config.initial_level;
305 response[4] = config.signal_inversion ? 1 : 0;
306 response_len = 5;
307 }
308 }
309 break;
310
312 /* Get pin state */
313 {
316 if (err == MESHX_SUCCESS) {
317 /* Serialize state into response */
318 response[0] = state.current_level;
319 response[1] = state.interrupt_registered ? 1 : 0;
320 response[2] = state.intr_type;
321 response_len = 3;
322 }
323 }
324 break;
325
326 default:
327 MESHX_LOGW(MODULE_ID_COMMON, "Unknown GPIO command: %d", cmd->cmd);
328 err = MESHX_INVALID_ARG;
329 break;
330 }
331
332 /* Send response back to host */
333 mxsp_send_gpio_rsp(cmd->cmd, cmd->logical_pin, (uint8_t)err, response, response_len);
334}
@ MESHX_INVALID_ARG
Definition meshx_err.h:46
meshx_err_t meshx_gpio_set_level(uint8_t logical_pin, uint8_t level)
Set GPIO pin level.
Definition meshx_gpio.c:322
meshx_err_t meshx_gpio_execute_function(uint8_t logical_pin, meshx_io_function_t function, const uint32_t *args, uint8_t arg_count)
Execute IO function on GPIO pin.
Definition meshx_gpio.c:616
meshx_err_t meshx_gpio_get_level(uint8_t logical_pin, uint8_t *level)
Get GPIO pin level.
Definition meshx_gpio.c:392
meshx_err_t meshx_gpio_intr_enable(uint8_t logical_pin, bool enable)
Enable/disable GPIO interrupt.
Definition meshx_gpio.c:591
meshx_err_t meshx_gpio_get_pin_state(uint8_t logical_pin, meshx_gpio_pin_state_t *state)
Get pin runtime state.
Definition meshx_gpio.c:689
meshx_err_t meshx_gpio_get_pin_config(uint8_t logical_pin, meshx_gpio_pin_config_t *config)
Get pin configuration.
Definition meshx_gpio.c:663
meshx_err_t meshx_gpio_toggle(uint8_t logical_pin)
Toggle GPIO pin level.
Definition meshx_gpio.c:453
@ MESHX_IO_FUNCTION_SET_PWM_FREQUENCY
Definition meshx_gpio.h:68
@ MESHX_IO_FUNCTION_SET_PWM_DUTY
Definition meshx_gpio.h:67
#define MESHX_LOGW(module_id, format,...)
Definition meshx_log.h:120
uint8_t state
Definition meshx_serial.c:39
meshx_err_t mxsp_send_gpio_rsp(uint8_t cmd, uint8_t logical_pin, uint8_t status, const uint8_t *response, uint8_t response_len)
Send GPIO response via MXSP.
Definition meshx_serial.c:153
@ MXSP_GPIO_CMD_SET_PWM_FREQ
Definition meshx_serial.h:58
@ MXSP_GPIO_CMD_GET_CONFIG
Definition meshx_serial.h:61
@ MXSP_GPIO_CMD_GET_STATE
Definition meshx_serial.h:62
@ MXSP_GPIO_CMD_INTR_ENABLE
Definition meshx_serial.h:59
@ MXSP_GPIO_CMD_TOGGLE
Definition meshx_serial.h:56
@ MXSP_GPIO_CMD_SET_LEVEL
Definition meshx_serial.h:54
@ MXSP_GPIO_CMD_GET_LEVEL
Definition meshx_serial.h:55
@ MXSP_GPIO_CMD_INTR_DISABLE
Definition meshx_serial.h:60
@ MXSP_GPIO_CMD_SET_PWM_DUTY
Definition meshx_serial.h:57
GPIO Pin Configuration Structure.
Definition meshx_gpio_types.h:51
uint8_t mode
Definition meshx_gpio_types.h:54
uint8_t drive_strength
Definition meshx_gpio_types.h:56
uint8_t initial_level
Definition meshx_gpio_types.h:57
bool signal_inversion
Definition meshx_gpio_types.h:58
uint8_t pull
Definition meshx_gpio_types.h:55
GPIO Pin State Structure.
Definition meshx_gpio_types.h:91
GPIO Command Payload (Host -> Engine).
Definition meshx_serial.h:79
uint8_t cmd
Definition meshx_serial.h:80
uint8_t logical_pin
Definition meshx_serial.h:81
uint8_t payload[8]
Definition meshx_serial.h:84
uint8_t payload_len
Definition meshx_serial.h:83

◆ mxsp_handle_gpio_evt_from_host()

void mxsp_handle_gpio_evt_from_host ( const uint8_t * payload,
uint8_t len )
static

Handle GPIO event from host (interrupt notification).

In some configurations, the host MCU can send GPIO interrupt events to the engine when an interrupt occurs on the host side.

Parameters
payloadEvent payload
lenPayload length
346{
347 if (payload == NULL || len < sizeof(mxsp_gpio_evt_payload_t)) {
348 MESHX_LOGE(MODULE_ID_COMMON, "Invalid GPIO event payload from host");
349 return;
350 }
351
352 const mxsp_gpio_evt_payload_t *evt = (const mxsp_gpio_evt_payload_t *)payload;
353
354 /* Handle interrupt event from host */
357 } else {
358 MESHX_LOGW(MODULE_ID_COMMON, "Unexpected GPIO event from host: type=%d", evt->event_type);
359 }
360}
meshx_err_t meshx_gpio_process_hosted_interrupt(uint8_t logical_pin, uint8_t value)
Process GPIO interrupt event from host (hosted mode).
Definition meshx_gpio.c:856
GPIO Event Payload (Engine -> Host, async).
Definition meshx_serial.h:101
uint8_t event_type
Definition meshx_serial.h:102
uint8_t logical_pin
Definition meshx_serial.h:103
uint8_t value
Definition meshx_serial.h:104

◆ mxsp_send_ctrl_event()

meshx_err_t mxsp_send_ctrl_event ( const meshx_ctrl_msg_header_t * evt_header,
const meshx_ctrl_payload_t * payload )

Send a control event via MXSP.

Parameters
evt_headerControl message header
payloadControl message payload
Returns
meshx_err_t MESHX_SUCCESS on success
119{
120 if (!mxsp_ctx.hosted_mode_enabled) {
121 return MESHX_SUCCESS;
122 }
123 uint8_t buff[sizeof(meshx_ctrl_msg_header_t) + sizeof(meshx_ctrl_payload_t)];
124 uint8_t len = 0;
125
126 memcpy(&buff[len], evt_header, sizeof(meshx_ctrl_msg_header_t));
127 len += sizeof(meshx_ctrl_msg_header_t);
128
129 // For simplicity, we send the whole payload union. In optimization, we could send only active part.
130 memcpy(&buff[len], payload, sizeof(meshx_ctrl_payload_t));
131 len += sizeof(meshx_ctrl_payload_t);
132
134}
union meshx_ctrl_payload meshx_ctrl_payload_t
Structure defines the payload for meshx_ctrl_payload.
meshx_err_t mxsp_send_frame(mxsp_msg_type_t type, const uint8_t *payload, uint8_t len)
Definition meshx_serial.c:99
@ MXSP_MSG_TYPE_SYS_EVT_NOTIFY
Definition meshx_serial.h:25

◆ mxsp_send_data_event()

meshx_err_t mxsp_send_data_event ( const meshx_app_element_msg_header_t * msg_hdr,
const meshx_data_payload_t * payload )

Send data event via MXSP.

Parameters
msg_hdrData message header
payloadData message payload
Returns
meshx_err_t MESHX_SUCCESS on success
137{
138 if (!mxsp_ctx.hosted_mode_enabled) {
139 return MESHX_SUCCESS;
140 }
141 uint8_t buff[sizeof(meshx_app_element_msg_header_t) + sizeof(meshx_data_payload_t)];
142 uint8_t len = 0;
143
144 memcpy(&buff[len], msg_hdr, sizeof(meshx_app_element_msg_header_t));
145 len += sizeof(meshx_app_element_msg_header_t);
146
147 memcpy(&buff[len], payload, sizeof(meshx_data_payload_t));
148 len += sizeof(meshx_data_payload_t);
149
151}
union meshx_data_payload meshx_data_payload_t
Structure for the BLE Mesh application control message.
@ MXSP_MSG_TYPE_DATA_EVT_NOTIFY
Definition meshx_serial.h:26

◆ mxsp_send_frame()

meshx_err_t mxsp_send_frame ( mxsp_msg_type_t type,
const uint8_t * payload,
uint8_t len )
100{
101 uint8_t frame_buff[260];
102 uint8_t ptr = 0;
103
104 frame_buff[ptr++] = MXSP_SOF;
105 frame_buff[ptr++] = len;
106 frame_buff[ptr++] = (uint8_t)type;
107 if (len > 0 && payload != NULL) {
108 memcpy(&frame_buff[ptr], payload, len);
109 ptr += len;
110 }
111 frame_buff[ptr++] = calculate_checksum(len, (uint8_t)type, payload);
112 frame_buff[ptr++] = MXSP_EOF;
113
114 meshx_platform_serial_write(frame_buff, ptr);
115 return MESHX_SUCCESS;
116}
void meshx_platform_serial_write(const uint8_t *data, uint16_t len)
Writes data to the serial interface.
Definition esp_platform.c:131

◆ mxsp_send_gpio_evt()

meshx_err_t mxsp_send_gpio_evt ( uint8_t event_type,
uint8_t logical_pin,
uint8_t value )

Send GPIO async event via MXSP.

Parameters
event_typeEvent type (mxsp_gpio_evt_t)
logical_pinLogical pin number
valueEvent value
Returns
meshx_err_t MESHX_SUCCESS on success
175{
176 if (!mxsp_ctx.hosted_mode_enabled) {
177 return MESHX_SUCCESS;
178 }
179
181 memset(&evt, 0, sizeof(evt));
182
183 evt.event_type = event_type;
184 evt.logical_pin = logical_pin;
185 evt.value = value;
186 evt.reserved = 0;
188
189 return mxsp_send_frame(MXSP_MSG_TYPE_GPIO_EVT, (const uint8_t*)&evt, sizeof(evt));
190}
static uint32_t meshx_get_sys_time_ms(void)
Get system time in milliseconds.
Definition meshx_serial.c:92
uint32_t timestamp
Definition meshx_serial.h:106
uint8_t reserved
Definition meshx_serial.h:105

◆ mxsp_send_gpio_rsp()

meshx_err_t mxsp_send_gpio_rsp ( uint8_t cmd,
uint8_t logical_pin,
uint8_t status,
const uint8_t * response,
uint8_t response_len )

Send GPIO response via MXSP.

Parameters
cmdOriginal command type
logical_pinLogical pin number
statusStatus code (0 = success)
responseResponse data
response_lenResponse data length
Returns
meshx_err_t MESHX_SUCCESS on success
155{
156 if (!mxsp_ctx.hosted_mode_enabled) {
157 return MESHX_SUCCESS;
158 }
159
161 memset(&rsp, 0, sizeof(rsp));
162
163 rsp.cmd = cmd;
164 rsp.logical_pin = logical_pin;
165 rsp.status = status;
166 rsp.response_len = (response_len > 8) ? 8 : response_len;
167 if (response != NULL && rsp.response_len > 0) {
168 memcpy(rsp.response, response, rsp.response_len);
169 }
170
171 return mxsp_send_frame(MXSP_MSG_TYPE_GPIO_RSP, (const uint8_t*)&rsp, sizeof(rsp));
172}
@ MXSP_MSG_TYPE_GPIO_RSP
Definition meshx_serial.h:32
GPIO Response Payload (Engine -> Host).
Definition meshx_serial.h:90
uint8_t response[8]
Definition meshx_serial.h:95
uint8_t status
Definition meshx_serial.h:93
uint8_t logical_pin
Definition meshx_serial.h:92
uint8_t cmd
Definition meshx_serial.h:91
uint8_t response_len
Definition meshx_serial.h:94

◆ mxsp_uart_rx_task()

void mxsp_uart_rx_task ( void * pvParameters)
static

UART RX task to parse incoming MXSP frames.

463{
464 uint8_t data;
465 while (1) {
466 int32_t len = meshx_platform_serial_read(&data, 1);
467 if (len > 0) {
469 }
470 }
471}
int32_t meshx_platform_serial_read(uint8_t *data, uint16_t len)
Reads data from the serial interface.
Definition esp_platform.c:139
void meshx_serial_parse_byte(uint8_t data)
Parse incoming byte stream for MXSP frames.
Definition meshx_serial.c:362

Variable Documentation

◆ hosted_mode_enabled

bool hosted_mode_enabled

◆ [struct]

struct { ... } mxsp_ctx

◆ rx_frame

mxsp_frame_t rx_frame

◆ rx_ptr

uint8_t rx_ptr

◆ state

uint8_t state