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
40
41
42
43
44
45
46
47
48
52
53
64
65
69
70
71
72
73
74
75
78
79
80
84
85
86
87
88
89
90
91
92
93
94
95
96
97
99
100
101
102
103
104
105
106
107
108
109
110
111
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
130
131
132
136
137
138
139
140
141
142
143
144
145
146
147
148
149
153
154
157
158
164
168
169
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
190
191
192
193
194
195
196
197
198
199
200
203
204
205
207
208
209
210
211
212
213
214
215
216
219
220
221
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
255
256
257
258
259
261
262
263
264
265
266
267
268
269
270
271
279
295
296
297
300
301
304
305
308
309
310
311
312
313
314
315
316
324
325
326
327
328
329
330
331
332
335
336
337
338
339
340
341
350
351
352
353
354
355
356
357
358
359
362
363
366
367
370
371
376
377
378
379
381
382
383
384
385
386
387
388
389
390
391
392
393
398
399
400
401
402
403
404
405
406
407
408
409
410
411
418
419
424
425
426
427
428
429
430
431
435
438
439
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
483
484
499
500
505
506
507
508
509
510
511
512
513
514
522
523
532
533
538
539
543
544
545
546
547
548
549
550
551
552
553
556
557
558
559
560
561
562
563
564
565
566
569
570
571
572
573
574
577
578
581
582
583
584
585
586
587
588
589
592
593
594
595
596
597
600
601
604
605
606
607
608
609
610
611
612
613
614
619
620
621
622
627
628
629
630
631
632
633
634
635
640
641
642
643
644
645
646
647
648
649
650
651
655
656
657
658
659
660
661
662
663
664
667
668
669
670
675
676
677
678
679
680
681
682
683
686
687
690
691
692
693
694
695
696
697
698
702
703
707
708
709
710
711
712
713
714
717
718
719
720
721
722
723
724
725
731
736
737
738
739
740
745
746
747
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
770
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
794
795
803
804
811
812
813
814
815
819
820
828
829
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
873
874
875
876
877
882
883
884
885
886
891
892
893
894
895
896
897
898
899
900
901
910
911
912
917
918
919
920
921
932
933
934
935
939
940
941
942
943
944
949
950
951
956
957
958
962
963
966
969
970
971
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1008
1009
1010
1011
1012
1013
1014
1015
1016
1020
1021
1031
1032
1036
1037
1040
1041
1042
1043
1044
1045
1046
1047
1048
1052
1053
1054
1055
1056
1057
1060
1064
1065
1066
1067
1068
1069
1070
1071
1082
1083
1084
1085
1086
1087
1088
1089
1090
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1121
1122
1123
1124
1125
1133
1141
1144
1145
1146
1147
1148
1149
1150
1151
1152
1157
1158
1161
1165
1166
1170
1171
1172
1173
1174
1175
1179
1184
1185
1202
1203
1207
1208
1209
1210
1211
1212
1213
1214
1215
1229
1230
1231
1232
1233
1234
1235
1236
1237
1243
1244
1245
1246
1247
1248
1249
1250
1251
1254
1255
1256
1257
1260
1261
1267
1268
1269
1270
1271
1274
1275
1276
1277
1278
1279
1280
1286
1287
1288
1289
1290
1300
1301
1302
1303
1307
1308
1309
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1326
1327
1331
1332
1333
1334
1335
1336
1337
1338
1339
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1360
1361
1365
1366
1370
1375
1376
1380
1381
1386
1387
1404
1405
1409
1410
1411
1412
1413
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1442
1443
1444
1445
1446
1449
1453
1454
1457
1458
1459
1460
1461
1462
1463
1464
1465
1469
1470
1471
1477
1478
1481
1482
1483
1484
1485
1486
1487
1488
1492
1493
1494
1495
1496
1497
1498
1499
1500
1506
1507
1508
1509
1510
1511
1512
1515
1516
1517
1518
1519
1520
1521
1522
1526
1527
1528
1529
1533
1534
1535
1536
1537
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1563
1564
1565
1566
1567
1568
1569
1570
1574
1575
1576
1577
1578
1587
1588
1589
1590
1594
1595
1596
1597
1598
1599
1600
1601
1602
1608
1609
1615
1616
1617
1618
1619
1620
1624
1625
1626
1627
1628
1629
1630
1631
1635
1636
1640
1641
1642
1643
1644
1645
1646
/* ... */
#include <errno.h>
#include <string.h>
#include "crypto.h"
#include "adv.h"
#include "mesh.h"
#include "access.h"
#include "foundation.h"
#include "mesh/common.h"
#include "mesh/proxy.h"
#include "proxy_common.h"
#include "proxy_server.h"
#include "proxy_client.h"
#include "prov_common.h"
#include "prov_node.h"14 includes
#if CONFIG_BLE_MESH_V11_SUPPORT
#include "mesh_v1.1/utils.h"
#endif
#if CONFIG_BLE_MESH_NODE
#if CONFIG_BLE_MESH_PB_ADV && !CONFIG_BLE_MESH_PB_GATT
NET_BUF_SIMPLE_DEFINE_STATIC(rx_buf, PROV_RX_BUF_SIZE);
#endif
static struct bt_mesh_prov_link prov_link;
#if CONFIG_BLE_MESH_PB_ADV
static void reset_adv_link(struct bt_mesh_prov_link *link, uint8_t reason);
extern uint8_t node_next_xact_id(struct bt_mesh_prov_link *link);/* ... */
#endif
#if CONFIG_BLE_MESH_PB_GATT
static int prov_send_gatt(struct bt_mesh_prov_link *link, struct net_buf_simple *msg);
#endif
static void prov_send_fail_msg(uint8_t err);
struct bt_mesh_prov_link *bt_mesh_prov_node_get_link(void)
{
return &prov_link;
}{ ... }
static void close_link(uint8_t reason)
{
if (bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE)) {
if (prov_link.pb_remote_close) {
prov_link.pb_remote_close(&prov_link, reason);
}{...}
return;
}{...}
prov_send_fail_msg(reason);
}{ ... }
void bt_mesh_prov_node_close_link(uint8_t reason)
{
close_link(reason);
}{ ... }
static void reset_state(void)
{
k_delayed_work_cancel(&prov_link.prot_timer);
if (prov_link.conf_inputs[0]) {
bt_mesh_attention(NULL, 0);
}{...}
#if CONFIG_BLE_MESH_PB_GATT
if (prov_link.conn) {
bt_mesh_conn_unref(prov_link.conn);
}{...}
#endif/* ... */
#if CONFIG_BLE_MESH_PB_ADV
/* ... */
(void)memset(&prov_link, 0, offsetof(struct bt_mesh_prov_link, tx.retransmit));
prov_link.pending_ack = PROV_XACT_NVAL;
prov_link.rx.prev_id = PROV_XACT_NVAL;
#if CONFIG_BLE_MESH_PB_GATT
prov_link.rx.buf = bt_mesh_proxy_server_get_buf();
#else
net_buf_simple_reset(&rx_buf);
prov_link.rx.buf = &rx_buf;/* ... */
#endif
prov_link.next_xact_id = node_next_xact_id;
prov_link.reset_adv_link = reset_adv_link;
prov_link.retrans_timeout = reset_adv_link;
prov_link.invite_recv = false;
#if CONFIG_BLE_MESH_FAST_PROV
prov_link.last_tx_pdu = PROV_COMPLETE;
#endif
/* ... */
#else
(void)memset(&prov_link, 0, offsetof(struct bt_mesh_prov_link, prot_timer));/* ... */
#endif
#if CONFIG_BLE_MESH_PB_GATT
prov_link.pb_gatt_send = prov_send_gatt;
#endif
}{ ... }
#if CONFIG_BLE_MESH_PB_ADV
static void reset_adv_link(struct bt_mesh_prov_link *link, uint8_t reason)
{
ARG_UNUSED(link);
bt_mesh_prov_clear_tx(&prov_link, true);
if (bt_mesh_prov_get()->link_close) {
bt_mesh_prov_get()->link_close(BLE_MESH_PROV_ADV, reason);
}{...}
#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN
bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_REMOVE,
BLE_MESH_EXCEP_LIST_TYPE_MESH_LINK_ID,
&prov_link.link_id);/* ... */
#endif
reset_state();
}{ ... }
/* ... */#endif
#if CONFIG_BLE_MESH_PB_GATT
static int prov_send_gatt(struct bt_mesh_prov_link *link, struct net_buf_simple *msg)
{
int err = 0;
ARG_UNUSED(link);
if (!prov_link.conn) {
BT_ERR("PB-GATT send, not connected");
return -ENOTCONN;
}{...}
/* ... */
#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT && CONFIG_BLE_MESH_RPR_SRV
if (bt_mesh_atomic_test_bit(link->flags, PB_REMOTE)) {
err = bt_mesh_proxy_client_send(link->conn, BLE_MESH_PROXY_PROV, msg);
bt_mesh_rpr_srv_send_outbound_report(link->pb_remote_uuid, true);
}{...} else
#endif
{
err = bt_mesh_proxy_server_send(prov_link.conn, BLE_MESH_PROXY_PROV, msg);
}{...}
if (err) {
BT_ERR("Failed to send provisioning PDU");
return err;
}{...}
k_delayed_work_submit(&prov_link.prot_timer, PROTOCOL_TIMEOUT);
return 0;
}{ ... }
/* ... */#endif
static void prov_send_fail_msg(uint8_t err)
{
PROV_BUF(buf, 2);
bt_mesh_prov_buf_init(&buf, PROV_FAILED);
net_buf_simple_add_u8(&buf, err);
if (bt_mesh_prov_send(&prov_link, &buf)) {
BT_ERR("Failed to send Provisioning Failed message");
}{...}
bt_mesh_atomic_set_bit(prov_link.flags, LINK_INVALID);
}{ ... }
static void prov_invite(const uint8_t *data)
{
PROV_BUF(buf, 12);
BT_DBG("Attention Duration: %u seconds", data[0]);
if (data[0]) {
bt_mesh_attention(NULL, data[0]);
}{...}
#if CONFIG_BLE_MESH_CERT_BASED_PROV
prov_link.invite_recv = true;/* ... */
#endif
prov_link.conf_inputs[0] = data[0];
bt_mesh_prov_buf_init(&buf, PROV_CAPABILITIES);
net_buf_simple_add_u8(&buf, bt_mesh_elem_count());
/* ... */
#if CONFIG_BLE_MESH_PROV_EPA
if (bt_mesh_prov_get()->oob_type & BIT(PROV_ONLY_OOB_AUTH_SUPPORT)) {
net_buf_simple_add_be16(&buf, BIT(PROV_ALG_P256_HMAC_SHA256));
}{...} else {
net_buf_simple_add_be16(&buf, (BIT(PROV_ALG_P256_CMAC_AES128) |
BIT(PROV_ALG_P256_HMAC_SHA256)));
}{...}
#else/* ... */
net_buf_simple_add_be16(&buf, BIT(PROV_ALG_P256_CMAC_AES128));
#endif
net_buf_simple_add_u8(&buf, bt_mesh_prov_get()->oob_pub_key);
net_buf_simple_add_u8(&buf, bt_mesh_prov_get()->oob_type);
net_buf_simple_add_u8(&buf, bt_mesh_prov_get()->output_size);
net_buf_simple_add_be16(&buf, bt_mesh_prov_get()->output_actions);
net_buf_simple_add_u8(&buf, bt_mesh_prov_get()->input_size);
net_buf_simple_add_be16(&buf, bt_mesh_prov_get()->input_actions);
memcpy(&prov_link.conf_inputs[1], &buf.data[1], 11);
if (bt_mesh_prov_send(&prov_link, &buf)) {
BT_ERR("Failed to send capabilities");
return;
}{...}
prov_link.expect = PROV_START;
}{ ... }
static void prov_capabilities(const uint8_t *data)
{}{ ... }
static int prov_auth(uint8_t method, uint8_t action, uint8_t size)
{
bt_mesh_output_action_t output = 0U;
bt_mesh_input_action_t input = 0U;
uint8_t auth_size = 0;
auth_size = PROV_AUTH_SIZE(&prov_link);
switch (method) {
case AUTH_METHOD_NO_OOB:
if (action || size) {
return -EINVAL;
}{...}
(void)memset(prov_link.auth, 0, sizeof(prov_link.auth));
return 0;
...
case AUTH_METHOD_STATIC:
if (action || size) {
return -EINVAL;
}{...}
if (bt_mesh_prov_get()->static_val_len > auth_size) {
memcpy(prov_link.auth, bt_mesh_prov_get()->static_val, auth_size);
}{...} else {
memcpy(prov_link.auth + auth_size - bt_mesh_prov_get()->static_val_len,
bt_mesh_prov_get()->static_val, bt_mesh_prov_get()->static_val_len);
(void)memset(prov_link.auth, 0,
auth_size - bt_mesh_prov_get()->static_val_len);
}{...}
return 0;
...
case AUTH_METHOD_OUTPUT:
output = bt_mesh_prov_output_action(action);
if (!output) {
return -EINVAL;
}{...}
if (!(bt_mesh_prov_get()->output_actions & output)) {
return -EINVAL;
}{...}
if (size > bt_mesh_prov_get()->output_size) {
return -EINVAL;
}{...}
if (output == BLE_MESH_DISPLAY_STRING) {
unsigned char str[9] = {'\0'};
uint8_t i = 0U;
bt_mesh_rand(str, size);
for (i = 0U; i < size; i++) {
str[i] %= 36;
if (str[i] < 10) {
str[i] += '0';
}{...} else {
str[i] += 'A' - 10;
}{...}
}{...}
str[size] = '\0';
memcpy(prov_link.auth, str, size);
(void)memset(prov_link.auth + size, 0,
sizeof(prov_link.auth) - size);
return bt_mesh_prov_get()->output_string((char *)str);
}{...} else {
uint32_t div[8] = { 10, 100, 1000, 10000, 100000,
1000000, 10000000, 100000000
}{...};
uint32_t num = 0U;
bt_mesh_rand(&num, sizeof(num));
if (output == BLE_MESH_BLINK ||
output == BLE_MESH_BEEP ||
output == BLE_MESH_VIBRATE) {
/* ... */
num = (num % (div[size - 1] - 1)) + 1;
}{...} else {
num %= div[size - 1];
}{...}
sys_put_be32(num, &prov_link.auth[auth_size - 4]);
(void)memset(prov_link.auth, 0, auth_size - 4);
return bt_mesh_prov_get()->output_number(output, num);
}{...}
... case AUTH_METHOD_INPUT:
input = bt_mesh_prov_input_action(action);
if (!input) {
return -EINVAL;
}{...}
if (!(bt_mesh_prov_get()->input_actions & input)) {
return -EINVAL;
}{...}
if (size > bt_mesh_prov_get()->input_size) {
return -EINVAL;
}{...}
if (input == BLE_MESH_ENTER_STRING) {
bt_mesh_atomic_set_bit(prov_link.flags, WAIT_STRING);
}{...} else {
bt_mesh_atomic_set_bit(prov_link.flags, WAIT_NUMBER);
}{...}
return bt_mesh_prov_get()->input(input, size);
...
default:
return -EINVAL;...
}{...}
}{ ... }
static void prov_start(const uint8_t *data)
{
BT_INFO("Algorithm: 0x%02x", data[0]);
BT_INFO("Public Key: 0x%02x", data[1]);
BT_INFO("Auth Method: 0x%02x", data[2]);
BT_INFO("Auth Action: 0x%02x", data[3]);
BT_INFO("Auth Size: 0x%02x", data[4]);
prov_link.algorithm = data[0];
if (data[0] >= PROV_ALG_METHOD_MAX_NUM) {
BT_ERR("Unknown algorithm 0x%02x", data[0]);
close_link(PROV_ERR_NVAL_FMT);
return;
}{...}
/* ... */
#if CONFIG_BLE_MESH_PROV_EPA
if ((bt_mesh_prov_get()->oob_type & BIT(PROV_ONLY_OOB_AUTH_SUPPORT)) &&
((data[0] == PROV_ALG_P256_HMAC_SHA256 && data[2] == AUTH_METHOD_NO_OOB) ||
data[0] == PROV_ALG_P256_CMAC_AES128)) {
close_link(PROV_ERR_NVAL_FMT);
return;
}{...}
#endif/* ... */
if (data[1] != bt_mesh_prov_get()->oob_pub_key) {
BT_ERR("Invalid public key type: 0x%02x", data[1]);
close_link(PROV_ERR_NVAL_FMT);
return;
}{...}
prov_link.public_key = data[1];
memcpy(&prov_link.conf_inputs[12], data, 5);
prov_link.expect = PROV_PUB_KEY;
/* ... */
if (prov_link.public_key == PROV_OOB_PUB_KEY) {
bt_mesh_prov_get()->oob_pub_key_cb();
}{...}
if (prov_auth(data[2], data[3], data[4])) {
BT_ERR("Invalid authentication method: 0x%02x; "
"action: 0x%02x; size: 0x%02x",
data[2], data[3], data[4]);
close_link(PROV_ERR_NVAL_FMT);
}{...}
}{ ... }
static void send_confirm(void)
{
uint8_t *local_conf = NULL;
uint8_t conf_val_size = 0;
uint8_t rand_val_size = 0;
uint8_t conf_salt_size = 0;
uint8_t conf_key_size = 0;
conf_val_size = PROV_CONF_SIZE(&prov_link);
rand_val_size = PROV_RAND_SIZE(&prov_link);
conf_salt_size = PROV_CONF_SALT_SIZE(&prov_link);
conf_key_size = PROV_CONF_KEY_SIZE(&prov_link);
ARG_UNUSED(conf_salt_size);
ARG_UNUSED(conf_key_size);
PROV_BUF(cfm, (conf_val_size + 1));
BT_DBG("ConfInputs[0] %s", bt_hex(prov_link.conf_inputs, 64));
BT_DBG("ConfInputs[64] %s", bt_hex(&prov_link.conf_inputs[64], 64));
BT_DBG("ConfInputs[128] %s", bt_hex(&prov_link.conf_inputs[128], 17));
if (prov_link.algorithm == PROV_ALG_P256_CMAC_AES128) {
if (bt_mesh_prov_conf_salt(prov_link.conf_inputs, prov_link.conf_salt)) {
BT_ERR("Unable to generate confirmation salt");
close_link(PROV_ERR_UNEXP_ERR);
return;
}{...}
if (bt_mesh_prov_conf_key(prov_link.dhkey, prov_link.conf_salt,
prov_link.conf_key)) {
BT_ERR("Unable to generate confirmation key");
close_link(PROV_ERR_UNEXP_ERR);
return;
}{...}
}{...}
#if CONFIG_BLE_MESH_PROV_EPA
else {
if (bt_mesh_prov_conf_salt_epa(prov_link.conf_inputs, prov_link.conf_salt)) {
BT_ERR("Unable to generate confirmation salt(epa)");
close_link(PROV_ERR_UNEXP_ERR);
return;
}{...}
if (bt_mesh_prov_conf_key_epa(prov_link.dhkey, prov_link.auth,
prov_link.conf_salt, prov_link.conf_key)) {
BT_ERR("Unable to generate confirmation key(epa)");
close_link(PROV_ERR_UNEXP_ERR);
return;
}{...}
}{...}
#endif/* ... */
if (bt_mesh_rand(prov_link.rand, rand_val_size)) {
BT_ERR("Unable to generate random number");
close_link(PROV_ERR_UNEXP_ERR);
return;
}{...}
BT_DBG("ConfirmationSalt: %s", bt_hex(prov_link.conf_salt, conf_salt_size));
BT_DBG("ConfirmationKey: %s", bt_hex(prov_link.conf_key, conf_key_size));
BT_DBG("LocalRandom: %s", bt_hex(prov_link.rand, rand_val_size));
bt_mesh_prov_buf_init(&cfm, PROV_CONFIRM);
local_conf = net_buf_simple_add(&cfm, conf_val_size);
if (prov_link.algorithm == PROV_ALG_P256_CMAC_AES128) {
if (bt_mesh_prov_conf(prov_link.conf_key, prov_link.rand,
prov_link.auth, local_conf)) {
BT_ERR("Unable to generate confirmation value");
close_link(PROV_ERR_UNEXP_ERR);
return;
}{...}
}{...}
#if CONFIG_BLE_MESH_PROV_EPA
else {
if (bt_mesh_prov_conf_epa(prov_link.conf_key, prov_link.rand,
local_conf)) {
BT_ERR("Unable to generate confirmation value(epa)");
close_link(PROV_ERR_UNEXP_ERR);
return;
}{...}
}{...}
#endif/* ... */
if (!memcmp(prov_link.conf, local_conf, conf_val_size)) {
BT_ERR("Confirmation value is identical to ours, rejecting.");
prov_send_fail_msg(PROV_ERR_NVAL_FMT);
return;
}{...}
if (bt_mesh_prov_send(&prov_link, &cfm)) {
BT_ERR("Unable to send Provisioning Confirm");
return;
}{...}
prov_link.expect = PROV_RANDOM;
}{ ... }
static void send_input_complete(void)
{
PROV_BUF(buf, 1);
bt_mesh_prov_buf_init(&buf, PROV_INPUT_COMPLETE);
if (bt_mesh_prov_send(&prov_link, &buf)) {
BT_ERR("Failed to send Provisioning Input Complete");
}{...}
}{ ... }
int bt_mesh_input_number(uint32_t num)
{
uint8_t auth_size = 0;
auth_size = PROV_AUTH_SIZE(&prov_link);
BT_INFO("%u", num);
if (!bt_mesh_atomic_test_and_clear_bit(prov_link.flags, WAIT_NUMBER)) {
return -EINVAL;
}{...}
sys_put_be32(num, &prov_link.auth[auth_size - 4]);
send_input_complete();
if (!bt_mesh_atomic_test_bit(prov_link.flags, HAVE_DHKEY)) {
return 0;
}{...}
if (bt_mesh_atomic_test_and_clear_bit(prov_link.flags, SEND_CONFIRM)) {
send_confirm();
}{...}
return 0;
}{ ... }
int bt_mesh_input_string(const char *str)
{
BT_INFO("%s", str);
if (!bt_mesh_atomic_test_and_clear_bit(prov_link.flags, WAIT_STRING)) {
return -EINVAL;
}{...}
(void)memcpy(prov_link.auth, str, bt_mesh_prov_get()->input_size);
send_input_complete();
if (!bt_mesh_atomic_test_bit(prov_link.flags, HAVE_DHKEY)) {
return 0;
}{...}
if (bt_mesh_atomic_test_and_clear_bit(prov_link.flags, SEND_CONFIRM)) {
send_confirm();
}{...}
return 0;
}{ ... }
static void send_pub_key(void)
{
const uint8_t *key = NULL;
uint8_t dhkey[32] = {0};
PROV_BUF(buf, 65);
/* ... */
sys_memcpy_swap(buf.data, &prov_link.conf_inputs[17], 32);
sys_memcpy_swap(&buf.data[32], &prov_link.conf_inputs[49], 32);
if (bt_mesh_dh_key_gen(buf.data, dhkey)) {
BT_ERR("Unable to generate DHKey");
close_link(PROV_ERR_UNEXP_ERR);
return;
}{...}
sys_memcpy_swap(prov_link.dhkey, dhkey, 32);
BT_DBG("DHkey: %s", bt_hex(prov_link.dhkey, 32));
bt_mesh_atomic_set_bit(prov_link.flags, HAVE_DHKEY);
key = bt_mesh_pub_key_get();
if (!key) {
BT_ERR("No public key available");
close_link(PROV_ERR_UNEXP_ERR);
return;
}{...}
BT_DBG("Local Public Key: %s", bt_hex(key, 64));
bt_mesh_prov_buf_init(&buf, PROV_PUB_KEY);
sys_memcpy_swap(net_buf_simple_add(&buf, 32), key, 32);
sys_memcpy_swap(net_buf_simple_add(&buf, 32), &key[32], 32);
memcpy(&prov_link.conf_inputs[81], &buf.data[1], 64);
if (bt_mesh_prov_send(&prov_link, &buf)) {
BT_ERR("Failed to send Public Key");
return;
}{...}
prov_link.expect = PROV_CONFIRM;
}{ ... }
static int bt_mesh_calc_dh_key(void)
{
uint8_t pub_key[64] = {0};
uint8_t dhkey[32] = {0};
/* ... */
sys_memcpy_swap(&pub_key[0], &prov_link.conf_inputs[17], 32);
sys_memcpy_swap(&pub_key[32], &prov_link.conf_inputs[49], 32);
if (bt_mesh_dh_key_gen(pub_key, dhkey)) {
BT_ERR("Unable to generate DHKey");
close_link(PROV_ERR_UNEXP_ERR);
return -EIO;
}{...}
sys_memcpy_swap(prov_link.dhkey, dhkey, 32);
BT_DBG("DHkey: %s", bt_hex(prov_link.dhkey, 32));
bt_mesh_atomic_set_bit(prov_link.flags, HAVE_DHKEY);
if (bt_mesh_atomic_test_bit(prov_link.flags, WAIT_NUMBER) ||
bt_mesh_atomic_test_bit(prov_link.flags, WAIT_STRING)) {
return 0;
}{...}
if (bt_mesh_atomic_test_and_clear_bit(prov_link.flags, SEND_CONFIRM)) {
send_confirm();
}{...}
return 0;
}{ ... }
int bt_mesh_set_oob_pub_key(const uint8_t pub_key_x[32],
const uint8_t pub_key_y[32],
const uint8_t pri_key[32])
{
if (!pub_key_x || !pub_key_y || !pri_key) {
BT_ERR("%s, Invalid parameter", __func__);
return -EINVAL;
}{...}
/* ... */
sys_memcpy_swap(&prov_link.conf_inputs[81], pub_key_x, 32);
sys_memcpy_swap(&prov_link.conf_inputs[81] + 32, pub_key_y, 32);
bt_mesh_set_private_key(pri_key);
bt_mesh_atomic_set_bit(prov_link.flags, OOB_PUB_KEY);
if (!bt_mesh_atomic_test_bit(prov_link.flags, REMOTE_PUB_KEY)) {
return 0;
}{...}
return bt_mesh_calc_dh_key();
}{ ... }
static void prov_pub_key(const uint8_t *data)
{
BT_DBG("Remote Public Key: %s", bt_hex(data, 64));
/* ... */
if (!bt_mesh_check_public_key(data)) {
BT_ERR("Invalid public key");
close_link(PROV_ERR_UNEXP_PDU);
return;
}{...}
memcpy(&prov_link.conf_inputs[17], data, 64);
bt_mesh_atomic_set_bit(prov_link.flags, REMOTE_PUB_KEY);
if (prov_link.public_key == PROV_NO_OOB_PUB_KEY) {
send_pub_key();
}{...} else {
prov_link.expect = PROV_CONFIRM;
}{...}
}{ ... }
static void prov_input_complete(const uint8_t *data)
{}{ ... }
static void prov_confirm(const uint8_t *data)
{
uint8_t conf_val_size = 0;
conf_val_size = PROV_CONF_SIZE(&prov_link);
BT_DBG("Remote Confirm: %s", bt_hex(data, conf_val_size));
memcpy(prov_link.conf, data, conf_val_size);
if (!bt_mesh_atomic_test_bit(prov_link.flags, HAVE_DHKEY)) {
#if CONFIG_BLE_MESH_PB_ADV
bt_mesh_prov_clear_tx(&prov_link, true);
#endif
bt_mesh_atomic_set_bit(prov_link.flags, SEND_CONFIRM);
if (prov_link.public_key == PROV_OOB_PUB_KEY &&
bt_mesh_atomic_test_bit(prov_link.flags, OOB_PUB_KEY)) {
bt_mesh_calc_dh_key();
}{...}
}{...} else {
send_confirm();
}{...}
}{ ... }
static void prov_random(const uint8_t *data)
{
uint8_t conf_verify[32] = {0};
uint8_t rand_val_size = 0;
uint8_t conf_val_size = 0;
rand_val_size = PROV_RAND_SIZE(&prov_link);
conf_val_size = PROV_CONF_SIZE(&prov_link);
PROV_BUF(rnd, rand_val_size);
BT_INFO("Remote Random: %s", bt_hex(data, rand_val_size));
if (prov_link.algorithm == PROV_ALG_P256_CMAC_AES128) {
if (bt_mesh_prov_conf(prov_link.conf_key, data, prov_link.auth, conf_verify)) {
BT_ERR("Unable to calculate confirmation verification");
close_link(PROV_ERR_UNEXP_ERR);
return;
}{...}
}{...}
#if CONFIG_BLE_MESH_PROV_EPA
else {
if (bt_mesh_prov_conf_epa(prov_link.conf_key, data, conf_verify)) {
BT_ERR("Unable to calculate confirmation verification");
close_link(PROV_ERR_UNEXP_ERR);
return;
}{...}
}{...}
#endif/* ... */
if (memcmp(conf_verify, prov_link.conf, conf_val_size)) {
BT_ERR("Invalid confirmation value");
BT_ERR("Received: %s", bt_hex(prov_link.conf, conf_val_size));
BT_ERR("Calculated: %s", bt_hex(conf_verify, conf_val_size));
close_link(PROV_ERR_CFM_FAILED);
return;
}{...}
bt_mesh_prov_buf_init(&rnd, PROV_RANDOM);
net_buf_simple_add_mem(&rnd, prov_link.rand, rand_val_size);
if (bt_mesh_prov_send(&prov_link, &rnd)) {
BT_ERR("Failed to send Provisioning Random");
return;
}{...}
if (prov_link.algorithm == PROV_ALG_P256_CMAC_AES128) {
if (bt_mesh_prov_salt(prov_link.conf_salt, data, prov_link.rand,
prov_link.prov_salt)) {
BT_ERR("Failed to generate provisioning salt");
close_link(PROV_ERR_UNEXP_ERR);
return;
}{...}
}{...}
#if CONFIG_BLE_MESH_PROV_EPA
else {
if (bt_mesh_prov_salt_epa(prov_link.conf_salt, data, prov_link.rand,
prov_link.prov_salt)) {
BT_ERR("Failed to generate provisioning salt");
close_link(PROV_ERR_UNEXP_ERR);
return;
}{...}
}{...}
#endif/* ... */
BT_DBG("ProvisioningSalt: %s", bt_hex(prov_link.prov_salt, 16));
prov_link.expect = PROV_DATA;
}{ ... }
static inline bool is_pb_gatt(void)
{
#if CONFIG_BLE_MESH_PB_GATT
return !!prov_link.conn;
#else
return false;
#endif
}{ ... }
static void prov_data(const uint8_t *data)
{
uint8_t session_key[16] = {0};
bool identity_enable = false;
uint8_t dev_key[16] = {0};
uint8_t nonce[13] = {0};
uint32_t iv_index = 0U;
uint16_t net_idx = 0U;
uint8_t pdu[25] = {0};
uint16_t addr = 0U;
uint8_t flags = 0U;
PROV_BUF(msg, 1);
int err = 0;
err = bt_mesh_session_key(prov_link.dhkey, prov_link.prov_salt, session_key);
if (err) {
BT_ERR("Unable to generate session key");
close_link(PROV_ERR_UNEXP_ERR);
return;
}{...}
BT_DBG("SessionKey: %s", bt_hex(session_key, 16));
err = bt_mesh_prov_nonce(prov_link.dhkey, prov_link.prov_salt, nonce);
if (err) {
BT_ERR("Unable to generate session nonce");
close_link(PROV_ERR_UNEXP_ERR);
return;
}{...}
BT_DBG("Nonce: %s", bt_hex(nonce, 13));
err = bt_mesh_prov_decrypt(session_key, nonce, data, pdu);
if (err) {
BT_ERR("Unable to decrypt provisioning data");
close_link(PROV_ERR_DECRYPT);
return;
}{...}
net_idx = sys_get_be16(&pdu[16]);
flags = pdu[18];
iv_index = sys_get_be32(&pdu[19]);
addr = sys_get_be16(&pdu[23]);
BT_DBG("net_idx %u iv_index 0x%08x, addr 0x%04x",
net_idx, iv_index, addr);
#if CONFIG_BLE_MESH_RPR_SRV
if (bt_mesh_atomic_test_bit(prov_link.flags, PB_NPPI)) {
uint8_t reason = 0;
if (bt_mesh_rpr_srv_nppi_check(prov_link.pb_remote_nppi, pdu, net_idx,
iv_index, addr, &reason) == false) {
close_link(reason);
return;
}{...}
}{...}
#endif/* ... */
err = bt_mesh_dev_key(prov_link.dhkey, prov_link.prov_salt, dev_key);
if (err) {
BT_ERR("Unable to generate device key");
close_link(PROV_ERR_UNEXP_ERR);
return;
}{...}
BT_DBG("DevKey: %s", bt_hex(dev_key, 16));
#if CONFIG_BLE_MESH_RPR_SRV
if (bt_mesh_atomic_test_bit(prov_link.flags, PB_NPPI)) {
err = bt_mesh_rpr_srv_store_nppi_data(prov_link.pb_remote_uuid,
pdu, net_idx, flags,
iv_index, addr, dev_key);
if (err) {
close_link(PROV_ERR_UNEXP_ERR);
return;
}{...}
}{...}
#endif/* ... */
bt_mesh_prov_buf_init(&msg, PROV_COMPLETE);
if (bt_mesh_prov_send(&prov_link, &msg)) {
BT_ERR("Failed to send Provisioning Complete");
return;
}{...}
prov_link.expect = 0U;
#if CONFIG_BLE_MESH_RPR_SRV
if (bt_mesh_atomic_test_bit(prov_link.flags, PB_NPPI)) {
return;
}{...}
#endif/* ... */
if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER)) {
identity_enable = is_pb_gatt();
}{...} else {
identity_enable = false;
}{...}
err = bt_mesh_provision(pdu, net_idx, flags, iv_index, addr, dev_key);
if (err) {
BT_ERR("Failed to provision (err %d)", err);
return;
}{...}
/* ... */
if (IS_ENABLED(CONFIG_BLE_MESH_GATT_PROXY_SERVER) && identity_enable) {
bt_mesh_proxy_identity_enable();
}{...}
}{ ... }
static void prov_complete(const uint8_t *data)
{}{ ... }
static void prov_failed(const uint8_t *data)
{
BT_WARN("Error: 0x%02x", data[0]);
#if CONFIG_BLE_MESH_RPR_SRV
if (bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE)) {
prov_link.pb_remote_cbd = true;
prov_link.pb_remote_reset = true;
close_link(data[0]);
return;
}{...}
#endif/* ... */
}{ ... }
static const struct {
void (*func)(const uint8_t *data);
bool pb_remote;
}{ ... } prov_handlers[] = {
{ prov_invite, false },
{ prov_capabilities, true },
{ prov_start, false },
{ prov_pub_key, true },
{ prov_input_complete, true },
{ prov_confirm, true },
{ prov_random, true },
{ prov_data, false },
{ prov_complete, true },
{ prov_failed, true },
#if CONFIG_BLE_MESH_CERT_BASED_PROV
{ bt_mesh_prov_record_req, false },
{ bt_mesh_prov_record_rsp, false },
{ bt_mesh_prov_records_get, false },
{ bt_mesh_prov_records_list, false },/* ... */
#endif
}{...};
#if CONFIG_BLE_MESH_PB_ADV
static void link_open(struct prov_rx *rx, struct net_buf_simple *buf)
{
BT_DBG("len %u", buf->len);
if (buf->len < 16) {
BT_ERR("Too short bearer open message (len %u)", buf->len);
return;
}{...}
if (bt_mesh_atomic_test_bit(prov_link.flags, LINK_ACTIVE)) {
if (prov_link.link_id == rx->link_id && prov_link.expect == PROV_INVITE) {
BT_DBG("Resending link ack");
bt_mesh_prov_bearer_ctl_send(&prov_link, LINK_ACK, NULL, 0);
}{...} else {
BT_INFO("Ignoring bearer open: link already active");
}{...}
return;
}{...}
if (memcmp(buf->data, bt_mesh_prov_get()->uuid, 16)) {
BT_DBG("Bearer open message not for us");
return;
}{...}
if (bt_mesh_prov_get()->link_open) {
bt_mesh_prov_get()->link_open(BLE_MESH_PROV_ADV);
}{...}
prov_link.link_id = rx->link_id;
bt_mesh_atomic_set_bit(prov_link.flags, LINK_ACTIVE);
net_buf_simple_reset(prov_link.rx.buf);
#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN
bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_ADD,
BLE_MESH_EXCEP_LIST_TYPE_MESH_LINK_ID,
&prov_link.link_id);/* ... */
#endif
bt_mesh_prov_bearer_ctl_send(&prov_link, LINK_ACK, NULL, 0);
#if CONFIG_BLE_MESH_CERT_BASED_PROV
if (PROV_REC_SUPPORT(bt_mesh_prov_get()->oob_info)) {
prov_link.expect = PROV_REC_EXP;
}{...} else
#endif
{
prov_link.expect = PROV_INVITE;
}{...}
}{ ... }
static void link_ack(struct prov_rx *rx, struct net_buf_simple *buf)
{
BT_DBG("len %u", buf->len);
#if CONFIG_BLE_MESH_RPR_SRV
if (bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE)) {
if (bt_mesh_atomic_test_and_clear_bit(prov_link.flags, PBR_OPENING)) {
BT_INFO("PB-Remote, receive Link ACK");
bt_mesh_prov_clear_tx(&prov_link, true);
bt_mesh_rpr_srv_recv_link_ack(prov_link.pb_remote_uuid, true);
}{...} else {
BT_INFO("Link ACK for PB-Remote already received");
}{...}
}{...}
#endif/* ... */
}{ ... }
static void link_close(struct prov_rx *rx, struct net_buf_simple *buf)
{
uint8_t reason = 0;
BT_DBG("len %u", buf->len);
if (buf->len != 1) {
BT_ERR("Invalid Link Close length %d", buf->len);
return;
}{...}
reason = net_buf_simple_pull_u8(buf);
#if CONFIG_BLE_MESH_RPR_SRV
if (bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE)) {
prov_link.pb_remote_cbd = true;
prov_link.pb_remote_reset = true;
close_link(reason);
return;
}{...}
#endif/* ... */
reset_adv_link(&prov_link, reason);
}{ ... }
static void gen_prov_ctl(struct prov_rx *rx, struct net_buf_simple *buf)
{
BT_DBG("op 0x%02x len %u", BEARER_CTL(rx->gpc), buf->len);
switch (BEARER_CTL(rx->gpc)) {
case LINK_OPEN:
#if CONFIG_BLE_MESH_RPR_SRV
if (bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE)) {
return;
}{...}
#endif/* ... */
link_open(rx, buf);
break;
...
case LINK_ACK:
if (!bt_mesh_atomic_test_bit(prov_link.flags, LINK_ACTIVE)) {
return;
}{...}
link_ack(rx, buf);
break;
...
case LINK_CLOSE:
if (!bt_mesh_atomic_test_bit(prov_link.flags, LINK_ACTIVE)) {
return;
}{...}
link_close(rx, buf);
break;
...
default:
BT_ERR("Unknown bearer opcode: 0x%02x", BEARER_CTL(rx->gpc));
return;...
}{...}
}{ ... }
static void prov_msg_recv(void)
{
uint8_t err_code = 0;
uint8_t type = 0;
if (bt_mesh_atomic_test_bit(prov_link.flags, LINK_INVALID)) {
BT_WARN("Unexpected msg 0x%02x on invalidated link", type);
close_link(PROV_ERR_UNEXP_PDU);
return;
}{...}
/* ... */
if (bt_mesh_atomic_test_bit(prov_link.flags, LINK_CLOSING)) {
BT_WARN("Link is closing, unexpected msg 0x%02x", type);
return;
}{...}
if (!bt_mesh_fcs_check(prov_link.rx.buf, prov_link.rx.fcs)) {
BT_ERR("Incorrect FCS");
return;
}{...}
type = net_buf_simple_pull_u8(prov_link.rx.buf);
BT_DBG("type 0x%02x len %u", type, prov_link.rx.buf->len);
/* ... */
if (type >= ARRAY_SIZE(prov_handlers)) {
BT_ERR("Unknown provisioning PDU type 0x%02x", type);
prov_send_fail_msg(PROV_ERR_NVAL_PDU);
return;
}{...}
if (type != PROV_FAILED && type != prov_link.expect) {
#if CONFIG_BLE_MESH_CERT_BASED_PROV
if (prov_link.expect == PROV_REC_EXP &&
(type == PROV_INVITE || type == PROV_REC_GET || type == PROV_REC_REQ)) {
if (prov_link.invite_recv) {
BT_ERR("%s, Provisioning invite PDU already received", __func__);
close_link(PROV_ERR_UNEXP_PDU);
return;
}{...}
}{...} else
#endif
{
BT_WARN("Unexpected msg 0x%02x != 0x%02x", type, prov_link.expect);
close_link(PROV_ERR_UNEXP_PDU);
return;
}{...}
}{...}
if (!bt_mesh_prov_pdu_check(type, prov_link.rx.buf->len, &err_code)) {
close_link(err_code);
return;
}{...}
bt_mesh_gen_prov_ack_send(&prov_link, prov_link.rx.id);
prov_link.rx.prev_id = prov_link.rx.id;
prov_link.rx.id = 0U;
k_delayed_work_submit(&prov_link.prot_timer, PROTOCOL_TIMEOUT);
#if CONFIG_BLE_MESH_RPR_SRV
if (bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE)) {
if (prov_handlers[type].pb_remote) {
BT_INFO("PB-Remote, receive prov pdu 0x%02x", type);
/* ... */
bt_mesh_rpr_srv_send_pdu_report(prov_link.pb_remote_uuid, type,
prov_link.rx.buf);
}{...}
return;
}{...}
#endif/* ... */
prov_handlers[type].func(prov_link.rx.buf->data);
}{ ... }
static void gen_prov_cont(struct prov_rx *rx, struct net_buf_simple *buf)
{
bool close = false;
if (!bt_mesh_gen_prov_cont(&prov_link, buf, rx, &close)) {
if (close) {
close_link(PROV_ERR_NVAL_FMT);
}{...}
return;
}{...}
prov_msg_recv();
}{ ... }
static void gen_prov_ack(struct prov_rx *rx, struct net_buf_simple *buf)
{
BT_DBG("len %u", buf->len);
if (!prov_link.tx.buf[0]) {
return;
}{...}
#if CONFIG_BLE_MESH_RPR_SRV
if (bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE)) {
if (prov_link.tx.id == 0) {
return;
}{...}
if (rx->xact_id == prov_link.tx.id - 1) {
bt_mesh_prov_clear_tx(&prov_link, true);
BT_INFO("PB-Remote, receive Transaction ACK");
bt_mesh_rpr_srv_send_outbound_report(prov_link.pb_remote_uuid, true);
}{...}
return;
}{...}
#endif/* ... */
if (rx->xact_id == prov_link.tx.id) {
bt_mesh_prov_clear_tx(&prov_link, true);
}{...}
}{ ... }
static void gen_prov_start(struct prov_rx *rx, struct net_buf_simple *buf)
{
bool close = false;
if (!bt_mesh_gen_prov_start(&prov_link, buf, rx, &close)) {
if (close) {
close_link(PROV_ERR_NVAL_FMT);
}{...}
return;
}{...}
prov_msg_recv();
}{ ... }
static const struct {
void (*func)(struct prov_rx *rx, struct net_buf_simple *buf);
bool require_link;
uint8_t min_len;
}{ ... } gen_prov[] = {
{ gen_prov_start, true, 3 },
{ gen_prov_ack, true, 0 },
{ gen_prov_cont, true, 0 },
{ gen_prov_ctl, false, 0 },
}{...};
static void gen_prov_recv(struct prov_rx *rx, struct net_buf_simple *buf)
{
if (buf->len < gen_prov[GPCF(rx->gpc)].min_len) {
BT_ERR("Too short GPC message type %u", GPCF(rx->gpc));
return;
}{...}
if (!bt_mesh_atomic_test_bit(prov_link.flags, LINK_ACTIVE) &&
gen_prov[GPCF(rx->gpc)].require_link) {
BT_DBG("Ignoring message that requires active link");
return;
}{...}
gen_prov[GPCF(rx->gpc)].func(rx, buf);
}{ ... }
void bt_mesh_pb_adv_recv(struct net_buf_simple *buf)
{
struct prov_rx rx = {0};
if (!bt_mesh_atomic_test_bit(prov_link.flags, LINK_ACTIVE) &&
bt_mesh_is_provisioned()) {
BT_DBG("Ignoring provisioning PDU - already provisioned");
return;
}{...}
if (buf->len < 6) {
BT_WARN("Too short provisioning packet (len %u)", buf->len);
return;
}{...}
rx.link_id = net_buf_simple_pull_be32(buf);
rx.xact_id = net_buf_simple_pull_u8(buf);
rx.gpc = net_buf_simple_pull_u8(buf);
BT_DBG("link_id 0x%08x xact_id %u", rx.link_id, rx.xact_id);
if (bt_mesh_atomic_test_bit(prov_link.flags, LINK_ACTIVE) &&
prov_link.link_id != rx.link_id) {
BT_DBG("Ignoring mesh beacon for unknown link");
return;
}{...}
gen_prov_recv(&rx, buf);
}{ ... }
#endif/* ... */
#if CONFIG_BLE_MESH_PB_GATT
int bt_mesh_pb_gatt_recv(struct bt_mesh_conn *conn, struct net_buf_simple *buf)
{
uint8_t err_code = 0U;
uint8_t type = 0U;
BT_DBG("%u bytes: %s", buf->len, bt_hex(buf->data, buf->len));
if (prov_link.conn != conn) {
BT_WARN("Data for unexpected connection");
return -ENOTCONN;
}{...}
if (buf->len < 1) {
BT_WARN("Too short provisioning packet (len %u)", buf->len);
return -EINVAL;
}{...}
/* ... */
if (bt_mesh_atomic_test_bit(prov_link.flags, LINK_INVALID)) {
BT_WARN("Unexpected msg 0x%02x on invalid link", type);
prov_send_fail_msg(PROV_ERR_UNEXP_PDU);
return -EINVAL;
}{...}
/* ... */
type = net_buf_simple_pull_u8(buf);
if (type >= ARRAY_SIZE(prov_handlers)) {
BT_ERR("Unknown provisioning PDU type 0x%02x", type);
prov_send_fail_msg(PROV_ERR_NVAL_PDU);
return -EINVAL;
}{...}
if (type != PROV_FAILED && type != prov_link.expect) {
#if CONFIG_BLE_MESH_CERT_BASED_PROV
if (prov_link.expect == PROV_REC_EXP &&
(type == PROV_INVITE || type == PROV_REC_GET || type == PROV_REC_REQ)) {
if (prov_link.invite_recv) {
BT_ERR("%s, Provisioning invite PDU already received", __func__);
close_link(PROV_ERR_UNEXP_PDU);
return -EINVAL;
}{...}
}{...} else
#endif
{
BT_WARN("Unexpected msg 0x%02x != 0x%02x", type, prov_link.expect);
close_link(PROV_ERR_UNEXP_PDU);
return -EINVAL;
}{...}
}{...}
if (!bt_mesh_prov_pdu_check(type, buf->len, &err_code)) {
close_link(err_code);
return -EINVAL;
}{...}
k_delayed_work_submit(&prov_link.prot_timer, PROTOCOL_TIMEOUT);
#if CONFIG_BLE_MESH_RPR_SRV
if (bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE)) {
if (prov_handlers[type].pb_remote) {
BT_INFO("PB-Remote, receive prov pdu 0x%02x by GATT %s",
type, bt_hex(buf->data, buf->len));
/* ... */
bt_mesh_rpr_srv_send_pdu_report(prov_link.pb_remote_uuid, type, buf);
}{...}
return 0;
}{...}
#endif/* ... */
prov_handlers[type].func(buf->data);
return 0;
}{ ... }
int bt_mesh_pb_gatt_open(struct bt_mesh_conn *conn)
{
BT_DBG("conn %p", conn);
if (!bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE) &&
bt_mesh_atomic_test_and_set_bit(prov_link.flags, LINK_ACTIVE)) {
BT_ERR("Link is busy");
return -EBUSY;
}{...}
prov_link.conn = bt_mesh_conn_ref(conn);
#if CONFIG_BLE_MESH_CERT_BASED_PROV
if (PROV_REC_SUPPORT(bt_mesh_prov_get()->oob_info)) {
prov_link.expect = PROV_REC_EXP;
}{...} else
#endif
{
prov_link.expect = PROV_INVITE;
}{...}
if (bt_mesh_prov_get()->link_open) {
bt_mesh_prov_get()->link_open(BLE_MESH_PROV_GATT);
}{...}
return 0;
}{ ... }
int bt_mesh_pb_gatt_close(struct bt_mesh_conn *conn, uint8_t reason)
{
BT_DBG("conn %p", conn);
if (prov_link.conn != conn) {
BT_ERR("Not connected");
return -ENOTCONN;
}{...}
#if CONFIG_BLE_MESH_RPR_SRV && CONFIG_BLE_MESH_PB_GATT
if (bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE)) {
prov_link.pb_remote_cbd = true;
prov_link.pb_remote_reset = true;
prov_link.pb_remote_close(&prov_link, reason);
}{...}
#endif/* ... */
if (bt_mesh_prov_get()->link_close) {
bt_mesh_prov_get()->link_close(BLE_MESH_PROV_GATT, reason);
}{...}
reset_state();
return 0;
}{ ... }
#endif/* ... */
bool bt_mesh_prov_active(void)
{
return bt_mesh_atomic_test_bit(prov_link.flags, LINK_ACTIVE);
}{ ... }
static void protocol_timeout(struct k_work *work)
{
BT_WARN("Protocol timeout");
#if CONFIG_BLE_MESH_PB_GATT
if (prov_link.conn) {
#if CONFIG_BLE_MESH_GATT_PROXY_CLIENT && CONFIG_BLE_MESH_RPR_SRV
if (bt_mesh_atomic_test_bit(prov_link.flags, PB_REMOTE)) {
prov_link.pb_remote_reset = true;
bt_mesh_gattc_disconnect(prov_link.conn);
return;
}{...}
#endif/* ... */
bt_mesh_pb_gatt_close(prov_link.conn, CLOSE_REASON_TIMEOUT);
return;
}{...}
#endif/* ... */
#if CONFIG_BLE_MESH_PB_ADV
uint8_t reason = CLOSE_REASON_TIMEOUT;
prov_link.rx.seg = 0U;
bt_mesh_prov_bearer_ctl_send(&prov_link, LINK_CLOSE, &reason, sizeof(reason));/* ... */
#endif
}{ ... }
int bt_mesh_prov_init(void)
{
const uint8_t *key = NULL;
if (bt_mesh_prov_get() == NULL) {
BT_ERR("No provisioning context provided");
return -EINVAL;
}{...}
if (bt_mesh_prov_get()->static_val_len > BLE_MESH_PROV_STATIC_OOB_MAX_LEN ||
bt_mesh_prov_get()->output_size > BLE_MESH_PROV_OUTPUT_OOB_MAX_LEN ||
bt_mesh_prov_get()->input_size > BLE_MESH_PROV_INPUT_OOB_MAX_LEN) {
BT_ERR("Invalid authentication oob length");
return -EINVAL;
}{...}
__ASSERT(bt_mesh_prov_get()->uuid, "Device UUID not initialized");
key = bt_mesh_pub_key_get();
if (!key) {
BT_ERR("Failed to generate public key");
return -EIO;
}{...}
k_delayed_work_init(&prov_link.prot_timer, protocol_timeout);
#if CONFIG_BLE_MESH_PB_ADV
bt_mesh_prov_retransmit_init(&prov_link);
#endif
reset_state();
#if CONFIG_BLE_MESH_PB_ADV
bt_mesh_mutex_create(&prov_link.buf_lock);
#endif
#if CONFIG_BLE_MESH_CERT_BASED_PROV
if (PROV_REC_SUPPORT(bt_mesh_prov_get()->oob_info)) {
int err = bt_mesh_node_cert_based_prov_init();
if (err) {
BT_ERR("Loading provisioning records failed!");
return err;
}{...}
}{...}
#endif/* ... */
return 0;
}{ ... }
#if CONFIG_BLE_MESH_DEINIT
int bt_mesh_prov_deinit(void)
{
if (bt_mesh_prov_get() == NULL) {
BT_ERR("No provisioning context provided");
return -EINVAL;
}{...}
k_delayed_work_free(&prov_link.prot_timer);
#if CONFIG_BLE_MESH_PB_ADV
bt_mesh_prov_clear_tx(&prov_link, true);
k_delayed_work_free(&prov_link.tx.retransmit);
#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN
bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_REMOVE,
BLE_MESH_EXCEP_LIST_TYPE_MESH_LINK_ID,
&prov_link.link_id);/* ... */
#endif
bt_mesh_mutex_free(&prov_link.buf_lock);/* ... */
#endif
#if CONFIG_BLE_MESH_CERT_BASED_PROV
if (PROV_REC_SUPPORT(bt_mesh_prov_get()->oob_info)) {
bt_mesh_node_cert_based_prov_deinit();
}{...}
#endif/* ... */
(void)memset(&prov_link, 0, sizeof(prov_link));
return 0;
}{ ... }
/* ... */#endif
void bt_mesh_prov_complete(uint16_t net_idx, const uint8_t net_key[16],
uint16_t addr, uint8_t flags, uint32_t iv_index)
{
if (bt_mesh_prov_get()->complete) {
bt_mesh_prov_get()->complete(net_idx, net_key, addr, flags, iv_index);
}{...}
}{ ... }
void bt_mesh_prov_reset(void)
{
if (bt_mesh_prov_get()->reset) {
bt_mesh_prov_get()->reset();
}{...}
}{ ... }
#if CONFIG_BLE_MESH_RPR_SRV
void bt_mesh_rpr_srv_reset_prov_link(struct bt_mesh_prov_link *link, uint8_t reason)
{
#if CONFIG_BLE_MESH_USE_DUPLICATE_SCAN
bt_mesh_update_exceptional_list(BLE_MESH_EXCEP_LIST_SUB_CODE_REMOVE,
BLE_MESH_EXCEP_LIST_TYPE_MESH_LINK_ID,
&link->link_id);/* ... */
#endif
reset_state();
}{...}
int bt_mesh_rpr_srv_nppi_pdu_recv(uint8_t type, const uint8_t *data)
{
if (!bt_mesh_atomic_test_bit(prov_link.flags, PB_NPPI)) {
BT_ERR("Not a NPPI provisioning link");
return -EINVAL;
}{...}
if (type != prov_link.expect) {
BT_ERR("NPPI, unexpected msg 0x%02x != 0x%02x", type, prov_link.expect);
return -EINVAL;
}{...}
prov_handlers[type].func(data);
return 0;
}{...}
/* ... */#endif
/* ... */
#endif