MeshX 0.3
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
Go to the documentation of this file.
1
13#include "meshx.h"
14
19#define ROOT_ELEMENT_IDX 0
20
25#define FRESHBOOT_TIMEOUT_MS 1500
26
27
34static const char meshX_banner[] = {
35"*********************************************************************************************************************\n"
36"* MMMMMMMM MMMMMMMM hhhhhhh XXXXXXX XXXXXXX *\n"
37"* M:::::::M M:::::::M h:::::h X:::::X X:::::X *\n"
38"* M::::::::M M::::::::M h:::::h X:::::X X:::::X *\n"
39"* M:::::::::M M:::::::::M h:::::h X::::::X X:::::X *\n"
40"* M::::::::::M M::::::::::M eeeeeeeeeeee ssssssssss h:::: hhhhhh XX:::::X X:::::XX *\n"
41"* M:::::::::::M M:::::::::::M ee::::::::::::ee ss::::::::::s h::::::::::hhh X:::::X X:::::X *\n"
42"* M:::::::M::::M M::::M:::::::M e::::::eeeee:::::eess:::::::::::::s h::::::::::::::hh X:::::X:::::X *\n"
43"* M::::::M M::::M M::::M M::::::Me::::::e e:::::es::::::ssss:::::sh:::::::hhh::::::h X:::::::::X *\n"
44"* M::::::M M::::M::::M M::::::Me:::::::eeeee::::::e s:::::s ssssss h::::::h h::::::h X:::::::::X *\n"
45"* M::::::M M:::::::M M::::::Me:::::::::::::::::e s::::::s h:::::h h:::::h X:::::X:::::X *\n"
46"* M::::::M M:::::M M::::::Me::::::eeeeeeeeeee s::::::s h:::::h h:::::h X:::::X X:::::X *\n"
47"* M::::::M MMMMM M::::::Me:::::::e ssssss s:::::s h:::::h h:::::h XXX:::::X X:::::XXX *\n"
48"* M::::::M M::::::Me::::::::e s:::::ssss::::::sh:::::h h:::::h X::::::X X::::::X *\n"
49"* M::::::M M::::::M e::::::::eeeeeeee s::::::::::::::s h:::::h h:::::h X:::::X X:::::X *\n"
50"* M::::::M M::::::M ee:::::::::::::e s:::::::::::ss h:::::h h:::::h X:::::X X:::::X *\n"
51"* MMMMMMMM MMMMMMMM eeeeeeeeeeeeee sssssssssss hhhhhhh hhhhhhh XXXXXXX XXXXXXX *\n"
52"*********************************************************************************************************************\n"
53};
54
56
58
60 .uuid = MESHX_UUID_EMPTY,
61 .node_name = NULL
62};
63
64extern size_t get_root_sig_models_count(void);
65extern size_t get_root_ven_models_count(void);
69
79{
80 if (!p_dev)
82
86
88 if (err)
89 {
90 MESHX_LOGE(MODULE_ID_COMMON, "Failed to create platform composition: (%d)", err);
91 return err;
92 }
93
94 /* Start with element index 1 as 0 is root element */
95 p_dev->element_idx = 1;
96
97 err = meshx_create_element_composition(p_dev, config);
98 if(err)
99 {
100 MESHX_LOGE(MODULE_ID_COMMON, "Failed to create BLE Mesh Element Composition: (%d)", err);
101 return err;
102 }
104 p_dev->composition,
105 p_dev->elements,
106 config->cid,
107 config->pid,
108 (uint16_t)p_dev->element_idx
109 );
110 if(err)
111 {
112 MESHX_LOGE(MODULE_ID_COMMON, "Failed to initialise MeshX Composition: (%d)", err);
113 return err;
114 }
115
117 if(meshx_sig_root_model_arr == NULL)
118 {
119 MESHX_LOGE(MODULE_ID_COMMON, "Failed to get root SIG models");
120 return MESHX_FAIL;
121 }
122
130 p_dev->elements,
133 (uint8_t) get_root_sig_models_count(),
134 (uint8_t) get_root_ven_models_count()
135 );
136 if(err)
137 {
138 MESHX_LOGE(MODULE_ID_COMMON, "Failed to add element to composition: (%d)", err);
139 return err;
140 }
141 return MESHX_SUCCESS;
142}
143
152{
153 meshx_err_t err;
154
155 err = create_control_task(pdev);
156 MESHX_ERR_PRINT_RET("Failed to create control task", err);
157
158#if CONFIG_TXCM_ENABLE
159 err = meshx_txcm_init(pdev);
160 MESHX_ERR_PRINT_RET("Failed to create Tx Control Module", err);
161#endif /* CONFIG_TXCM_ENABLE */
162 return err;
163}
164
175{
177
178 err = meshx_nvs_open(config->cid, config->pid, config->meshx_nvs_save_period);
179 MESHX_ERR_PRINT_RET("MeshX NVS Open failed", err);
180
182 MESHX_ERR_PRINT_RET("Failed to restore meshx device state", err);
183
184 return err;
185}
186
195{
196 if(config == NULL || config->product_name == NULL)
197 return MESHX_INVALID_ARG;
198
199 meshx_err_t err;
200 g_prov_cfg.node_name = (uint8_t *)config->product_name;
201
203 MESHX_ERR_PRINT_RET("Platform BT init failed", err);
204
205 g_prov_cfg.uuid = config->meshx_uuid_addr;
206 g_prov_cfg.freshboot_timeout_ms = FRESHBOOT_TIMEOUT_MS;
207
208 meshx_dev_restore(&g_dev, config);
209
210 err = meshx_element_init(&g_dev, config);
211 MESHX_ERR_PRINT_RET("Failed to initialize BLE Elements", err);
212
214 MESHX_ERR_PRINT_RET("Failed to initialize provisioning", err);
215
216 err = meshx_plat_ble_mesh_init(&g_prov_cfg, g_dev.composition);
217 MESHX_ERR_PRINT_RET("Failed to initialize BLE Mesh stack", err);
218
219 return MESHX_SUCCESS;
220}
221
232{
233 /* Check if the configuration is valid */
234 if(!config)
235 return MESHX_INVALID_ARG;
236
238
239 /* Copy the configuration to the global config structure */
240 memcpy(&g_config, config, sizeof(meshx_config_t));
241
242 meshx_logging_t logging_cfg;
243
244 logging_cfg.def_log_level = config->meshx_log_level == MESHX_LOG_VERBOSE ?
246
247 err = meshx_logging_init(&logging_cfg);
248 MESHX_ERR_PRINT_RET("Logging init failed", err);
249
250 /* Initialise Platform deps */
251 err = meshx_platform_init();
252 MESHX_ERR_PRINT_RET("Platform init failed", err);
253
254 /* Initialize OS timer */
255 err = meshx_os_timer_init();
256 MESHX_ERR_PRINT_RET("OS Timer Init failed", err);
257
258 /* Initialize MeshX NVS */
259 err = meshx_nvs_init();
260 MESHX_ERR_PRINT_RET("MeshX NVS Init failed", err);
261
262 /* Initialize application tasks */
263 err = meshx_tasks_init(&g_dev);
264 MESHX_ERR_PRINT_RET("Tasks initialization failed", err);
265
266 /* Register application element callback */
267 err = meshx_app_reg_element_callback(g_config.app_element_cb);
268 MESHX_ERR_PRINT_RET("Failed to register app element callback", err);
269
270 /* Register application control callback */
272 MESHX_ERR_PRINT_RET("Failed to register app control callback", err);
273
274 /* Initialize the Bluetooth Mesh Subsystem */
276 MESHX_ERR_PRINT_RET("Bluetooth mesh init failed", err);
277
278 /* Print the MeshX banner */
279 CONFIG_MESHX_LOG_PRINTF(LOG_ANSI_COLOR_REGULAR(LOG_ANSI_COLOR_CYAN) "%s" LOG_ANSI_COLOR_RESET, meshX_banner);
280
281#if CONFIG_ENABLE_UNIT_TEST
282 /* Register unit test command */
283 err = register_ut_command();
284 MESHX_ERR_PRINT_RET("Failed to register unit test command", err);
285
286 /* Initialize unit test console */
288 MESHX_ERR_PRINT_RET("Failed to initialize production console", err);
289#endif /* CONFIG_ENABLE_UNIT_TEST */
290
291 return err;
292}
293
static MESHX_MODEL * meshx_ven_root_model_arr
Definition composition.c:65
size_t get_root_sig_models_count(void)
Returns the count of the root models.
static MESHX_MODEL * meshx_sig_root_model_arr
Definition composition.c:64
size_t get_root_ven_models_count(void)
Returns the count of the vendor root models.
static meshx_config_t g_config
Definition meshx.c:57
static const char meshX_banner[]
Definition meshx.c:34
#define ROOT_ELEMENT_IDX
Defines the index for the root element.
Definition meshx.c:19
meshx_ptr_t get_root_ven_models(void)
Returns the root models for BLE Mesh elements.
static meshx_err_t meshx_element_init(dev_struct_t *p_dev, meshx_config_t const *config)
Initializes BLE Mesh elements.
Definition meshx.c:78
meshx_prov_params_t g_prov_cfg
Definition meshx.c:59
size_t get_root_sig_models_count(void)
Returns the count of the root models.
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:174
static meshx_err_t meshx_tasks_init(dev_struct_t *pdev)
Initializes application tasks.
Definition meshx.c:151
meshx_err_t meshx_init(meshx_config_t const *config)
MeshX initialisation function.
Definition meshx.c:231
static meshx_err_t meshx_ble_mesh_init(meshx_config_t *config)
Initializes the BLE Mesh subsystem.
Definition meshx.c:194
meshx_ptr_t get_root_sig_models(void)
Returns the root models for BLE Mesh elements.
Definition composition.c:89
meshx_err_t meshx_create_element_composition(dev_struct_t *p_dev, meshx_config_t const *config)
Creates the BLE Mesh element composition.
#define FRESHBOOT_TIMEOUT_MS
Defines the timeout duration in milliseconds for a fresh boot.
Definition meshx.c:25
static dev_struct_t g_dev
Definition meshx.c:55
This file contains the headers for meshx.c.
struct meshx_config meshx_config_t
#define MESHX_ERR_PRINT_RET(_e_str, _err)
Print and return error message if an error occurs.
Definition meshx.h:37
meshx_err_t meshx_app_reg_element_callback(meshx_app_data_cb_t cb)
Registers the BLE Mesh application callback.
Definition meshx_api.c:148
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:176
meshx_err_t meshx_plat_add_element_to_composition(uint16_t index, meshx_ptr_t p_element_list, meshx_ptr_t p_sig_models, meshx_ptr_t p_ven_models, uint8_t sig_cnt, uint8_t ven_cnt)
Adds an element to the BLE Mesh composition.
meshx_err_t meshx_create_plat_composition(meshx_ptr_t *p_comp)
Creates a platform-specific BLE Mesh composition object.
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_composition_init(meshx_ptr_t p_composition, meshx_ptr_t p_elements, uint16_t cid, uint16_t pid, uint16_t element_idx)
Initializes a platform-specific BLE Mesh composition.
struct meshx_prov_params meshx_prov_params_t
Structure to hold provisioning parameters.
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.
void * meshx_ptr_t
#define MESHX_UUID_EMPTY
struct dev_struct dev_struct_t
Structure representing the device composition and elements.
#define MESHX_NVS_STORE
struct meshx_app_store meshx_app_store_t
Structure to store mesh application data.
meshx_err_t create_control_task(dev_struct_t *pdev)
Create the control task.
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:39
@ MESHX_SUCCESS
Definition meshx_err.h:40
@ MESHX_INVALID_ARG
Definition meshx_err.h:42
@ MESHX_FAIL
Definition meshx_err.h:41
@ MESHX_INVALID_STATE
Definition meshx_err.h:45
meshx_err_t meshx_logging_init(const meshx_logging_t *config)
Initializes the MeshX logging system with the provided configuration.
Definition meshx_log.c:61
#define MESHX_LOG_VERBOSE
Definition meshx_log.h:39
#define MESHX_LOGE(module_id, format,...)
Definition meshx_log.h:73
#define CONFIG_MESHX_DEFAULT_LOG_LEVEL
Definition meshx_log.h:23
struct meshx_logging meshx_logging_t
#define CONFIG_MESHX_LOG_PRINTF
Macro to define printf function.
Definition meshx_log.h:19
meshx_err_t meshx_nvs_init(void)
MeshX NVS Initialisation.
Definition meshx_nvs.c:135
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:165
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:346
meshx_err_t meshx_os_timer_init(void)
Initialize the OS timer module.
meshx_err_t meshx_platform_init(void)
Initializes the MeshX platform.
meshx_err_t meshx_init_prov(dev_struct_t *p_dev, const meshx_prov_params_t *prov_cfg)
Initialize provisioning parameters.
meshx_err_t meshx_txcm_init(dev_struct_t *pdev)
Initializes the MeshX Tx Control Module.
@ MODULE_ID_COMMON
Definition module_id.h:32
void * composition
meshx_app_store_t meshx_store
size_t element_idx
MESHX_ELEMENT elements[MAX_ELE_CNT]
uint32_t meshx_nvs_save_period
Definition meshx.h:65
uint16_t cid
Definition meshx.h:62
unsigned meshx_log_level
Definition meshx.h:70
uint16_t pid
Definition meshx.h:63
meshx_uuid_addr_t meshx_uuid_addr
Definition meshx.h:71
char * product_name
Definition meshx.h:64
unsigned def_log_level
Definition meshx_log.h:53
meshx_err_t register_ut_command()
Registers the unit test (ut) command with the ESP console.
Definition unit_test.c:81
meshx_err_t init_unit_test_console()
Initialize the production console.
Definition unit_test.c:100