Select one of the symbols to view example projects that use it.
 
Outline
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_ble_mesh_common_api.h"
#include "esp_ble_mesh_provisioning_api.h"
#include "esp_ble_mesh_networking_api.h"
#include "esp_ble_mesh_config_model_api.h"
#include "esp_ble_mesh_generic_model_api.h"
#include "board.h"
#include "ble_mesh_example_init.h"
#include "ble_mesh_example_nvs.h"
#define TAG
#define CID_ESP
dev_uuid
store
NVS_HANDLE
NVS_KEY
onoff_client
config_server
net_buf_data_bt_mesh_pub_msg_onoff_cli_pub
bt_mesh_pub_msg_onoff_cli_pub
onoff_cli_pub
root_models
elements
composition
provision
mesh_example_info_store()
mesh_example_info_restore()
prov_complete(uint16_t, uint16_t, uint8_t, uint32_t)
example_ble_mesh_provisioning_cb(esp_ble_mesh_prov_cb_event_t, esp_ble_mesh_prov_cb_param_t *)
example_ble_mesh_send_gen_onoff_set()
example_ble_mesh_generic_client_cb(esp_ble_mesh_generic_client_cb_event_t, esp_ble_mesh_generic_client_cb_param_t *)
example_ble_mesh_config_server_cb(esp_ble_mesh_cfg_server_cb_event_t, esp_ble_mesh_cfg_server_cb_param_t *)
ble_mesh_init()
app_main()
Files
loading...
SourceVuESP-IDF Framework and Examplesonoff_client samplemain/main.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* main.c - Application main entry point */ /* * SPDX-FileCopyrightText: 2017 Intel Corporation * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 *//* ... */ #include <stdio.h> #include <string.h> #include <inttypes.h> #include "esp_log.h" #include "nvs_flash.h" #include "esp_ble_mesh_common_api.h" #include "esp_ble_mesh_provisioning_api.h" #include "esp_ble_mesh_networking_api.h" #include "esp_ble_mesh_config_model_api.h" #include "esp_ble_mesh_generic_model_api.h" #include "board.h" #include "ble_mesh_example_init.h" #include "ble_mesh_example_nvs.h"13 includes #define TAG "EXAMPLE" #define CID_ESP 0x02E5 static uint8_t dev_uuid[16] = { 0xdd, 0xdd }; static struct example_info_store { uint16_t net_idx; /* NetKey Index */ uint16_t app_idx; /* AppKey Index */ uint8_t onoff; /* Remote OnOff */ uint8_t tid; /* Message TID */ }{ ... } __attribute__((packed)) store = { .net_idx = ESP_BLE_MESH_KEY_UNUSED, .app_idx = ESP_BLE_MESH_KEY_UNUSED, .onoff = LED_OFF, .tid = 0x0, }{...}; static nvs_handle_t NVS_HANDLE; static const char * NVS_KEY = "onoff_client"; static esp_ble_mesh_client_t onoff_client; static esp_ble_mesh_cfg_srv_t config_server = { /* 3 transmissions with 20ms interval */ .net_transmit = ESP_BLE_MESH_TRANSMIT(2, 20), .relay = ESP_BLE_MESH_RELAY_DISABLED, .relay_retransmit = ESP_BLE_MESH_TRANSMIT(2, 20), .beacon = ESP_BLE_MESH_BEACON_ENABLED, #if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER) .gatt_proxy = ESP_BLE_MESH_GATT_PROXY_ENABLED, #else .gatt_proxy = ESP_BLE_MESH_GATT_PROXY_NOT_SUPPORTED, #endif #if defined(CONFIG_BLE_MESH_FRIEND) .friend_state = ESP_BLE_MESH_FRIEND_ENABLED, #else .friend_state = ESP_BLE_MESH_FRIEND_NOT_SUPPORTED, #endif .default_ttl = 7, }{...}; ESP_BLE_MESH_MODEL_PUB_DEFINE(onoff_cli_pub, 2 + 1, ROLE_NODE); static esp_ble_mesh_model_t root_models[] = { ESP_BLE_MESH_MODEL_CFG_SRV(&config_server), ESP_BLE_MESH_MODEL_GEN_ONOFF_CLI(&onoff_cli_pub, &onoff_client), }{...}; static esp_ble_mesh_elem_t elements[] = { ESP_BLE_MESH_ELEMENT(0, root_models, ESP_BLE_MESH_MODEL_NONE), }{...}; static esp_ble_mesh_comp_t composition = { .cid = CID_ESP, .element_count = ARRAY_SIZE(elements), .elements = elements, }{...}; /* Disable OOB security for SILabs Android app */ static esp_ble_mesh_prov_t provision = { .uuid = dev_uuid, #if 0 .output_size = 4, .output_actions = ESP_BLE_MESH_DISPLAY_NUMBER, .input_size = 4, .input_actions = ESP_BLE_MESH_PUSH,/* ... */ #else .output_size = 0, .output_actions = 0,/* ... */ #endif }{...}; static void mesh_example_info_store(void) { ble_mesh_nvs_store(NVS_HANDLE, NVS_KEY, &store, sizeof(store)); }{ ... } static void mesh_example_info_restore(void) { esp_err_t err = ESP_OK; bool exist = false; err = ble_mesh_nvs_restore(NVS_HANDLE, NVS_KEY, &store, sizeof(store), &exist); if (err != ESP_OK) { return; }{...} if (exist) { ESP_LOGI(TAG, "Restore, net_idx 0x%04x, app_idx 0x%04x, onoff %u, tid 0x%02x", store.net_idx, store.app_idx, store.onoff, store.tid); }{...} }{ ... } static void prov_complete(uint16_t net_idx, uint16_t addr, uint8_t flags, uint32_t iv_index) { ESP_LOGI(TAG, "net_idx: 0x%04x, addr: 0x%04x", net_idx, addr); ESP_LOGI(TAG, "flags: 0x%02x, iv_index: 0x%08" PRIx32, flags, iv_index); board_led_operation(LED_G, LED_OFF); store.net_idx = net_idx; /* mesh_example_info_store() shall not be invoked here, because if the device * is restarted and goes into a provisioned state, then the following events * will come: * 1st: ESP_BLE_MESH_NODE_PROV_COMPLETE_EVT * 2nd: ESP_BLE_MESH_PROV_REGISTER_COMP_EVT * So the store.net_idx will be updated here, and if we store the mesh example * info here, the wrong app_idx (initialized with 0xFFFF) will be stored in nvs * just before restoring it. *//* ... */ }{ ... } static void example_ble_mesh_provisioning_cb(esp_ble_mesh_prov_cb_event_t event, esp_ble_mesh_prov_cb_param_t *param) { switch (event) { case ESP_BLE_MESH_PROV_REGISTER_COMP_EVT: ESP_LOGI(TAG, "ESP_BLE_MESH_PROV_REGISTER_COMP_EVT, err_code %d", param->prov_register_comp.err_code); mesh_example_info_restore(); /* Restore proper mesh example info */ break;... case ESP_BLE_MESH_NODE_PROV_ENABLE_COMP_EVT: ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_ENABLE_COMP_EVT, err_code %d", param->node_prov_enable_comp.err_code); break;... case ESP_BLE_MESH_NODE_PROV_LINK_OPEN_EVT: ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_LINK_OPEN_EVT, bearer %s", param->node_prov_link_open.bearer == ESP_BLE_MESH_PROV_ADV ? "PB-ADV" : "PB-GATT"); break;... case ESP_BLE_MESH_NODE_PROV_LINK_CLOSE_EVT: ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_LINK_CLOSE_EVT, bearer %s", param->node_prov_link_close.bearer == ESP_BLE_MESH_PROV_ADV ? "PB-ADV" : "PB-GATT"); break;... case ESP_BLE_MESH_NODE_PROV_COMPLETE_EVT: ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_PROV_COMPLETE_EVT"); prov_complete(param->node_prov_complete.net_idx, param->node_prov_complete.addr, param->node_prov_complete.flags, param->node_prov_complete.iv_index); break;... case ESP_BLE_MESH_NODE_PROV_RESET_EVT: break;... case ESP_BLE_MESH_NODE_SET_UNPROV_DEV_NAME_COMP_EVT: ESP_LOGI(TAG, "ESP_BLE_MESH_NODE_SET_UNPROV_DEV_NAME_COMP_EVT, err_code %d", param->node_set_unprov_dev_name_comp.err_code); break;... default: break;... }{...} }{ ... } void example_ble_mesh_send_gen_onoff_set(void) { esp_ble_mesh_generic_client_set_state_t set = {0}; esp_ble_mesh_client_common_param_t common = {0}; esp_err_t err = ESP_OK; common.opcode = ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET_UNACK; common.model = onoff_client.model; common.ctx.net_idx = store.net_idx; common.ctx.app_idx = store.app_idx; common.ctx.addr = 0xFFFF; /* to all nodes */ common.ctx.send_ttl = 3; common.msg_timeout = 0; /* 0 indicates that timeout value from menuconfig will be used */ #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 2, 0) common.msg_role = ROLE_NODE; #endif set.onoff_set.op_en = false; set.onoff_set.onoff = store.onoff; set.onoff_set.tid = store.tid++; err = esp_ble_mesh_generic_client_set_state(&common, &set); if (err) { ESP_LOGE(TAG, "Send Generic OnOff Set Unack failed"); return; }{...} store.onoff = !store.onoff; mesh_example_info_store(); /* Store proper mesh example info */ }{ ... } static void example_ble_mesh_generic_client_cb(esp_ble_mesh_generic_client_cb_event_t event, esp_ble_mesh_generic_client_cb_param_t *param) { ESP_LOGI(TAG, "Generic client, event %u, error code %d, opcode is 0x%04" PRIx32, event, param->error_code, param->params->opcode); switch (event) { case ESP_BLE_MESH_GENERIC_CLIENT_GET_STATE_EVT: ESP_LOGI(TAG, "ESP_BLE_MESH_GENERIC_CLIENT_GET_STATE_EVT"); if (param->params->opcode == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET) { ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_GET, onoff %d", param->status_cb.onoff_status.present_onoff); }{...} break;... case ESP_BLE_MESH_GENERIC_CLIENT_SET_STATE_EVT: ESP_LOGI(TAG, "ESP_BLE_MESH_GENERIC_CLIENT_SET_STATE_EVT"); if (param->params->opcode == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET) { ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET, onoff %d", param->status_cb.onoff_status.present_onoff); }{...} break;... case ESP_BLE_MESH_GENERIC_CLIENT_PUBLISH_EVT: ESP_LOGI(TAG, "ESP_BLE_MESH_GENERIC_CLIENT_PUBLISH_EVT"); break;... case ESP_BLE_MESH_GENERIC_CLIENT_TIMEOUT_EVT: ESP_LOGI(TAG, "ESP_BLE_MESH_GENERIC_CLIENT_TIMEOUT_EVT"); if (param->params->opcode == ESP_BLE_MESH_MODEL_OP_GEN_ONOFF_SET) { /* If failed to get the response of Generic OnOff Set, resend Generic OnOff Set */ example_ble_mesh_send_gen_onoff_set(); }{...} break;... default: break;... }{...} }{ ... } static void example_ble_mesh_config_server_cb(esp_ble_mesh_cfg_server_cb_event_t event, esp_ble_mesh_cfg_server_cb_param_t *param) { if (event == ESP_BLE_MESH_CFG_SERVER_STATE_CHANGE_EVT) { switch (param->ctx.recv_op) { case ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD: ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_APP_KEY_ADD"); ESP_LOGI(TAG, "net_idx 0x%04x, app_idx 0x%04x", param->value.state_change.appkey_add.net_idx, param->value.state_change.appkey_add.app_idx); ESP_LOG_BUFFER_HEX("AppKey", param->value.state_change.appkey_add.app_key, 16); break;... case ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND: ESP_LOGI(TAG, "ESP_BLE_MESH_MODEL_OP_MODEL_APP_BIND"); ESP_LOGI(TAG, "elem_addr 0x%04x, app_idx 0x%04x, cid 0x%04x, mod_id 0x%04x", param->value.state_change.mod_app_bind.element_addr, param->value.state_change.mod_app_bind.app_idx, param->value.state_change.mod_app_bind.company_id, param->value.state_change.mod_app_bind.model_id); if (param->value.state_change.mod_app_bind.company_id == 0xFFFF && param->value.state_change.mod_app_bind.model_id == ESP_BLE_MESH_MODEL_ID_GEN_ONOFF_CLI) { store.app_idx = param->value.state_change.mod_app_bind.app_idx; mesh_example_info_store(); /* Store proper mesh example info */ }{...} break;... default: break;... }{...} }{...} }{ ... } static esp_err_t ble_mesh_init(void) { esp_err_t err = ESP_OK; esp_ble_mesh_register_prov_callback(example_ble_mesh_provisioning_cb); esp_ble_mesh_register_generic_client_callback(example_ble_mesh_generic_client_cb); esp_ble_mesh_register_config_server_callback(example_ble_mesh_config_server_cb); err = esp_ble_mesh_init(&provision, &composition); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to initialize mesh stack (err %d)", err); return err; }{...} err = esp_ble_mesh_node_prov_enable((esp_ble_mesh_prov_bearer_t)(ESP_BLE_MESH_PROV_ADV | ESP_BLE_MESH_PROV_GATT)); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to enable mesh node (err %d)", err); return err; }{...} ESP_LOGI(TAG, "BLE Mesh Node initialized"); board_led_operation(LED_G, LED_ON); return err; }{ ... } void app_main(void) { esp_err_t err; ESP_LOGI(TAG, "Initializing..."); board_init(); err = nvs_flash_init(); if (err == ESP_ERR_NVS_NO_FREE_PAGES) { ESP_ERROR_CHECK(nvs_flash_erase()); err = nvs_flash_init(); }{...} ESP_ERROR_CHECK(err); err = bluetooth_init(); if (err) { ESP_LOGE(TAG, "esp32_bluetooth_init failed (err %d)", err); return; }{...} /* Open nvs namespace for storing/restoring mesh example info */ err = ble_mesh_nvs_open(&NVS_HANDLE); if (err) { return; }{...} ble_mesh_get_dev_uuid(dev_uuid); /* Initialize the Bluetooth Mesh Subsystem */ err = ble_mesh_init(); if (err) { ESP_LOGE(TAG, "Bluetooth mesh init failed (err %d)", err); }{...} }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.