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

meshX application file for ESP BLE Mesh node. More...

#include "interface/meshx_platform.h"
#include <meshx.h>
#include <meshx_serial.h>
#include <meshx_ro_cfg.h>
#include <stdio.h>
#include <string.h>
#include "interface/utils/meshx_tiny_printf.h"
#include <meshx_builder_api.h>

Macros

#define ROOT_ELEMENT_IDX   0
 Defines the index for the root element.
#define FRESHBOOT_TIMEOUT_MS   1500
 Defines the timeout duration in milliseconds for a fresh boot.

Functions

size_t get_root_sig_models_count (void)
size_t get_root_ven_models_count (void)
meshx_ptr_t get_root_sig_models (void)
meshx_ptr_t get_root_ven_models (void)
meshx_err_t meshx_create_element_composition (dev_struct_t *p_dev, meshx_config_t const *config)
static meshx_err_t meshx_element_init (dev_struct_t *p_dev, meshx_config_t const *config)
 Initializes BLE Mesh elements.
static meshx_err_t meshx_tasks_init (dev_struct_t *pdev)
 Initializes application tasks.
static meshx_err_t meshx_dev_restore (dev_struct_t *pdev, meshx_config_t const *config)
 Restore the device state from the NVS.
static meshx_err_t meshx_ble_mesh_init (meshx_config_t *config)
 Initializes the BLE Mesh subsystem.
static meshx_err_t meshx_load_persistent_config (meshx_config_t const *config)
 Load persistent read-only configuration from flash and apply it.
meshx_err_t meshx_init (meshx_config_t const *config)
 MeshX initialisation function.
uint16_t meshx_get_net_key_id (void)
 Get the network key identifier.

Variables

static dev_struct_t g_dev
static meshx_config_t g_config
static char g_product_name [32]
meshx_prov_params_t g_prov_cfg

Detailed Description

meshX application file for ESP BLE Mesh node.

This file contains initialization routines for BLE Mesh provisioning, configuration, and light control servers, as well as the main application entry point.

Author
Pranjal Chanda

Macro Definition Documentation

◆ FRESHBOOT_TIMEOUT_MS

#define FRESHBOOT_TIMEOUT_MS   1500

Defines the timeout duration in milliseconds for a fresh boot.

◆ ROOT_ELEMENT_IDX

#define ROOT_ELEMENT_IDX   0

Defines the index for the root element.

Function Documentation

◆ get_root_sig_models()

meshx_ptr_t get_root_sig_models ( void )
extern

◆ get_root_sig_models_count()

size_t get_root_sig_models_count ( void )
extern

◆ get_root_ven_models()

meshx_ptr_t get_root_ven_models ( void )
extern

◆ get_root_ven_models_count()

size_t get_root_ven_models_count ( void )
extern

◆ meshx_ble_mesh_init()

meshx_err_t meshx_ble_mesh_init ( meshx_config_t * config)
static

Initializes the BLE Mesh subsystem.

This function sets up provisioning, configuration servers, and BLE Mesh stack initialization.

Returns
MESHX_SUCCESS on success, error code otherwise.
140{
141 if(config == NULL || config->product_name == NULL)
142 return MESHX_INVALID_ARG;
143
144 meshx_err_t err;
145 g_prov_cfg.node_name = (uint8_t *)config->product_name;
146
147 /* Copy the UUID to the global device structure for visualization and management */
148 memcpy(g_dev.uuid, config->meshx_uuid_addr, sizeof(g_dev.uuid));
149
150 err = meshx_platform_bt_init(g_dev.uuid);
151 MESHX_ERR_PRINT_RET("Platform BT init failed", err);
152
153 g_prov_cfg.uuid = g_dev.uuid;
154 g_prov_cfg.freshboot_timeout_ms = FRESHBOOT_TIMEOUT_MS;
155
156 err = meshx_element_init(&g_dev, config);
157 MESHX_ERR_PRINT_RET("Failed to initialize BLE Elements", err);
158
160 MESHX_ERR_PRINT_RET("Failed to initialize provisioning", err);
161
162 err = meshx_plat_ble_mesh_init(&g_prov_cfg, g_dev.composition);
163 MESHX_ERR_PRINT_RET("Failed to initialize BLE Mesh stack", err);
164
165 /* Publish system ready event to notify lower layers */
167
168 return MESHX_SUCCESS;
169}
static meshx_err_t meshx_element_init(dev_struct_t *p_dev, meshx_config_t const *config)
Initializes BLE Mesh elements.
Definition meshx.c:63
meshx_prov_params_t g_prov_cfg
Definition meshx.c:38
#define FRESHBOOT_TIMEOUT_MS
Defines the timeout duration in milliseconds for a fresh boot.
Definition meshx.c:31
static dev_struct_t g_dev
Definition meshx.c:33
#define MESHX_ERR_PRINT_RET(_e_str, _err)
Print and return error message if an error occurs.
Definition meshx.h:41
meshx_err_t meshx_platform_bt_init(meshx_uuid_addr_t uuid)
Initializes the Bluetooth subsystem of the MeshX platform.
meshx_err_t meshx_plat_ble_mesh_init(const meshx_prov_params_t *prov_cfg, meshx_ptr_t comp)
Initializes the BLE Mesh stack with the given provisioning parameters.
@ CONTROL_TASK_MSG_CODE_PROVISION
Definition meshx_control_task.h:70
CONTROL_TASK_MSG_EVT_SYSTEM_STACK_READY
Definition meshx_control_task.h:168
meshx_err_t control_task_msg_publish(control_task_msg_code_t msg_code, control_task_msg_evt_t msg_evt, const void *msg_evt_params, size_t sizeof_msg_evt_params)
Publish a control task message.
Definition meshx_control_task.c:84
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
@ MESHX_INVALID_ARG
Definition meshx_err.h:46
**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_init_prov(dev_struct_t *p_dev, const meshx_prov_params_t *prov_cfg)
Initialize provisioning parameters.
Definition meshx_prov_srv.cpp:251
meshx_uuid_addr_t meshx_uuid_addr
Definition meshx.h:76
char * product_name
Definition meshx.h:69

◆ meshx_create_element_composition()

meshx_err_t meshx_create_element_composition ( dev_struct_t * p_dev,
meshx_config_t const * config )
extern

◆ meshx_dev_restore()

meshx_err_t meshx_dev_restore ( dev_struct_t * pdev,
meshx_config_t const * config )
static

Restore the device state from the NVS.

Parameters
[in]pdevPointer to the device structure.
[in]configPointer to the meshX configuration.
Returns
MESHX_SUCCESS on success, error code otherwise.
116{
118
119 err = meshx_nvs_open(config->cid, config->pid, config->meshx_nvs_save_period);
120 MESHX_ERR_PRINT_RET("MeshX NVS Open failed", err);
121
123 if (err != MESHX_SUCCESS) {
124 MESHX_LOGW(MODULE_ID_COMMON, "No meshx device state found in NVS, starting fresh (err 0x%x)", err);
125 memset(&pdev->meshx_store, 0, sizeof(meshx_app_store_t));
126 return MESHX_SUCCESS; // Non-fatal
127 }
128
129 return MESHX_SUCCESS;
130}
#define MESHX_NVS_STORE
Definition meshx_common.h:54
struct meshx_app_store meshx_app_store_t
Structure to store mesh application data.
#define MESHX_LOGW(module_id, format,...)
Definition meshx_log.h:120
meshx_err_t meshx_nvs_open(uint16_t cid, uint16_t pid, uint32_t commit_timeout_ms)
Open the NVS with a timeout.
Definition meshx_nvs.c:180
meshx_err_t meshx_nvs_get(char const *key, void *blob, uint16_t blob_size)
Get a value from the NVS.
Definition meshx_nvs.c:368
@ MODULE_ID_COMMON
Definition module_id.h:36
meshx_app_store_t meshx_store
Definition meshx_common.h:75

◆ meshx_element_init()

meshx_err_t meshx_element_init ( dev_struct_t * p_dev,
meshx_config_t const * config )
static

Initializes BLE Mesh elements.

Parameters
[in]p_devPointer to the device structure.
[in]configPointer to the meshX configuration.
Returns
MESHX_SUCCESS on success, error code otherwise.

Detect if Dynamic Composition Builder was used

Note
If the builder is active, we bake the dynamic composition and skip legacy init
64{
65 if (!p_dev)
67
69
70 /**
71 * @brief Detect if Dynamic Composition Builder was used
72 * @note If the builder is active, we bake the dynamic composition and skip legacy init
73 */
75 MESHX_LOGD(MODULE_ID_COMMON, "Dynamic Composition detected. Baking...");
76 err = meshx_builder_bake(p_dev, config->cid, config->pid, config->vid);
77 if (err != MESHX_SUCCESS) return err;
78
79 // Dynamic comp already initialized plat composition and models
80 return MESHX_SUCCESS;
81 }
82 return MESHX_SUCCESS;
83}
bool meshx_builder_is_active(void)
Checks if a dynamic composition has been built.
Definition meshx_composition_builder.cpp:98
meshx_err_t meshx_builder_bake(dev_struct_t *pdev, uint16_t cid, uint16_t pid, uint16_t vid)
Bakes the dynamic composition into the device structure.
Definition meshx_composition_builder.cpp:102
@ MESHX_INVALID_STATE
Definition meshx_err.h:49
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:132

◆ meshx_get_net_key_id()

uint16_t meshx_get_net_key_id ( void )

Get the network key identifier.

Returns
uint16_t The network key identifier.
317{
318 return g_dev.meshx_store.net_key_id;
319}

◆ meshx_init()

meshx_err_t meshx_init ( meshx_config_t const * config)

MeshX initialisation function.

This function initialises the MeshX stack with the given configuration.

Parameters
[in]configPointer to the configuration structure
Returns
MESHX_SUCCESS, Success
244{
245 /* Check if the configuration is valid */
246 if(!config)
247 return MESHX_INVALID_ARG;
248
250
251 /* Copy the configuration to the global config structure */
252 memcpy(&g_config, config, sizeof(meshx_config_t));
253
254 /* Initialise Platform deps */
255 err = meshx_platform_init();
256 MESHX_ERR_PRINT_RET("Platform init failed", err);
257
258 meshx_logging_t logging_cfg;
259 logging_cfg.def_log_level = config->meshx_log_level == MESHX_LOG_VERBOSE ?
260 CONFIG_MESHX_DEFAULT_LOG_LEVEL : config->meshx_log_level;
261
262 err = meshx_logging_init(&logging_cfg);
263 MESHX_ERR_PRINT_RET("Logging init failed", err);
264
265 MESHX_LOGI(MODULE_ID_COMMON, "MeshX Logging Initialized successfully");
266
267 /* Load Persistent Configuration and apply UUID/Product Info */
268 err = meshx_load_persistent_config(config);
269 MESHX_ERR_PRINT_RET("Failed to load persistent configuration", err);
270
271 /* Initialise Control Task messaging early (required for NVS and OS Timers) */
272 err = control_task_init();
273 MESHX_ERR_PRINT_RET("Control Task Init failed", err);
274
275 /* Initialize OS timer */
276 err = meshx_os_timer_init();
277 MESHX_ERR_PRINT_RET("OS Timer Init failed", err);
278
279 /* Initialize MeshX NVS */
280 err = meshx_nvs_init();
281 MESHX_ERR_PRINT_RET("MeshX NVS Init failed", err);
282
283 /* Restore the device state and open NVS (Required before element constructors run) */
285 MESHX_ERR_PRINT_RET("Device restore failed", err);
286
287 /* Initialize application tasks */
288 err = meshx_tasks_init(&g_dev);
289 MESHX_ERR_PRINT_RET("Tasks initialization failed", err);
290
291 /* Register application element callback */
292 err = meshx_app_reg_element_callback(g_config.app_element_cb);
293 MESHX_ERR_PRINT_RET("Failed to register app element callback", err);
294
295 /* Register application control callback */
297 MESHX_ERR_PRINT_RET("Failed to register app control callback", err);
298
299 /* Initialize the Bluetooth Mesh Subsystem */
301 MESHX_ERR_PRINT_RET("Bluetooth mesh init failed", err);
302
303 /* Initialize Hosted Serial Protocol */
304 err = meshx_serial_init();
305 MESHX_ERR_PRINT_RET("Serial init failed", err);
306
307#if CONFIG_ENABLE_UNIT_TEST
308 /* Initialize unit test console (this also registers the 'ut' command) */
310 MESHX_ERR_PRINT_RET("Failed to initialize unit test console", err);
311#endif /* CONFIG_ENABLE_UNIT_TEST */
312
313 return err;
314}
static meshx_config_t g_config
Definition meshx.c:35
static meshx_err_t meshx_load_persistent_config(meshx_config_t const *config)
Load persistent read-only configuration from flash and apply it.
Definition meshx.c:181
static meshx_err_t meshx_dev_restore(dev_struct_t *pdev, meshx_config_t const *config)
Restore the device state from the NVS.
Definition meshx.c:115
static meshx_err_t meshx_tasks_init(dev_struct_t *pdev)
Initializes application tasks.
Definition meshx.c:92
static meshx_err_t meshx_ble_mesh_init(meshx_config_t *config)
Initializes the BLE Mesh subsystem.
Definition meshx.c:139
struct meshx_config meshx_config_t
meshx_err_t meshx_app_reg_element_callback(meshx_app_data_cb_t cb)
Registers the BLE Mesh application callback.
Definition meshx_api.c:206
meshx_err_t meshx_app_reg_system_events_callback(meshx_app_ctrl_cb_t cb)
Registers the BLE Mesh application control callback.
Definition meshx_api.c:234
meshx_err_t control_task_init(void)
Initialize the control task messaging system.
Definition meshx_control_task.c:45
meshx_err_t meshx_logging_init(const meshx_logging_t *config)
Definition meshx_log.c:88
#define MESHX_LOGI(module_id, format,...)
Definition meshx_log.h:126
#define MESHX_LOG_VERBOSE
Definition meshx_log.h:31
#define CONFIG_MESHX_DEFAULT_LOG_LEVEL
Definition meshx_log.h:66
meshx_err_t meshx_nvs_init(void)
MeshX NVS Initialisation.
Definition meshx_nvs.c:142
meshx_err_t meshx_os_timer_init(void)
Initialize the OS timer module.
Definition meshx_os_timer.c:227
meshx_err_t meshx_platform_init(void)
Initializes the MeshX platform.
Definition esp_platform.c:60
meshx_err_t meshx_serial_init(void)
Initialize the MeshX Serial Protocol handler.
Definition meshx_serial.c:517
Definition meshx_log.h:37
meshx_log_level_t def_log_level
Definition meshx_log.h:38
meshx_err_t init_unit_test_console(void)
Initialize the unit test console.
Definition unit_test.c:103

◆ meshx_load_persistent_config()

meshx_err_t meshx_load_persistent_config ( meshx_config_t const * config)
static

Load persistent read-only configuration from flash and apply it.

This function attempts to load product identification (CID, PID, Name) and device UUID from the meshx_cfg partition. If the partition is missing or the data is invalid, it falls back to the defaults provided in the config struct.

Parameters
configPointer to the initial configuration provided by the application.
Returns
meshx_err_t MESHX_SUCCESS on success, or an error code on fatal failure.
182{
183 meshx_err_t err;
184 uint16_t loaded_cid = 0xFFFF;
185 uint16_t loaded_pid = 0xFFFF;
186 uint8_t loaded_uuid[16] = {0};
187
188 /* Load Persistent Read-Only Configuration */
189 err = meshx_ro_cfg_init(&loaded_cid, &loaded_pid, g_product_name, sizeof(g_product_name), loaded_uuid);
190 if (err != MESHX_SUCCESS) {
191 if (err == MESHX_NOT_FOUND || err == MESHX_ERR_RO_CFG_FORMAT) {
192 MESHX_LOGW(MODULE_ID_COMMON, "No valid read-only config found (err 0x%x). Using legacy defaults.", err);
193
194 // Fallback product name if not loaded from partition
195 if (g_product_name[0] == '\0' && config->product_name) {
196 strncpy(g_product_name, config->product_name, sizeof(g_product_name) - 1);
197 g_product_name[sizeof(g_product_name)-1] = '\0';
198 }
199
200 // Also fallback CID/PID to what was provided in config
201 loaded_cid = config->cid;
202 loaded_pid = config->pid;
203 } else {
204 return err;
205 }
206 }
207
208 g_config.cid = loaded_cid;
209 g_config.pid = loaded_pid;
210 g_config.product_name = g_product_name;
211
212 // Check if UUID was loaded (if it's not all zeros)
213 bool uuid_loaded = false;
214 for (int i = 0; i < 16; i++) {
215 if (loaded_uuid[i] != 0) {
216 uuid_loaded = true;
217 break;
218 }
219 }
220
221 if (uuid_loaded) {
222 memcpy(g_config.meshx_uuid_addr, loaded_uuid, 16);
223 MESHX_LOGD(MODULE_ID_COMMON, "Using Persistent UUID from config");
224 } else {
225 /* Fallback to legacy way: use the UUID from the passed configuration (which might be all zeros) */
226 memcpy(g_config.meshx_uuid_addr, config->meshx_uuid_addr, 16);
227 MESHX_LOGI(MODULE_ID_COMMON, "Using Legacy UUID generation");
228 }
229
230 return MESHX_SUCCESS;
231}
static char g_product_name[32]
Definition meshx.c:36
@ MESHX_NOT_FOUND
Definition meshx_err.h:50
@ MESHX_ERR_RO_CFG_FORMAT
Definition meshx_err.h:57
meshx_err_t meshx_ro_cfg_init(uint16_t *cid, uint16_t *pid, char *product_name, size_t name_max_len, uint8_t *uuid)
Initialize and load the MeshX configuration from the meshx_cfg partition.
Definition meshx_ro_cfg.c:90

◆ meshx_tasks_init()

meshx_err_t meshx_tasks_init ( dev_struct_t * pdev)
static

Initializes application tasks.

Parameters
[in]pdevPointer to the device structure.
Returns
MESHX_SUCCESS on success, error code otherwise.
93{
94 meshx_err_t err;
95
96 err = create_control_task(pdev);
97 MESHX_ERR_PRINT_RET("Failed to create control task", err);
98
99#if CONFIG_TXCM_ENABLE
100 err = meshx_txcm_init(pdev);
101 MESHX_ERR_PRINT_RET("Failed to create Tx Control Module", err);
102#endif /* CONFIG_TXCM_ENABLE */
103 return err;
104}
meshx_err_t create_control_task(dev_struct_t *pdev)
Create the control task.
Definition meshx_control_task.c:58
meshx_err_t meshx_txcm_init(dev_struct_t *pdev)
Initializes the MeshX Tx Control Module.
Definition meshx_txcm.c:628

Variable Documentation

◆ g_config

meshx_config_t g_config
static

◆ g_dev

dev_struct_t g_dev
static

◆ g_product_name

char g_product_name[32]
static

◆ g_prov_cfg

meshx_prov_params_t g_prov_cfg
Initial value:
= {
.uuid = (meshx_uuid_addr_t){0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} ,
.node_name = NULL
}
uint8_t meshx_uuid_addr_t[16]
Definition meshx_ble_mesh_cmn_def.h:649
38 {
39 .uuid = MESHX_UUID_EMPTY, /**< UUID for the provisioning device */
40 .node_name = NULL /**< Node name for the provisioning device */
41};
#define MESHX_UUID_EMPTY
Definition meshx_ble_mesh_cmn_def.h:651