1 /*
2  * SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <string.h>
8 #include <errno.h>
9 
10 #include "btc_ble_mesh_prov.h"
11 #include "btc_ble_mesh_config_model.h"
12 #include "btc_ble_mesh_health_model.h"
13 #include "btc_ble_mesh_generic_model.h"
14 #include "btc_ble_mesh_time_scene_model.h"
15 #include "btc_ble_mesh_sensor_model.h"
16 #include "btc_ble_mesh_lighting_model.h"
17 
18 #include "adv.h"
19 #include "mesh_kernel.h"
20 #include "mesh_proxy.h"
21 #include "mesh.h"
22 #include "access.h"
23 #include "prov.h"
24 #include "settings_uid.h"
25 #include "proxy_server.h"
26 #include "proxy_client.h"
27 #include "provisioner_prov.h"
28 #include "provisioner_main.h"
29 
30 #if CONFIG_BLE_MESH_CFG_CLI
31 #include "cfg_cli.h"
32 #endif /* CONFIG_BLE_MESH_CFG_CLI */
33 #if CONFIG_BLE_MESH_HEALTH_CLI
34 #include "health_cli.h"
35 #endif /* CONFIG_BLE_MESH_HEALTH_CLI */
36 #include "cfg_srv.h"
37 #if CONFIG_BLE_MESH_HEALTH_SRV
38 #include "health_srv.h"
39 #endif /* CONFIG_BLE_MESH_HEALTH_SRV */
40 #if CONFIG_BLE_MESH_GENERIC_CLIENT
41 #include "generic_client.h"
42 #endif /* CONFIG_BLE_MESH_GENERIC_CLIENT */
43 #if CONFIG_BLE_MESH_LIGHTING_CLIENT
44 #include "lighting_client.h"
45 #endif /* CONFIG_BLE_MESH_LIGHTING_CLIENT */
46 #if CONFIG_BLE_MESH_SENSOR_CLI
47 #include "sensor_client.h"
48 #endif /* CONFIG_BLE_MESH_SENSOR_CLI */
49 #if CONFIG_BLE_MESH_TIME_SCENE_CLIENT
50 #include "time_scene_client.h"
51 #endif /* CONFIG_BLE_MESH_TIME_SCENE_CLIENT */
52 #include "client_common.h"
53 #include "state_binding.h"
54 #include "local_operation.h"
55 
56 #include "esp_ble_mesh_common_api.h"
57 #include "esp_ble_mesh_provisioning_api.h"
58 #include "esp_ble_mesh_networking_api.h"
59 
60 #if CONFIG_BLE_MESH_DEINIT
61 static SemaphoreHandle_t deinit_comp_semaphore;
62 #endif
63 
btc_ble_mesh_prov_cb_to_app_reprocess(esp_ble_mesh_prov_cb_event_t event,esp_ble_mesh_prov_cb_param_t * param)64 static inline void btc_ble_mesh_prov_cb_to_app_reprocess(esp_ble_mesh_prov_cb_event_t event,
65                                                          esp_ble_mesh_prov_cb_param_t *param)
66 {
67     switch (event) {
68 #if CONFIG_BLE_MESH_DEINIT
69     case ESP_BLE_MESH_DEINIT_MESH_COMP_EVT:
70         assert(deinit_comp_semaphore);
71         /* Give the semaphore when BLE Mesh de-initialization is finished.
72          * @note: At nimble host, once this lock is released, it will cause
73          * the btc task to be deleted.
74          */
75         xSemaphoreGive(deinit_comp_semaphore);
76         break;
77 #endif
78     default:
79         break;
80     }
81 }
82 
btc_ble_mesh_prov_cb_to_app(esp_ble_mesh_prov_cb_event_t event,esp_ble_mesh_prov_cb_param_t * param)83 static inline void btc_ble_mesh_prov_cb_to_app(esp_ble_mesh_prov_cb_event_t event,
84                                                esp_ble_mesh_prov_cb_param_t *param)
85 {
86     esp_ble_mesh_prov_cb_t btc_ble_mesh_cb =
87         (esp_ble_mesh_prov_cb_t)btc_profile_cb_get(BTC_PID_PROV);
88     if (btc_ble_mesh_cb) {
89         btc_ble_mesh_cb(event, param);
90     }
91 
92     btc_ble_mesh_prov_cb_to_app_reprocess(event, param);
93 }
94 
btc_ble_mesh_model_cb_to_app(esp_ble_mesh_model_cb_event_t event,esp_ble_mesh_model_cb_param_t * param)95 static inline void btc_ble_mesh_model_cb_to_app(esp_ble_mesh_model_cb_event_t event,
96                                                 esp_ble_mesh_model_cb_param_t *param)
97 {
98     esp_ble_mesh_model_cb_t btc_ble_mesh_cb =
99         (esp_ble_mesh_model_cb_t)btc_profile_cb_get(BTC_PID_MODEL);
100     if (btc_ble_mesh_cb) {
101         btc_ble_mesh_cb(event, param);
102     }
103 }
104 
btc_ble_mesh_prov_arg_deep_copy(btc_msg_t * msg,void * p_dest,void * p_src)105 void btc_ble_mesh_prov_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
106 {
107     btc_ble_mesh_prov_args_t *dst = (btc_ble_mesh_prov_args_t *)p_dest;
108     btc_ble_mesh_prov_args_t *src = (btc_ble_mesh_prov_args_t *)p_src;
109 
110     if (!msg || !dst || !src) {
111         BT_ERR("%s, Invalid parameter", __func__);
112         return;
113     }
114 
115     switch (msg->act) {
116     case BTC_BLE_MESH_ACT_PROXY_CLIENT_ADD_FILTER_ADDR:
117         dst->proxy_client_add_filter_addr.addr = (uint16_t *)bt_mesh_calloc(src->proxy_client_add_filter_addr.addr_num << 1);
118         if (dst->proxy_client_add_filter_addr.addr) {
119             memcpy(dst->proxy_client_add_filter_addr.addr, src->proxy_client_add_filter_addr.addr,
120                    src->proxy_client_add_filter_addr.addr_num << 1);
121         } else {
122             BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
123         }
124         break;
125     case BTC_BLE_MESH_ACT_PROXY_CLIENT_REMOVE_FILTER_ADDR:
126         dst->proxy_client_remove_filter_addr.addr = bt_mesh_calloc(src->proxy_client_remove_filter_addr.addr_num << 1);
127         if (dst->proxy_client_remove_filter_addr.addr) {
128             memcpy(dst->proxy_client_remove_filter_addr.addr, src->proxy_client_remove_filter_addr.addr,
129                    src->proxy_client_remove_filter_addr.addr_num << 1);
130         } else {
131             BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
132         }
133         break;
134     case BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA:
135         dst->store_node_comp_data.data = bt_mesh_calloc(src->store_node_comp_data.length);
136         if (dst->store_node_comp_data.data) {
137             memcpy(dst->store_node_comp_data.data, src->store_node_comp_data.data, src->store_node_comp_data.length);
138         } else {
139             BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
140         }
141         break;
142     default:
143         BT_DBG("%s, Unknown act %d", __func__, msg->act);
144         break;
145     }
146 }
147 
btc_ble_mesh_prov_arg_deep_free(btc_msg_t * msg)148 void btc_ble_mesh_prov_arg_deep_free(btc_msg_t *msg)
149 {
150     btc_ble_mesh_prov_args_t *arg = NULL;
151 
152     if (!msg) {
153         BT_ERR("%s, Invalid parameter", __func__);
154         return;
155     }
156 
157     arg = (btc_ble_mesh_prov_args_t *)(msg->arg);
158 
159     switch (msg->act) {
160     case BTC_BLE_MESH_ACT_PROXY_CLIENT_ADD_FILTER_ADDR:
161         if (arg->proxy_client_add_filter_addr.addr) {
162             bt_mesh_free(arg->proxy_client_add_filter_addr.addr);
163         }
164         break;
165     case BTC_BLE_MESH_ACT_PROXY_CLIENT_REMOVE_FILTER_ADDR:
166         if (arg->proxy_client_remove_filter_addr.addr) {
167             bt_mesh_free(arg->proxy_client_remove_filter_addr.addr);
168         }
169         break;
170     case BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA:
171         if (arg->store_node_comp_data.data) {
172             bt_mesh_free(arg->store_node_comp_data.data);
173         }
174         break;
175     default:
176         break;
177     }
178 }
179 
btc_ble_mesh_model_arg_deep_copy(btc_msg_t * msg,void * p_dest,void * p_src)180 void btc_ble_mesh_model_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
181 {
182     btc_ble_mesh_model_args_t *dst = (btc_ble_mesh_model_args_t *)p_dest;
183     btc_ble_mesh_model_args_t *src = (btc_ble_mesh_model_args_t *)p_src;
184 
185     if (!msg || !dst || !src) {
186         BT_ERR("%s, Invalid parameter", __func__);
187         return;
188     }
189 
190     switch (msg->act) {
191     case BTC_BLE_MESH_ACT_SERVER_MODEL_SEND:
192     case BTC_BLE_MESH_ACT_CLIENT_MODEL_SEND: {
193         dst->model_send.data = src->model_send.length ? (uint8_t *)bt_mesh_malloc(src->model_send.length) : NULL;
194         dst->model_send.ctx = bt_mesh_malloc(sizeof(esp_ble_mesh_msg_ctx_t));
195         if (src->model_send.length) {
196             if (dst->model_send.data) {
197                 memcpy(dst->model_send.data, src->model_send.data, src->model_send.length);
198             } else {
199                 BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
200             }
201         }
202         if (dst->model_send.ctx) {
203             memcpy(dst->model_send.ctx, src->model_send.ctx, sizeof(esp_ble_mesh_msg_ctx_t));
204         } else {
205             BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
206         }
207         break;
208     }
209     case BTC_BLE_MESH_ACT_SERVER_MODEL_UPDATE_STATE:
210         dst->model_update_state.value = bt_mesh_malloc(sizeof(esp_ble_mesh_server_state_value_t));
211         if (dst->model_update_state.value) {
212             memcpy(dst->model_update_state.value, src->model_update_state.value,
213                    sizeof(esp_ble_mesh_server_state_value_t));
214         } else {
215             BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
216         }
217         break;
218     default:
219         BT_DBG("%s, Unknown act %d", __func__, msg->act);
220         break;
221     }
222 }
223 
btc_ble_mesh_model_arg_deep_free(btc_msg_t * msg)224 void btc_ble_mesh_model_arg_deep_free(btc_msg_t *msg)
225 {
226     btc_ble_mesh_model_args_t *arg = NULL;
227 
228     if (!msg) {
229         BT_ERR("%s, Invalid parameter", __func__);
230         return;
231     }
232 
233     arg = (btc_ble_mesh_model_args_t *)(msg->arg);
234 
235     switch (msg->act) {
236     case BTC_BLE_MESH_ACT_SERVER_MODEL_SEND:
237     case BTC_BLE_MESH_ACT_CLIENT_MODEL_SEND:
238         if (arg->model_send.data) {
239             bt_mesh_free(arg->model_send.data);
240         }
241         if (arg->model_send.ctx) {
242             bt_mesh_free(arg->model_send.ctx);
243         }
244         break;
245     case BTC_BLE_MESH_ACT_SERVER_MODEL_UPDATE_STATE:
246         if (arg->model_update_state.value) {
247             bt_mesh_free(arg->model_update_state.value);
248         }
249         break;
250     default:
251         break;
252     }
253 
254     return;
255 }
256 
btc_ble_mesh_model_copy_req_data(btc_msg_t * msg,void * p_dest,void * p_src)257 static void btc_ble_mesh_model_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src)
258 {
259     esp_ble_mesh_model_cb_param_t *p_dest_data = (esp_ble_mesh_model_cb_param_t *)p_dest;
260     esp_ble_mesh_model_cb_param_t *p_src_data = (esp_ble_mesh_model_cb_param_t *)p_src;
261 
262     if (!msg || !p_src_data || !p_dest_data) {
263         BT_ERR("%s, Invalid parameter", __func__);
264         return;
265     }
266 
267     switch (msg->act) {
268     case ESP_BLE_MESH_MODEL_OPERATION_EVT: {
269         if (p_src_data->model_operation.ctx && p_src_data->model_operation.msg) {
270             p_dest_data->model_operation.ctx = bt_mesh_malloc(sizeof(esp_ble_mesh_msg_ctx_t));
271             p_dest_data->model_operation.msg = p_src_data->model_operation.length ? (uint8_t *)bt_mesh_malloc(p_src_data->model_operation.length) : NULL;
272             if (p_dest_data->model_operation.ctx) {
273                 memcpy(p_dest_data->model_operation.ctx, p_src_data->model_operation.ctx, sizeof(esp_ble_mesh_msg_ctx_t));
274             } else {
275                 BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
276             }
277             if (p_src_data->model_operation.length) {
278                 if (p_dest_data->model_operation.msg) {
279                     memcpy(p_dest_data->model_operation.msg, p_src_data->model_operation.msg, p_src_data->model_operation.length);
280                 } else {
281                     BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
282                 }
283             }
284         }
285         break;
286     }
287     case ESP_BLE_MESH_CLIENT_MODEL_RECV_PUBLISH_MSG_EVT: {
288         if (p_src_data->client_recv_publish_msg.ctx && p_src_data->client_recv_publish_msg.msg) {
289             p_dest_data->client_recv_publish_msg.ctx = bt_mesh_malloc(sizeof(esp_ble_mesh_msg_ctx_t));
290             p_dest_data->client_recv_publish_msg.msg = p_src_data->client_recv_publish_msg.length ? (uint8_t *)bt_mesh_malloc(p_src_data->client_recv_publish_msg.length) : NULL;
291             if (p_dest_data->client_recv_publish_msg.ctx) {
292                 memcpy(p_dest_data->client_recv_publish_msg.ctx, p_src_data->client_recv_publish_msg.ctx, sizeof(esp_ble_mesh_msg_ctx_t));
293             } else {
294                 BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
295             }
296             if (p_src_data->client_recv_publish_msg.length) {
297                 if (p_dest_data->client_recv_publish_msg.msg) {
298                     memcpy(p_dest_data->client_recv_publish_msg.msg, p_src_data->client_recv_publish_msg.msg, p_src_data->client_recv_publish_msg.length);
299                 } else {
300                     BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
301                 }
302             }
303         }
304         break;
305     }
306     case ESP_BLE_MESH_MODEL_SEND_COMP_EVT: {
307         if (p_src_data->model_send_comp.ctx) {
308             p_dest_data->model_send_comp.ctx = bt_mesh_malloc(sizeof(esp_ble_mesh_msg_ctx_t));
309             if (p_dest_data->model_send_comp.ctx) {
310                 memcpy(p_dest_data->model_send_comp.ctx, p_src_data->model_send_comp.ctx, sizeof(esp_ble_mesh_msg_ctx_t));
311             } else {
312                 BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
313             }
314         }
315         break;
316     }
317     case ESP_BLE_MESH_CLIENT_MODEL_SEND_TIMEOUT_EVT: {
318         if (p_src_data->client_send_timeout.ctx) {
319             p_dest_data->client_send_timeout.ctx = bt_mesh_malloc(sizeof(esp_ble_mesh_msg_ctx_t));
320             if (p_dest_data->client_send_timeout.ctx) {
321                 memcpy(p_dest_data->client_send_timeout.ctx, p_src_data->client_send_timeout.ctx, sizeof(esp_ble_mesh_msg_ctx_t));
322             } else {
323                 BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
324             }
325         }
326         break;
327     }
328     default:
329         break;
330     }
331 }
332 
btc_ble_mesh_model_free_req_data(btc_msg_t * msg)333 static void btc_ble_mesh_model_free_req_data(btc_msg_t *msg)
334 {
335     esp_ble_mesh_model_cb_param_t *arg = NULL;
336 
337     if (!msg) {
338         BT_ERR("%s, Invalid parameter", __func__);
339         return;
340     }
341 
342     arg = (esp_ble_mesh_model_cb_param_t *)(msg->arg);
343 
344     switch (msg->act) {
345     case ESP_BLE_MESH_MODEL_OPERATION_EVT: {
346         if (arg->model_operation.msg) {
347             bt_mesh_free(arg->model_operation.msg);
348         }
349         if (arg->model_operation.ctx) {
350             bt_mesh_free(arg->model_operation.ctx);
351         }
352         break;
353     }
354     case ESP_BLE_MESH_CLIENT_MODEL_RECV_PUBLISH_MSG_EVT: {
355         if (arg->client_recv_publish_msg.msg) {
356             bt_mesh_free(arg->client_recv_publish_msg.msg);
357         }
358         if (arg->client_recv_publish_msg.ctx) {
359             bt_mesh_free(arg->client_recv_publish_msg.ctx);
360         }
361         break;
362     }
363     case ESP_BLE_MESH_MODEL_SEND_COMP_EVT: {
364         if (arg->model_send_comp.ctx) {
365             bt_mesh_free(arg->model_send_comp.ctx);
366         }
367         break;
368     }
369     case ESP_BLE_MESH_CLIENT_MODEL_SEND_TIMEOUT_EVT: {
370         if (arg->client_send_timeout.ctx) {
371             bt_mesh_free(arg->client_send_timeout.ctx);
372         }
373         break;
374     }
375     default:
376         break;
377     }
378 }
379 
btc_ble_mesh_model_callback(esp_ble_mesh_model_cb_param_t * param,uint8_t act)380 static bt_status_t btc_ble_mesh_model_callback(esp_ble_mesh_model_cb_param_t *param, uint8_t act)
381 {
382     btc_msg_t msg = {0};
383     bt_status_t ret = BT_STATUS_SUCCESS;
384 
385     BT_DBG("%s", __func__);
386 
387     /* If corresponding callback is not registered, event will not be posted. */
388     if (!btc_profile_cb_get(BTC_PID_MODEL)) {
389         return BT_STATUS_SUCCESS;
390     }
391 
392     msg.sig = BTC_SIG_API_CB;
393     msg.pid = BTC_PID_MODEL;
394     msg.act = act;
395 
396     ret = btc_transfer_context(&msg, param, param == NULL ? 0 : sizeof(esp_ble_mesh_model_cb_param_t),
397                                btc_ble_mesh_model_copy_req_data, btc_ble_mesh_model_free_req_data);
398     if (ret != BT_STATUS_SUCCESS) {
399         BT_ERR("btc_transfer_context failed");
400     }
401     return ret;
402 }
403 
btc_ble_mesh_server_model_op_cb(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)404 static void btc_ble_mesh_server_model_op_cb(struct bt_mesh_model *model,
405                                             struct bt_mesh_msg_ctx *ctx,
406                                             struct net_buf_simple *buf)
407 {
408     esp_ble_mesh_model_cb_param_t mesh_param = {0};
409 
410     mesh_param.model_operation.opcode = ctx->recv_op;
411     mesh_param.model_operation.model = (esp_ble_mesh_model_t *)model;
412     mesh_param.model_operation.ctx = (esp_ble_mesh_msg_ctx_t *)ctx;
413     mesh_param.model_operation.length = buf->len;
414     mesh_param.model_operation.msg = buf->data;
415 
416     btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_MODEL_OPERATION_EVT);
417     return;
418 }
419 
btc_ble_mesh_client_model_op_cb(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)420 static void btc_ble_mesh_client_model_op_cb(struct bt_mesh_model *model,
421                                             struct bt_mesh_msg_ctx *ctx,
422                                             struct net_buf_simple *buf)
423 {
424     esp_ble_mesh_model_cb_param_t mesh_param = {0};
425     bt_mesh_client_node_t *node = NULL;
426 
427     if (!model || !model->user_data || !ctx || !buf) {
428         BT_ERR("%s, Invalid parameter", __func__);
429         return;
430     }
431 
432     bt_mesh_client_model_lock();
433 
434     node = bt_mesh_is_client_recv_publish_msg(model, ctx, buf, false);
435     if (node == NULL) {
436         mesh_param.client_recv_publish_msg.opcode = ctx->recv_op;
437         mesh_param.client_recv_publish_msg.model = (esp_ble_mesh_model_t *)model;
438         mesh_param.client_recv_publish_msg.ctx = (esp_ble_mesh_msg_ctx_t *)ctx;
439         mesh_param.client_recv_publish_msg.length = buf->len;
440         mesh_param.client_recv_publish_msg.msg = buf->data;
441         btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_CLIENT_MODEL_RECV_PUBLISH_MSG_EVT);
442     } else {
443         mesh_param.model_operation.opcode = ctx->recv_op;
444         mesh_param.model_operation.model = (esp_ble_mesh_model_t *)model;
445         mesh_param.model_operation.ctx = (esp_ble_mesh_msg_ctx_t *)ctx;
446         mesh_param.model_operation.length = buf->len;
447         mesh_param.model_operation.msg = buf->data;
448         if (!k_delayed_work_free(&node->timer)) {
449             bt_mesh_client_free_node(node);
450             btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_MODEL_OPERATION_EVT);
451         }
452     }
453 
454     bt_mesh_client_model_unlock();
455     return;
456 }
457 
btc_ble_mesh_client_model_timeout_cb(struct k_work * work)458 static void btc_ble_mesh_client_model_timeout_cb(struct k_work *work)
459 {
460     esp_ble_mesh_model_cb_param_t mesh_param = {0};
461     struct k_delayed_work *timer = NULL;
462     bt_mesh_client_node_t *node = NULL;
463     struct bt_mesh_msg_ctx ctx = {0};
464 
465     bt_mesh_client_model_lock();
466 
467     timer = CONTAINER_OF(work, struct k_delayed_work, work);
468 
469     if (timer && !k_delayed_work_free(timer)) {
470         node = CONTAINER_OF(work, bt_mesh_client_node_t, timer.work);
471         if (node) {
472             memcpy(&ctx, &node->ctx, sizeof(ctx));
473             mesh_param.client_send_timeout.opcode = node->opcode;
474             mesh_param.client_send_timeout.model = (esp_ble_mesh_model_t *)ctx.model;
475             mesh_param.client_send_timeout.ctx = (esp_ble_mesh_msg_ctx_t *)&ctx;
476             bt_mesh_client_free_node(node);
477             btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_CLIENT_MODEL_SEND_TIMEOUT_EVT);
478         }
479     }
480 
481     bt_mesh_client_model_unlock();
482     return;
483 }
484 
btc_ble_mesh_model_send_comp_cb(esp_ble_mesh_model_t * model,esp_ble_mesh_msg_ctx_t * ctx,uint32_t opcode,int err)485 static void btc_ble_mesh_model_send_comp_cb(esp_ble_mesh_model_t *model,
486                                             esp_ble_mesh_msg_ctx_t *ctx,
487                                             uint32_t opcode, int err)
488 {
489     esp_ble_mesh_model_cb_param_t mesh_param = {0};
490 
491     mesh_param.model_send_comp.err_code = err;
492     mesh_param.model_send_comp.opcode = opcode;
493     mesh_param.model_send_comp.model = model;
494     mesh_param.model_send_comp.ctx = ctx;
495 
496     btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_MODEL_SEND_COMP_EVT);
497     return;
498 }
499 
btc_ble_mesh_model_publish_comp_cb(esp_ble_mesh_model_t * model,int err)500 static void btc_ble_mesh_model_publish_comp_cb(esp_ble_mesh_model_t *model, int err)
501 {
502     esp_ble_mesh_model_cb_param_t mesh_param = {0};
503 
504     mesh_param.model_publish_comp.err_code = err;
505     mesh_param.model_publish_comp.model = model;
506 
507     btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_MODEL_PUBLISH_COMP_EVT);
508     return;
509 }
510 
btc_ble_mesh_model_publish_update(struct bt_mesh_model * mod)511 static int btc_ble_mesh_model_publish_update(struct bt_mesh_model *mod)
512 {
513     esp_ble_mesh_model_cb_param_t mesh_param = {0};
514     bt_status_t ret = BT_STATUS_SUCCESS;
515 
516     BT_DBG("%s", __func__);
517 
518     mesh_param.model_publish_update.model = (esp_ble_mesh_model_t *)mod;
519 
520     ret = btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_MODEL_PUBLISH_UPDATE_EVT);
521     return (ret == BT_STATUS_SUCCESS) ? 0 : -1;
522 }
523 
524 #if CONFIG_BLE_MESH_SERVER_MODEL
btc_ble_mesh_server_model_update_state_comp_cb(esp_ble_mesh_model_t * model,esp_ble_mesh_server_state_type_t type,int err)525 static void btc_ble_mesh_server_model_update_state_comp_cb(esp_ble_mesh_model_t *model,
526                                                            esp_ble_mesh_server_state_type_t type,
527                                                            int err)
528 {
529     esp_ble_mesh_model_cb_param_t mesh_param = {0};
530 
531     mesh_param.server_model_update_state.err_code = err;
532     mesh_param.server_model_update_state.model = model;
533     mesh_param.server_model_update_state.type = type;
534 
535     btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_SERVER_MODEL_UPDATE_STATE_COMP_EVT);
536     return;
537 }
538 #endif /* CONFIG_BLE_MESH_SERVER_MODEL */
539 
btc_ble_mesh_prov_callback(esp_ble_mesh_prov_cb_param_t * param,uint8_t act)540 static bt_status_t btc_ble_mesh_prov_callback(esp_ble_mesh_prov_cb_param_t *param, uint8_t act)
541 {
542     btc_msg_t msg = {0};
543     bt_status_t ret = BT_STATUS_SUCCESS;
544 
545     BT_DBG("%s", __func__);
546 
547     /* If corresponding callback is not registered, event will not be posted. */
548     if (!btc_profile_cb_get(BTC_PID_PROV)) {
549         return BT_STATUS_SUCCESS;
550     }
551 
552     msg.sig = BTC_SIG_API_CB;
553     msg.pid = BTC_PID_PROV;
554     msg.act = act;
555 
556     ret = btc_transfer_context(&msg, param, param == NULL ? 0 : sizeof(esp_ble_mesh_prov_cb_param_t), NULL, NULL);
557     if (ret != BT_STATUS_SUCCESS) {
558         BT_ERR("btc_transfer_context failed");
559     }
560     return ret;
561 }
562 
563 #if CONFIG_BLE_MESH_NODE
btc_ble_mesh_oob_pub_key_cb(void)564 static void btc_ble_mesh_oob_pub_key_cb(void)
565 {
566     BT_DBG("%s", __func__);
567 
568     btc_ble_mesh_prov_callback(NULL, ESP_BLE_MESH_NODE_PROV_OOB_PUB_KEY_EVT);
569     return;
570 }
571 
btc_ble_mesh_output_number_cb(bt_mesh_output_action_t act,uint32_t num)572 static int btc_ble_mesh_output_number_cb(bt_mesh_output_action_t act, uint32_t num)
573 {
574     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
575     bt_status_t ret = BT_STATUS_SUCCESS;
576 
577     BT_DBG("%s", __func__);
578 
579     mesh_param.node_prov_output_num.action = (esp_ble_mesh_output_action_t)act;
580     mesh_param.node_prov_output_num.number = num;
581 
582     ret = btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_OUTPUT_NUMBER_EVT);
583     return (ret == BT_STATUS_SUCCESS) ? 0 : -1;
584 }
585 
btc_ble_mesh_output_string_cb(const char * str)586 static int btc_ble_mesh_output_string_cb(const char *str)
587 {
588     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
589     bt_status_t ret = BT_STATUS_SUCCESS;
590 
591     BT_DBG("%s", __func__);
592 
593     strncpy(mesh_param.node_prov_output_str.string, str,
594         MIN(strlen(str), sizeof(mesh_param.node_prov_output_str.string)));
595 
596     ret = btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_OUTPUT_STRING_EVT);
597     return (ret == BT_STATUS_SUCCESS) ? 0 : -1;
598 }
599 
btc_ble_mesh_input_cb(bt_mesh_input_action_t act,uint8_t size)600 static int btc_ble_mesh_input_cb(bt_mesh_input_action_t act, uint8_t size)
601 {
602     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
603     bt_status_t ret = BT_STATUS_SUCCESS;
604 
605     BT_DBG("%s", __func__);
606 
607     mesh_param.node_prov_input.action = (esp_ble_mesh_input_action_t)act;
608     mesh_param.node_prov_input.size = size;
609 
610     ret = btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_INPUT_EVT);
611     return (ret == BT_STATUS_SUCCESS) ? 0 : -1;
612 }
613 
btc_ble_mesh_link_open_cb(bt_mesh_prov_bearer_t bearer)614 static void btc_ble_mesh_link_open_cb(bt_mesh_prov_bearer_t bearer)
615 {
616     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
617 
618     BT_DBG("%s", __func__);
619 
620     mesh_param.node_prov_link_open.bearer = (esp_ble_mesh_prov_bearer_t)bearer;
621 
622     btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_LINK_OPEN_EVT);
623     return;
624 }
625 
btc_ble_mesh_link_close_cb(bt_mesh_prov_bearer_t bearer)626 static void btc_ble_mesh_link_close_cb(bt_mesh_prov_bearer_t bearer)
627 {
628     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
629 
630     BT_DBG("%s", __func__);
631 
632     mesh_param.node_prov_link_close.bearer = (esp_ble_mesh_prov_bearer_t)bearer;
633 
634     btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_LINK_CLOSE_EVT);
635     return;
636 }
637 
btc_ble_mesh_complete_cb(uint16_t net_idx,const uint8_t net_key[16],uint16_t addr,uint8_t flags,uint32_t iv_index)638 static void btc_ble_mesh_complete_cb(uint16_t net_idx, const uint8_t net_key[16],
639                                      uint16_t addr, uint8_t flags, uint32_t iv_index)
640 {
641     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
642 
643     BT_DBG("%s", __func__);
644 
645     mesh_param.node_prov_complete.net_idx = net_idx;
646     memcpy(mesh_param.node_prov_complete.net_key, net_key, 16);
647     mesh_param.node_prov_complete.addr = addr;
648     mesh_param.node_prov_complete.flags = flags;
649     mesh_param.node_prov_complete.iv_index = iv_index;
650 
651     btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_COMPLETE_EVT);
652     return;
653 }
654 
btc_ble_mesh_reset_cb(void)655 static void btc_ble_mesh_reset_cb(void)
656 {
657     BT_DBG("%s", __func__);
658 
659     btc_ble_mesh_prov_callback(NULL, ESP_BLE_MESH_NODE_PROV_RESET_EVT);
660     return;
661 }
662 
btc_ble_mesh_node_get_local_net_key(uint16_t net_idx)663 const uint8_t *btc_ble_mesh_node_get_local_net_key(uint16_t net_idx)
664 {
665     return bt_mesh_node_get_local_net_key(net_idx);
666 }
667 
btc_ble_mesh_node_get_local_app_key(uint16_t app_idx)668 const uint8_t *btc_ble_mesh_node_get_local_app_key(uint16_t app_idx)
669 {
670     return bt_mesh_node_get_local_app_key(app_idx);
671 }
672 #endif /* CONFIG_BLE_MESH_NODE */
673 
btc_ble_mesh_prov_register_complete_cb(int err_code)674 static void btc_ble_mesh_prov_register_complete_cb(int err_code)
675 {
676     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
677 
678     BT_DBG("%s", __func__);
679 
680     mesh_param.prov_register_comp.err_code = err_code;
681 
682     btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROV_REGISTER_COMP_EVT);
683     return;
684 }
685 
btc_ble_mesh_prov_set_complete_cb(esp_ble_mesh_prov_cb_param_t * param,uint8_t act)686 static void btc_ble_mesh_prov_set_complete_cb(esp_ble_mesh_prov_cb_param_t *param, uint8_t act)
687 {
688     BT_DBG("%s", __func__);
689 
690     btc_ble_mesh_prov_callback(param, act);
691     return;
692 }
693 
694 #if CONFIG_BLE_MESH_PROVISIONER
btc_ble_mesh_provisioner_recv_unprov_adv_pkt_cb(const uint8_t addr[6],const uint8_t addr_type,const uint8_t adv_type,const uint8_t dev_uuid[16],uint16_t oob_info,bt_mesh_prov_bearer_t bearer,int8_t rssi)695 static void btc_ble_mesh_provisioner_recv_unprov_adv_pkt_cb(const uint8_t addr[6], const uint8_t addr_type,
696                                                             const uint8_t adv_type, const uint8_t dev_uuid[16],
697                                                             uint16_t oob_info, bt_mesh_prov_bearer_t bearer,
698                                                             int8_t rssi)
699 {
700     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
701 
702     BT_DBG("%s", __func__);
703 
704     if (addr == NULL || dev_uuid == NULL ||
705             (bearer != BLE_MESH_PROV_ADV && bearer != BLE_MESH_PROV_GATT)) {
706         BT_ERR("%s, Invalid parameter", __func__);
707         return;
708     }
709 
710     memcpy(mesh_param.provisioner_recv_unprov_adv_pkt.dev_uuid, dev_uuid, 16);
711     memcpy(mesh_param.provisioner_recv_unprov_adv_pkt.addr, addr, BLE_MESH_ADDR_LEN);
712     mesh_param.provisioner_recv_unprov_adv_pkt.addr_type = addr_type;
713     mesh_param.provisioner_recv_unprov_adv_pkt.oob_info = oob_info;
714     mesh_param.provisioner_recv_unprov_adv_pkt.adv_type = adv_type;
715     mesh_param.provisioner_recv_unprov_adv_pkt.bearer = bearer;
716     mesh_param.provisioner_recv_unprov_adv_pkt.rssi = rssi;
717 
718     btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_RECV_UNPROV_ADV_PKT_EVT);
719     return;
720 }
721 
btc_ble_mesh_provisioner_prov_read_oob_pub_key_cb(uint8_t link_idx)722 static int btc_ble_mesh_provisioner_prov_read_oob_pub_key_cb(uint8_t link_idx)
723 {
724     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
725     bt_status_t ret = BT_STATUS_SUCCESS;
726 
727     BT_DBG("%s", __func__);
728 
729     mesh_param.provisioner_prov_read_oob_pub_key.link_idx = link_idx;
730 
731     ret = btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_READ_OOB_PUB_KEY_EVT);
732     return (ret == BT_STATUS_SUCCESS) ? 0 : -1;
733 }
734 
btc_ble_mesh_provisioner_prov_input_cb(uint8_t method,bt_mesh_output_action_t act,uint8_t size,uint8_t link_idx)735 static int btc_ble_mesh_provisioner_prov_input_cb(uint8_t method, bt_mesh_output_action_t act,
736                                                   uint8_t size, uint8_t link_idx)
737 {
738     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
739     bt_status_t ret = BT_STATUS_SUCCESS;
740 
741     BT_DBG("%s", __func__);
742 
743     mesh_param.provisioner_prov_input.method = (esp_ble_mesh_oob_method_t)method;
744     mesh_param.provisioner_prov_input.action = (esp_ble_mesh_output_action_t)act;
745     mesh_param.provisioner_prov_input.size = size;
746     mesh_param.provisioner_prov_input.link_idx = link_idx;
747 
748     ret = btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_INPUT_EVT);
749     return (ret == BT_STATUS_SUCCESS) ? 0 : -1;
750 }
751 
btc_ble_mesh_provisioner_prov_output_cb(uint8_t method,bt_mesh_input_action_t act,void * data,uint8_t size,uint8_t link_idx)752 static int btc_ble_mesh_provisioner_prov_output_cb(uint8_t method, bt_mesh_input_action_t act,
753                                                    void *data, uint8_t size, uint8_t link_idx)
754 {
755     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
756     bt_status_t ret = BT_STATUS_SUCCESS;
757 
758     BT_DBG("%s", __func__);
759 
760     mesh_param.provisioner_prov_output.method = (esp_ble_mesh_oob_method_t)method;
761     mesh_param.provisioner_prov_output.action = (esp_ble_mesh_input_action_t)act;
762     mesh_param.provisioner_prov_output.size = size;
763     mesh_param.provisioner_prov_output.link_idx = link_idx;
764     if (act == BLE_MESH_ENTER_STRING) {
765         strncpy(mesh_param.provisioner_prov_output.string, (char *)data, size);
766     } else {
767         mesh_param.provisioner_prov_output.number = sys_get_le32((uint8_t *)data);
768     }
769 
770     ret = btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_OUTPUT_EVT);
771     return (ret == BT_STATUS_SUCCESS) ? 0 : -1;
772 }
773 
btc_ble_mesh_provisioner_link_open_cb(bt_mesh_prov_bearer_t bearer)774 static void btc_ble_mesh_provisioner_link_open_cb(bt_mesh_prov_bearer_t bearer)
775 {
776     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
777 
778     BT_DBG("%s", __func__);
779 
780     mesh_param.provisioner_prov_link_open.bearer = (esp_ble_mesh_prov_bearer_t)bearer;
781 
782     btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_LINK_OPEN_EVT);
783     return;
784 }
785 
btc_ble_mesh_provisioner_link_close_cb(bt_mesh_prov_bearer_t bearer,uint8_t reason)786 static void btc_ble_mesh_provisioner_link_close_cb(bt_mesh_prov_bearer_t bearer, uint8_t reason)
787 {
788     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
789 
790     BT_DBG("%s", __func__);
791 
792     mesh_param.provisioner_prov_link_close.bearer = (esp_ble_mesh_prov_bearer_t)bearer;
793     mesh_param.provisioner_prov_link_close.reason = reason;
794 
795     btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_LINK_CLOSE_EVT);
796     return;
797 }
798 
btc_ble_mesh_provisioner_prov_complete_cb(uint16_t node_idx,const uint8_t device_uuid[16],uint16_t unicast_addr,uint8_t element_num,uint16_t netkey_idx)799 static void btc_ble_mesh_provisioner_prov_complete_cb(uint16_t node_idx, const uint8_t device_uuid[16],
800                                                       uint16_t unicast_addr, uint8_t element_num,
801                                                       uint16_t netkey_idx)
802 {
803     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
804 
805     BT_DBG("%s", __func__);
806 
807     mesh_param.provisioner_prov_complete.node_idx = node_idx;
808     mesh_param.provisioner_prov_complete.unicast_addr = unicast_addr;
809     mesh_param.provisioner_prov_complete.element_num = element_num;
810     mesh_param.provisioner_prov_complete.netkey_idx = netkey_idx;
811     memcpy(mesh_param.provisioner_prov_complete.device_uuid, device_uuid, sizeof(esp_ble_mesh_octet16_t));
812 
813     btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_COMPLETE_EVT);
814     return;
815 }
816 
btc_ble_mesh_provisioner_get_node_with_uuid(const uint8_t uuid[16])817 esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_uuid(const uint8_t uuid[16])
818 {
819     return (esp_ble_mesh_node_t *)bt_mesh_provisioner_get_node_with_uuid(uuid);
820 }
821 
btc_ble_mesh_provisioner_get_node_with_addr(uint16_t unicast_addr)822 esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_addr(uint16_t unicast_addr)
823 {
824     return (esp_ble_mesh_node_t *)bt_mesh_provisioner_get_node_with_addr(unicast_addr);
825 }
826 
btc_ble_mesh_provisioner_get_node_with_name(const char * name)827 esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_name(const char *name)
828 {
829     return (esp_ble_mesh_node_t *)bt_mesh_provisioner_get_node_with_name(name);
830 }
831 
btc_ble_mesh_provisioner_get_prov_node_count(void)832 uint16_t btc_ble_mesh_provisioner_get_prov_node_count(void)
833 {
834     return bt_mesh_provisioner_get_node_count();
835 }
836 
btc_ble_mesh_provisioner_get_node_table_entry(void)837 const esp_ble_mesh_node_t **btc_ble_mesh_provisioner_get_node_table_entry(void)
838 {
839     return (const esp_ble_mesh_node_t **)bt_mesh_provisioner_get_node_table_entry();
840 }
841 
842 #if CONFIG_BLE_MESH_PROVISIONER_RECV_HB
btc_ble_mesh_provisioner_recv_heartbeat_cb(uint16_t hb_src,uint16_t hb_dst,uint8_t init_ttl,uint8_t rx_ttl,uint8_t hops,uint16_t feat,int8_t rssi)843 static void btc_ble_mesh_provisioner_recv_heartbeat_cb(uint16_t hb_src, uint16_t hb_dst,
844                                                        uint8_t init_ttl, uint8_t rx_ttl,
845                                                        uint8_t hops, uint16_t feat, int8_t rssi)
846 {
847     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
848 
849     mesh_param.provisioner_recv_heartbeat.hb_src = hb_src;
850     mesh_param.provisioner_recv_heartbeat.hb_dst = hb_dst;
851     mesh_param.provisioner_recv_heartbeat.init_ttl = init_ttl;
852     mesh_param.provisioner_recv_heartbeat.rx_ttl = rx_ttl;
853     mesh_param.provisioner_recv_heartbeat.hops = hops;
854     mesh_param.provisioner_recv_heartbeat.feature = feat;
855     mesh_param.provisioner_recv_heartbeat.rssi = rssi;
856 
857     btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_RECV_HEARTBEAT_MESSAGE_EVT);
858 }
859 #endif /* CONFIG_BLE_MESH_PROVISIONER_RECV_HB */
860 
861 #if CONFIG_BLE_MESH_USE_MULTIPLE_NAMESPACE
btc_ble_mesh_provisioner_get_settings_uid(uint8_t index)862 const char *btc_ble_mesh_provisioner_get_settings_uid(uint8_t index)
863 {
864     return bt_mesh_provisioner_get_settings_uid(index);
865 }
866 
btc_ble_mesh_provisioner_get_settings_index(const char * uid)867 uint8_t btc_ble_mesh_provisioner_get_settings_index(const char *uid)
868 {
869     return bt_mesh_provisioner_get_settings_index(uid);
870 }
871 
btc_ble_mesh_provisioner_get_free_settings_count(void)872 uint8_t btc_ble_mesh_provisioner_get_free_settings_count(void)
873 {
874     return bt_mesh_provisioner_get_free_settings_count();
875 }
876 #endif /* CONFIG_BLE_MESH_USE_MULTIPLE_NAMESPACE */
877 
878 #endif /* CONFIG_BLE_MESH_PROVISIONER */
879 
btc_ble_mesh_node_recv_heartbeat_cb(uint8_t hops,uint16_t feature)880 static void btc_ble_mesh_node_recv_heartbeat_cb(uint8_t hops, uint16_t feature)
881 {
882     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
883 
884     BT_DBG("%s", __func__);
885 
886     mesh_param.heartbeat_msg_recv.hops = hops;
887     mesh_param.heartbeat_msg_recv.feature = feature;
888 
889     btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_HEARTBEAT_MESSAGE_RECV_EVT);
890     return;
891 }
892 
893 #if CONFIG_BLE_MESH_LOW_POWER
btc_ble_mesh_lpn_cb(uint16_t friend_addr,bool established)894 static void btc_ble_mesh_lpn_cb(uint16_t friend_addr, bool established)
895 {
896     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
897     uint8_t act = 0U;
898 
899     BT_DBG("%s", __func__);
900 
901     if (established) {
902         mesh_param.lpn_friendship_establish.friend_addr = friend_addr;
903         act = ESP_BLE_MESH_LPN_FRIENDSHIP_ESTABLISH_EVT;
904     } else {
905         mesh_param.lpn_friendship_terminate.friend_addr = friend_addr;
906         act = ESP_BLE_MESH_LPN_FRIENDSHIP_TERMINATE_EVT;
907     }
908 
909     btc_ble_mesh_prov_callback(&mesh_param, act);
910     return;
911 }
912 #endif /* CONFIG_BLE_MESH_LOW_POWER */
913 
914 #if CONFIG_BLE_MESH_FRIEND
btc_ble_mesh_friend_cb(bool establish,uint16_t lpn_addr,uint8_t reason)915 void btc_ble_mesh_friend_cb(bool establish, uint16_t lpn_addr, uint8_t reason)
916 {
917     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
918     uint8_t act = 0U;
919 
920     BT_DBG("%s", __func__);
921 
922     if (!BLE_MESH_ADDR_IS_UNICAST(lpn_addr)) {
923         BT_ERR("Not a unicast lpn address 0x%04x", lpn_addr);
924         return;
925     }
926 
927     if (establish) {
928         mesh_param.frnd_friendship_establish.lpn_addr = lpn_addr;
929         act = ESP_BLE_MESH_FRIEND_FRIENDSHIP_ESTABLISH_EVT;
930     } else {
931         mesh_param.frnd_friendship_terminate.lpn_addr = lpn_addr;
932         mesh_param.frnd_friendship_terminate.reason = reason;
933         act = ESP_BLE_MESH_FRIEND_FRIENDSHIP_TERMINATE_EVT;
934     }
935 
936     btc_ble_mesh_prov_callback(&mesh_param, act);
937     return;
938 }
939 #endif /* CONFIG_BLE_MESH_FRIEND */
940 
941 #if CONFIG_BLE_MESH_GATT_PROXY_CLIENT
btc_ble_mesh_proxy_client_adv_recv_cb(const bt_mesh_addr_t * addr,uint8_t type,bt_mesh_proxy_adv_ctx_t * ctx,int8_t rssi)942 static void btc_ble_mesh_proxy_client_adv_recv_cb(const bt_mesh_addr_t *addr, uint8_t type,
943                                                   bt_mesh_proxy_adv_ctx_t *ctx, int8_t rssi)
944 {
945     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
946 
947     if (!addr || !ctx || type != BLE_MESH_PROXY_ADV_NET_ID) {
948         BT_ERR("%s, Invalid parameter", __func__);
949         return;
950     }
951 
952     BT_DBG("%s", __func__);
953 
954     mesh_param.proxy_client_recv_adv_pkt.addr_type = addr->type;
955     memcpy(mesh_param.proxy_client_recv_adv_pkt.addr, addr->val, BD_ADDR_LEN);
956     mesh_param.proxy_client_recv_adv_pkt.net_idx = ctx->net_id.net_idx;
957     memcpy(mesh_param.proxy_client_recv_adv_pkt.net_id, ctx->net_id.net_id, 8);
958     mesh_param.proxy_client_recv_adv_pkt.rssi = rssi;
959 
960     btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROXY_CLIENT_RECV_ADV_PKT_EVT);
961     return;
962 }
963 
btc_ble_mesh_proxy_client_connect_cb(const bt_mesh_addr_t * addr,uint8_t conn_handle,uint16_t net_idx)964 static void btc_ble_mesh_proxy_client_connect_cb(const bt_mesh_addr_t *addr,
965                                                  uint8_t conn_handle, uint16_t net_idx)
966 {
967     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
968 
969     if (!addr || conn_handle >= BLE_MESH_MAX_CONN) {
970         BT_ERR("%s, Invalid parameter", __func__);
971         return;
972     }
973 
974     BT_DBG("%s", __func__);
975 
976     mesh_param.proxy_client_connected.addr_type = addr->type;
977     memcpy(mesh_param.proxy_client_connected.addr, addr->val, BD_ADDR_LEN);
978     mesh_param.proxy_client_connected.conn_handle = conn_handle;
979     mesh_param.proxy_client_connected.net_idx = net_idx;
980 
981     btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROXY_CLIENT_CONNECTED_EVT);
982     return;
983 }
984 
btc_ble_mesh_proxy_client_disconnect_cb(const bt_mesh_addr_t * addr,uint8_t conn_handle,uint16_t net_idx,uint8_t reason)985 static void btc_ble_mesh_proxy_client_disconnect_cb(const bt_mesh_addr_t *addr, uint8_t conn_handle,
986                                                     uint16_t net_idx, uint8_t reason)
987 {
988     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
989 
990     if (!addr || conn_handle >= BLE_MESH_MAX_CONN) {
991         BT_ERR("%s, Invalid parameter", __func__);
992         return;
993     }
994 
995     BT_DBG("%s", __func__);
996 
997     mesh_param.proxy_client_disconnected.addr_type = addr->type;
998     memcpy(mesh_param.proxy_client_disconnected.addr, addr->val, BD_ADDR_LEN);
999     mesh_param.proxy_client_disconnected.conn_handle = conn_handle;
1000     mesh_param.proxy_client_disconnected.net_idx = net_idx;
1001     mesh_param.proxy_client_disconnected.reason = reason;
1002 
1003     btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROXY_CLIENT_DISCONNECTED_EVT);
1004     return;
1005 }
1006 
btc_ble_mesh_proxy_client_filter_status_recv_cb(uint8_t conn_handle,uint16_t src,uint16_t net_idx,uint8_t filter_type,uint16_t list_size)1007 static void btc_ble_mesh_proxy_client_filter_status_recv_cb(uint8_t conn_handle, uint16_t src, uint16_t net_idx,
1008                                                             uint8_t filter_type, uint16_t list_size)
1009 {
1010     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
1011 
1012     if (conn_handle >= BLE_MESH_MAX_CONN) {
1013         BT_ERR("%s, Invalid parameter", __func__);
1014         return;
1015     }
1016 
1017     BT_DBG("%s", __func__);
1018 
1019     mesh_param.proxy_client_recv_filter_status.conn_handle = conn_handle;
1020     mesh_param.proxy_client_recv_filter_status.server_addr = src;
1021     mesh_param.proxy_client_recv_filter_status.net_idx = net_idx;
1022     mesh_param.proxy_client_recv_filter_status.filter_type = filter_type;
1023     mesh_param.proxy_client_recv_filter_status.list_size = list_size;
1024 
1025     btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROXY_CLIENT_RECV_FILTER_STATUS_EVT);
1026     return;
1027 }
1028 #endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */
1029 
1030 #if CONFIG_BLE_MESH_GATT_PROXY_SERVER
btc_ble_mesh_proxy_server_connect_cb(uint8_t conn_handle)1031 static void btc_ble_mesh_proxy_server_connect_cb(uint8_t conn_handle)
1032 {
1033     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
1034 
1035     if (conn_handle >= BLE_MESH_MAX_CONN) {
1036         BT_ERR("%s, Invalid parameter", __func__);
1037         return;
1038     }
1039 
1040     BT_DBG("%s", __func__);
1041 
1042     mesh_param.proxy_server_connected.conn_handle = conn_handle;
1043 
1044     btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROXY_SERVER_CONNECTED_EVT);
1045 }
1046 
btc_ble_mesh_proxy_server_disconnect_cb(uint8_t conn_handle,uint8_t reason)1047 static void btc_ble_mesh_proxy_server_disconnect_cb(uint8_t conn_handle, uint8_t reason)
1048 {
1049     esp_ble_mesh_prov_cb_param_t mesh_param = {0};
1050 
1051     if (conn_handle >= BLE_MESH_MAX_CONN) {
1052         BT_ERR("%s, Invalid parameter", __func__);
1053         return;
1054     }
1055 
1056     BT_DBG("%s", __func__);
1057 
1058     mesh_param.proxy_server_disconnected.conn_handle = conn_handle;
1059     mesh_param.proxy_server_disconnected.reason = reason;
1060 
1061     btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROXY_SERVER_DISCONNECTED_EVT);
1062 }
1063 #endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */
1064 
btc_ble_mesh_client_model_init(esp_ble_mesh_model_t * model)1065 int btc_ble_mesh_client_model_init(esp_ble_mesh_model_t *model)
1066 {
1067     if (!bt_mesh_is_initialized()) {
1068         BT_ERR("Mesh stack is not initialized!");
1069         return -EINVAL;
1070     }
1071 
1072     __ASSERT(model && model->op, "Invalid parameter");
1073     esp_ble_mesh_model_op_t *op = model->op;
1074     while (op != NULL && op->opcode != 0) {
1075         op->param_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_client_model_op_cb;
1076         op++;
1077     }
1078     return bt_mesh_client_init((struct bt_mesh_model *)model);
1079 }
1080 
1081 #if CONFIG_BLE_MESH_DEINIT
btc_ble_mesh_client_model_deinit(esp_ble_mesh_model_t * model)1082 int btc_ble_mesh_client_model_deinit(esp_ble_mesh_model_t *model)
1083 {
1084     return bt_mesh_client_deinit((struct bt_mesh_model *)model);
1085 }
1086 #endif /* CONFIG_BLE_MESH_DEINIT */
1087 
btc_ble_mesh_model_pub_period_get(esp_ble_mesh_model_t * mod)1088 int32_t btc_ble_mesh_model_pub_period_get(esp_ble_mesh_model_t *mod)
1089 {
1090     return bt_mesh_model_pub_period_get((struct bt_mesh_model *)mod);
1091 }
1092 
btc_ble_mesh_get_primary_addr(void)1093 uint16_t btc_ble_mesh_get_primary_addr(void)
1094 {
1095     return bt_mesh_primary_addr();
1096 }
1097 
btc_ble_mesh_model_find_group(esp_ble_mesh_model_t * mod,uint16_t addr)1098 uint16_t *btc_ble_mesh_model_find_group(esp_ble_mesh_model_t *mod, uint16_t addr)
1099 {
1100     return bt_mesh_model_find_group((struct bt_mesh_model *)mod, addr);
1101 }
1102 
btc_ble_mesh_elem_find(uint16_t addr)1103 esp_ble_mesh_elem_t *btc_ble_mesh_elem_find(uint16_t addr)
1104 {
1105     return (esp_ble_mesh_elem_t *)bt_mesh_elem_find(addr);
1106 }
1107 
btc_ble_mesh_elem_count(void)1108 uint8_t btc_ble_mesh_elem_count(void)
1109 {
1110     return bt_mesh_elem_count();
1111 }
1112 
btc_ble_mesh_model_find_vnd(const esp_ble_mesh_elem_t * elem,uint16_t company,uint16_t id)1113 esp_ble_mesh_model_t *btc_ble_mesh_model_find_vnd(const esp_ble_mesh_elem_t *elem,
1114                                                   uint16_t company, uint16_t id)
1115 {
1116     return (esp_ble_mesh_model_t *)bt_mesh_model_find_vnd((struct bt_mesh_elem *)elem, company, id);
1117 }
1118 
btc_ble_mesh_model_find(const esp_ble_mesh_elem_t * elem,uint16_t id)1119 esp_ble_mesh_model_t *btc_ble_mesh_model_find(const esp_ble_mesh_elem_t *elem, uint16_t id)
1120 {
1121     return (esp_ble_mesh_model_t *)bt_mesh_model_find((struct bt_mesh_elem *)elem, id);
1122 }
1123 
btc_ble_mesh_comp_get(void)1124 const esp_ble_mesh_comp_t *btc_ble_mesh_comp_get(void)
1125 {
1126     return (const esp_ble_mesh_comp_t *)bt_mesh_comp_get();
1127 }
1128 
1129 /* Configuration Models */
1130 extern const struct bt_mesh_model_op bt_mesh_cfg_srv_op[];
1131 extern const struct bt_mesh_model_cb bt_mesh_cfg_srv_cb;
1132 #if CONFIG_BLE_MESH_CFG_CLI
1133 extern const struct bt_mesh_model_op bt_mesh_cfg_cli_op[];
1134 extern const struct bt_mesh_model_cb bt_mesh_cfg_cli_cb;
1135 #endif /* CONFIG_BLE_MESH_CFG_CLI */
1136 
1137 /* Health Models */
1138 #if CONFIG_BLE_MESH_HEALTH_SRV
1139 extern const struct bt_mesh_model_op bt_mesh_health_srv_op[];
1140 extern const struct bt_mesh_model_cb bt_mesh_health_srv_cb;
1141 #endif /* CONFIG_BLE_MESH_HEALTH_SRV */
1142 #if CONFIG_BLE_MESH_HEALTH_CLI
1143 extern const struct bt_mesh_model_op bt_mesh_health_cli_op[];
1144 extern const struct bt_mesh_model_cb bt_mesh_health_cli_cb;
1145 #endif /* CONFIG_BLE_MESH_HEALTH_CLI */
1146 
1147 /* Generic Client Models */
1148 #if CONFIG_BLE_MESH_GENERIC_CLIENT
1149 extern const struct bt_mesh_model_op bt_mesh_gen_onoff_cli_op[];
1150 extern const struct bt_mesh_model_op bt_mesh_gen_level_cli_op[];
1151 extern const struct bt_mesh_model_op bt_mesh_gen_def_trans_time_cli_op[];
1152 extern const struct bt_mesh_model_op bt_mesh_gen_power_onoff_cli_op[];
1153 extern const struct bt_mesh_model_op bt_mesh_gen_power_level_cli_op[];
1154 extern const struct bt_mesh_model_op bt_mesh_gen_battery_cli_op[];
1155 extern const struct bt_mesh_model_op bt_mesh_gen_location_cli_op[];
1156 extern const struct bt_mesh_model_op bt_mesh_gen_property_cli_op[];
1157 extern const struct bt_mesh_model_cb bt_mesh_generic_client_cb;
1158 #endif /* CONFIG_BLE_MESH_GENERIC_CLIENT */
1159 
1160 /* Lighting Client Models */
1161 #if CONFIG_BLE_MESH_LIGHTING_CLIENT
1162 extern const struct bt_mesh_model_op bt_mesh_light_lightness_cli_op[];
1163 extern const struct bt_mesh_model_op bt_mesh_light_ctl_cli_op[];
1164 extern const struct bt_mesh_model_op bt_mesh_light_hsl_cli_op[];
1165 extern const struct bt_mesh_model_op bt_mesh_light_xyl_cli_op[];
1166 extern const struct bt_mesh_model_op bt_mesh_light_lc_cli_op[];
1167 extern const struct bt_mesh_model_cb bt_mesh_lighting_client_cb;
1168 #endif /* CONFIG_BLE_MESH_LIGHTING_CLIENT */
1169 
1170 /* Sensor Client Models */
1171 #if CONFIG_BLE_MESH_SENSOR_CLI
1172 extern const struct bt_mesh_model_op bt_mesh_sensor_cli_op[];
1173 extern const struct bt_mesh_model_cb bt_mesh_sensor_client_cb;
1174 #endif /* CONFIG_BLE_MESH_SENSOR_CLI */
1175 
1176 /* Time and Scenes Client Models */
1177 #if CONFIG_BLE_MESH_TIME_SCENE_CLIENT
1178 extern const struct bt_mesh_model_op bt_mesh_time_cli_op[];
1179 extern const struct bt_mesh_model_op bt_mesh_scene_cli_op[];
1180 extern const struct bt_mesh_model_op bt_mesh_scheduler_cli_op[];
1181 extern const struct bt_mesh_model_cb bt_mesh_time_scene_client_cb;
1182 #endif /* CONFIG_BLE_MESH_TIME_SCENE_CLIENT */
1183 
1184 /* Generic Server Models */
1185 #if CONFIG_BLE_MESH_GENERIC_SERVER
1186 extern const struct bt_mesh_model_op bt_mesh_gen_onoff_srv_op[];
1187 extern const struct bt_mesh_model_op bt_mesh_gen_level_srv_op[];
1188 extern const struct bt_mesh_model_op bt_mesh_gen_def_trans_time_srv_op[];
1189 extern const struct bt_mesh_model_op bt_mesh_gen_power_onoff_srv_op[];
1190 extern const struct bt_mesh_model_op bt_mesh_gen_power_onoff_setup_srv_op[];
1191 extern const struct bt_mesh_model_op bt_mesh_gen_power_level_srv_op[];
1192 extern const struct bt_mesh_model_op bt_mesh_gen_power_level_setup_srv_op[];
1193 extern const struct bt_mesh_model_op bt_mesh_gen_battery_srv_op[];
1194 extern const struct bt_mesh_model_op bt_mesh_gen_location_srv_op[];
1195 extern const struct bt_mesh_model_op bt_mesh_gen_location_setup_srv_op[];
1196 extern const struct bt_mesh_model_op bt_mesh_gen_user_prop_srv_op[];
1197 extern const struct bt_mesh_model_op bt_mesh_gen_admin_prop_srv_op[];
1198 extern const struct bt_mesh_model_op bt_mesh_gen_manu_prop_srv_op[];
1199 extern const struct bt_mesh_model_op bt_mesh_gen_client_prop_srv_op[];
1200 extern const struct bt_mesh_model_cb bt_mesh_gen_onoff_srv_cb;
1201 extern const struct bt_mesh_model_cb bt_mesh_gen_level_srv_cb;
1202 extern const struct bt_mesh_model_cb bt_mesh_gen_def_trans_time_srv_cb;
1203 extern const struct bt_mesh_model_cb bt_mesh_gen_power_onoff_srv_cb;
1204 extern const struct bt_mesh_model_cb bt_mesh_gen_power_onoff_setup_srv_cb;
1205 extern const struct bt_mesh_model_cb bt_mesh_gen_power_level_srv_cb;
1206 extern const struct bt_mesh_model_cb bt_mesh_gen_power_level_setup_srv_cb;
1207 extern const struct bt_mesh_model_cb bt_mesh_gen_battery_srv_cb;
1208 extern const struct bt_mesh_model_cb bt_mesh_gen_location_srv_cb;
1209 extern const struct bt_mesh_model_cb bt_mesh_gen_location_setup_srv_cb;
1210 extern const struct bt_mesh_model_cb bt_mesh_gen_user_prop_srv_cb;
1211 extern const struct bt_mesh_model_cb bt_mesh_gen_admin_prop_srv_cb;
1212 extern const struct bt_mesh_model_cb bt_mesh_gen_manu_prop_srv_cb;
1213 extern const struct bt_mesh_model_cb bt_mesh_gen_client_prop_srv_cb;
1214 #endif /* CONFIG_BLE_MESH_GENERIC_SERVER */
1215 
1216 /* Lighting Server Models */
1217 #if CONFIG_BLE_MESH_LIGHTING_SERVER
1218 extern const struct bt_mesh_model_op bt_mesh_light_lightness_srv_op[];
1219 extern const struct bt_mesh_model_op bt_mesh_light_lightness_setup_srv_op[];
1220 extern const struct bt_mesh_model_op bt_mesh_light_ctl_srv_op[];
1221 extern const struct bt_mesh_model_op bt_mesh_light_ctl_setup_srv_op[];
1222 extern const struct bt_mesh_model_op bt_mesh_light_ctl_temp_srv_op[];
1223 extern const struct bt_mesh_model_op bt_mesh_light_hsl_srv_op[];
1224 extern const struct bt_mesh_model_op bt_mesh_light_hsl_hue_srv_op[];
1225 extern const struct bt_mesh_model_op bt_mesh_light_hsl_sat_srv_op[];
1226 extern const struct bt_mesh_model_op bt_mesh_light_hsl_setup_srv_op[];
1227 extern const struct bt_mesh_model_op bt_mesh_light_xyl_srv_op[];
1228 extern const struct bt_mesh_model_op bt_mesh_light_xyl_setup_srv_op[];
1229 extern const struct bt_mesh_model_cb bt_mesh_light_lightness_srv_cb;
1230 extern const struct bt_mesh_model_cb bt_mesh_light_lightness_setup_srv_cb;
1231 extern const struct bt_mesh_model_cb bt_mesh_light_ctl_srv_cb;
1232 extern const struct bt_mesh_model_cb bt_mesh_light_ctl_setup_srv_cb;
1233 extern const struct bt_mesh_model_cb bt_mesh_light_ctl_temp_srv_cb;
1234 extern const struct bt_mesh_model_cb bt_mesh_light_hsl_srv_cb;
1235 extern const struct bt_mesh_model_cb bt_mesh_light_hsl_hue_srv_cb;
1236 extern const struct bt_mesh_model_cb bt_mesh_light_hsl_sat_srv_cb;
1237 extern const struct bt_mesh_model_cb bt_mesh_light_hsl_setup_srv_cb;
1238 extern const struct bt_mesh_model_cb bt_mesh_light_xyl_srv_cb;
1239 extern const struct bt_mesh_model_cb bt_mesh_light_xyl_setup_srv_cb;
1240 extern const struct bt_mesh_model_op bt_mesh_light_lc_srv_op[];
1241 extern const struct bt_mesh_model_op bt_mesh_light_lc_setup_srv_op[];
1242 extern const struct bt_mesh_model_cb bt_mesh_light_lc_srv_cb;
1243 extern const struct bt_mesh_model_cb bt_mesh_light_lc_setup_srv_cb;
1244 #endif /* CONFIG_BLE_MESH_LIGHTING_SERVER */
1245 
1246 /* Time and Scenes Server Models */
1247 #if CONFIG_BLE_MESH_TIME_SCENE_SERVER
1248 extern const struct bt_mesh_model_op bt_mesh_time_srv_op[];
1249 extern const struct bt_mesh_model_op bt_mesh_time_setup_srv_op[];
1250 extern const struct bt_mesh_model_op bt_mesh_scene_srv_op[];
1251 extern const struct bt_mesh_model_op bt_mesh_scene_setup_srv_op[];
1252 extern const struct bt_mesh_model_op bt_mesh_scheduler_srv_op[];
1253 extern const struct bt_mesh_model_op bt_mesh_scheduler_setup_srv_op[];
1254 extern const struct bt_mesh_model_cb bt_mesh_time_srv_cb;
1255 extern const struct bt_mesh_model_cb bt_mesh_time_setup_srv_cb;
1256 extern const struct bt_mesh_model_cb bt_mesh_scene_srv_cb;
1257 extern const struct bt_mesh_model_cb bt_mesh_scene_setup_srv_cb;
1258 extern const struct bt_mesh_model_cb bt_mesh_scheduler_srv_cb;
1259 extern const struct bt_mesh_model_cb bt_mesh_scheduler_setup_srv_cb;
1260 #endif /* CONFIG_BLE_MESH_TIME_SCENE_SERVER */
1261 
1262 /* Sensor Server Models */
1263 #if CONFIG_BLE_MESH_SENSOR_SERVER
1264 extern const struct bt_mesh_model_op bt_mesh_sensor_srv_op[];
1265 extern const struct bt_mesh_model_op bt_mesh_sensor_setup_srv_op[];
1266 extern const struct bt_mesh_model_cb bt_mesh_sensor_srv_cb;
1267 extern const struct bt_mesh_model_cb bt_mesh_sensor_setup_srv_cb;
1268 #endif /* CONFIG_BLE_MESH_SENSOR_SERVER */
1269 
btc_ble_mesh_model_op_set(esp_ble_mesh_model_t * model)1270 static void btc_ble_mesh_model_op_set(esp_ble_mesh_model_t *model)
1271 {
1272     if (!model) {
1273         BT_ERR("%s, Invalid parameter", __func__);
1274         return;
1275     }
1276 
1277     /* For SIG client and server models, model->op will be NULL and initialized here.
1278      * For vendor models whose opcode is 3 bytes, model->op will be initialized here.
1279      */
1280     if (model->op && BLE_MESH_MODEL_OP_LEN(model->op->opcode) == 3) {
1281         goto set_vnd_op;
1282     }
1283 
1284     switch (model->model_id) {
1285     case BLE_MESH_MODEL_ID_CFG_SRV: {
1286         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_cfg_srv_op;
1287         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_cfg_srv_cb;
1288         struct bt_mesh_cfg_srv *srv = (struct bt_mesh_cfg_srv *)model->user_data;
1289         if (srv) {
1290             srv->hb_sub.func = btc_ble_mesh_node_recv_heartbeat_cb;
1291         }
1292         break;
1293     }
1294 #if CONFIG_BLE_MESH_CFG_CLI
1295     case BLE_MESH_MODEL_ID_CFG_CLI: {
1296         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_cfg_cli_op;
1297         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_cfg_cli_cb;
1298         bt_mesh_config_client_t *cli = (bt_mesh_config_client_t *)model->user_data;
1299         if (cli != NULL) {
1300             cli->publish_status = btc_ble_mesh_config_client_publish_callback;
1301         }
1302         break;
1303     }
1304 #endif /* CONFIG_BLE_MESH_CFG_CLI */
1305 #if CONFIG_BLE_MESH_HEALTH_SRV
1306     case BLE_MESH_MODEL_ID_HEALTH_SRV: {
1307         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_health_srv_op;
1308         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_health_srv_cb;
1309         struct bt_mesh_health_srv *srv = (struct bt_mesh_health_srv *)model->user_data;
1310         if (srv) {
1311             srv->cb.fault_clear = btc_ble_mesh_health_server_fault_clear;
1312             srv->cb.fault_test = btc_ble_mesh_health_server_fault_test;
1313             srv->cb.attn_on = btc_ble_mesh_health_server_attention_on;
1314             srv->cb.attn_off = btc_ble_mesh_health_server_attention_off;
1315         }
1316         break;
1317     }
1318 #endif /* CONFIG_BLE_MESH_HEALTH_SRV */
1319 #if CONFIG_BLE_MESH_HEALTH_CLI
1320     case BLE_MESH_MODEL_ID_HEALTH_CLI: {
1321         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_health_cli_op;
1322         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_health_cli_cb;
1323         bt_mesh_health_client_t *cli = (bt_mesh_health_client_t *)model->user_data;
1324         if (cli != NULL) {
1325             cli->publish_status = btc_ble_mesh_health_publish_callback;
1326         }
1327         break;
1328     }
1329 #endif /* CONFIG_BLE_MESH_HEALTH_CLI */
1330 #if CONFIG_BLE_MESH_GENERIC_CLIENT
1331     case BLE_MESH_MODEL_ID_GEN_ONOFF_CLI: {
1332         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_onoff_cli_op;
1333         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_generic_client_cb;
1334         bt_mesh_gen_onoff_client_t *cli = (bt_mesh_gen_onoff_client_t *)model->user_data;
1335         if (cli != NULL) {
1336             cli->publish_status = btc_ble_mesh_generic_client_publish_callback;
1337         }
1338         break;
1339     }
1340     case BLE_MESH_MODEL_ID_GEN_LEVEL_CLI: {
1341         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_level_cli_op;
1342         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_generic_client_cb;
1343         bt_mesh_gen_level_client_t *cli = (bt_mesh_gen_level_client_t *)model->user_data;
1344         if (cli != NULL) {
1345             cli->publish_status = btc_ble_mesh_generic_client_publish_callback;
1346         }
1347         break;
1348     }
1349     case BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_CLI: {
1350         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_def_trans_time_cli_op;
1351         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_generic_client_cb;
1352         bt_mesh_gen_def_trans_time_client_t *cli = (bt_mesh_gen_def_trans_time_client_t *)model->user_data;
1353         if (cli != NULL) {
1354             cli->publish_status = btc_ble_mesh_generic_client_publish_callback;
1355         }
1356         break;
1357     }
1358     case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_CLI: {
1359         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_power_onoff_cli_op;
1360         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_generic_client_cb;
1361         bt_mesh_gen_power_onoff_client_t *cli = (bt_mesh_gen_power_onoff_client_t *)model->user_data;
1362         if (cli != NULL) {
1363             cli->publish_status = btc_ble_mesh_generic_client_publish_callback;
1364         }
1365         break;
1366     }
1367     case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_CLI: {
1368         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_power_level_cli_op;
1369         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_generic_client_cb;
1370         bt_mesh_gen_power_level_client_t *cli = (bt_mesh_gen_power_level_client_t *)model->user_data;
1371         if (cli != NULL) {
1372             cli->publish_status = btc_ble_mesh_generic_client_publish_callback;
1373         }
1374         break;
1375     }
1376     case BLE_MESH_MODEL_ID_GEN_BATTERY_CLI: {
1377         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_battery_cli_op;
1378         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_generic_client_cb;
1379         bt_mesh_gen_battery_client_t *cli = (bt_mesh_gen_battery_client_t *)model->user_data;
1380         if (cli != NULL) {
1381             cli->publish_status = btc_ble_mesh_generic_client_publish_callback;
1382         }
1383         break;
1384     }
1385     case BLE_MESH_MODEL_ID_GEN_LOCATION_CLI: {
1386         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_location_cli_op;
1387         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_generic_client_cb;
1388         bt_mesh_gen_location_client_t *cli = (bt_mesh_gen_location_client_t *)model->user_data;
1389         if (cli != NULL) {
1390             cli->publish_status = btc_ble_mesh_generic_client_publish_callback;
1391         }
1392         break;
1393     }
1394     case BLE_MESH_MODEL_ID_GEN_PROP_CLI: {
1395         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_property_cli_op;
1396         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_generic_client_cb;
1397         bt_mesh_gen_property_client_t *cli = (bt_mesh_gen_property_client_t *)model->user_data;
1398         if (cli != NULL) {
1399             cli->publish_status = btc_ble_mesh_generic_client_publish_callback;
1400         }
1401         break;
1402     }
1403 #endif /* CONFIG_BLE_MESH_GENERIC_CLIENT */
1404 #if CONFIG_BLE_MESH_LIGHTING_CLIENT
1405     case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_CLI: {
1406         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_lightness_cli_op;
1407         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_lighting_client_cb;
1408         bt_mesh_light_lightness_client_t *cli = (bt_mesh_light_lightness_client_t *)model->user_data;
1409         if (cli != NULL) {
1410             cli->publish_status = btc_ble_mesh_lighting_client_publish_callback;
1411         }
1412         break;
1413     }
1414     case BLE_MESH_MODEL_ID_LIGHT_CTL_CLI: {
1415         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_ctl_cli_op;
1416         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_lighting_client_cb;
1417         bt_mesh_light_ctl_client_t *cli = (bt_mesh_light_ctl_client_t *)model->user_data;
1418         if (cli != NULL) {
1419             cli->publish_status = btc_ble_mesh_lighting_client_publish_callback;
1420         }
1421         break;
1422     }
1423     case BLE_MESH_MODEL_ID_LIGHT_HSL_CLI: {
1424         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_hsl_cli_op;
1425         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_lighting_client_cb;
1426         bt_mesh_light_hsl_client_t *cli = (bt_mesh_light_hsl_client_t *)model->user_data;
1427         if (cli != NULL) {
1428             cli->publish_status = btc_ble_mesh_lighting_client_publish_callback;
1429         }
1430         break;
1431     }
1432     case BLE_MESH_MODEL_ID_LIGHT_XYL_CLI: {
1433         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_xyl_cli_op;
1434         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_lighting_client_cb;
1435         bt_mesh_light_xyl_client_t *cli = (bt_mesh_light_xyl_client_t *)model->user_data;
1436         if (cli != NULL) {
1437             cli->publish_status = btc_ble_mesh_lighting_client_publish_callback;
1438         }
1439         break;
1440     }
1441     case BLE_MESH_MODEL_ID_LIGHT_LC_CLI: {
1442         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_lc_cli_op;
1443         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_lighting_client_cb;
1444         bt_mesh_light_lc_client_t *cli = (bt_mesh_light_lc_client_t *)model->user_data;
1445         if (cli != NULL) {
1446             cli->publish_status = btc_ble_mesh_lighting_client_publish_callback;
1447         }
1448         break;
1449     }
1450 #endif /* CONFIG_BLE_MESH_LIGHTING_CLIENT */
1451 #if CONFIG_BLE_MESH_SENSOR_CLI
1452     case BLE_MESH_MODEL_ID_SENSOR_CLI: {
1453         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_sensor_cli_op;
1454         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_sensor_client_cb;
1455         bt_mesh_sensor_client_t *cli = (bt_mesh_sensor_client_t *)model->user_data;
1456         if (cli != NULL) {
1457             cli->publish_status = btc_ble_mesh_sensor_client_publish_callback;
1458         }
1459         break;
1460     }
1461 #endif /* CONFIG_BLE_MESH_SENSOR_CLI */
1462 #if CONFIG_BLE_MESH_TIME_SCENE_CLIENT
1463     case BLE_MESH_MODEL_ID_TIME_CLI: {
1464         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_time_cli_op;
1465         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_time_scene_client_cb;
1466         bt_mesh_time_client_t *cli = (bt_mesh_time_client_t *)model->user_data;
1467         if (cli != NULL) {
1468             cli->publish_status = btc_ble_mesh_time_scene_client_publish_callback;
1469         }
1470         break;
1471     }
1472     case BLE_MESH_MODEL_ID_SCENE_CLI: {
1473         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_scene_cli_op;
1474         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_time_scene_client_cb;
1475         bt_mesh_scene_client_t *cli = (bt_mesh_scene_client_t *)model->user_data;
1476         if (cli != NULL) {
1477             cli->publish_status = btc_ble_mesh_time_scene_client_publish_callback;
1478         }
1479         break;
1480     }
1481     case BLE_MESH_MODEL_ID_SCHEDULER_CLI: {
1482         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_scheduler_cli_op;
1483         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_time_scene_client_cb;
1484         bt_mesh_scheduler_client_t *cli = (bt_mesh_scheduler_client_t *)model->user_data;
1485         if (cli != NULL) {
1486             cli->publish_status = btc_ble_mesh_time_scene_client_publish_callback;
1487         }
1488         break;
1489     }
1490 #endif /* CONFIG_BLE_MESH_TIME_SCENE_CLIENT */
1491 #if CONFIG_BLE_MESH_GENERIC_SERVER
1492     case BLE_MESH_MODEL_ID_GEN_ONOFF_SRV:
1493         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_onoff_srv_op;
1494         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_gen_onoff_srv_cb;
1495         if (model->pub) {
1496             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1497         }
1498         break;
1499     case BLE_MESH_MODEL_ID_GEN_LEVEL_SRV:
1500         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_level_srv_op;
1501         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_gen_level_srv_cb;
1502         if (model->pub) {
1503             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1504         }
1505         break;
1506     case BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV:
1507         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_def_trans_time_srv_op;
1508         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_gen_def_trans_time_srv_cb;
1509         if (model->pub) {
1510             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1511         }
1512         break;
1513     case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SRV:
1514         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_power_onoff_srv_op;
1515         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_gen_power_onoff_srv_cb;
1516         if (model->pub) {
1517             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1518         }
1519         break;
1520     case BLE_MESH_MODEL_ID_GEN_POWER_ONOFF_SETUP_SRV:
1521         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_power_onoff_setup_srv_op;
1522         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_gen_power_onoff_setup_srv_cb;
1523         if (model->pub) {
1524             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1525         }
1526         break;
1527     case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SRV:
1528         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_power_level_srv_op;
1529         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_gen_power_level_srv_cb;
1530         if (model->pub) {
1531             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1532         }
1533         break;
1534     case BLE_MESH_MODEL_ID_GEN_POWER_LEVEL_SETUP_SRV:
1535         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_power_level_setup_srv_op;
1536         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_gen_power_level_setup_srv_cb;
1537         if (model->pub) {
1538             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1539         }
1540         break;
1541     case BLE_MESH_MODEL_ID_GEN_BATTERY_SRV:
1542         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_battery_srv_op;
1543         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_gen_battery_srv_cb;
1544         if (model->pub) {
1545             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1546         }
1547         break;
1548     case BLE_MESH_MODEL_ID_GEN_LOCATION_SRV:
1549         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_location_srv_op;
1550         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_gen_location_srv_cb;
1551         if (model->pub) {
1552             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1553         }
1554         break;
1555     case BLE_MESH_MODEL_ID_GEN_USER_PROP_SRV:
1556         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_user_prop_srv_op;
1557         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_gen_user_prop_srv_cb;
1558         if (model->pub) {
1559             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1560         }
1561         break;
1562     case BLE_MESH_MODEL_ID_GEN_ADMIN_PROP_SRV:
1563         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_admin_prop_srv_op;
1564         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_gen_admin_prop_srv_cb;
1565         if (model->pub) {
1566             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1567         }
1568         break;
1569     case BLE_MESH_MODEL_ID_GEN_MANUFACTURER_PROP_SRV:
1570         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_manu_prop_srv_op;
1571         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_gen_manu_prop_srv_cb;
1572         if (model->pub) {
1573             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1574         }
1575         break;
1576     case BLE_MESH_MODEL_ID_GEN_CLIENT_PROP_SRV:
1577         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_client_prop_srv_op;
1578         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_gen_client_prop_srv_cb;
1579         if (model->pub) {
1580             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1581         }
1582         break;
1583     case BLE_MESH_MODEL_ID_GEN_LOCATION_SETUP_SRV:
1584         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_gen_location_setup_srv_op;
1585         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_gen_location_setup_srv_cb;
1586         if (model->pub) {
1587             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1588         }
1589         break;
1590 #endif /* CONFIG_BLE_MESH_GENERIC_SERVER */
1591 #if CONFIG_BLE_MESH_LIGHTING_SERVER
1592     case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SRV:
1593         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_lightness_srv_op;
1594         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_light_lightness_srv_cb;
1595         if (model->pub) {
1596             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1597         }
1598         break;
1599     case BLE_MESH_MODEL_ID_LIGHT_LIGHTNESS_SETUP_SRV:
1600         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_lightness_setup_srv_op;
1601         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_light_lightness_setup_srv_cb;
1602         if (model->pub) {
1603             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1604         }
1605         break;
1606     case BLE_MESH_MODEL_ID_LIGHT_CTL_SRV:
1607         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_ctl_srv_op;
1608         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_light_ctl_srv_cb;
1609         if (model->pub) {
1610             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1611         }
1612         break;
1613     case BLE_MESH_MODEL_ID_LIGHT_CTL_SETUP_SRV:
1614         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_ctl_setup_srv_op;
1615         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_light_ctl_setup_srv_cb;
1616         if (model->pub) {
1617             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1618         }
1619         break;
1620     case BLE_MESH_MODEL_ID_LIGHT_CTL_TEMP_SRV:
1621         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_ctl_temp_srv_op;
1622         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_light_ctl_temp_srv_cb;
1623         if (model->pub) {
1624             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1625         }
1626         break;
1627     case BLE_MESH_MODEL_ID_LIGHT_HSL_SRV:
1628         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_hsl_srv_op;
1629         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_light_hsl_srv_cb;
1630         if (model->pub) {
1631             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1632         }
1633         break;
1634     case BLE_MESH_MODEL_ID_LIGHT_HSL_HUE_SRV:
1635         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_hsl_hue_srv_op;
1636         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_light_hsl_hue_srv_cb;
1637         if (model->pub) {
1638             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1639         }
1640         break;
1641     case BLE_MESH_MODEL_ID_LIGHT_HSL_SAT_SRV:
1642         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_hsl_sat_srv_op;
1643         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_light_hsl_sat_srv_cb;
1644         if (model->pub) {
1645             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1646         }
1647         break;
1648     case BLE_MESH_MODEL_ID_LIGHT_HSL_SETUP_SRV:
1649         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_hsl_setup_srv_op;
1650         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_light_hsl_setup_srv_cb;
1651         if (model->pub) {
1652             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1653         }
1654         break;
1655     case BLE_MESH_MODEL_ID_LIGHT_XYL_SRV:
1656         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_xyl_srv_op;
1657         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_light_xyl_srv_cb;
1658         if (model->pub) {
1659             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1660         }
1661         break;
1662     case BLE_MESH_MODEL_ID_LIGHT_XYL_SETUP_SRV:
1663         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_xyl_setup_srv_op;
1664         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_light_xyl_setup_srv_cb;
1665         if (model->pub) {
1666             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1667         }
1668         break;
1669     case BLE_MESH_MODEL_ID_LIGHT_LC_SRV:
1670         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_lc_srv_op;
1671         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_light_lc_srv_cb;
1672         if (model->pub) {
1673             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1674         }
1675         break;
1676     case BLE_MESH_MODEL_ID_LIGHT_LC_SETUP_SRV:
1677         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_light_lc_setup_srv_op;
1678         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_light_lc_setup_srv_cb;
1679         if (model->pub) {
1680             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1681         }
1682         break;
1683 #endif /* CONFIG_BLE_MESH_LIGHTING_SERVER */
1684 #if CONFIG_BLE_MESH_TIME_SCENE_SERVER
1685     case BLE_MESH_MODEL_ID_TIME_SRV:
1686         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_time_srv_op;
1687         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_time_srv_cb;
1688         if (model->pub) {
1689             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1690         }
1691         break;
1692     case BLE_MESH_MODEL_ID_TIME_SETUP_SRV:
1693         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_time_setup_srv_op;
1694         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_time_setup_srv_cb;
1695         if (model->pub) {
1696             /* Time Setup Server model does not support subscribing nor publishing. */
1697             BT_ERR("Time Setup Server shall not support publication");
1698             return;
1699         }
1700         break;
1701     case BLE_MESH_MODEL_ID_SCENE_SRV:
1702         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_scene_srv_op;
1703         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_scene_srv_cb;
1704         if (model->pub) {
1705             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1706         }
1707         break;
1708     case BLE_MESH_MODEL_ID_SCENE_SETUP_SRV:
1709         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_scene_setup_srv_op;
1710         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_scene_setup_srv_cb;
1711         if (model->pub) {
1712             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1713         }
1714         break;
1715     case BLE_MESH_MODEL_ID_SCHEDULER_SRV:
1716         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_scheduler_srv_op;
1717         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_scheduler_srv_cb;
1718         if (model->pub) {
1719             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1720         }
1721         break;
1722     case BLE_MESH_MODEL_ID_SCHEDULER_SETUP_SRV:
1723         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_scheduler_setup_srv_op;
1724         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_scheduler_setup_srv_cb;
1725         if (model->pub) {
1726             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1727         }
1728         break;
1729 #endif /* CONFIG_BLE_MESH_TIME_SCENE_SERVER */
1730 #if CONFIG_BLE_MESH_SENSOR_SERVER
1731     case BLE_MESH_MODEL_ID_SENSOR_SRV:
1732         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_sensor_srv_op;
1733         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_sensor_srv_cb;
1734         if (model->pub) {
1735             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1736         }
1737         break;
1738     case BLE_MESH_MODEL_ID_SENSOR_SETUP_SRV:
1739         model->op = (esp_ble_mesh_model_op_t *)bt_mesh_sensor_setup_srv_op;
1740         model->cb = (esp_ble_mesh_model_cbs_t *)&bt_mesh_sensor_setup_srv_cb;
1741         if (model->pub) {
1742             model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1743         }
1744         break;
1745 #endif /* CONFIG_BLE_MESH_SENSOR_SERVER */
1746     default:
1747         goto set_vnd_op;
1748     }
1749     return;
1750 
1751 set_vnd_op:
1752     if (model->pub) {
1753         model->pub->update = (esp_ble_mesh_cb_t)btc_ble_mesh_model_publish_update;
1754     }
1755     esp_ble_mesh_model_op_t *op = model->op;
1756     while (op != NULL && op->opcode != 0) {
1757         op->param_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_server_model_op_cb;
1758         op++;
1759     }
1760     return;
1761 }
1762 
btc_ble_mesh_prov_call_handler(btc_msg_t * msg)1763 void btc_ble_mesh_prov_call_handler(btc_msg_t *msg)
1764 {
1765     esp_ble_mesh_prov_cb_param_t param = {0};
1766     btc_ble_mesh_prov_args_t *arg = NULL;
1767     uint8_t act = 0U;
1768 
1769     if (!msg) {
1770         BT_ERR("%s, Invalid parameter", __func__);
1771         return;
1772     }
1773 
1774     arg = (btc_ble_mesh_prov_args_t *)(msg->arg);
1775 
1776     switch (msg->act) {
1777     case BTC_BLE_MESH_ACT_MESH_INIT: {
1778         for (int i = 0; i < arg->mesh_init.comp->element_count; i++) {
1779             esp_ble_mesh_elem_t *elem = &arg->mesh_init.comp->elements[i];
1780 
1781             for (int j = 0; j < elem->sig_model_count; j++) {
1782                 esp_ble_mesh_model_t *sig_model = &elem->sig_models[j];
1783                 if (sig_model->op && BLE_MESH_MODEL_OP_LEN(sig_model->op->opcode) == 3) {
1784                     /* Opcode of SIG model must be 1 or 2 bytes. */
1785                     btc_ble_mesh_prov_register_complete_cb(-EINVAL);
1786                     return;
1787                 }
1788                 btc_ble_mesh_model_op_set(sig_model);
1789             }
1790 
1791             for (int k = 0; k < elem->vnd_model_count; k++) {
1792                 esp_ble_mesh_model_t *vnd_model = &elem->vnd_models[k];
1793                 if (vnd_model->op && BLE_MESH_MODEL_OP_LEN(vnd_model->op->opcode) < 3) {
1794                     /* Opcode of vendor model must be 3 bytes. */
1795                     btc_ble_mesh_prov_register_complete_cb(-EINVAL);
1796                     return;
1797                 }
1798                 btc_ble_mesh_model_op_set(vnd_model);
1799             }
1800         }
1801 #if CONFIG_BLE_MESH_NODE
1802         arg->mesh_init.prov->oob_pub_key_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_oob_pub_key_cb;
1803         arg->mesh_init.prov->output_num_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_output_number_cb;
1804         arg->mesh_init.prov->output_str_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_output_string_cb;
1805         arg->mesh_init.prov->input_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_input_cb;
1806         arg->mesh_init.prov->link_open_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_link_open_cb;
1807         arg->mesh_init.prov->link_close_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_link_close_cb;
1808         arg->mesh_init.prov->complete_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_complete_cb;
1809         arg->mesh_init.prov->reset_cb = (esp_ble_mesh_cb_t)btc_ble_mesh_reset_cb;
1810 #endif /* CONFIG_BLE_MESH_NODE */
1811 #if CONFIG_BLE_MESH_PROVISIONER
1812         arg->mesh_init.prov->provisioner_prov_read_oob_pub_key = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_prov_read_oob_pub_key_cb;
1813         arg->mesh_init.prov->provisioner_prov_input = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_prov_input_cb;
1814         arg->mesh_init.prov->provisioner_prov_output = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_prov_output_cb;
1815         arg->mesh_init.prov->provisioner_link_open = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_link_open_cb;
1816         arg->mesh_init.prov->provisioner_link_close = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_link_close_cb;
1817         arg->mesh_init.prov->provisioner_prov_comp = (esp_ble_mesh_cb_t)btc_ble_mesh_provisioner_prov_complete_cb;
1818         bt_mesh_provisioner_adv_pkt_cb_register(btc_ble_mesh_provisioner_recv_unprov_adv_pkt_cb);
1819 #endif /* CONFIG_BLE_MESH_PROVISIONER */
1820 #if CONFIG_BLE_MESH_LOW_POWER
1821         bt_mesh_lpn_set_cb(btc_ble_mesh_lpn_cb);
1822 #endif /* CONFIG_BLE_MESH_LOW_POWER */
1823 #if CONFIG_BLE_MESH_FRIEND
1824         bt_mesh_friend_set_cb(btc_ble_mesh_friend_cb);
1825 #endif /* CONFIG_BLE_MESH_FRIEND */
1826 #if CONFIG_BLE_MESH_GATT_PROXY_CLIENT
1827         bt_mesh_proxy_client_set_adv_recv_cb(btc_ble_mesh_proxy_client_adv_recv_cb);
1828         bt_mesh_proxy_client_set_conn_cb(btc_ble_mesh_proxy_client_connect_cb);
1829         bt_mesh_proxy_client_set_disconn_cb(btc_ble_mesh_proxy_client_disconnect_cb);
1830         bt_mesh_proxy_client_set_filter_status_cb(btc_ble_mesh_proxy_client_filter_status_recv_cb);
1831 #endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */
1832 #if CONFIG_BLE_MESH_GATT_PROXY_SERVER
1833         bt_mesh_proxy_server_set_conn_cb(btc_ble_mesh_proxy_server_connect_cb);
1834         bt_mesh_proxy_server_set_disconn_cb(btc_ble_mesh_proxy_server_disconnect_cb);
1835 #endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */
1836         int err_code = bt_mesh_init((struct bt_mesh_prov *)arg->mesh_init.prov,
1837                                     (struct bt_mesh_comp *)arg->mesh_init.comp);
1838         /* Give the semaphore when BLE Mesh initialization is finished. */
1839         xSemaphoreGive(arg->mesh_init.semaphore);
1840         btc_ble_mesh_prov_register_complete_cb(err_code);
1841         return;
1842     }
1843 #if CONFIG_BLE_MESH_NODE
1844     case BTC_BLE_MESH_ACT_PROV_ENABLE:
1845         act = ESP_BLE_MESH_NODE_PROV_ENABLE_COMP_EVT;
1846         param.node_prov_enable_comp.err_code = bt_mesh_prov_enable(arg->node_prov_enable.bearers);
1847         break;
1848     case BTC_BLE_MESH_ACT_PROV_DISABLE:
1849         act = ESP_BLE_MESH_NODE_PROV_DISABLE_COMP_EVT;
1850         param.node_prov_disable_comp.err_code = bt_mesh_prov_disable(arg->node_prov_disable.bearers);
1851         break;
1852     case BTC_BLE_MESH_ACT_NODE_RESET:
1853         bt_mesh_node_reset();
1854         return;
1855     case BTC_BLE_MESH_ACT_SET_OOB_PUB_KEY:
1856         act = ESP_BLE_MESH_NODE_PROV_SET_OOB_PUB_KEY_COMP_EVT;
1857         param.node_prov_set_oob_pub_key_comp.err_code =
1858             bt_mesh_set_oob_pub_key(arg->set_oob_pub_key.pub_key_x,
1859                                     arg->set_oob_pub_key.pub_key_y,
1860                                     arg->set_oob_pub_key.private_key);
1861         break;
1862     case BTC_BLE_MESH_ACT_INPUT_NUMBER:
1863         act = ESP_BLE_MESH_NODE_PROV_INPUT_NUMBER_COMP_EVT;
1864         param.node_prov_input_num_comp.err_code = bt_mesh_input_number(arg->input_number.number);
1865         break;
1866     case BTC_BLE_MESH_ACT_INPUT_STRING:
1867         act = ESP_BLE_MESH_NODE_PROV_INPUT_STRING_COMP_EVT;
1868         param.node_prov_input_str_comp.err_code = bt_mesh_input_string(arg->input_string.string);
1869         break;
1870     case BTC_BLE_MESH_ACT_NODE_ADD_LOCAL_NET_KEY:
1871         act = ESP_BLE_MESH_NODE_ADD_LOCAL_NET_KEY_COMP_EVT;
1872         param.node_add_net_key_comp.net_idx = arg->node_add_local_net_key.net_idx;
1873         param.node_add_net_key_comp.err_code =
1874             bt_mesh_node_local_net_key_add(arg->node_add_local_net_key.net_idx,
1875                                            arg->node_add_local_net_key.net_key);
1876         break;
1877     case BTC_BLE_MESH_ACT_NODE_ADD_LOCAL_APP_KEY:
1878         act = ESP_BLE_MESH_NODE_ADD_LOCAL_APP_KEY_COMP_EVT;
1879         param.node_add_app_key_comp.net_idx = arg->node_add_local_app_key.net_idx;
1880         param.node_add_app_key_comp.app_idx = arg->node_add_local_app_key.app_idx;
1881         param.node_add_app_key_comp.err_code =
1882             bt_mesh_node_local_app_key_add(arg->node_add_local_app_key.net_idx,
1883                                            arg->node_add_local_app_key.app_idx,
1884                                            arg->node_add_local_app_key.app_key);
1885         break;
1886     case BTC_BLE_MESH_ACT_NODE_BIND_APP_KEY_TO_MODEL:
1887         act = ESP_BLE_MESH_NODE_BIND_APP_KEY_TO_MODEL_COMP_EVT;
1888         param.node_bind_app_key_to_model_comp.element_addr = arg->node_local_mod_app_bind.element_addr;
1889         param.node_bind_app_key_to_model_comp.model_id = arg->node_local_mod_app_bind.model_id;
1890         param.node_bind_app_key_to_model_comp.company_id = arg->node_local_mod_app_bind.company_id;
1891         param.node_bind_app_key_to_model_comp.app_idx = arg->node_local_mod_app_bind.app_idx;
1892         param.node_bind_app_key_to_model_comp.err_code =
1893             bt_mesh_node_bind_app_key_to_model(arg->node_local_mod_app_bind.element_addr,
1894                                                arg->node_local_mod_app_bind.model_id,
1895                                                arg->node_local_mod_app_bind.company_id,
1896                                                arg->node_local_mod_app_bind.app_idx);
1897         break;
1898 #endif /* CONFIG_BLE_MESH_NODE */
1899 #if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \
1900     CONFIG_BLE_MESH_GATT_PROXY_SERVER
1901     case BTC_BLE_MESH_ACT_SET_DEVICE_NAME:
1902         act = ESP_BLE_MESH_NODE_SET_UNPROV_DEV_NAME_COMP_EVT;
1903         param.node_set_unprov_dev_name_comp.err_code = bt_mesh_set_device_name(arg->set_device_name.name);
1904         break;
1905 #if defined(CONFIG_BLE_MESH_GATT_PROXY_SERVER)
1906     case BTC_BLE_MESH_ACT_PROXY_IDENTITY_ENABLE:
1907         act = ESP_BLE_MESH_NODE_PROXY_IDENTITY_ENABLE_COMP_EVT;
1908         param.node_proxy_identity_enable_comp.err_code = bt_mesh_proxy_identity_enable();
1909         break;
1910     case BTC_BLE_MESH_ACT_PROXY_GATT_ENABLE:
1911         act = ESP_BLE_MESH_NODE_PROXY_GATT_ENABLE_COMP_EVT;
1912         param.node_proxy_gatt_enable_comp.err_code = bt_mesh_proxy_server_gatt_enable();
1913         break;
1914     case BTC_BLE_MESH_ACT_PROXY_GATT_DISABLE:
1915         act = ESP_BLE_MESH_NODE_PROXY_GATT_DISABLE_COMP_EVT;
1916         param.node_proxy_gatt_disable_comp.err_code = bt_mesh_proxy_server_gatt_disable();
1917         break;
1918 #endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */
1919 #endif /* (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || CONFIG_BLE_MESH_GATT_PROXY_SERVER */
1920 #if CONFIG_BLE_MESH_PROVISIONER
1921     case BTC_BLE_MESH_ACT_PROVISIONER_READ_OOB_PUB_KEY:
1922         act = ESP_BLE_MESH_PROVISIONER_PROV_READ_OOB_PUB_KEY_COMP_EVT;
1923         param.provisioner_prov_read_oob_pub_key_comp.err_code =
1924             bt_mesh_provisioner_read_oob_pub_key(arg->provisioner_read_oob_pub_key.link_idx,
1925                                                  arg->provisioner_read_oob_pub_key.pub_key_x,
1926                                                  arg->provisioner_read_oob_pub_key.pub_key_y);
1927         break;
1928     case BTC_BLE_MESH_ACT_PROVISIONER_INPUT_STR:
1929         act = ESP_BLE_MESH_PROVISIONER_PROV_INPUT_STRING_COMP_EVT;
1930         param.provisioner_prov_input_str_comp.err_code =
1931             bt_mesh_provisioner_set_oob_input_data(arg->provisioner_input_str.link_idx,
1932                                                    (const uint8_t *)&arg->provisioner_input_str.string,
1933                                                    false);
1934         break;
1935     case BTC_BLE_MESH_ACT_PROVISIONER_INPUT_NUM:
1936         act = ESP_BLE_MESH_PROVISIONER_PROV_INPUT_NUMBER_COMP_EVT;
1937         param.provisioner_prov_input_num_comp.err_code =
1938             bt_mesh_provisioner_set_oob_input_data(arg->provisioner_input_num.link_idx,
1939                                                    (const uint8_t *)&arg->provisioner_input_num.number,
1940                                                    true);
1941         break;
1942     case BTC_BLE_MESH_ACT_PROVISIONER_ENABLE:
1943         act = ESP_BLE_MESH_PROVISIONER_PROV_ENABLE_COMP_EVT;
1944         param.provisioner_prov_enable_comp.err_code =
1945             bt_mesh_provisioner_enable(arg->provisioner_enable.bearers);
1946         break;
1947     case BTC_BLE_MESH_ACT_PROVISIONER_DISABLE:
1948         act = ESP_BLE_MESH_PROVISIONER_PROV_DISABLE_COMP_EVT;
1949         param.provisioner_prov_disable_comp.err_code =
1950             bt_mesh_provisioner_disable(arg->provisioner_disable.bearers);
1951         break;
1952     case BTC_BLE_MESH_ACT_PROVISIONER_DEV_ADD: {
1953         struct bt_mesh_unprov_dev_add add_dev = {0};
1954         add_dev.addr_type = arg->provisioner_dev_add.add_dev.addr_type;
1955         add_dev.oob_info = arg->provisioner_dev_add.add_dev.oob_info;
1956         add_dev.bearer = arg->provisioner_dev_add.add_dev.bearer;
1957         memcpy(add_dev.addr, arg->provisioner_dev_add.add_dev.addr, 6);
1958         memcpy(add_dev.uuid, arg->provisioner_dev_add.add_dev.uuid, 16);
1959         act = ESP_BLE_MESH_PROVISIONER_ADD_UNPROV_DEV_COMP_EVT;
1960         param.provisioner_add_unprov_dev_comp.err_code =
1961             bt_mesh_provisioner_add_unprov_dev(&add_dev, arg->provisioner_dev_add.flags);
1962         break;
1963     }
1964     case BTC_BLE_MESH_ACT_PROVISIONER_PROV_DEV_WITH_ADDR:
1965         act = ESP_BLE_MESH_PROVISIONER_PROV_DEV_WITH_ADDR_COMP_EVT;
1966         param.provisioner_prov_dev_with_addr_comp.err_code =
1967             bt_mesh_provisioner_prov_device_with_addr(arg->provisioner_prov_dev_with_addr.uuid,
1968                 arg->provisioner_prov_dev_with_addr.addr, arg->provisioner_prov_dev_with_addr.addr_type,
1969                 arg->provisioner_prov_dev_with_addr.bearer, arg->provisioner_prov_dev_with_addr.oob_info,
1970                 arg->provisioner_prov_dev_with_addr.unicast_addr);
1971         break;
1972     case BTC_BLE_MESH_ACT_PROVISIONER_DEV_DEL: {
1973         struct bt_mesh_device_delete del_dev = {0};
1974         if (arg->provisioner_dev_del.del_dev.flag & DEL_DEV_ADDR_FLAG) {
1975             del_dev.addr_type = arg->provisioner_dev_del.del_dev.addr_type;
1976             memcpy(del_dev.addr, arg->provisioner_dev_del.del_dev.addr, 6);
1977         } else if (arg->provisioner_dev_del.del_dev.flag & DEL_DEV_UUID_FLAG) {
1978             memcpy(del_dev.uuid, arg->provisioner_dev_del.del_dev.uuid, 16);
1979         }
1980         act = ESP_BLE_MESH_PROVISIONER_DELETE_DEV_COMP_EVT;
1981         param.provisioner_delete_dev_comp.err_code = bt_mesh_provisioner_delete_device(&del_dev);
1982         break;
1983     }
1984     case BTC_BLE_MESH_ACT_PROVISIONER_SET_DEV_UUID_MATCH:
1985         act = ESP_BLE_MESH_PROVISIONER_SET_DEV_UUID_MATCH_COMP_EVT;
1986         param.provisioner_set_dev_uuid_match_comp.err_code =
1987             bt_mesh_provisioner_set_dev_uuid_match(arg->set_dev_uuid_match.offset,
1988                     arg->set_dev_uuid_match.match_len,
1989                     arg->set_dev_uuid_match.match_val,
1990                     arg->set_dev_uuid_match.prov_after_match);
1991         break;
1992     case BTC_BLE_MESH_ACT_PROVISIONER_SET_PROV_DATA_INFO: {
1993         struct bt_mesh_prov_data_info info = {0};
1994         info.flag = arg->set_prov_data_info.prov_data.flag;
1995         if (arg->set_prov_data_info.prov_data.flag & PROV_DATA_NET_IDX_FLAG) {
1996             info.net_idx = arg->set_prov_data_info.prov_data.net_idx;
1997         } else if (arg->set_prov_data_info.prov_data.flag & PROV_DATA_FLAGS_FLAG) {
1998             info.flags = arg->set_prov_data_info.prov_data.flags;
1999         } else if (arg->set_prov_data_info.prov_data.flag & PROV_DATA_IV_INDEX_FLAG) {
2000             info.iv_index = arg->set_prov_data_info.prov_data.iv_index;
2001         }
2002         act = ESP_BLE_MESH_PROVISIONER_SET_PROV_DATA_INFO_COMP_EVT;
2003         param.provisioner_set_prov_data_info_comp.err_code =
2004             bt_mesh_provisioner_set_prov_data_info(&info);
2005         break;
2006     }
2007     case BTC_BLE_MESH_ACT_PROVISIONER_SET_STATIC_OOB_VAL:
2008         act = ESP_BLE_MESH_PROVISIONER_SET_STATIC_OOB_VALUE_COMP_EVT;
2009         param.provisioner_set_static_oob_val_comp.err_code =
2010             bt_mesh_provisioner_set_static_oob_value(
2011                 arg->set_static_oob_val.value, arg->set_static_oob_val.length);
2012         break;
2013     case BTC_BLE_MESH_ACT_PROVISIONER_SET_PRIMARY_ELEM_ADDR:
2014         act = ESP_BLE_MESH_PROVISIONER_SET_PRIMARY_ELEM_ADDR_COMP_EVT;
2015         param.provisioner_set_primary_elem_addr_comp.err_code =
2016             bt_mesh_provisioner_set_primary_elem_addr(arg->set_primary_elem_addr.addr);
2017         break;
2018     case BTC_BLE_MESH_ACT_PROVISIONER_SET_NODE_NAME:
2019         act = ESP_BLE_MESH_PROVISIONER_SET_NODE_NAME_COMP_EVT;
2020         param.provisioner_set_node_name_comp.node_index = arg->set_node_name.index;
2021         param.provisioner_set_node_name_comp.err_code =
2022             bt_mesh_provisioner_set_node_name(arg->set_node_name.index, arg->set_node_name.name);
2023         break;
2024     case BTC_BLE_MESH_ACT_PROVISIONER_ADD_LOCAL_APP_KEY: {
2025         const uint8_t *app_key = NULL;
2026         const uint8_t zero[16] = {0};
2027         if (memcmp(arg->add_local_app_key.app_key, zero, 16)) {
2028             app_key = arg->add_local_app_key.app_key;
2029         }
2030         act = ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT;
2031         param.provisioner_add_app_key_comp.err_code =
2032             bt_mesh_provisioner_local_app_key_add(app_key, arg->add_local_app_key.net_idx,
2033                     &arg->add_local_app_key.app_idx);
2034         param.provisioner_add_app_key_comp.net_idx = arg->add_local_app_key.net_idx;
2035         param.provisioner_add_app_key_comp.app_idx = arg->add_local_app_key.app_idx;
2036         break;
2037     }
2038     case BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_APP_KEY:
2039         act = ESP_BLE_MESH_PROVISIONER_UPDATE_LOCAL_APP_KEY_COMP_EVT;
2040         param.provisioner_update_app_key_comp.net_idx = arg->update_local_app_key.net_idx;
2041         param.provisioner_update_app_key_comp.app_idx = arg->update_local_app_key.app_idx;
2042         param.provisioner_update_app_key_comp.err_code =
2043             bt_mesh_provisioner_local_app_key_update(arg->update_local_app_key.app_key,
2044                     arg->update_local_app_key.net_idx, arg->update_local_app_key.app_idx);
2045         break;
2046     case BTC_BLE_MESH_ACT_PROVISIONER_BIND_LOCAL_MOD_APP:
2047         act = ESP_BLE_MESH_PROVISIONER_BIND_APP_KEY_TO_MODEL_COMP_EVT;
2048         param.provisioner_bind_app_key_to_model_comp.element_addr = arg->local_mod_app_bind.elem_addr;
2049         param.provisioner_bind_app_key_to_model_comp.app_idx = arg->local_mod_app_bind.app_idx;
2050         param.provisioner_bind_app_key_to_model_comp.company_id = arg->local_mod_app_bind.cid;
2051         param.provisioner_bind_app_key_to_model_comp.model_id = arg->local_mod_app_bind.model_id;
2052         param.provisioner_bind_app_key_to_model_comp.err_code =
2053             bt_mesh_provisioner_bind_local_model_app_idx(arg->local_mod_app_bind.elem_addr,
2054                     arg->local_mod_app_bind.model_id,
2055                     arg->local_mod_app_bind.cid,
2056                     arg->local_mod_app_bind.app_idx);
2057         break;
2058     case BTC_BLE_MESH_ACT_PROVISIONER_ADD_LOCAL_NET_KEY: {
2059         const uint8_t *net_key = NULL;
2060         const uint8_t zero[16] = {0};
2061         if (memcmp(arg->add_local_net_key.net_key, zero, 16)) {
2062             net_key = arg->add_local_net_key.net_key;
2063         }
2064         act = ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_NET_KEY_COMP_EVT;
2065         param.provisioner_add_net_key_comp.err_code =
2066             bt_mesh_provisioner_local_net_key_add(net_key, &arg->add_local_net_key.net_idx);
2067         param.provisioner_add_net_key_comp.net_idx = arg->add_local_net_key.net_idx;
2068         break;
2069     }
2070     case BTC_BLE_MESH_ACT_PROVISIONER_UPDATE_LOCAL_NET_KEY:
2071         act = ESP_BLE_MESH_PROVISIONER_UPDATE_LOCAL_NET_KEY_COMP_EVT;
2072         param.provisioner_update_net_key_comp.net_idx = arg->update_local_net_key.net_idx;
2073         param.provisioner_update_net_key_comp.err_code =
2074             bt_mesh_provisioner_local_net_key_update(arg->update_local_net_key.net_key,
2075                 arg->update_local_net_key.net_idx);
2076         break;
2077     case BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA:
2078         act = ESP_BLE_MESH_PROVISIONER_STORE_NODE_COMP_DATA_COMP_EVT;
2079         param.provisioner_store_node_comp_data_comp.addr = arg->store_node_comp_data.unicast_addr;
2080         param.provisioner_store_node_comp_data_comp.err_code =
2081             bt_mesh_provisioner_store_node_comp_data(arg->store_node_comp_data.unicast_addr,
2082                 arg->store_node_comp_data.data, arg->store_node_comp_data.length);
2083         break;
2084     case BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_UUID:
2085         act = ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_UUID_COMP_EVT;
2086         memcpy(param.provisioner_delete_node_with_uuid_comp.uuid, arg->delete_node_with_uuid.uuid, 16);
2087         param.provisioner_delete_node_with_uuid_comp.err_code =
2088             bt_mesh_provisioner_delete_node_with_uuid(arg->delete_node_with_uuid.uuid);
2089         break;
2090     case BTC_BLE_MESH_ACT_PROVISIONER_DELETE_NODE_WITH_ADDR:
2091         act = ESP_BLE_MESH_PROVISIONER_DELETE_NODE_WITH_ADDR_COMP_EVT;
2092         param.provisioner_delete_node_with_addr_comp.unicast_addr = arg->delete_node_with_addr.unicast_addr;
2093         param.provisioner_delete_node_with_addr_comp.err_code =
2094             bt_mesh_provisioner_delete_node_with_node_addr(arg->delete_node_with_addr.unicast_addr);
2095         break;
2096 #if CONFIG_BLE_MESH_PROVISIONER_RECV_HB
2097     case BTC_BLE_MESH_ACT_PROVISIONER_ENABLE_HEARTBEAT_RECV:
2098         act = ESP_BLE_MESH_PROVISIONER_ENABLE_HEARTBEAT_RECV_COMP_EVT;
2099         param.provisioner_enable_heartbeat_recv_comp.enable = arg->enable_heartbeat_recv.enable;
2100         param.provisioner_enable_heartbeat_recv_comp.err_code =
2101             bt_mesh_provisioner_recv_heartbeat(arg->enable_heartbeat_recv.enable ?
2102                                                btc_ble_mesh_provisioner_recv_heartbeat_cb : NULL);
2103         break;
2104     case BTC_BLE_MESH_ACT_PROVISIONER_SET_HEARTBEAT_FILTER_TYPE:
2105         act = ESP_BLE_MESH_PROVISIONER_SET_HEARTBEAT_FILTER_TYPE_COMP_EVT;
2106         param.provisioner_set_heartbeat_filter_type_comp.type = arg->set_heartbeat_filter_type.type;
2107         param.provisioner_set_heartbeat_filter_type_comp.err_code =
2108             bt_mesh_provisioner_set_heartbeat_filter_type(arg->set_heartbeat_filter_type.type);
2109         break;
2110     case BTC_BLE_MESH_ACT_PROVISIONER_SET_HEARTBEAT_FILTER_INFO:
2111         act = ESP_BLE_MESH_PROVISIONER_SET_HEARTBEAT_FILTER_INFO_COMP_EVT;
2112         param.provisioner_set_heartbeat_filter_info_comp.op = arg->set_heartbeat_filter_info.op;
2113         param.provisioner_set_heartbeat_filter_info_comp.hb_src = arg->set_heartbeat_filter_info.hb_src;
2114         param.provisioner_set_heartbeat_filter_info_comp.hb_dst = arg->set_heartbeat_filter_info.hb_dst;
2115         param.provisioner_set_heartbeat_filter_info_comp.err_code =
2116             bt_mesh_provisioner_set_heartbeat_filter_info(arg->set_heartbeat_filter_info.op,
2117                                                           arg->set_heartbeat_filter_info.hb_src,
2118                                                           arg->set_heartbeat_filter_info.hb_dst);
2119         break;
2120 #endif /* CONFIG_BLE_MESH_PROVISIONER_RECV_HB */
2121 #if CONFIG_BLE_MESH_SETTINGS
2122     case BTC_BLE_MESH_ACT_PROVISIONER_DIRECT_ERASE_SETTINGS:
2123         act = ESP_BLE_MESH_PROVISIONER_DIRECT_ERASE_SETTINGS_COMP_EVT;
2124         param.provisioner_direct_erase_settings_comp.err_code = bt_mesh_provisioner_direct_erase_settings();
2125         break;
2126 #endif /* CONFIG_BLE_MESH_SETTINGS */
2127 #if CONFIG_BLE_MESH_USE_MULTIPLE_NAMESPACE
2128     case BTC_BLE_MESH_ACT_PROVISIONER_OPEN_SETTINGS_WITH_INDEX:
2129         act = ESP_BLE_MESH_PROVISIONER_OPEN_SETTINGS_WITH_INDEX_COMP_EVT;
2130         param.provisioner_open_settings_with_index_comp.index = arg->open_settings_with_index.index;
2131         param.provisioner_open_settings_with_index_comp.err_code =
2132             bt_mesh_provisioner_open_settings_with_index(arg->open_settings_with_index.index);
2133         break;
2134     case BTC_BLE_MESH_ACT_PROVISIONER_OPEN_SETTINGS_WITH_UID:
2135         act = ESP_BLE_MESH_PROVISIONER_OPEN_SETTINGS_WITH_UID_COMP_EVT;
2136         strncpy(param.provisioner_open_settings_with_uid_comp.uid,
2137                 arg->open_settings_with_uid.uid, ESP_BLE_MESH_SETTINGS_UID_SIZE + 1);
2138         param.provisioner_open_settings_with_uid_comp.err_code =
2139             bt_mesh_provisioner_open_settings_with_uid(arg->open_settings_with_uid.uid,
2140                                                        &param.provisioner_open_settings_with_uid_comp.index);
2141         break;
2142     case BTC_BLE_MESH_ACT_PROVISIONER_CLOSE_SETTINGS_WITH_INDEX:
2143         act = ESP_BLE_MESH_PROVISIONER_CLOSE_SETTINGS_WITH_INDEX_COMP_EVT;
2144         param.provisioner_close_settings_with_index_comp.index = arg->close_settings_with_index.index;
2145         param.provisioner_close_settings_with_index_comp.err_code =
2146             bt_mesh_provisioner_close_settings_with_index(arg->close_settings_with_index.index,
2147                                                           arg->close_settings_with_index.erase);
2148         break;
2149     case BTC_BLE_MESH_ACT_PROVISIONER_CLOSE_SETTINGS_WITH_UID:
2150         act = ESP_BLE_MESH_PROVISIONER_CLOSE_SETTINGS_WITH_UID_COMP_EVT;
2151         strncpy(param.provisioner_close_settings_with_uid_comp.uid,
2152                 arg->close_settings_with_uid.uid, ESP_BLE_MESH_SETTINGS_UID_SIZE + 1);
2153         param.provisioner_close_settings_with_uid_comp.err_code =
2154             bt_mesh_provisioner_close_settings_with_uid(arg->close_settings_with_uid.uid,
2155                                                         arg->close_settings_with_uid.erase,
2156                                                         &param.provisioner_close_settings_with_uid_comp.index);
2157         break;
2158     case BTC_BLE_MESH_ACT_PROVISIONER_DELETE_SETTINGS_WITH_INDEX:
2159         act = ESP_BLE_MESH_PROVISIONER_DELETE_SETTINGS_WITH_INDEX_COMP_EVT;
2160         param.provisioner_delete_settings_with_index_comp.index = arg->delete_settings_with_index.index;
2161         param.provisioner_delete_settings_with_index_comp.err_code =
2162             bt_mesh_provisioner_delete_settings_with_index(arg->delete_settings_with_index.index);
2163         break;
2164     case BTC_BLE_MESH_ACT_PROVISIONER_DELETE_SETTINGS_WITH_UID:
2165         act = ESP_BLE_MESH_PROVISIONER_DELETE_SETTINGS_WITH_UID_COMP_EVT;
2166         strncpy(param.provisioner_delete_settings_with_uid_comp.uid,
2167                 arg->delete_settings_with_uid.uid, ESP_BLE_MESH_SETTINGS_UID_SIZE + 1);
2168         param.provisioner_delete_settings_with_uid_comp.err_code =
2169             bt_mesh_provisioner_delete_settings_with_uid(arg->delete_settings_with_uid.uid,
2170                                                          &param.provisioner_delete_settings_with_uid_comp.index);
2171         break;
2172 #endif /* CONFIG_BLE_MESH_USE_MULTIPLE_NAMESPACE */
2173 #endif /* CONFIG_BLE_MESH_PROVISIONER */
2174 #if CONFIG_BLE_MESH_FAST_PROV
2175     case BTC_BLE_MESH_ACT_SET_FAST_PROV_INFO:
2176         act = ESP_BLE_MESH_SET_FAST_PROV_INFO_COMP_EVT;
2177         param.set_fast_prov_info_comp.status_unicast =
2178             bt_mesh_set_fast_prov_unicast_addr_range(arg->set_fast_prov_info.unicast_min,
2179                     arg->set_fast_prov_info.unicast_max);
2180         param.set_fast_prov_info_comp.status_net_idx =
2181             bt_mesh_set_fast_prov_net_idx(arg->set_fast_prov_info.net_idx);
2182         bt_mesh_set_fast_prov_flags_iv_index(arg->set_fast_prov_info.flags,
2183                                              arg->set_fast_prov_info.iv_index);
2184         param.set_fast_prov_info_comp.status_match =
2185             bt_mesh_provisioner_set_dev_uuid_match(arg->set_fast_prov_info.offset,
2186                     arg->set_fast_prov_info.match_len,
2187                     arg->set_fast_prov_info.match_val, false);
2188         break;
2189     case BTC_BLE_MESH_ACT_SET_FAST_PROV_ACTION:
2190         act = ESP_BLE_MESH_SET_FAST_PROV_ACTION_COMP_EVT;
2191         param.set_fast_prov_action_comp.status_action =
2192             bt_mesh_set_fast_prov_action(arg->set_fast_prov_action.action);
2193         break;
2194 #endif /* CONFIG_BLE_MESH_FAST_PROV */
2195 #if CONFIG_BLE_MESH_LOW_POWER
2196     case BTC_BLE_MESH_ACT_LPN_ENABLE:
2197         act = ESP_BLE_MESH_LPN_ENABLE_COMP_EVT;
2198         param.lpn_enable_comp.err_code = bt_mesh_lpn_set(true, false);
2199         break;
2200     case BTC_BLE_MESH_ACT_LPN_DISABLE:
2201         act = ESP_BLE_MESH_LPN_DISABLE_COMP_EVT;
2202         param.lpn_disable_comp.err_code = bt_mesh_lpn_set(false, arg->lpn_disable.force);
2203         break;
2204     case BTC_BLE_MESH_ACT_LPN_POLL:
2205         act = ESP_BLE_MESH_LPN_POLL_COMP_EVT;
2206         param.lpn_poll_comp.err_code = bt_mesh_lpn_poll();
2207         break;
2208 #endif /* CONFIG_BLE_MESH_LOW_POWER */
2209 #if CONFIG_BLE_MESH_GATT_PROXY_CLIENT
2210     case BTC_BLE_MESH_ACT_PROXY_CLIENT_CONNECT:
2211         act = ESP_BLE_MESH_PROXY_CLIENT_CONNECT_COMP_EVT;
2212         memcpy(param.proxy_client_connect_comp.addr, arg->proxy_client_connect.addr, BD_ADDR_LEN);
2213         param.proxy_client_connect_comp.addr_type = arg->proxy_client_connect.addr_type;
2214         param.proxy_client_connect_comp.net_idx = arg->proxy_client_connect.net_idx;
2215         param.proxy_client_connect_comp.err_code =
2216             bt_mesh_proxy_client_connect(arg->proxy_client_connect.addr,
2217                                          arg->proxy_client_connect.addr_type,
2218                                          arg->proxy_client_connect.net_idx);
2219         break;
2220     case BTC_BLE_MESH_ACT_PROXY_CLIENT_DISCONNECT:
2221         act = ESP_BLE_MESH_PROXY_CLIENT_DISCONNECT_COMP_EVT;
2222         param.proxy_client_disconnect_comp.conn_handle = arg->proxy_client_disconnect.conn_handle;
2223         param.proxy_client_disconnect_comp.err_code =
2224             bt_mesh_proxy_client_disconnect(arg->proxy_client_disconnect.conn_handle);
2225         break;
2226     case BTC_BLE_MESH_ACT_PROXY_CLIENT_SET_FILTER_TYPE: {
2227         struct bt_mesh_proxy_cfg_pdu pdu = {
2228             .opcode = BLE_MESH_PROXY_CFG_FILTER_SET,
2229             .set.filter_type = arg->proxy_client_set_filter_type.filter_type,
2230         };
2231         act = ESP_BLE_MESH_PROXY_CLIENT_SET_FILTER_TYPE_COMP_EVT;
2232         param.proxy_client_set_filter_type_comp.conn_handle = arg->proxy_client_set_filter_type.conn_handle;
2233         param.proxy_client_set_filter_type_comp.net_idx = arg->proxy_client_set_filter_type.net_idx;
2234         param.proxy_client_set_filter_type_comp.err_code =
2235             bt_mesh_proxy_client_cfg_send(arg->proxy_client_set_filter_type.conn_handle,
2236                                           arg->proxy_client_set_filter_type.net_idx, &pdu);
2237         break;
2238     }
2239     case BTC_BLE_MESH_ACT_PROXY_CLIENT_ADD_FILTER_ADDR: {
2240         struct bt_mesh_proxy_cfg_pdu pdu = {
2241             .opcode = BLE_MESH_PROXY_CFG_FILTER_ADD,
2242             .add.addr = arg->proxy_client_add_filter_addr.addr,
2243             .add.addr_num = arg->proxy_client_add_filter_addr.addr_num,
2244         };
2245         act = ESP_BLE_MESH_PROXY_CLIENT_ADD_FILTER_ADDR_COMP_EVT;
2246         param.proxy_client_add_filter_addr_comp.conn_handle = arg->proxy_client_add_filter_addr.conn_handle;
2247         param.proxy_client_add_filter_addr_comp.net_idx = arg->proxy_client_add_filter_addr.net_idx;
2248         param.proxy_client_add_filter_addr_comp.err_code =
2249             bt_mesh_proxy_client_cfg_send(arg->proxy_client_add_filter_addr.conn_handle,
2250                                           arg->proxy_client_add_filter_addr.net_idx, &pdu);
2251         break;
2252     }
2253     case BTC_BLE_MESH_ACT_PROXY_CLIENT_REMOVE_FILTER_ADDR: {
2254         struct bt_mesh_proxy_cfg_pdu pdu = {
2255             .opcode = BLE_MESH_PROXY_CFG_FILTER_REMOVE,
2256             .remove.addr = arg->proxy_client_remove_filter_addr.addr,
2257             .remove.addr_num = arg->proxy_client_remove_filter_addr.addr_num,
2258         };
2259         act = ESP_BLE_MESH_PROXY_CLIENT_REMOVE_FILTER_ADDR_COMP_EVT;
2260         param.proxy_client_remove_filter_addr_comp.conn_handle = arg->proxy_client_remove_filter_addr.conn_handle;
2261         param.proxy_client_remove_filter_addr_comp.net_idx = arg->proxy_client_remove_filter_addr.net_idx;
2262         param.proxy_client_remove_filter_addr_comp.err_code =
2263             bt_mesh_proxy_client_cfg_send(arg->proxy_client_remove_filter_addr.conn_handle,
2264                                           arg->proxy_client_remove_filter_addr.net_idx, &pdu);
2265         break;
2266     }
2267 #endif /* CONFIG_BLE_MESH_GATT_PROXY_CLIENT */
2268     case BTC_BLE_MESH_ACT_MODEL_SUBSCRIBE_GROUP_ADDR:
2269         act = ESP_BLE_MESH_MODEL_SUBSCRIBE_GROUP_ADDR_COMP_EVT;
2270         param.model_sub_group_addr_comp.element_addr = arg->model_sub_group_addr.element_addr;
2271         param.model_sub_group_addr_comp.company_id = arg->model_sub_group_addr.company_id;
2272         param.model_sub_group_addr_comp.model_id = arg->model_sub_group_addr.model_id;
2273         param.model_sub_group_addr_comp.group_addr = arg->model_sub_group_addr.group_addr;
2274         param.model_sub_group_addr_comp.err_code =
2275             bt_mesh_model_subscribe_group_addr(arg->model_sub_group_addr.element_addr,
2276                                                arg->model_sub_group_addr.company_id,
2277                                                arg->model_sub_group_addr.model_id,
2278                                                arg->model_sub_group_addr.group_addr);
2279         break;
2280     case BTC_BLE_MESH_ACT_MODEL_UNSUBSCRIBE_GROUP_ADDR:
2281         act = ESP_BLE_MESH_MODEL_UNSUBSCRIBE_GROUP_ADDR_COMP_EVT;
2282         param.model_unsub_group_addr_comp.element_addr = arg->model_unsub_group_addr.element_addr;
2283         param.model_unsub_group_addr_comp.company_id = arg->model_unsub_group_addr.company_id;
2284         param.model_unsub_group_addr_comp.model_id = arg->model_unsub_group_addr.model_id;
2285         param.model_unsub_group_addr_comp.group_addr = arg->model_unsub_group_addr.group_addr;
2286         param.model_unsub_group_addr_comp.err_code =
2287             bt_mesh_model_unsubscribe_group_addr(arg->model_unsub_group_addr.element_addr,
2288                                                  arg->model_unsub_group_addr.company_id,
2289                                                  arg->model_unsub_group_addr.model_id,
2290                                                  arg->model_unsub_group_addr.group_addr);
2291         break;
2292 #if CONFIG_BLE_MESH_DEINIT
2293     case BTC_BLE_MESH_ACT_DEINIT_MESH:
2294         act = ESP_BLE_MESH_DEINIT_MESH_COMP_EVT;
2295         param.deinit_mesh_comp.err_code = bt_mesh_deinit((struct bt_mesh_deinit_param *)&arg->mesh_deinit.param);
2296         /* Temporarily save the deinit semaphore and release it after the mesh deinit complete event is handled in the app layer */
2297         deinit_comp_semaphore = arg->mesh_deinit.semaphore;
2298         break;
2299 #endif /* CONFIG_BLE_MESH_DEINIT */
2300     default:
2301         BT_WARN("%s, Unknown act %d", __func__, msg->act);
2302         return;
2303     }
2304 
2305     /* Callback operation completion events */
2306     btc_ble_mesh_prov_set_complete_cb(&param, act);
2307 
2308     btc_ble_mesh_prov_arg_deep_free(msg);
2309     return;
2310 }
2311 
btc_ble_mesh_prov_cb_handler(btc_msg_t * msg)2312 void btc_ble_mesh_prov_cb_handler(btc_msg_t *msg)
2313 {
2314     esp_ble_mesh_prov_cb_param_t *param = NULL;
2315 
2316     if (!msg) {
2317         BT_ERR("%s, Invalid parameter", __func__);
2318         return;
2319     }
2320 
2321     param = (esp_ble_mesh_prov_cb_param_t *)(msg->arg);
2322 
2323     if (msg->act < ESP_BLE_MESH_PROV_EVT_MAX) {
2324         btc_ble_mesh_prov_cb_to_app(msg->act, param);
2325     } else {
2326         BT_ERR("%s, Unknown act %d", __func__, msg->act);
2327     }
2328 }
2329 
btc_ble_mesh_model_call_handler(btc_msg_t * msg)2330 void btc_ble_mesh_model_call_handler(btc_msg_t *msg)
2331 {
2332     btc_ble_mesh_model_args_t *arg = NULL;
2333     int err = 0;
2334 
2335     if (!msg) {
2336         BT_ERR("%s, Invalid parameter", __func__);
2337         return;
2338     }
2339 
2340     arg = (btc_ble_mesh_model_args_t *)(msg->arg);
2341 
2342     switch (msg->act) {
2343     case BTC_BLE_MESH_ACT_MODEL_PUBLISH: {
2344         if (arg->model_publish.device_role == PROVISIONER) {
2345             /* Currently Provisioner only supports client model */
2346             err = bt_mesh_set_client_model_role((struct bt_mesh_model *)arg->model_publish.model,
2347                                                 arg->model_publish.device_role);
2348             if (err) {
2349                 BT_ERR("Failed to set client role");
2350                 btc_ble_mesh_model_publish_comp_cb(arg->model_publish.model, err);
2351                 break;
2352             }
2353         }
2354         err = bt_mesh_model_publish((struct bt_mesh_model *)arg->model_publish.model);
2355         btc_ble_mesh_model_publish_comp_cb(arg->model_publish.model, err);
2356         break;
2357     }
2358     case BTC_BLE_MESH_ACT_SERVER_MODEL_SEND: {
2359         /* arg->model_send.length contains opcode & payload, plus extra 4-bytes TransMIC */
2360         struct net_buf_simple *buf = bt_mesh_alloc_buf(arg->model_send.length + BLE_MESH_MIC_SHORT);
2361         if (!buf) {
2362             BT_ERR("%s, Out of memory", __func__);
2363             btc_ble_mesh_model_send_comp_cb(arg->model_send.model, arg->model_send.ctx,
2364                                             arg->model_send.opcode, -ENOMEM);
2365             break;
2366         }
2367 
2368         net_buf_simple_add_mem(buf, arg->model_send.data, arg->model_send.length);
2369         arg->model_send.ctx->srv_send = true;
2370 
2371         err = bt_mesh_model_send((struct bt_mesh_model *)arg->model_send.model,
2372                                  (struct bt_mesh_msg_ctx *)arg->model_send.ctx,
2373                                  buf, NULL, NULL);
2374         bt_mesh_free_buf(buf);
2375         btc_ble_mesh_model_send_comp_cb(arg->model_send.model, arg->model_send.ctx,
2376                                         arg->model_send.opcode, err);
2377         break;
2378     }
2379     case BTC_BLE_MESH_ACT_CLIENT_MODEL_SEND: {
2380         /* arg->model_send.length contains opcode & message, plus extra 4-bytes TransMIC */
2381         struct net_buf_simple *buf = bt_mesh_alloc_buf(arg->model_send.length + BLE_MESH_MIC_SHORT);
2382         if (!buf) {
2383             BT_ERR("%s, Out of memory", __func__);
2384             btc_ble_mesh_model_send_comp_cb(arg->model_send.model, arg->model_send.ctx,
2385                                             arg->model_send.opcode, -ENOMEM);
2386             break;
2387         }
2388 
2389         net_buf_simple_add_mem(buf, arg->model_send.data, arg->model_send.length);
2390         bt_mesh_client_common_param_t param = {
2391             .opcode = arg->model_send.opcode,
2392             .model = (struct bt_mesh_model *)arg->model_send.model,
2393             .ctx.net_idx = arg->model_send.ctx->net_idx,
2394             .ctx.app_idx = arg->model_send.ctx->app_idx,
2395             .ctx.addr = arg->model_send.ctx->addr,
2396             .ctx.send_rel = arg->model_send.ctx->send_rel,
2397             .ctx.send_ttl = arg->model_send.ctx->send_ttl,
2398             .ctx.srv_send = false,
2399             .msg_timeout = arg->model_send.msg_timeout,
2400             .msg_role = arg->model_send.device_role,
2401         };
2402         err = bt_mesh_client_send_msg(&param, buf, arg->model_send.need_rsp,
2403                                       btc_ble_mesh_client_model_timeout_cb);
2404         bt_mesh_free_buf(buf);
2405         btc_ble_mesh_model_send_comp_cb(arg->model_send.model, arg->model_send.ctx,
2406                                         arg->model_send.opcode, err);
2407         break;
2408     }
2409 #if CONFIG_BLE_MESH_SERVER_MODEL
2410     case BTC_BLE_MESH_ACT_SERVER_MODEL_UPDATE_STATE:
2411         err = bt_mesh_update_binding_state(
2412                   (struct bt_mesh_model *)arg->model_update_state.model, arg->model_update_state.type,
2413                   (bt_mesh_server_state_value_t *)arg->model_update_state.value);
2414         btc_ble_mesh_server_model_update_state_comp_cb(arg->model_update_state.model,
2415                 arg->model_update_state.type, err);
2416         break;
2417 #endif /* CONFIG_BLE_MESH_SERVER_MODEL */
2418     default:
2419         BT_WARN("%s, Unknown act %d", __func__, msg->act);
2420         break;
2421     }
2422 
2423     btc_ble_mesh_model_arg_deep_free(msg);
2424     return;
2425 }
2426 
btc_ble_mesh_model_cb_handler(btc_msg_t * msg)2427 void btc_ble_mesh_model_cb_handler(btc_msg_t *msg)
2428 {
2429     esp_ble_mesh_model_cb_param_t *param = NULL;
2430 
2431     if (!msg) {
2432         BT_ERR("%s, Invalid parameter", __func__);
2433         return;
2434     }
2435 
2436     param = (esp_ble_mesh_model_cb_param_t *)(msg->arg);
2437 
2438     if (msg->act < ESP_BLE_MESH_MODEL_EVT_MAX) {
2439         btc_ble_mesh_model_cb_to_app(msg->act, param);
2440     } else {
2441         BT_ERR("%s, Unknown act %d", __func__, msg->act);
2442     }
2443 
2444     btc_ble_mesh_model_free_req_data(msg);
2445     return;
2446 }
2447