1
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
37
38
39
40
41
42
48
49
50
51
52
53
54
55
56
57
58
60
61
62
63
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
94
95
96
97
99
100
101
102
108
109
110
111
112
113
114
127
128
129
130
131
133
134
135
136
137
138
139
140
141
146
147
148
156
160
161
165
166
170
171
175
176
177
187
188
189
190
191
192
193
194
198
199
205
206
210
211
212
213
214
215
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
246
247
248
249
250
251
252
253
254
255
256
257
258
262
263
264
276
277
278
279
280
281
282
283
286
287
288
291
292
293
294
295
296
297
301
302
303
307
308
314
317
318
319
327
328
329
337
338
339
342
343
344
345
346
347
348
349
350
351
352
356
357
358
359
362
363
368
369
373
374
375
376
377
378
379
383
384
385
386
389
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
418
419
420
421
424
425
430
431
435
436
437
438
439
443
444
445
446
447
448
449
452
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
486
487
491
492
496
497
498
499
503
504
505
506
510
511
517
518
519
523
524
525
526
527
531
532
533
534
538
539
540
551
552
553
554
557
561
562
563
564
565
566
567
568
569
570
571
575
576
577
578
579
580
581
582
583
584
585
586
587
590
591
592
593
594
595
596
597
598
599
600
604
605
606
609
610
611
616
617
618
623
624
625
626
627
628
629
630
631
632
633
634
635
639
640
644
645
646
647
651
652
653
654
658
659
663
664
668
669
670
671
672
673
674
675
676
677
678
679
680
684
685
686
687
688
689
690
691
692
693
697
698
699
702
703
707
708
709
713
714
715
719
720
721
722
723
724
725
726
727
728
729
733
734
738
739
740
746
747
748
754
755
756
757
758
759
760
761
765
766
767
768
769
770
773
774
775
776
777
778
788
789
790
791
792
793
796
797
806
807
808
809
810
814
815
816
817
818
821
825
826
827
833
834
835
839
840
841
850
851
852
853
854
857
858
859
860
861
862
863
864
872
873
877
878
879
880
881
882
886
887
888
894
895
896
897
898
899
900
901
902
903
907
908
909
910
911
912
916
917
918
922
923
924
928
929
933
936
937
938
939
940
943
944
945
946
947
948
949
950
951
952
953
954
960
961
964
965
966
967
968
969
970
971
975
976
980
981
985
986
987
988
992
993
997
998
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1033
1034
1035
1036
1037
1038
1039
1043
1044
1045
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1090
1091
1092
1099
1100
1103
1104
1105
1106
1110
1111
1112
1113
1114
1115
1116
1117
1120
1121
1122
1123
1124
1125
1126
1127
1128
1132
1133
1134
1138
1139
1140
1141
1142
1143
1144
1145
1146
1148
1149
1150
1152
1153
1154
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1193
1194
1195
1196
1197
1198
1199
1200
1204
1205
1206
1207
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1225
1226
1227
1228
1232
1233
1234
1240
1241
1242
1243
1247
1248
1249
1250
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1273
1274
1275
1281
1282
1302
1303
1304
1305
1306
1307
1308
1309
1317
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1332
1333
1337
1338
1342
1343
1347
1350
1351
1352
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1386
1387
1390
1391
1398
1399
1400
1401
1402
1403
1404
1407
1408
1409
1410
1411
1412
1413
1414
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1472
1473
1474
1475
1476
1477
1478
1479
1480
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1504
1505
1506
1507
1508
1509
1520
1521
1533
1534
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1553
1554
1561
1562
1563
1564
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1594
1595
1596
1597
1598
1599
1603
1604
1605
1606
1613
1614
1615
1616
1617
1621
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1660
1661
1662
1663
1664
1665
1669
1670
1671
1672
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1697
1698
1699
1700
1701
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1730
1731
1732
1733
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1760
1761
1766
1767
1770
1771
1776
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1807
1808
1809
1810
1811
1812
1813
1814
1815
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1837
1838
1842
1843
1844
1845
1846
1847
1848
1849
1850
1854
1855
1856
1864
1865
1868
1873
1874
1875
1876
1877
1878
1879
1880
1885
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1925
1930
1931
1932
1933
1939
1940
1941
1942
1943
1944
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1970
1971
1972
1973
1974
1978
1979
1980
1981
1985
1986
1987
1991
1992
1993
1994
1995
1996
1997
1998
2002
2003
2004
2005
2006
2007
2008
2009
2015
2016
2017
2018
2019
2020
2026
2027
2030
2031
2034
2035
2036
2037
2038
2045
2046
2047
2058
2059
2063
2064
2068
2072
2073
2074
2075
2076
2077
2078
2079
2080
2084
2085
2089
2090
2096
2120
2121
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2150
2154
2155
2161
2162
2169
2170
2176
2177
2183
2190
2191
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2225
2226
2231
2232
2233
2236
2237
2238
2239
2240
2241
2246
2247
2248
2249
2254
2257
2258
2261
2262
2263
2264
2265
2266
2267
2268
2272
2273
2274
2281
2282
2283
2284
2285
2286
2292
2293
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2311
2312
2313
2314
2315
2316
2322
2323
2324
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2345
2346
2347
2351
2352
2353
2358
2362
2363
2364
2365
2366
2367
2368
2372
2373
2374
2375
2376
2405
2406
2407
2408
2409
2410
2413
2417
2418
2422
2423
2424
2425
2426
2427
2431
2432
2433
2434
2435
2436
2439
2443
2444
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2491
2492
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2507
2510
2511
2514
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2550
2551
2552
2553
2554
2555
2559
2560
2561
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2593
2594
2595
2596
2597
2598
2599
2600
2601
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2638
2639
2643
2644
2645
2646
2651
2652
2657
2658
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2679
2680
2687
2688
2692
2693
2694
2695
2705
2706
2707
2708
2709
2712
2713
2714
2715
2716
2721
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2740
2741
2742
2743
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2775
2776
2777
2781
2782
2783
2784
2785
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2821
2822
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2858
2859
2860
2861
2862
2863
2864
2867
2868
2869
2876
2877
2878
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2908
2909
2910
2911
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2969
2970
2971
2972
2973
2976
2977
2981
2982
2983
2984
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3009
3010
3014
3015
3016
3017
3018
3019
3020
3021
3024
3025
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3044
3045
3049
3050
3051
3052
3053
3054
3055
3058
3059
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3082
3083
3084
3085
3089
3090
3091
3092
3093
3094
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3190
3191
3192
3193
3194
3197
3198
3199
3200
3201
/* ... */
#include <string.h>
#include <errno.h>
#include "crypto.h"
#include "adv.h"
#include "scan.h"
#include "mesh.h"
#include "access.h"
#include "settings.h"
#include "fast_prov.h"
#include "mesh/common.h"
#include "proxy_common.h"
#include "proxy_client.h"
#include "prov_common.h"
#include "prov_node.h"
#include "prov_pvnr.h"
#include "pvnr_mgmt.h"16 includes
#if CONFIG_BLE_MESH_V11_SUPPORT
#include "mesh_v1.1/utils.h"
#endif
#if CONFIG_BLE_MESH_PROVISIONER
_Static_assert(BLE_MESH_MAX_CONN >= CONFIG_BLE_MESH_PBG_SAME_TIME,
"Too large BLE Mesh PB-GATT count");
#define BLE_MESH_PROV_SAME_TIME \
(CONFIG_BLE_MESH_PBA_SAME_TIME + CONFIG_BLE_MESH_PBG_SAME_TIME)...
#define UNICAST_ADDR_LIMIT 0x7FFF
#define PROV_SVC_ADV_RX_CHECK(pre, cur) ((cur) < (pre) ? ((cur) + (UINT32_MAX - (pre)) >= 200) : ((cur) - (pre) >= 200))
/* ... */
static struct bt_mesh_prov_link prov_links[BLE_MESH_PROV_SAME_TIME];
struct bt_mesh_prov_ctx {
uint16_t primary_addr;
bt_mesh_prov_bearer_t bearers;
#if CONFIG_BLE_MESH_PB_ADV
uint8_t pba_count;/* ... */
#endif
#if CONFIG_BLE_MESH_PB_GATT
uint8_t pbg_count;/* ... */
#endif
uint16_t alloc_addr;
uint16_t net_idx;
uint8_t static_oob_len;
uint8_t static_oob_val[32];
uint8_t match_offset;
uint8_t match_length;
uint8_t match_value[16];
bool prov_after_match;
#if CONFIG_BLE_MESH_PB_ADV
bt_mesh_mutex_t pb_adv_lock;/* ... */
#endif
#if CONFIG_BLE_MESH_PB_GATT
bt_mesh_mutex_t pb_gatt_lock;/* ... */
#endif
struct {
bool enable;
uint16_t net_idx;
uint16_t unicast_addr_min;
uint16_t unicast_addr_max;
}{...} fast_prov;
}{...};
static struct bt_mesh_prov_ctx prov_ctx;
#define FAST_PROV_ENABLE() (prov_ctx.fast_prov.enable)
struct unprov_dev_queue {
bt_mesh_addr_t addr;
uint8_t uuid[16];
uint16_t oob_info;
uint8_t bearer;
uint8_t flags;
}{...} __attribute__((packed)) unprov_dev[CONFIG_BLE_MESH_WAIT_FOR_PROV_MAX_DEV_NUM] = {
[0 ... (CONFIG_BLE_MESH_WAIT_FOR_PROV_MAX_DEV_NUM - 1)] = {
.addr.type = 0xff,
.bearer = 0,
.flags = false,
}{...},
}{...};
static unprov_adv_pkt_cb_t notify_unprov_adv_pkt_cb;
#if CONFIG_BLE_MESH_PB_ADV
static void send_link_open(struct bt_mesh_prov_link *link);
extern uint8_t pvnr_next_xact_id(struct bt_mesh_prov_link *link);/* ... */
#endif
static void prov_gen_dh_key(struct bt_mesh_prov_link *link);
static void send_pub_key(struct bt_mesh_prov_link *link);
static void close_link(struct bt_mesh_prov_link *link, uint8_t reason);
static void send_invite(struct bt_mesh_prov_link *link);
#if CONFIG_BLE_MESH_PB_ADV
static struct prov_rx_buf {
struct net_buf_simple buf;
}{...} rx_buf[CONFIG_BLE_MESH_PBA_SAME_TIME];
static uint8_t rx_buf_data[PROV_RX_BUF_SIZE * CONFIG_BLE_MESH_PBA_SAME_TIME];/* ... */
#endif
#define PROV_FREE_MEM(_idx, member) \
{ \
if (prov_links[_idx].member) { \
bt_mesh_free(prov_links[_idx].member); \
prov_links[_idx].member = NULL; \
}{...} \
}{...}
...
struct bt_mesh_prov_link *bt_mesh_prov_pvnr_get_link(void)
{
return &prov_links[0];
}{...}
uint8_t bt_mesh_prov_pvnr_get_link_count(void)
{
return BLE_MESH_PROV_SAME_TIME;
}{...}
void bt_mesh_prov_pvnr_close_link(struct bt_mesh_prov_link *link, uint8_t reason)
{
close_link(link, reason);
}{...}
void bt_mesh_prov_pvnr_send_invite(struct bt_mesh_prov_link *link)
{
send_invite(link);
}{...}
#if CONFIG_BLE_MESH_PB_ADV
static inline void bt_mesh_pb_adv_lock(void)
{
bt_mesh_mutex_lock(&prov_ctx.pb_adv_lock);
}{...}
static inline void bt_mesh_pb_adv_unlock(void)
{
bt_mesh_mutex_unlock(&prov_ctx.pb_adv_lock);
}{...}
/* ... */#endif
#if CONFIG_BLE_MESH_PB_GATT
static inline void bt_mesh_pb_gatt_lock(void)
{
bt_mesh_mutex_lock(&prov_ctx.pb_gatt_lock);
}{...}
static inline void bt_mesh_pb_gatt_unlock(void)
{
bt_mesh_mutex_unlock(&prov_ctx.pb_gatt_lock);
}{...}
void bt_mesh_provisioner_pbg_count_dec(void)
{
if (prov_ctx.pbg_count) {
prov_ctx.pbg_count--;
}{...}
}{...}
static inline void provisioner_pbg_count_inc(void)
{
prov_ctx.pbg_count++;
}{...}
void bt_mesh_provisioner_clear_link_info(const uint8_t addr[6])
{
int i;
if (!addr) {
BT_ERR("%s, Invalid parameter", __func__);
return;
}{...}
BT_DBG("Clear device info, addr %s", bt_hex(addr, BLE_MESH_ADDR_LEN));
for (i = CONFIG_BLE_MESH_PBA_SAME_TIME; i < BLE_MESH_PROV_SAME_TIME; i++) {
if (!memcmp(prov_links[i].addr.val, addr, BLE_MESH_ADDR_LEN)) {
bt_mesh_atomic_clear_bit(prov_links[i].flags, CONNECTING);
prov_links[i].conn = NULL;
prov_links[i].oob_info = 0x0;
memset(prov_links[i].uuid, 0, 16);
memset(&prov_links[i].addr, 0, sizeof(bt_mesh_addr_t));
bt_mesh_atomic_clear_bit(prov_links[i].flags, LINK_ACTIVE);
k_delayed_work_cancel(&prov_links[i].prot_timer);
return;
}{...}
}{...}
BT_WARN("Device not found, addr %s", bt_hex(addr, BLE_MESH_ADDR_LEN));
}{...}
/* ... */#endif
#if CONFIG_BLE_MESH_SETTINGS
void bt_mesh_provisioner_restore_prov_info(uint16_t primary_addr, uint16_t alloc_addr)
{
prov_ctx.primary_addr = primary_addr;
prov_ctx.alloc_addr = alloc_addr;
}{...}
/* ... */#endif
static bool is_unprov_dev_being_provision(const uint8_t uuid[16])
{
int i;
#if CONFIG_BLE_MESH_FAST_PROV
/* ... */
if (bt_mesh_provisioner_get_node_with_uuid(uuid)) {
BT_WARN("Device has already been provisioned");
return true;
}{...}
/* ... */#endif
for (i = 0; i < BLE_MESH_PROV_SAME_TIME; i++) {
if (bt_mesh_atomic_test_bit(prov_links[i].flags, LINK_ACTIVE)
#if CONFIG_BLE_MESH_PB_GATT
|| bt_mesh_atomic_test_bit(prov_links[i].flags, CONNECTING)
#endif
) {
if (!memcmp(prov_links[i].uuid, uuid, 16)) {
BT_DBG("Device is being provisioning");
return true;
}{...}
}{...}
}{...}
return false;
}{...}
static bool is_unprov_dev_uuid_match(const uint8_t uuid[16])
{
if (prov_ctx.match_length == 0) {
return true;
}{...}
if (memcmp(uuid + prov_ctx.match_offset,
prov_ctx.match_value, prov_ctx.match_length)) {
return false;
}{...}
return true;
}{...}
static int provisioner_check_unprov_dev_info(const uint8_t uuid[16], bt_mesh_prov_bearer_t bearer)
{
if (!uuid) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}{...}
if (is_unprov_dev_uuid_match(uuid) == false) {
BT_DBG("Device uuid mismatch");
return -EIO;
}{...}
/* ... */
if (is_unprov_dev_being_provision(uuid)) {
return -EALREADY;
}{...}
#if CONFIG_BLE_MESH_PB_ADV
if ((prov_ctx.bearers & BLE_MESH_PROV_ADV) &&
(bearer == BLE_MESH_PROV_ADV) &&
(prov_ctx.pba_count == CONFIG_BLE_MESH_PBA_SAME_TIME)) {
BT_INFO("Current PB-ADV links reach max limit");
return -ENOMEM;
}{...}
/* ... */#endif
#if CONFIG_BLE_MESH_PB_GATT
if ((prov_ctx.bearers & BLE_MESH_PROV_GATT) &&
(bearer == BLE_MESH_PROV_GATT) &&
(prov_ctx.pbg_count == CONFIG_BLE_MESH_PBG_SAME_TIME)) {
BT_INFO("Current PB-GATT links reach max limit");
return -ENOMEM;
}{...}
/* ... */#endif
if (bt_mesh_provisioner_get_node_with_uuid(uuid)) {
BT_INFO("Provisioned before, start to provision again");
}{...}
return 0;
}{...}
#if CONFIG_BLE_MESH_PB_ADV
static int provisioner_start_prov_pb_adv(const uint8_t uuid[16], const bt_mesh_addr_t *addr,
uint16_t oob_info, uint16_t assign_addr)
{
int i;
if (uuid == NULL) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}{...}
bt_mesh_pb_adv_lock();
/* ... */
if (assign_addr == BLE_MESH_ADDR_UNASSIGNED &&
prov_ctx.alloc_addr == BLE_MESH_ADDR_UNASSIGNED) {
BT_ERR("No available unicast address to assign");
bt_mesh_pb_adv_unlock();
return -EIO;
}{...}
if (is_unprov_dev_being_provision(uuid)) {
bt_mesh_pb_adv_unlock();
return 0;
}{...}
for (i = 0; i < CONFIG_BLE_MESH_PBA_SAME_TIME; i++) {
if (!bt_mesh_atomic_test_bit(prov_links[i].flags, LINK_ACTIVE) &&
!bt_mesh_atomic_test_bit(prov_links[i].flags, LINK_CLOSING)) {
memcpy(prov_links[i].uuid, uuid, 16);
prov_links[i].oob_info = oob_info;
if (addr) {
prov_links[i].addr.type = addr->type;
memcpy(prov_links[i].addr.val, addr->val, BLE_MESH_ADDR_LEN);
}{...}
send_link_open(&prov_links[i]);
/* ... */
if (BLE_MESH_ADDR_IS_UNICAST(assign_addr)) {
prov_links[i].assign_addr = assign_addr;
}{...}
prov_ctx.pba_count++;
bt_mesh_pb_adv_unlock();
return 0;
}{...}
}{...}
BT_ERR("No PB-ADV link available");
bt_mesh_pb_adv_unlock();
return -ENOMEM;
}{...}
/* ... */#endif
#if CONFIG_BLE_MESH_PB_GATT
static int provisioner_start_prov_pb_gatt(const uint8_t uuid[16], const bt_mesh_addr_t *addr,
uint16_t oob_info, uint16_t assign_addr)
{
int i;
if (uuid == NULL || addr == NULL) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}{...}
bt_mesh_pb_gatt_lock();
/* ... */
if (assign_addr == BLE_MESH_ADDR_UNASSIGNED &&
prov_ctx.alloc_addr == BLE_MESH_ADDR_UNASSIGNED) {
BT_ERR("No available unicast address to assign");
bt_mesh_pb_gatt_unlock();
return -EIO;
}{...}
if (is_unprov_dev_being_provision(uuid)) {
bt_mesh_pb_gatt_unlock();
return 0;
}{...}
for (i = CONFIG_BLE_MESH_PBA_SAME_TIME; i < BLE_MESH_PROV_SAME_TIME; i++) {
if (!bt_mesh_atomic_test_bit(prov_links[i].flags, CONNECTING) &&
!bt_mesh_atomic_test_bit(prov_links[i].flags, LINK_ACTIVE)) {
if (bt_mesh_gattc_conn_create(addr, BLE_MESH_UUID_MESH_PROV_VAL)) {
bt_mesh_pb_gatt_unlock();
return -EIO;
}{...}
memcpy(prov_links[i].uuid, uuid, 16);
prov_links[i].oob_info = oob_info;
prov_links[i].addr.type = addr->type;
memcpy(prov_links[i].addr.val, addr->val, BLE_MESH_ADDR_LEN);
/* ... */
if (BLE_MESH_ADDR_IS_UNICAST(assign_addr)) {
prov_links[i].assign_addr = assign_addr;
}{...}
bt_mesh_atomic_set_bit(prov_links[i].flags, CONNECTING);
provisioner_pbg_count_inc();
bt_mesh_pb_gatt_unlock();
return 0;
}{...}
}{...}
BT_ERR("No PB-GATT link available");
bt_mesh_pb_gatt_unlock();
return -ENOMEM;
}{...}
/* ... */#endif
int bt_mesh_provisioner_add_unprov_dev(struct bt_mesh_unprov_dev_add *add_dev, uint8_t flags)
{
bt_mesh_addr_t add_addr = {0};
bool addr_valid = false;
uint8_t zero[16] = {0};
int err = 0;
int i;
if (add_dev == NULL) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}{...}
if (!memcmp(add_dev->uuid, zero, 16)) {
BT_ERR("Invalid device uuid to add");
return -EINVAL;
}{...}
if (!(add_dev->bearer & (BLE_MESH_PROV_ADV | BLE_MESH_PROV_GATT))) {
BT_ERR("Invalid bearer 0x%02x", add_dev->bearer);
return -EINVAL;
}{...}
if ((!IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) ||
!(prov_ctx.bearers & BLE_MESH_PROV_GATT))
&& (add_dev->bearer & BLE_MESH_PROV_GATT)) {
BT_ERR("Not support PB-GATT");
return -EINVAL;
}{...}
if ((!IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) ||
!(prov_ctx.bearers & BLE_MESH_PROV_ADV))
&& (add_dev->bearer & BLE_MESH_PROV_ADV)) {
BT_ERR("Not support PB-ADV");
return -EINVAL;
}{...}
if (memcmp(add_dev->addr, zero, BLE_MESH_ADDR_LEN)) {
addr_valid = true;
add_addr.type = add_dev->addr_type;
memcpy(add_addr.val, add_dev->addr, BLE_MESH_ADDR_LEN);
}{...}
if ((add_dev->bearer & BLE_MESH_PROV_GATT) && (addr_valid == false)) {
BT_ERR("Invalid device address for PB-GATT");
return -EINVAL;
}{...}
if ((flags & START_PROV_NOW) &&
(add_dev->bearer != BLE_MESH_PROV_ADV) &&
(add_dev->bearer != BLE_MESH_PROV_GATT)) {
BT_ERR("Can not start PB-ADV & PB-GATT simultaneously");
return -EINVAL;
}{...}
if (bt_mesh_provisioner_get_node_with_uuid(add_dev->uuid) == NULL &&
bt_mesh_provisioner_get_node_count() == CONFIG_BLE_MESH_MAX_PROV_NODES) {
BT_WARN("Current provisioned devices reach max limit");
return -ENOMEM;
}{...}
for (i = 0; i < ARRAY_SIZE(unprov_dev); i++) {
if (!memcmp(unprov_dev[i].uuid, add_dev->uuid, 16)) {
if (!(add_dev->bearer & unprov_dev[i].bearer)) {
BT_WARN("Add device with only bearer updated");
unprov_dev[i].bearer |= add_dev->bearer;
}{...} else {
BT_WARN("Device already exists in queue");
}{...}
goto start;
}{...}
}{...}
for (i = 0; i < ARRAY_SIZE(unprov_dev); i++) {
if (unprov_dev[i].bearer) {
continue;
}{...}
if (addr_valid) {
unprov_dev[i].addr.type = add_dev->addr_type;
memcpy(unprov_dev[i].addr.val, add_dev->addr, BLE_MESH_ADDR_LEN);
}{...}
memcpy(unprov_dev[i].uuid, add_dev->uuid, 16);
unprov_dev[i].bearer = add_dev->bearer & BIT_MASK(2);
unprov_dev[i].flags = flags & BIT_MASK(3);
goto start;
}{...}
for (i = 0; i < ARRAY_SIZE(unprov_dev); i++) {
if (unprov_dev[i].flags & FLUSHABLE_DEV) {
memset(&unprov_dev[i], 0, sizeof(struct unprov_dev_queue));
if (addr_valid) {
unprov_dev[i].addr.type = add_dev->addr_type;
memcpy(unprov_dev[i].addr.val, add_dev->addr, BLE_MESH_ADDR_LEN);
}{...}
memcpy(unprov_dev[i].uuid, add_dev->uuid, 16);
unprov_dev[i].bearer = add_dev->bearer & BIT_MASK(2);
unprov_dev[i].flags = flags & BIT_MASK(3);
goto start;
}{...}
}{...}
BT_ERR("Unprovisioned device queue is full");
return -ENOMEM;
start:
if (!(flags & START_PROV_NOW)) {
return 0;
}{...}
if (bt_mesh_provisioner_get_node_with_uuid(add_dev->uuid) == NULL) {
if (bt_mesh_provisioner_get_node_count()
#if CONFIG_BLE_MESH_PB_ADV
+ prov_ctx.pba_count
#endif
#if CONFIG_BLE_MESH_PB_GATT
+ prov_ctx.pbg_count
#endif
>= CONFIG_BLE_MESH_MAX_PROV_NODES) {
BT_WARN("Node count + active link count reach max limit");
return -EIO;
}{...}
}{...}
if ((err = provisioner_check_unprov_dev_info(add_dev->uuid, add_dev->bearer))) {
return err;
}{...}
#if CONFIG_BLE_MESH_PB_ADV
if (add_dev->bearer == BLE_MESH_PROV_ADV) {
return provisioner_start_prov_pb_adv(add_dev->uuid, addr_valid ? &add_addr : NULL,
add_dev->oob_info, BLE_MESH_ADDR_UNASSIGNED);
}{...}
/* ... */#endif
#if CONFIG_BLE_MESH_PB_GATT
if (add_dev->bearer == BLE_MESH_PROV_GATT) {
return provisioner_start_prov_pb_gatt(add_dev->uuid, &add_addr, add_dev->oob_info,
BLE_MESH_ADDR_UNASSIGNED);
}{...}
/* ... */#endif
return 0;
}{...}
int bt_mesh_provisioner_prov_device_with_addr(const uint8_t uuid[16], const uint8_t addr[6],
uint8_t addr_type, bt_mesh_prov_bearer_t bearer,
uint16_t oob_info, uint16_t unicast_addr)
{
bt_mesh_addr_t dev_addr = {0};
int err = 0;
if (uuid == NULL) {
BT_ERR("Invalid device uuid");
return -EINVAL;
}{...}
if (bearer != BLE_MESH_PROV_ADV && bearer != BLE_MESH_PROV_GATT) {
BT_ERR("Invalid provisioning bearer 0x%02x", bearer);
return -EINVAL;
}{...}
if ((!IS_ENABLED(CONFIG_BLE_MESH_PB_ADV) ||
!(prov_ctx.bearers & BLE_MESH_PROV_ADV)) &&
(bearer == BLE_MESH_PROV_ADV)) {
BT_ERR("Not support PB-ADV");
return -ENOTSUP;
}{...}
if ((!IS_ENABLED(CONFIG_BLE_MESH_PB_GATT) ||
!(prov_ctx.bearers & BLE_MESH_PROV_GATT)) &&
(bearer == BLE_MESH_PROV_GATT)) {
BT_ERR("Not support PB-GATT");
return -ENOTSUP;
}{...}
if (bearer == BLE_MESH_PROV_GATT && addr == NULL) {
BT_ERR("Invalid device address for PB-GATT");
return -EINVAL;
}{...}
if (!BLE_MESH_ADDR_IS_UNICAST(unicast_addr)) {
BT_ERR("Invalid unicast address 0x%04x", unicast_addr);
return -EINVAL;
}{...}
/* ... */
if (bt_mesh_provisioner_get_node_with_uuid(uuid) == NULL) {
if (bt_mesh_provisioner_get_node_count() == CONFIG_BLE_MESH_MAX_PROV_NODES) {
BT_WARN("Current provisioned devices reach max limit");
return -ENOMEM;
}{...}
if (bt_mesh_provisioner_get_node_count()
#if CONFIG_BLE_MESH_PB_ADV
+ prov_ctx.pba_count
#endif
#if CONFIG_BLE_MESH_PB_GATT
+ prov_ctx.pbg_count
#endif
>= CONFIG_BLE_MESH_MAX_PROV_NODES) {
BT_WARN("Node count + active link count reach max limit");
return -EIO;
}{...}
}{...}
if ((err = provisioner_check_unprov_dev_info(uuid, bearer))) {
return err;
}{...}
if (addr) {
dev_addr.type = addr_type;
memcpy(dev_addr.val, addr, BLE_MESH_ADDR_LEN);
}{...}
#if CONFIG_BLE_MESH_PB_ADV
if (bearer == BLE_MESH_PROV_ADV) {
return provisioner_start_prov_pb_adv(uuid, addr ? &dev_addr : NULL, oob_info, unicast_addr);
}{...}
/* ... */#endif
#if CONFIG_BLE_MESH_PB_GATT
if (bearer == BLE_MESH_PROV_GATT) {
return provisioner_start_prov_pb_gatt(uuid, &dev_addr, oob_info, unicast_addr);
}{...}
/* ... */#endif
return 0;
}{...}
int bt_mesh_provisioner_delete_device(struct bt_mesh_device_delete *del_dev)
{
uint8_t zero[16] = {0};
int i;
if (del_dev == NULL) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}{...}
if (!memcmp(del_dev->uuid, zero, 16)) {
BT_ERR("Invalid device uuid to delete");
return -EINVAL;
}{...}
for (i = 0; i < ARRAY_SIZE(unprov_dev); i++) {
if (!memcmp(unprov_dev[i].uuid, del_dev->uuid, 16)) {
memset(&unprov_dev[i], 0, sizeof(struct unprov_dev_queue));
break;
}{...}
}{...}
for (i = 0; i < ARRAY_SIZE(prov_links); i++) {
if (!memcmp(prov_links[i].uuid, del_dev->uuid, 16)) {
close_link(&prov_links[i], CLOSE_REASON_FAILED);
break;
}{...}
}{...}
return 0;
}{...}
int bt_mesh_provisioner_set_dev_uuid_match(uint8_t offset, uint8_t length,
const uint8_t *match, bool prov_flag)
{
if (length && (match == NULL || (offset + length > 16))) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}{...}
(void)memset(prov_ctx.match_value, 0, 16);
prov_ctx.match_offset = offset;
prov_ctx.match_length = length;
if (length) {
memcpy(prov_ctx.match_value, match, length);
}{...}
prov_ctx.prov_after_match = prov_flag;
return 0;
}{...}
int bt_mesh_provisioner_adv_pkt_cb_register(unprov_adv_pkt_cb_t cb)
{
if (!cb) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}{...}
notify_unprov_adv_pkt_cb = cb;
return 0;
}{...}
int bt_mesh_provisioner_set_prov_data_info(struct bt_mesh_prov_data_info *info)
{
const uint8_t *key = NULL;
if (info == NULL || info->flag == 0) {
return -EINVAL;
}{...}
if (info->flag & NET_IDX_FLAG) {
key = bt_mesh_provisioner_net_key_get(info->net_idx);
if (!key) {
BT_ERR("Failed to get NetKey");
return -EINVAL;
}{...}
prov_ctx.net_idx = info->net_idx;
}{...}
return 0;
}{...}
static inline uint8_t get_net_flags(uint16_t net_idx)
{
return bt_mesh_net_flags(bt_mesh_subnet_get(net_idx));
}{...}
int bt_mesh_provisioner_init_prov_info(void)
{
if (prov_ctx.primary_addr == BLE_MESH_ADDR_UNASSIGNED) {
/* ... */
if (bt_mesh_prov_get() == NULL) {
BT_ERR("No provisioning context provided");
return -EINVAL;
}{...}
if (!BLE_MESH_ADDR_IS_UNICAST(bt_mesh_prov_get()->prov_unicast_addr) ||
!BLE_MESH_ADDR_IS_UNICAST(bt_mesh_prov_get()->prov_start_address)) {
BT_ERR("Invalid address, own 0x%04x, start 0x%04x",
bt_mesh_prov_get()->prov_unicast_addr,
bt_mesh_prov_get()->prov_start_address);
return -EINVAL;
}{...}
const struct bt_mesh_comp *comp = bt_mesh_comp_get();
if (!comp) {
BT_ERR("Invalid composition data");
return -EINVAL;
}{...}
if (bt_mesh_prov_get()->prov_unicast_addr + comp->elem_count >
bt_mesh_prov_get()->prov_start_address) {
BT_WARN("Too small start address 0x%04x, update to 0x%04x",
bt_mesh_prov_get()->prov_start_address,
bt_mesh_prov_get()->prov_unicast_addr + comp->elem_count);
prov_ctx.alloc_addr = bt_mesh_prov_get()->prov_unicast_addr + comp->elem_count;
}{...} else {
prov_ctx.alloc_addr = bt_mesh_prov_get()->prov_start_address;
}{...}
prov_ctx.primary_addr = bt_mesh_prov_get()->prov_unicast_addr;
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_prov_info(prov_ctx.primary_addr, prov_ctx.alloc_addr);
}{...}
}{...}
prov_ctx.net_idx = BLE_MESH_KEY_PRIMARY;
return 0;
}{...}
void bt_mesh_provisioner_set_prov_bearer(bt_mesh_prov_bearer_t bearers, bool clear)
{
if (clear == false) {
prov_ctx.bearers |= bearers;
}{...} else {
prov_ctx.bearers &= ~bearers;
}{...}
}{...}
bt_mesh_prov_bearer_t bt_mesh_provisioner_get_prov_bearer(void)
{
return prov_ctx.bearers;
}{...}
int bt_mesh_provisioner_set_static_oob_value(const uint8_t *value, uint8_t length)
{
int i;
if (value == NULL || length == 0U || length > BLE_MESH_PROV_STATIC_OOB_MAX_LEN) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}{...}
for (i = 0; i < BLE_MESH_PROV_SAME_TIME; i++) {
if (prov_links[i].auth_method == AUTH_METHOD_STATIC) {
BT_ERR("Static OOB is being used");
return -EINVAL;
}{...}
}{...}
(void)memset(prov_ctx.static_oob_val, 0, BLE_MESH_PROV_STATIC_OOB_MAX_LEN);
prov_ctx.static_oob_len = MIN(BLE_MESH_PROV_STATIC_OOB_MAX_LEN, length);
memcpy(prov_ctx.static_oob_val, value, prov_ctx.static_oob_len);
return 0;
}{...}
uint16_t bt_mesh_provisioner_get_primary_elem_addr(void)
{
return prov_ctx.primary_addr;
}{...}
int bt_mesh_provisioner_set_primary_elem_addr(uint16_t addr)
{
const struct bt_mesh_comp *comp = NULL;
if (!BLE_MESH_ADDR_IS_UNICAST(addr)) {
BT_ERR("Invalid primary address 0x%04x", addr);
return -EINVAL;
}{...}
comp = bt_mesh_comp_get();
if (!comp) {
BT_ERR("Invalid composition data");
return -EINVAL;
}{...}
if (bt_mesh_provisioner_check_is_addr_dup(addr, comp->elem_count, false)) {
BT_ERR("Address 0x%04x is duplicated with node address", addr);
return -EINVAL;
}{...}
/* ... */
if (addr + comp->elem_count > prov_ctx.alloc_addr) {
prov_ctx.alloc_addr = addr + comp->elem_count;
}{...}
BT_INFO("Primary address updated, old 0x%04x, new 0x%04x", prov_ctx.primary_addr, addr);
prov_ctx.primary_addr = addr;
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_prov_info(prov_ctx.primary_addr, prov_ctx.alloc_addr);
}{...}
bt_mesh_comp_provision(addr);
return 0;
}{...}
#if CONFIG_BLE_MESH_TEST_AUTO_ENTER_NETWORK
int bt_mesh_test_provisioner_update_alloc_addr(uint16_t unicast_addr, uint16_t element_num)
{
uint16_t max_addr = FAST_PROV_ENABLE() ? prov_ctx.fast_prov.unicast_addr_max : UNICAST_ADDR_LIMIT;
if (unicast_addr + element_num > max_addr) {
BT_WARN("Not enough unicast address to allocate");
prov_ctx.alloc_addr = BLE_MESH_ADDR_UNASSIGNED;
}{...} else {
prov_ctx.alloc_addr = unicast_addr + element_num;
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_prov_info(prov_ctx.primary_addr, prov_ctx.alloc_addr);
}{...}
return 0;
}{...}
/* ... */#endif
void bt_mesh_provisioner_fast_prov_enable(bool enable)
{
prov_ctx.fast_prov.enable = enable;
}{...}
void bt_mesh_provisioner_set_fast_prov_net_idx(uint16_t net_idx)
{
prov_ctx.fast_prov.net_idx = net_idx;
}{...}
uint16_t bt_mesh_provisioner_get_fast_prov_net_idx(void)
{
return prov_ctx.fast_prov.net_idx;
}{...}
uint8_t bt_mesh_set_fast_prov_unicast_addr_range(uint16_t min, uint16_t max)
{
if (!BLE_MESH_ADDR_IS_UNICAST(min) || !BLE_MESH_ADDR_IS_UNICAST(max)) {
BT_ERR("Invalid unicast address, min 0x%04x, max 0x%04x", min, max);
return 0x01;
}{...}
if (min > max) {
BT_ERR("Unicast address min is bigger than max");
return 0x02;
}{...}
if (min <= prov_ctx.fast_prov.unicast_addr_max) {
BT_ERR("Unicast address overlap");
return 0x03;
}{...}
prov_ctx.fast_prov.unicast_addr_min = min;
prov_ctx.fast_prov.unicast_addr_max = max;
prov_ctx.alloc_addr = prov_ctx.fast_prov.unicast_addr_min;
return 0x0;
}{...}
static void prov_memory_free(struct bt_mesh_prov_link *link)
{
}{...}
#if CONFIG_BLE_MESH_PB_ADV
static struct net_buf_simple *get_rx_buf(const uint8_t idx)
{
struct net_buf_simple *buf = &(rx_buf[idx].buf);
net_buf_simple_reset(buf);
return buf;
}{...}
static void reset_adv_link(struct bt_mesh_prov_link *link, uint8_t reason)
{
bt_mesh_prov_clear_tx(link, true);
if (bt_mesh_prov_get()->prov_link_close) {
bt_mesh_prov_get()->prov_link_close(BLE_MESH_PROV_ADV, reason);
}{...}
prov_memory_free(link);
k_delayed_work_cancel(&link->prot_timer);
#if CONFIG_BLE_MESH_PB_GATT
if (link->conn) {
bt_mesh_conn_unref(link->conn);
}{...}
/* ... */#endif
#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN
bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_REMOVE,
BLE_MESH_EXCEP_LIST_TYPE_MESH_LINK_ID,
&link->link_id);/* ... */
#endif
memset(link, 0, offsetof(struct bt_mesh_prov_link, tx.retransmit));
link->pending_ack = PROV_XACT_NVAL;
link->rx.prev_id = PROV_XACT_NVAL;
link->rx.buf = get_rx_buf(link - prov_links);
link->next_xact_id = pvnr_next_xact_id;
link->reset_adv_link = reset_adv_link;
link->retrans_timeout = close_link;
#if CONFIG_BLE_MESH_FAST_PROV
link->last_tx_pdu = PROV_DATA;
#endif
if (prov_ctx.pba_count) {
prov_ctx.pba_count--;
}{...}
}{...}
static void send_link_open(struct bt_mesh_prov_link *link)
{
uint8_t count;
int i;
link->link_id = 0;
while (1) {
count = 0;
while(link->link_id == 0) {
bt_mesh_rand(&link->link_id, sizeof(link->link_id));
if (count++ > 10) {
BT_ERR("Link ID error: all zero");
return;
}{...}
}{...}
for (i = 0; i < CONFIG_BLE_MESH_PBA_SAME_TIME; i++) {
if (bt_mesh_atomic_test_bit(prov_links[i].flags, LINK_ACTIVE) &&
prov_links[i].link_id == link->link_id) {
bt_mesh_rand(&link->link_id, sizeof(link->link_id));
break;
}{...}
}{...}
if (i == CONFIG_BLE_MESH_PBA_SAME_TIME) {
break;
}{...}
}{...}
#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN
bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_ADD,
BLE_MESH_EXCEP_LIST_TYPE_MESH_LINK_ID,
&link->link_id);/* ... */
#endif
bt_mesh_prov_bearer_ctl_send(link, LINK_OPEN, link->uuid, 16);
bt_mesh_atomic_set_bit(link->flags, LINK_ACTIVE);
if (bt_mesh_prov_get()->prov_link_open) {
bt_mesh_prov_get()->prov_link_open(BLE_MESH_PROV_ADV);
}{...}
}{...}
/* ... */#endif
#if CONFIG_BLE_MESH_PB_GATT
static int prov_send_gatt(struct bt_mesh_prov_link *link, struct net_buf_simple *msg)
{
int err = 0;
if (link->conn == NULL) {
BT_ERR("PB-GATT send, not connected");
return -ENOTCONN;
}{...}
err = bt_mesh_proxy_client_send(link->conn, BLE_MESH_PROXY_PROV, msg);
if (err) {
BT_ERR("Failed to send PB-GATT pdu");
return err;
}{...}
k_delayed_work_submit(&link->prot_timer, PROTOCOL_TIMEOUT);
return 0;
}{...}
/* ... */#endif
static void prov_invite(struct bt_mesh_prov_link *link,
struct net_buf_simple *buf)
{}{...}
static void prov_start(struct bt_mesh_prov_link *link,
struct net_buf_simple *buf)
{}{...}
static void prov_data(struct bt_mesh_prov_link *link,
struct net_buf_simple *buf)
{}{...}
static void send_invite(struct bt_mesh_prov_link *link)
{
PROV_BUF(buf, 2);
bt_mesh_prov_buf_init(&buf, PROV_INVITE);
net_buf_simple_add_u8(&buf, bt_mesh_prov_get()->prov_attention);
link->conf_inputs[0] = bt_mesh_prov_get()->prov_attention;
if (bt_mesh_prov_send(link, &buf)) {
BT_ERR("Failed to send Provisioning Invite");
close_link(link, CLOSE_REASON_FAILED);
return;
}{...}
link->expect = PROV_CAPABILITIES;
}{...}
static void prov_capabilities(struct bt_mesh_prov_link *link,
struct net_buf_simple *buf)
{
uint16_t output_action = 0U, input_action = 0U;
uint8_t output_size = 0U, input_size = 0U;
uint8_t oob_type = 0U;
uint8_t pub_key_type = 0U;
uint16_t algorithms = 0U;
uint8_t *data = buf->data;
PROV_BUF(prov_buf, 6);
link->element_num = net_buf_simple_pull_u8(buf);
BT_INFO("Elements: 0x%02x", link->element_num);
if (link->element_num == 0) {
BT_ERR("Invalid element number %d", link->element_num);
goto fail;
}{...}
algorithms = net_buf_simple_pull_be16(buf);
BT_INFO("Algorithms: 0x%04x", algorithms);
if (!(algorithms & BIT(PROV_ALG_P256_CMAC_AES128))
#if CONFIG_BLE_MESH_PROV_EPA
&& !(algorithms & BIT(PROV_ALG_P256_HMAC_SHA256))
#endif
) {
BT_ERR("Invalid algorithms 0x%04x", algorithms);
goto fail;
}{...}
pub_key_type = net_buf_simple_pull_u8(buf);
BT_INFO("Public Key Type: 0x%02x", pub_key_type);
if (pub_key_type > PROV_OOB_PUB_KEY) {
BT_ERR("Invalid public key type 0x%02x", pub_key_type);
goto fail;
}{...}
link->public_key = ((bt_mesh_prov_get()->prov_pub_key_oob &&
bt_mesh_prov_get()->prov_pub_key_oob_cb) ?
pub_key_type : PROV_NO_OOB_PUB_KEY);
oob_type = net_buf_simple_pull_u8(buf);
BT_INFO("OOB Type: 0x%02x", oob_type);
output_size = net_buf_simple_pull_u8(buf);
BT_INFO("Output OOB Size: 0x%02x", output_size);
if (output_size > 0x08) {
BT_ERR("Invalid Output OOB size %d", output_size);
goto fail;
}{...}
output_action = net_buf_simple_pull_be16(buf);
BT_INFO("Output OOB Action: 0x%04x", output_action);
if (output_action > 0x1f) {
BT_ERR("Invalid Output OOB action 0x%04x", output_action);
goto fail;
}{...}
if (bt_mesh_prov_get()->prov_input_num && output_size) {
output_action = __builtin_ctz(output_action);
}{...} else {
output_size = 0x0;
output_action = 0x0;
}{...}
input_size = net_buf_simple_pull_u8(buf);
BT_INFO("Input OOB Size: 0x%02x", input_size);
if (input_size > 0x08) {
BT_ERR("Invalid Input OOB size %d", input_size);
goto fail;
}{...}
input_action = net_buf_simple_pull_be16(buf);
BT_INFO("Input OOB Action: 0x%04x", input_action);
if (input_action > 0x0f) {
BT_ERR("Invalid Input OOB action 0x%04x", input_action);
goto fail;
}{...}
/* ... */
if (oob_type & BIT(PROV_ONLY_OOB_AUTH_SUPPORT)) {
if ((algorithms & BIT(PROV_ALG_P256_CMAC_AES128)) ||
(!((oob_type & BIT(PROV_STATIC_OOB_AVAILABLE)) == 0x00 ||
output_size == 0x00 || input_size == 0x00))) {
goto fail;
}{...}
}{...}
if (bt_mesh_prov_get()->prov_output_num && input_size) {
input_action = __builtin_ctz(input_action);
}{...} else {
input_size = 0x0;
input_action = 0x0;
}{...}
if (oob_type & BIT(PROV_STATIC_OOB_AVAILABLE)) {
link->auth_method = AUTH_METHOD_STATIC;
link->auth_action = 0x00;
link->auth_size = 0x00;
}{...} else {
if (!output_size && !input_size) {
link->auth_method = AUTH_METHOD_NO_OOB;
link->auth_action = 0x00;
link->auth_size = 0x00;
}{...} else if (!output_size && input_size) {
link->auth_method = AUTH_METHOD_INPUT;
link->auth_action = (uint8_t)input_action;
link->auth_size = input_size;
}{...} else {
link->auth_method = AUTH_METHOD_OUTPUT;
link->auth_action = (uint8_t)output_action;
link->auth_size = output_size;
}{...}
}{...}
memcpy(&link->conf_inputs[1], data, 11);
bt_mesh_prov_buf_init(&prov_buf, PROV_START);
#if CONFIG_BLE_MESH_PROV_EPA
if (algorithms & BIT(PROV_ALG_P256_HMAC_SHA256)) {
net_buf_simple_add_u8(&prov_buf, PROV_ALG_P256_HMAC_SHA256);
link->algorithm = PROV_ALG_P256_HMAC_SHA256;
}{...} else {
net_buf_simple_add_u8(&prov_buf, PROV_ALG_P256_CMAC_AES128);
link->algorithm = PROV_ALG_P256_CMAC_AES128;
}{...}
/* ... */#else
net_buf_simple_add_u8(&prov_buf, PROV_ALG_P256_CMAC_AES128);
link->algorithm = PROV_ALG_P256_CMAC_AES128;/* ... */
#endif
net_buf_simple_add_u8(&prov_buf, link->public_key);
net_buf_simple_add_u8(&prov_buf, link->auth_method);
net_buf_simple_add_u8(&prov_buf, link->auth_action);
net_buf_simple_add_u8(&prov_buf, link->auth_size);
memcpy(&link->conf_inputs[12], &prov_buf.data[1], 5);
if (bt_mesh_prov_send(link, &prov_buf)) {
BT_ERR("Failed to send Provisioning Start");
goto fail;
}{...}
/* ... */
if (link->public_key == PROV_OOB_PUB_KEY &&
bt_mesh_prov_get()->prov_pub_key_oob_cb(link - prov_links)) {
BT_ERR("Failed to notify input OOB Public Key");
goto fail;
}{...}
/* ... */
if (bt_mesh_atomic_test_bit(link->flags, PB_REMOTE)) {
return;
}{...}
#if CONFIG_BLE_MESH_PB_ADV
/* ... */
if (link - prov_links < CONFIG_BLE_MESH_PBA_SAME_TIME) {
link->expect_ack_for = PROV_START;
return;
}{...}
/* ... */#endif
send_pub_key(link);
return;
fail:
close_link(link, CLOSE_REASON_FAILED);
}{...}
static int prov_auth(struct bt_mesh_prov_link *link,
uint8_t method, uint8_t action,
uint8_t size)
{
bt_mesh_output_action_t output = 0U;
bt_mesh_input_action_t input = 0U;
uint8_t auth_size = PROV_AUTH_SIZE(link);
switch (method) {
case AUTH_METHOD_NO_OOB:
if (action || size) {
return -EINVAL;
}{...}
memset(link->auth, 0, PROV_AUTH_MAX_SIZE);
return 0;
...
case AUTH_METHOD_STATIC:
if (action || size) {
return -EINVAL;
}{...}
if (prov_ctx.static_oob_len > auth_size) {
memcpy(link->auth, prov_ctx.static_oob_val, auth_size);
}{...} else {
memcpy(link->auth + auth_size - prov_ctx.static_oob_len,
prov_ctx.static_oob_val, prov_ctx.static_oob_len);
memset(link->auth, 0, auth_size - prov_ctx.static_oob_len);
}{...}
return 0;
...
case AUTH_METHOD_OUTPUT:
output = bt_mesh_prov_output_action(action);
if (!output) {
return -EINVAL;
}{...}
return bt_mesh_prov_get()->prov_input_num(AUTH_METHOD_OUTPUT, output,
size, link - prov_links);
...
case AUTH_METHOD_INPUT:
input = bt_mesh_prov_input_action(action);
if (!input) {
return -EINVAL;
}{...}
link->expect = PROV_INPUT_COMPLETE;
/* ... */
if (input == BLE_MESH_ENTER_STRING) {
unsigned char str[9] = {'\0'};
uint8_t j = 0U;
bt_mesh_rand(str, size);
for (j = 0U; j < size; j++) {
str[j] %= 36;
if (str[j] < 10) {
str[j] += '0';
}{...} else {
str[j] += 'A' - 10;
}{...}
}{...}
str[size] = '\0';
memcpy(link->auth, str, size);
memset(link->auth + size, 0, auth_size - size);
return bt_mesh_prov_get()->prov_output_num(AUTH_METHOD_INPUT, input, str,
size, link - prov_links);
}{...} else {
uint32_t div[8] = { 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 };
uint32_t num = 0U;
bt_mesh_rand(&num, sizeof(num));
if (input == BLE_MESH_PUSH ||
input == BLE_MESH_TWIST) {
/* ... */
num = (num % (div[size - 1] - 1)) + 1;
}{...} else {
num %= div[size - 1];
}{...}
sys_put_be32(num, &link->auth[auth_size - 4]);
memset(link->auth, 0, auth_size - 4);
return bt_mesh_prov_get()->prov_output_num(AUTH_METHOD_INPUT, input, &num,
size, link - prov_links);
}{...}
...
default:
return -EINVAL;...
}{...}
}{...}
static void send_confirm(struct bt_mesh_prov_link *link)
{
uint8_t *conf = NULL;
uint8_t conf_salt_size = 0;
uint8_t conf_key_size = 0;
uint8_t rand_size = 0;
uint8_t conf_size = 0;
conf_salt_size = PROV_CONF_SALT_SIZE(link);
conf_key_size = PROV_CONF_KEY_SIZE(link);
rand_size = PROV_RAND_SIZE(link);
conf_size = PROV_CONF_SIZE(link);
PROV_BUF(buf, (conf_size + 1));
if (!bt_mesh_atomic_test_bit(link->flags, HAVE_DHKEY)) {
BT_WARN("Wait for generating DHKey");
return;
}{...}
BT_DBG("ConfInputs[0] %s", bt_hex(link->conf_inputs, 64));
BT_DBG("ConfInputs[64] %s", bt_hex(link->conf_inputs + 64, 64));
BT_DBG("ConfInputs[128] %s", bt_hex(link->conf_inputs + 128, 17));
if (link->algorithm == PROV_ALG_P256_CMAC_AES128) {
if (bt_mesh_prov_conf_salt(link->conf_inputs, link->conf_salt)) {
BT_ERR("Failed to generate confirmation salt");
goto fail;
}{...}
if (bt_mesh_prov_conf_key(link->dhkey, link->conf_salt, link->conf_key)) {
BT_ERR("Failed to generate confirmation key");
goto fail;
}{...}
}{...}
#if CONFIG_BLE_MESH_PROV_EPA
else {
if (bt_mesh_prov_conf_salt_epa(link->conf_inputs, link->conf_salt)) {
BT_ERR("Failed to generate confirmation salt");
goto fail;
}{...}
if (bt_mesh_prov_conf_key_epa(link->dhkey, link->auth, link->conf_salt, link->conf_key)) {
BT_ERR("Failed to generate confirmation key");
goto fail;
}{...}
}{...}
/* ... */#endif
if (bt_mesh_rand(link->rand, rand_size)) {
BT_ERR("Failed to generate random number");
goto fail;
}{...}
BT_DBG("ConfirmationSalt: %s", bt_hex(link->conf_salt, conf_salt_size));
BT_DBG("ConfirmationKey: %s", bt_hex(link->conf_key, conf_key_size));
BT_DBG("LocalRandom: %s", bt_hex(link->rand, rand_size));
bt_mesh_prov_buf_init(&buf, PROV_CONFIRM);
conf = net_buf_simple_add(&buf, conf_size);
if (link->algorithm == PROV_ALG_P256_CMAC_AES128) {
if (bt_mesh_prov_conf(link->conf_key, link->rand, link->auth, conf)) {
BT_ERR("Failed to generate confirmation value");
goto fail;
}{...}
}{...}
#if CONFIG_BLE_MESH_PROV_EPA
else {
if (bt_mesh_prov_conf_epa(link->conf_key, link->rand, conf)) {
BT_ERR("Failed to generate confirmation value");
goto fail;
}{...}
}{...}
/* ... */#endif
memcpy(link->local_conf, conf, conf_size);
if (bt_mesh_prov_send(link, &buf)) {
BT_ERR("Failed to send Provisioning Confirm");
goto fail;
}{...}
link->expect = PROV_CONFIRM;
return;
fail:
close_link(link, CLOSE_REASON_FAILED);
}{...}
int bt_mesh_provisioner_set_oob_input_data(const uint8_t idx, const uint8_t *val, bool num_flag)
{
/* ... */
struct bt_mesh_prov_link *link = NULL;
uint8_t auth_size = 0;
if (idx >= ARRAY_SIZE(prov_links) || val == NULL) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}{...}
link = &prov_links[idx];
auth_size = PROV_AUTH_SIZE(link);
if (!bt_mesh_atomic_test_bit(link->flags, LINK_ACTIVE) ||
link->auth_method != AUTH_METHOD_OUTPUT) {
BT_ERR("Not ready for Output OOB, link idx %d", idx);
return -EIO;
}{...}
memset(link->auth, 0, PROV_AUTH_MAX_SIZE);
if (num_flag) {
sys_memcpy_swap(link->auth + auth_size - 4, val, sizeof(uint32_t));
}{...} else {
memcpy(link->auth, val, link->auth_size);
}{...}
BT_INFO("Output OOB, idx %d, type %s, auth %s", idx,
num_flag ? "number" : "string", bt_hex(link->auth, auth_size));
/* ... */
if (bt_mesh_atomic_test_bit(link->flags, PB_REMOTE)) {
if (bt_mesh_atomic_test_bit(link->flags, WAIT_PK_OBR)) {
BT_INFO("PB-Remote, wait for outbound report for public key");
/* ... */
bt_mesh_atomic_set_bit(link->flags, SEND_CONFIRM);
return 0;
}{...}
}{...}
send_confirm(link);
return 0;
}{...}
int bt_mesh_provisioner_set_oob_output_data(const uint8_t idx, const uint8_t *num,
uint8_t size, bool num_flag)
{
/* ... */
struct bt_mesh_prov_link *link = NULL;
uint8_t auth_size = 0;
if (idx >= ARRAY_SIZE(prov_links) || num == NULL ||
size > BLE_MESH_PROV_INPUT_OOB_MAX_LEN) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}{...}
link = &prov_links[idx];
auth_size = PROV_AUTH_SIZE(link);
if (!bt_mesh_atomic_test_bit(link->flags, LINK_ACTIVE) ||
link->auth_method != AUTH_METHOD_INPUT) {
BT_ERR("Not ready for Input OOB, link idx %d", idx);
return -EIO;
}{...}
memset(link->auth, 0, PROV_AUTH_MAX_SIZE);
if (num_flag) {
sys_memcpy_swap(link->auth + auth_size - size, num, size);
}{...} else {
memcpy(link->auth, num, size);
}{...}
BT_INFO("Input OOB, idx %d, type %s, auth %s", idx,
num_flag ? "number" : "string", bt_hex(link->auth, auth_size));
link->expect = PROV_INPUT_COMPLETE;
return 0;
}{...}
int bt_mesh_provisioner_read_oob_pub_key(const uint8_t idx, const uint8_t pub_key_x[32],
const uint8_t pub_key_y[32])
{
struct bt_mesh_prov_link *link = NULL;
if (idx >= ARRAY_SIZE(prov_links) || pub_key_x == NULL || pub_key_y == NULL) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}{...}
link = &prov_links[idx];
if (!bt_mesh_atomic_test_bit(link->flags, LINK_ACTIVE) ||
link->public_key != PROV_OOB_PUB_KEY) {
BT_ERR("Not ready for OOB Public Key, link idx %d", idx);
return -EIO;
}{...}
BT_INFO("OOB Public Key, idx %d, x %s, y %s", idx,
bt_hex(pub_key_x, 32), bt_hex(pub_key_y, 32));
sys_memcpy_swap(&link->conf_inputs[81], pub_key_x, 32);
sys_memcpy_swap(&link->conf_inputs[81] + 32, pub_key_y, 32);
bt_mesh_atomic_set_bit(link->flags, REMOTE_PUB_KEY);
if (bt_mesh_atomic_test_and_clear_bit(link->flags, WAIT_GEN_DHKEY)) {
prov_gen_dh_key(link);
}{...}
return 0;
}{...}
static void prov_gen_dh_key(struct bt_mesh_prov_link *link)
{
uint8_t pub_key[64] = {0};
uint8_t dhkey[32] = {0};
/* ... */
sys_memcpy_swap(&pub_key[0], &link->conf_inputs[81], 32);
sys_memcpy_swap(&pub_key[32], &link->conf_inputs[113], 32);
if (bt_mesh_dh_key_gen(pub_key, dhkey)) {
BT_ERR("Failed to generate DHKey");
close_link(link, CLOSE_REASON_FAILED);
return;
}{...}
sys_memcpy_swap(link->dhkey, dhkey, 32);
BT_DBG("DHKey: %s", bt_hex(link->dhkey, 32));
bt_mesh_atomic_set_bit(link->flags, HAVE_DHKEY);
/* ... */
if (prov_auth(link, link->auth_method,
link->auth_action, link->auth_size)) {
BT_ERR("Failed to authenticate");
close_link(link, CLOSE_REASON_FAILED);
return;
}{...}
/* ... */
if (link->auth_method == AUTH_METHOD_OUTPUT ||
link->auth_method == AUTH_METHOD_INPUT) {
return;
}{...}
/* ... */
if (bt_mesh_atomic_test_bit(link->flags, PB_REMOTE)) {
if (bt_mesh_atomic_test_bit(link->flags, WAIT_PK_OBR)) {
BT_INFO("PB-Remote, wait for outbound report for public key");
/* ... */
bt_mesh_atomic_set_bit(link->flags, SEND_CONFIRM);
return;
}{...}
}{...}
/* ... */
if (link->expect != PROV_INPUT_COMPLETE) {
send_confirm(link);
}{...}
}{...}
static void send_pub_key(struct bt_mesh_prov_link *link)
{
const uint8_t *key = NULL;
PROV_BUF(buf, 65);
key = bt_mesh_pub_key_get();
if (!key) {
BT_ERR("No public key available");
close_link(link, CLOSE_REASON_FAILED);
return;
}{...}
BT_DBG("Local Public Key: %s", bt_hex(key, 64));
bt_mesh_prov_buf_init(&buf, PROV_PUB_KEY);
sys_memcpy_swap(net_buf_simple_add(&buf, 32), key, 32);
sys_memcpy_swap(net_buf_simple_add(&buf, 32), &key[32], 32);
memcpy(&link->conf_inputs[17], &buf.data[1], 64);
if (bt_mesh_prov_send(link, &buf)) {
BT_ERR("Failed to send Provisioning Public Key");
close_link(link, CLOSE_REASON_FAILED);
return;
}{...}
if (link->public_key == PROV_NO_OOB_PUB_KEY) {
link->expect = PROV_PUB_KEY;
return;
}{...}
#if CONFIG_BLE_MESH_PB_ADV
/* ... */
if (!bt_mesh_atomic_test_bit(link->flags, PB_REMOTE) &&
(link - prov_links < CONFIG_BLE_MESH_PBA_SAME_TIME)) {
link->expect_ack_for = PROV_PUB_KEY;
return;
}{...}
/* ... */#endif
if (bt_mesh_atomic_test_bit(link->flags, PB_REMOTE)) {
/* ... */
bt_mesh_atomic_set_bit(link->flags, WAIT_PK_OBR);
}{...}
/* ... */
if (bt_mesh_atomic_test_bit(link->flags, REMOTE_PUB_KEY)) {
prov_gen_dh_key(link);
}{...} else {
bt_mesh_atomic_set_bit(link->flags, WAIT_GEN_DHKEY);
}{...}
}{...}
static void prov_pub_key(struct bt_mesh_prov_link *link,
struct net_buf_simple *buf)
{
BT_DBG("Remote Public Key: %s", bt_hex(buf->data, 64));
/* ... */
if (!bt_mesh_check_public_key(buf->data)) {
BT_ERR("Invalid public key");
close_link(link, CLOSE_REASON_FAILED);
return;
}{...}
memcpy(&link->conf_inputs[81], buf->data, 64);
prov_gen_dh_key(link);
}{...}
static void prov_input_complete(struct bt_mesh_prov_link *link,
struct net_buf_simple *buf)
{
/* ... */
send_confirm(link);
}{...}
static void prov_confirm(struct bt_mesh_prov_link *link,
struct net_buf_simple *buf)
{
uint8_t conf_size = PROV_CONF_SIZE(link);
PROV_BUF(prov_buf, conf_size + 1);
BT_DBG("Remote Confirm: %s", bt_hex(buf->data, conf_size));
/* ... */
if (!memcmp(buf->data, link->local_conf, conf_size)) {
BT_ERR("Confirmation value is identical to ours, rejecting.");
close_link(link, CLOSE_REASON_FAILED);
return;
}{...}
memcpy(link->conf, buf->data, conf_size);
if (!bt_mesh_atomic_test_bit(link->flags, HAVE_DHKEY)) {
#if CONFIG_BLE_MESH_PB_ADV
bt_mesh_prov_clear_tx(link, true);
#endif
bt_mesh_atomic_set_bit(link->flags, SEND_CONFIRM);
}{...}
bt_mesh_prov_buf_init(&prov_buf, PROV_RANDOM);
net_buf_simple_add_mem(&prov_buf, link->rand, PROV_RAND_SIZE(link));
if (bt_mesh_prov_send(link, &prov_buf)) {
BT_ERR("Failed to send Provisioning Random");
close_link(link, CLOSE_REASON_FAILED);
return;
}{...}
link->expect = PROV_RANDOM;
}{...}
static void send_prov_data(struct bt_mesh_prov_link *link)
{
uint16_t prev_addr = BLE_MESH_ADDR_UNASSIGNED;
uint16_t max_addr = BLE_MESH_ADDR_UNASSIGNED;
struct bt_mesh_node *node = NULL;
const uint8_t *netkey = NULL;
uint8_t session_key[16] = {0};
uint8_t nonce[13] = {0};
uint8_t pdu[25] = {0};
PROV_BUF(buf, 34);
int err = 0;
err = bt_mesh_session_key(link->dhkey, link->prov_salt, session_key);
if (err) {
BT_ERR("Failed to generate session key");
goto fail;
}{...}
BT_DBG("SessionKey: %s", bt_hex(session_key, 16));
err = bt_mesh_prov_nonce(link->dhkey, link->prov_salt, nonce);
if (err) {
BT_ERR("Failed to generate session nonce");
goto fail;
}{...}
BT_DBG("Nonce: %s", bt_hex(nonce, 13));
/* ... */
if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV) && FAST_PROV_ENABLE()) {
netkey = bt_mesh_fast_prov_net_key_get(prov_ctx.fast_prov.net_idx);
if (!netkey) {
BT_ERR("No NetKey for fast provisioning");
goto fail;
}{...}
memcpy(pdu, netkey, 16);
sys_put_be16(prov_ctx.fast_prov.net_idx, &pdu[16]);
pdu[18] = get_net_flags(prov_ctx.fast_prov.net_idx);
sys_put_be32(bt_mesh.iv_index, &pdu[19]);
}{...} else {
netkey = bt_mesh_provisioner_net_key_get(prov_ctx.net_idx);
if (!netkey) {
BT_ERR("No NetKey for provisioning data");
goto fail;
}{...}
memcpy(pdu, netkey, 16);
sys_put_be16(prov_ctx.net_idx, &pdu[16]);
pdu[18] = get_net_flags(prov_ctx.net_idx);
sys_put_be32(bt_mesh.iv_index, &pdu[19]);
}{...}
/* ... */
node = bt_mesh_provisioner_get_node_with_uuid(link->uuid);
if (node) {
if (link->element_num <= node->element_num &&
link->pb_remote_nppi != NPPI_NODE_ADDR_REFRESH) {
/* ... */
prev_addr = node->unicast_addr;
}{...}
/* ... */
if (!bt_mesh_atomic_test_bit(link->flags, PB_REMOTE) ||
link->pb_remote_nppi == NPPI_UNKNOWN) {
bt_mesh_provisioner_remove_node(link->uuid);
}{...}
}{...}
max_addr = FAST_PROV_ENABLE() ? prov_ctx.fast_prov.unicast_addr_max : UNICAST_ADDR_LIMIT;
if (BLE_MESH_ADDR_IS_UNICAST(prev_addr)) {
/* ... */
sys_put_be16(prev_addr, &pdu[23]);
link->unicast_addr = prev_addr;
}{...} else {
uint16_t alloc_addr = BLE_MESH_ADDR_UNASSIGNED;
if (BLE_MESH_ADDR_IS_UNICAST(link->assign_addr)) {
alloc_addr = link->assign_addr;
}{...} else {
if (prov_ctx.alloc_addr == BLE_MESH_ADDR_UNASSIGNED) {
BT_ERR("Not enough unicast address to be allocated");
goto fail;
}{...}
alloc_addr = prov_ctx.alloc_addr;
}{...}
if (alloc_addr + link->element_num - 1 > max_addr) {
BT_ERR("Not enough unicast address for the device");
goto fail;
}{...}
/* ... */
if (bt_mesh_provisioner_check_is_addr_dup(alloc_addr, link->element_num, true)) {
BT_ERR("Duplicate assigned address 0x%04x", alloc_addr);
goto fail;
}{...}
sys_put_be16(alloc_addr, &pdu[23]);
link->unicast_addr = alloc_addr;
}{...}
bt_mesh_prov_buf_init(&buf, PROV_DATA);
err = bt_mesh_prov_encrypt(session_key, nonce, pdu, net_buf_simple_add(&buf, 33));
if (err) {
BT_ERR("Failed to encrypt provisioning data");
goto fail;
}{...}
if (bt_mesh_prov_send(link, &buf)) {
BT_ERR("Failed to send Provisioning Data");
goto fail;
}{...}
/* ... */
if (!BLE_MESH_ADDR_IS_UNICAST(prev_addr)) {
if (BLE_MESH_ADDR_IS_UNICAST(link->assign_addr)) {
/* ... */
if (prov_ctx.alloc_addr < link->assign_addr + link->element_num) {
prov_ctx.alloc_addr = link->assign_addr + link->element_num;
}{...}
}{...} else {
prov_ctx.alloc_addr += link->element_num;
if (prov_ctx.alloc_addr > max_addr) {
prov_ctx.alloc_addr = BLE_MESH_ADDR_UNASSIGNED;
}{...}
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_prov_info(prov_ctx.primary_addr, prov_ctx.alloc_addr);
}{...}
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV) && FAST_PROV_ENABLE()) {
link->kri_flags = get_net_flags(prov_ctx.fast_prov.net_idx);
}{...} else {
link->kri_flags = get_net_flags(prov_ctx.net_idx);
}{...}
link->expect = PROV_COMPLETE;
return;
fail:
close_link(link, CLOSE_REASON_FAILED);
}{...}
static void prov_random(struct bt_mesh_prov_link *link,
struct net_buf_simple *buf)
{
uint8_t conf_verify[32] = {0};
uint8_t rand_size = 0;
uint8_t *data = buf->data;
rand_size = PROV_RAND_SIZE(link);
BT_DBG("Remote Random: %s", bt_hex(data, rand_size));
/* ... */
if (!memcmp(data, link->rand, rand_size)) {
BT_ERR("Random value is identical to ours, rejecting.");
goto fail;
}{...}
if (link->algorithm == PROV_ALG_P256_CMAC_AES128) {
if (bt_mesh_prov_conf(link->conf_key, data, link->auth, conf_verify)) {
BT_ERR("Failed to calculate confirmation verification");
goto fail;
}{...}
}{...}
#if CONFIG_BLE_MESH_PROV_EPA
else {
if (bt_mesh_prov_conf_epa(link->conf_key, data, conf_verify)) {
BT_ERR("Failed to calculate confirmation verification");
goto fail;
}{...}
}{...}
/* ... */#endif
if (memcmp(conf_verify, link->conf, PROV_CONF_SIZE(link))) {
BT_ERR("Invalid confirmation value");
BT_ERR("Received: %s", bt_hex(link->conf, PROV_CONF_SIZE(link)));
BT_ERR("Calculated: %s", bt_hex(conf_verify, PROV_CONF_SIZE(link)));
goto fail;
}{...}
/* ... */
if (link->algorithm == PROV_ALG_P256_CMAC_AES128) {
if (bt_mesh_prov_salt(link->conf_salt, link->rand, data,
link->prov_salt)) {
BT_ERR("Failed to generate ProvisioningSalt");
goto fail;
}{...}
}{...}
#if CONFIG_BLE_MESH_PROV_EPA
else {
if (bt_mesh_prov_salt_epa(link->conf_salt, link->rand, data,
link->prov_salt)) {
BT_ERR("Failed to generate ProvisioningSalt");
goto fail;
}{...}
}{...}
/* ... */#endif
BT_DBG("ProvisioningSalt: %s", bt_hex(link->prov_salt, PROV_CONF_SALT_SIZE(link)));
send_prov_data(link);
return;
fail:
close_link(link, CLOSE_REASON_FAILED);
}{...}
static void prov_complete(struct bt_mesh_prov_link *link,
struct net_buf_simple *buf)
{
uint8_t device_key[16] = {0};
uint16_t net_idx = 0U;
uint16_t index = 0U;
bool nppi = false;
int err = 0;
int i;
err = bt_mesh_dev_key(link->dhkey, link->prov_salt, device_key);
if (err) {
BT_ERR("Failed to generate device key");
close_link(link, CLOSE_REASON_FAILED);
return;
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV) && FAST_PROV_ENABLE()) {
net_idx = prov_ctx.fast_prov.net_idx;
}{...} else {
net_idx = prov_ctx.net_idx;
}{...}
if (bt_mesh_atomic_test_bit(link->flags, PB_REMOTE) &&
link->pb_remote_nppi != NPPI_UNKNOWN) {
nppi = true;
}{...}
err = bt_mesh_provisioner_provision(&link->addr, link->uuid, link->oob_info,
link->unicast_addr, link->element_num,
net_idx, link->kri_flags, bt_mesh.iv_index,
device_key, &index, nppi);
if (err) {
BT_ERR("Failed to store node info");
close_link(link, CLOSE_REASON_FAILED);
return;
}{...}
#if CONFIG_BLE_MESH_RPR_CLI
if (bt_mesh_atomic_test_bit(link->flags, PB_REMOTE)) {
/* ... */
if (link->pb_remote_nppi == NPPI_NODE_ADDR_REFRESH) {
bt_mesh_rpr_cli_node_addr_update(link);
}{...}
/* ... */
bt_mesh_rpr_cli_prov_complete(link, index, net_idx);
close_link(link, CLOSE_REASON_SUCCESS);
return;
}{...}
/* ... */#endif
if (bt_mesh_prov_get()->prov_complete) {
bt_mesh_prov_get()->prov_complete(index, link->uuid, link->unicast_addr,
link->element_num, net_idx);
}{...}
for (i = 0; i < ARRAY_SIZE(unprov_dev); i++) {
if (!memcmp(unprov_dev[i].uuid, link->uuid, 16) &&
(unprov_dev[i].flags & RM_AFTER_PROV)) {
memset(&unprov_dev[i], 0, sizeof(struct unprov_dev_queue));
break;
}{...}
}{...}
close_link(link, CLOSE_REASON_SUCCESS);
}{...}
static void prov_failed(struct bt_mesh_prov_link *link,
struct net_buf_simple *buf)
{
BT_WARN("Error 0x%02x", buf->data[0]);
close_link(link, CLOSE_REASON_FAILED);
}{...}
static const struct {
void (*func)(struct bt_mesh_prov_link *link, struct net_buf_simple *buf);
}{...} prov_handlers[] = {
{ prov_invite, },
{ prov_capabilities, },
{ prov_start, },
{ prov_pub_key, },
{ prov_input_complete, },
{ prov_confirm, },
{ prov_random, },
{ prov_data, },
{ prov_complete, },
{ prov_failed, },
#if CONFIG_BLE_MESH_CERT_BASED_PROV
{ bt_mesh_pvnr_record_req, },
{ bt_mesh_pvnr_record_rsp, },
{ bt_mesh_pvnr_records_get, },
{ bt_mesh_pvnr_records_list, },/* ... */
#endif
}{...};
static void close_link(struct bt_mesh_prov_link *link, uint8_t reason)
{
if (bt_mesh_atomic_test_bit(link->flags, PB_REMOTE)) {
if (link->pb_remote_close) {
link->pb_remote_close(link, reason);
}{...}
return;
}{...}
#if CONFIG_BLE_MESH_PB_GATT
if (link->conn) {
bt_mesh_gattc_disconnect(link->conn);
return;
}{...}
/* ... */#endif
#if CONFIG_BLE_MESH_PB_ADV
bt_mesh_prov_bearer_ctl_send(link, LINK_CLOSE, &reason, sizeof(reason));
#endif
}{...}
#if CONFIG_BLE_MESH_PB_ADV
static void link_ack(struct bt_mesh_prov_link *link, struct prov_rx *rx, struct net_buf_simple *buf)
{
BT_DBG("len %u", buf->len);
if (buf->len) {
BT_ERR("Invalid Link ACK length %d", buf->len);
close_link(link, CLOSE_REASON_FAILED);
return;
}{...}
if (link->expect == PROV_CAPABILITIES ||
link->expect == PROV_REC_LIST || link->expect == PROV_REC_RSP) {
BT_INFO("Link ACK is already received");
return;
}{...}
#if CONFIG_BLE_MESH_CERT_BASED_PROV
if (PROV_REC_SUPPORT(link->oob_info)) {
if (bt_mesh_prov_get()->cert_based_prov_start) {
bt_mesh_prov_get()->cert_based_prov_start(link - prov_links);
}{...}
}{...} else
#endif
{
send_invite(link);
}{...}
}{...}
static void link_close(struct bt_mesh_prov_link *link, struct prov_rx *rx, struct net_buf_simple *buf)
{
BT_DBG("len %u", buf->len);
if (buf->len != 1) {
BT_ERR("Invalid Link Close length %d", buf->len);
return;
}{...}
reset_adv_link(link, net_buf_simple_pull_u8(buf));
}{...}
static void gen_prov_ctl(struct bt_mesh_prov_link *link, struct prov_rx *rx, struct net_buf_simple *buf)
{
BT_DBG("op 0x%02x len %u", BEARER_CTL(rx->gpc), buf->len);
switch (BEARER_CTL(rx->gpc)) {
case LINK_OPEN:
break;
...
case LINK_ACK:
if (!bt_mesh_atomic_test_bit(link->flags, LINK_ACTIVE)) {
return;
}{...}
link_ack(link, rx, buf);
break;
...
case LINK_CLOSE:
if (!bt_mesh_atomic_test_bit(link->flags, LINK_ACTIVE)) {
return;
}{...}
link_close(link, rx, buf);
break;
...
default:
BT_ERR("Unknown bearer opcode 0x%02x", BEARER_CTL(rx->gpc));
return;...
}{...}
}{...}
static void prov_msg_recv(struct bt_mesh_prov_link *link)
{
uint8_t type = 0;
/* ... */
if (bt_mesh_atomic_test_bit(link->flags, LINK_CLOSING)) {
BT_WARN("Link is closing, unexpected msg 0x%02x", type);
return;
}{...}
if (!bt_mesh_fcs_check(link->rx.buf, link->rx.fcs)) {
BT_ERR("Incorrect FCS");
return;
}{...}
type = net_buf_simple_pull_u8(link->rx.buf);
BT_DBG("type 0x%02x len %u", type, link->rx.buf->len);
if (type >= ARRAY_SIZE(prov_handlers)) {
BT_ERR("Unknown provisioning PDU type 0x%02x", type);
goto fail;
}{...}
bt_mesh_gen_prov_ack_send(link, link->rx.id);
link->rx.prev_id = link->rx.id;
link->rx.id = 0;
/* ... */
if (type != PROV_FAILED && type != link->expect) {
BT_ERR("Unexpected msg 0x%02x != 0x%02x", type, link->expect);
goto fail;
}{...}
if (!bt_mesh_prov_pdu_check(type, link->rx.buf->len, NULL)) {
goto fail;
}{...}
k_delayed_work_submit(&link->prot_timer, PROTOCOL_TIMEOUT);
prov_handlers[type].func(link, link->rx.buf);
net_buf_simple_reset(link->rx.buf);
return;
fail:
/* ... */
bt_mesh_gen_prov_ack_send(link, link->rx.id);
close_link(link, CLOSE_REASON_FAILED);
}{...}
static void gen_prov_cont(struct bt_mesh_prov_link *link,
struct prov_rx *rx,
struct net_buf_simple *buf)
{
bool close = false;
if (!bt_mesh_gen_prov_cont(link, buf, rx, &close)) {
if (close) {
close_link(link, CLOSE_REASON_FAILED);
}{...}
return;
}{...}
prov_msg_recv(link);
}{...}
static void gen_prov_ack(struct bt_mesh_prov_link *link,
struct prov_rx *rx,
struct net_buf_simple *buf)
{
BT_DBG("len %u", buf->len);
if (!link->tx.buf[0]) {
return;
}{...}
if (!link->tx.id) {
return;
}{...}
if (rx->xact_id == (link->tx.id - 1)) {
bt_mesh_prov_clear_tx(link, true);
switch (link->expect_ack_for) {
case PROV_START:
send_pub_key(link);
uint8_t pub_key_oob = link->conf_inputs[13];
/* ... */
if (pub_key_oob) {
return;
}{...}
break;...
case PROV_PUB_KEY:
prov_gen_dh_key(link);
break;...
default:
break;...
}{...}
link->expect_ack_for = 0x00;
}{...}
}{...}
static void gen_prov_start(struct bt_mesh_prov_link *link,
struct prov_rx *rx,
struct net_buf_simple *buf)
{
bool close = false;
if (!bt_mesh_gen_prov_start(link, buf, rx, &close)) {
if (close) {
close_link(link, CLOSE_REASON_FAILED);
}{...}
return;
}{...}
prov_msg_recv(link);
}{...}
static const struct {
void (*const func)(struct bt_mesh_prov_link *link,
struct prov_rx *rx,
struct net_buf_simple *buf);
const uint8_t require_link;
const uint8_t min_len;
}{...} gen_prov[] = {
{ gen_prov_start, true, 3 },
{ gen_prov_ack, true, 0 },
{ gen_prov_cont, true, 0 },
{ gen_prov_ctl, true, 0 },
}{...};
static void gen_prov_recv(struct bt_mesh_prov_link *link,
struct prov_rx *rx,
struct net_buf_simple *buf)
{
if (buf->len < gen_prov[GPCF(rx->gpc)].min_len) {
BT_ERR("Too short GPC message type %u", GPCF(rx->gpc));
return;
}{...}
if (!bt_mesh_atomic_test_bit(link->flags, LINK_ACTIVE) &&
gen_prov[GPCF(rx->gpc)].require_link) {
BT_DBG("Ignoring message that requires active link");
return;
}{...}
gen_prov[GPCF(rx->gpc)].func(link, rx, buf);
}{...}
static struct bt_mesh_prov_link *find_pba_link(uint32_t link_id)
{
int i;
for (i = 0; i < CONFIG_BLE_MESH_PBA_SAME_TIME; i++) {
if (bt_mesh_atomic_test_bit(prov_links[i].flags, LINK_ACTIVE) &&
prov_links[i].link_id == link_id) {
return &prov_links[i];
}{...}
}{...}
return NULL;
}{...}
void bt_mesh_provisioner_pb_adv_recv(struct net_buf_simple *buf)
{
struct bt_mesh_prov_link *link = NULL;
struct prov_rx rx = {0};
if (buf->len < 6) {
BT_ERR("Too short provisioning packet (len %u)", buf->len);
return;
}{...}
rx.link_id = net_buf_simple_pull_be32(buf);
rx.xact_id = net_buf_simple_pull_u8(buf);
rx.gpc = net_buf_simple_pull_u8(buf);
BT_DBG("link_id 0x%08x xact_id %u", rx.link_id, rx.xact_id);
link = find_pba_link(rx.link_id);
if (link == NULL) {
BT_DBG("Ignoring mesh beacon for unknown link");
return;
}{...}
gen_prov_recv(link, &rx, buf);
}{...}
/* ... */#endif
#if CONFIG_BLE_MESH_PB_GATT
static struct bt_mesh_prov_link *find_pbg_link(struct bt_mesh_conn *conn)
{
int i;
for (i = CONFIG_BLE_MESH_PBA_SAME_TIME; i < BLE_MESH_PROV_SAME_TIME; i++) {
if (bt_mesh_atomic_test_bit(prov_links[i].flags, LINK_ACTIVE) &&
prov_links[i].conn == conn) {
return &prov_links[i];
}{...}
}{...}
return NULL;
}{...}
int bt_mesh_provisioner_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_simple *buf)
{
struct bt_mesh_prov_link *link = NULL;
uint8_t type = 0U;
BT_DBG("%u bytes: %s", buf->len, bt_hex(buf->data, buf->len));
link = find_pbg_link(conn);
if (link == NULL) {
BT_ERR("Link not found, conn %p", conn);
return -ENOTCONN;
}{...}
if (buf->len < 1) {
BT_ERR("Too short provisioning packet (len %u)", buf->len);
return -EINVAL;
}{...}
type = net_buf_simple_pull_u8(buf);
if (type >= ARRAY_SIZE(prov_handlers)) {
BT_ERR("Unknown provisioning PDU type 0x%02x", type);
close_link(link, CLOSE_REASON_FAILED);
return -EINVAL;
}{...}
if (type != PROV_FAILED && type != link->expect) {
BT_ERR("Unexpected msg 0x%02x != 0x%02x", type, link->expect);
close_link(link, CLOSE_REASON_FAILED);
return -EINVAL;
}{...}
if (!bt_mesh_prov_pdu_check(type, buf->len, NULL)) {
close_link(link, CLOSE_REASON_FAILED);
return -EINVAL;
}{...}
k_delayed_work_submit(&link->prot_timer, PROTOCOL_TIMEOUT);
prov_handlers[type].func(link, buf);
return 0;
}{...}
int bt_mesh_provisioner_pb_gatt_open(struct bt_mesh_conn *conn, const uint8_t addr[6])
{
struct bt_mesh_prov_link *link = NULL;
int i;
if (conn == NULL || addr == NULL) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}{...}
for (i = CONFIG_BLE_MESH_PBA_SAME_TIME; i < BLE_MESH_PROV_SAME_TIME; i++) {
if (!memcmp(prov_links[i].addr.val, addr, BLE_MESH_ADDR_LEN)) {
prov_links[i].conn = bt_mesh_conn_ref(conn);
link = &prov_links[i];
break;
}{...}
}{...}
if (link == NULL) {
BT_ERR("Device address %s not found", bt_hex(addr, BLE_MESH_ADDR_LEN));
return -ENODEV;
}{...}
#if CONFIG_BLE_MESH_PB_ADV
for (i = 0; i < CONFIG_BLE_MESH_PBA_SAME_TIME; i++) {
if (bt_mesh_atomic_test_bit(prov_links[i].flags, LINK_ACTIVE)) {
if (!memcmp(prov_links[i].uuid, link->uuid, 16)) {
BT_WARN("Provision using PB-GATT & PB-ADV same time");
close_link(link, CLOSE_REASON_FAILED);
return -EALREADY;
}{...}
}{...}
}{...}
/* ... */#endif
bt_mesh_atomic_set_bit(link->flags, LINK_ACTIVE);
if (bt_mesh_prov_get()->prov_link_open) {
bt_mesh_prov_get()->prov_link_open(BLE_MESH_PROV_GATT);
}{...}
#if CONFIG_BLE_MESH_CERT_BASED_PROV
if (PROV_REC_SUPPORT(link->oob_info)) {
if (bt_mesh_prov_get()->cert_based_prov_start) {
bt_mesh_prov_get()->cert_based_prov_start(link - prov_links);
}{...}
}{...} else
#endif
{
send_invite(link);
}{...}
return 0;
}{...}
int bt_mesh_provisioner_pb_gatt_close(struct bt_mesh_conn *conn, uint8_t reason)
{
struct bt_mesh_prov_link *link = NULL;
BT_DBG("conn %p", conn);
link = find_pbg_link(conn);
if (link == NULL) {
BT_ERR("Not connected, conn %p", conn);
return -ENOTCONN;
}{...}
k_delayed_work_cancel(&link->prot_timer);
if (bt_mesh_prov_get()->prov_link_close) {
bt_mesh_prov_get()->prov_link_close(BLE_MESH_PROV_GATT, reason);
}{...}
prov_memory_free(link);
memset(link, 0, offsetof(struct bt_mesh_prov_link, prot_timer));
link->pb_gatt_send = prov_send_gatt;
return 0;
}{...}
/* ... */#endif
static void protocol_timeout(struct k_work *work)
{
struct bt_mesh_prov_link *link = work->user_data;
BT_WARN("Protocol timeout");
close_link(link, CLOSE_REASON_TIMEOUT);
}{...}
int bt_mesh_provisioner_prov_init(void)
{
const uint8_t *key = NULL;
int i;
if (bt_mesh_prov_get() == NULL) {
BT_ERR("No provisioning context provided");
return -EINVAL;
}{...}
key = bt_mesh_pub_key_get();
if (!key) {
BT_ERR("Failed to generate Public Key");
return -EIO;
}{...}
prov_ctx.primary_addr = BLE_MESH_ADDR_UNASSIGNED;
if (bt_mesh_prov_get()->prov_static_oob_val &&
bt_mesh_prov_get()->prov_static_oob_len) {
prov_ctx.static_oob_len = MIN(BLE_MESH_PROV_STATIC_OOB_MAX_LEN, bt_mesh_prov_get()->prov_static_oob_len);
memcpy(prov_ctx.static_oob_val,
bt_mesh_prov_get()->prov_static_oob_val,
prov_ctx.static_oob_len);
}{...}
#if CONFIG_BLE_MESH_PB_ADV
for (i = 0; i < CONFIG_BLE_MESH_PBA_SAME_TIME; i++) {
rx_buf[i].buf.size = PROV_RX_BUF_SIZE;
rx_buf[i].buf.__buf = rx_buf_data + (i * PROV_RX_BUF_SIZE);
prov_links[i].pending_ack = PROV_XACT_NVAL;
bt_mesh_prov_retransmit_init(&prov_links[i]);
prov_links[i].rx.prev_id = PROV_XACT_NVAL;
prov_links[i].rx.buf = get_rx_buf(i);
prov_links[i].next_xact_id = pvnr_next_xact_id;
prov_links[i].reset_adv_link = reset_adv_link;
prov_links[i].retrans_timeout = close_link;
#if CONFIG_BLE_MESH_FAST_PROV
prov_links[i].last_tx_pdu = PROV_DATA;
#endif
bt_mesh_mutex_create(&prov_links[i].buf_lock);
}{...}
/* ... */#endif
#if CONFIG_BLE_MESH_PB_GATT
for (i = CONFIG_BLE_MESH_PBA_SAME_TIME; i < BLE_MESH_PROV_SAME_TIME; i++) {
prov_links[i].pb_gatt_send = prov_send_gatt;
}{...}
/* ... */#endif
for (i = 0; i < BLE_MESH_PROV_SAME_TIME; i++) {
k_delayed_work_init(&prov_links[i].prot_timer, protocol_timeout);
prov_links[i].prot_timer.work.user_data = &prov_links[i];
}{...}
#if CONFIG_BLE_MESH_PB_ADV
bt_mesh_mutex_create(&prov_ctx.pb_adv_lock);
#endif
#if CONFIG_BLE_MESH_PB_GATT
bt_mesh_mutex_create(&prov_ctx.pb_gatt_lock);
#endif
return 0;
}{...}
int bt_mesh_provisioner_prov_reset(bool erase)
{
int i;
if (bt_mesh_prov_get() == NULL) {
BT_ERR("No provisioning context provided");
return -EINVAL;
}{...}
for (i = 0; i < BLE_MESH_PROV_SAME_TIME; i++) {
k_delayed_work_cancel(&prov_links[i].prot_timer);
prov_memory_free(&prov_links[i]);
if (i < CONFIG_BLE_MESH_PBA_SAME_TIME) {
#if CONFIG_BLE_MESH_PB_ADV
bt_mesh_prov_clear_tx(&prov_links[i], true);
#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN
bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_REMOVE,
BLE_MESH_EXCEP_LIST_TYPE_MESH_LINK_ID,
&prov_links[i].link_id);/* ... */
#endif
memset(&prov_links[i], 0, offsetof(struct bt_mesh_prov_link, tx.retransmit));
prov_links[i].pending_ack = PROV_XACT_NVAL;
prov_links[i].rx.prev_id = PROV_XACT_NVAL;
prov_links[i].rx.buf = get_rx_buf(i);/* ... */
#endif
}{...} else {
memset(&prov_links[i], 0, offsetof(struct bt_mesh_prov_link, prot_timer));
}{...}
#if CONFIG_BLE_MESH_CERT_BASED_PROV
for (size_t j = 0; j < BLE_MESH_REC_MAX_ID; j++) {
if (prov_links[i].records[j]) {
bt_mesh_free(prov_links[i].records[j]);
prov_links[i].records[j] = NULL;
}{...}
}{...}
/* ... */#endif
}{...}
/* ... */
memset(&prov_ctx, 0, offsetof(struct bt_mesh_prov_ctx, static_oob_len));
prov_ctx.match_offset = 0;
prov_ctx.match_length = 0;
prov_ctx.prov_after_match = false;
memset(prov_ctx.match_value, 0, sizeof(prov_ctx.match_value));
memset(&prov_ctx.fast_prov, 0, sizeof(prov_ctx.fast_prov));
memset(unprov_dev, 0, sizeof(unprov_dev));
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && erase) {
bt_mesh_clear_prov_info();
}{...}
return 0;
}{...}
#if CONFIG_BLE_MESH_DEINIT
int bt_mesh_provisioner_prov_deinit(bool erase)
{
int i;
if (bt_mesh_prov_get() == NULL) {
BT_ERR("No provisioning context provided");
return -EINVAL;
}{...}
bt_mesh_provisioner_prov_reset(erase);
for (i = 0; i < BLE_MESH_PROV_SAME_TIME; i++) {
#if CONFIG_BLE_MESH_PB_ADV
if (i < CONFIG_BLE_MESH_PBA_SAME_TIME) {
k_delayed_work_free(&prov_links[i].tx.retransmit);
bt_mesh_mutex_free(&prov_links[i].buf_lock);
}{...}
/* ... */#endif
k_delayed_work_free(&prov_links[i].prot_timer);
memset(&prov_links[i], 0, sizeof(prov_links[i]));
}{...}
#if CONFIG_BLE_MESH_PB_ADV
bt_mesh_mutex_free(&prov_ctx.pb_adv_lock);
#endif
#if CONFIG_BLE_MESH_PB_GATT
bt_mesh_mutex_free(&prov_ctx.pb_gatt_lock);
#endif
prov_ctx.static_oob_len = 0U;
memset(prov_ctx.static_oob_val, 0, sizeof(prov_ctx.static_oob_val));
#if CONFIG_BLE_MESH_PB_ADV
memset(rx_buf, 0, sizeof(rx_buf));
memset(rx_buf_data, 0, sizeof(rx_buf_data));/* ... */
#endif
return 0;
}{...}
/* ... */#endif
static bool bt_mesh_prov_svc_adv_filter(void)
{
static uint32_t timestamp = 0;
static uint32_t pre_timestamp = 0;
timestamp = k_uptime_get_32();
if (PROV_SVC_ADV_RX_CHECK(pre_timestamp, timestamp)) {
pre_timestamp = timestamp;
return false;
}{...}
return true;
}{...}
static bool notify_unprov_dev_info(bt_mesh_prov_bearer_t bearer, const uint8_t uuid[16],
const bt_mesh_addr_t *addr, uint16_t oob_info, int8_t rssi)
{
int i;
if (prov_ctx.prov_after_match == false) {
uint8_t adv_type = (bearer == BLE_MESH_PROV_ADV) ?
BLE_MESH_ADV_NONCONN_IND : BLE_MESH_ADV_IND;
for (i = 0; i < ARRAY_SIZE(unprov_dev); i++) {
if (!memcmp(unprov_dev[i].uuid, uuid, 16)) {
break;
}{...}
}{...}
if (i == ARRAY_SIZE(unprov_dev)) {
BT_DBG("Device not in queue, notify to app layer");
if (adv_type == BLE_MESH_ADV_IND && bt_mesh_prov_svc_adv_filter()) {
return true;
}{...}
if (notify_unprov_adv_pkt_cb) {
notify_unprov_adv_pkt_cb(addr->val, addr->type, adv_type,
uuid, oob_info, bearer, rssi);
}{...}
return true;
}{...}
if (!(unprov_dev[i].bearer & bearer)) {
BT_WARN("Device in queue not support PB-%s",
(bearer == BLE_MESH_PROV_ADV) ? "ADV" : "GATT");
if (notify_unprov_adv_pkt_cb) {
notify_unprov_adv_pkt_cb(addr->val, addr->type, adv_type,
uuid, oob_info, bearer, rssi);
}{...}
return true;
}{...}
}{...}
return false;
}{...}
#if CONFIG_BLE_MESH_PB_ADV
void bt_mesh_provisioner_unprov_beacon_recv(struct net_buf_simple *buf, int8_t rssi)
{
const bt_mesh_addr_t *addr = NULL;
const uint8_t *uuid = NULL;
uint16_t oob_info = 0U;
if (!(prov_ctx.bearers & BLE_MESH_PROV_ADV)) {
BT_INFO("Not support PB-ADV bearer");
return;
}{...}
if (buf->len != 0x12 && buf->len != 0x16) {
BT_ERR("Invalid Unprovisioned Device Beacon length %d", buf->len);
return;
}{...}
addr = bt_mesh_get_unprov_dev_addr();
uuid = buf->data;
net_buf_simple_pull(buf, 16);
oob_info = net_buf_simple_pull_be16(buf);
if (provisioner_check_unprov_dev_info(uuid, BLE_MESH_PROV_ADV)) {
return;
}{...}
if (notify_unprov_dev_info(BLE_MESH_PROV_ADV, uuid, addr, oob_info, rssi)) {
return;
}{...}
provisioner_start_prov_pb_adv(uuid, addr, oob_info, BLE_MESH_ADDR_UNASSIGNED);
}{...}
/* ... */#endif
#if CONFIG_BLE_MESH_PB_GATT
void bt_mesh_provisioner_prov_adv_recv(struct net_buf_simple *buf,
const bt_mesh_addr_t *addr, int8_t rssi)
{
const uint8_t *uuid = NULL;
uint16_t oob_info = 0U;
if (!(prov_ctx.bearers & BLE_MESH_PROV_GATT)) {
BT_INFO("Not support PB-GATT bearer");
return;
}{...}
if (bt_mesh_gattc_get_free_conn_count() == 0) {
BT_INFO("BLE connections for mesh reach max limit");
return;
}{...}
uuid = buf->data;
net_buf_simple_pull(buf, 16);
oob_info = net_buf_simple_pull_le16(buf);
if (provisioner_check_unprov_dev_info(uuid, BLE_MESH_PROV_GATT)) {
return;
}{...}
if (notify_unprov_dev_info(BLE_MESH_PROV_GATT, uuid, addr, oob_info, rssi)) {
return;
}{...}
/* ... */
provisioner_start_prov_pb_gatt(uuid, addr, oob_info, BLE_MESH_ADDR_UNASSIGNED);
}{...}
/* ... */#endif
/* ... */
int bt_mesh_rpr_cli_pdu_recv(struct bt_mesh_prov_link *link, uint8_t type,
struct net_buf_simple *buf)
{
if (type != link->expect) {
BT_ERR("PB-Remote, unexpected msg 0x%02x != 0x%02x", type, link->expect);
return -EINVAL;
}{...}
prov_handlers[type].func(link, buf);
return 0;
}{...}
int bt_mesh_rpr_cli_pdu_send(struct bt_mesh_prov_link *link, uint8_t type)
{
switch (type) {
case PROV_INVITE:
send_invite(link);
break;...
case PROV_PUB_KEY:
send_pub_key(link);
break;...
case PROV_CONFIRM:
send_confirm(link);
break;...
default:
break;...
}{...}
return 0;
}{...}
/* ... */
/* ... */
int bt_mesh_rpr_cli_recv_pub_key_outbound_report(struct bt_mesh_prov_link *link)
{
bt_mesh_atomic_test_and_clear_bit(link->flags, WAIT_PK_OBR);
if (bt_mesh_atomic_test_and_clear_bit(link->flags, SEND_CONFIRM)) {
send_confirm(link);
}{...}
return 0;
}{...}
/* ... */
#endif