-
Notifications
You must be signed in to change notification settings - Fork 7.2k
/
mesh_bearer_adapt.c
1991 lines (1678 loc) · 69 KB
/
mesh_bearer_adapt.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 Nordic Semiconductor ASA
* SPDX-FileCopyrightText: 2015-2016 Intel Corporation
* SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include <errno.h>
#include "bta/bta_api.h"
#include "bta/bta_gatt_api.h"
#include "bta/bta_gatt_common.h"
#include "bta_gattc_int.h"
#include "stack/btm_ble_api.h"
#include "p_256_ecc_pp.h"
#include "osi/future.h"
#include "device/controller.h"
#include "mbedtls/aes.h"
#include <tinycrypt/aes.h>
#include <tinycrypt/constants.h>
#include "mesh_hci.h"
#include "mesh_bearer_adapt.h"
#include "mesh_common.h"
#include "provisioner_prov.h"
struct bt_mesh_dev bt_mesh_dev;
#define BLE_MESH_BTM_CHECK_STATUS(func) do { \
tBTM_STATUS __status = (func); \
if ((__status != BTM_SUCCESS) && (__status != BTM_CMD_STARTED)) { \
BT_ERR("%s, Invalid status %d", __func__, __status); \
return -1; \
} \
} while(0);
#define BLE_MESH_GATT_GET_CONN_ID(conn_id) (((uint16_t)(conn_id)) >> 8)
#define BLE_MESH_GATT_CREATE_CONN_ID(gatt_if, conn_id) ((uint16_t)((((uint8_t)(conn_id)) << 8) | ((uint8_t)(gatt_if))))
/* We don't need to manage the BLE_MESH_DEV_ADVERTISING flags in the version of Bluedroid,
* it will manage it in the BTM layer.
*/
#define BLE_MESH_DEV 0
/* P-256 Variables */
static uint8_t bt_mesh_public_key[64];
static BT_OCTET32 bt_mesh_private_key;
/* Scan related functions */
static bt_mesh_scan_cb_t *bt_mesh_scan_dev_found_cb;
static void bt_mesh_scan_result_callback(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data);
#if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \
CONFIG_BLE_MESH_GATT_PROXY_SERVER
/* Using UUID with a fixed pattern 0x96 for BLE Mesh GATT Proxy Server */
#define BLE_MESH_GATTS_APP_UUID_BYTE 0x96
/* the gatt database list to save the attribute table */
static sys_slist_t bt_mesh_gatts_db;
/* Static Variables */
static struct bt_mesh_conn bt_mesh_gatts_conn[BLE_MESH_MAX_CONN];
static struct bt_mesh_conn_cb *bt_mesh_gatts_conn_cb;
static tBTA_GATTS_IF bt_mesh_gatts_if;
static BD_ADDR bt_mesh_gatts_addr;
static uint16_t svc_handle, char_handle;
static future_t *future_mesh;
/* Static Functions */
static struct bt_mesh_gatt_attr *bt_mesh_gatts_find_attr_by_handle(uint16_t handle);
#endif
#if (CONFIG_BLE_MESH_PROVISIONER && CONFIG_BLE_MESH_PB_GATT) || \
CONFIG_BLE_MESH_GATT_PROXY_CLIENT
/* Using UUID with a fixed pattern 0x97 for BLE Mesh GATT Proxy Client */
#define BLE_MESH_GATTC_APP_UUID_BYTE 0x97
static struct gattc_prov_info {
/* Service to be found depends on the type of adv pkt received */
struct bt_mesh_conn conn;
bt_mesh_addr_t addr;
uint16_t service_uuid;
uint16_t mtu;
bool wr_desc_done; /* Indicate if write char descriptor event is received */
uint16_t start_handle; /* Service attribute start handle */
uint16_t end_handle; /* Service attribute end handle */
uint16_t data_in_handle; /* Data In Characteristic attribute handle */
uint16_t data_out_handle; /* Data Out Characteristic attribute handle */
uint16_t ccc_handle; /* Data Out Characteristic CCC attribute handle */
} bt_mesh_gattc_info[BLE_MESH_MAX_CONN];
static struct bt_mesh_prov_conn_cb *bt_mesh_gattc_conn_cb;
static tBTA_GATTC_IF bt_mesh_gattc_if;
#endif
int bt_mesh_host_init(void)
{
return 0;
}
void bt_mesh_hci_init(void)
{
const uint8_t *features = controller_get_interface()->get_features_ble()->as_array;
if (features != NULL) {
memcpy(bt_mesh_dev.features[0], features, 8);
memcpy(bt_mesh_dev.le.features, features, 8);
}
/**
* Currently 20ms non-connectable adv interval is supported, and we need to add
* a flag to indicate this support.
*/
#ifdef CONFIG_BLE_MESH_HCI_5_0
bt_mesh_dev.hci_version = BLE_MESH_HCI_VERSION_5_0;
#else
bt_mesh_dev.hci_version = controller_get_interface()->get_bt_version()->hci_version;
#endif
bt_mesh_dev.lmp_version = controller_get_interface()->get_bt_version()->lmp_version;
bt_mesh_dev.hci_revision = controller_get_interface()->get_bt_version()->hci_revision;
bt_mesh_dev.lmp_subversion = controller_get_interface()->get_bt_version()->lmp_subversion;
bt_mesh_dev.manufacturer = controller_get_interface()->get_bt_version()->manufacturer;
const uint8_t *p = controller_get_interface()->get_ble_supported_states();
uint64_t states_fh = 0, states_sh = 0;
STREAM_TO_UINT32(states_fh, p);
STREAM_TO_UINT32(states_sh, p);
bt_mesh_dev.le.states = (states_sh << 32) | states_fh;
}
static void bt_mesh_scan_results_change_2_bta(tBTM_INQ_RESULTS *p_inq, uint8_t *p_eir,
tBTA_DM_SEARCH_CBACK *p_scan_cback)
{
tBTM_INQ_INFO *p_inq_info = NULL;
tBTA_DM_SEARCH result = {0};
bdcpy(result.inq_res.bd_addr, p_inq->remote_bd_addr);
result.inq_res.rssi = p_inq->rssi;
result.inq_res.ble_addr_type = p_inq->ble_addr_type;
result.inq_res.inq_result_type = p_inq->inq_result_type;
result.inq_res.device_type = p_inq->device_type;
result.inq_res.flag = p_inq->flag;
result.inq_res.adv_data_len = p_inq->adv_data_len;
result.inq_res.scan_rsp_len = p_inq->scan_rsp_len;
memcpy(result.inq_res.dev_class, p_inq->dev_class, sizeof(DEV_CLASS));
result.inq_res.ble_evt_type = p_inq->ble_evt_type;
/* application will parse EIR to find out remote device name */
result.inq_res.p_eir = p_eir;
if ((p_inq_info = BTM_InqDbRead(p_inq->remote_bd_addr)) != NULL) {
/* initialize remt_name_not_required to FALSE so that we get the name by default */
result.inq_res.remt_name_not_required = FALSE;
}
if (p_scan_cback) {
p_scan_cback(BTA_DM_INQ_RES_EVT, &result);
}
if (p_inq_info) {
/* application indicates if it knows the remote name, inside the callback
copy that to the inquiry data base*/
if (result.inq_res.remt_name_not_required) {
p_inq_info->appl_knows_rem_name = TRUE;
}
}
}
static void bt_mesh_scan_results_cb(tBTM_INQ_RESULTS *p_inq, uint8_t *p_eir)
{
bt_mesh_scan_results_change_2_bta(p_inq, p_eir, bt_mesh_scan_result_callback);
}
static bool valid_adv_param(const struct bt_mesh_adv_param *param)
{
if (!(param->options & BLE_MESH_ADV_OPT_CONNECTABLE)) {
#if BLE_MESH_DEV
if (bt_mesh_dev.hci_version < BLE_MESH_HCI_VERSION_5_0 &&
param->interval_min < 0x00a0) {
return false;
}
#endif
}
if (param->interval_min > param->interval_max ||
param->interval_min < 0x0020 || param->interval_max > 0x4000) {
return false;
}
return true;
}
static int set_adv_data(uint16_t hci_op, const struct bt_mesh_adv_data *ad, size_t ad_len)
{
struct bt_mesh_hci_cp_set_adv_data param = {0};
int i;
if (ad == NULL || ad_len == 0) {
return 0;
}
for (i = 0; i < ad_len; i++) {
/* Check if ad fit in the remaining buffer */
if (param.len + ad[i].data_len + 2 > 31) {
return -EINVAL;
}
param.data[param.len++] = ad[i].data_len + 1;
param.data[param.len++] = ad[i].type;
memcpy(¶m.data[param.len], ad[i].data, ad[i].data_len);
param.len += ad[i].data_len;
}
/* Set adv data and scan rsp data. */
if (hci_op == BLE_MESH_HCI_OP_SET_ADV_DATA) {
BLE_MESH_BTM_CHECK_STATUS(BTM_BleWriteAdvDataRaw(param.data, param.len));
} else if (hci_op == BLE_MESH_HCI_OP_SET_SCAN_RSP_DATA) {
BLE_MESH_BTM_CHECK_STATUS(BTM_BleWriteScanRspRaw(param.data, param.len));
}
return 0;
}
static void start_adv_completed_cb(uint8_t status)
{
#if BLE_MESH_DEV
if (!status) {
bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING);
}
#endif
}
static bool valid_scan_param(const struct bt_mesh_scan_param *param)
{
if (param->type != BLE_MESH_SCAN_PASSIVE &&
param->type != BLE_MESH_SCAN_ACTIVE) {
return false;
}
if (param->filter_dup != BLE_MESH_SCAN_FILTER_DUP_DISABLE &&
param->filter_dup != BLE_MESH_SCAN_FILTER_DUP_ENABLE) {
return false;
}
if (param->interval < 0x0004 || param->interval > 0x4000) {
return false;
}
if (param->window < 0x0004 || param->window > 0x4000) {
return false;
}
if (param->window > param->interval) {
return false;
}
return true;
}
static int start_le_scan(uint8_t scan_type, uint16_t interval, uint16_t window,
uint8_t filter_dup, uint8_t scan_fil_policy)
{
UINT8 addr_type_own = BLE_MESH_ADDR_PUBLIC; /* Currently only support Public Address */
tGATT_IF client_if = 0xFF; /* Default GATT interface id */
BLE_MESH_BTM_CHECK_STATUS(
BTM_BleSetScanFilterParams(client_if, interval, window, scan_type, addr_type_own,
filter_dup, scan_fil_policy, NULL));
/* BLE Mesh scan permanently, so no duration of scan here */
BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(true, 0, bt_mesh_scan_results_cb, NULL, NULL));
#if BLE_MESH_DEV
if (scan_type == BLE_MESH_SCAN_ACTIVE) {
bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ACTIVE_SCAN);
} else {
bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ACTIVE_SCAN);
}
#endif
return 0;
}
static void bt_mesh_scan_result_callback(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
{
struct net_buf_simple buf = {0};
bt_mesh_addr_t addr = {0};
BT_DBG("%s, event %d", __func__, event);
if (event == BTA_DM_INQ_RES_EVT) {
/* TODO: How to process scan response here? PS: p_data->inq_res.scan_rsp_len */
addr.type = p_data->inq_res.ble_addr_type;
memcpy(addr.val, p_data->inq_res.bd_addr, BLE_MESH_ADDR_LEN);
net_buf_simple_init_with_data(&buf, p_data->inq_res.p_eir, p_data->inq_res.adv_data_len);
if (bt_mesh_scan_dev_found_cb) {
bt_mesh_scan_dev_found_cb(&addr, p_data->inq_res.rssi, p_data->inq_res.ble_evt_type, &buf);
}
} else if (event == BTA_DM_INQ_CMPL_EVT) {
BT_INFO("Scan completed, number of scan response %d", p_data->inq_cmpl.num_resps);
} else {
BT_WARN("Unexpected scan result event %d", event);
}
}
/* APIs functions */
int bt_le_adv_start(const struct bt_mesh_adv_param *param,
const struct bt_mesh_adv_data *ad, size_t ad_len,
const struct bt_mesh_adv_data *sd, size_t sd_len)
{
tBTA_START_ADV_CMPL_CBACK *p_start_adv_cb = NULL;
tBTM_BLE_ADV_CHNL_MAP channel_map = 0U;
tBLE_ADDR_TYPE addr_type_own = 0U;
tBLE_BD_ADDR p_dir_bda = {0};
tBTM_BLE_AFP adv_fil_pol = 0U;
uint8_t adv_type = 0U;
int err = 0;
#if BLE_MESH_DEV
if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING)) {
return -EALREADY;
}
#endif
if (!valid_adv_param(param)) {
BT_ERR("Invalid adv parameters");
return -EINVAL;
}
err = set_adv_data(BLE_MESH_HCI_OP_SET_ADV_DATA, ad, ad_len);
if (err) {
BT_ERR("Failed to set adv data");
return err;
}
/*
* We need to set SCAN_RSP when enabling advertising type that allows
* for Scan Requests.
*
* If sd was not provided but we enable connectable undirected
* advertising sd needs to be cleared from values set by previous calls.
* Clearing sd is done by calling set_adv_data() with NULL data and zero len.
* So following condition check is unusual but correct.
*/
if (sd && (param->options & BLE_MESH_ADV_OPT_CONNECTABLE)) {
err = set_adv_data(BLE_MESH_HCI_OP_SET_SCAN_RSP_DATA, sd, sd_len);
if (err) {
BT_ERR("Failed to set scan rsp data");
return err;
}
}
if (param->options & BLE_MESH_ADV_OPT_CONNECTABLE) {
adv_type = BLE_MESH_ADV_IND;
} else if (sd != NULL) {
adv_type = BLE_MESH_ADV_SCAN_IND;
} else {
adv_type = BLE_MESH_ADV_NONCONN_IND;
}
addr_type_own = BLE_MESH_ADDR_PUBLIC; /* Currently only support Public Address */
channel_map = BLE_MESH_ADV_CHNL_37 | BLE_MESH_ADV_CHNL_38 | BLE_MESH_ADV_CHNL_39;
adv_fil_pol = BLE_MESH_AP_SCAN_CONN_ALL;
p_start_adv_cb = start_adv_completed_cb;
/* Check if we can start adv using BTM_BleSetAdvParamsStartAdvCheck */
BLE_MESH_BTM_CHECK_STATUS(
BTM_BleSetAdvParamsAll(param->interval_min, param->interval_max, adv_type,
addr_type_own, &p_dir_bda,
channel_map, adv_fil_pol, p_start_adv_cb));
BLE_MESH_BTM_CHECK_STATUS(BTM_BleStartAdv());
#if BLE_MESH_DEV
bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING);
if (!(param->options & BLE_MESH_ADV_OPT_ONE_TIME)) {
bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_KEEP_ADVERTISING);
}
#endif
return 0;
}
#if CONFIG_BLE_MESH_SUPPORT_BLE_ADV
int bt_mesh_ble_adv_start(const struct bt_mesh_ble_adv_param *param,
const struct bt_mesh_ble_adv_data *data)
{
struct bt_mesh_hci_cp_set_adv_data set = {0};
tBTM_BLE_ADV_CHNL_MAP channel_map = 0U;
tBTM_BLE_AFP adv_fil_pol = 0U;
tBLE_BD_ADDR p_dir_bda = {0};
if (data && param->adv_type != BLE_MESH_ADV_DIRECT_IND &&
param->adv_type != BLE_MESH_ADV_DIRECT_IND_LOW_DUTY) {
if (data->adv_data_len) {
set.len = data->adv_data_len;
memcpy(set.data, data->adv_data, data->adv_data_len);
BLE_MESH_BTM_CHECK_STATUS(BTM_BleWriteAdvDataRaw(set.data, set.len));
}
if (data->scan_rsp_data_len && param->adv_type != BLE_MESH_ADV_NONCONN_IND) {
set.len = data->scan_rsp_data_len;
memcpy(set.data, data->scan_rsp_data, data->scan_rsp_data_len);
BLE_MESH_BTM_CHECK_STATUS(BTM_BleWriteScanRspRaw(set.data, set.len));
}
}
channel_map = BLE_MESH_ADV_CHNL_37 | BLE_MESH_ADV_CHNL_38 | BLE_MESH_ADV_CHNL_39;
adv_fil_pol = BLE_MESH_AP_SCAN_CONN_ALL;
if (param->own_addr_type == BLE_MESH_ADDR_PUBLIC_ID ||
param->own_addr_type == BLE_MESH_ADDR_RANDOM_ID ||
param->adv_type == BLE_MESH_ADV_DIRECT_IND ||
param->adv_type == BLE_MESH_ADV_DIRECT_IND_LOW_DUTY) {
p_dir_bda.type = param->peer_addr_type;
memcpy(p_dir_bda.bda, param->peer_addr, BLE_MESH_ADDR_LEN);
}
/* Check if we can start adv using BTM_BleSetAdvParamsStartAdvCheck */
BLE_MESH_BTM_CHECK_STATUS(
BTM_BleSetAdvParamsAll(param->interval, param->interval, param->adv_type,
param->own_addr_type, &p_dir_bda,
channel_map, adv_fil_pol, NULL));
BLE_MESH_BTM_CHECK_STATUS(BTM_BleStartAdv());
return 0;
}
#endif /* CONFIG_BLE_MESH_SUPPORT_BLE_ADV */
int bt_le_adv_stop(void)
{
#if BLE_MESH_DEV
bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_KEEP_ADVERTISING);
if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING)) {
return 0;
}
#endif
BLE_MESH_BTM_CHECK_STATUS(BTM_BleBroadcast(false, NULL));
#if BLE_MESH_DEV
bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING);
#endif
return 0;
}
int bt_le_scan_start(const struct bt_mesh_scan_param *param, bt_mesh_scan_cb_t cb)
{
int err = 0;
if (bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
BT_INFO("Scan is already started");
return -EALREADY;
}
if (!valid_scan_param(param)) {
return -EINVAL;
}
#if BLE_MESH_DEV
if (param->filter_dup) {
bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCAN_FILTER_DUP);
} else {
bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCAN_FILTER_DUP);
}
#endif
err = start_le_scan(param->type, param->interval, param->window,
param->filter_dup, param->scan_fil_policy);
if (err) {
return err;
}
bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
bt_mesh_scan_dev_found_cb = cb;
return 0;
}
int bt_le_scan_stop(void)
{
if (!bt_mesh_atomic_test_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING)) {
BT_INFO("Scan is already stopped");
return -EALREADY;
}
BLE_MESH_BTM_CHECK_STATUS(BTM_BleScan(false, 0, NULL, NULL, NULL));
bt_mesh_atomic_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_SCANNING);
bt_mesh_scan_dev_found_cb = NULL;
return 0;
}
#if CONFIG_BLE_MESH_TEST_USE_WHITE_LIST
int bt_le_update_white_list(struct bt_mesh_white_list *wl)
{
if (wl == NULL) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}
if (BTM_BleUpdateAdvWhitelist(wl->add_remove, wl->remote_bda,
wl->addr_type, (tBTM_UPDATE_WHITELIST_CBACK *)wl->update_wl_comp_cb) == false) {
return -EIO;
}
return 0;
}
#endif
#if (CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PB_GATT) || \
CONFIG_BLE_MESH_GATT_PROXY_SERVER
static void bt_mesh_bta_gatts_cb(tBTA_GATTS_EVT event, tBTA_GATTS *p_data)
{
switch (event) {
case BTA_GATTS_REG_EVT:
if (p_data->reg_oper.status == BTA_GATT_OK) {
bt_mesh_gatts_if = p_data->reg_oper.server_if;
}
break;
case BTA_GATTS_READ_EVT: {
struct bt_mesh_gatt_attr *attr = bt_mesh_gatts_find_attr_by_handle(p_data->req_data.p_data->read_req.handle);
uint8_t index = BLE_MESH_GATT_GET_CONN_ID(p_data->req_data.conn_id);
tBTA_GATTS_RSP rsp = {0};
uint8_t buf[100] = {0};
uint16_t len = 0;
BT_DBG("gatts read, handle %d", p_data->req_data.p_data->read_req.handle);
if (attr != NULL && attr->read != NULL) {
if ((len = attr->read(&bt_mesh_gatts_conn[index], attr, buf, 100,
p_data->req_data.p_data->read_req.offset)) > 0) {
rsp.attr_value.handle = p_data->req_data.p_data->read_req.handle;
rsp.attr_value.len = len;
memcpy(&rsp.attr_value.value[0], buf, len);
BTA_GATTS_SendRsp(p_data->req_data.conn_id, p_data->req_data.trans_id,
p_data->req_data.status, &rsp);
BT_DBG("Send gatts read rsp, handle %d", attr->handle);
} else {
BT_WARN("Mesh gatts read failed");
}
}
break;
}
case BTA_GATTS_WRITE_EVT: {
struct bt_mesh_gatt_attr *attr = bt_mesh_gatts_find_attr_by_handle(p_data->req_data.p_data->write_req.handle);
uint8_t index = BLE_MESH_GATT_GET_CONN_ID(p_data->req_data.conn_id);
uint16_t len = 0;
BT_DBG("gatts write, handle %d, len %d, data %s", p_data->req_data.p_data->write_req.handle,
p_data->req_data.p_data->write_req.len,
bt_hex(p_data->req_data.p_data->write_req.value, p_data->req_data.p_data->write_req.len));
if (attr != NULL && attr->write != NULL) {
if ((len = attr->write(&bt_mesh_gatts_conn[index], attr,
p_data->req_data.p_data->write_req.value,
p_data->req_data.p_data->write_req.len,
p_data->req_data.p_data->write_req.offset, 0)) > 0) {
if (p_data->req_data.p_data->write_req.need_rsp) {
BTA_GATTS_SendRsp(p_data->req_data.conn_id, p_data->req_data.trans_id,
p_data->req_data.status, NULL);
BT_DBG("Send gatts write rsp, handle %d", attr->handle);
}
}
}
break;
}
case BTA_GATTS_EXEC_WRITE_EVT:
break;
case BTA_GATTS_MTU_EVT:
break;
case BTA_GATTS_CONF_EVT:
break;
case BTA_GATTS_CREATE_EVT:
svc_handle = p_data->create.service_id;
BT_DBG("svc_handle %d, future_mesh %p", svc_handle, future_mesh);
if (future_mesh != NULL) {
future_ready(future_mesh, FUTURE_SUCCESS);
}
break;
case BTA_GATTS_ADD_INCL_SRVC_EVT:
svc_handle = p_data->add_result.attr_id;
if (future_mesh != NULL) {
future_ready(future_mesh, FUTURE_SUCCESS);
}
break;
case BTA_GATTS_ADD_CHAR_EVT:
char_handle = p_data->add_result.attr_id;
if (future_mesh != NULL) {
future_ready(future_mesh, FUTURE_SUCCESS);
}
break;
case BTA_GATTS_ADD_CHAR_DESCR_EVT:
char_handle = p_data->add_result.attr_id;
if (future_mesh != NULL) {
future_ready(future_mesh, FUTURE_SUCCESS);
}
break;
case BTA_GATTS_DELELTE_EVT:
break;
case BTA_GATTS_START_EVT:
break;
case BTA_GATTS_STOP_EVT:
break;
case BTA_GATTS_CONNECT_EVT:
#if BLE_MESH_DEV
/* When connection is created, advertising will be stopped automatically. */
bt_mesh_atomic_test_and_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING);
#endif
if (bt_mesh_gatts_conn_cb != NULL && bt_mesh_gatts_conn_cb->connected != NULL) {
uint8_t index = BLE_MESH_GATT_GET_CONN_ID(p_data->conn.conn_id);
if (index < BLE_MESH_MAX_CONN) {
bt_mesh_gatts_conn[index].handle = BLE_MESH_GATT_GET_CONN_ID(p_data->conn.conn_id);
(bt_mesh_gatts_conn_cb->connected)(&bt_mesh_gatts_conn[index], 0);
}
memcpy(bt_mesh_gatts_addr, p_data->conn.remote_bda, BLE_MESH_ADDR_LEN);
/* This is for EspBleMesh Android app. When it tries to connect with the
* device at the first time and it fails due to some reason. And after
* the second connection, the device needs to send GATT service change
* indication to the phone manually to notify it discovering service again.
*/
BTA_GATTS_SendServiceChangeIndication(bt_mesh_gatts_if, bt_mesh_gatts_addr);
}
break;
case BTA_GATTS_DISCONNECT_EVT:
#if BLE_MESH_DEV
bt_mesh_atomic_test_and_clear_bit(bt_mesh_dev.flags, BLE_MESH_DEV_ADVERTISING);
#endif
if (bt_mesh_gatts_conn_cb != NULL && bt_mesh_gatts_conn_cb->disconnected != NULL) {
uint8_t index = BLE_MESH_GATT_GET_CONN_ID(p_data->conn.conn_id);
if (index < BLE_MESH_MAX_CONN) {
bt_mesh_gatts_conn[index].handle = BLE_MESH_GATT_GET_CONN_ID(p_data->conn.conn_id);
(bt_mesh_gatts_conn_cb->disconnected)(&bt_mesh_gatts_conn[index], p_data->conn.reason);
}
memset(bt_mesh_gatts_addr, 0x0, BLE_MESH_ADDR_LEN);
}
break;
case BTA_GATTS_CLOSE_EVT:
break;
default:
break;
}
}
void bt_mesh_gatts_conn_cb_register(struct bt_mesh_conn_cb *cb)
{
bt_mesh_gatts_conn_cb = cb;
}
void bt_mesh_gatts_conn_cb_deregister(void)
{
bt_mesh_gatts_conn_cb = NULL;
}
static struct bt_mesh_gatt_attr *bt_mesh_gatts_find_attr_by_handle(uint16_t handle)
{
struct bt_mesh_gatt_service *svc = NULL;
struct bt_mesh_gatt_attr *attr = NULL;
SYS_SLIST_FOR_EACH_CONTAINER(&bt_mesh_gatts_db, svc, node) {
int i;
for (i = 0; i < svc->attr_count; i++) {
attr = &svc->attrs[i];
/* Check the attrs handle is equal to the handle or not */
if (attr->handle == handle) {
return attr;
}
}
}
return NULL;
}
static void bt_mesh_gatts_foreach_attr(uint16_t start_handle, uint16_t end_handle,
bt_mesh_gatt_attr_func_t func, void *user_data)
{
struct bt_mesh_gatt_service *svc = NULL;
SYS_SLIST_FOR_EACH_CONTAINER(&bt_mesh_gatts_db, svc, node) {
int i;
for (i = 0; i < svc->attr_count; i++) {
struct bt_mesh_gatt_attr *attr = &svc->attrs[i];
/* Check if attribute handle is within range */
if (attr->handle < start_handle ||
attr->handle > end_handle) {
continue;
}
if (func(attr, user_data) == BLE_MESH_GATT_ITER_STOP) {
return;
}
}
}
}
static uint8_t find_next(const struct bt_mesh_gatt_attr *attr, void *user_data)
{
struct bt_mesh_gatt_attr **next = user_data;
*next = (struct bt_mesh_gatt_attr *)attr;
return BLE_MESH_GATT_ITER_STOP;
}
static struct bt_mesh_gatt_attr *bt_mesh_gatts_attr_next(const struct bt_mesh_gatt_attr *attr)
{
struct bt_mesh_gatt_attr *next = NULL;
bt_mesh_gatts_foreach_attr(attr->handle + 1, attr->handle + 1, find_next, &next);
return next;
}
ssize_t bt_mesh_gatts_attr_read(struct bt_mesh_conn *conn,
const struct bt_mesh_gatt_attr *attr,
void *buf, uint16_t buf_len, uint16_t offset,
const void *value, uint16_t value_len)
{
uint16_t len = 0U;
if (offset > value_len) {
return BLE_MESH_GATT_ERR(BLE_MESH_ATT_ERR_INVALID_OFFSET);
}
len = MIN(buf_len, value_len - offset);
BT_DBG("handle 0x%04x offset %u length %u", attr->handle, offset, len);
memcpy(buf, value + offset, len);
return len;
}
struct gatts_incl {
uint16_t start_handle;
uint16_t end_handle;
uint16_t uuid16;
} __packed;
ssize_t bt_mesh_gatts_attr_read_included(struct bt_mesh_conn *conn,
const struct bt_mesh_gatt_attr *attr,
void *buf, uint16_t len, uint16_t offset)
{
struct bt_mesh_gatt_attr *incl = attr->user_data;
struct bt_mesh_uuid *uuid = incl->user_data;
struct gatts_incl pdu = {0};
uint8_t value_len = 0U;
/* First attr points to the start handle */
pdu.start_handle = sys_cpu_to_le16(incl->handle);
value_len = sizeof(pdu.start_handle) + sizeof(pdu.end_handle);
/*
* Core 4.2, Vol 3, Part G, 3.2,
* The Service UUID shall only be present when the UUID is a 16-bit Bluetooth UUID.
*/
if (uuid->type == BLE_MESH_UUID_TYPE_16) {
pdu.uuid16 = sys_cpu_to_le16(BLE_MESH_UUID_16(uuid)->val);
value_len += sizeof(pdu.uuid16);
}
return bt_mesh_gatts_attr_read(conn, attr, buf, len, offset, &pdu, value_len);
}
ssize_t bt_mesh_gatts_attr_read_service(struct bt_mesh_conn *conn,
const struct bt_mesh_gatt_attr *attr,
void *buf, uint16_t len, uint16_t offset)
{
struct bt_mesh_uuid *uuid = attr->user_data;
if (uuid->type == BLE_MESH_UUID_TYPE_16) {
uint16_t uuid16 = sys_cpu_to_le16(BLE_MESH_UUID_16(uuid)->val);
return bt_mesh_gatts_attr_read(conn, attr, buf, len, offset, &uuid16, 2);
}
return bt_mesh_gatts_attr_read(conn, attr, buf, len, offset,
BLE_MESH_UUID_128(uuid)->val, 16);
}
struct gatts_chrc {
uint8_t properties;
uint16_t value_handle;
union {
uint16_t uuid16;
uint8_t uuid[16];
};
} __packed;
ssize_t bt_mesh_gatts_attr_read_chrc(struct bt_mesh_conn *conn,
const struct bt_mesh_gatt_attr *attr,
void *buf, uint16_t len, uint16_t offset)
{
struct bt_mesh_gatt_char *chrc = attr->user_data;
const struct bt_mesh_gatt_attr *next = NULL;
struct gatts_chrc pdu = {0};
uint8_t value_len = 0U;
pdu.properties = chrc->properties;
/* BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part G] page 534:
* 3.3.2 Characteristic Value Declaration
* The Characteristic Value declaration contains the value of the
* characteristic. It is the first Attribute after the characteristic
* declaration. All characteristic definitions shall have a
* Characteristic Value declaration.
*/
next = bt_mesh_gatts_attr_next(attr);
if (!next) {
BT_WARN("No value for characteristic, handle 0x%04x", attr->handle);
pdu.value_handle = 0x0000;
} else {
pdu.value_handle = sys_cpu_to_le16(next->handle);
}
value_len = sizeof(pdu.properties) + sizeof(pdu.value_handle);
if (chrc->uuid->type == BLE_MESH_UUID_TYPE_16) {
pdu.uuid16 = sys_cpu_to_le16(BLE_MESH_UUID_16(chrc->uuid)->val);
value_len += 2;
} else {
memcpy(pdu.uuid, BLE_MESH_UUID_128(chrc->uuid)->val, 16);
value_len += 16;
}
return bt_mesh_gatts_attr_read(conn, attr, buf, len, offset, &pdu, value_len);
}
static void bta_uuid_to_bt_mesh_uuid(tBT_UUID *bta_uuid, const struct bt_mesh_uuid *uuid)
{
assert(uuid != NULL && bta_uuid != NULL);
if (uuid->type == BLE_MESH_UUID_TYPE_16) {
bta_uuid->len = LEN_UUID_16;
bta_uuid->uu.uuid16 = BLE_MESH_UUID_16(uuid)->val;
} else if (uuid->type == BLE_MESH_UUID_TYPE_32) {
bta_uuid->len = LEN_UUID_32;
bta_uuid->uu.uuid32 = BLE_MESH_UUID_32(uuid)->val;
} else if (uuid->type == BLE_MESH_UUID_TYPE_128) {
bta_uuid->len = LEN_UUID_128;
memcpy(bta_uuid->uu.uuid128, BLE_MESH_UUID_128(uuid)->val, LEN_UUID_128);
} else {
BT_ERR("Invalid mesh uuid type %d", uuid->type);
}
return;
}
static int gatts_register(struct bt_mesh_gatt_service *svc)
{
struct bt_mesh_gatt_service *last = NULL;
uint16_t handle = 0U;
if (sys_slist_is_empty(&bt_mesh_gatts_db)) {
handle = 0;
goto populate;
}
last = SYS_SLIST_PEEK_TAIL_CONTAINER(&bt_mesh_gatts_db, last, node);
handle = last->attrs[last->attr_count - 1].handle;
BT_DBG("gatts register, handle %d", handle);
((void) handle);
populate:
sys_slist_append(&bt_mesh_gatts_db, &svc->node);
return 0;
}
static int gatts_deregister(struct bt_mesh_gatt_service *svc)
{
if (sys_slist_is_empty(&bt_mesh_gatts_db)) {
return 0;
}
sys_slist_find_and_remove(&bt_mesh_gatts_db, &svc->node);
return 0;
}
static tBTA_GATT_PERM bt_mesh_perm_to_bta_perm(uint8_t perm)
{
tBTA_GATT_PERM bta_perm = 0;
if ((perm & BLE_MESH_GATT_PERM_READ) == BLE_MESH_GATT_PERM_READ) {
bta_perm |= BTA_GATT_PERM_READ;
}
if ((perm & BLE_MESH_GATT_PERM_WRITE) == BLE_MESH_GATT_PERM_WRITE) {
bta_perm |= BTA_GATT_PERM_WRITE;
}
if ((perm & BLE_MESH_GATT_PERM_READ_ENCRYPT) == BLE_MESH_GATT_PERM_READ_ENCRYPT) {
bta_perm |= BTA_GATT_PERM_READ_ENCRYPTED;
}
if ((perm & BLE_MESH_GATT_PERM_WRITE_ENCRYPT) == BLE_MESH_GATT_PERM_WRITE_ENCRYPT) {
bta_perm |= BTA_GATT_PERM_WRITE_ENCRYPTED;
}
if ((perm & BLE_MESH_GATT_PERM_READ_AUTHEN) == BLE_MESH_GATT_PERM_READ_AUTHEN) {
bta_perm |= BTA_GATT_PERM_READ_ENC_MITM;
}
if ((perm & BLE_MESH_GATT_PERM_WRITE_AUTHEN) == BLE_MESH_GATT_PERM_WRITE_AUTHEN) {
bta_perm |= BTA_GATT_PERM_WRITE_ENC_MITM;
}
return bta_perm;
}
int bt_mesh_gatts_service_register(struct bt_mesh_gatt_service *svc)
{
tBT_UUID bta_uuid = {0};
assert(svc != NULL);
for (int i = 0; i < svc->attr_count; i++) {
if (svc->attrs[i].uuid->type == BLE_MESH_UUID_TYPE_16) {
switch (BLE_MESH_UUID_16(svc->attrs[i].uuid)->val) {
case BLE_MESH_UUID_GATT_PRIMARY_VAL: {
future_mesh = future_new();
bta_uuid_to_bt_mesh_uuid(&bta_uuid, (struct bt_mesh_uuid *)svc->attrs[i].user_data);
BTA_GATTS_CreateService(bt_mesh_gatts_if,
&bta_uuid, 0, svc->attr_count, true);
if (future_await(future_mesh) == FUTURE_FAIL) {
BT_ERR("Failed to add primary service");
return ESP_FAIL;
}
svc->attrs[i].handle = svc_handle;
BT_DBG("Add primary service, uuid 0x%04x, perm %d, handle %d",
bta_uuid.uu.uuid16, svc->attrs[i].perm, svc_handle);
break;
}
case BLE_MESH_UUID_GATT_SECONDARY_VAL: {
future_mesh = future_new();
bta_uuid_to_bt_mesh_uuid(&bta_uuid, (struct bt_mesh_uuid *)svc->attrs[i].user_data);
BTA_GATTS_CreateService(bt_mesh_gatts_if,
&bta_uuid, 0, svc->attr_count, false);
if (future_await(future_mesh) == FUTURE_FAIL) {
BT_ERR("Failed to add secondary service");
return ESP_FAIL;
}
svc->attrs[i].handle = svc_handle;
BT_DBG("Add secondary service, uuid 0x%04x, perm %d, handle %d",
bta_uuid.uu.uuid16, svc->attrs[i].perm, svc_handle);
break;
}
case BLE_MESH_UUID_GATT_INCLUDE_VAL: {
break;
}
case BLE_MESH_UUID_GATT_CHRC_VAL: {
future_mesh = future_new();
struct bt_mesh_gatt_char *gatts_chrc = (struct bt_mesh_gatt_char *)svc->attrs[i].user_data;
bta_uuid_to_bt_mesh_uuid(&bta_uuid, gatts_chrc->uuid);
BTA_GATTS_AddCharacteristic(svc_handle, &bta_uuid, bt_mesh_perm_to_bta_perm(svc->attrs[i + 1].perm), gatts_chrc->properties, NULL, NULL);
if (future_await(future_mesh) == FUTURE_FAIL) {
BT_ERR("Failed to add characteristic");
return ESP_FAIL;
}
/* All the characteristic should have two handles: the declaration handle and the value handle */
svc->attrs[i].handle = char_handle - 1;
svc->attrs[i + 1].handle = char_handle;
BT_DBG("Add characteristic, uuid 0x%04x, handle %d, perm %d, properties %d",
BLE_MESH_UUID_16(gatts_chrc->uuid)->val, char_handle, svc->attrs[i + 1].perm, gatts_chrc->properties);
break;
}
case BLE_MESH_UUID_GATT_CEP_VAL:
case BLE_MESH_UUID_GATT_CUD_VAL:
case BLE_MESH_UUID_GATT_CCC_VAL:
case BLE_MESH_UUID_GATT_SCC_VAL:
case BLE_MESH_UUID_GATT_CPF_VAL:
case BLE_MESH_UUID_VALID_RANGE_VAL:
case BLE_MESH_UUID_HIDS_EXT_REPORT_VAL:
case BLE_MESH_UUID_HIDS_REPORT_REF_VAL:
case BLE_MESH_UUID_ES_CONFIGURATION_VAL:
case BLE_MESH_UUID_ES_MEASUREMENT_VAL:
case BLE_MESH_UUID_ES_TRIGGER_SETTING_VAL: {
future_mesh = future_new();
bta_uuid_to_bt_mesh_uuid(&bta_uuid, svc->attrs[i].uuid);
BTA_GATTS_AddCharDescriptor(svc_handle, bt_mesh_perm_to_bta_perm(svc->attrs[i].perm), &bta_uuid, NULL, NULL);
if (future_await(future_mesh) == FUTURE_FAIL) {
BT_ERR("Failed to add descriptor");
return ESP_FAIL;
}
svc->attrs[i].handle = char_handle;
BT_DBG("Add descriptor, uuid 0x%04x, perm %d, handle %d",
BLE_MESH_UUID_16(svc->attrs[i].uuid)->val, svc->attrs[i].perm, char_handle);
break;
}
default:
break;
}
}
}
if (svc_handle != 0) {
svc_handle = 0;