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.h
Go to the documentation of this file.
1/**
2 * @file meshx_serial.h
3 * @brief MeshX Serial Protocol (MXSP) Definitions
4 *
5 * This file defines the frame structure and APIs for the MeshX Serial Protocol,
6 * used for communication between the Mesh engine and an external Host MCU.
7 */
8
9#ifndef __MESHX_SERIAL_H__
10#define __MESHX_SERIAL_H__
11
12#include "meshx_common.h"
13#include "meshx_api.h"
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
19#define MXSP_SOF 0xFE
20#define MXSP_EOF 0xEF
21
22#define MXSP_PAYLOAD_MAX_SIZE 255
23
24typedef enum {
25 MXSP_MSG_TYPE_SYS_EVT_NOTIFY = 0xB1, // Engine -> Host (Control Path)
26 MXSP_MSG_TYPE_DATA_EVT_NOTIFY = 0xB2, // Engine -> Host (Data Path)
27 MXSP_MSG_TYPE_EL_CMD_SEND = 0xC1, // Host -> Engine (Command Path)
28 MXSP_MSG_TYPE_SYS_CMD_SEND = 0xC2, // Host -> Engine (System Command)
29 MXSP_MSG_TYPE_HOSTED_MODE = 0x03, // Internal/Setup
30 /* GPIO Hosted Mode Message Types */
31 MXSP_MSG_TYPE_GPIO_CMD = 0xD1, // Host -> Engine (GPIO Command)
32 MXSP_MSG_TYPE_GPIO_RSP = 0xD2, // Engine -> Host (GPIO Response)
33 MXSP_MSG_TYPE_GPIO_EVT = 0xD3, // Engine -> Host (GPIO Event - async)
35
36/**
37 * @brief Set the hosted mode state
38 *
39 * @param enabled True to enable, false to disable
40 */
41void meshx_serial_set_hosted_mode(bool enabled);
42
43/**
44 * @brief Check if hosted mode is enabled
45 *
46 * @return true if enabled, false otherwise
47 */
49
50/**
51 * @brief GPIO Command Types (Host -> Engine)
52 */
53typedef enum {
54 MXSP_GPIO_CMD_SET_LEVEL = 0x01, /**< Set GPIO pin level */
55 MXSP_GPIO_CMD_GET_LEVEL = 0x02, /**< Get GPIO pin level */
56 MXSP_GPIO_CMD_TOGGLE = 0x03, /**< Toggle GPIO pin */
57 MXSP_GPIO_CMD_SET_PWM_DUTY = 0x04, /**< Set PWM duty cycle */
58 MXSP_GPIO_CMD_SET_PWM_FREQ = 0x05, /**< Set PWM frequency */
59 MXSP_GPIO_CMD_INTR_ENABLE = 0x06, /**< Enable/disable interrupt */
60 MXSP_GPIO_CMD_INTR_DISABLE = 0x07, /**< Disable interrupt */
61 MXSP_GPIO_CMD_GET_CONFIG = 0x08, /**< Get pin configuration */
62 MXSP_GPIO_CMD_GET_STATE = 0x09, /**< Get pin state */
64
65/**
66 * @brief GPIO Event Types (Engine -> Host, async)
67 */
68typedef enum {
69 MXSP_GPIO_EVT_LEVEL_CHANGE = 0x01, /**< Pin level changed */
70 MXSP_GPIO_EVT_INTERRUPT = 0x02, /**< Interrupt triggered */
71 MXSP_GPIO_EVT_MODE_CHANGE = 0x03, /**< Mode changed */
72 MXSP_GPIO_EVT_ERROR = 0xFF, /**< Error event */
74
75/**
76 * @brief GPIO Command Payload (Host -> Engine)
77 */
78#pragma pack(push, 1)
79typedef struct {
80 uint8_t cmd; /**< Command type (mxsp_gpio_cmd_t) */
81 uint8_t logical_pin; /**< Logical pin number (0-255) */
82 uint8_t reserved; /**< Reserved for alignment */
83 uint8_t payload_len; /**< Payload length (0-8 bytes) */
84 uint8_t payload[8]; /**< Command-specific payload */
86
87/**
88 * @brief GPIO Response Payload (Engine -> Host)
89 */
90typedef struct {
91 uint8_t cmd; /**< Original command type */
92 uint8_t logical_pin; /**< Logical pin number */
93 uint8_t status; /**< Status (0 = success, error code otherwise) */
94 uint8_t response_len; /**< Response data length */
95 uint8_t response[8]; /**< Response data */
97
98/**
99 * @brief GPIO Event Payload (Engine -> Host, async)
100 */
101typedef struct {
102 uint8_t event_type; /**< Event type (mxsp_gpio_evt_t) */
103 uint8_t logical_pin; /**< Logical pin number */
104 uint8_t value; /**< Event value */
105 uint8_t reserved; /**< Reserved for alignment */
106 uint32_t timestamp; /**< Event timestamp (ms since boot) */
108#pragma pack(pop)
109
110/**
111 * @brief MXSP Frame structure
112 */
113typedef struct {
114 uint8_t sof;
115 uint8_t len;
116 uint8_t type;
118 uint8_t checksum;
119 uint8_t eof;
121
122/**
123 * @brief Initialize the MeshX Serial Protocol handler
124 *
125 * @return meshx_err_t MESHX_SUCCESS on success
126 */
128
129/**
130 * @brief Parse incoming byte stream for MXSP frames
131 *
132 * @param data Byte to parse
133 */
134void meshx_serial_parse_byte(uint8_t data);
135
136/**
137 * @brief Send a control event via MXSP
138 *
139 * @param evt_header Control message header
140 * @param payload Control message payload
141 * @return meshx_err_t MESHX_SUCCESS on success
142 */
144
145/**
146 * @brief Send data event via MXSP
147 *
148 * @param msg_hdr Data message header
149 * @param payload Data message payload
150 * @return meshx_err_t MESHX_SUCCESS on success
151 */
153
154/**
155 * @brief Send GPIO response via MXSP
156 *
157 * @param cmd Original command type
158 * @param logical_pin Logical pin number
159 * @param status Status code (0 = success)
160 * @param response Response data
161 * @param response_len Response data length
162 * @return meshx_err_t MESHX_SUCCESS on success
163 */
164meshx_err_t mxsp_send_gpio_rsp(uint8_t cmd, uint8_t logical_pin, uint8_t status,
165 const uint8_t *response, uint8_t response_len);
166
167/**
168 * @brief Send GPIO async event via MXSP
169 *
170 * @param event_type Event type (mxsp_gpio_evt_t)
171 * @param logical_pin Logical pin number
172 * @param value Event value
173 * @return meshx_err_t MESHX_SUCCESS on success
174 */
175meshx_err_t mxsp_send_gpio_evt(uint8_t event_type, uint8_t logical_pin, uint8_t value);
176
177#ifdef __cplusplus
178}
179#endif
180
181#endif /* __MESHX_SERIAL_H__ */
This file contains the API definitions for the MeshX application.
union meshx_data_payload meshx_data_payload_t
Structure for the BLE Mesh application control message.
union meshx_ctrl_payload meshx_ctrl_payload_t
Structure defines the payload for meshx_ctrl_payload.
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.
Common application definitions and includes for BLE Mesh Node.
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
meshx_err_t meshx_serial_init(void)
Initialize the MeshX Serial Protocol handler.
Definition meshx_serial.c:517
mxsp_gpio_cmd_t
GPIO Command Types (Host -> Engine).
Definition meshx_serial.h:53
@ 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
void meshx_serial_set_hosted_mode(bool enabled)
Set the hosted mode state.
Definition meshx_serial.c:444
mxsp_msg_type_t
Definition meshx_serial.h:24
@ MXSP_MSG_TYPE_GPIO_EVT
Definition meshx_serial.h:33
@ MXSP_MSG_TYPE_SYS_EVT_NOTIFY
Definition meshx_serial.h:25
@ MXSP_MSG_TYPE_GPIO_CMD
Definition meshx_serial.h:31
@ MXSP_MSG_TYPE_DATA_EVT_NOTIFY
Definition meshx_serial.h:26
@ 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_GPIO_RSP
Definition meshx_serial.h:32
@ MXSP_MSG_TYPE_HOSTED_MODE
Definition meshx_serial.h:29
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_evt_t
GPIO Event Types (Engine -> Host, async).
Definition meshx_serial.h:68
@ MXSP_GPIO_EVT_MODE_CHANGE
Definition meshx_serial.h:71
@ MXSP_GPIO_EVT_INTERRUPT
Definition meshx_serial.h:70
@ MXSP_GPIO_EVT_ERROR
Definition meshx_serial.h:72
@ MXSP_GPIO_EVT_LEVEL_CHANGE
Definition meshx_serial.h:69
void meshx_serial_parse_byte(uint8_t data)
Parse incoming byte stream for MXSP frames.
Definition meshx_serial.c:362
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.
Definition meshx_serial.c:118
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
#define MXSP_PAYLOAD_MAX_SIZE
Definition meshx_serial.h:22
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.
Definition meshx_serial.c:136
bool meshx_serial_is_hosted_mode_enabled(void)
Check if hosted mode is enabled.
Definition meshx_serial.c:454
MXSP Frame structure.
Definition meshx_serial.h:113
uint8_t eof
Definition meshx_serial.h:119
uint8_t type
Definition meshx_serial.h:116
uint8_t checksum
Definition meshx_serial.h:118
uint8_t sof
Definition meshx_serial.h:114
uint8_t len
Definition meshx_serial.h:115
uint8_t payload[255]
Definition meshx_serial.h:117
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
uint8_t reserved
Definition meshx_serial.h:82
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
uint32_t timestamp
Definition meshx_serial.h:106
uint8_t value
Definition meshx_serial.h:104
uint8_t reserved
Definition meshx_serial.h:105
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