1
2
3
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
63
64
65
66
67
71
72
82
83
84
85
86
87
88
89
90
91
94
95
98
99
102
103
106
107
108
109
110
111
112
113
114
115
116
120
121
124
125
126
127
128
129
133
134
139
140
141
142
143
144
145
146
147
148
149
154
155
156
161
162
163
164
168
169
170
171
172
173
174
175
178
179
180
181
182
183
184
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
239
240
241
242
246
247
248
249
250
257
262
263
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
306
307
308
309
310
311
312
313
314
315
318
319
322
323
326
327
328
331
332
333
334
335
336
337
338
339
340
343
344
347
348
349
350
351
354
355
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
377
378
379
382
383
384
385
386
387
388
389
390
391
392
395
396
402
403
414
415
416
417
418
419
420
421
422
423
424
427
428
429
432
433
434
435
438
439
443
444
445
446
447
448
449
450
451
452
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
477
478
479
480
483
484
487
488
489
490
495
498
499
506
507
508
509
510
511
514
515
518
519
520
521
522
523
526
527
528
529
530
537
538
539
540
541
542
543
544
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
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
610
611
612
613
614
615
616
617
618
619
620
621
625
626
627
633
634
635
636
637
638
639
640
641
642
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
667
668
669
676
677
681
682
683
684
685
686
687
688
689
690
691
692
695
696
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
722
723
724
725
726
727
732
733
734
735
736
739
740
741
742
743
744
747
748
752
753
754
755
756
757
760
761
762
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
783
784
785
786
787
788
789
790
791
792
793
794
795
796
816
817
818
819
820
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
841
842
843
844
845
846
847
848
849
850
851
852
853
854
868
869
870
871
872
875
876
877
878
879
880
881
882
883
884
885
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
915
916
917
920
921
925
926
927
928
931
932
933
934
935
942
943
944
949
950
951
954
955
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
1005
1006
1007
1008
1009
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1047
1048
1049
1052
1053
1054
1055
1056
1059
1060
1061
1062
1063
1064
1065
1066
1067
1070
1074
1075
1076
1077
1078
1079
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1116
1117
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1137
1138
1139
1140
1141
1142
1143
1149
1150
1151
1155
1156
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1185
1186
1187
1188
1189
1190
1191
1192
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1216
1217
1218
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1254
1255
1256
1257
1258
1259
1262
1263
1264
1265
1266
1267
1268
1269
1270
1273
1274
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1303
1304
1307
1308
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1331
1332
1333
1334
1335
1336
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1359
1360
1361
1362
1363
1364
1365
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1392
1393
1394
1395
1396
1397
1398
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1429
1430
1431
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1472
1473
1474
1475
1476
1477
1478
1479
1485
1486
1487
1491
1492
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1527
1528
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1550
1551
1552
1553
1554
1555
1556
1557
1558
1564
1565
1566
1570
1571
1575
1576
1582
1583
1584
1585
1586
1587
1593
1594
1597
1598
1599
1602
1603
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1644
1645
1646
1647
1648
1649
1650
1651
1652
1658
1659
1660
1664
1665
1669
1670
1673
1674
1675
1678
1679
1680
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1719
1720
1721
1722
1723
1724
1725
1726
1727
1733
1734
1735
1739
1740
1744
1745
1748
1749
1750
1751
1752
1753
1754
1755
1758
1759
1762
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1788
1789
1790
1791
1792
1793
1794
1800
1801
1802
1806
1807
1810
1811
1812
1813
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1840
1841
1842
1843
1844
1845
1846
1847
1848
1854
1855
1856
1862
1863
1864
1865
1866
1867
1868
1873
1874
1875
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
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
1921
1922
1923
1924
1925
1926
1927
1928
1933
1934
1935
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1980
1981
1982
1985
1986
1991
1992
1993
1994
1995
1996
2002
2003
2006
2009
2010
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2062
2063
2064
2067
2068
2071
2072
2073
2074
2075
2076
2079
2080
2081
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2107
2108
2109
2110
2111
2112
2113
2114
2115
2121
2122
2123
2127
2128
2131
2132
2133
2134
2135
2136
2137
2138
2139
2142
2143
2146
2147
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2172
2173
2174
2175
2176
2177
2178
2184
2185
2186
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2213
2214
2215
2216
2217
2218
2219
2224
2225
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2252
2253
2254
2255
2256
2257
2258
2263
2264
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2306
2307
2308
2309
2310
2311
2312
2313
2319
2320
2325
2326
2327
2328
2329
2330
2331
2336
2337
2338
2339
2340
2341
2342
2346
2347
2348
2349
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2371
2372
2373
2374
2377
2378
2379
2380
2381
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2406
2407
2408
2409
2410
2414
2415
2421
2438
2439
2440
2441
2444
2445
2449
2450
2451
2452
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2491
2492
2493
2494
2495
2502
2503
2506
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2577
2578
2582
2583
2584
2585
2586
2587
2590
2591
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2614
2615
2616
2617
2618
2625
2626
2627
2628
2629
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2652
2653
2654
2658
2659
2660
2661
2662
2667
2682
2683
2684
2685
2686
2687
2688
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2727
2728
2729
2730
2731
2732
2738
2739
2740
2744
2745
2746
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2791
2792
2793
2794
2795
2796
2802
2803
2804
2808
2809
2810
2811
2812
2813
2814
2815
2816
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2853
2854
2855
2856
2857
2858
2859
2865
2866
2867
2871
2872
2873
2874
2875
2880
2881
2882
2883
2884
2889
2890
2899
2900
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2921
2924
2925
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2969
2970
2974
2975
2976
2977
2980
2981
2982
2983
2984
2987
2988
2989
2990
2991
2992
2996
2997
2998
2999
3000
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3026
3027
3028
3029
3030
3034
3035
3036
3040
3041
3042
3043
3044
3045
3046
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3079
3080
3081
3082
3083
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3105
3106
3107
3108
3109
3113
3114
3115
3116
3117
3118
3122
3123
3124
3125
3126
3127
3128
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3169
3170
3183
3184
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3236
3237
3238
3239
3240
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3264
3265
3269
3270
3274
3275
3279
3280
3281
3282
3283
3287
3288
3292
3293
3294
3295
3296
3297
3298
3301
3302
3303
3304
3305
3306
3307
3308
3309
3319
3320
3321
3324
3325
3326
3327
3331
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3375
3376
3377
3378
3379
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3409
3410
3411
3412
3416
3417
3421
3422
3423
3424
3425
3429
3430
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3457
3458
3459
3460
3463
3464
3465
3468
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3541
3542
3545
3546
3547
3550
3551
3552
3553
3556
3557
3558
3559
3560
3563
3564
3567
3568
3571
3572
3576
3577
3581
3582
3586
3587
3588
3589
3590
3591
3592
3593
3594
3598
3599
3603
3604
3608
3609
3610
3611
3612
3615
3616
3619
3620
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3644
3645
3649
3650
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3670
3671
3672
3673
3674
3675
3676
3677
3682
3683
3684
3685
3688
3689
3690
3694
3695
3696
3697
3698
3699
3700
3703
3704
3705
3706
3707
3708
3709
3710
3713
3720
3721
3722
3723
3724
3725
3726
3734
3735
3745
3746
3754
3755
3764
3765
3773
3774
3782
3783
3791
3792
3800
3801
3802
3803
3804
3805
3806
3807
3814
3815
3816
3817
3818
3819
3820
3828
3829
3835
3836
3840
3841
3842
3843
3844
3845
3846
3847
3854
3855
3856
3863
3864
3867
3868
3871
3872
3873
3874
/* ... */
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include "btc_ble_mesh_config_model.h"
#include "mesh.h"
#include "adv.h"
#include "lpn.h"
#include "transport.h"
#include "crypto.h"
#include "net.h"
#include "access.h"
#include "beacon.h"
#include "foundation.h"
#include "friend.h"
#include "settings.h"
#include "mesh/cfg_srv.h"
#include "proxy_server.h"
#include "mesh/main.h"
#include "mesh/common.h"
#include "heartbeat.h"20 includes
#if CONFIG_BLE_MESH_V11_SUPPORT
#include "mesh_v1.1/utils.h"
#endif
#define DEFAULT_TTL 7
static struct bt_mesh_cfg_srv *conf;
static struct label labels[CONFIG_BLE_MESH_LABEL_COUNT];
#if !CONFIG_BLE_MESH_V11_SUPPORT
const void *comp_0;
static uint8_t bt_mesh_comp_page_check(uint8_t page, bool largest)
{
/* ... */
ARG_UNUSED(largest);
if (page != 0) {
BT_WARN("Composition Data Page %d not exists", page);
}{...}
return 0;
}{...}
static inline uint16_t get_comp_elem_size(struct bt_mesh_elem *elem)
{
return (4 + elem->model_count * 2 + elem->vnd_model_count * 4);
}{...}
static uint16_t get_comp_data_size(const struct bt_mesh_comp *comp)
{
uint16_t size = 10;
for (int i = 0; i < comp->elem_count; i++) {
size += get_comp_elem_size(&(comp->elem[i]));
}{...}
return size;
}{...}
static void get_comp_data(struct net_buf_simple *buf,
const struct bt_mesh_comp *comp,
bool full_element)
{
struct bt_mesh_model *model = NULL;
struct bt_mesh_elem *elem = NULL;
uint16_t feat = 0;
if (IS_ENABLED(CONFIG_BLE_MESH_RELAY)) {
feat |= BLE_MESH_FEAT_RELAY;
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) {
feat |= BLE_MESH_FEAT_PROXY;
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
feat |= BLE_MESH_FEAT_FRIEND;
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) {
feat |= BLE_MESH_FEAT_LOW_POWER;
}{...}
net_buf_simple_add_le16(buf, comp->cid);
net_buf_simple_add_le16(buf, comp->pid);
net_buf_simple_add_le16(buf, comp->vid);
net_buf_simple_add_le16(buf, CONFIG_BLE_MESH_CRPL);
net_buf_simple_add_le16(buf, feat);
for (size_t i = 0; i < comp->elem_count; i++) {
elem = &(comp->elem[i]);
/* ... */
if (full_element &&
net_buf_simple_tailroom(buf) < get_comp_elem_size(elem)) {
return;
}{...}
net_buf_simple_add_le16(buf, elem->loc);
net_buf_simple_add_u8(buf, elem->model_count);
net_buf_simple_add_u8(buf, elem->vnd_model_count);
for (size_t j = 0; j < elem->model_count; j++) {
model = &(elem->models[j]);
net_buf_simple_add_le16(buf, model->id);
}{...}
for (size_t j = 0; j < elem->vnd_model_count; j++) {
model = &(elem->vnd_models[j]);
net_buf_simple_add_le16(buf, model->vnd.company);
net_buf_simple_add_le16(buf, model->vnd.id);
}{...}
}{...}
}{...}
static int fetch_comp_data(struct net_buf_simple *buf,
const struct bt_mesh_comp *comp,
uint8_t page, uint16_t offset,
bool full_element)
{
uint16_t size = get_comp_data_size(comp);
if (offset >= size) {
BT_WARN("Too large offset %d for comp data %d, size %d",
page, offset, size);
return 0;
}{...}
if (net_buf_simple_tailroom(buf) < 10 ||
size - offset > net_buf_simple_tailroom(buf)) {
BT_ERR("Too small buffer for comp data %d, %d, expected %d",
page, buf->size, size - offset);
return -EINVAL;
}{...}
if (offset) {
struct net_buf_simple *pdu = bt_mesh_alloc_buf(size);
if (pdu == NULL) {
BT_ERR("%s, Out of memory", __func__);
return -ENOMEM;
}{...}
get_comp_data(pdu, comp, false);
net_buf_simple_add_mem(buf, pdu->data + offset, pdu->len - offset);
bt_mesh_free_buf(pdu);
}{...} else {
get_comp_data(buf, comp, full_element);
}{...}
return 0;
}{...}
static int bt_mesh_get_comp_data(struct net_buf_simple *buf,
uint8_t page, uint16_t offset,
bool full_element)
{
if (page == 0) {
return fetch_comp_data(buf, comp_0, page, offset, full_element);
}{...}
BT_ERR("Invalid Composition Data Page %d", page);
return -EINVAL;
}{...}
/* ... */#endif
static void comp_data_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct net_buf_simple *sdu = NULL;
uint8_t page = 0U;
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
/* ... */
page = net_buf_simple_pull_u8(buf);
/* ... */
page = bt_mesh_comp_page_check(page, false);
sdu = bt_mesh_alloc_buf(MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_MAX_PDU_LEN_WITH_SMIC));
if (!sdu) {
BT_ERR("%s, Out of memory", __func__);
return;
}{...}
bt_mesh_model_msg_init(sdu, OP_COMP_DATA_STATUS);
net_buf_simple_add_u8(sdu, page);
/* ... */
if (bt_mesh_get_comp_data(sdu, page, 0, true)) {
BT_ERR("Unable to get composition page 0x%02x", page);
bt_mesh_free_buf(sdu);
return;
}{...}
if (bt_mesh_model_send(model, ctx, sdu, NULL, NULL)) {
BT_ERR("Unable to send Config Composition Data Status");
}{...}
bt_mesh_free_buf(sdu);
}{ ... }
static struct bt_mesh_model *get_model(struct bt_mesh_elem *elem,
struct net_buf_simple *buf, bool *vnd)
{
uint16_t company = 0U, id = 0U;
if (buf->len < 4) {
id = net_buf_simple_pull_le16(buf);
BT_DBG("ID 0x%04x addr 0x%04x", id, elem->addr);
*vnd = false;
return bt_mesh_model_find(elem, id);
}{...}
company = net_buf_simple_pull_le16(buf);
id = net_buf_simple_pull_le16(buf);
BT_DBG("Company 0x%04x ID 0x%04x addr 0x%04x", company, id,
elem->addr);
*vnd = true;
return bt_mesh_model_find_vnd(elem, company, id);
}{ ... }
static bool mod_pub_app_key_bound(struct bt_mesh_model *model,
uint16_t app_idx)
{
int i;
for (i = 0; i < ARRAY_SIZE(model->keys); i++) {
if (model->keys[i] == app_idx) {
return true;
}{...}
}{...}
BT_ERR("AppKey(0x%02x) not bound to this model", app_idx);
return false;
}{ ... }
static uint8_t _mod_pub_set(struct bt_mesh_model *model, uint16_t pub_addr,
uint16_t app_idx, uint8_t cred_flag, uint8_t ttl, uint8_t period,
uint8_t retransmit, bool store)
{
if (!model->pub) {
return STATUS_NVAL_PUB_PARAM;
}{...}
if (!IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) && cred_flag) {
return STATUS_FEAT_NOT_SUPP;
}{...}
if (!model->pub->update && period) {
return STATUS_NVAL_PUB_PARAM;
}{...}
if (pub_addr == BLE_MESH_ADDR_UNASSIGNED) {
if (model->pub->addr == BLE_MESH_ADDR_UNASSIGNED) {
return STATUS_SUCCESS;
}{...}
model->pub->addr = BLE_MESH_ADDR_UNASSIGNED;
model->pub->key = 0U;
model->pub->cred = 0U;
model->pub->ttl = 0U;
model->pub->period = 0U;
model->pub->retransmit = 0U;
model->pub->count = 0U;
if (model->pub->update) {
k_delayed_work_cancel(&model->pub->timer);
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && store) {
bt_mesh_store_mod_pub(model);
}{...}
return STATUS_SUCCESS;
}{...}
/* ... */
if (!bt_mesh_app_key_get(app_idx) ||
!mod_pub_app_key_bound(model, app_idx)) {
return STATUS_INVALID_APPKEY;
}{...}
model->pub->addr = pub_addr;
model->pub->key = app_idx;
model->pub->cred = cred_flag;
model->pub->ttl = ttl;
model->pub->period = period;
model->pub->retransmit = retransmit;
if (model->pub->update) {
int32_t period_ms;
period_ms = bt_mesh_model_pub_period_get(model);
BT_DBG("period %u ms", period_ms);
if (period_ms) {
k_delayed_work_submit(&model->pub->timer, period_ms);
}{...} else {
k_delayed_work_cancel(&model->pub->timer);
}{...}
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && store) {
bt_mesh_store_mod_pub(model);
}{...}
return STATUS_SUCCESS;
}{ ... }
static uint8_t mod_bind(struct bt_mesh_model *model, uint16_t key_idx)
{
int i;
BT_DBG("model %p key_idx 0x%03x", model, key_idx);
if (!bt_mesh_app_key_get(key_idx)) {
return STATUS_INVALID_APPKEY;
}{...}
for (i = 0; i < ARRAY_SIZE(model->keys); i++) {
if (model->keys[i] == key_idx) {
return STATUS_SUCCESS;
}{...}
}{...}
for (i = 0; i < ARRAY_SIZE(model->keys); i++) {
if (model->keys[i] == BLE_MESH_KEY_UNUSED) {
model->keys[i] = key_idx;
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_mod_bind(model);
}{...}
return STATUS_SUCCESS;
}{...}
}{...}
return STATUS_INSUFF_RESOURCES;
}{ ... }
static uint8_t mod_unbind(struct bt_mesh_model *model, uint16_t key_idx, bool store)
{
int i;
BT_DBG("model %p key_idx 0x%03x store %u", model, key_idx, store);
if (!bt_mesh_app_key_get(key_idx)) {
return STATUS_INVALID_APPKEY;
}{...}
for (i = 0; i < ARRAY_SIZE(model->keys); i++) {
if (model->keys[i] != key_idx) {
continue;
}{...}
model->keys[i] = BLE_MESH_KEY_UNUSED;
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && store) {
bt_mesh_store_mod_bind(model);
}{...}
if (model->pub && model->pub->key == key_idx) {
_mod_pub_set(model, BLE_MESH_ADDR_UNASSIGNED,
0, 0, 0, 0, 0, store);
}{...}
}{...}
return STATUS_SUCCESS;
}{ ... }
struct bt_mesh_app_key *bt_mesh_app_key_alloc(uint16_t app_idx)
{
int i;
for (i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) {
struct bt_mesh_app_key *key = &bt_mesh.app_keys[i];
if (key->net_idx == BLE_MESH_KEY_UNUSED) {
return key;
}{...}
}{...}
return NULL;
}{ ... }
static uint8_t app_key_set(uint16_t net_idx, uint16_t app_idx, const uint8_t val[16],
bool update)
{
struct bt_mesh_app_keys *keys = NULL;
struct bt_mesh_app_key *key = NULL;
struct bt_mesh_subnet *sub = NULL;
BT_DBG("net_idx 0x%04x app_idx %04x update %u val %s",
net_idx, app_idx, update, bt_hex(val, 16));
sub = bt_mesh_subnet_get(net_idx);
if (!sub) {
return STATUS_INVALID_NETKEY;
}{...}
key = bt_mesh_app_key_get(app_idx);
if (update) {
if (!key) {
return STATUS_INVALID_APPKEY;
}{...}
if (key->net_idx != net_idx) {
return STATUS_INVALID_BINDING;
}{...}
keys = &key->keys[1];
/* ... */
if (sub->kr_phase != BLE_MESH_KR_PHASE_1) {
return STATUS_CANNOT_UPDATE;
}{...}
if (key->updated) {
if (memcmp(keys->val, val, 16)) {
return STATUS_CANNOT_UPDATE;
}{...}
return STATUS_SUCCESS;
}{...}
key->updated = true;
}{...} else {
if (key) {
if (key->net_idx == net_idx &&
!memcmp(key->keys[0].val, val, 16)) {
return STATUS_SUCCESS;
}{...}
if (key->net_idx == net_idx) {
return STATUS_IDX_ALREADY_STORED;
}{...}
return STATUS_INVALID_NETKEY;
}{...}
key = bt_mesh_app_key_alloc(app_idx);
if (!key) {
return STATUS_INSUFF_RESOURCES;
}{...}
keys = &key->keys[0];
}{...}
if (bt_mesh_app_id(val, &keys->id)) {
if (update) {
key->updated = false;
}{...}
return STATUS_STORAGE_FAIL;
}{...}
BT_DBG("app_idx 0x%04x AID 0x%02x", app_idx, keys->id);
key->net_idx = net_idx;
key->app_idx = app_idx;
memcpy(keys->val, val, 16);
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
BT_DBG("Storing AppKey persistently");
bt_mesh_store_app_key(key);
}{...}
return STATUS_SUCCESS;
}{ ... }
static void app_key_add(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_STATUS, 4);
uint16_t key_net_idx = 0U, key_app_idx = 0U;
uint8_t status = 0U;
key_idx_unpack(buf, &key_net_idx, &key_app_idx);
BT_DBG("AppIdx 0x%04x NetIdx 0x%04x", key_app_idx, key_net_idx);
bt_mesh_model_msg_init(&msg, OP_APP_KEY_STATUS);
status = app_key_set(key_net_idx, key_app_idx, buf->data, false);
BT_DBG("status 0x%02x", status);
net_buf_simple_add_u8(&msg, status);
key_idx_pack(&msg, key_net_idx, key_app_idx);
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config AppKey Status");
return;
}{...}
if (status == STATUS_SUCCESS) {
bt_mesh_cfg_server_state_change_t change = {0};
change.cfg_appkey_add.net_idx = key_net_idx;
change.cfg_appkey_add.app_idx = key_app_idx;
memcpy(change.cfg_appkey_add.app_key, buf->data, 16);
bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
}{...}
}{ ... }
static void app_key_update(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_STATUS, 4);
uint16_t key_net_idx = 0U, key_app_idx = 0U;
uint8_t status = 0U;
key_idx_unpack(buf, &key_net_idx, &key_app_idx);
BT_DBG("AppIdx 0x%04x NetIdx 0x%04x", key_app_idx, key_net_idx);
bt_mesh_model_msg_init(&msg, OP_APP_KEY_STATUS);
status = app_key_set(key_net_idx, key_app_idx, buf->data, true);
BT_DBG("status 0x%02x", status);
net_buf_simple_add_u8(&msg, status);
key_idx_pack(&msg, key_net_idx, key_app_idx);
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config AppKey Status");
}{...}
if (status == STATUS_SUCCESS) {
bt_mesh_cfg_server_state_change_t change = {0};
change.cfg_appkey_update.net_idx = key_net_idx;
change.cfg_appkey_update.app_idx = key_app_idx;
memcpy(change.cfg_appkey_update.app_key, buf->data, 16);
bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
}{...}
}{ ... }
struct unbind_data {
uint16_t app_idx;
bool store;
}{ ... };
static void _mod_unbind(struct bt_mesh_model *mod, struct bt_mesh_elem *elem,
bool vnd, bool primary, void *user_data)
{
struct unbind_data *data = user_data;
mod_unbind(mod, data->app_idx, data->store);
}{ ... }
void bt_mesh_app_key_del(struct bt_mesh_app_key *key, bool store)
{
struct unbind_data data = { .app_idx = key->app_idx, .store = store };
BT_DBG("AppIdx 0x%03x store %u", key->app_idx, store);
bt_mesh_model_foreach(_mod_unbind, &data);
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && store) {
bt_mesh_clear_app_key(key);
}{...}
key->net_idx = BLE_MESH_KEY_UNUSED;
(void)memset(key->keys, 0, sizeof(key->keys));
}{ ... }
static void app_key_del(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_STATUS, 4);
uint16_t key_net_idx = 0U, key_app_idx = 0U;
struct bt_mesh_app_key *key = NULL;
uint8_t status = 0U;
key_idx_unpack(buf, &key_net_idx, &key_app_idx);
BT_DBG("AppIdx 0x%04x NetIdx 0x%04x", key_app_idx, key_net_idx);
if (!bt_mesh_subnet_get(key_net_idx)) {
status = STATUS_INVALID_NETKEY;
goto send_status;
}{...}
key = bt_mesh_app_key_get(key_app_idx);
if (!key) {
/* ... */
status = STATUS_SUCCESS;
goto send_status;
}{...}
if (key->net_idx != key_net_idx) {
status = STATUS_INVALID_BINDING;
goto send_status;
}{...}
bt_mesh_app_key_del(key, true);
status = STATUS_SUCCESS;
send_status:
bt_mesh_model_msg_init(&msg, OP_APP_KEY_STATUS);
net_buf_simple_add_u8(&msg, status);
key_idx_pack(&msg, key_net_idx, key_app_idx);
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config AppKey Status");
}{...}
if (status == STATUS_SUCCESS) {
bt_mesh_cfg_server_state_change_t change = {0};
change.cfg_appkey_delete.net_idx = key_net_idx;
change.cfg_appkey_delete.app_idx = key_app_idx;
bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
}{...}
}{ ... }
#define IDX_LEN(num) (((num) / 2) * 3 + ((num) % 2) * 2)
static void app_key_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_APP_KEY_LIST,
3 + IDX_LEN(CONFIG_BLE_MESH_APP_KEY_COUNT));
uint16_t get_idx = 0U, i = 0U, prev = 0U;
uint8_t status = 0U;
get_idx = net_buf_simple_pull_le16(buf);
if (get_idx > 0xfff) {
BT_ERR("Invalid NetKeyIndex 0x%04x", get_idx);
return;
}{...}
BT_DBG("idx 0x%04x", get_idx);
bt_mesh_model_msg_init(&msg, OP_APP_KEY_LIST);
if (!bt_mesh_subnet_get(get_idx)) {
status = STATUS_INVALID_NETKEY;
}{...} else {
status = STATUS_SUCCESS;
}{...}
net_buf_simple_add_u8(&msg, status);
net_buf_simple_add_le16(&msg, get_idx);
if (status != STATUS_SUCCESS) {
goto send_status;
}{...}
prev = BLE_MESH_KEY_UNUSED;
for (i = 0U; i < ARRAY_SIZE(bt_mesh.app_keys); i++) {
struct bt_mesh_app_key *key = &bt_mesh.app_keys[i];
if (key->net_idx != get_idx) {
continue;
}{...}
if (prev == BLE_MESH_KEY_UNUSED) {
prev = key->app_idx;
continue;
}{...}
key_idx_pack(&msg, prev, key->app_idx);
prev = BLE_MESH_KEY_UNUSED;
}{...}
if (prev != BLE_MESH_KEY_UNUSED) {
net_buf_simple_add_le16(&msg, prev);
}{...}
send_status:
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config AppKey List");
}{...}
}{ ... }
static void beacon_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_BEACON_STATUS, 1);
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
bt_mesh_model_msg_init(&msg, OP_BEACON_STATUS);
net_buf_simple_add_u8(&msg, bt_mesh_secure_beacon_get());
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config Beacon Status");
}{...}
}{ ... }
static void beacon_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_BEACON_STATUS, 1);
struct bt_mesh_cfg_srv *cfg = model->user_data;
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
if (!cfg) {
BT_WARN("No Configuration Server context available");
}{...} else if (buf->data[0] == 0x00 || buf->data[0] == 0x01) {
if (buf->data[0] != cfg->beacon) {
cfg->beacon = buf->data[0];
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_cfg();
}{...}
if (cfg->beacon) {
bt_mesh_secure_beacon_enable();
}{...} else {
bt_mesh_secure_beacon_disable();
}{...}
}{...}
}{...} else {
BT_WARN("Invalid Config Beacon value 0x%02x", buf->data[0]);
return;
}{...}
bt_mesh_model_msg_init(&msg, OP_BEACON_STATUS);
net_buf_simple_add_u8(&msg, bt_mesh_secure_beacon_get());
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config Beacon Status");
}{...}
}{ ... }
static void default_ttl_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_DEFAULT_TTL_STATUS, 1);
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
bt_mesh_model_msg_init(&msg, OP_DEFAULT_TTL_STATUS);
net_buf_simple_add_u8(&msg, bt_mesh_default_ttl_get());
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config Default TTL Status");
}{...}
}{ ... }
static void default_ttl_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_DEFAULT_TTL_STATUS, 1);
struct bt_mesh_cfg_srv *cfg = model->user_data;
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
if (!cfg) {
BT_WARN("No Configuration Server context available");
}{...} else if (buf->data[0] <= BLE_MESH_TTL_MAX && buf->data[0] != 0x01) {
if (cfg->default_ttl != buf->data[0]) {
cfg->default_ttl = buf->data[0];
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_cfg();
}{...}
}{...}
}{...} else {
BT_WARN("Prohibited Default TTL value 0x%02x", buf->data[0]);
return;
}{...}
bt_mesh_model_msg_init(&msg, OP_DEFAULT_TTL_STATUS);
net_buf_simple_add_u8(&msg, bt_mesh_default_ttl_get());
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config Default TTL Status");
}{...}
}{ ... }
static void send_gatt_proxy_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_GATT_PROXY_STATUS, 1);
bt_mesh_model_msg_init(&msg, OP_GATT_PROXY_STATUS);
net_buf_simple_add_u8(&msg, bt_mesh_gatt_proxy_get());
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config GATT Proxy Status");
}{...}
}{ ... }
static void gatt_proxy_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
send_gatt_proxy_status(model, ctx);
}{ ... }
static void gatt_proxy_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_cfg_srv *cfg = model->user_data;
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
if (buf->data[0] != 0x00 && buf->data[0] != 0x01) {
BT_WARN("Invalid GATT Proxy value 0x%02x", buf->data[0]);
return;
}{...}
if (!IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) ||
bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_NOT_SUPPORTED) {
goto send_status;
}{...}
if (!cfg) {
BT_WARN("No Configuration Server context available");
goto send_status;
}{...}
BT_DBG("GATT Proxy 0x%02x -> 0x%02x", cfg->gatt_proxy, buf->data[0]);
if (cfg->gatt_proxy == buf->data[0]) {
goto send_status;
}{...}
cfg->gatt_proxy = buf->data[0];
#if CONFIG_BLE_MESH_PRB_SRV
/* ... */
if (buf->data[0] == BLE_MESH_GATT_PROXY_ENABLED) {
bt_mesh_disable_private_gatt_proxy();
}{...}
#endif/* ... */
#if CONFIG_BLE_MESH_DF_SRV
/* ... */
bt_mesh_disable_directed_proxy_state(ctx->net_idx);/* ... */
#endif
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_cfg();
}{...}
if (cfg->hb_pub.feat & BLE_MESH_FEAT_PROXY) {
bt_mesh_heartbeat_send();
}{...}
send_status:
send_gatt_proxy_status(model, ctx);
}{ ... }
static void net_transmit_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_TRANSMIT_STATUS, 1);
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
bt_mesh_model_msg_init(&msg, OP_NET_TRANSMIT_STATUS);
net_buf_simple_add_u8(&msg, bt_mesh_net_transmit_get());
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config Network Transmit Status");
}{...}
}{ ... }
static void net_transmit_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_TRANSMIT_STATUS, 1);
struct bt_mesh_cfg_srv *cfg = model->user_data;
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
BT_DBG("Transmit 0x%02x (count %u interval %ums)", buf->data[0],
BLE_MESH_TRANSMIT_COUNT(buf->data[0]),
BLE_MESH_TRANSMIT_INT(buf->data[0]));
if (!cfg) {
BT_WARN("No Configuration Server context available");
}{...} else {
cfg->net_transmit = buf->data[0];
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_cfg();
}{...}
}{...}
bt_mesh_model_msg_init(&msg, OP_NET_TRANSMIT_STATUS);
net_buf_simple_add_u8(&msg, bt_mesh_net_transmit_get());
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config Network Transmit Status");
}{...}
}{ ... }
static void relay_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_RELAY_STATUS, 2);
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
bt_mesh_model_msg_init(&msg, OP_RELAY_STATUS);
net_buf_simple_add_u8(&msg, bt_mesh_relay_get());
net_buf_simple_add_u8(&msg, bt_mesh_relay_retransmit_get());
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config Relay Status");
}{...}
}{ ... }
static void relay_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_RELAY_STATUS, 2);
struct bt_mesh_cfg_srv *cfg = model->user_data;
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
if (!cfg) {
BT_WARN("No Configuration Server context available");
}{...} else if (buf->data[0] == 0x00 || buf->data[0] == 0x01) {
bool change;
if (cfg->relay == BLE_MESH_RELAY_NOT_SUPPORTED) {
change = false;
}{...} else {
change = (cfg->relay != buf->data[0]);
cfg->relay = buf->data[0];
cfg->relay_retransmit = buf->data[1];
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_cfg();
}{...}
}{...}
BT_DBG("Relay 0x%02x (%s) xmit 0x%02x (count %u interval %u)",
cfg->relay, change ? "changed" : "not changed",
cfg->relay_retransmit,
BLE_MESH_TRANSMIT_COUNT(cfg->relay_retransmit),
BLE_MESH_TRANSMIT_INT(cfg->relay_retransmit));
if ((cfg->hb_pub.feat & BLE_MESH_FEAT_RELAY) && change) {
bt_mesh_heartbeat_send();
}{...}
}{...} else {
BT_WARN("Invalid Relay value 0x%02x", buf->data[0]);
return;
}{...}
bt_mesh_model_msg_init(&msg, OP_RELAY_STATUS);
net_buf_simple_add_u8(&msg, bt_mesh_relay_get());
net_buf_simple_add_u8(&msg, bt_mesh_relay_retransmit_get());
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config Relay Status");
}{...}
}{ ... }
static void send_mod_pub_status(struct bt_mesh_model *cfg_mod,
struct bt_mesh_msg_ctx *ctx,
uint16_t elem_addr, uint16_t pub_addr,
bool vnd, struct bt_mesh_model *mod,
uint8_t status, uint8_t *mod_id)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_PUB_STATUS, 14);
bt_mesh_model_msg_init(&msg, OP_MOD_PUB_STATUS);
net_buf_simple_add_u8(&msg, status);
net_buf_simple_add_le16(&msg, elem_addr);
if (status != STATUS_SUCCESS) {
(void)memset(net_buf_simple_add(&msg, 7), 0, 7);
}{...} else {
uint16_t idx_cred;
net_buf_simple_add_le16(&msg, pub_addr);
idx_cred = mod->pub->key | (uint16_t)mod->pub->cred << 12;
net_buf_simple_add_le16(&msg, idx_cred);
net_buf_simple_add_u8(&msg, mod->pub->ttl);
net_buf_simple_add_u8(&msg, mod->pub->period);
net_buf_simple_add_u8(&msg, mod->pub->retransmit);
}{...}
if (vnd) {
memcpy(net_buf_simple_add(&msg, 4), mod_id, 4);
}{...} else {
memcpy(net_buf_simple_add(&msg, 2), mod_id, 2);
}{...}
if (bt_mesh_model_send(cfg_mod, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config Model Publication Status");
}{...}
}{ ... }
static void mod_pub_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
uint16_t elem_addr = 0U, pub_addr = 0U;
struct bt_mesh_model *mod = NULL;
struct bt_mesh_elem *elem = NULL;
uint8_t *mod_id = NULL, status = 0U;
bool vnd = false;
elem_addr = net_buf_simple_pull_le16(buf);
if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) {
BT_ERR("Prohibited element address 0x%04x", elem_addr);
return;
}{...}
mod_id = buf->data;
BT_DBG("elem_addr 0x%04x", elem_addr);
elem = bt_mesh_elem_find(elem_addr);
if (!elem) {
mod = NULL;
vnd = (buf->len == 4U);
status = STATUS_INVALID_ADDRESS;
goto send_status;
}{...}
mod = get_model(elem, buf, &vnd);
if (!mod) {
status = STATUS_INVALID_MODEL;
goto send_status;
}{...}
if (!mod->pub) {
status = STATUS_NVAL_PUB_PARAM;
goto send_status;
}{...}
pub_addr = mod->pub->addr;
status = STATUS_SUCCESS;
send_status:
send_mod_pub_status(model, ctx, elem_addr, pub_addr, vnd, mod,
status, mod_id);
}{ ... }
static void mod_pub_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
uint8_t retransmit = 0U, status = 0U, pub_ttl = 0U, pub_period = 0U, cred_flag = 0U;
uint16_t elem_addr = 0U, pub_addr = 0U, pub_app_idx = 0U;
struct bt_mesh_model *mod = NULL;
struct bt_mesh_elem *elem = NULL;
uint8_t *mod_id = NULL;
bool vnd = false;
elem_addr = net_buf_simple_pull_le16(buf);
if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) {
BT_ERR("Prohibited element address 0x%04x", elem_addr);
return;
}{...}
pub_addr = net_buf_simple_pull_le16(buf);
pub_app_idx = net_buf_simple_pull_le16(buf);
cred_flag = ((pub_app_idx >> 12) & BIT_MASK(1));
pub_app_idx &= BIT_MASK(12);
pub_ttl = net_buf_simple_pull_u8(buf);
if (pub_ttl > BLE_MESH_TTL_MAX && pub_ttl != BLE_MESH_TTL_DEFAULT) {
BT_ERR("Invalid TTL value 0x%02x", pub_ttl);
return;
}{...}
pub_period = net_buf_simple_pull_u8(buf);
retransmit = net_buf_simple_pull_u8(buf);
mod_id = buf->data;
BT_DBG("elem_addr 0x%04x pub_addr 0x%04x cred_flag %u",
elem_addr, pub_addr, cred_flag);
BT_DBG("pub_app_idx 0x%03x, pub_ttl %u pub_period 0x%02x",
pub_app_idx, pub_ttl, pub_period);
BT_DBG("retransmit 0x%02x (count %u interval %ums)", retransmit,
BLE_MESH_PUB_TRANSMIT_COUNT(retransmit),
BLE_MESH_PUB_TRANSMIT_INT(retransmit));
elem = bt_mesh_elem_find(elem_addr);
if (!elem) {
mod = NULL;
vnd = (buf->len == 4U);
status = STATUS_INVALID_ADDRESS;
goto send_status;
}{...}
mod = get_model(elem, buf, &vnd);
if (!mod) {
status = STATUS_INVALID_MODEL;
goto send_status;
}{...}
status = _mod_pub_set(mod, pub_addr, pub_app_idx, cred_flag, pub_ttl,
pub_period, retransmit, true);
send_status:
send_mod_pub_status(model, ctx, elem_addr, pub_addr, vnd, mod,
status, mod_id);
if (status == STATUS_SUCCESS && mod->pub) {
bt_mesh_cfg_server_state_change_t change = {0};
change.cfg_mod_pub_set.elem_addr = elem_addr;
change.cfg_mod_pub_set.pub_addr = mod->pub->addr;
change.cfg_mod_pub_set.app_idx = mod->pub->key;
change.cfg_mod_pub_set.cred_flag = mod->pub->cred;
change.cfg_mod_pub_set.ttl = mod->pub->ttl;
change.cfg_mod_pub_set.period = mod->pub->period;
change.cfg_mod_pub_set.transmit = mod->pub->retransmit;
change.cfg_mod_pub_set.cid = vnd ? mod->vnd.company : 0xFFFF;
change.cfg_mod_pub_set.mod_id = vnd ? mod->vnd.id : mod->id;
bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
}{...}
}{ ... }
struct label *get_label(uint16_t index)
{
if (index >= ARRAY_SIZE(labels)) {
return NULL;
}{...}
return &labels[index];
}{ ... }
#if CONFIG_BLE_MESH_LABEL_COUNT > 0
static inline void va_store(struct label *store)
{
bt_mesh_atomic_set_bit(store->flags, BLE_MESH_VA_CHANGED);
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_label();
}{...}
}{ ... }
static struct label *va_find(const uint8_t *label_uuid,
struct label **free_slot)
{
struct label *match = NULL;
int i;
if (free_slot != NULL) {
*free_slot = NULL;
}{...}
for (i = 0; i < ARRAY_SIZE(labels); i++) {
if (labels[i].ref == 0) {
if (free_slot != NULL) {
*free_slot = &labels[i];
}{...}
continue;
}{...}
if (!memcmp(labels[i].uuid, label_uuid, 16)) {
match = &labels[i];
}{...}
}{...}
return match;
}{ ... }
uint8_t va_add(uint8_t *label_uuid, uint16_t *addr)
{
struct label *update = NULL, *free_slot = NULL;
update = va_find(label_uuid, &free_slot);
if (update) {
update->ref++;
va_store(update);
if (addr) {
*addr = update->addr;
}{...}
return STATUS_SUCCESS;
}{...}
if (!free_slot) {
return STATUS_INSUFF_RESOURCES;
}{...}
if (bt_mesh_virtual_addr(label_uuid, addr) < 0) {
return STATUS_UNSPECIFIED;
}{...}
free_slot->ref = 1U;
free_slot->addr = *addr;
memcpy(free_slot->uuid, label_uuid, 16);
va_store(free_slot);
return STATUS_SUCCESS;
}{ ... }
uint8_t va_del(uint8_t *label_uuid, uint16_t *addr)
{
struct label *update = NULL;
update = va_find(label_uuid, NULL);
if (update) {
update->ref--;
if (addr) {
*addr = update->addr;
}{...}
va_store(update);
return STATUS_SUCCESS;
}{...}
if (addr) {
*addr = BLE_MESH_ADDR_UNASSIGNED;
}{...}
return STATUS_CANNOT_REMOVE;
}{ ... }
static size_t mod_sub_list_clear(struct bt_mesh_model *mod)
{
uint8_t *label_uuid = NULL;
size_t clear_count = 0U;
int i;
for (i = 0, clear_count = 0; i < ARRAY_SIZE(mod->groups); i++) {
if (!BLE_MESH_ADDR_IS_VIRTUAL(mod->groups[i])) {
if (mod->groups[i] != BLE_MESH_ADDR_UNASSIGNED) {
mod->groups[i] = BLE_MESH_ADDR_UNASSIGNED;
clear_count++;
}{...}
continue;
}{...}
label_uuid = bt_mesh_label_uuid_get(mod->groups[i]);
mod->groups[i] = BLE_MESH_ADDR_UNASSIGNED;
clear_count++;
if (label_uuid) {
va_del(label_uuid, NULL);
}{...} else {
BT_ERR("Label UUID not found");
}{...}
}{...}
return clear_count;
}{ ... }
static void mod_pub_va_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
uint8_t retransmit = 0U, status = 0U, pub_ttl = 0U, pub_period = 0U, cred_flag = 0U;
uint16_t elem_addr = 0U, pub_addr = 0U, pub_app_idx = 0U;
struct bt_mesh_model *mod = NULL;
struct bt_mesh_elem *elem = NULL;
uint8_t *label_uuid = NULL;
uint8_t *mod_id = NULL;
bool vnd = false;
elem_addr = net_buf_simple_pull_le16(buf);
if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) {
BT_ERR("Prohibited element address 0x%04x", elem_addr);
return;
}{...}
label_uuid = net_buf_simple_pull_mem(buf, 16);
pub_app_idx = net_buf_simple_pull_le16(buf);
cred_flag = ((pub_app_idx >> 12) & BIT_MASK(1));
pub_app_idx &= BIT_MASK(12);
pub_ttl = net_buf_simple_pull_u8(buf);
if (pub_ttl > BLE_MESH_TTL_MAX && pub_ttl != BLE_MESH_TTL_DEFAULT) {
BT_ERR("Invalid TTL value 0x%02x", pub_ttl);
return;
}{...}
pub_period = net_buf_simple_pull_u8(buf);
retransmit = net_buf_simple_pull_u8(buf);
mod_id = buf->data;
BT_DBG("elem_addr 0x%04x cred_flag %u", elem_addr, cred_flag);
BT_DBG("pub_app_idx 0x%03x, pub_ttl %u pub_period 0x%02x",
pub_app_idx, pub_ttl, pub_period);
BT_DBG("retransmit 0x%02x (count %u interval %ums)", retransmit,
BLE_MESH_PUB_TRANSMIT_COUNT(retransmit),
BLE_MESH_PUB_TRANSMIT_INT(retransmit));
elem = bt_mesh_elem_find(elem_addr);
if (!elem) {
mod = NULL;
vnd = (buf->len == 4U);
pub_addr = 0U;
status = STATUS_INVALID_ADDRESS;
goto send_status;
}{...}
mod = get_model(elem, buf, &vnd);
if (!mod) {
pub_addr = 0U;
status = STATUS_INVALID_MODEL;
goto send_status;
}{...}
status = va_add(label_uuid, &pub_addr);
if (status == STATUS_SUCCESS) {
status = _mod_pub_set(mod, pub_addr, pub_app_idx, cred_flag,
pub_ttl, pub_period, retransmit, true);
}{...}
send_status:
send_mod_pub_status(model, ctx, elem_addr, pub_addr, vnd, mod,
status, mod_id);
}{ ... }
#else/* ... */
static size_t mod_sub_list_clear(struct bt_mesh_model *mod)
{
size_t clear_count = 0U;
int i;
for (i = 0, clear_count = 0; i < ARRAY_SIZE(mod->groups); i++) {
if (mod->groups[i] != BLE_MESH_ADDR_UNASSIGNED) {
mod->groups[i] = BLE_MESH_ADDR_UNASSIGNED;
clear_count++;
}{...}
}{...}
return clear_count;
}{...}
static void mod_pub_va_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
uint8_t *mod_id = NULL, status = 0U;
struct bt_mesh_model *mod = NULL;
struct bt_mesh_elem *elem = NULL;
uint16_t elem_addr = 0U, pub_addr = 0U;
bool vnd = false;
elem_addr = net_buf_simple_pull_le16(buf);
if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) {
BT_ERR("Prohibited element address 0x%04x", elem_addr);
return;
}{...}
net_buf_simple_pull(buf, 16);
mod_id = net_buf_simple_pull(buf, 4);
BT_DBG("elem_addr 0x%04x", elem_addr);
elem = bt_mesh_elem_find(elem_addr);
if (!elem) {
mod = NULL;
vnd = (buf->len == 4U);
status = STATUS_INVALID_ADDRESS;
goto send_status;
}{...}
mod = get_model(elem, buf, &vnd);
if (!mod) {
status = STATUS_INVALID_MODEL;
goto send_status;
}{...}
if (!mod->pub) {
status = STATUS_NVAL_PUB_PARAM;
goto send_status;
}{...}
pub_addr = mod->pub->addr;
status = STATUS_INSUFF_RESOURCES;
send_status:
send_mod_pub_status(model, ctx, elem_addr, pub_addr, vnd, mod,
status, mod_id);
}{...}
/* ... */#endif
static void send_mod_sub_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx, uint8_t status,
uint16_t elem_addr, uint16_t sub_addr, uint8_t *mod_id,
bool vnd)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_SUB_STATUS, 9);
BT_DBG("status 0x%02x elem_addr 0x%04x sub_addr 0x%04x", status,
elem_addr, sub_addr);
bt_mesh_model_msg_init(&msg, OP_MOD_SUB_STATUS);
net_buf_simple_add_u8(&msg, status);
net_buf_simple_add_le16(&msg, elem_addr);
net_buf_simple_add_le16(&msg, sub_addr);
if (vnd) {
memcpy(net_buf_simple_add(&msg, 4), mod_id, 4);
}{...} else {
memcpy(net_buf_simple_add(&msg, 2), mod_id, 2);
}{...}
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config Model Subscription Status");
}{...}
}{ ... }
static void mod_sub_add(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
uint16_t elem_addr = 0U, sub_addr = 0U;
struct bt_mesh_model *mod = NULL;
struct bt_mesh_elem *elem = NULL;
uint8_t *mod_id = NULL;
uint8_t status = 0U;
bool vnd = false;
int i;
elem_addr = net_buf_simple_pull_le16(buf);
if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) {
BT_ERR("Prohibited element address 0x%04x", elem_addr);
return;
}{...}
sub_addr = net_buf_simple_pull_le16(buf);
BT_DBG("elem_addr 0x%04x, sub_addr 0x%04x", elem_addr, sub_addr);
mod_id = buf->data;
elem = bt_mesh_elem_find(elem_addr);
if (!elem) {
mod = NULL;
vnd = (buf->len == 4U);
status = STATUS_INVALID_ADDRESS;
goto send_status;
}{...}
mod = get_model(elem, buf, &vnd);
if (!mod) {
status = STATUS_INVALID_MODEL;
goto send_status;
}{...}
if (!BLE_MESH_ADDR_IS_GROUP(sub_addr)) {
status = STATUS_INVALID_ADDRESS;
goto send_status;
}{...}
if (bt_mesh_model_find_group(mod, sub_addr)) {
BT_DBG("found existing subscription");
status = STATUS_SUCCESS;
goto send_status;
}{...}
BT_BQB(BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_NODE | \
BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_TNPT,
"SubGroupAddr: 0x%x", sub_addr);
for (i = 0; i < ARRAY_SIZE(mod->groups); i++) {
if (mod->groups[i] == BLE_MESH_ADDR_UNASSIGNED) {
mod->groups[i] = sub_addr;
break;
}{...}
}{...}
if (i == ARRAY_SIZE(mod->groups)) {
status = STATUS_INSUFF_RESOURCES;
}{...} else {
status = STATUS_SUCCESS;
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_mod_sub(mod);
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) {
bt_mesh_lpn_group_add(sub_addr);
}{...}
}{...}
send_status:
send_mod_sub_status(model, ctx, status, elem_addr, sub_addr,
mod_id, vnd);
#if CONFIG_BLE_MESH_DF_SRV
bt_mesh_directed_forwarding_node_solicitation(mod, bt_mesh_subnet_get(ctx->net_idx));
#endif
if (status == STATUS_SUCCESS) {
bt_mesh_cfg_server_state_change_t change = {0};
change.cfg_mod_sub_add.elem_addr = elem_addr;
change.cfg_mod_sub_add.sub_addr = sub_addr;
change.cfg_mod_sub_add.cid = vnd ? mod->vnd.company : 0xFFFF;
change.cfg_mod_sub_add.mod_id = vnd ? mod->vnd.id : mod->id;
bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
}{...}
}{ ... }
static void mod_sub_del(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
uint16_t elem_addr = 0U, sub_addr = 0U;
struct bt_mesh_model *mod = NULL;
struct bt_mesh_elem *elem = NULL;
uint8_t *mod_id = NULL;
uint16_t *match = NULL;
uint8_t status = 0U;
bool vnd = false;
elem_addr = net_buf_simple_pull_le16(buf);
if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) {
BT_ERR("Prohibited element address 0x%04x", elem_addr);
return;
}{...}
sub_addr = net_buf_simple_pull_le16(buf);
BT_DBG("elem_addr 0x%04x sub_addr 0x%04x", elem_addr, sub_addr);
mod_id = buf->data;
elem = bt_mesh_elem_find(elem_addr);
if (!elem) {
mod = NULL;
vnd = (buf->len == 4U);
status = STATUS_INVALID_ADDRESS;
goto send_status;
}{...}
mod = get_model(elem, buf, &vnd);
if (!mod) {
status = STATUS_INVALID_MODEL;
goto send_status;
}{...}
if (!BLE_MESH_ADDR_IS_GROUP(sub_addr)) {
status = STATUS_INVALID_ADDRESS;
goto send_status;
}{...}
/* ... */
status = STATUS_SUCCESS;
if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) {
bt_mesh_lpn_group_del(&sub_addr, 1);
}{...}
match = bt_mesh_model_find_group(mod, sub_addr);
if (match) {
*match = BLE_MESH_ADDR_UNASSIGNED;
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_mod_sub(mod);
}{...}
}{...}
send_status:
send_mod_sub_status(model, ctx, status, elem_addr, sub_addr,
mod_id, vnd);
if (status == STATUS_SUCCESS) {
bt_mesh_cfg_server_state_change_t change = {0};
change.cfg_mod_sub_delete.elem_addr = elem_addr;
change.cfg_mod_sub_delete.sub_addr = sub_addr;
change.cfg_mod_sub_delete.cid = vnd ? mod->vnd.company : 0xFFFF;
change.cfg_mod_sub_delete.mod_id = vnd ? mod->vnd.id : mod->id;
bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
}{...}
}{ ... }
static void mod_sub_overwrite(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
uint16_t elem_addr = 0U, sub_addr = 0U;
struct bt_mesh_model *mod = NULL;
struct bt_mesh_elem *elem = NULL;
uint8_t *mod_id = NULL;
uint8_t status = 0U;
bool vnd = false;
elem_addr = net_buf_simple_pull_le16(buf);
if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) {
BT_ERR("Prohibited element address 0x%04x", elem_addr);
return;
}{...}
sub_addr = net_buf_simple_pull_le16(buf);
BT_DBG("elem_addr 0x%04x sub_addr 0x%04x", elem_addr, sub_addr);
mod_id = buf->data;
elem = bt_mesh_elem_find(elem_addr);
if (!elem) {
mod = NULL;
vnd = (buf->len == 4U);
status = STATUS_INVALID_ADDRESS;
goto send_status;
}{...}
mod = get_model(elem, buf, &vnd);
if (!mod) {
status = STATUS_INVALID_MODEL;
goto send_status;
}{...}
if (!BLE_MESH_ADDR_IS_GROUP(sub_addr)) {
status = STATUS_INVALID_ADDRESS;
goto send_status;
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) {
bt_mesh_lpn_group_del(mod->groups, ARRAY_SIZE(mod->groups));
}{...}
mod_sub_list_clear(mod);
if (ARRAY_SIZE(mod->groups) > 0) {
mod->groups[0] = sub_addr;
status = STATUS_SUCCESS;
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_mod_sub(mod);
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) {
bt_mesh_lpn_group_add(sub_addr);
}{...}
}{...} else {
status = STATUS_INSUFF_RESOURCES;
}{...}
send_status:
send_mod_sub_status(model, ctx, status, elem_addr, sub_addr,
mod_id, vnd);
}{ ... }
static void mod_sub_del_all(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_model *mod = NULL;
struct bt_mesh_elem *elem = NULL;
uint16_t elem_addr = 0U;
uint8_t *mod_id = NULL;
uint8_t status = 0U;
bool vnd = false;
elem_addr = net_buf_simple_pull_le16(buf);
if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) {
BT_ERR("Prohibited element address 0x%04x", elem_addr);
return;
}{...}
BT_DBG("elem_addr 0x%04x", elem_addr);
mod_id = buf->data;
elem = bt_mesh_elem_find(elem_addr);
if (!elem) {
mod = NULL;
vnd = (buf->len == 4U);
status = STATUS_INVALID_ADDRESS;
goto send_status;
}{...}
mod = get_model(elem, buf, &vnd);
if (!mod) {
status = STATUS_INVALID_MODEL;
goto send_status;
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) {
bt_mesh_lpn_group_del(mod->groups, ARRAY_SIZE(mod->groups));
}{...}
mod_sub_list_clear(mod);
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_mod_sub(mod);
}{...}
status = STATUS_SUCCESS;
send_status:
send_mod_sub_status(model, ctx, status, elem_addr,
BLE_MESH_ADDR_UNASSIGNED, mod_id, vnd);
}{ ... }
static void mod_sub_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_SUB_LIST,
5 + CONFIG_BLE_MESH_MODEL_GROUP_COUNT * 2);
struct bt_mesh_model *mod = NULL;
struct bt_mesh_elem *elem = NULL;
uint16_t addr = 0U, id = 0U;
int i;
addr = net_buf_simple_pull_le16(buf);
if (!BLE_MESH_ADDR_IS_UNICAST(addr)) {
BT_ERR("Prohibited element address 0x%04x", addr);
return;
}{...}
id = net_buf_simple_pull_le16(buf);
BT_DBG("addr 0x%04x id 0x%04x", addr, id);
bt_mesh_model_msg_init(&msg, OP_MOD_SUB_LIST);
elem = bt_mesh_elem_find(addr);
if (!elem) {
net_buf_simple_add_u8(&msg, STATUS_INVALID_ADDRESS);
net_buf_simple_add_le16(&msg, addr);
net_buf_simple_add_le16(&msg, id);
goto send_list;
}{...}
mod = bt_mesh_model_find(elem, id);
if (!mod) {
net_buf_simple_add_u8(&msg, STATUS_INVALID_MODEL);
net_buf_simple_add_le16(&msg, addr);
net_buf_simple_add_le16(&msg, id);
goto send_list;
}{...}
net_buf_simple_add_u8(&msg, STATUS_SUCCESS);
net_buf_simple_add_le16(&msg, addr);
net_buf_simple_add_le16(&msg, id);
for (i = 0; i < ARRAY_SIZE(mod->groups); i++) {
if (mod->groups[i] != BLE_MESH_ADDR_UNASSIGNED) {
net_buf_simple_add_le16(&msg, mod->groups[i]);
}{...}
}{...}
send_list:
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config Model Subscription List");
}{...}
}{ ... }
static void mod_sub_get_vnd(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_SUB_LIST_VND,
7 + CONFIG_BLE_MESH_MODEL_GROUP_COUNT * 2);
struct bt_mesh_model *mod = NULL;
struct bt_mesh_elem *elem = NULL;
uint16_t company = 0U, addr = 0U, id = 0U;
int i;
addr = net_buf_simple_pull_le16(buf);
if (!BLE_MESH_ADDR_IS_UNICAST(addr)) {
BT_ERR("Prohibited element address 0x%04x", addr);
return;
}{...}
company = net_buf_simple_pull_le16(buf);
id = net_buf_simple_pull_le16(buf);
BT_DBG("addr 0x%04x company 0x%04x id 0x%04x", addr, company, id);
bt_mesh_model_msg_init(&msg, OP_MOD_SUB_LIST_VND);
elem = bt_mesh_elem_find(addr);
if (!elem) {
net_buf_simple_add_u8(&msg, STATUS_INVALID_ADDRESS);
net_buf_simple_add_le16(&msg, addr);
net_buf_simple_add_le16(&msg, company);
net_buf_simple_add_le16(&msg, id);
goto send_list;
}{...}
mod = bt_mesh_model_find_vnd(elem, company, id);
if (!mod) {
net_buf_simple_add_u8(&msg, STATUS_INVALID_MODEL);
net_buf_simple_add_le16(&msg, addr);
net_buf_simple_add_le16(&msg, company);
net_buf_simple_add_le16(&msg, id);
goto send_list;
}{...}
net_buf_simple_add_u8(&msg, STATUS_SUCCESS);
net_buf_simple_add_le16(&msg, addr);
net_buf_simple_add_le16(&msg, company);
net_buf_simple_add_le16(&msg, id);
for (i = 0; i < ARRAY_SIZE(mod->groups); i++) {
if (mod->groups[i] != BLE_MESH_ADDR_UNASSIGNED) {
net_buf_simple_add_le16(&msg, mod->groups[i]);
}{...}
}{...}
send_list:
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config Vendor Model Subscription List");
}{...}
}{ ... }
#if CONFIG_BLE_MESH_LABEL_COUNT > 0
static void mod_sub_va_add(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
uint16_t elem_addr = 0U, sub_addr = 0U;
struct bt_mesh_model *mod = NULL;
struct bt_mesh_elem *elem = NULL;
uint8_t *label_uuid = NULL;
uint8_t *mod_id = NULL;
uint8_t status = 0U;
bool vnd = false;
int i;
elem_addr = net_buf_simple_pull_le16(buf);
if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) {
BT_ERR("Prohibited element address 0x%04x", elem_addr);
return;
}{...}
label_uuid = net_buf_simple_pull_mem(buf, 16);
BT_DBG("elem_addr 0x%04x", elem_addr);
mod_id = buf->data;
elem = bt_mesh_elem_find(elem_addr);
if (!elem) {
mod = NULL;
vnd = (buf->len == 4U);
sub_addr = BLE_MESH_ADDR_UNASSIGNED;
status = STATUS_INVALID_ADDRESS;
goto send_status;
}{...}
mod = get_model(elem, buf, &vnd);
if (!mod) {
sub_addr = BLE_MESH_ADDR_UNASSIGNED;
status = STATUS_INVALID_MODEL;
goto send_status;
}{...}
status = va_add(label_uuid, &sub_addr);
if (status != STATUS_SUCCESS) {
goto send_status;
}{...}
if (bt_mesh_model_find_group(mod, sub_addr)) {
status = STATUS_SUCCESS;
goto send_status;
}{...}
BT_BQB(BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_NODE | \
BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_TNPT,
"SubVirtualAddr: 0x%x", sub_addr);
for (i = 0; i < ARRAY_SIZE(mod->groups); i++) {
if (mod->groups[i] == BLE_MESH_ADDR_UNASSIGNED) {
mod->groups[i] = sub_addr;
break;
}{...}
}{...}
if (i == ARRAY_SIZE(mod->groups)) {
status = STATUS_INSUFF_RESOURCES;
}{...} else {
if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) {
bt_mesh_lpn_group_add(sub_addr);
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_mod_sub(mod);
}{...}
status = STATUS_SUCCESS;
}{...}
send_status:
send_mod_sub_status(model, ctx, status, elem_addr, sub_addr,
mod_id, vnd);
}{ ... }
static void mod_sub_va_del(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
uint16_t elem_addr = 0U, sub_addr = 0U;
struct bt_mesh_model *mod = NULL;
struct bt_mesh_elem *elem = NULL;
uint8_t *label_uuid = NULL;
uint8_t *mod_id = NULL;
uint16_t *match = NULL;
uint8_t status = 0U;
bool vnd = false;
elem_addr = net_buf_simple_pull_le16(buf);
if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) {
BT_ERR("Prohibited element address 0x%04x", elem_addr);
return;
}{...}
label_uuid = net_buf_simple_pull_mem(buf, 16);
BT_DBG("elem_addr 0x%04x", elem_addr);
mod_id = buf->data;
elem = bt_mesh_elem_find(elem_addr);
if (!elem) {
mod = NULL;
vnd = (buf->len == 4U);
sub_addr = BLE_MESH_ADDR_UNASSIGNED;
status = STATUS_INVALID_ADDRESS;
goto send_status;
}{...}
mod = get_model(elem, buf, &vnd);
if (!mod) {
sub_addr = BLE_MESH_ADDR_UNASSIGNED;
status = STATUS_INVALID_MODEL;
goto send_status;
}{...}
status = va_del(label_uuid, &sub_addr);
if (sub_addr == BLE_MESH_ADDR_UNASSIGNED) {
goto send_status;
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) {
bt_mesh_lpn_group_del(&sub_addr, 1);
}{...}
match = bt_mesh_model_find_group(mod, sub_addr);
if (match) {
*match = BLE_MESH_ADDR_UNASSIGNED;
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_mod_sub(mod);
}{...}
status = STATUS_SUCCESS;
}{...} else {
status = STATUS_CANNOT_REMOVE;
}{...}
send_status:
send_mod_sub_status(model, ctx, status, elem_addr, sub_addr,
mod_id, vnd);
}{ ... }
static void mod_sub_va_overwrite(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
uint16_t elem_addr = 0U, sub_addr = BLE_MESH_ADDR_UNASSIGNED;
struct bt_mesh_model *mod = NULL;
struct bt_mesh_elem *elem = NULL;
uint8_t *label_uuid = NULL;
uint8_t *mod_id = NULL;
uint8_t status = 0U;
bool vnd = false;
elem_addr = net_buf_simple_pull_le16(buf);
if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) {
BT_ERR("Prohibited element address 0x%04x", elem_addr);
return;
}{...}
label_uuid = net_buf_simple_pull_mem(buf, 16);
BT_DBG("elem_addr 0x%04x", elem_addr);
mod_id = buf->data;
elem = bt_mesh_elem_find(elem_addr);
if (!elem) {
mod = NULL;
vnd = (buf->len == 4U);
status = STATUS_INVALID_ADDRESS;
goto send_status;
}{...}
mod = get_model(elem, buf, &vnd);
if (!mod) {
status = STATUS_INVALID_MODEL;
goto send_status;
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) {
bt_mesh_lpn_group_del(mod->groups, ARRAY_SIZE(mod->groups));
}{...}
mod_sub_list_clear(mod);
if (ARRAY_SIZE(mod->groups) > 0) {
status = va_add(label_uuid, &sub_addr);
if (status == STATUS_SUCCESS) {
mod->groups[0] = sub_addr;
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_mod_sub(mod);
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER)) {
bt_mesh_lpn_group_add(sub_addr);
}{...}
}{...}
}{...} else {
status = STATUS_INSUFF_RESOURCES;
}{...}
send_status:
send_mod_sub_status(model, ctx, status, elem_addr, sub_addr,
mod_id, vnd);
}{ ... }
#else/* ... */
static void mod_sub_va_add(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_model *mod = NULL;
struct bt_mesh_elem *elem = NULL;
uint16_t elem_addr = 0U;
uint8_t *mod_id = NULL;
uint8_t status = 0U;
bool vnd = false;
elem_addr = net_buf_simple_pull_le16(buf);
if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) {
BT_ERR("Prohibited element address 0x%04x", elem_addr);
return;
}{...}
net_buf_simple_pull(buf, 16);
mod_id = buf->data;
elem = bt_mesh_elem_find(elem_addr);
if (!elem) {
mod = NULL;
vnd = (buf->len == 4U);
status = STATUS_INVALID_ADDRESS;
goto send_status;
}{...}
mod = get_model(elem, buf, &vnd);
if (!mod) {
status = STATUS_INVALID_MODEL;
goto send_status;
}{...}
status = STATUS_INSUFF_RESOURCES;
send_status:
send_mod_sub_status(model, ctx, status, elem_addr,
BLE_MESH_ADDR_UNASSIGNED, mod_id, vnd);
}{...}
static void mod_sub_va_del(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_elem *elem = NULL;
uint16_t elem_addr = 0U;
uint8_t *mod_id = NULL;
uint8_t status = 0U;
bool vnd = false;
elem_addr = net_buf_simple_pull_le16(buf);
if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) {
BT_ERR("Prohibited element address 0x%04x", elem_addr);
return;
}{...}
net_buf_simple_pull(buf, 16);
mod_id = buf->data;
elem = bt_mesh_elem_find(elem_addr);
if (!elem) {
vnd = (buf->len == 4U);
status = STATUS_INVALID_ADDRESS;
goto send_status;
}{...}
if (!get_model(elem, buf, &vnd)) {
status = STATUS_INVALID_MODEL;
goto send_status;
}{...}
status = STATUS_INSUFF_RESOURCES;
send_status:
send_mod_sub_status(model, ctx, status, elem_addr,
BLE_MESH_ADDR_UNASSIGNED, mod_id, vnd);
}{...}
static void mod_sub_va_overwrite(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_elem *elem = NULL;
uint16_t elem_addr = 0U;
uint8_t *mod_id = NULL;
uint8_t status = 0U;
bool vnd = false;
elem_addr = net_buf_simple_pull_le16(buf);
if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) {
BT_ERR("Prohibited element address 0x%04x", elem_addr);
return;
}{...}
net_buf_simple_pull(buf, 18);
mod_id = buf->data;
elem = bt_mesh_elem_find(elem_addr);
if (!elem) {
vnd = (buf->len == 4U);
status = STATUS_INVALID_ADDRESS;
goto send_status;
}{...}
if (!get_model(elem, buf, &vnd)) {
status = STATUS_INVALID_MODEL;
goto send_status;
}{...}
status = STATUS_INSUFF_RESOURCES;
send_status:
send_mod_sub_status(model, ctx, status, elem_addr,
BLE_MESH_ADDR_UNASSIGNED, mod_id, vnd);
}{...}
/* ... */#endif
static void send_net_key_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
uint16_t idx, uint8_t status)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_KEY_STATUS, 3);
bt_mesh_model_msg_init(&msg, OP_NET_KEY_STATUS);
net_buf_simple_add_u8(&msg, status);
net_buf_simple_add_le16(&msg, idx);
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config NetKey Status");
}{...}
}{ ... }
static void net_key_add(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_subnet *sub = NULL;
uint16_t idx = 0U;
int err = 0;
idx = net_buf_simple_pull_le16(buf);
if (idx > 0xfff) {
BT_ERR("Invalid NetKeyIndex 0x%04x", idx);
return;
}{...}
BT_DBG("idx 0x%04x", idx);
sub = bt_mesh_subnet_get(idx);
if (!sub) {
int i;
for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) {
if (bt_mesh.sub[i].net_idx == BLE_MESH_KEY_UNUSED) {
sub = &bt_mesh.sub[i];
break;
}{...}
}{...}
if (!sub) {
send_net_key_status(model, ctx, idx,
STATUS_INSUFF_RESOURCES);
return;
}{...}
}{...}
if (sub->net_idx == idx) {
uint8_t status = 0U;
if (memcmp(buf->data, sub->keys[0].net, 16)) {
status = STATUS_IDX_ALREADY_STORED;
}{...} else {
status = STATUS_SUCCESS;
}{...}
send_net_key_status(model, ctx, idx, status);
return;
}{...}
err = bt_mesh_net_keys_create(&sub->keys[0], buf->data);
if (err) {
send_net_key_status(model, ctx, idx, STATUS_UNSPECIFIED);
return;
}{...}
sub->net_idx = idx;
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
BT_DBG("Storing NetKey persistently");
bt_mesh_store_subnet(sub);
}{...}
bt_mesh_net_secure_beacon_update(sub);
if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) {
sub->node_id = BLE_MESH_NODE_IDENTITY_STOPPED;
bt_mesh_proxy_server_beacon_send(sub);
#if CONFIG_BLE_MESH_DF_SRV && CONFIG_BLE_MESH_SUPPORT_DIRECTED_PROXY
/* ... */
if (sub->directed_proxy != BLE_MESH_DIRECTED_PROXY_NOT_SUPPORTED) {
bt_mesh_directed_proxy_server_directed_proxy_caps_send(sub, false);
}{...}
#endif/* ... */
bt_mesh_adv_update();
}{...} else {
sub->node_id = BLE_MESH_NODE_IDENTITY_NOT_SUPPORTED;
}{...}
send_net_key_status(model, ctx, idx, STATUS_SUCCESS);
#if CONFIG_BLE_MESH_DF_SRV
if (bt_mesh_directed_forwarding_sub_init(sub)) {
BT_ERR("Failed to init subnet for directed forward");
}{...}
#endif/* ... */
bt_mesh_cfg_server_state_change_t change = {0};
change.cfg_netkey_add.net_idx = sub->net_idx;
memcpy(change.cfg_netkey_add.net_key, sub->keys[0].net, 16);
bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
}{ ... }
static void net_key_update(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_subnet *sub = NULL;
uint16_t idx = 0U;
int err = 0;
idx = net_buf_simple_pull_le16(buf);
if (idx > 0xfff) {
BT_ERR("Invalid NetKeyIndex 0x%04x", idx);
return;
}{...}
BT_DBG("idx 0x%04x", idx);
sub = bt_mesh_subnet_get(idx);
if (!sub) {
send_net_key_status(model, ctx, idx, STATUS_INVALID_NETKEY);
return;
}{...}
/* ... */
switch (sub->kr_phase) {
case BLE_MESH_KR_NORMAL:
if (!memcmp(buf->data, sub->keys[0].net, 16)) {
return;
}{...}
break;...
case BLE_MESH_KR_PHASE_1:
if (!memcmp(buf->data, sub->keys[1].net, 16)) {
send_net_key_status(model, ctx, idx, STATUS_SUCCESS);
return;
}{...}
...
case BLE_MESH_KR_PHASE_2:
case BLE_MESH_KR_PHASE_3:
send_net_key_status(model, ctx, idx, STATUS_CANNOT_UPDATE);
return;...
}{...}
err = bt_mesh_net_keys_create(&sub->keys[1], buf->data);
if (!err && (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) ||
IS_ENABLED(CONFIG_BLE_MESH_FRIEND))) {
err = friend_cred_update(sub);
}{...}
if (err) {
send_net_key_status(model, ctx, idx, STATUS_UNSPECIFIED);
return;
}{...}
sub->kr_phase = BLE_MESH_KR_PHASE_1;
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
BT_DBG("Storing NetKey persistently");
bt_mesh_store_subnet(sub);
}{...}
bt_mesh_net_secure_beacon_update(sub);
send_net_key_status(model, ctx, idx, STATUS_SUCCESS);
bt_mesh_cfg_server_state_change_t change = {0};
change.cfg_netkey_update.net_idx = sub->net_idx;
memcpy(change.cfg_netkey_update.net_key, sub->keys[1].net, 16);
bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
}{ ... }
static void hb_pub_disable(struct bt_mesh_cfg_srv *cfg)
{
cfg->hb_pub.dst = BLE_MESH_ADDR_UNASSIGNED;
cfg->hb_pub.count = 0U;
cfg->hb_pub.ttl = 0U;
cfg->hb_pub.period = 0U;
k_delayed_work_cancel(&cfg->hb_pub.timer);
}{ ... }
static void net_key_del(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_subnet *sub = NULL;
uint16_t del_idx = 0U;
uint8_t status = 0U;
del_idx = net_buf_simple_pull_le16(buf);
if (del_idx > 0xfff) {
BT_ERR("Invalid NetKeyIndex 0x%04x", del_idx);
return;
}{...}
BT_DBG("idx 0x%04x", del_idx);
sub = bt_mesh_subnet_get(del_idx);
if (!sub) {
/* ... */
status = STATUS_SUCCESS;
goto send_status;
}{...}
/* ... */
if (ctx->net_idx == del_idx) {
status = STATUS_CANNOT_REMOVE;
goto send_status;
}{...}
#if CONFIG_BLE_MESH_DF_SRV && CONFIG_BLE_MESH_SUPPORT_DIRECTED_PROXY
/* ... */
if (sub->directed_proxy != BLE_MESH_DIRECTED_PROXY_NOT_SUPPORTED) {
bt_mesh_directed_proxy_server_directed_proxy_caps_send(sub, true);
}{...}
#if CONFIG_BLE_MESH_SETTINGS
bt_mesh_clear_directed_forwarding_table_data(del_idx);
#endif/* ... */
#endif
bt_mesh_subnet_del(sub, true);
status = STATUS_SUCCESS;
#if CONFIG_BLE_MESH_BRC_SRV
/* ... */
bt_mesh_delete_netkey_in_bridge_table(del_idx);/* ... */
#endif
#if CONFIG_BLE_MESH_RPR_SRV
bt_mesh_rpr_srv_netkey_del(del_idx);
#endif
send_status:
send_net_key_status(model, ctx, del_idx, status);
if (status == STATUS_SUCCESS) {
bt_mesh_cfg_server_state_change_t change = {0};
change.cfg_netkey_delete.net_idx = sub ? sub->net_idx : BLE_MESH_KEY_UNUSED;
bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
}{...}
}{ ... }
static void net_key_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NET_KEY_LIST,
IDX_LEN(CONFIG_BLE_MESH_SUBNET_COUNT));
uint16_t prev = 0U, i = 0U;
bt_mesh_model_msg_init(&msg, OP_NET_KEY_LIST);
prev = BLE_MESH_KEY_UNUSED;
for (i = 0U; i < ARRAY_SIZE(bt_mesh.sub); i++) {
struct bt_mesh_subnet *sub = &bt_mesh.sub[i];
if (sub->net_idx == BLE_MESH_KEY_UNUSED) {
continue;
}{...}
if (prev == BLE_MESH_KEY_UNUSED) {
prev = sub->net_idx;
continue;
}{...}
key_idx_pack(&msg, prev, sub->net_idx);
prev = BLE_MESH_KEY_UNUSED;
}{...}
if (prev != BLE_MESH_KEY_UNUSED) {
net_buf_simple_add_le16(&msg, prev);
}{...}
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config NetKey List");
}{...}
}{ ... }
static void node_identity_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NODE_IDENTITY_STATUS, 4);
struct bt_mesh_subnet *sub = NULL;
uint8_t node_id = 0U;
uint16_t idx = 0U;
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
idx = net_buf_simple_pull_le16(buf);
if (idx > 0xfff) {
BT_ERR("Invalid NetKeyIndex 0x%04x", idx);
return;
}{...}
bt_mesh_model_msg_init(&msg, OP_NODE_IDENTITY_STATUS);
sub = bt_mesh_subnet_get(idx);
if (!sub) {
net_buf_simple_add_u8(&msg, STATUS_INVALID_NETKEY);
node_id = 0x00;
}{...} else {
net_buf_simple_add_u8(&msg, STATUS_SUCCESS);
node_id = sub->node_id;
}{...}
net_buf_simple_add_le16(&msg, idx);
net_buf_simple_add_u8(&msg, node_id);
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config Node Identity Status");
}{...}
}{ ... }
static void node_identity_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NODE_IDENTITY_STATUS, 4);
struct bt_mesh_subnet *sub = NULL;
uint8_t node_id = 0U;
uint16_t idx = 0U;
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
idx = net_buf_simple_pull_le16(buf);
if (idx > 0xfff) {
BT_WARN("Invalid NetKeyIndex 0x%04x", idx);
return;
}{...}
node_id = net_buf_simple_pull_u8(buf);
if (node_id != 0x00 && node_id != 0x01) {
BT_WARN("Invalid Node ID value 0x%02x", node_id);
return;
}{...}
bt_mesh_model_msg_init(&msg, OP_NODE_IDENTITY_STATUS);
sub = bt_mesh_subnet_get(idx);
if (!sub) {
net_buf_simple_add_u8(&msg, STATUS_INVALID_NETKEY);
net_buf_simple_add_le16(&msg, idx);
net_buf_simple_add_u8(&msg, node_id);
}{...} else {
if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) {
if (node_id == BLE_MESH_NODE_IDENTITY_RUNNING) {
#if CONFIG_BLE_MESH_PRB_SRV
/* ... */
bt_mesh_proxy_private_identity_disable();/* ... */
#endif
bt_mesh_proxy_server_identity_start(sub);
}{...} else {
bt_mesh_proxy_server_identity_stop(sub);
}{...}
bt_mesh_adv_update();
}{...}
net_buf_simple_add_u8(&msg, STATUS_SUCCESS);
net_buf_simple_add_le16(&msg, idx);
net_buf_simple_add_u8(&msg, sub->node_id);
}{...}
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config Node Identity Status");
}{...}
}{ ... }
static void create_mod_app_status(struct net_buf_simple *msg,
struct bt_mesh_model *mod, bool vnd,
uint16_t elem_addr, uint16_t app_idx,
uint8_t status, uint8_t *mod_id)
{
bt_mesh_model_msg_init(msg, OP_MOD_APP_STATUS);
net_buf_simple_add_u8(msg, status);
net_buf_simple_add_le16(msg, elem_addr);
net_buf_simple_add_le16(msg, app_idx);
if (vnd) {
memcpy(net_buf_simple_add(msg, 4), mod_id, 4);
}{...} else {
memcpy(net_buf_simple_add(msg, 2), mod_id, 2);
}{...}
}{ ... }
static void mod_app_bind(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_APP_STATUS, 9);
uint16_t elem_addr = 0U, key_app_idx = 0U;
struct bt_mesh_model *mod = NULL;
struct bt_mesh_elem *elem = NULL;
uint8_t *mod_id = NULL, status = 0U;
bool vnd = false;
elem_addr = net_buf_simple_pull_le16(buf);
if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) {
BT_ERR("Prohibited element address 0x%04x", elem_addr);
return;
}{...}
key_app_idx = net_buf_simple_pull_le16(buf);
mod_id = buf->data;
elem = bt_mesh_elem_find(elem_addr);
if (!elem) {
mod = NULL;
vnd = (buf->len == 4U);
status = STATUS_INVALID_ADDRESS;
goto send_status;
}{...}
mod = get_model(elem, buf, &vnd);
if (!mod) {
status = STATUS_INVALID_MODEL;
goto send_status;
}{...}
if (model == mod) {
BT_ERR("Client tried to bind AppKey to Configuration Model");
status = STATUS_CANNOT_BIND;
goto send_status;
}{...}
status = mod_bind(mod, key_app_idx);
BT_INFO("bind app key %#x on mode %#x", key_app_idx, mod->id);
send_status:
BT_DBG("status 0x%02x", status);
create_mod_app_status(&msg, mod, vnd, elem_addr, key_app_idx, status,
mod_id);
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config Model App Bind Status");
}{...}
if (status == STATUS_SUCCESS) {
bt_mesh_cfg_server_state_change_t change = {0};
change.cfg_mod_app_bind.elem_addr = elem_addr;
change.cfg_mod_app_bind.app_idx = key_app_idx;
change.cfg_mod_app_bind.cid = vnd ? mod->vnd.company : 0xFFFF;
change.cfg_mod_app_bind.mod_id = vnd ? mod->vnd.id : mod->id;
bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
}{...}
}{ ... }
static void mod_app_unbind(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_MOD_APP_STATUS, 9);
uint16_t elem_addr = 0U, key_app_idx = 0U;
struct bt_mesh_model *mod = NULL;
struct bt_mesh_elem *elem = NULL;
uint8_t *mod_id = NULL, status = 0U;
bool vnd = false;
elem_addr = net_buf_simple_pull_le16(buf);
if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) {
BT_ERR("Prohibited element address 0x%04x", elem_addr);
return;
}{...}
key_app_idx = net_buf_simple_pull_le16(buf);
mod_id = buf->data;
elem = bt_mesh_elem_find(elem_addr);
if (!elem) {
mod = NULL;
vnd = (buf->len == 4U);
status = STATUS_INVALID_ADDRESS;
goto send_status;
}{...}
mod = get_model(elem, buf, &vnd);
if (!mod) {
status = STATUS_INVALID_MODEL;
goto send_status;
}{...}
status = mod_unbind(mod, key_app_idx, true);
send_status:
BT_DBG("status 0x%02x", status);
create_mod_app_status(&msg, mod, vnd, elem_addr, key_app_idx, status,
mod_id);
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config Model App Unbind Status");
}{...}
if (status == STATUS_SUCCESS) {
bt_mesh_cfg_server_state_change_t change = {0};
change.cfg_mod_app_unbind.elem_addr = elem_addr;
change.cfg_mod_app_unbind.app_idx = key_app_idx;
change.cfg_mod_app_unbind.cid = vnd ? mod->vnd.company : 0xFFFF;
change.cfg_mod_app_unbind.mod_id = vnd ? mod->vnd.id : mod->id;
bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
}{...}
}{ ... }
#define KEY_LIST_LEN (CONFIG_BLE_MESH_MODEL_KEY_COUNT * 2)
static void mod_app_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
NET_BUF_SIMPLE_DEFINE(msg,
MAX(BLE_MESH_MODEL_BUF_LEN(OP_VND_MOD_APP_LIST,
9 + KEY_LIST_LEN),
BLE_MESH_MODEL_BUF_LEN(OP_SIG_MOD_APP_LIST,
9 + KEY_LIST_LEN)));
struct bt_mesh_model *mod = NULL;
struct bt_mesh_elem *elem = NULL;
uint8_t *mod_id = NULL, status = 0U;
uint16_t elem_addr = 0U;
bool vnd = false;
elem_addr = net_buf_simple_pull_le16(buf);
if (!BLE_MESH_ADDR_IS_UNICAST(elem_addr)) {
BT_ERR("Prohibited element address 0x%04x", elem_addr);
return;
}{...}
mod_id = buf->data;
BT_DBG("elem_addr 0x%04x", elem_addr);
elem = bt_mesh_elem_find(elem_addr);
if (!elem) {
mod = NULL;
vnd = (buf->len == 4U);
status = STATUS_INVALID_ADDRESS;
goto send_list;
}{...}
mod = get_model(elem, buf, &vnd);
if (!mod) {
status = STATUS_INVALID_MODEL;
goto send_list;
}{...}
status = STATUS_SUCCESS;
send_list:
if (vnd) {
bt_mesh_model_msg_init(&msg, OP_VND_MOD_APP_LIST);
}{...} else {
bt_mesh_model_msg_init(&msg, OP_SIG_MOD_APP_LIST);
}{...}
net_buf_simple_add_u8(&msg, status);
net_buf_simple_add_le16(&msg, elem_addr);
if (vnd) {
net_buf_simple_add_mem(&msg, mod_id, 4);
}{...} else {
net_buf_simple_add_mem(&msg, mod_id, 2);
}{...}
if (mod) {
int i;
for (i = 0; i < ARRAY_SIZE(mod->keys); i++) {
if (mod->keys[i] != BLE_MESH_KEY_UNUSED) {
net_buf_simple_add_le16(&msg, mod->keys[i]);
}{...}
}{...}
}{...}
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config Model Application List");
}{...}
}{ ... }
static void node_reset(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_NODE_RESET_STATUS, 0);
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
bt_mesh_model_msg_init(&msg, OP_NODE_RESET_STATUS);
/* ... */
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config Node Reset Status");
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_NODE)) {
bt_mesh_node_reset();
}{...}
}{ ... }
static void send_friend_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_FRIEND_STATUS, 1);
struct bt_mesh_cfg_srv *cfg = model->user_data;
bt_mesh_model_msg_init(&msg, OP_FRIEND_STATUS);
net_buf_simple_add_u8(&msg, cfg->frnd);
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config Friend Status");
}{...}
}{ ... }
static void friend_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
send_friend_status(model, ctx);
}{ ... }
static void friend_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_cfg_srv *cfg = model->user_data;
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x len %u: %s",
ctx->net_idx, ctx->app_idx, ctx->addr, buf->len,
bt_hex(buf->data, buf->len));
if (buf->data[0] != 0x00 && buf->data[0] != 0x01) {
BT_WARN("Invalid Friend value 0x%02x", buf->data[0]);
return;
}{...}
if (!cfg) {
BT_WARN("No Configuration Server context available");
goto send_status;
}{...}
BT_DBG("Friend 0x%02x -> 0x%02x", cfg->frnd, buf->data[0]);
if (cfg->frnd == buf->data[0]) {
goto send_status;
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
cfg->frnd = buf->data[0];
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_cfg();
}{...}
if (cfg->frnd == BLE_MESH_FRIEND_DISABLED) {
bt_mesh_friend_clear_net_idx(BLE_MESH_KEY_ANY);
#if CONFIG_BLE_MESH_DF_SRV && CONFIG_BLE_MESH_FRIEND
/* ... */
bt_mesh_disable_directed_friend_state(ctx->net_idx);/* ... */
#endif
}{...}
}{...}
if (cfg->hb_pub.feat & BLE_MESH_FEAT_FRIEND) {
bt_mesh_heartbeat_send();
}{...}
send_status:
send_friend_status(model, ctx);
}{ ... }
static void lpn_timeout_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_LPN_TIMEOUT_STATUS, 5);
struct bt_mesh_friend *frnd = NULL;
uint16_t lpn_addr = 0U;
int32_t timeout = 0;
lpn_addr = net_buf_simple_pull_le16(buf);
BT_DBG("net_idx 0x%04x app_idx 0x%04x src 0x%04x lpn_addr 0x%02x",
ctx->net_idx, ctx->app_idx, ctx->addr, lpn_addr);
if (!BLE_MESH_ADDR_IS_UNICAST(lpn_addr)) {
BT_WARN("Invalid LPNAddress; ignoring msg");
return;
}{...}
bt_mesh_model_msg_init(&msg, OP_LPN_TIMEOUT_STATUS);
net_buf_simple_add_le16(&msg, lpn_addr);
if (!IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
timeout = 0;
goto send_rsp;
}{...}
frnd = bt_mesh_friend_find(BLE_MESH_KEY_ANY, lpn_addr, true, true);
if (!frnd) {
timeout = 0;
goto send_rsp;
}{...}
timeout = k_delayed_work_remaining_get(&frnd->timer) / 100;
send_rsp:
net_buf_simple_add_le24(&msg, timeout);
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config LPN PollTimeout Status");
}{...}
}{ ... }
static void send_krp_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
uint16_t idx, uint8_t phase, uint8_t status)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_KRP_STATUS, 4);
bt_mesh_model_msg_init(&msg, OP_KRP_STATUS);
net_buf_simple_add_u8(&msg, status);
net_buf_simple_add_le16(&msg, idx);
net_buf_simple_add_u8(&msg, phase);
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config Key Refresh Phase Status");
}{...}
}{ ... }
static void krp_get(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_subnet *sub = NULL;
uint16_t idx = 0U;
idx = net_buf_simple_pull_le16(buf);
if (idx > 0xfff) {
BT_ERR("Invalid NetKeyIndex 0x%04x", idx);
return;
}{...}
BT_DBG("idx 0x%04x", idx);
sub = bt_mesh_subnet_get(idx);
if (!sub) {
send_krp_status(model, ctx, idx, 0x00, STATUS_INVALID_NETKEY);
}{...} else {
send_krp_status(model, ctx, idx, sub->kr_phase,
STATUS_SUCCESS);
}{...}
}{ ... }
static void krp_set(struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_subnet *sub = NULL;
uint8_t phase = 0U;
uint16_t idx = 0U;
idx = net_buf_simple_pull_le16(buf);
phase = net_buf_simple_pull_u8(buf);
if (idx > 0xfff) {
BT_ERR("Invalid NetKeyIndex 0x%04x", idx);
return;
}{...}
BT_DBG("idx 0x%04x transition 0x%02x", idx, phase);
sub = bt_mesh_subnet_get(idx);
if (!sub) {
send_krp_status(model, ctx, idx, 0x00, STATUS_INVALID_NETKEY);
return;
}{...}
BT_DBG("%u -> %u", sub->kr_phase, phase);
if (phase < BLE_MESH_KR_PHASE_2 || phase > BLE_MESH_KR_PHASE_3 ||
(sub->kr_phase == BLE_MESH_KR_NORMAL &&
phase == BLE_MESH_KR_PHASE_2)) {
BT_WARN("Prohibited transition %u -> %u", sub->kr_phase, phase);
return;
}{...}
if (sub->kr_phase == BLE_MESH_KR_PHASE_1 &&
phase == BLE_MESH_KR_PHASE_2) {
sub->kr_phase = BLE_MESH_KR_PHASE_2;
sub->kr_flag = 1;
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
BT_DBG("Storing kr phase persistently");
bt_mesh_store_subnet(sub);
}{...}
bt_mesh_net_secure_beacon_update(sub);
}{...} else if ((sub->kr_phase == BLE_MESH_KR_PHASE_1 ||
sub->kr_phase == BLE_MESH_KR_PHASE_2) &&
phase == BLE_MESH_KR_PHASE_3) {
sub->kr_phase = BLE_MESH_KR_NORMAL;
sub->kr_flag = 0;
bt_mesh_net_revoke_keys(sub);
if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) ||
IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
friend_cred_refresh(sub->net_idx);
}{...}
bt_mesh_net_secure_beacon_update(sub);
}{...}
send_krp_status(model, ctx, idx, sub->kr_phase, STATUS_SUCCESS);
bt_mesh_cfg_server_state_change_t change = {0};
change.cfg_kr_phase_set.net_idx = idx;
change.cfg_kr_phase_set.kr_phase = phase;
bt_mesh_config_server_cb_evt_to_btc(BTC_BLE_MESH_EVT_CONFIG_SERVER_STATE_CHANGE,
model, ctx, (const uint8_t *)&change, sizeof(change));
}{ ... }
static uint8_t hb_log(uint16_t val)
{
switch (val) {
case 0x0000:
return 0x00;...
case 0xFFFF:
return 0xFF;...
default:
return 32 - __builtin_clz(val);...
}{...}
}{ ... }
static uint8_t hb_pub_count_log(uint16_t val)
{
switch (val) {
case 0x0000:
return 0x00;...
case 0x0001:
return 0x01;...
case 0xFFFF:
return 0xFF;...
default:
return 32 - __builtin_clz(val - 1) + 1;...
}{...}
}{ ... }
static uint16_t hb_pwr2(uint8_t val, uint8_t sub)
{
switch (val) {
case 0x00:
return 0x0000;...
case 0x11:
case 0xFF:
return 0xFFFF;...
default:
return (1 << (val - sub));...
}{...}
}{ ... }
struct hb_pub_param {
uint16_t dst;
uint8_t count_log;
uint8_t period_log;
uint8_t ttl;
uint16_t feat;
uint16_t net_idx;
}{ ... } __attribute__((packed));
static void hb_pub_send_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx, uint8_t status,
struct hb_pub_param *orig_msg)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_PUB_STATUS, 10);
struct bt_mesh_cfg_srv *cfg = model->user_data;
BT_DBG("src 0x%04x status 0x%02x", ctx->addr, status);
bt_mesh_model_msg_init(&msg, OP_HEARTBEAT_PUB_STATUS);
net_buf_simple_add_u8(&msg, status);
if (orig_msg) {
memcpy(net_buf_simple_add(&msg, sizeof(*orig_msg)), orig_msg,
sizeof(*orig_msg));
goto send;
}{...}
net_buf_simple_add_le16(&msg, cfg->hb_pub.dst);
net_buf_simple_add_u8(&msg, hb_pub_count_log(cfg->hb_pub.count));
net_buf_simple_add_u8(&msg, cfg->hb_pub.period);
net_buf_simple_add_u8(&msg, cfg->hb_pub.ttl);
net_buf_simple_add_le16(&msg, cfg->hb_pub.feat);
net_buf_simple_add_le16(&msg, cfg->hb_pub.net_idx);
send:
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config Heartbeat Publication Status");
}{...}
}{ ... }
static void heartbeat_pub_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BT_DBG("src 0x%04x", ctx->addr);
hb_pub_send_status(model, ctx, STATUS_SUCCESS, NULL);
}{ ... }
static void heartbeat_pub_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct hb_pub_param *param = (void *)buf->data;
struct bt_mesh_cfg_srv *cfg = model->user_data;
uint16_t dst = 0U, feat = 0U, idx = 0U;
uint8_t status = 0U;
BT_DBG("src 0x%04x", ctx->addr);
dst = sys_le16_to_cpu(param->dst);
if (BLE_MESH_ADDR_IS_VIRTUAL(dst)) {
status = STATUS_INVALID_ADDRESS;
goto failed;
}{...}
if (param->count_log > 0x11 && param->count_log != 0xff) {
status = STATUS_CANNOT_SET;
goto failed;
}{...}
if (param->period_log > 0x11) {
status = STATUS_CANNOT_SET;
goto failed;
}{...}
if (param->ttl > BLE_MESH_TTL_MAX && param->ttl != BLE_MESH_TTL_DEFAULT) {
BT_ERR("Invalid TTL value 0x%02x", param->ttl);
return;
}{...}
feat = sys_le16_to_cpu(param->feat);
idx = sys_le16_to_cpu(param->net_idx);
if (idx > 0xfff) {
BT_ERR("Invalid NetKeyIndex 0x%04x", idx);
return;
}{...}
if (!bt_mesh_subnet_get(idx)) {
status = STATUS_INVALID_NETKEY;
goto failed;
}{...}
cfg->hb_pub.dst = dst;
cfg->hb_pub.period = param->period_log;
cfg->hb_pub.feat = feat & BLE_MESH_FEAT_SUPPORTED;
cfg->hb_pub.net_idx = idx;
if (dst == BLE_MESH_ADDR_UNASSIGNED) {
hb_pub_disable(cfg);
}{...} else {
cfg->hb_pub.count = hb_pwr2(param->count_log, 1);
cfg->hb_pub.ttl = param->ttl;
BT_DBG("period %u ms", hb_pwr2(param->period_log, 1) * 1000U);
#if 0
/* ... */
if (param->period_log && param->count_log) {
k_delayed_work_submit(&cfg->hb_pub.timer, K_NO_WAIT);
}{...} else {
k_delayed_work_cancel(&cfg->hb_pub.timer);
}{...}
#endif/* ... */
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_hb_pub();
}{...}
hb_pub_send_status(model, ctx, STATUS_SUCCESS, NULL);
/* ... */
if (dst != BLE_MESH_ADDR_UNASSIGNED) {
if (param->period_log && param->count_log) {
k_delayed_work_submit(&cfg->hb_pub.timer, K_NO_WAIT);
}{...} else {
k_delayed_work_cancel(&cfg->hb_pub.timer);
}{...}
}{...}
return;
failed:
hb_pub_send_status(model, ctx, status, param);
}{ ... }
static void hb_sub_send_status(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx, uint8_t status)
{
BLE_MESH_MODEL_BUF_DEFINE(msg, OP_HEARTBEAT_SUB_STATUS, 9);
struct bt_mesh_cfg_srv *cfg = model->user_data;
uint16_t period = 0U;
int64_t uptime = 0;
BT_DBG("src 0x%04x status 0x%02x", ctx->addr, status);
uptime = k_uptime_get();
if (uptime > cfg->hb_sub.expiry) {
period = 0U;
}{...} else {
period = (cfg->hb_sub.expiry - uptime) / 1000;
}{...}
bt_mesh_model_msg_init(&msg, OP_HEARTBEAT_SUB_STATUS);
net_buf_simple_add_u8(&msg, status);
net_buf_simple_add_le16(&msg, cfg->hb_sub.src);
net_buf_simple_add_le16(&msg, cfg->hb_sub.dst);
net_buf_simple_add_u8(&msg, hb_log(period));
net_buf_simple_add_u8(&msg, hb_log(cfg->hb_sub.count));
net_buf_simple_add_u8(&msg, cfg->hb_sub.min_hops);
net_buf_simple_add_u8(&msg, cfg->hb_sub.max_hops);
if (bt_mesh_model_send(model, ctx, &msg, NULL, NULL)) {
BT_ERR("Unable to send Config Heartbeat Subscription Status");
}{...}
}{ ... }
static void heartbeat_sub_get(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
BT_DBG("src 0x%04x", ctx->addr);
hb_sub_send_status(model, ctx, STATUS_SUCCESS);
}{ ... }
static void heartbeat_sub_set(struct bt_mesh_model *model,
struct bt_mesh_msg_ctx *ctx,
struct net_buf_simple *buf)
{
struct bt_mesh_cfg_srv *cfg = model->user_data;
uint16_t sub_src = 0U, sub_dst = 0U;
uint8_t sub_period = 0U;
int32_t period_ms = 0;
BT_DBG("src 0x%04x", ctx->addr);
sub_src = net_buf_simple_pull_le16(buf);
sub_dst = net_buf_simple_pull_le16(buf);
sub_period = net_buf_simple_pull_u8(buf);
BT_DBG("sub_src 0x%04x sub_dst 0x%04x period 0x%02x",
sub_src, sub_dst, sub_period);
if (sub_src != BLE_MESH_ADDR_UNASSIGNED &&
!BLE_MESH_ADDR_IS_UNICAST(sub_src)) {
BT_WARN("Prohibited source address");
return;
}{...}
if (BLE_MESH_ADDR_IS_VIRTUAL(sub_dst) || BLE_MESH_ADDR_IS_RFU(sub_dst) ||
(BLE_MESH_ADDR_IS_UNICAST(sub_dst) &&
sub_dst != bt_mesh_primary_addr())) {
BT_WARN("Prohibited destination address");
return;
}{...}
if (sub_period > 0x11) {
BT_WARN("Prohibited subscription period 0x%02x", sub_period);
return;
}{...}
if (sub_src == BLE_MESH_ADDR_UNASSIGNED ||
sub_dst == BLE_MESH_ADDR_UNASSIGNED ||
sub_period == 0x00) {
/* ... */
if (sub_src == BLE_MESH_ADDR_UNASSIGNED ||
sub_dst == BLE_MESH_ADDR_UNASSIGNED) {
cfg->hb_sub.src = BLE_MESH_ADDR_UNASSIGNED;
cfg->hb_sub.dst = BLE_MESH_ADDR_UNASSIGNED;
cfg->hb_sub.min_hops = BLE_MESH_TTL_MAX;
cfg->hb_sub.max_hops = 0U;
}{...}
period_ms = 0;
}{...} else {
cfg->hb_sub.src = sub_src;
cfg->hb_sub.dst = sub_dst;
cfg->hb_sub.min_hops = BLE_MESH_TTL_MAX;
cfg->hb_sub.max_hops = 0U;
cfg->hb_sub.count = 0U;
period_ms = hb_pwr2(sub_period, 1) * 1000U;
}{...}
bt_mesh_set_hb_sub_dst(cfg->hb_sub.dst);
BT_DBG("period_ms %u", period_ms);
if (period_ms) {
cfg->hb_sub.expiry = k_uptime_get() + period_ms;
}{...} else {
cfg->hb_sub.expiry = 0;
}{...}
hb_sub_send_status(model, ctx, STATUS_SUCCESS);
/* ... */
cfg->hb_sub.count = 0U;
/* ... */
if (!period_ms) {
cfg->hb_sub.min_hops = 0U;
}{...}
}{ ... }
const struct bt_mesh_model_op bt_mesh_cfg_srv_op[] = {
{ OP_COMP_DATA_GET, 1, comp_data_get },
{ OP_APP_KEY_ADD, 19, app_key_add },
{ OP_APP_KEY_UPDATE, 19, app_key_update },
{ OP_APP_KEY_DEL, 3, app_key_del },
{ OP_APP_KEY_GET, 2, app_key_get },
{ OP_BEACON_GET, 0, beacon_get },
{ OP_BEACON_SET, 1, beacon_set },
{ OP_DEFAULT_TTL_GET, 0, default_ttl_get },
{ OP_DEFAULT_TTL_SET, 1, default_ttl_set },
{ OP_GATT_PROXY_GET, 0, gatt_proxy_get },
{ OP_GATT_PROXY_SET, 1, gatt_proxy_set },
{ OP_NET_TRANSMIT_GET, 0, net_transmit_get },
{ OP_NET_TRANSMIT_SET, 1, net_transmit_set },
{ OP_RELAY_GET, 0, relay_get },
{ OP_RELAY_SET, 2, relay_set },
{ OP_MOD_PUB_GET, 4, mod_pub_get },
{ OP_MOD_PUB_SET, 11, mod_pub_set },
{ OP_MOD_PUB_VA_SET, 24, mod_pub_va_set },
{ OP_MOD_SUB_ADD, 6, mod_sub_add },
{ OP_MOD_SUB_VA_ADD, 20, mod_sub_va_add },
{ OP_MOD_SUB_DEL, 6, mod_sub_del },
{ OP_MOD_SUB_VA_DEL, 20, mod_sub_va_del },
{ OP_MOD_SUB_OVERWRITE, 6, mod_sub_overwrite },
{ OP_MOD_SUB_VA_OVERWRITE, 20, mod_sub_va_overwrite },
{ OP_MOD_SUB_DEL_ALL, 4, mod_sub_del_all },
{ OP_MOD_SUB_GET, 4, mod_sub_get },
{ OP_MOD_SUB_GET_VND, 6, mod_sub_get_vnd },
{ OP_NET_KEY_ADD, 18, net_key_add },
{ OP_NET_KEY_UPDATE, 18, net_key_update },
{ OP_NET_KEY_DEL, 2, net_key_del },
{ OP_NET_KEY_GET, 0, net_key_get },
{ OP_NODE_IDENTITY_GET, 2, node_identity_get },
{ OP_NODE_IDENTITY_SET, 3, node_identity_set },
{ OP_MOD_APP_BIND, 6, mod_app_bind },
{ OP_MOD_APP_UNBIND, 6, mod_app_unbind },
{ OP_SIG_MOD_APP_GET, 4, mod_app_get },
{ OP_VND_MOD_APP_GET, 6, mod_app_get },
{ OP_NODE_RESET, 0, node_reset },
{ OP_FRIEND_GET, 0, friend_get },
{ OP_FRIEND_SET, 1, friend_set },
{ OP_LPN_TIMEOUT_GET, 2, lpn_timeout_get },
{ OP_KRP_GET, 2, krp_get },
{ OP_KRP_SET, 3, krp_set },
{ OP_HEARTBEAT_PUB_GET, 0, heartbeat_pub_get },
{ OP_HEARTBEAT_PUB_SET, 9, heartbeat_pub_set },
{ OP_HEARTBEAT_SUB_GET, 0, heartbeat_sub_get },
{ OP_HEARTBEAT_SUB_SET, 5, heartbeat_sub_set },
BLE_MESH_MODEL_OP_END,
}{...};
static void hb_publish(struct k_work *work)
{
struct bt_mesh_cfg_srv *cfg = CONTAINER_OF(work,
struct bt_mesh_cfg_srv,
hb_pub.timer.work);
struct bt_mesh_subnet *sub = NULL;
uint16_t period_ms = 0U;
BT_DBG("hb_pub.count: %u", cfg->hb_pub.count);
sub = bt_mesh_subnet_get(cfg->hb_pub.net_idx);
if (!sub) {
BT_ERR("No matching subnet for idx 0x%04x",
cfg->hb_pub.net_idx);
cfg->hb_pub.dst = BLE_MESH_ADDR_UNASSIGNED;
return;
}{...}
if (cfg->hb_pub.count == 0U) {
return;
}{...}
period_ms = hb_pwr2(cfg->hb_pub.period, 1) * 1000U;
if (period_ms && cfg->hb_pub.count > 1) {
k_delayed_work_submit(&cfg->hb_pub.timer, period_ms);
}{...}
bt_mesh_heartbeat_send();
if (cfg->hb_pub.count != 0xffff) {
cfg->hb_pub.count--;
}{...}
}{ ... }
static bool conf_is_valid(struct bt_mesh_cfg_srv *cfg)
{
if (cfg->relay > 0x02) {
return false;
}{...}
if (cfg->beacon > 0x01) {
return false;
}{...}
if (cfg->default_ttl > BLE_MESH_TTL_MAX) {
return false;
}{...}
if (!IS_ENABLED(CONFIG_BLE_MESH_RELAY)) {
BT_INFO("Relay not supported");
cfg->relay = BLE_MESH_RELAY_NOT_SUPPORTED;
}{...}
if (!IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) {
BT_INFO("GATT Proxy not supported");
cfg->gatt_proxy = BLE_MESH_GATT_PROXY_NOT_SUPPORTED;
}{...}
if (!IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
BT_INFO("Friend not supported");
cfg->frnd = BLE_MESH_FRIEND_NOT_SUPPORTED;
}{...}
return true;
}{ ... }
static int cfg_srv_init(struct bt_mesh_model *model)
{
struct bt_mesh_cfg_srv *cfg = model->user_data;
if (!bt_mesh_model_in_primary(model)) {
BT_ERR("Configuration Server only allowed in primary element");
return -EINVAL;
}{...}
if (!cfg) {
BT_ERR("No Configuration Server context provided");
return -EINVAL;
}{...}
if (!conf_is_valid(cfg)) {
BT_ERR("Invalid values in configuration");
return -EINVAL;
}{...}
model->keys[0] = BLE_MESH_KEY_DEV;
if (!IS_ENABLED(CONFIG_BLE_MESH_RELAY)) {
cfg->relay = BLE_MESH_RELAY_NOT_SUPPORTED;
}{...}
if (!IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
cfg->frnd = BLE_MESH_FRIEND_NOT_SUPPORTED;
}{...}
if (!IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) {
cfg->gatt_proxy = BLE_MESH_GATT_PROXY_NOT_SUPPORTED;
}{...}
k_delayed_work_init(&cfg->hb_pub.timer, hb_publish);
cfg->hb_pub.net_idx = BLE_MESH_KEY_UNUSED;
cfg->hb_sub.expiry = 0;
cfg->model = model;
conf = cfg;
return 0;
}{ ... }
#if CONFIG_BLE_MESH_DEINIT
static int cfg_srv_deinit(struct bt_mesh_model *model)
{
struct bt_mesh_cfg_srv *cfg = model->user_data;
if (!bt_mesh_model_in_primary(model)) {
BT_ERR("Configuration Server only allowed in primary element");
return -EINVAL;
}{...}
if (!cfg) {
BT_ERR("No Configuration Server context provided");
return -EINVAL;
}{...}
/* ... */
bt_mesh_cfg_reset(false);
k_delayed_work_free(&cfg->hb_pub.timer);
cfg->hb_pub.dst = BLE_MESH_ADDR_UNASSIGNED;
conf = NULL;
return 0;
}{ ... }
/* ... */#endif
const struct bt_mesh_model_cb bt_mesh_cfg_srv_cb = {
.init = cfg_srv_init,
#if CONFIG_BLE_MESH_DEINIT
.deinit = cfg_srv_deinit,
#endif
}{...};
static void mod_reset(struct bt_mesh_model *mod, struct bt_mesh_elem *elem,
bool vnd, bool primary, void *user_data)
{
bool store = *(bool *)user_data;
size_t clear_count = 0U;
/* ... */
clear_count = mod_sub_list_clear(mod);
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && clear_count && store) {
bt_mesh_store_mod_sub(mod);
}{...}
}{ ... }
void bt_mesh_mod_sub_reset(bool store)
{
bt_mesh_model_foreach(mod_reset, &store);
}{ ... }
void bt_mesh_cfg_reset(bool store)
{
struct bt_mesh_cfg_srv *cfg = conf;
int i;
if (!cfg) {
return;
}{...}
bt_mesh_set_hb_sub_dst(BLE_MESH_ADDR_UNASSIGNED);
cfg->hb_sub.src = BLE_MESH_ADDR_UNASSIGNED;
cfg->hb_sub.dst = BLE_MESH_ADDR_UNASSIGNED;
cfg->hb_sub.expiry = 0;
/* ... */
for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) {
struct bt_mesh_subnet *sub = &bt_mesh.sub[i];
if (sub->net_idx != BLE_MESH_KEY_UNUSED) {
bt_mesh_subnet_del(sub, store);
}{...}
}{...}
bt_mesh_mod_sub_reset(store);
(void)memset(labels, 0, sizeof(labels));
}{ ... }
uint8_t bt_mesh_net_transmit_get(void)
{
if (conf) {
return conf->net_transmit;
}{...}
return 0;
}{ ... }
void bt_mesh_relay_local_set(bool enable)
{
if (conf && conf->relay != BLE_MESH_RELAY_NOT_SUPPORTED) {
if (enable) {
conf->relay = BLE_MESH_RELAY_ENABLED;
}{...} else {
conf->relay = BLE_MESH_RELAY_DISABLED;
}{...}
}{...}
}{ ... }
uint8_t bt_mesh_relay_get(void)
{
if (conf) {
return conf->relay;
}{...}
return BLE_MESH_RELAY_NOT_SUPPORTED;
}{ ... }
uint8_t bt_mesh_friend_get(void)
{
if (conf) {
BT_DBG("conf %p conf->frnd 0x%02x", conf, conf->frnd);
return conf->frnd;
}{...}
return BLE_MESH_FRIEND_NOT_SUPPORTED;
}{ ... }
uint8_t bt_mesh_relay_retransmit_get(void)
{
if (conf) {
return conf->relay_retransmit;
}{...}
return 0;
}{ ... }
uint8_t bt_mesh_secure_beacon_get(void)
{
if (conf) {
return conf->beacon;
}{...}
return BLE_MESH_SECURE_BEACON_DISABLED;
}{ ... }
uint8_t bt_mesh_gatt_proxy_get(void)
{
if (conf) {
return conf->gatt_proxy;
}{...}
return BLE_MESH_GATT_PROXY_NOT_SUPPORTED;
}{ ... }
uint8_t bt_mesh_default_ttl_get(void)
{
if (conf) {
return conf->default_ttl;
}{...}
return DEFAULT_TTL;
}{ ... }
uint8_t *bt_mesh_label_uuid_get(uint16_t addr)
{
int i;
BT_DBG("addr 0x%04x", addr);
for (i = 0; i < ARRAY_SIZE(labels); i++) {
if (labels[i].addr == addr) {
BT_DBG("Found Label UUID for 0x%04x: %s", addr,
bt_hex(labels[i].uuid, 16));
return labels[i].uuid;
}{...}
}{...}
BT_WARN("No matching Label UUID for 0x%04x", addr);
return NULL;
}{ ... }
struct bt_mesh_hb_pub *bt_mesh_hb_pub_get(void)
{
if (!conf) {
return NULL;
}{...}
return &conf->hb_pub;
}{ ... }
void bt_mesh_hb_pub_disable(void)
{
if (conf) {
hb_pub_disable(conf);
}{...}
}{ ... }
struct bt_mesh_cfg_srv *bt_mesh_cfg_get(void)
{
return conf;
}{ ... }
void bt_mesh_subnet_del(struct bt_mesh_subnet *sub, bool store)
{
int i;
BT_DBG("NetIdx 0x%03x store %u", sub->net_idx, store);
if (conf && conf->hb_pub.net_idx == sub->net_idx) {
hb_pub_disable(conf);
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && store) {
bt_mesh_store_hb_pub();
}{...}
}{...}
for (i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) {
struct bt_mesh_app_key *key = &bt_mesh.app_keys[i];
if (key->net_idx == sub->net_idx) {
bt_mesh_app_key_del(key, store);
}{...}
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
bt_mesh_friend_clear_net_idx(sub->net_idx);
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS) && store) {
bt_mesh_clear_subnet(sub);
}{...}
(void)memset(sub, 0, sizeof(*sub));
sub->net_idx = BLE_MESH_KEY_UNUSED;
}{ ... }