1 /*
2  * SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdint.h>
8 #include <string.h>
9 
10 #include "esp_err.h"
11 
12 #include "btc_ble_mesh_prov.h"
13 #include "esp_ble_mesh_networking_api.h"
14 
15 #define ESP_BLE_MESH_TX_SDU_MAX ((CONFIG_BLE_MESH_ADV_BUF_COUNT - 3) * 12)
16 
ble_mesh_model_send_msg(esp_ble_mesh_model_t * model,esp_ble_mesh_msg_ctx_t * ctx,uint32_t opcode,btc_ble_mesh_model_act_t act,uint16_t length,uint8_t * data,int32_t msg_timeout,bool need_rsp,esp_ble_mesh_dev_role_t device_role)17 static esp_err_t ble_mesh_model_send_msg(esp_ble_mesh_model_t *model,
18                                          esp_ble_mesh_msg_ctx_t *ctx,
19                                          uint32_t opcode,
20                                          btc_ble_mesh_model_act_t act,
21                                          uint16_t length, uint8_t *data,
22                                          int32_t msg_timeout, bool need_rsp,
23                                          esp_ble_mesh_dev_role_t device_role)
24 {
25     btc_ble_mesh_model_args_t arg = {0};
26     uint8_t op_len = 0, mic_len = 0;
27     uint8_t *msg_data = NULL;
28     btc_msg_t msg = {0};
29     esp_err_t status = ESP_OK;
30 
31     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
32 
33     if (ctx && ctx->addr == ESP_BLE_MESH_ADDR_UNASSIGNED) {
34         BT_ERR("Invalid destination address 0x0000");
35         return ESP_ERR_INVALID_ARG;
36     }
37 
38     if (device_role > ROLE_FAST_PROV) {
39         BT_ERR("Invalid device role 0x%02x", device_role);
40         return ESP_ERR_INVALID_ARG;
41     }
42 
43     /* When data is NULL, it is mandatory to set length to 0 to prevent users from misinterpreting parameters. */
44     if (data == NULL) {
45         length = 0;
46     }
47 
48     if (opcode < 0x100) {
49         op_len = 1;
50     } else if (opcode < 0x10000) {
51         op_len = 2;
52     } else {
53         op_len = 3;
54     }
55 
56     if (act == BTC_BLE_MESH_ACT_MODEL_PUBLISH) {
57         if (op_len + length > model->pub->msg->size) {
58             BT_ERR("Too small publication msg size %d", model->pub->msg->size);
59             return ESP_ERR_INVALID_ARG;
60         }
61     }
62 
63     if (act == BTC_BLE_MESH_ACT_MODEL_PUBLISH) {
64         mic_len = ESP_BLE_MESH_MIC_SHORT;
65     } else {
66         mic_len = ctx->send_rel ? ESP_BLE_MESH_MIC_LONG : ESP_BLE_MESH_MIC_SHORT;
67     }
68 
69     if (op_len + length + mic_len > MIN(ESP_BLE_MESH_SDU_MAX_LEN, ESP_BLE_MESH_TX_SDU_MAX)) {
70         BT_ERR("Too large data length %d", length);
71         return ESP_ERR_INVALID_ARG;
72     }
73 
74     if (act == BTC_BLE_MESH_ACT_MODEL_PUBLISH) {
75         bt_mesh_model_msg_init(model->pub->msg, opcode);
76         net_buf_simple_add_mem(model->pub->msg, data, length);
77     } else {
78         msg_data = (uint8_t *)bt_mesh_malloc(op_len + length);
79         if (msg_data == NULL) {
80             return ESP_ERR_NO_MEM;
81         }
82         esp_ble_mesh_model_msg_opcode_init(msg_data, opcode);
83         memcpy(msg_data + op_len, data, length);
84     }
85 
86     msg.sig = BTC_SIG_API_CALL;
87     msg.pid = BTC_PID_MODEL;
88     msg.act = act;
89 
90     if (act == BTC_BLE_MESH_ACT_MODEL_PUBLISH) {
91         arg.model_publish.model = model;
92         arg.model_publish.device_role = device_role;
93     } else {
94         arg.model_send.model = model;
95         arg.model_send.ctx = ctx;
96         arg.model_send.need_rsp = need_rsp;
97         arg.model_send.opcode = opcode;
98         arg.model_send.length = op_len + length;
99         arg.model_send.data = msg_data;
100         arg.model_send.device_role = device_role;
101         arg.model_send.msg_timeout = msg_timeout;
102     }
103 
104     status = (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_model_args_t), btc_ble_mesh_model_arg_deep_copy,
105                 btc_ble_mesh_model_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
106 
107     bt_mesh_free(msg_data);
108 
109     return status;
110 }
111 
esp_ble_mesh_register_custom_model_callback(esp_ble_mesh_model_cb_t callback)112 esp_err_t esp_ble_mesh_register_custom_model_callback(esp_ble_mesh_model_cb_t callback)
113 {
114     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
115 
116     return (btc_profile_cb_set(BTC_PID_MODEL, callback) == 0 ? ESP_OK : ESP_FAIL);
117 }
118 
esp_ble_mesh_model_msg_opcode_init(uint8_t * data,uint32_t opcode)119 esp_err_t esp_ble_mesh_model_msg_opcode_init(uint8_t *data, uint32_t opcode)
120 {
121     uint16_t val = 0;
122 
123     if (data == NULL) {
124         return ESP_ERR_INVALID_ARG;
125     }
126 
127     if (opcode < 0x100) {
128         /* 1-byte OpCode */
129         data[0] = opcode & 0xff;
130         return ESP_OK;
131     }
132 
133     if (opcode < 0x10000) {
134         /* 2-byte OpCode, big endian */
135         val = sys_cpu_to_be16 (opcode);
136         memcpy(data, &val, 2);
137         return ESP_OK;
138     }
139 
140     /* 3-byte OpCode, note that little endian for the least 2 bytes(Company ID) of opcode */
141     data[0] = (opcode >> 16) & 0xff;
142     val = sys_cpu_to_le16(opcode & 0xffff);
143     memcpy(&data[1], &val, 2);
144 
145     return ESP_OK;
146 }
147 
esp_ble_mesh_client_model_init(esp_ble_mesh_model_t * model)148 esp_err_t esp_ble_mesh_client_model_init(esp_ble_mesh_model_t *model)
149 {
150     if (model == NULL) {
151         return ESP_ERR_INVALID_ARG;
152     }
153 
154     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
155 
156     return btc_ble_mesh_client_model_init(model);
157 }
158 
159 #if CONFIG_BLE_MESH_DEINIT
esp_ble_mesh_client_model_deinit(esp_ble_mesh_model_t * model)160 esp_err_t esp_ble_mesh_client_model_deinit(esp_ble_mesh_model_t *model)
161 {
162     if (model == NULL) {
163         return ESP_ERR_INVALID_ARG;
164     }
165 
166     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
167 
168     return btc_ble_mesh_client_model_deinit(model);
169 }
170 #endif /* CONFIG_BLE_MESH_DEINIT */
171 
esp_ble_mesh_server_model_send_msg(esp_ble_mesh_model_t * model,esp_ble_mesh_msg_ctx_t * ctx,uint32_t opcode,uint16_t length,uint8_t * data)172 esp_err_t esp_ble_mesh_server_model_send_msg(esp_ble_mesh_model_t *model,
173                                              esp_ble_mesh_msg_ctx_t *ctx,
174                                              uint32_t opcode,
175                                              uint16_t length, uint8_t *data)
176 {
177     if (model == NULL || ctx == NULL ||
178         ctx->net_idx == ESP_BLE_MESH_KEY_UNUSED ||
179         ctx->app_idx == ESP_BLE_MESH_KEY_UNUSED) {
180         return ESP_ERR_INVALID_ARG;
181     }
182 
183     return ble_mesh_model_send_msg(model, ctx, opcode, BTC_BLE_MESH_ACT_SERVER_MODEL_SEND,
184                                    length, data, 0, false, ROLE_NODE);
185 }
186 
esp_ble_mesh_client_model_send_msg(esp_ble_mesh_model_t * model,esp_ble_mesh_msg_ctx_t * ctx,uint32_t opcode,uint16_t length,uint8_t * data,int32_t msg_timeout,bool need_rsp,esp_ble_mesh_dev_role_t device_role)187 esp_err_t esp_ble_mesh_client_model_send_msg(esp_ble_mesh_model_t *model,
188                                              esp_ble_mesh_msg_ctx_t *ctx,
189                                              uint32_t opcode,
190                                              uint16_t length, uint8_t *data,
191                                              int32_t msg_timeout, bool need_rsp,
192                                              esp_ble_mesh_dev_role_t device_role)
193 {
194     if (model == NULL || ctx == NULL ||
195         ctx->net_idx == ESP_BLE_MESH_KEY_UNUSED ||
196         ctx->app_idx == ESP_BLE_MESH_KEY_UNUSED) {
197         return ESP_ERR_INVALID_ARG;
198     }
199 
200     return ble_mesh_model_send_msg(model, ctx, opcode, BTC_BLE_MESH_ACT_CLIENT_MODEL_SEND,
201                                    length, data, msg_timeout, need_rsp, device_role);
202 }
203 
esp_ble_mesh_model_publish(esp_ble_mesh_model_t * model,uint32_t opcode,uint16_t length,uint8_t * data,esp_ble_mesh_dev_role_t device_role)204 esp_err_t esp_ble_mesh_model_publish(esp_ble_mesh_model_t *model, uint32_t opcode,
205                                      uint16_t length, uint8_t *data,
206                                      esp_ble_mesh_dev_role_t device_role)
207 {
208     if (model == NULL || model->pub == NULL || model->pub->msg == NULL ||
209         model->pub->publish_addr == ESP_BLE_MESH_ADDR_UNASSIGNED) {
210         return ESP_ERR_INVALID_ARG;
211     }
212 
213     return ble_mesh_model_send_msg(model, NULL, opcode, BTC_BLE_MESH_ACT_MODEL_PUBLISH,
214                                    length, data, 0, false, device_role);
215 }
216 
217 #if CONFIG_BLE_MESH_SERVER_MODEL
esp_ble_mesh_server_model_update_state(esp_ble_mesh_model_t * model,esp_ble_mesh_server_state_type_t type,esp_ble_mesh_server_state_value_t * value)218 esp_err_t esp_ble_mesh_server_model_update_state(esp_ble_mesh_model_t *model,
219                                                  esp_ble_mesh_server_state_type_t type,
220                                                  esp_ble_mesh_server_state_value_t *value)
221 {
222     btc_ble_mesh_model_args_t arg = {0};
223     btc_msg_t msg = {0};
224 
225     if (!model || !value || type >= ESP_BLE_MESH_SERVER_MODEL_STATE_MAX) {
226         return ESP_ERR_INVALID_ARG;
227     }
228 
229     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
230 
231     arg.model_update_state.model = model;
232     arg.model_update_state.type = type;
233     arg.model_update_state.value = value;
234 
235     msg.sig = BTC_SIG_API_CALL;
236     msg.pid = BTC_PID_MODEL;
237     msg.act = BTC_BLE_MESH_ACT_SERVER_MODEL_UPDATE_STATE;
238 
239     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_model_args_t), btc_ble_mesh_model_arg_deep_copy,
240                 btc_ble_mesh_model_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
241 }
242 #endif /* CONFIG_BLE_MESH_SERVER_MODEL */
243 
esp_ble_mesh_node_local_reset(void)244 esp_err_t esp_ble_mesh_node_local_reset(void)
245 {
246     btc_msg_t msg = {0};
247 
248     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
249 
250     msg.sig = BTC_SIG_API_CALL;
251     msg.pid = BTC_PID_PROV;
252     msg.act = BTC_BLE_MESH_ACT_NODE_RESET;
253 
254     return (btc_transfer_context(&msg, NULL, 0, NULL, NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
255 }
256 
257 #if (CONFIG_BLE_MESH_PROVISIONER)
258 
esp_ble_mesh_provisioner_set_node_name(uint16_t index,const char * name)259 esp_err_t esp_ble_mesh_provisioner_set_node_name(uint16_t index, const char *name)
260 {
261     btc_ble_mesh_prov_args_t arg = {0};
262     btc_msg_t msg = {0};
263 
264     if (!name || (strlen(name) > ESP_BLE_MESH_NODE_NAME_MAX_LEN)) {
265         return ESP_ERR_INVALID_ARG;
266     }
267 
268     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
269 
270     msg.sig = BTC_SIG_API_CALL;
271     msg.pid = BTC_PID_PROV;
272     msg.act = BTC_BLE_MESH_ACT_PROVISIONER_SET_NODE_NAME;
273 
274     arg.set_node_name.index = index;
275     memset(arg.set_node_name.name, 0, sizeof(arg.set_node_name.name));
276     strncpy(arg.set_node_name.name, name, ESP_BLE_MESH_NODE_NAME_MAX_LEN);
277 
278     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL)
279             == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
280 }
281 
esp_ble_mesh_provisioner_get_node_name(uint16_t index)282 const char *esp_ble_mesh_provisioner_get_node_name(uint16_t index)
283 {
284     return bt_mesh_provisioner_get_node_name(index);
285 }
286 
esp_ble_mesh_provisioner_get_node_index(const char * name)287 uint16_t esp_ble_mesh_provisioner_get_node_index(const char *name)
288 {
289     if (!name || (strlen(name) > ESP_BLE_MESH_NODE_NAME_MAX_LEN)) {
290         return ESP_BLE_MESH_INVALID_NODE_INDEX;
291     }
292 
293     return bt_mesh_provisioner_get_node_index(name);
294 }
295 
esp_ble_mesh_provisioner_store_node_comp_data(uint16_t unicast_addr,uint8_t * data,uint16_t length)296 esp_err_t esp_ble_mesh_provisioner_store_node_comp_data(uint16_t unicast_addr,
297                                                         uint8_t *data, uint16_t length)
298 {
299     btc_ble_mesh_prov_args_t arg = {0};
300     btc_msg_t msg = {0};
301 
302     if (!ESP_BLE_MESH_ADDR_IS_UNICAST(unicast_addr) || !data || length <= 14) {
303         return ESP_ERR_INVALID_ARG;
304     }
305 
306     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
307 
308     msg.sig = BTC_SIG_API_CALL;
309     msg.pid = BTC_PID_PROV;
310     msg.act = BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA;
311 
312     arg.store_node_comp_data.unicast_addr = unicast_addr;
313     arg.store_node_comp_data.length = length;
314     arg.store_node_comp_data.data = data;
315     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), btc_ble_mesh_prov_arg_deep_copy,
316                 btc_ble_mesh_prov_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
317 }
318 
esp_ble_mesh_provisioner_get_node_with_uuid(const uint8_t uuid[16])319 esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_with_uuid(const uint8_t uuid[16])
320 {
321     if (!uuid) {
322         return NULL;
323     }
324 
325     return btc_ble_mesh_provisioner_get_node_with_uuid(uuid);
326 }
327 
esp_ble_mesh_provisioner_get_node_with_addr(uint16_t unicast_addr)328 esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_with_addr(uint16_t unicast_addr)
329 {
330     if (!ESP_BLE_MESH_ADDR_IS_UNICAST(unicast_addr)) {
331         return NULL;
332     }
333 
334     return btc_ble_mesh_provisioner_get_node_with_addr(unicast_addr);
335 }
336 
esp_ble_mesh_provisioner_get_node_with_name(const char * name)337 esp_ble_mesh_node_t *esp_ble_mesh_provisioner_get_node_with_name(const char *name)
338 {
339     if (!name || (strlen(name) > ESP_BLE_MESH_NODE_NAME_MAX_LEN)) {
340         return NULL;
341     }
342 
343     return btc_ble_mesh_provisioner_get_node_with_name(name);
344 }
345 
esp_ble_mesh_provisioner_get_prov_node_count(void)346 uint16_t esp_ble_mesh_provisioner_get_prov_node_count(void)
347 {
348     return btc_ble_mesh_provisioner_get_prov_node_count();
349 }
350 
esp_ble_mesh_provisioner_get_node_table_entry(void)351 const esp_ble_mesh_node_t **esp_ble_mesh_provisioner_get_node_table_entry(void)
352 {
353     return btc_ble_mesh_provisioner_get_node_table_entry();
354 }
355 
esp_ble_mesh_provisioner_delete_node_with_uuid(const uint8_t uuid[16])356 esp_err_t esp_ble_mesh_provisioner_delete_node_with_uuid(const uint8_t uuid[16])
357 {
358     btc_ble_mesh_prov_args_t arg = {0};
359     btc_msg_t msg = {0};
360 
361     if (!uuid) {
362         return ESP_ERR_INVALID_ARG;
363     }
364 
365     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
366 
367     msg.sig = BTC_SIG_API_CALL;
368     msg.pid = BTC_PID_PROV;
369     msg.act = BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_UUID;
370 
371     memcpy(arg.delete_node_with_uuid.uuid, uuid, 16);
372 
373     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL)
374             == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
375 }
376 
esp_ble_mesh_provisioner_delete_node_with_addr(uint16_t unicast_addr)377 esp_err_t esp_ble_mesh_provisioner_delete_node_with_addr(uint16_t unicast_addr)
378 {
379     btc_ble_mesh_prov_args_t arg = {0};
380     btc_msg_t msg = {0};
381 
382     if (!ESP_BLE_MESH_ADDR_IS_UNICAST(unicast_addr)) {
383         return ESP_ERR_INVALID_ARG;
384     }
385 
386     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
387 
388     msg.sig = BTC_SIG_API_CALL;
389     msg.pid = BTC_PID_PROV;
390     msg.act = BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_ADDR;
391 
392     arg.delete_node_with_addr.unicast_addr = unicast_addr;
393 
394     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL)
395             == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
396 }
397 
esp_ble_mesh_provisioner_add_local_app_key(const uint8_t app_key[16],uint16_t net_idx,uint16_t app_idx)398 esp_err_t esp_ble_mesh_provisioner_add_local_app_key(const uint8_t app_key[16],
399                                                      uint16_t net_idx, uint16_t app_idx)
400 {
401     btc_ble_mesh_prov_args_t arg = {0};
402     btc_msg_t msg = {0};
403 
404     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
405 
406     msg.sig = BTC_SIG_API_CALL;
407     msg.pid = BTC_PID_PROV;
408     msg.act = BTC_BLE_MESH_ACT_PROVISIONER_ADD_LOCAL_APP_KEY;
409 
410     arg.add_local_app_key.net_idx = net_idx;
411     arg.add_local_app_key.app_idx = app_idx;
412     if (app_key) {
413         memcpy(arg.add_local_app_key.app_key, app_key, 16);
414     } else {
415         bzero(arg.add_local_app_key.app_key, 16);
416     }
417     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL)
418             == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
419 }
420 
esp_ble_mesh_provisioner_update_local_app_key(const uint8_t app_key[16],uint16_t net_idx,uint16_t app_idx)421 esp_err_t esp_ble_mesh_provisioner_update_local_app_key(const uint8_t app_key[16],
422                                                         uint16_t net_idx, uint16_t app_idx)
423 {
424     btc_ble_mesh_prov_args_t arg = {0};
425     btc_msg_t msg = {0};
426 
427     if (app_key == NULL) {
428         return ESP_ERR_INVALID_ARG;
429     }
430 
431     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
432 
433     msg.sig = BTC_SIG_API_CALL;
434     msg.pid = BTC_PID_PROV;
435     msg.act = BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_APP_KEY;
436 
437     memcpy(arg.update_local_app_key.app_key, app_key, 16);
438     arg.update_local_app_key.net_idx = net_idx;
439     arg.update_local_app_key.app_idx = app_idx;
440     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL)
441             == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
442 }
443 
esp_ble_mesh_provisioner_get_local_app_key(uint16_t net_idx,uint16_t app_idx)444 const uint8_t *esp_ble_mesh_provisioner_get_local_app_key(uint16_t net_idx, uint16_t app_idx)
445 {
446     return bt_mesh_provisioner_local_app_key_get(net_idx, app_idx);
447 }
448 
esp_ble_mesh_provisioner_bind_app_key_to_local_model(uint16_t element_addr,uint16_t app_idx,uint16_t model_id,uint16_t company_id)449 esp_err_t esp_ble_mesh_provisioner_bind_app_key_to_local_model(uint16_t element_addr, uint16_t app_idx,
450                                                                uint16_t model_id, uint16_t company_id)
451 {
452     btc_ble_mesh_prov_args_t arg = {0};
453     btc_msg_t msg = {0};
454 
455     if (!ESP_BLE_MESH_ADDR_IS_UNICAST(element_addr)) {
456         return ESP_ERR_INVALID_ARG;
457     }
458 
459     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
460 
461     msg.sig = BTC_SIG_API_CALL;
462     msg.pid = BTC_PID_PROV;
463     msg.act = BTC_BLE_MESH_ACT_PROVISIONER_BIND_LOCAL_MOD_APP;
464 
465     arg.local_mod_app_bind.elem_addr = element_addr;
466     arg.local_mod_app_bind.app_idx = app_idx;
467     arg.local_mod_app_bind.model_id = model_id;
468     arg.local_mod_app_bind.cid = company_id;
469     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL)
470             == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
471 }
472 
esp_ble_mesh_provisioner_add_local_net_key(const uint8_t net_key[16],uint16_t net_idx)473 esp_err_t esp_ble_mesh_provisioner_add_local_net_key(const uint8_t net_key[16], uint16_t net_idx)
474 {
475     btc_ble_mesh_prov_args_t arg = {0};
476     btc_msg_t msg = {0};
477 
478     if (net_idx == ESP_BLE_MESH_KEY_PRIMARY) {
479         return ESP_ERR_INVALID_ARG;
480     }
481 
482     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
483 
484     msg.sig = BTC_SIG_API_CALL;
485     msg.pid = BTC_PID_PROV;
486     msg.act = BTC_BLE_MESH_ACT_PROVISIONER_ADD_LOCAL_NET_KEY;
487 
488     arg.add_local_net_key.net_idx = net_idx;
489     if (net_key) {
490         memcpy(arg.add_local_net_key.net_key, net_key, 16);
491     } else {
492         bzero(arg.add_local_net_key.net_key, 16);
493     }
494     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL)
495             == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
496 }
497 
esp_ble_mesh_provisioner_update_local_net_key(const uint8_t net_key[16],uint16_t net_idx)498 esp_err_t esp_ble_mesh_provisioner_update_local_net_key(const uint8_t net_key[16], uint16_t net_idx)
499 {
500     btc_ble_mesh_prov_args_t arg = {0};
501     btc_msg_t msg = {0};
502 
503     if (net_key == NULL) {
504         return ESP_ERR_INVALID_ARG;
505     }
506 
507     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
508 
509     msg.sig = BTC_SIG_API_CALL;
510     msg.pid = BTC_PID_PROV;
511     msg.act = BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_NET_KEY;
512 
513     memcpy(arg.update_local_net_key.net_key, net_key, 16);
514     arg.update_local_net_key.net_idx = net_idx;
515     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL)
516             == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
517 }
518 
esp_ble_mesh_provisioner_get_local_net_key(uint16_t net_idx)519 const uint8_t *esp_ble_mesh_provisioner_get_local_net_key(uint16_t net_idx)
520 {
521     return bt_mesh_provisioner_local_net_key_get(net_idx);
522 }
523 
524 #if CONFIG_BLE_MESH_PROVISIONER_RECV_HB
esp_ble_mesh_provisioner_recv_heartbeat(bool enable)525 esp_err_t esp_ble_mesh_provisioner_recv_heartbeat(bool enable)
526 {
527     btc_ble_mesh_prov_args_t arg = {0};
528     btc_msg_t msg = {0};
529 
530     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
531 
532     msg.sig = BTC_SIG_API_CALL;
533     msg.pid = BTC_PID_PROV;
534     msg.act = BTC_BLE_MESH_ACT_PROVISIONER_ENABLE_HEARTBEAT_RECV;
535 
536     arg.enable_heartbeat_recv.enable = enable;
537 
538     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL)
539             == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
540 }
541 
esp_ble_mesh_provisioner_set_heartbeat_filter_type(uint8_t type)542 esp_err_t esp_ble_mesh_provisioner_set_heartbeat_filter_type(uint8_t type)
543 {
544     btc_ble_mesh_prov_args_t arg = {0};
545     btc_msg_t msg = {0};
546 
547     if (type > ESP_BLE_MESH_HEARTBEAT_FILTER_REJECTLIST) {
548         return ESP_ERR_INVALID_ARG;
549     }
550 
551     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
552 
553     msg.sig = BTC_SIG_API_CALL;
554     msg.pid = BTC_PID_PROV;
555     msg.act = BTC_BLE_MESH_ACT_PROVISIONER_SET_HEARTBEAT_FILTER_TYPE;
556 
557     arg.set_heartbeat_filter_type.type = type;
558 
559     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL)
560             == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
561 }
562 
esp_ble_mesh_provisioner_set_heartbeat_filter_info(uint8_t op,esp_ble_mesh_heartbeat_filter_info_t * info)563 esp_err_t esp_ble_mesh_provisioner_set_heartbeat_filter_info(uint8_t op, esp_ble_mesh_heartbeat_filter_info_t *info)
564 {
565     btc_ble_mesh_prov_args_t arg = {0};
566     btc_msg_t msg = {0};
567 
568     if (op > ESP_BLE_MESH_HEARTBEAT_FILTER_REMOVE || info == NULL) {
569         return ESP_ERR_INVALID_ARG;
570     }
571 
572     if (!(ESP_BLE_MESH_ADDR_IS_UNICAST(info->hb_src) &&
573         (ESP_BLE_MESH_ADDR_IS_UNICAST(info->hb_dst) ||
574         ESP_BLE_MESH_ADDR_IS_GROUP(info->hb_dst)))) {
575         return ESP_ERR_INVALID_ARG;
576     }
577 
578     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
579 
580     msg.sig = BTC_SIG_API_CALL;
581     msg.pid = BTC_PID_PROV;
582     msg.act = BTC_BLE_MESH_ACT_PROVISIONER_SET_HEARTBEAT_FILTER_INFO;
583 
584     arg.set_heartbeat_filter_info.op = op;
585     arg.set_heartbeat_filter_info.hb_src = info->hb_src;
586     arg.set_heartbeat_filter_info.hb_dst = info->hb_dst;
587 
588     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL)
589             == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
590 }
591 #endif /* CONFIG_BLE_MESH_PROVISIONER_RECV_HB */
592 
593 #if CONFIG_BLE_MESH_SETTINGS
esp_ble_mesh_provisioner_direct_erase_settings(void)594 esp_err_t esp_ble_mesh_provisioner_direct_erase_settings(void)
595 {
596     btc_msg_t msg = {0};
597 
598     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
599 
600     msg.sig = BTC_SIG_API_CALL;
601     msg.pid = BTC_PID_PROV;
602 
603     msg.act = BTC_BLE_MESH_ACT_PROVISIONER_DIRECT_ERASE_SETTINGS;
604 
605     return (btc_transfer_context(&msg, NULL, 0, NULL, NULL)
606             == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
607 }
608 #endif /* CONFIG_BLE_MESH_SETTINGS */
609 
610 #if CONFIG_BLE_MESH_USE_MULTIPLE_NAMESPACE
esp_ble_mesh_provisioner_open_settings_with_index(uint8_t index)611 esp_err_t esp_ble_mesh_provisioner_open_settings_with_index(uint8_t index)
612 {
613     btc_ble_mesh_prov_args_t arg = {0};
614     btc_msg_t msg = {0};
615 
616     if (index >= CONFIG_BLE_MESH_MAX_NVS_NAMESPACE) {
617         return ESP_ERR_INVALID_ARG;
618     }
619 
620     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
621 
622     msg.sig = BTC_SIG_API_CALL;
623     msg.pid = BTC_PID_PROV;
624     msg.act = BTC_BLE_MESH_ACT_PROVISIONER_OPEN_SETTINGS_WITH_INDEX;
625 
626     arg.open_settings_with_index.index = index;
627 
628     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL)
629             == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
630 }
631 
esp_ble_mesh_provisioner_open_settings_with_uid(const char * uid)632 esp_err_t esp_ble_mesh_provisioner_open_settings_with_uid(const char *uid)
633 {
634     btc_ble_mesh_prov_args_t arg = {0};
635     btc_msg_t msg = {0};
636 
637     if (!uid || strlen(uid) > ESP_BLE_MESH_SETTINGS_UID_SIZE) {
638         return ESP_ERR_INVALID_ARG;
639     }
640 
641     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
642 
643     msg.sig = BTC_SIG_API_CALL;
644     msg.pid = BTC_PID_PROV;
645     msg.act = BTC_BLE_MESH_ACT_PROVISIONER_OPEN_SETTINGS_WITH_UID;
646 
647     strncpy(arg.open_settings_with_uid.uid, uid, ESP_BLE_MESH_SETTINGS_UID_SIZE);
648 
649     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL)
650             == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
651 }
652 
esp_ble_mesh_provisioner_close_settings_with_index(uint8_t index,bool erase)653 esp_err_t esp_ble_mesh_provisioner_close_settings_with_index(uint8_t index, bool erase)
654 {
655     btc_ble_mesh_prov_args_t arg = {0};
656     btc_msg_t msg = {0};
657 
658     if (index >= CONFIG_BLE_MESH_MAX_NVS_NAMESPACE) {
659         return ESP_ERR_INVALID_ARG;
660     }
661 
662     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
663 
664     msg.sig = BTC_SIG_API_CALL;
665     msg.pid = BTC_PID_PROV;
666     msg.act = BTC_BLE_MESH_ACT_PROVISIONER_CLOSE_SETTINGS_WITH_INDEX;
667 
668     arg.close_settings_with_index.index = index;
669     arg.close_settings_with_index.erase = erase;
670 
671     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL)
672             == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
673 }
674 
esp_ble_mesh_provisioner_close_settings_with_uid(const char * uid,bool erase)675 esp_err_t esp_ble_mesh_provisioner_close_settings_with_uid(const char *uid, bool erase)
676 {
677     btc_ble_mesh_prov_args_t arg = {0};
678     btc_msg_t msg = {0};
679 
680     if (!uid || strlen(uid) > ESP_BLE_MESH_SETTINGS_UID_SIZE) {
681         return ESP_ERR_INVALID_ARG;
682     }
683 
684     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
685 
686     msg.sig = BTC_SIG_API_CALL;
687     msg.pid = BTC_PID_PROV;
688     msg.act = BTC_BLE_MESH_ACT_PROVISIONER_CLOSE_SETTINGS_WITH_UID;
689 
690     strncpy(arg.close_settings_with_uid.uid, uid, ESP_BLE_MESH_SETTINGS_UID_SIZE);
691     arg.close_settings_with_uid.erase = erase;
692 
693     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL)
694             == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
695 }
696 
esp_ble_mesh_provisioner_delete_settings_with_index(uint8_t index)697 esp_err_t esp_ble_mesh_provisioner_delete_settings_with_index(uint8_t index)
698 {
699     btc_ble_mesh_prov_args_t arg = {0};
700     btc_msg_t msg = {0};
701 
702     if (index >= CONFIG_BLE_MESH_MAX_NVS_NAMESPACE) {
703         return ESP_ERR_INVALID_ARG;
704     }
705 
706     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
707 
708     msg.sig = BTC_SIG_API_CALL;
709     msg.pid = BTC_PID_PROV;
710     msg.act = BTC_BLE_MESH_ACT_PROVISIONER_DELETE_SETTINGS_WITH_INDEX;
711 
712     arg.delete_settings_with_index.index = index;
713 
714     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL)
715             == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
716 }
717 
esp_ble_mesh_provisioner_delete_settings_with_uid(const char * uid)718 esp_err_t esp_ble_mesh_provisioner_delete_settings_with_uid(const char *uid)
719 {
720     btc_ble_mesh_prov_args_t arg = {0};
721     btc_msg_t msg = {0};
722 
723     if (!uid || strlen(uid) > ESP_BLE_MESH_SETTINGS_UID_SIZE) {
724         return ESP_ERR_INVALID_ARG;
725     }
726 
727     ESP_BLE_HOST_STATUS_CHECK(ESP_BLE_HOST_STATUS_ENABLED);
728 
729     msg.sig = BTC_SIG_API_CALL;
730     msg.pid = BTC_PID_PROV;
731     msg.act = BTC_BLE_MESH_ACT_PROVISIONER_DELETE_SETTINGS_WITH_UID;
732 
733     strncpy(arg.delete_settings_with_uid.uid, uid, ESP_BLE_MESH_SETTINGS_UID_SIZE);
734 
735     return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_mesh_prov_args_t), NULL, NULL)
736             == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
737 }
738 
esp_ble_mesh_provisioner_get_settings_uid(uint8_t index)739 const char *esp_ble_mesh_provisioner_get_settings_uid(uint8_t index)
740 {
741     if (index >= CONFIG_BLE_MESH_MAX_NVS_NAMESPACE) {
742         return NULL;
743     }
744 
745     return btc_ble_mesh_provisioner_get_settings_uid(index);
746 }
747 
esp_ble_mesh_provisioner_get_settings_index(const char * uid)748 uint8_t esp_ble_mesh_provisioner_get_settings_index(const char *uid)
749 {
750     if (!uid || strlen(uid) > ESP_BLE_MESH_SETTINGS_UID_SIZE) {
751         return ESP_BLE_MESH_INVALID_SETTINGS_IDX;
752     }
753 
754     return btc_ble_mesh_provisioner_get_settings_index(uid);
755 }
756 
esp_ble_mesh_provisioner_get_free_settings_count(void)757 uint8_t esp_ble_mesh_provisioner_get_free_settings_count(void)
758 {
759     return btc_ble_mesh_provisioner_get_free_settings_count();
760 }
761 #endif /* CONFIG_BLE_MESH_USE_MULTIPLE_NAMESPACE */
762 
763 #endif /* CONFIG_BLE_MESH_PROVISIONER */
764 
765 #if (CONFIG_BLE_MESH_FAST_PROV)
esp_ble_mesh_get_fast_prov_app_key(uint16_t net_idx,uint16_t app_idx)766 const uint8_t *esp_ble_mesh_get_fast_prov_app_key(uint16_t net_idx, uint16_t app_idx)
767 {
768     return bt_mesh_get_fast_prov_app_key(net_idx, app_idx);
769 }
770 #endif  /* CONFIG_BLE_MESH_FAST_PROV */
771