-
Notifications
You must be signed in to change notification settings - Fork 7k
/
btc_ble_mesh_prov.c
2421 lines (2186 loc) · 104 KB
/
btc_ble_mesh_prov.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include <errno.h>
#include "btc_ble_mesh_prov.h"
#include "btc_ble_mesh_config_model.h"
#include "btc_ble_mesh_health_model.h"
#include "btc_ble_mesh_generic_model.h"
#include "btc_ble_mesh_time_scene_model.h"
#include "btc_ble_mesh_sensor_model.h"
#include "btc_ble_mesh_lighting_model.h"
#include "adv.h"
#include "mesh_kernel.h"
#include "mesh_proxy.h"
#include "mesh.h"
#include "access.h"
#include "prov.h"
#include "settings_uid.h"
#include "proxy_server.h"
#include "proxy_client.h"
#include "provisioner_prov.h"
#include "provisioner_main.h"
#if CONFIG_BLE_MESH_CFG_CLI
#include "cfg_cli.h"
#endif /* CONFIG_BLE_MESH_CFG_CLI */
#if CONFIG_BLE_MESH_HEALTH_CLI
#include "health_cli.h"
#endif /* CONFIG_BLE_MESH_HEALTH_CLI */
#include "cfg_srv.h"
#if CONFIG_BLE_MESH_HEALTH_SRV
#include "health_srv.h"
#endif /* CONFIG_BLE_MESH_HEALTH_SRV */
#if CONFIG_BLE_MESH_GENERIC_CLIENT
#include "generic_client.h"
#endif /* CONFIG_BLE_MESH_GENERIC_CLIENT */
#if CONFIG_BLE_MESH_LIGHTING_CLIENT
#include "lighting_client.h"
#endif /* CONFIG_BLE_MESH_LIGHTING_CLIENT */
#if CONFIG_BLE_MESH_SENSOR_CLI
#include "sensor_client.h"
#endif /* CONFIG_BLE_MESH_SENSOR_CLI */
#if CONFIG_BLE_MESH_TIME_SCENE_CLIENT
#include "time_scene_client.h"
#endif /* CONFIG_BLE_MESH_TIME_SCENE_CLIENT */
#include "client_common.h"
#include "state_binding.h"
#include "local_operation.h"
#include "esp_ble_mesh_common_api.h"
#include "esp_ble_mesh_provisioning_api.h"
#include "esp_ble_mesh_networking_api.h"
static inline void btc_ble_mesh_prov_cb_to_app(esp_ble_mesh_prov_cb_event_t event,
esp_ble_mesh_prov_cb_param_t *param)
{
esp_ble_mesh_prov_cb_t btc_ble_mesh_cb =
(esp_ble_mesh_prov_cb_t)btc_profile_cb_get(BTC_PID_PROV);
if (btc_ble_mesh_cb) {
btc_ble_mesh_cb(event, param);
}
}
static inline void btc_ble_mesh_model_cb_to_app(esp_ble_mesh_model_cb_event_t event,
esp_ble_mesh_model_cb_param_t *param)
{
esp_ble_mesh_model_cb_t btc_ble_mesh_cb =
(esp_ble_mesh_model_cb_t)btc_profile_cb_get(BTC_PID_MODEL);
if (btc_ble_mesh_cb) {
btc_ble_mesh_cb(event, param);
}
}
void btc_ble_mesh_prov_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
{
btc_ble_mesh_prov_args_t *dst = (btc_ble_mesh_prov_args_t *)p_dest;
btc_ble_mesh_prov_args_t *src = (btc_ble_mesh_prov_args_t *)p_src;
if (!msg || !dst || !src) {
BT_ERR("%s, Invalid parameter", __func__);
return;
}
switch (msg->act) {
case BTC_BLE_MESH_ACT_PROXY_CLIENT_ADD_FILTER_ADDR:
dst->proxy_client_add_filter_addr.addr = (uint16_t *)bt_mesh_calloc(src->proxy_client_add_filter_addr.addr_num << 1);
if (dst->proxy_client_add_filter_addr.addr) {
memcpy(dst->proxy_client_add_filter_addr.addr, src->proxy_client_add_filter_addr.addr,
src->proxy_client_add_filter_addr.addr_num << 1);
} else {
BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
}
break;
case BTC_BLE_MESH_ACT_PROXY_CLIENT_REMOVE_FILTER_ADDR:
dst->proxy_client_remove_filter_addr.addr = bt_mesh_calloc(src->proxy_client_remove_filter_addr.addr_num << 1);
if (dst->proxy_client_remove_filter_addr.addr) {
memcpy(dst->proxy_client_remove_filter_addr.addr, src->proxy_client_remove_filter_addr.addr,
src->proxy_client_remove_filter_addr.addr_num << 1);
} else {
BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
}
break;
case BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA:
dst->store_node_comp_data.data = bt_mesh_calloc(src->store_node_comp_data.length);
if (dst->store_node_comp_data.data) {
memcpy(dst->store_node_comp_data.data, src->store_node_comp_data.data, src->store_node_comp_data.length);
} else {
BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
}
break;
default:
BT_DBG("%s, Unknown act %d", __func__, msg->act);
break;
}
}
static void btc_ble_mesh_prov_arg_deep_free(btc_msg_t *msg)
{
btc_ble_mesh_prov_args_t *arg = NULL;
if (!msg || !msg->arg) {
BT_ERR("%s, Invalid parameter", __func__);
return;
}
arg = (btc_ble_mesh_prov_args_t *)(msg->arg);
switch (msg->act) {
case BTC_BLE_MESH_ACT_PROXY_CLIENT_ADD_FILTER_ADDR:
if (arg->proxy_client_add_filter_addr.addr) {
bt_mesh_free(arg->proxy_client_add_filter_addr.addr);
}
break;
case BTC_BLE_MESH_ACT_PROXY_CLIENT_REMOVE_FILTER_ADDR:
if (arg->proxy_client_remove_filter_addr.addr) {
bt_mesh_free(arg->proxy_client_remove_filter_addr.addr);
}
break;
case BTC_BLE_MESH_ACT_PROVISIONER_STORE_NODE_COMP_DATA:
if (arg->store_node_comp_data.data) {
bt_mesh_free(arg->store_node_comp_data.data);
}
break;
default:
break;
}
}
void btc_ble_mesh_model_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
{
btc_ble_mesh_model_args_t *dst = (btc_ble_mesh_model_args_t *)p_dest;
btc_ble_mesh_model_args_t *src = (btc_ble_mesh_model_args_t *)p_src;
if (!msg || !dst || !src) {
BT_ERR("%s, Invalid parameter", __func__);
return;
}
switch (msg->act) {
case BTC_BLE_MESH_ACT_SERVER_MODEL_SEND:
case BTC_BLE_MESH_ACT_CLIENT_MODEL_SEND: {
dst->model_send.data = src->model_send.length ? (uint8_t *)bt_mesh_malloc(src->model_send.length) : NULL;
dst->model_send.ctx = bt_mesh_malloc(sizeof(esp_ble_mesh_msg_ctx_t));
if (src->model_send.length) {
if (dst->model_send.data) {
memcpy(dst->model_send.data, src->model_send.data, src->model_send.length);
} else {
BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
}
}
if (dst->model_send.ctx) {
memcpy(dst->model_send.ctx, src->model_send.ctx, sizeof(esp_ble_mesh_msg_ctx_t));
} else {
BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
}
break;
}
case BTC_BLE_MESH_ACT_SERVER_MODEL_UPDATE_STATE:
dst->model_update_state.value = bt_mesh_malloc(sizeof(esp_ble_mesh_server_state_value_t));
if (dst->model_update_state.value) {
memcpy(dst->model_update_state.value, src->model_update_state.value,
sizeof(esp_ble_mesh_server_state_value_t));
} else {
BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
}
break;
default:
BT_DBG("%s, Unknown act %d", __func__, msg->act);
break;
}
}
static void btc_ble_mesh_model_arg_deep_free(btc_msg_t *msg)
{
btc_ble_mesh_model_args_t *arg = NULL;
if (!msg || !msg->arg) {
BT_ERR("%s, Invalid parameter", __func__);
return;
}
arg = (btc_ble_mesh_model_args_t *)(msg->arg);
switch (msg->act) {
case BTC_BLE_MESH_ACT_SERVER_MODEL_SEND:
case BTC_BLE_MESH_ACT_CLIENT_MODEL_SEND:
if (arg->model_send.data) {
bt_mesh_free(arg->model_send.data);
}
if (arg->model_send.ctx) {
bt_mesh_free(arg->model_send.ctx);
}
break;
case BTC_BLE_MESH_ACT_SERVER_MODEL_UPDATE_STATE:
if (arg->model_update_state.value) {
bt_mesh_free(arg->model_update_state.value);
}
break;
default:
break;
}
return;
}
static void btc_ble_mesh_model_copy_req_data(btc_msg_t *msg, void *p_dest, void *p_src)
{
esp_ble_mesh_model_cb_param_t *p_dest_data = (esp_ble_mesh_model_cb_param_t *)p_dest;
esp_ble_mesh_model_cb_param_t *p_src_data = (esp_ble_mesh_model_cb_param_t *)p_src;
if (!msg || !p_src_data || !p_dest_data) {
BT_ERR("%s, Invalid parameter", __func__);
return;
}
switch (msg->act) {
case ESP_BLE_MESH_MODEL_OPERATION_EVT: {
if (p_src_data->model_operation.ctx && p_src_data->model_operation.msg) {
p_dest_data->model_operation.ctx = bt_mesh_malloc(sizeof(esp_ble_mesh_msg_ctx_t));
p_dest_data->model_operation.msg = p_src_data->model_operation.length ? (uint8_t *)bt_mesh_malloc(p_src_data->model_operation.length) : NULL;
if (p_dest_data->model_operation.ctx) {
memcpy(p_dest_data->model_operation.ctx, p_src_data->model_operation.ctx, sizeof(esp_ble_mesh_msg_ctx_t));
} else {
BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
}
if (p_src_data->model_operation.length) {
if (p_dest_data->model_operation.msg) {
memcpy(p_dest_data->model_operation.msg, p_src_data->model_operation.msg, p_src_data->model_operation.length);
} else {
BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
}
}
}
break;
}
case ESP_BLE_MESH_CLIENT_MODEL_RECV_PUBLISH_MSG_EVT: {
if (p_src_data->client_recv_publish_msg.ctx && p_src_data->client_recv_publish_msg.msg) {
p_dest_data->client_recv_publish_msg.ctx = bt_mesh_malloc(sizeof(esp_ble_mesh_msg_ctx_t));
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;
if (p_dest_data->client_recv_publish_msg.ctx) {
memcpy(p_dest_data->client_recv_publish_msg.ctx, p_src_data->client_recv_publish_msg.ctx, sizeof(esp_ble_mesh_msg_ctx_t));
} else {
BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
}
if (p_src_data->client_recv_publish_msg.length) {
if (p_dest_data->client_recv_publish_msg.msg) {
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);
} else {
BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
}
}
}
break;
}
case ESP_BLE_MESH_MODEL_SEND_COMP_EVT: {
if (p_src_data->model_send_comp.ctx) {
p_dest_data->model_send_comp.ctx = bt_mesh_malloc(sizeof(esp_ble_mesh_msg_ctx_t));
if (p_dest_data->model_send_comp.ctx) {
memcpy(p_dest_data->model_send_comp.ctx, p_src_data->model_send_comp.ctx, sizeof(esp_ble_mesh_msg_ctx_t));
} else {
BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
}
}
break;
}
case ESP_BLE_MESH_CLIENT_MODEL_SEND_TIMEOUT_EVT: {
if (p_src_data->client_send_timeout.ctx) {
p_dest_data->client_send_timeout.ctx = bt_mesh_malloc(sizeof(esp_ble_mesh_msg_ctx_t));
if (p_dest_data->client_send_timeout.ctx) {
memcpy(p_dest_data->client_send_timeout.ctx, p_src_data->client_send_timeout.ctx, sizeof(esp_ble_mesh_msg_ctx_t));
} else {
BT_ERR("%s, Out of memory, act %d", __func__, msg->act);
}
}
break;
}
default:
break;
}
}
static void btc_ble_mesh_model_free_req_data(btc_msg_t *msg)
{
esp_ble_mesh_model_cb_param_t *arg = NULL;
if (!msg || !msg->arg) {
BT_ERR("%s, Invalid parameter", __func__);
return;
}
arg = (esp_ble_mesh_model_cb_param_t *)(msg->arg);
switch (msg->act) {
case ESP_BLE_MESH_MODEL_OPERATION_EVT: {
if (arg->model_operation.msg) {
bt_mesh_free(arg->model_operation.msg);
}
if (arg->model_operation.ctx) {
bt_mesh_free(arg->model_operation.ctx);
}
break;
}
case ESP_BLE_MESH_CLIENT_MODEL_RECV_PUBLISH_MSG_EVT: {
if (arg->client_recv_publish_msg.msg) {
bt_mesh_free(arg->client_recv_publish_msg.msg);
}
if (arg->client_recv_publish_msg.ctx) {
bt_mesh_free(arg->client_recv_publish_msg.ctx);
}
break;
}
case ESP_BLE_MESH_MODEL_SEND_COMP_EVT: {
if (arg->model_send_comp.ctx) {
bt_mesh_free(arg->model_send_comp.ctx);
}
break;
}
case ESP_BLE_MESH_CLIENT_MODEL_SEND_TIMEOUT_EVT: {
if (arg->client_send_timeout.ctx) {
bt_mesh_free(arg->client_send_timeout.ctx);
}
break;
}
default:
break;
}
}
static bt_status_t btc_ble_mesh_model_callback(esp_ble_mesh_model_cb_param_t *param, uint8_t act)
{
btc_msg_t msg = {0};
bt_status_t ret = BT_STATUS_SUCCESS;
BT_DBG("%s", __func__);
/* If corresponding callback is not registered, event will not be posted. */
if (!btc_profile_cb_get(BTC_PID_MODEL)) {
return BT_STATUS_SUCCESS;
}
msg.sig = BTC_SIG_API_CB;
msg.pid = BTC_PID_MODEL;
msg.act = act;
ret = btc_transfer_context(&msg, param, param == NULL ? 0 : sizeof(esp_ble_mesh_model_cb_param_t),
btc_ble_mesh_model_copy_req_data);
if (ret != BT_STATUS_SUCCESS) {
BT_ERR("btc_transfer_context failed");
}
return ret;
}
static void btc_ble_mesh_server_model_op_cb(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
esp_ble_mesh_model_cb_param_t mesh_param = {0};
mesh_param.model_operation.opcode = ctx->recv_op;
mesh_param.model_operation.model = (esp_ble_mesh_model_t *)model;
mesh_param.model_operation.ctx = (esp_ble_mesh_msg_ctx_t *)ctx;
mesh_param.model_operation.length = buf->len;
mesh_param.model_operation.msg = buf->data;
btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_MODEL_OPERATION_EVT);
return;
}
static void btc_ble_mesh_client_model_op_cb(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
esp_ble_mesh_model_cb_param_t mesh_param = {0};
bt_mesh_client_node_t *node = NULL;
if (!model || !model->user_data || !ctx || !buf) {
BT_ERR("%s, Invalid parameter", __func__);
return;
}
bt_mesh_client_model_lock();
node = bt_mesh_is_client_recv_publish_msg(model, ctx, buf, false);
if (node == NULL) {
mesh_param.client_recv_publish_msg.opcode = ctx->recv_op;
mesh_param.client_recv_publish_msg.model = (esp_ble_mesh_model_t *)model;
mesh_param.client_recv_publish_msg.ctx = (esp_ble_mesh_msg_ctx_t *)ctx;
mesh_param.client_recv_publish_msg.length = buf->len;
mesh_param.client_recv_publish_msg.msg = buf->data;
btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_CLIENT_MODEL_RECV_PUBLISH_MSG_EVT);
} else {
mesh_param.model_operation.opcode = ctx->recv_op;
mesh_param.model_operation.model = (esp_ble_mesh_model_t *)model;
mesh_param.model_operation.ctx = (esp_ble_mesh_msg_ctx_t *)ctx;
mesh_param.model_operation.length = buf->len;
mesh_param.model_operation.msg = buf->data;
if (!k_delayed_work_free(&node->timer)) {
bt_mesh_client_free_node(node);
btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_MODEL_OPERATION_EVT);
}
}
bt_mesh_client_model_unlock();
return;
}
static void btc_ble_mesh_client_model_timeout_cb(struct k_work *work)
{
esp_ble_mesh_model_cb_param_t mesh_param = {0};
struct k_delayed_work *timer = NULL;
bt_mesh_client_node_t *node = NULL;
struct bt_mesh_msg_ctx ctx = {0};
bt_mesh_client_model_lock();
timer = CONTAINER_OF(work, struct k_delayed_work, work);
if (timer && !k_delayed_work_free(timer)) {
node = CONTAINER_OF(work, bt_mesh_client_node_t, timer.work);
if (node) {
memcpy(&ctx, &node->ctx, sizeof(ctx));
mesh_param.client_send_timeout.opcode = node->opcode;
mesh_param.client_send_timeout.model = (esp_ble_mesh_model_t *)ctx.model;
mesh_param.client_send_timeout.ctx = (esp_ble_mesh_msg_ctx_t *)&ctx;
bt_mesh_client_free_node(node);
btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_CLIENT_MODEL_SEND_TIMEOUT_EVT);
}
}
bt_mesh_client_model_unlock();
return;
}
static void 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)
{
esp_ble_mesh_model_cb_param_t mesh_param = {0};
mesh_param.model_send_comp.err_code = err;
mesh_param.model_send_comp.opcode = opcode;
mesh_param.model_send_comp.model = model;
mesh_param.model_send_comp.ctx = ctx;
btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_MODEL_SEND_COMP_EVT);
return;
}
static void btc_ble_mesh_model_publish_comp_cb(esp_ble_mesh_model_t *model, int err)
{
esp_ble_mesh_model_cb_param_t mesh_param = {0};
mesh_param.model_publish_comp.err_code = err;
mesh_param.model_publish_comp.model = model;
btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_MODEL_PUBLISH_COMP_EVT);
return;
}
static int btc_ble_mesh_model_publish_update(struct bt_mesh_model *mod)
{
esp_ble_mesh_model_cb_param_t mesh_param = {0};
bt_status_t ret = BT_STATUS_SUCCESS;
BT_DBG("%s", __func__);
mesh_param.model_publish_update.model = (esp_ble_mesh_model_t *)mod;
ret = btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_MODEL_PUBLISH_UPDATE_EVT);
return (ret == BT_STATUS_SUCCESS) ? 0 : -1;
}
#if CONFIG_BLE_MESH_SERVER_MODEL
static void 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)
{
esp_ble_mesh_model_cb_param_t mesh_param = {0};
mesh_param.server_model_update_state.err_code = err;
mesh_param.server_model_update_state.model = model;
mesh_param.server_model_update_state.type = type;
btc_ble_mesh_model_callback(&mesh_param, ESP_BLE_MESH_SERVER_MODEL_UPDATE_STATE_COMP_EVT);
return;
}
#endif /* CONFIG_BLE_MESH_SERVER_MODEL */
static bt_status_t btc_ble_mesh_prov_callback(esp_ble_mesh_prov_cb_param_t *param, uint8_t act)
{
btc_msg_t msg = {0};
bt_status_t ret = BT_STATUS_SUCCESS;
BT_DBG("%s", __func__);
/* If corresponding callback is not registered, event will not be posted. */
if (!btc_profile_cb_get(BTC_PID_PROV)) {
return BT_STATUS_SUCCESS;
}
msg.sig = BTC_SIG_API_CB;
msg.pid = BTC_PID_PROV;
msg.act = act;
ret = btc_transfer_context(&msg, param, param == NULL ? 0 : sizeof(esp_ble_mesh_prov_cb_param_t), NULL);
if (ret != BT_STATUS_SUCCESS) {
BT_ERR("btc_transfer_context failed");
}
return ret;
}
#if CONFIG_BLE_MESH_NODE
static void btc_ble_mesh_oob_pub_key_cb(void)
{
BT_DBG("%s", __func__);
btc_ble_mesh_prov_callback(NULL, ESP_BLE_MESH_NODE_PROV_OOB_PUB_KEY_EVT);
return;
}
static int btc_ble_mesh_output_number_cb(bt_mesh_output_action_t act, uint32_t num)
{
esp_ble_mesh_prov_cb_param_t mesh_param = {0};
bt_status_t ret = BT_STATUS_SUCCESS;
BT_DBG("%s", __func__);
mesh_param.node_prov_output_num.action = (esp_ble_mesh_output_action_t)act;
mesh_param.node_prov_output_num.number = num;
ret = btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_OUTPUT_NUMBER_EVT);
return (ret == BT_STATUS_SUCCESS) ? 0 : -1;
}
static int btc_ble_mesh_output_string_cb(const char *str)
{
esp_ble_mesh_prov_cb_param_t mesh_param = {0};
bt_status_t ret = BT_STATUS_SUCCESS;
BT_DBG("%s", __func__);
strncpy(mesh_param.node_prov_output_str.string, str,
MIN(strlen(str), sizeof(mesh_param.node_prov_output_str.string)));
ret = btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_OUTPUT_STRING_EVT);
return (ret == BT_STATUS_SUCCESS) ? 0 : -1;
}
static int btc_ble_mesh_input_cb(bt_mesh_input_action_t act, uint8_t size)
{
esp_ble_mesh_prov_cb_param_t mesh_param = {0};
bt_status_t ret = BT_STATUS_SUCCESS;
BT_DBG("%s", __func__);
mesh_param.node_prov_input.action = (esp_ble_mesh_input_action_t)act;
mesh_param.node_prov_input.size = size;
ret = btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_INPUT_EVT);
return (ret == BT_STATUS_SUCCESS) ? 0 : -1;
}
static void btc_ble_mesh_link_open_cb(bt_mesh_prov_bearer_t bearer)
{
esp_ble_mesh_prov_cb_param_t mesh_param = {0};
BT_DBG("%s", __func__);
mesh_param.node_prov_link_open.bearer = (esp_ble_mesh_prov_bearer_t)bearer;
btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_LINK_OPEN_EVT);
return;
}
static void btc_ble_mesh_link_close_cb(bt_mesh_prov_bearer_t bearer)
{
esp_ble_mesh_prov_cb_param_t mesh_param = {0};
BT_DBG("%s", __func__);
mesh_param.node_prov_link_close.bearer = (esp_ble_mesh_prov_bearer_t)bearer;
btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_LINK_CLOSE_EVT);
return;
}
static void 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)
{
esp_ble_mesh_prov_cb_param_t mesh_param = {0};
BT_DBG("%s", __func__);
mesh_param.node_prov_complete.net_idx = net_idx;
memcpy(mesh_param.node_prov_complete.net_key, net_key, 16);
mesh_param.node_prov_complete.addr = addr;
mesh_param.node_prov_complete.flags = flags;
mesh_param.node_prov_complete.iv_index = iv_index;
btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_NODE_PROV_COMPLETE_EVT);
return;
}
static void btc_ble_mesh_reset_cb(void)
{
BT_DBG("%s", __func__);
btc_ble_mesh_prov_callback(NULL, ESP_BLE_MESH_NODE_PROV_RESET_EVT);
return;
}
const uint8_t *btc_ble_mesh_node_get_local_net_key(uint16_t net_idx)
{
return bt_mesh_node_get_local_net_key(net_idx);
}
const uint8_t *btc_ble_mesh_node_get_local_app_key(uint16_t app_idx)
{
return bt_mesh_node_get_local_app_key(app_idx);
}
#endif /* CONFIG_BLE_MESH_NODE */
static void btc_ble_mesh_prov_register_complete_cb(int err_code)
{
esp_ble_mesh_prov_cb_param_t mesh_param = {0};
BT_DBG("%s", __func__);
mesh_param.prov_register_comp.err_code = err_code;
btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROV_REGISTER_COMP_EVT);
return;
}
static void btc_ble_mesh_prov_set_complete_cb(esp_ble_mesh_prov_cb_param_t *param, uint8_t act)
{
BT_DBG("%s", __func__);
btc_ble_mesh_prov_callback(param, act);
return;
}
#if CONFIG_BLE_MESH_PROVISIONER
static void 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)
{
esp_ble_mesh_prov_cb_param_t mesh_param = {0};
BT_DBG("%s", __func__);
if (addr == NULL || dev_uuid == NULL ||
(bearer != BLE_MESH_PROV_ADV && bearer != BLE_MESH_PROV_GATT)) {
BT_ERR("%s, Invalid parameter", __func__);
return;
}
memcpy(mesh_param.provisioner_recv_unprov_adv_pkt.dev_uuid, dev_uuid, 16);
memcpy(mesh_param.provisioner_recv_unprov_adv_pkt.addr, addr, BLE_MESH_ADDR_LEN);
mesh_param.provisioner_recv_unprov_adv_pkt.addr_type = addr_type;
mesh_param.provisioner_recv_unprov_adv_pkt.oob_info = oob_info;
mesh_param.provisioner_recv_unprov_adv_pkt.adv_type = adv_type;
mesh_param.provisioner_recv_unprov_adv_pkt.bearer = bearer;
mesh_param.provisioner_recv_unprov_adv_pkt.rssi = rssi;
btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_RECV_UNPROV_ADV_PKT_EVT);
return;
}
static int btc_ble_mesh_provisioner_prov_read_oob_pub_key_cb(uint8_t link_idx)
{
esp_ble_mesh_prov_cb_param_t mesh_param = {0};
bt_status_t ret = BT_STATUS_SUCCESS;
BT_DBG("%s", __func__);
mesh_param.provisioner_prov_read_oob_pub_key.link_idx = link_idx;
ret = btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_READ_OOB_PUB_KEY_EVT);
return (ret == BT_STATUS_SUCCESS) ? 0 : -1;
}
static int btc_ble_mesh_provisioner_prov_input_cb(uint8_t method, bt_mesh_output_action_t act,
uint8_t size, uint8_t link_idx)
{
esp_ble_mesh_prov_cb_param_t mesh_param = {0};
bt_status_t ret = BT_STATUS_SUCCESS;
BT_DBG("%s", __func__);
mesh_param.provisioner_prov_input.method = (esp_ble_mesh_oob_method_t)method;
mesh_param.provisioner_prov_input.action = (esp_ble_mesh_output_action_t)act;
mesh_param.provisioner_prov_input.size = size;
mesh_param.provisioner_prov_input.link_idx = link_idx;
ret = btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_INPUT_EVT);
return (ret == BT_STATUS_SUCCESS) ? 0 : -1;
}
static int 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)
{
esp_ble_mesh_prov_cb_param_t mesh_param = {0};
bt_status_t ret = BT_STATUS_SUCCESS;
BT_DBG("%s", __func__);
mesh_param.provisioner_prov_output.method = (esp_ble_mesh_oob_method_t)method;
mesh_param.provisioner_prov_output.action = (esp_ble_mesh_input_action_t)act;
mesh_param.provisioner_prov_output.size = size;
mesh_param.provisioner_prov_output.link_idx = link_idx;
if (act == BLE_MESH_ENTER_STRING) {
strncpy(mesh_param.provisioner_prov_output.string, (char *)data, size);
} else {
mesh_param.provisioner_prov_output.number = sys_get_le32((uint8_t *)data);
}
ret = btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_OUTPUT_EVT);
return (ret == BT_STATUS_SUCCESS) ? 0 : -1;
}
static void btc_ble_mesh_provisioner_link_open_cb(bt_mesh_prov_bearer_t bearer)
{
esp_ble_mesh_prov_cb_param_t mesh_param = {0};
BT_DBG("%s", __func__);
mesh_param.provisioner_prov_link_open.bearer = (esp_ble_mesh_prov_bearer_t)bearer;
btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_LINK_OPEN_EVT);
return;
}
static void btc_ble_mesh_provisioner_link_close_cb(bt_mesh_prov_bearer_t bearer, uint8_t reason)
{
esp_ble_mesh_prov_cb_param_t mesh_param = {0};
BT_DBG("%s", __func__);
mesh_param.provisioner_prov_link_close.bearer = (esp_ble_mesh_prov_bearer_t)bearer;
mesh_param.provisioner_prov_link_close.reason = reason;
btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_LINK_CLOSE_EVT);
return;
}
static void 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)
{
esp_ble_mesh_prov_cb_param_t mesh_param = {0};
BT_DBG("%s", __func__);
mesh_param.provisioner_prov_complete.node_idx = node_idx;
mesh_param.provisioner_prov_complete.unicast_addr = unicast_addr;
mesh_param.provisioner_prov_complete.element_num = element_num;
mesh_param.provisioner_prov_complete.netkey_idx = netkey_idx;
memcpy(mesh_param.provisioner_prov_complete.device_uuid, device_uuid, sizeof(esp_ble_mesh_octet16_t));
btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_PROV_COMPLETE_EVT);
return;
}
esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_uuid(const uint8_t uuid[16])
{
return (esp_ble_mesh_node_t *)bt_mesh_provisioner_get_node_with_uuid(uuid);
}
esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_addr(uint16_t unicast_addr)
{
return (esp_ble_mesh_node_t *)bt_mesh_provisioner_get_node_with_addr(unicast_addr);
}
esp_ble_mesh_node_t *btc_ble_mesh_provisioner_get_node_with_name(const char *name)
{
return (esp_ble_mesh_node_t *)bt_mesh_provisioner_get_node_with_name(name);
}
uint16_t btc_ble_mesh_provisioner_get_prov_node_count(void)
{
return bt_mesh_provisioner_get_node_count();
}
const esp_ble_mesh_node_t **btc_ble_mesh_provisioner_get_node_table_entry(void)
{
return (const esp_ble_mesh_node_t **)bt_mesh_provisioner_get_node_table_entry();
}
#if CONFIG_BLE_MESH_PROVISIONER_RECV_HB
static void 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)
{
esp_ble_mesh_prov_cb_param_t mesh_param = {0};
mesh_param.provisioner_recv_heartbeat.hb_src = hb_src;
mesh_param.provisioner_recv_heartbeat.hb_dst = hb_dst;
mesh_param.provisioner_recv_heartbeat.init_ttl = init_ttl;
mesh_param.provisioner_recv_heartbeat.rx_ttl = rx_ttl;
mesh_param.provisioner_recv_heartbeat.hops = hops;
mesh_param.provisioner_recv_heartbeat.feature = feat;
mesh_param.provisioner_recv_heartbeat.rssi = rssi;
btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROVISIONER_RECV_HEARTBEAT_MESSAGE_EVT);
}
#endif /* CONFIG_BLE_MESH_PROVISIONER_RECV_HB */
#if CONFIG_BLE_MESH_USE_MULTIPLE_NAMESPACE
const char *btc_ble_mesh_provisioner_get_settings_uid(uint8_t index)
{
return bt_mesh_provisioner_get_settings_uid(index);
}
uint8_t btc_ble_mesh_provisioner_get_settings_index(const char *uid)
{
return bt_mesh_provisioner_get_settings_index(uid);
}
uint8_t btc_ble_mesh_provisioner_get_free_settings_count(void)
{
return bt_mesh_provisioner_get_free_settings_count();
}
#endif /* CONFIG_BLE_MESH_USE_MULTIPLE_NAMESPACE */
#endif /* CONFIG_BLE_MESH_PROVISIONER */
static void btc_ble_mesh_node_recv_heartbeat_cb(uint8_t hops, uint16_t feature)
{
esp_ble_mesh_prov_cb_param_t mesh_param = {0};
BT_DBG("%s", __func__);
mesh_param.heartbeat_msg_recv.hops = hops;
mesh_param.heartbeat_msg_recv.feature = feature;
btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_HEARTBEAT_MESSAGE_RECV_EVT);
return;
}
#if CONFIG_BLE_MESH_LOW_POWER
static void btc_ble_mesh_lpn_cb(uint16_t friend_addr, bool established)
{
esp_ble_mesh_prov_cb_param_t mesh_param = {0};
uint8_t act = 0U;
BT_DBG("%s", __func__);
if (established) {
mesh_param.lpn_friendship_establish.friend_addr = friend_addr;
act = ESP_BLE_MESH_LPN_FRIENDSHIP_ESTABLISH_EVT;
} else {
mesh_param.lpn_friendship_terminate.friend_addr = friend_addr;
act = ESP_BLE_MESH_LPN_FRIENDSHIP_TERMINATE_EVT;
}
btc_ble_mesh_prov_callback(&mesh_param, act);
return;
}
#endif /* CONFIG_BLE_MESH_LOW_POWER */
#if CONFIG_BLE_MESH_FRIEND
void btc_ble_mesh_friend_cb(bool establish, uint16_t lpn_addr, uint8_t reason)
{
esp_ble_mesh_prov_cb_param_t mesh_param = {0};
uint8_t act = 0U;
BT_DBG("%s", __func__);
if (!BLE_MESH_ADDR_IS_UNICAST(lpn_addr)) {
BT_ERR("Not a unicast lpn address 0x%04x", lpn_addr);
return;
}
if (establish) {
mesh_param.frnd_friendship_establish.lpn_addr = lpn_addr;
act = ESP_BLE_MESH_FRIEND_FRIENDSHIP_ESTABLISH_EVT;
} else {
mesh_param.frnd_friendship_terminate.lpn_addr = lpn_addr;
mesh_param.frnd_friendship_terminate.reason = reason;
act = ESP_BLE_MESH_FRIEND_FRIENDSHIP_TERMINATE_EVT;
}
btc_ble_mesh_prov_callback(&mesh_param, act);
return;
}
#endif /* CONFIG_BLE_MESH_FRIEND */
#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT
static void 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)
{
esp_ble_mesh_prov_cb_param_t mesh_param = {0};
if (!addr || !ctx || type != BLE_MESH_PROXY_ADV_NET_ID) {
BT_ERR("%s, Invalid parameter", __func__);
return;
}
BT_DBG("%s", __func__);
mesh_param.proxy_client_recv_adv_pkt.addr_type = addr->type;
memcpy(mesh_param.proxy_client_recv_adv_pkt.addr, addr->val, BD_ADDR_LEN);
mesh_param.proxy_client_recv_adv_pkt.net_idx = ctx->net_id.net_idx;
memcpy(mesh_param.proxy_client_recv_adv_pkt.net_id, ctx->net_id.net_id, 8);
mesh_param.proxy_client_recv_adv_pkt.rssi = rssi;
btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROXY_CLIENT_RECV_ADV_PKT_EVT);
return;
}
static void btc_ble_mesh_proxy_client_connect_cb(const bt_mesh_addr_t *addr,
uint8_t conn_handle, uint16_t net_idx)
{
esp_ble_mesh_prov_cb_param_t mesh_param = {0};
if (!addr || conn_handle >= BLE_MESH_MAX_CONN) {
BT_ERR("%s, Invalid parameter", __func__);
return;
}
BT_DBG("%s", __func__);
mesh_param.proxy_client_connected.addr_type = addr->type;
memcpy(mesh_param.proxy_client_connected.addr, addr->val, BD_ADDR_LEN);
mesh_param.proxy_client_connected.conn_handle = conn_handle;
mesh_param.proxy_client_connected.net_idx = net_idx;
btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROXY_CLIENT_CONNECTED_EVT);
return;
}
static void btc_ble_mesh_proxy_client_disconnect_cb(const bt_mesh_addr_t *addr, uint8_t conn_handle,
uint16_t net_idx, uint8_t reason)
{
esp_ble_mesh_prov_cb_param_t mesh_param = {0};
if (!addr || conn_handle >= BLE_MESH_MAX_CONN) {
BT_ERR("%s, Invalid parameter", __func__);
return;
}
BT_DBG("%s", __func__);
mesh_param.proxy_client_disconnected.addr_type = addr->type;
memcpy(mesh_param.proxy_client_disconnected.addr, addr->val, BD_ADDR_LEN);
mesh_param.proxy_client_disconnected.conn_handle = conn_handle;
mesh_param.proxy_client_disconnected.net_idx = net_idx;
mesh_param.proxy_client_disconnected.reason = reason;
btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROXY_CLIENT_DISCONNECTED_EVT);
return;
}
static void 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)
{
esp_ble_mesh_prov_cb_param_t mesh_param = {0};
if (conn_handle >= BLE_MESH_MAX_CONN) {
BT_ERR("%s, Invalid parameter", __func__);
return;
}
BT_DBG("%s", __func__);
mesh_param.proxy_client_recv_filter_status.conn_handle = conn_handle;
mesh_param.proxy_client_recv_filter_status.server_addr = src;
mesh_param.proxy_client_recv_filter_status.net_idx = net_idx;
mesh_param.proxy_client_recv_filter_status.filter_type = filter_type;
mesh_param.proxy_client_recv_filter_status.list_size = list_size;
btc_ble_mesh_prov_callback(&mesh_param, ESP_BLE_MESH_PROXY_CLIENT_RECV_FILTER_STATUS_EVT);