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_prov_srv.cpp File Reference

This file contains the implementation of the provisioning process for the BLE mesh node. Migrated to C++ to align with the modern MeshX element architecture. More...

#include <meshx_c_header.h>

Data Structures

struct  prov_cb_evt_ctrl_task_evt_table
 Structure to map provisioning callback events to control task events. More...

Macros

#define CONTROL_TASK_PROV_EVT_MASK
 Mask for control task provisioning events.
#define MESHX_PROV_SRV_CLIENT_EVENT_BMAP   (CONTROL_TASK_MSG_EVT_SYSTEM_FRESH_BOOT)
#define MESHX_PROV_SRV_SERVER_EVENT_BMAP   (CONTROL_TASK_MSG_EVT_SYSTEM_STACK_READY)

Typedefs

typedef struct prov_cb_evt_ctrl_task_evt_table prov_cb_evt_ctrl_task_evt_table_t
 Structure to map provisioning callback events to control task events.

Functions

static void init_prov_cb_table (void)
static meshx_err_t meshx_prov_srv_control_task_handler (dev_struct_t *pdev, control_task_msg_evt_t evt, meshx_prov_srv_param_t *params)
 Control task handler for the provisioning server.
static void meshx_handle_node_reset (dev_struct_t *pdev)
 Handle node reset event.
static meshx_err_t meshx_prov_control_task_handler (dev_struct_t *pdev, control_task_msg_evt_t evt, void *params)
 Handles provisioning control task events.
meshx_err_t meshx_prov_srv_meshx_reg_cb (void)
static void meshx_init_freshboot_timer_trigger_cb (const meshx_os_timer_t *p_timer)
static meshx_err_t meshx_init_freshboot_timer (dev_struct_t *p_dev, uint16_t timeout_ms)
meshx_err_t meshx_prov_srv_reg_from_ble_cb (void)
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_prov_srv_reg_el_client_cb (prov_srv_cb_t cb)
 Register a callback function for provisioning events.
meshx_err_t meshx_prov_srv_reg_el_server_cb (prov_srv_cb_t cb)
 Register a callback function for provisioning events.
meshx_err_t meshx_prov_srv_notify_plat_event (meshx_prov_srv_param_t *param)
 Notify the model event to the application.
bool meshx_prov_srv_is_provisioned (void)
 Checks if the device is provisioned.

Variables

static prov_cb_evt_ctrl_task_evt_table_t prov_cb_evt_ctrl_task_evt_table [MESHX_PROV_EVT_MAX] = {}
 Table mapping provisioning events to control task events.
static meshx_os_timer_tg_boot_timer

Detailed Description

This file contains the implementation of the provisioning process for the BLE mesh node. Migrated to C++ to align with the modern MeshX element architecture.

Copyright © 2024 - 2025 MeshX

Author
Pranjal Chanda

Macro Definition Documentation

◆ CONTROL_TASK_PROV_EVT_MASK

#define CONTROL_TASK_PROV_EVT_MASK
Value:
CONTROL_TASK_MSG_EVT_IDENTIFY_START
Definition meshx_control_task.h:161
CONTROL_TASK_MSG_EVT_PROVISION_STOP
Definition meshx_control_task.h:160
CONTROL_TASK_MSG_EVT_PROXY_CONNECT
Definition meshx_control_task.h:164
CONTROL_TASK_MSG_EVT_PROV_REG_COMP
Definition meshx_control_task.h:158
CONTROL_TASK_MSG_EVT_SYSTEM_STACK_READY
Definition meshx_control_task.h:168
CONTROL_TASK_MSG_EVT_IDENTIFY_STOP
Definition meshx_control_task.h:162
CONTROL_TASK_MSG_EVT_NODE_RESET
Definition meshx_control_task.h:163
CONTROL_TASK_MSG_EVT_EN_NODE_PROV
Definition meshx_control_task.h:166
CONTROL_TASK_MSG_EVT_SET_NAME_COMP
Definition meshx_control_task.h:159
CONTROL_TASK_MSG_EVT_PROXY_DISCONN
Definition meshx_control_task.h:165

Mask for control task provisioning events.

18#define CONTROL_TASK_PROV_EVT_MASK ( CONTROL_TASK_MSG_EVT_PROV_REG_COMP | \
19 CONTROL_TASK_MSG_EVT_IDENTIFY_START | \
20 CONTROL_TASK_MSG_EVT_IDENTIFY_STOP | \
21 CONTROL_TASK_MSG_EVT_PROVISION_STOP | \
22 CONTROL_TASK_MSG_EVT_NODE_RESET | \
23 CONTROL_TASK_MSG_EVT_SET_NAME_COMP | \
24 CONTROL_TASK_MSG_EVT_EN_NODE_PROV | \
25 CONTROL_TASK_MSG_EVT_SYSTEM_STACK_READY | \
26 CONTROL_TASK_MSG_EVT_PROXY_CONNECT | \
27 CONTROL_TASK_MSG_EVT_PROXY_DISCONN )

◆ MESHX_PROV_SRV_CLIENT_EVENT_BMAP

#define MESHX_PROV_SRV_CLIENT_EVENT_BMAP   (CONTROL_TASK_MSG_EVT_SYSTEM_FRESH_BOOT)

◆ MESHX_PROV_SRV_SERVER_EVENT_BMAP

#define MESHX_PROV_SRV_SERVER_EVENT_BMAP   (CONTROL_TASK_MSG_EVT_SYSTEM_STACK_READY)

Typedef Documentation

◆ prov_cb_evt_ctrl_task_evt_table_t

Structure to map provisioning callback events to control task events.

Function Documentation

◆ init_prov_cb_table()

void init_prov_cb_table ( void )
static
51{
52 static bool initialized = false;
53 if (initialized) return;
54
55#if CONFIG_MESHX_DEFAULT_LOG_LEVEL < MESHX_LOG_INFO
65#else
75#endif
76 initialized = true;
77}
@ MESHX_NODE_PROV_COMPLETE_EVT
Definition meshx_ble_mesh_cmn_def.h:510
@ MESHX_PROXY_SERVER_CONNECTED_EVT
Definition meshx_ble_mesh_cmn_def.h:589
@ MESHX_NODE_PROV_RESET_EVT
Definition meshx_ble_mesh_cmn_def.h:511
@ MESHX_PROV_REGISTER_COMP_EVT
Definition meshx_ble_mesh_cmn_def.h:500
@ MESHX_PROXY_SERVER_DISCONNECTED_EVT
Definition meshx_ble_mesh_cmn_def.h:590
@ MESHX_NODE_SET_UPROV_NAME_COMP_EVT
Definition meshx_ble_mesh_cmn_def.h:501
@ MESHX_NODE_PROV_LINK_OPEN_EVT
Definition meshx_ble_mesh_cmn_def.h:504
@ MESHX_NODE_PROV_LINK_CLOSE_EVT
Definition meshx_ble_mesh_cmn_def.h:505
@ MESHX_NODE_PROV_ENABLE_COMP_EVT
Definition meshx_ble_mesh_cmn_def.h:502
bool initialized
Definition meshx_shell.c:27
Structure to map provisioning callback events to control task events.
Definition meshx_prov_srv.cpp:36
control_task_msg_evt_provision_t ctrl_task_evt
Definition meshx_prov_srv.cpp:40

◆ meshx_handle_node_reset()

void meshx_handle_node_reset ( dev_struct_t * pdev)
static

Handle node reset event.

134{
135 if(!pdev)
136 {
137 MESHX_LOGE(MODULE_ID_COMMON, "Invalid device structure");
138 return;
139 }
140
141 for(size_t i = 1; i < pdev->element_idx; ++i)
142 {
143 meshx_err_t err = meshx_nvs_element_ctx_remove(static_cast<uint16_t>(i), MESHX_ELEMENT_TYPE_ALL);
144 if(err != MESHX_SUCCESS)
145 {
146 MESHX_LOGE(MODULE_ID_COMMON, "Failed to erase element context (%zu): (%d)", i, err);
147 }
148 }
149
150 /* Notify application about node reset */
152
153 /* Reset the MCU */
155}
meshx_err_t meshx_send_ctrl_msg_to_app(meshx_ctrl_evt_t evt, uint16_t msg_len, const void *msg)
Sends a control message to the BLE Mesh application.
Definition meshx_api.c:154
@ MESHX_CTRL_EVT_NODE_RESET
Definition meshx_common.h:32
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
#define MESHX_LOGE(module_id, format,...)
Definition meshx_log.h:114
**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_nvs_element_ctx_remove(uint16_t element_id, meshx_element_type_t element_type)
Remove the context of a specific element from NVS.
Definition meshx_nvs.c:482
void meshx_platform_reset(void)
Resets the MeshX platform. This function performs a system reset, restarting the platform.
Definition esp_platform.c:89
@ MODULE_ID_COMMON
Definition module_id.h:36
size_t element_idx
Definition meshx_common.h:71

◆ meshx_init_freshboot_timer()

meshx_err_t meshx_init_freshboot_timer ( dev_struct_t * p_dev,
uint16_t timeout_ms )
static
215{
217 {
218 MESHX_LOGI(MODULE_ID_COMMON, "Device not provisioned, not starting boot timer");
219 return MESHX_SUCCESS;
220 }
221 meshx_err_t err = meshx_os_timer_create("boot_timer",
222 timeout_ms,
223 false,
226 );
227 if(err != MESHX_SUCCESS)
228 {
229 MESHX_LOGE(MODULE_ID_COMMON, "Failed to create boot timer: (%d)", err);
230 }
231
233 if(err != MESHX_SUCCESS)
234 {
235 MESHX_LOGE(MODULE_ID_COMMON, "Failed to start boot timer: (%d)", err);
236 }
237
238 return err;
239}
#define MESHX_ADDR_UNASSIGNED
Definition meshx_ble_mesh_cmn_def.h:30
#define MESHX_LOGI(module_id, format,...)
Definition meshx_log.h:126
meshx_err_t meshx_os_timer_start(const meshx_os_timer_t *timer_handle)
Start a timer.
Definition meshx_os_timer.c:315
meshx_err_t meshx_os_timer_create(const char *name, uint32_t period, bool reload, meshx_os_timer_cb_t cb, meshx_os_timer_t **timer_handle)
Create a timer.
Definition meshx_os_timer.c:264
static meshx_os_timer_t * g_boot_timer
Definition meshx_prov_srv.cpp:79
static void meshx_init_freshboot_timer_trigger_cb(const meshx_os_timer_t *p_timer)
Definition meshx_prov_srv.cpp:197
meshx_app_store_t meshx_store
Definition meshx_common.h:75
uint16_t node_addr
Definition meshx_common.h:62

◆ meshx_init_freshboot_timer_trigger_cb()

void meshx_init_freshboot_timer_trigger_cb ( const meshx_os_timer_t * p_timer)
static
198{
199 MESHX_UNUSED(p_timer);
200 MESHX_LOGD(MODULE_ID_COMMON, "Fresh Boot Timer Expired");
201
205 NULL,
206 0
207 );
208 if(err)
209 {
210 MESHX_LOGE(MODULE_ID_COMMON, "Failed to publish fresh boot event: (%d)", err);
211 }
212}
CONTROL_TASK_MSG_EVT_SYSTEM_FRESH_BOOT
Definition meshx_control_task.h:167
@ CONTROL_TASK_MSG_CODE_PROVISION
Definition meshx_control_task.h:70
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
#define MESHX_UNUSED(x)
Definition meshx_err.h:19
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:132

◆ meshx_init_prov()

meshx_err_t meshx_init_prov ( dev_struct_t * p_dev,
const meshx_prov_params_t * prov_cfg )

Initialize provisioning parameters.

This function initializes the provisioning parameters by copying the UUID from the provided server configuration and registering the provisioning callback.

Parameters
[in]p_devPointer to the device structure.
[in]prov_cfgPointer to the provisioning parameters structure containing the UUID.
Returns
  • MESHX_SUCCESS: Success
  • MESHX_FAIL: Failed to register provisioning callback
252{
253 if (!prov_cfg || memcmp(prov_cfg->uuid, MESHX_UUID_EMPTY, sizeof(meshx_uuid_addr_t)) == 0)
254 {
255 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Invalid server configuration");
256 return MESHX_INVALID_ARG;
257 }
258
260 if (err != MESHX_SUCCESS)
261 {
262 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Failed to register provisioning callback");
263 return err;
264 }
265
267 if (err != MESHX_SUCCESS)
268 {
269 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Failed to register provisioning callback");
270 return err;
271 }
272
273 err = meshx_init_freshboot_timer(p_dev, prov_cfg->freshboot_timeout_ms);
274 if (err != MESHX_SUCCESS)
275 {
276 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Failed to initialize boot timer");
277 return err;
278 }
279
280 return meshx_plat_init_prov(prov_cfg->uuid);
281}
uint8_t meshx_uuid_addr_t[16]
Definition meshx_ble_mesh_cmn_def.h:649
#define MESHX_UUID_EMPTY
Definition meshx_ble_mesh_cmn_def.h:651
meshx_err_t meshx_plat_init_prov(const uint8_t *uuid)
Initialize provisioning parameters.
@ MESHX_INVALID_ARG
Definition meshx_err.h:46
static meshx_err_t meshx_init_freshboot_timer(dev_struct_t *p_dev, uint16_t timeout_ms)
Definition meshx_prov_srv.cpp:214
meshx_err_t meshx_prov_srv_reg_from_ble_cb(void)
Definition meshx_prov_srv.cpp:241
meshx_err_t meshx_prov_srv_meshx_reg_cb(void)
Definition meshx_prov_srv.cpp:189
@ MODULE_ID_MODEL_SERVER
Definition module_id.h:34
uint8_t * uuid
Definition meshx_ble_mesh_cmn.h:60
uint16_t freshboot_timeout_ms
Definition meshx_ble_mesh_cmn.h:62

◆ meshx_prov_control_task_handler()

meshx_err_t meshx_prov_control_task_handler ( dev_struct_t * pdev,
control_task_msg_evt_t evt,
void * params )
static

Handles provisioning control task events.

161{
162 const meshx_prov_cb_param_t *param = (meshx_prov_cb_param_t*) params;
163
164 switch (evt)
165 {
170 break;
172 MESHX_LOGI(MODULE_ID_COMMON, "Identify Start");
173 break;
175 MESHX_LOGW(MODULE_ID_COMMON, "Node Reset Event");
177 break;
181 /* Benign events during boot, handled to silence warnings */
182 break;
183 default:
184 break;
185 }
186 return MESHX_SUCCESS;
187}
#define MESHX_NVS_STORE
Definition meshx_common.h:54
#define MESHX_LOGW(module_id, format,...)
Definition meshx_log.h:120
meshx_err_t meshx_nvs_set(char const *key, void const *blob, uint16_t blob_size, bool arm_timer)
Set a value in the NVS.
Definition meshx_nvs.c:388
#define MESHX_NVS_AUTO_COMMIT
Definition meshx_nvs.h:30
static void meshx_handle_node_reset(dev_struct_t *pdev)
Handle node reset event.
Definition meshx_prov_srv.cpp:133
uint16_t net_key_id
Definition meshx_common.h:61
uint16_t addr
Definition meshx_ble_mesh_prov_srv.h:135
uint16_t net_idx
Definition meshx_ble_mesh_prov_srv.h:133
BLE Mesh Node/Provisioner callback parameters union.
Definition meshx_ble_mesh_prov_srv.h:61
struct meshx_prov_cb_param_t::meshx_provision_complete_evt_param node_prov_complete

◆ meshx_prov_srv_control_task_handler()

meshx_err_t meshx_prov_srv_control_task_handler ( dev_struct_t * pdev,
control_task_msg_evt_t evt,
meshx_prov_srv_param_t * params )
static

Control task handler for the provisioning server.

88{
89 if (!pdev || !params)
90 {
91 return MESHX_INVALID_ARG;
92 }
96 {
97 return MESHX_INVALID_ARG;
98 }
99
101 {
102#if CONFIG_MESHX_DEFAULT_LOG_LEVEL < MESHX_LOG_INFO
103 MESHX_LOGD(MODULE_ID_MODEL_SERVER, "Provisioning event mapped: %s",
105#endif
107 }
108 else
109 {
110 MESHX_LOGD(MODULE_ID_MODEL_SERVER, "Unhandled event: %d", params->prov_evt);
111 }
113 {
114 MESHX_LOGI(MODULE_ID_MODEL_SERVER, "net_idx: 0x%04x, addr: 0x%04x", params->param.node_prov_complete.net_idx, params->param.node_prov_complete.addr);
115 MESHX_LOGD(MODULE_ID_MODEL_SERVER, "flags: 0x%02x, iv_index: 0x%08" PRIx32, params->param.node_prov_complete.flags, params->param.node_prov_complete.iv_index);
116 }
117 if (prov_evt == 0)
118 {
119 return MESHX_SUCCESS;
120 }
121
122 /* Publish the event */
125 (control_task_msg_evt_t)prov_evt,
126 &params->param,
127 sizeof(meshx_prov_cb_param_t));
128}
@ MESHX_PROV_EVT_MAX
Definition meshx_ble_mesh_cmn_def.h:595
uint32_t control_task_msg_evt_t
Type definition for control task message event.
Definition meshx_control_task.h:81
CONTROL_TASK_MSG_EVT_PROVISION_ALL
Definition meshx_control_task.h:169
enum __packed control_task_msg_evt_provision control_task_msg_evt_provision_t
Enumeration for control task provisioning events.
static void init_prov_cb_table(void)
Definition meshx_prov_srv.cpp:50
meshx_prov_cb_param_t param
Definition meshx_ble_mesh_prov_srv.h:861
uint16_t prov_evt
Definition meshx_ble_mesh_prov_srv.h:860
uint8_t flags
Definition meshx_ble_mesh_prov_srv.h:136
uint32_t iv_index
Definition meshx_ble_mesh_prov_srv.h:137

◆ meshx_prov_srv_is_provisioned()

bool meshx_prov_srv_is_provisioned ( void )

Checks if the device is provisioned.

This function returns whether the device has been provisioned.

Returns
bool true if provisioned, false otherwise.
317{
318 uint16_t primary_addr = 0;
319 if (MESHX_SUCCESS == meshx_get_base_element_id(&primary_addr))
320 {
321 return (primary_addr != MESHX_ADDR_UNASSIGNED);
322 }
323 return false;
324}
meshx_err_t meshx_get_base_element_id(uint16_t *base_el_id)
Retrieves the base element ID for the BLE Mesh platform.

◆ meshx_prov_srv_meshx_reg_cb()

meshx_err_t meshx_prov_srv_meshx_reg_cb ( void )
190{
195}
meshx_err_t(* control_task_msg_handle_t)(dev_struct_t *pdev, control_task_msg_evt_t evt, void *params)
Function pointer type for control task message handler.
Definition meshx_control_task.h:188
meshx_err_t control_task_msg_subscribe(control_task_msg_code_t msg_code, control_task_msg_evt_t evt_bmap, control_task_msg_handle_t callback)
Subscribe to a control task message.
Definition meshx_control_task.c:142
#define CONTROL_TASK_PROV_EVT_MASK
Mask for control task provisioning events.
Definition meshx_prov_srv.cpp:18
static meshx_err_t meshx_prov_control_task_handler(dev_struct_t *pdev, control_task_msg_evt_t evt, void *params)
Handles provisioning control task events.
Definition meshx_prov_srv.cpp:160

◆ meshx_prov_srv_notify_plat_event()

meshx_err_t meshx_prov_srv_notify_plat_event ( meshx_prov_srv_param_t * param)

Notify the model event to the application.

Note
This API is not to be defined in Platform Port layer and shall be called by the respective platform event handler to notify the MeshX of a model event.

This function notifies the application of a model event by invoking the registered provisioning callback with the provided event parameters.

Parameters
[in]paramPointer to the event parameters structure.
Returns
  • MESHX_SUCCESS: Success
  • MESHX_FAIL: Failed to notify model event
302{
303 if (!param)
304 {
305 return MESHX_INVALID_ARG;
306 }
307
311 param,
313 );
314}
struct meshx_ble_mesh_prov_srv_param meshx_prov_srv_param_t
@ CONTROL_TASK_MSG_CODE_PROV_PLAT
Definition meshx_control_task.h:71

◆ meshx_prov_srv_reg_el_client_cb()

meshx_err_t meshx_prov_srv_reg_el_client_cb ( prov_srv_cb_t cb)

Register a callback function for provisioning events.

This function registers a callback function that will be called when certain provisioning events occur.

Parameters
[in]cbThe callback function to register.
Returns
  • MESHX_SUCCESS: Success
  • MESHX_FAIL: Failed to register the callback
284{
289 );
290}
#define MESHX_PROV_SRV_CLIENT_EVENT_BMAP
Definition meshx_prov_srv.cpp:29

◆ meshx_prov_srv_reg_el_server_cb()

meshx_err_t meshx_prov_srv_reg_el_server_cb ( prov_srv_cb_t cb)

Register a callback function for provisioning events.

This function registers a callback function that will be called when certain provisioning events occur.

Parameters
[in]cbThe callback function to register.
Returns
  • MESHX_SUCCESS: Success
  • MESHX_FAIL: Failed to register the callback
293{
298 );
299}
#define MESHX_PROV_SRV_SERVER_EVENT_BMAP
Definition meshx_prov_srv.cpp:30

◆ meshx_prov_srv_reg_from_ble_cb()

meshx_err_t meshx_prov_srv_reg_from_ble_cb ( void )
242{
248 );
249}
static meshx_err_t meshx_prov_srv_control_task_handler(dev_struct_t *pdev, control_task_msg_evt_t evt, meshx_prov_srv_param_t *params)
Control task handler for the provisioning server.
Definition meshx_prov_srv.cpp:84

Variable Documentation

◆ g_boot_timer

meshx_os_timer_t* g_boot_timer
static

◆ prov_cb_evt_ctrl_task_evt_table

prov_cb_evt_ctrl_task_evt_table_t prov_cb_evt_ctrl_task_evt_table[MESHX_PROV_EVT_MAX] = {}
static

Table mapping provisioning events to control task events.

Note
Fixed for C++17 compatibility (removed C99 designated initializers for large arrays if needed, but here we use explicit indices for clarity).
48{};