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

This file contains the implementation of the provisioning process for the BLE mesh node. More...

#include "string.h"
#include "meshx_nvs.h"
#include "meshx_os_timer.h"
#include "meshx_control_task.h"
#include "meshx_prov_srv.h"
Include dependency graph for meshx_prov_srv.c:

Go to the source code of this file.

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_EN_NODE_PROV
 

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 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.
 
static meshx_err_t meshx_prov_srv_meshx_reg_cb (void)
 Register the BLE Mesh provisioning callback for MeshX Layer Internal.
 
static void meshx_init_freshboot_timer_trigger_cb (const meshx_os_timer_t *p_timer)
 Callback function for the boot timer.
 
static meshx_err_t meshx_init_freshboot_timer (dev_struct_t *p_dev, uint16_t timeout_ms)
 Initializes the fresh boot timer.
 
static meshx_err_t meshx_prov_srv_reg_from_ble_cb (void)
 Register the BLE Mesh provisioning callback.
 
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.
 

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.

Copyright © 2024 - 2025 MeshX

The provisioning process is responsible for setting up the BLE mesh node, including initializing necessary components and handling communication with other nodes.

Author
Pranjal Chanda

Definition in file meshx_prov_srv.c.

Macro Definition Documentation

◆ CONTROL_TASK_PROV_EVT_MASK

#define CONTROL_TASK_PROV_EVT_MASK
Value:

Mask for control task provisioning events.

This macro defines a mask that combines multiple control task message events related to provisioning.

Definition at line 27 of file meshx_prov_srv.c.

27#define CONTROL_TASK_PROV_EVT_MASK CONTROL_TASK_MSG_EVT_IDENTIFY_START \
28 | CONTROL_TASK_MSG_EVT_PROVISION_STOP \
29 | CONTROL_TASK_MSG_EVT_IDENTIFY_STOP \
30 | CONTROL_TASK_MSG_EVT_NODE_RESET

◆ MESHX_PROV_SRV_CLIENT_EVENT_BMAP

#define MESHX_PROV_SRV_CLIENT_EVENT_BMAP   CONTROL_TASK_MSG_EVT_SYSTEM_FRESH_BOOT

Definition at line 32 of file meshx_prov_srv.c.

◆ MESHX_PROV_SRV_SERVER_EVENT_BMAP

#define MESHX_PROV_SRV_SERVER_EVENT_BMAP   CONTROL_TASK_MSG_EVT_EN_NODE_PROV

Definition at line 33 of file meshx_prov_srv.c.

Typedef Documentation

◆ prov_cb_evt_ctrl_task_evt_table_t

Structure to map provisioning callback events to control task events.

This structure contains a string representation of the provisioning event and the corresponding control task event.

Function Documentation

◆ meshx_handle_node_reset()

static void meshx_handle_node_reset ( dev_struct_t * pdev)
static

Handle node reset event.

This function handles the node reset event by removing all element contexts from NVS and resetting the MCU.

Parameters
[in]pdevPointer to the device structure.

Definition at line 148 of file meshx_prov_srv.c.

149{
150 if(!pdev)
151 {
152 MESHX_LOGE(MODULE_ID_COMMON, "Invalid device structure");
153 return;
154 }
155
156 for(uint16_t i = 1; i < pdev->element_idx; i++)
157 {
159 if(err != MESHX_SUCCESS)
160 {
161 MESHX_LOGE(MODULE_ID_COMMON, "Failed to erase element context (%d): (%d)", i, err);
162 }
163 }
164 /* Reset the MCU */
166}
void meshx_platform_reset(void)
Resets the MeshX platform. This function performs a system reset, restarting the platform.
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:39
@ MESHX_SUCCESS
Definition meshx_err.h:40
#define MESHX_LOGE(module_id, format,...)
Definition meshx_log.h:73
meshx_err_t meshx_nvs_element_ctx_remove(uint16_t element_id)
Remove the context of a specific element from NVS.
Definition meshx_nvs.c:462
@ MODULE_ID_COMMON
Definition module_id.h:32
size_t element_idx
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_init_freshboot_timer()

static meshx_err_t meshx_init_freshboot_timer ( dev_struct_t * p_dev,
uint16_t timeout_ms )
static

Initializes the fresh boot timer.

Parameters
[in]p_devPointer to the device structure.
[in]timeout_msTimeout duration in milliseconds.
Returns
MESHX_SUCCESS on success, error code otherwise.

Definition at line 254 of file meshx_prov_srv.c.

255{
257 {
258 MESHX_LOGI(MODULE_ID_COMMON, "Device not provisioned, not starting boot timer");
259 return MESHX_SUCCESS;
260 }
261 meshx_err_t err = meshx_os_timer_create("boot_timer",
262 timeout_ms,
263 false,
266 );
267 if(err != MESHX_SUCCESS)
268 {
269 MESHX_LOGE(MODULE_ID_COMMON, "Failed to create boot timer: (%d)", err);
270 }
271
273 if(err != MESHX_SUCCESS)
274 {
275 MESHX_LOGE(MODULE_ID_COMMON, "Failed to start boot timer: (%d)", err);
276 }
277
278 return err;
279}
#define MESHX_ADDR_UNASSIGNED
#define MESHX_LOGI(module_id, format,...)
Definition meshx_log.h:100
meshx_err_t meshx_os_timer_start(const meshx_os_timer_t *timer_handle)
Start a timer.
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.
static meshx_os_timer_t * g_boot_timer
static void meshx_init_freshboot_timer_trigger_cb(const meshx_os_timer_t *p_timer)
Callback function for the boot timer.
meshx_app_store_t meshx_store
uint16_t node_addr
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_init_freshboot_timer_trigger_cb()

static void meshx_init_freshboot_timer_trigger_cb ( const meshx_os_timer_t * p_timer)
static

Callback function for the boot timer.

This function is called when the boot timer expires.

Parameters
[in]p_timerPointer to the timer structure.

Definition at line 230 of file meshx_prov_srv.c.

231{
232 MESHX_LOGI(MODULE_ID_COMMON, "Fresh Boot Timer Expired");
233
237 NULL,
238 0
239 );
240 if(err)
241 {
242 MESHX_LOGE(MODULE_ID_COMMON, "Failed to publish fresh boot event: (%d)", err);
243 }
244}
CONTROL_TASK_MSG_EVT_SYSTEM_FRESH_BOOT
@ CONTROL_TASK_MSG_CODE_PROVISION
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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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

Definition at line 313 of file meshx_prov_srv.c.

314{
315 if (!prov_cfg || memcmp(prov_cfg->uuid, MESHX_UUID_EMPTY, sizeof(meshx_uuid_addr_t)) == 0)
316 {
317 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Invalid server configuration");
318 return MESHX_INVALID_ARG;
319 }
320
322 if (err != MESHX_SUCCESS)
323 {
324 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Failed to register provisioning callback");
325 return err;
326 }
327
329 if (err != MESHX_SUCCESS)
330 {
331 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Failed to register provisioning callback");
332 return err;
333 }
334
335 err = meshx_init_freshboot_timer(p_dev, prov_cfg->freshboot_timeout_ms);
336 if (err != MESHX_SUCCESS)
337 {
338 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Failed to initialize boot timer");
339 return err;
340 }
341
342 return meshx_plat_init_prov(prov_cfg->uuid);
343}
#define MESHX_UUID_EMPTY
uint8_t meshx_uuid_addr_t[MESHX_UUID_ADDR_LEN]
meshx_err_t meshx_plat_init_prov(const uint8_t *uuid)
Initialize provisioning parameters.
@ MESHX_INVALID_ARG
Definition meshx_err.h:42
static meshx_err_t meshx_init_freshboot_timer(dev_struct_t *p_dev, uint16_t timeout_ms)
Initializes the fresh boot timer.
static meshx_err_t meshx_prov_srv_meshx_reg_cb(void)
Register the BLE Mesh provisioning callback for MeshX Layer Internal.
static meshx_err_t meshx_prov_srv_reg_from_ble_cb(void)
Register the BLE Mesh provisioning callback.
@ MODULE_ID_MODEL_SERVER
Definition module_id.h:30
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_prov_control_task_handler()

static 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.

This function processes various provisioning-related events and updates the device structure accordingly. It handles events such as provisioning completion and identification start.

Parameters
[in]pdevPointer to the device structure.
[in]evtThe control task message event type.
[in]paramsPointer to the event parameters.
Returns
MESHX_SUCCESS on success, or an error code on failure.

Definition at line 181 of file meshx_prov_srv.c.

182{
183 const meshx_prov_cb_param_t *param = (meshx_prov_cb_param_t*) params;
184
185 switch (evt)
186 {
191 break;
193 MESHX_LOGI(MODULE_ID_COMMON, "Identify Start");
194 break;
196 MESHX_LOGW(MODULE_ID_COMMON, "Node Reset Event");
198 break;
199 default:
200 break;
201 }
202 return MESHX_SUCCESS;
203}
#define MESHX_NVS_STORE
#define MESHX_LOGW(module_id, format,...)
Definition meshx_log.h:87
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:366
#define MESHX_NVS_AUTO_COMMIT
Definition meshx_nvs.h:25
static void meshx_handle_node_reset(dev_struct_t *pdev)
Handle node reset event.
uint16_t net_key_id
BLE Mesh Node/Provisioner callback parameters union.
struct meshx_prov_cb_param_t::meshx_provision_complete_evt_param node_prov_complete
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_prov_srv_control_task_handler()

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 )
static

Control task handler for the provisioning server.

Parameters
[in]pdevPointer to the device structure.
[in]evtEvent type.
[in]paramsPointer to the provisioning server parameters.
Returns
MESHX_SUCCESS on success, error code otherwise.

Definition at line 95 of file meshx_prov_srv.c.

99{
100 if (!pdev || !params)
101 {
102 return MESHX_INVALID_ARG;
103 }
106 {
107 return MESHX_INVALID_ARG;
108 }
109
111 {
112 MESHX_LOGD(MODULE_ID_MODEL_SERVER, "Provisioning event mapped: %s",
115 }
116 else
117 {
118 MESHX_LOGD(MODULE_ID_MODEL_SERVER, "Unhandled event: %d", params->prov_evt);
119 }
121 {
122 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);
123 MESHX_LOGI(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);
124 }
126 {
127 return MESHX_INVALID_ARG;
128 }
129
130 /* Publish the event */
133 prov_evt,
134 &params->param,
135 sizeof(meshx_prov_cb_param_t));
136 return MESHX_SUCCESS;
137}
@ MESHX_NODE_PROV_COMPLETE_EVT
control_task_msg_evt_provision_t prov_evt_t
CONTROL_TASK_MSG_EVT_PROVISION_ALL
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:113
Structure to map provisioning callback events to control task events.
control_task_msg_evt_provision_t ctrl_task_evt
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_prov_srv_meshx_reg_cb()

static meshx_err_t meshx_prov_srv_meshx_reg_cb ( void )
static

Register the BLE Mesh provisioning callback for MeshX Layer Internal.

This function registers the BLE Mesh provisioning callback to handle provisioning events.

Returns
  • MESHX_SUCCESS: Success
  • MESHX_FAIL: Failed to register provisioning callback

Definition at line 215 of file meshx_prov_srv.c.

216{
221}
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.
#define CONTROL_TASK_PROV_EVT_MASK
Mask for control task provisioning events.
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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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

Definition at line 402 of file meshx_prov_srv.c.

403{
404 if (!param)
405 {
406 return MESHX_INVALID_ARG;
407 }
408
412 param,
414 );
415}
struct meshx_ble_mesh_prov_srv_param meshx_prov_srv_param_t
@ CONTROL_TASK_MSG_CODE_FRM_BLE
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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

Definition at line 357 of file meshx_prov_srv.c.

358{
362 (prov_srv_cb_t)cb
363 );
364}
control_task_msg_handle_t prov_srv_cb_t
#define MESHX_PROV_SRV_CLIENT_EVENT_BMAP
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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

Definition at line 378 of file meshx_prov_srv.c.

379{
383 (prov_srv_cb_t)cb
384 );
385}
#define MESHX_PROV_SRV_SERVER_EVENT_BMAP
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_prov_srv_reg_from_ble_cb()

static meshx_err_t meshx_prov_srv_reg_from_ble_cb ( void )
static

Register the BLE Mesh provisioning callback.

This function registers the BLE Mesh provisioning callback to handle provisioning events.

Returns
  • MESHX_SUCCESS: Success
  • MESHX_FAIL: Failed to register provisioning callback

Definition at line 291 of file meshx_prov_srv.c.

292{
297 );
298}
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.
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ g_boot_timer

meshx_os_timer_t* g_boot_timer
static

Definition at line 84 of file meshx_prov_srv.c.

◆ 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
Initial value:

Table mapping provisioning events to control task events.

This table is indexed by provisioning event types and contains the string representation of the event and the corresponding control task event.

Definition at line 73 of file meshx_prov_srv.c.