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_platform.h File Reference

Platform abstraction layer for MeshX. More...

#include "meshx_err.h"
#include "meshx_platform_ble_mesh.h"
#include "interface/ble_mesh/meshx_ble_mesh_cmn_def.h"

Go to the source code of this file.

Functions

meshx_err_t meshx_platform_init (void)
 Initializes the MeshX platform.
void meshx_platform_reset (void)
 Resets the MeshX platform. This function performs a system reset, restarting the platform.
void meshx_platform_serial_write (const uint8_t *data, uint16_t len)
 Writes data to the serial interface.
meshx_err_t meshx_platform_serial_init (void)
 Initializes the serial interface for communication.
int32_t meshx_platform_serial_read (uint8_t *data, uint16_t len)
 Reads data from the serial interface.
void meshx_platform_console_write (const char *data, uint16_t len)
 Writes data to the console interface.
int32_t meshx_platform_console_read (uint8_t *data, uint16_t len)
 Reads data from the console interface.
meshx_err_t meshx_platform_console_init (void)
 Initializes the console interface.

Detailed Description

Platform abstraction layer for MeshX.

This header provides initialization functions for the MeshX platform and its Bluetooth subsystem.

Function Documentation

◆ meshx_platform_console_init()

meshx_err_t meshx_platform_console_init ( void )

Initializes the console interface.

Returns
meshx_err_t Returns MESHX_OK on success, or an appropriate error code.
145{
146#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
147 /* Disable buffering on stdin and stdout */
148 setvbuf(stdin, NULL, _IONBF, 0);
149 setvbuf(stdout, NULL, _IONBF, 0);
150
151 /* Install USB-SERIAL-JTAG driver for interrupt-driven reads */
152 usb_serial_jtag_driver_config_t usb_serial_jtag_config = USB_SERIAL_JTAG_DRIVER_CONFIG_DEFAULT();
153 esp_err_t ret = usb_serial_jtag_driver_install(&usb_serial_jtag_config);
154 if (ret != ESP_OK && ret != ESP_ERR_INVALID_STATE) return MESHX_ERR_PLAT;
155
156 /* Tell VFS to use USB-SERIAL-JTAG driver */
157 usb_serial_jtag_vfs_use_driver();
158 ESP_LOGD("MESHX_PLAT", "Console initialized via USB Serial JTAG");
159
160#elif CONFIG_ESP_CONSOLE_UART
161 if (!uart_is_driver_installed(CONFIG_MESHX_CONSOLE_UART_PORT)) {
162 uart_config_t uart_config = {
163 .baud_rate = 115200,
164 .data_bits = UART_DATA_8_BITS,
165 .parity = UART_PARITY_DISABLE,
166 .stop_bits = UART_STOP_BITS_1,
167 .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
168 .source_clk = UART_SCLK_DEFAULT,
169 };
170 ESP_ERROR_CHECK(uart_driver_install(CONFIG_MESHX_CONSOLE_UART_PORT, 1024, 0, 0, NULL, 0));
171 ESP_ERROR_CHECK(uart_param_config(CONFIG_MESHX_CONSOLE_UART_PORT, &uart_config));
172 ESP_LOGD("MESHX_PLAT", "Console initialized via UART%d", CONFIG_MESHX_CONSOLE_UART_PORT);
173 }
174#endif
175 return MESHX_SUCCESS;
176}
#define CONFIG_MESHX_CONSOLE_UART_PORT
Definition esp_platform.c:25
@ MESHX_ERR_PLAT
Definition meshx_err.h:47
**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_platform_console_read()

int32_t meshx_platform_console_read ( uint8_t * data,
uint16_t len )

Reads data from the console interface.

Parameters
[out]dataPointer to the data buffer.
[in]lenLength of the data to read.
Returns
int32_t Number of bytes read, or negative error code.
191{
192#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
193 return usb_serial_jtag_read_bytes(data, len, 10 / portTICK_PERIOD_MS);
194#elif CONFIG_ESP_CONSOLE_UART
195 return uart_read_bytes(CONFIG_MESHX_CONSOLE_UART_PORT, data, len, 10 / portTICK_PERIOD_MS);
196#else
197 return -1;
198#endif
199}

◆ meshx_platform_console_write()

void meshx_platform_console_write ( const char * data,
uint16_t len )

Writes data to the console interface.

Parameters
[in]dataPointer to the data buffer.
[in]lenLength of the data to write.
179{
180#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
181 usb_serial_jtag_write_bytes(data, len, portMAX_DELAY);
182#elif CONFIG_ESP_CONSOLE_UART
183 uart_write_bytes(CONFIG_MESHX_CONSOLE_UART_PORT, data, len);
184#else
185 // Fallback to standard printf/fwrite if needed
186 fwrite(data, 1, len, stdout);
187#endif
188}

◆ meshx_platform_init()

meshx_err_t meshx_platform_init ( void )

Initializes the MeshX platform.

This function sets up the necessary hardware and software components required for the MeshX platform to function correctly.

Returns
meshx_err_t Returns MESHX_OK on success, or an appropriate error code.

Initializes the MeshX platform.

This function sets up the necessary components and configurations required for the MeshX platform to operate on the ESP32. It ensures that the platform is ready for use by other components of the MeshX system.

Returns
  • MESHX_OK on successful initialization.
  • Appropriate error code of type meshx_err_t on failure.
61{
62 esp_err_t err = ESP_OK;
63 /* Initialize NVS flash */
64 err = nvs_flash_init();
65 if (err == ESP_ERR_NVS_NO_FREE_PAGES)
66 {
67 ESP_ERROR_CHECK(nvs_flash_erase());
68 err = nvs_flash_init();
69 }
70 if(err)
71 return MESHX_ERR_PLAT;
72
73 /* Initialize Serial interface by default */
75
76 /* Initialize Console interface by default for early logging */
78
79 /* Set log level for BLE Mesh */
80 esp_log_level_set("BLE_MESH", ESP_LOG_ERROR);
81
82 return MESHX_SUCCESS;
83}
meshx_err_t meshx_platform_serial_init(void)
Initializes the serial interface.
Definition esp_platform.c:104
meshx_err_t meshx_platform_console_init(void)
Initializes the console interface.
Definition esp_platform.c:144

◆ meshx_platform_reset()

void meshx_platform_reset ( void )

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

90{
91 esp_restart();
92}

◆ meshx_platform_serial_init()

meshx_err_t meshx_platform_serial_init ( void )

Initializes the serial interface for communication.

Returns
meshx_err_t Returns MESHX_OK on success, or an appropriate error code.

Initializes the serial interface for communication.

This function initializes the serial interface for the MeshX platform. It configures the UART port, sets the baud rate, and enables the UART.

Returns
  • MESHX_OK on successful initialization.
  • Appropriate error code of type meshx_err_t on failure.
105{
106 uart_config_t uart_config = {
107 .baud_rate = CONFIG_MXSP_UART_BAUD,
108 .data_bits = UART_DATA_8_BITS,
109 .parity = UART_PARITY_DISABLE,
110 .stop_bits = UART_STOP_BITS_1,
111 .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
112 .source_clk = UART_SCLK_DEFAULT,
113 };
114 int intr_alloc_flags = 0;
115
116 esp_err_t ret = uart_driver_install(CONFIG_MXSP_UART_PORT, 1024 * 2, 0, 0, NULL, intr_alloc_flags);
117 if (ret != ESP_OK) return MESHX_ERR_PLAT;
118
119 ret = uart_param_config(CONFIG_MXSP_UART_PORT, &uart_config);
120 if (ret != ESP_OK) return MESHX_ERR_PLAT;
121
122 ret = uart_set_pin(CONFIG_MXSP_UART_PORT, CONFIG_MXSP_UART_TX_PIN, CONFIG_MXSP_UART_RX_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
123 if (ret != ESP_OK) return MESHX_ERR_PLAT;
124
125 return MESHX_SUCCESS;
126}
#define CONFIG_MXSP_UART_PORT
Definition esp_platform.c:30
#define CONFIG_MXSP_UART_BAUD
Definition esp_platform.c:34
#define CONFIG_MXSP_UART_TX_PIN
Definition esp_platform.c:38
#define CONFIG_MXSP_UART_RX_PIN
Definition esp_platform.c:42

◆ meshx_platform_serial_read()

int32_t meshx_platform_serial_read ( uint8_t * data,
uint16_t len )

Reads data from the serial interface.

Parameters
[out]dataPointer to the data buffer.
[in]lenLength of the data to read.
Returns
int32_t Number of bytes read, or negative error code.
140{
141 return uart_read_bytes(CONFIG_MXSP_UART_PORT, data, len, 20 / portTICK_PERIOD_MS);
142}

◆ meshx_platform_serial_write()

void meshx_platform_serial_write ( const uint8_t * data,
uint16_t len )

Writes data to the serial interface.

Parameters
[in]dataPointer to the data buffer.
[in]lenLength of the data to write.
132{
133 uart_write_bytes(CONFIG_MXSP_UART_PORT, (const char *)data, len);
134}