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

This file contains platform-specific implementations for BLE Mesh functionality on the ESP32 using the MeshX framework. It provides APIs for managing BLE Mesh models, compositions, provisioning, and initialization. More...

Include dependency graph for esp_platform_ble_mesh.c:

Go to the source code of this file.

Functions

meshx_err_t meshx_is_group_subscribed (meshx_model_t *p_model, uint16_t addr)
 Checks if a model is subscribed to a specific group address.
 
meshx_err_t meshx_plat_create_model_pub (void **p_pub, uint16_t nmax)
 Creates and initializes model and publication structures.
 
meshx_err_t meshx_plat_del_model_pub (void **p_pub)
 Deletes the model and publication objects.
 
meshx_err_t meshx_plat_client_create (meshx_ptr_t p_model, meshx_ptr_t *p_pub, meshx_ptr_t *p_cli)
 Creates and initializes a generic client model for BLE Mesh.
 
meshx_err_t meshx_get_model_id (meshx_ptr_t p_model, uint16_t *model_id)
 Retrieve the model ID of a generic server model.
 
meshx_err_t meshx_create_plat_composition (meshx_ptr_t *p_comp)
 Creates a platform-specific BLE Mesh composition object.
 
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_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.
 
meshx_err_t meshx_get_base_element_id (uint16_t *base_el_id)
 Retrieves the base element ID for the BLE Mesh platform.
 
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.
 

Detailed Description

This file contains platform-specific implementations for BLE Mesh functionality on the ESP32 using the MeshX framework. It provides APIs for managing BLE Mesh models, compositions, provisioning, and initialization.

Copyright (c) 2024 - 2025 MeshX

The functions in this file handle tasks such as checking group subscriptions, creating and deleting model publications, managing model IDs, initializing compositions, and setting up BLE Mesh provisioning and node configurations.

Author
Pranjal Chanda

Definition in file esp_platform_ble_mesh.c.

Function Documentation

◆ meshx_create_plat_composition()

meshx_err_t meshx_create_plat_composition ( meshx_ptr_t * p_comp)

Creates a platform-specific BLE Mesh composition object.

This function allocates memory for a MESHX_COMPOSITION object and assigns its pointer to the provided pointer argument. It checks for invalid arguments and memory allocation failures, returning appropriate error codes.

Parameters
[out]p_compPointer to the location where the composition object pointer will be stored.
Returns
MESHX_SUCCESS on successful creation, MESHX_INVALID_ARG if the provided pointer is NULL, or MESHX_NO_MEM if memory allocation fails.

Definition at line 99 of file esp_platform_ble_mesh.c.

100{
101 if(!p_comp)
102 return MESHX_INVALID_ARG;
103
104 *p_comp = (MESHX_COMPOSITION *) MESHX_MALLOC (sizeof(MESHX_COMPOSITION));
105 if(!*p_comp)
106 return MESHX_NO_MEM;
107
108 return MESHX_SUCCESS;
109}
@ MESHX_SUCCESS
Definition meshx_err.h:40
@ MESHX_INVALID_ARG
Definition meshx_err.h:42
@ MESHX_NO_MEM
Definition meshx_err.h:44
#define MESHX_MALLOC
Definition meshx_err.h:24
#define MESHX_COMPOSITION
Here is the caller graph for this function:

◆ meshx_get_base_element_id()

meshx_err_t meshx_get_base_element_id ( uint16_t * base_el_id)

Retrieves the base element ID for the BLE Mesh platform.

This function fetches the base element ID, which is used as a reference point for other elements in the BLE Mesh composition.

Parameters
[out]base_el_idPointer to a variable where the base element ID will be stored.
Returns
meshx_err_t Returns MESHX_SUCCESS on success, or an appropriate error code on failure.

Definition at line 153 of file esp_platform_ble_mesh.c.

154{
155 if (!base_el_id)
156 {
157 return MESHX_INVALID_ARG;
158 }
159
160 *base_el_id = esp_ble_mesh_get_primary_element_address();
161
162 return MESHX_SUCCESS;
163}
Here is the caller graph for this function:

◆ meshx_get_model_id()

meshx_err_t meshx_get_model_id ( meshx_ptr_t p_model,
uint16_t * model_id )

Retrieve the model ID of a generic server model.

This function obtains the model ID associated with a specified generic server model.

Parameters
[in]p_modelPointer to the model whose ID is to be retrieved.
[out]model_idPointer to a variable where the retrieved model ID will be stored.
Returns
MESHX_SUCCESS on success, or an appropriate error code on failure.

Definition at line 88 of file esp_platform_ble_mesh.c.

89{
90 if(!p_model)
91 return MESHX_INVALID_ARG;
92
93 MESHX_MODEL * model = (MESHX_MODEL *)p_model;
94 *model_id = model->model_id;
95
96 return MESHX_SUCCESS;
97}
#define MESHX_MODEL
Here is the caller graph for this function:

◆ meshx_is_group_subscribed()

meshx_err_t meshx_is_group_subscribed ( meshx_model_t * p_model,
uint16_t addr )

Checks if a model is subscribed to a specific group address.

This function determines whether the specified BLE Mesh model is subscribed to a given group address by utilizing the internal function esp_ble_mesh_is_model_subscribed_to_group.

Parameters
[in]p_modelPointer to the BLE Mesh model structure.
[in]addrThe group address to check for subscription.
Returns
MESHX_SUCCESS if the model is subscribed to the group address, otherwise MESHX_FAIL.

Definition at line 23 of file esp_platform_ble_mesh.c.

24{
25 const uint16_t * res = esp_ble_mesh_is_model_subscribed_to_group(p_model->p_model, addr);
26 if(res != NULL)
27 return MESHX_SUCCESS;
28
29 return MESHX_FAIL;
30}
@ MESHX_FAIL
Definition meshx_err.h:41
meshx_ptr_t p_model
Here is the caller graph for this function:

◆ meshx_plat_add_element_to_composition()

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.

This function adds a new element to the BLE Mesh composition at the specified index. It assigns the provided SIG and vendor models to the element and sets their respective counts.

Parameters
[in]indexIndex at which the element is to be added.
[in,out]p_element_listPointer to the list of elements.
[in]p_sig_modelsPointer to the SIG models to be assigned to the element.
[in]p_ven_modelsPointer to the vendor models to be assigned to the element.
[in]sig_cntNumber of SIG models.
[in]ven_cntNumber of vendor models.
Returns
MESHX_SUCCESS on success, or MESHX_INVALID_ARG if the element list pointer is NULL.

Definition at line 111 of file esp_platform_ble_mesh.c.

118 {
119 if (!p_element_list) {
120 return MESHX_INVALID_ARG;
121 }
122
123 MESHX_ELEMENT* element = (MESHX_ELEMENT*)(p_element_list) + index;
124 element->sig_models = p_sig_models;
125 element->vnd_models = p_ven_models;
126
127 memcpy((meshx_ptr_t)&(element->sig_model_count), &sig_cnt, sizeof(uint8_t));
128 memcpy((meshx_ptr_t)&(element->vnd_model_count), &ven_cnt, sizeof(uint8_t));
129
130 return MESHX_SUCCESS;
131}
void * meshx_ptr_t
#define MESHX_ELEMENT
Here is the caller graph for this function:

◆ meshx_plat_ble_mesh_init()

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.

This function sets up the BLE Mesh stack and initializes it with the provided provisioning parameters.

Parameters
[in]prov_cfgPointer to the provisioning parameters structure.
[in]compPointer to the composition data.
Returns
meshx_err_t Returns MESHX_OK on success, or an appropriate error code.

Definition at line 193 of file esp_platform_ble_mesh.c.

194{
195 if(comp == NULL || prov_cfg == NULL)
196 return MESHX_INVALID_ARG;
197
199
200 /* Initialize BLE Mesh Provisioner */
201 MESHX_PROV *p_prov = NULL;
202
203 p_prov = (MESHX_PROV *)meshx_plat_get_prov();
204 if(p_prov == NULL)
205 {
206 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Failed to get provisioning instance");
207 return MESHX_ERR_PLAT;
208 }
209 err = esp_ble_mesh_init(p_prov, comp);
210 if(err)
211 {
212 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Failed to initialize mesh stack");
213 return err;
214 }
215 err = esp_ble_mesh_set_unprovisioned_device_name((char*)prov_cfg->node_name);
216 if(err)
217 {
218 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Failed to set device name");
219 return err;
220 }
221 err = esp_ble_mesh_node_prov_enable((esp_ble_mesh_prov_bearer_t)(ESP_BLE_MESH_PROV_ADV | ESP_BLE_MESH_PROV_GATT));
222 if(err)
223 {
224 MESHX_LOGE(MODULE_ID_MODEL_SERVER, "Failed to enable mesh node");
225 return err;
226 }
227 MESHX_LOGD(MODULE_ID_MODEL_SERVER, "BLE Mesh Node initialized");
228 return MESHX_SUCCESS;
229}
meshx_ptr_t meshx_plat_get_prov(void)
Get the provisioning parameters.
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:39
@ MESHX_ERR_PLAT
Definition meshx_err.h:43
#define MESHX_LOGE(module_id, format,...)
Definition meshx_log.h:73
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:113
#define MESHX_PROV
@ 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_plat_client_create()

meshx_err_t meshx_plat_client_create ( meshx_ptr_t p_model,
meshx_ptr_t * p_pub,
meshx_ptr_t * p_cli )

Creates and initializes a generic client model for BLE Mesh.

This function sets up the necessary structures and resources for a generic client model in the BLE Mesh stack. It initializes the model, publication context, and the on/off client instance.

Parameters
[in]p_modelPointer to the model structure to be initialized.
[out]p_pubPointer to a location where the address of the publication context will be stored.
[out]p_cliPointer to a location where the address of the on/off client instance will be stored.
Returns
meshx_err_t Returns an error code indicating the result of the operation. Typically, MESHX_OK on success or an appropriate error code on failure.

Definition at line 56 of file esp_platform_ble_mesh.c.

57{
58 if (!p_model || !p_pub || !p_cli)
59 {
60 return MESHX_INVALID_ARG; // Invalid arguments
61 }
63
64 // Create the publication context for the model
65 err = meshx_plat_create_model_pub(p_pub, 1);
66 if (err)
67 {
68 return meshx_plat_del_model_pub(p_pub); // Clean up on error
69 }
70
71 // Allocate memory for the generic client model
72 *p_cli = (MESHX_CLI *)MESHX_CALOC(1, sizeof(MESHX_CLI));
73 if (!*p_cli)
74 {
75 return MESHX_NO_MEM; // Memory allocation failed
76 }
77
78 // Initialize the generic client model
79 ((MESHX_MODEL *)p_model)->user_data = *p_cli;
80
81 meshx_ptr_t*temp = (meshx_ptr_t*)&((MESHX_MODEL *)p_model)->pub;
82
83 *temp = *p_pub;
84
85 return MESHX_SUCCESS; // Successfully created the model and publication context
86}
meshx_err_t meshx_plat_create_model_pub(void **p_pub, uint16_t nmax)
Creates and initializes model and publication structures.
meshx_err_t meshx_plat_del_model_pub(void **p_pub)
Deletes the model and publication objects.
#define MESHX_CALOC
Definition meshx_err.h:28
#define MESHX_CLI
Here is the call graph for this function:
Here is the caller graph for this function:

◆ meshx_plat_composition_init()

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.

This function sets up a BLE Mesh composition object with the specified company ID, product ID, and element index. It assigns the provided elements to the composition.

Parameters
[out]p_compositionPointer to the composition object to be initialized.
[in]p_elementsPointer to the elements to be included in the composition.
[in]cidCompany ID for the composition.
[in]pidProduct ID for the composition.
[in]element_idxIndex of the element within the composition.
Returns
MESHX_SUCCESS on successful initialization, or an appropriate error code on failure.

Definition at line 133 of file esp_platform_ble_mesh.c.

140{
141 if(!p_composition)
142 return MESHX_INVALID_ARG;
143
144 MESHX_COMPOSITION * composition = (MESHX_COMPOSITION *) p_composition;
145 composition->cid = cid;
146 composition->pid = pid;
147 composition->element_count = element_idx;
148 composition->elements = p_elements;
149
150 return MESHX_SUCCESS;
151}
Here is the caller graph for this function:

◆ meshx_plat_create_model_pub()

meshx_err_t meshx_plat_create_model_pub ( meshx_ptr_t * p_pub,
uint16_t nmax )

Creates and initializes model and publication structures.

This function allocates memory for model and publication structures based on the specified maximum number of elements. It initializes the provided pointers to point to the newly allocated memory.

Parameters
[out]p_pubPointer to the publication structure to be created.
[in]nmaxMaximum number of elements for the model and publication.
Returns
MESHX_SUCCESS on successful allocation and initialization, MESHX_INVALID_ARG if any input pointer is NULL, MESHX_NO_MEM if memory allocation fails.

Definition at line 31 of file esp_platform_ble_mesh.c.

32{
33 if(!p_pub)
34 return MESHX_INVALID_ARG;
35
36 *p_pub = (MESHX_MODEL_PUB *) MESHX_CALOC(nmax, sizeof(MESHX_MODEL_PUB));
37 if(!*p_pub)
38 return MESHX_NO_MEM;
39
40 return MESHX_SUCCESS;
41}
#define MESHX_MODEL_PUB
Here is the caller graph for this function:

◆ meshx_plat_del_model_pub()

meshx_err_t meshx_plat_del_model_pub ( meshx_ptr_t * p_pub)

Deletes the model and publication objects.

This function frees the memory allocated for the model and publication objects pointed to by the provided pointers and sets them to NULL.

Parameters
[in,out]p_pubPointer to the publication object to be deleted.
Returns
MESHX_SUCCESS on successful deletion, MESHX_INVALID_ARG if either pointer is NULL.

Definition at line 43 of file esp_platform_ble_mesh.c.

44{
45 if (!p_pub)
46 return MESHX_INVALID_ARG;
47
48 if (*p_pub) {
49 MESHX_FREE(*p_pub);
50 *p_pub = NULL;
51 }
52
53 return MESHX_SUCCESS;
54}
#define MESHX_FREE
Definition meshx_err.h:32
Here is the caller graph for this function:

◆ meshx_platform_bt_init()

meshx_err_t meshx_platform_bt_init ( meshx_uuid_addr_t uuid)

Initializes the Bluetooth subsystem of the MeshX platform.

This function sets up the Bluetooth-related components necessary for MeshX operation, such as BLE Mesh provisioning and communication.

Parameters
[in]uuidPointer to the UUID address to be used for the Bluetooth initialization.
Returns
meshx_err_t Returns MESHX_OK on success, or an appropriate error code.

Definition at line 165 of file esp_platform_ble_mesh.c.

166{
167 esp_err_t err = ESP_OK;
168 /* Initialize Bluetooth */
169 err = bluetooth_init();
170 if(err)
171 {
172 return MESHX_ERR_PLAT;
173 }
174
175 if(uuid == NULL)
176 {
177 MESHX_LOGE(MODULE_ID_COMMON, "Invalid configuration for Bluetooth initialization");
178 return MESHX_INVALID_ARG;
179 }
180 /* Set the device UUID */
181 if(memcmp(uuid, MESHX_UUID_EMPTY, sizeof(meshx_uuid_addr_t)) == 0)
182 {
183 const uint8_t *mac_addr = esp_bt_dev_get_address();
184 if (mac_addr == NULL) {
185 MESHX_LOGE(MODULE_ID_COMMON, "Failed to get device address");
186 return MESHX_ERR_PLAT;
187 }
188 memcpy(uuid + 2, mac_addr, MESHX_BD_ADDR_LEN);
189 }
190 return MESHX_SUCCESS;
191}
esp_err_t bluetooth_init(void)
Initialize the Bluetooth stack for BLE Mesh.
#define MESHX_UUID_EMPTY
uint8_t meshx_uuid_addr_t[MESHX_UUID_ADDR_LEN]
#define MESHX_BD_ADDR_LEN
@ MODULE_ID_COMMON
Definition module_id.h:32
Here is the call graph for this function:
Here is the caller graph for this function: