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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
65
66
70
71
72
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
119
120
121
122
123
124
125
126
130
131
135
136
140
141
145
146
150
151
164
165
194
195
196
197
198
199
200
210
211
215
216
217
221
222
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
247
248
249
250
257
258
259
260
261
262
263
273
274
275
276
284
285
286
287
288
289
290
298
299
300
301
302
303
308
309
310
311
312
313
314
315
316
321
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
351
352
353
354
355
356
365
366
367
368
369
370
371
372
373
374
375
376
379
380
381
382
383
386
387
392
393
394
395
396
404
405
417
418
424
425
429
430
434
435
436
437
438
439
440
441
447
448
449
450
451
452
453
456
457
461
462
463
464
465
466
471
472
473
474
480
481
482
483
484
485
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
512
513
517
518
525
526
527
532
533
534
535
536
537
538
539
540
541
542
543
548
549
550
551
552
553
554
555
556
557
563
564
565
566
567
568
569
570
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
594
595
596
597
598
599
606
607
608
609
610
611
612
613
614
615
616
621
622
626
629
630
631
632
640
641
642
645
646
647
648
649
650
651
652
653
654
655
656
657
661
662
665
666
667
668
669
670
671
672
675
676
677
678
679
680
685
686
687
688
689
690
695
696
697
698
699
700
704
705
708
709
710
711
712
713
714
715
716
717
718
719
720
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
743
744
749
750
755
756
757
758
759
762
763
767
768
769
770
771
772
773
774
775
779
780
781
782
783
784
785
786
789
790
791
792
793
794
795
796
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
827
828
829
832
833
838
839
840
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
862
863
864
865
866
867
868
869
870
873
874
875
876
877
878
879
880
881
882
883
884
885
888
889
892
893
898
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
920
921
922
923
924
925
931
932
933
934
935
936
937
941
942
946
947
948
949
954
955
959
960
961
962
972
973
979
980
981
982
983
984
985
986
987
988
989
993
994
995
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1039
1040
1041
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1055
1056
1057
1058
1059
1060
1062
1064
1066
1068
1070
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1084
1085
1089
1090
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1115
1116
1121
1122
1123
1124
1127
1128
1129
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1146
1147
1150
1151
1152
1153
1154
1155
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1172
1173
1174
1175
1176
1177
1178
1179
1180
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1199
1200
1201
1207
1208
1209
1210
1211
1212
1216
1217
1220
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1244
1245
1246
1247
1251
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1293
1294
1297
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1327
1328
1331
1332
1333
1334
1338
1339
1340
1341
1342
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1362
1363
1367
1368
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1426
1427
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1479
1480
1483
1484
1488
1489
1490
1491
1492
1493
1494
1495
1498
1499
1500
1501
1502
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1545
1546
1547
1548
1549
1553
1554
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1578
1579
1580
1581
1582
1583
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1599
1600
1601
1602
1605
1606
1607
1608
1609
1610
1611
1612
1613
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1652
1653
1654
1655
1659
1660
1663
1664
1665
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1695
1696
1697
1698
1707
1708
1709
1710
1711
1719
1720
1728
1729
1730
1731
1732
1733
1736
1737
1743
1744
1745
1746
1747
1748
1749
1752
1753
1759
1760
1761
1762
1763
1764
1765
1768
1769
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1792
1793
1796
1797
1798
1799
1800
/* ... */
#include <string.h>
#include <errno.h>
#include "crypto.h"
#include "tag.h"
#include "adv.h"
#include "mesh.h"
#include "lpn.h"
#include "rpl.h"
#include "friend.h"
#include "access.h"
#include "foundation.h"
#include "settings.h"
#include "transport.h"
#include "mesh/main.h"
#include "fast_prov.h"
#include "mesh/common.h"
#include "mesh/cfg_srv.h"
#include "heartbeat.h"18 includes
#if CONFIG_BLE_MESH_V11_SUPPORT
#include "mesh_v1.1/utils.h"
#endif
/* ... */
_Static_assert(CONFIG_BLE_MESH_ADV_BUF_COUNT >= (CONFIG_BLE_MESH_TX_SEG_MAX + 3),
"Too small BLE Mesh adv buffer count");
#define AID_MASK ((uint8_t)(BIT_MASK(6)))
#define SEG(data) ((data)[0] >> 7)
#define AKF(data) (((data)[0] >> 6) & 0x01)
#define AID(data) ((data)[0] & AID_MASK)
#define ASZMIC(data) (((data)[1] >> 7) & 1)
#define APP_MIC_LEN(aszmic) ((aszmic) ? BLE_MESH_MIC_LONG : BLE_MESH_MIC_SHORT)
#define UNSEG_HDR(akf, aid) ((akf << 6) | (aid & AID_MASK))
#define SEG_HDR(akf, aid) (UNSEG_HDR(akf, aid) | 0x80)
#define BLOCK_COMPLETE(seg_n) (uint32_t)(((uint64_t)1 << (seg_n + 1)) - 1)
#define SEQ_AUTH(iv_index, seq) (((uint64_t)iv_index) << 24 | (uint64_t)seq)
#define SEG_RETRANSMIT_ATTEMPTS 4
/* ... */
#define SEG_RETRANSMIT_TIMEOUT_UNICAST(tx) (K_MSEC(400) + 50 * (tx)->ttl)
/* ... */
#define SEG_RETRANSMIT_TIMEOUT_GROUP K_MSEC(50)
#define SEG_RETRANSMIT_TIMEOUT(tx) \
(BLE_MESH_ADDR_IS_UNICAST((tx)->dst) ? \
SEG_RETRANSMIT_TIMEOUT_UNICAST(tx) : \
SEG_RETRANSMIT_TIMEOUT_GROUP)...
#define BUF_TIMEOUT K_NO_WAIT15 defines
static struct seg_tx {
struct bt_mesh_subnet *sub;
struct net_buf *seg[CONFIG_BLE_MESH_TX_SEG_MAX];
uint64_t seq_auth;
uint16_t dst;
uint8_t xmit;
uint8_t seg_n:5,
new_key:1;
uint8_t nack_count;
uint8_t ttl;
uint8_t seg_pending;
uint8_t attempts;
uint8_t cred;
uint8_t tag;
const struct bt_mesh_send_cb *cb;
void *cb_data;
struct k_delayed_work rtx_timer;
}{ ... } seg_tx[CONFIG_BLE_MESH_TX_SEG_MSG_COUNT];
static struct seg_rx {
struct bt_mesh_subnet *sub;
uint64_t seq_auth;
uint8_t seg_n:5,
ctl:1,
in_use:1,
obo:1;
uint8_t hdr;
uint8_t ttl;
uint16_t src;
uint16_t dst;
uint32_t block;
uint32_t last;
struct k_delayed_work ack_timer;
struct net_buf_simple buf;
}{ ... } seg_rx[CONFIG_BLE_MESH_RX_SEG_MSG_COUNT] = {
[0 ... (CONFIG_BLE_MESH_RX_SEG_MSG_COUNT - 1)] = {
.buf.size = CONFIG_BLE_MESH_RX_SDU_MAX,
}{...},
}{...};
static uint8_t seg_rx_buf_data[(CONFIG_BLE_MESH_RX_SEG_MSG_COUNT *
CONFIG_BLE_MESH_RX_SDU_MAX)];
static bt_mesh_mutex_t seg_tx_lock;
static bt_mesh_mutex_t seg_rx_lock;
static inline void bt_mesh_seg_tx_lock(void)
{
bt_mesh_mutex_lock(&seg_tx_lock);
}{ ... }
static inline void bt_mesh_seg_tx_unlock(void)
{
bt_mesh_mutex_unlock(&seg_tx_lock);
}{ ... }
static inline void bt_mesh_seg_rx_lock(void)
{
bt_mesh_mutex_lock(&seg_rx_lock);
}{ ... }
static inline void bt_mesh_seg_rx_unlock(void)
{
bt_mesh_mutex_unlock(&seg_rx_lock);
}{ ... }
uint8_t bt_mesh_get_seg_rtx_num(void)
{
return SEG_RETRANSMIT_ATTEMPTS;
}{ ... }
int32_t bt_mesh_get_seg_rtx_timeout(uint16_t dst, uint8_t ttl)
{
/* ... */
struct seg_tx tx = {
.ttl = ttl,
}{...};
return SEG_RETRANSMIT_TIMEOUT_UNICAST(&tx);
}{ ... }
struct bt_mesh_app_key *bt_mesh_app_key_get(uint16_t app_idx)
{
if (bt_mesh_is_provisioned()) {
#if CONFIG_BLE_MESH_NODE
if (!IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV)) {
for (int i = 0; i < ARRAY_SIZE(bt_mesh.app_keys); i++) {
if (bt_mesh.app_keys[i].net_idx != BLE_MESH_KEY_UNUSED &&
bt_mesh.app_keys[i].app_idx == app_idx) {
return &bt_mesh.app_keys[i];
}{...}
}{...}
}{...} else {
return bt_mesh_fast_prov_app_key_find(app_idx);
}{...}
/* ... */#endif
}{...} else if (bt_mesh_is_provisioner_en()) {
#if CONFIG_BLE_MESH_PROVISIONER
for (int i = 0; i < ARRAY_SIZE(bt_mesh.p_app_keys); i++) {
if (bt_mesh.p_app_keys[i] &&
bt_mesh.p_app_keys[i]->net_idx != BLE_MESH_KEY_UNUSED &&
bt_mesh.p_app_keys[i]->app_idx == app_idx) {
return bt_mesh.p_app_keys[i];
}{...}
}{...}
#endif/* ... */
}{...}
return NULL;
}{ ... }
int bt_mesh_upper_key_get(const struct bt_mesh_subnet *subnet, uint16_t app_idx,
const uint8_t **key, uint8_t *aid, uint16_t dst)
{
struct bt_mesh_app_key *app_key = NULL;
if (app_idx == BLE_MESH_KEY_DEV) {
*key = bt_mesh_dev_key_get(dst);
if (!*key) {
BT_ERR("DevKey of 0x%04x not found", dst);
return -EINVAL;
}{...}
*aid = 0U;
return 0;
}{...}
if (!subnet) {
BT_ERR("Invalid subnet");
return -EINVAL;
}{...}
app_key = bt_mesh_app_key_get(app_idx);
if (!app_key) {
BT_ERR("AppKey 0x%04x not found", app_idx);
return -ENOENT;
}{...}
if (subnet->kr_phase == BLE_MESH_KR_PHASE_2 && app_key->updated) {
*key = app_key->keys[1].val;
*aid = app_key->keys[1].id;
}{...} else {
*key = app_key->keys[0].val;
*aid = app_key->keys[0].id;
}{...}
return 0;
}{ ... }
static int send_unseg(struct bt_mesh_net_tx *tx, struct net_buf_simple *sdu,
const struct bt_mesh_send_cb *cb, void *cb_data,
const uint8_t *ctl_op)
{
struct net_buf *buf = NULL;
BT_DBG("src 0x%04x dst 0x%04x app_idx 0x%04x sdu_len %u",
tx->src, tx->ctx->addr, tx->ctx->app_idx, sdu->len);
buf = bt_mesh_adv_create(BLE_MESH_ADV_DATA, BUF_TIMEOUT);
if (!buf) {
BT_ERR("Out of network buffers");
return -ENOBUFS;
}{...}
net_buf_reserve(buf, BLE_MESH_NET_HDR_LEN);
if (ctl_op) {
net_buf_add_u8(buf, TRANS_CTL_HDR(*ctl_op, 0));
}{...} else if (tx->ctx->app_idx == BLE_MESH_KEY_DEV) {
net_buf_add_u8(buf, UNSEG_HDR(0, 0));
}{...} else {
net_buf_add_u8(buf, UNSEG_HDR(1, tx->aid));
}{...}
net_buf_add_mem(buf, sdu->data, sdu->len);
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
if (!bt_mesh_friend_queue_has_space(tx->sub->net_idx,
tx->src, tx->ctx->addr,
NULL, 1)) {
if (BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) {
BT_ERR("Not enough space in Friend Queue");
net_buf_unref(buf);
return -ENOBUFS;
}{...}
BT_WARN("No space in Friend Queue");
goto send;
}{...}
if (bt_mesh_friend_enqueue_tx(tx, BLE_MESH_FRIEND_PDU_SINGLE,
NULL, 1, &buf->b) &&
BLE_MESH_ADDR_IS_UNICAST(tx->ctx->addr)) {
/* ... */
net_buf_unref(buf);
send_cb_finalize(cb, cb_data);
return 0;
}{...}
}{...}
send:
return bt_mesh_net_send(tx, buf, cb, cb_data);
}{ ... }
static inline uint8_t seg_len(bool ctl)
{
if (ctl) {
return BLE_MESH_CTL_SEG_SDU_MAX;
}{...}
return BLE_MESH_APP_SEG_SDU_MAX;
}{ ... }
bool bt_mesh_tx_in_progress(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(seg_tx); i++) {
if (seg_tx[i].nack_count) {
return true;
}{...}
}{...}
return false;
}{ ... }
static void seg_tx_done(struct seg_tx *tx, uint8_t seg_idx)
{
bt_mesh_adv_buf_ref_debug(__func__, tx->seg[seg_idx], 3U, BLE_MESH_BUF_REF_SMALL);
/* ... */
if (bt_mesh_atomic_cas(&BLE_MESH_ADV_BUSY(tx->seg[seg_idx]), 1, 0)) {
tx->seg_pending--;
}{...}
net_buf_unref(tx->seg[seg_idx]);
tx->seg[seg_idx] = NULL;
tx->nack_count--;
}{ ... }
static void seg_tx_reset(struct seg_tx *tx)
{
int i;
bt_mesh_seg_tx_lock();
k_delayed_work_cancel(&tx->rtx_timer);
tx->cb = NULL;
tx->cb_data = NULL;
tx->seq_auth = 0U;
tx->sub = NULL;
tx->dst = BLE_MESH_ADDR_UNASSIGNED;
for (i = 0; i <= tx->seg_n && tx->nack_count; i++) {
if (!tx->seg[i]) {
continue;
}{...}
seg_tx_done(tx, i);
}{...}
tx->nack_count = 0U;
bt_mesh_seg_tx_unlock();
if (bt_mesh_atomic_test_and_clear_bit(bt_mesh.flags, BLE_MESH_IVU_PENDING)) {
BT_DBG("Proceeding with pending IV Update");
/* ... */
if (bt_mesh_net_iv_update(bt_mesh.iv_index, false)) {
bt_mesh_net_sec_update(NULL);
}{...}
}{...}
}{ ... }
static inline void seg_tx_complete(struct seg_tx *tx, int err)
{
const struct bt_mesh_send_cb *cb = tx->cb;
void *cb_data = tx->cb_data;
seg_tx_reset(tx);
if (cb && cb->end) {
cb->end(err, cb_data);
}{...}
}{ ... }
static void schedule_retransmit(struct seg_tx *tx)
{
if (--tx->seg_pending) {
return;
}{...}
if (!BLE_MESH_ADDR_IS_UNICAST(tx->dst) && !tx->attempts) {
BT_INFO("Complete tx sdu to group");
seg_tx_complete(tx, 0);
return;
}{...}
k_delayed_work_submit(&tx->rtx_timer, SEG_RETRANSMIT_TIMEOUT(tx));
}{ ... }
static void seg_first_send_start(uint16_t duration, int err, void *user_data)
{
struct seg_tx *tx = user_data;
if (tx->cb && tx->cb->start) {
tx->cb->start(duration, err, tx->cb_data);
}{...}
}{ ... }
static void seg_send_start(uint16_t duration, int err, void *user_data)
{
struct seg_tx *tx = user_data;
/* ... */
if (err) {
schedule_retransmit(tx);
}{...}
}{ ... }
static void seg_sent(int err, void *user_data)
{
struct seg_tx *tx = user_data;
schedule_retransmit(tx);
}{ ... }
static const struct bt_mesh_send_cb first_sent_cb = {
.start = seg_first_send_start,
.end = seg_sent,
}{...};
static const struct bt_mesh_send_cb seg_sent_cb = {
.start = seg_send_start,
.end = seg_sent,
}{...};
static void seg_tx_send_unacked(struct seg_tx *tx)
{
int i, err = 0;
bt_mesh_seg_tx_lock();
if (!(tx->attempts--)) {
BT_WARN("Ran out of retransmit attempts");
bt_mesh_seg_tx_unlock();
seg_tx_complete(tx, -ETIMEDOUT);
return;
}{...}
BT_INFO("Attempts: %u", tx->attempts);
for (i = 0; i <= tx->seg_n; i++) {
struct net_buf *seg = tx->seg[i];
if (!seg) {
continue;
}{...}
if (bt_mesh_atomic_get(&BLE_MESH_ADV_BUSY(seg))) {
BT_DBG("Skipping segment that's still advertising");
continue;
}{...}
tx->seg_pending++;
BT_INFO("Resending %u/%u, cred 0x%02x", i, tx->seg_n, tx->cred);
/* ... */
err = bt_mesh_net_resend(tx->sub, seg, tx->new_key,
&tx->cred, tx->tag,
&seg_sent_cb, tx);
if (err) {
BT_ERR("Sending segment failed");
bt_mesh_seg_tx_unlock();
seg_tx_complete(tx, -EIO);
return;
}{...}
}{...}
bt_mesh_seg_tx_unlock();
}{ ... }
static void seg_retransmit(struct k_work *work)
{
struct seg_tx *tx = CONTAINER_OF(work, struct seg_tx, rtx_timer);
seg_tx_send_unacked(tx);
}{ ... }
static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu,
const struct bt_mesh_send_cb *cb, void *cb_data,
const uint8_t *ctl_op)
{
struct seg_tx *tx = NULL;
uint16_t seq_zero = 0U;
uint8_t seg_hdr = 0U;
uint8_t seg_o = 0U;
int i;
BT_DBG("src 0x%04x dst 0x%04x app_idx 0x%04x aszmic %u sdu_len %u",
net_tx->src, net_tx->ctx->addr, net_tx->ctx->app_idx,
net_tx->aszmic, sdu->len);
for (tx = NULL, i = 0; i < ARRAY_SIZE(seg_tx); i++) {
if (!seg_tx[i].nack_count) {
tx = &seg_tx[i];
break;
}{...}
}{...}
if (!tx) {
BT_ERR("No multi-segment message contexts available");
return -EBUSY;
}{...}
if (ctl_op) {
seg_hdr = TRANS_CTL_HDR(*ctl_op, 1);
}{...} else if (net_tx->ctx->app_idx == BLE_MESH_KEY_DEV) {
seg_hdr = SEG_HDR(0, 0);
}{...} else {
seg_hdr = SEG_HDR(1, net_tx->aid);
}{...}
tx->dst = net_tx->ctx->addr;
if (sdu->len) {
tx->seg_n = (sdu->len - 1) / seg_len(!!ctl_op);
}{...} else {
tx->seg_n = 0;
}{...}
tx->nack_count = tx->seg_n + 1;
tx->seq_auth = SEQ_AUTH(BLE_MESH_NET_IVI_TX, bt_mesh.seq);
tx->sub = net_tx->sub;
tx->new_key = net_tx->sub->kr_flag;
tx->attempts = SEG_RETRANSMIT_ATTEMPTS;
tx->seg_pending = 0;
tx->cred = net_tx->ctx->send_cred;
tx->tag = net_tx->ctx->send_tag;
tx->cb = cb;
tx->cb_data = cb_data;
if (net_tx->ctx->send_ttl == BLE_MESH_TTL_DEFAULT) {
tx->ttl = bt_mesh_default_ttl_get();
}{...} else {
tx->ttl = net_tx->ctx->send_ttl;
}{...}
seq_zero = tx->seq_auth & TRANS_SEQ_ZERO_MASK;
BT_DBG("SeqZero 0x%04x", seq_zero);
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) &&
!bt_mesh_friend_queue_has_space(tx->sub->net_idx, net_tx->src,
tx->dst, &tx->seq_auth,
tx->seg_n + 1) &&
BLE_MESH_ADDR_IS_UNICAST(tx->dst)) {
BT_ERR("Not enough space in Friend Queue for %u segments",
tx->seg_n + 1);
seg_tx_reset(tx);
return -ENOBUFS;
}{...}
for (seg_o = 0U; sdu->len; seg_o++) {
struct net_buf *seg = NULL;
uint16_t len = 0U;
int err = 0;
seg = bt_mesh_adv_create(BLE_MESH_ADV_DATA, BUF_TIMEOUT);
if (!seg) {
BT_ERR("Out of segment buffers");
seg_tx_reset(tx);
return -ENOBUFS;
}{...}
net_buf_reserve(seg, BLE_MESH_NET_HDR_LEN);
net_buf_add_u8(seg, seg_hdr);
net_buf_add_u8(seg, (net_tx->aszmic << 7) | seq_zero >> 6);
net_buf_add_u8(seg, (((seq_zero & 0x3f) << 2) | (seg_o >> 3)));
net_buf_add_u8(seg, ((seg_o & 0x07) << 5) | tx->seg_n);
len = MIN(sdu->len, seg_len(!!ctl_op));
net_buf_add_mem(seg, net_buf_simple_pull_mem(sdu, len), len);
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
enum bt_mesh_friend_pdu_type type = BLE_MESH_FRIEND_PDU_PARTIAL;
if (seg_o == tx->seg_n) {
type = BLE_MESH_FRIEND_PDU_COMPLETE;
}{...} else {
type = BLE_MESH_FRIEND_PDU_PARTIAL;
}{...}
if (bt_mesh_friend_enqueue_tx(net_tx, type,
&tx->seq_auth,
tx->seg_n + 1,
&seg->b) &&
BLE_MESH_ADDR_IS_UNICAST(net_tx->ctx->addr)) {
/* ... */
net_buf_unref(seg);
continue;
}{...}
}{...}
tx->seg[seg_o] = net_buf_ref(seg);
BT_DBG("Sending %u/%u", seg_o, tx->seg_n);
tx->seg_pending++;
err = bt_mesh_net_send(net_tx, seg,
seg_o ? &seg_sent_cb : &first_sent_cb,
tx);
if (err) {
BT_ERR("Sending segment failed (err %d)", err);
seg_tx_reset(tx);
return err;
}{...}
/* ... */
if (tx->cred != net_tx->ctx->send_cred) {
tx->cred = net_tx->ctx->send_cred;
}{...}
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && !tx->seg[0]) {
seg_tx_reset(tx);
/* ... */
send_cb_finalize(cb, cb_data);
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) &&
bt_mesh_lpn_established()) {
bt_mesh_lpn_poll();
}{...}
return 0;
}{ ... }
int bt_mesh_trans_send(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg,
const struct bt_mesh_send_cb *cb, void *cb_data)
{
const uint8_t *key = NULL;
uint8_t *ad = NULL;
uint8_t aid = 0U;
int err = 0;
if (msg->len < 1) {
BT_ERR("Zero-length SDU not allowed");
return -EINVAL;
}{...}
if (msg->len > BLE_MESH_SDU_UNSEG_MAX) {
tx->ctx->send_tag |= BLE_MESH_TAG_SEND_SEGMENTED;
}{...}
BT_DBG("net_idx 0x%04x app_idx 0x%04x dst 0x%04x", tx->sub->net_idx,
tx->ctx->app_idx, tx->ctx->addr);
BT_DBG("len %u: %s", msg->len, bt_hex(msg->data, msg->len));
err = bt_mesh_upper_key_get(tx->sub, tx->ctx->app_idx, &key,
&aid, tx->ctx->addr);
if (err) {
return err;
}{...}
tx->aid = aid;
if (!bt_mesh_tag_send_segmented(tx->ctx->send_tag) ||
tx->ctx->send_szmic == BLE_MESH_SEG_SZMIC_SHORT ||
net_buf_simple_tailroom(msg) < BLE_MESH_MIC_LONG) {
tx->aszmic = 0U;
}{...} else {
tx->aszmic = 1U;
}{...}
BT_INFO("%s, send_tag 0x%02x, send_szmic %d, aszmic %d",
bt_mesh_tag_send_segmented(tx->ctx->send_tag) ? "Seg" : "Unseg",
tx->ctx->send_tag, tx->ctx->send_szmic, tx->aszmic);
if (BLE_MESH_ADDR_IS_VIRTUAL(tx->ctx->addr)) {
ad = bt_mesh_label_uuid_get(tx->ctx->addr);
}{...} else {
ad = NULL;
}{...}
err = bt_mesh_app_encrypt(key, tx->ctx->app_idx == BLE_MESH_KEY_DEV,
tx->aszmic, msg, ad, tx->src,
tx->ctx->addr, bt_mesh.seq,
BLE_MESH_NET_IVI_TX);
if (err) {
BT_ERR("Encrypt failed (err %d)", err);
return err;
}{...}
if (bt_mesh_tag_send_segmented(tx->ctx->send_tag)) {
return send_seg(tx, msg, cb, cb_data, NULL);
}{...}
return send_unseg(tx, msg, cb, cb_data, NULL);
}{ ... }
static void revoke_dev_key(const uint8_t *dev_key)
{
if (!memcmp(dev_key, bt_mesh.dev_key_ca, 16)) {
BT_INFO("Revoke Device Key");
memcpy(bt_mesh.dev_key, bt_mesh.dev_key_ca, 16);
memset(bt_mesh.dev_key_ca, 0, 16);
if (IS_ENABLED(CONFIG_BLE_MESH_SETTINGS)) {
bt_mesh_store_net();
bt_mesh_clear_dkca();
}{...}
}{...}
}{ ... }
static int sdu_recv(struct bt_mesh_net_rx *rx, uint32_t seq, uint8_t hdr,
uint8_t aszmic, struct net_buf_simple *buf)
{
struct net_buf_simple *sdu = NULL;
size_t array_size = 0U;
uint8_t *ad = NULL;
size_t i = 0U;
int err = 0;
BT_DBG("ASZMIC %u AKF %u AID 0x%02x", aszmic, AKF(&hdr), AID(&hdr));
BT_DBG("len %u: %s", buf->len, bt_hex(buf->data, buf->len));
if (buf->len < 1 + APP_MIC_LEN(aszmic)) {
BT_ERR("Too short SDU + MIC (len %u)", buf->len);
return -EINVAL;
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && !rx->local_match) {
BT_DBG("Ignoring PDU for LPN 0x%04x of this Friend",
rx->ctx.recv_dst);
return 0;
}{...}
if (BLE_MESH_ADDR_IS_VIRTUAL(rx->ctx.recv_dst)) {
ad = bt_mesh_label_uuid_get(rx->ctx.recv_dst);
}{...} else {
ad = NULL;
}{...}
buf->len -= APP_MIC_LEN(aszmic);
/* ... */
sdu = bt_mesh_alloc_buf(CONFIG_BLE_MESH_RX_SDU_MAX - BLE_MESH_MIC_SHORT);
if (!sdu) {
BT_ERR("%s, Out of memory", __func__);
return -ENOMEM;
}{...}
if (!AKF(&hdr)) {
array_size = bt_mesh_rx_devkey_size();
for (i = 0U; i < array_size; i++) {
const uint8_t *dev_key = NULL;
dev_key = bt_mesh_rx_devkey_get(i, rx->ctx.addr);
if (!dev_key) {
BT_DBG("DevKey not found");
continue;
}{...}
net_buf_simple_reset(sdu);
err = bt_mesh_app_decrypt(dev_key, true, aszmic, buf,
sdu, ad, rx->ctx.addr,
rx->ctx.recv_dst, seq,
BLE_MESH_NET_IVI_RX(rx));
if (err) {
continue;
}{...}
BT_BQB(BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_NODE | \
BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_TNPT,
"\nTNPTRecv: ctl: 0x%04x, ttl: 0x%04x, src: 0x%04x, dst: 0x%04x, payload: 0x%s",
rx->ctl, rx->ctx.recv_ttl, rx->ctx.addr, rx->ctx.recv_dst,
bt_hex(sdu->data, sdu->len));
/* ... */
revoke_dev_key(dev_key);
rx->ctx.app_idx = BLE_MESH_KEY_DEV;
bt_mesh_model_recv(rx, sdu);
bt_mesh_free_buf(sdu);
return 0;
}{...}
BT_WARN("Unable to decrypt with DevKey");
bt_mesh_free_buf(sdu);
return -ENODEV;
}{...}
array_size = bt_mesh_rx_appkey_size();
for (i = 0U; i < array_size; i++) {
struct bt_mesh_app_keys *keys = NULL;
struct bt_mesh_app_key *key = NULL;
key = bt_mesh_rx_appkey_get(i);
if (!key) {
BT_DBG("AppKey not found");
continue;
}{...}
if (key->net_idx != rx->sub->net_idx) {
continue;
}{...}
if (rx->new_key && key->updated) {
keys = &key->keys[1];
}{...} else {
keys = &key->keys[0];
}{...}
if (AID(&hdr) != keys->id) {
continue;
}{...}
net_buf_simple_reset(sdu);
err = bt_mesh_app_decrypt(keys->val, false, aszmic, buf,
sdu, ad, rx->ctx.addr,
rx->ctx.recv_dst, seq,
BLE_MESH_NET_IVI_RX(rx));
BT_BQB(BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_NODE | \
BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_TNPT,
"\nTNPTRecv: ctl: 0x%04x, ttl: 0x%04x, src: 0x%04x, dst: 0x%04x, payload: 0x%s",
rx->ctl, rx->ctx.recv_ttl, rx->ctx.addr, rx->ctx.recv_dst,
bt_hex(sdu->data, sdu->len));
if (err) {
BT_DBG("Unable to decrypt with AppKey 0x%03x",
key->app_idx);
continue;
}{...}
rx->ctx.app_idx = key->app_idx;
bt_mesh_model_recv(rx, sdu);
bt_mesh_free_buf(sdu);
return 0;
}{...}
if (rx->local_match) {
BT_WARN("No matching AppKey");
}{...}
bt_mesh_free_buf(sdu);
return 0;
}{ ... }
static struct seg_tx *seg_tx_lookup(uint16_t seq_zero, uint8_t obo, uint16_t addr)
{
struct seg_tx *tx = NULL;
int i;
for (i = 0; i < ARRAY_SIZE(seg_tx); i++) {
tx = &seg_tx[i];
if ((tx->seq_auth & TRANS_SEQ_ZERO_MASK) != seq_zero) {
continue;
}{...}
if (tx->dst == addr) {
return tx;
}{...}
/* ... */
if (obo && tx->nack_count == tx->seg_n + 1) {
tx->dst = addr;
return tx;
}{...}
}{...}
return NULL;
}{ ... }
static int trans_ack(struct bt_mesh_net_rx *rx, uint8_t hdr,
struct net_buf_simple *buf, uint64_t *seq_auth)
{
struct seg_tx *tx = NULL;
unsigned int bit = 0;
uint32_t ack = 0U;
uint16_t seq_zero = 0U;
uint8_t obo = 0U;
if (buf->len != 6) {
BT_ERR("Malformed Segment Ack (len %u)", buf->len);
return -EINVAL;
}{...}
seq_zero = net_buf_simple_pull_be16(buf);
obo = seq_zero >> 15;
seq_zero = (seq_zero >> 2) & TRANS_SEQ_ZERO_MASK;
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && rx->friend_match) {
BT_DBG("Ack for LPN 0x%04x of this Friend", rx->ctx.recv_dst);
*seq_auth = SEQ_AUTH(BLE_MESH_NET_IVI_RX(rx), seq_zero);
return 0;
}{...}
ack = net_buf_simple_pull_be32(buf);
BT_DBG("OBO %u seq_zero 0x%04x ack 0x%08x", obo, seq_zero, ack);
tx = seg_tx_lookup(seq_zero, obo, rx->ctx.addr);
if (!tx) {
BT_INFO("No matching TX context for Seg Ack");
return -EINVAL;
}{...}
if (!BLE_MESH_ADDR_IS_UNICAST(tx->dst)) {
BT_WARN("Received ack for segments to group");
return -EINVAL;
}{...}
*seq_auth = tx->seq_auth;
if (!ack) {
BT_WARN("SDU canceled");
seg_tx_complete(tx, -ECANCELED);
return 0;
}{...}
if (find_msb_set(ack) - 1 > tx->seg_n) {
BT_ERR("Too large segment number in ack");
return -EINVAL;
}{...}
k_delayed_work_cancel(&tx->rtx_timer);
while ((bit = find_lsb_set(ack))) {
if (tx->seg[bit - 1]) {
BT_INFO("Seg %u/%u acked", bit - 1, tx->seg_n);
bt_mesh_seg_tx_lock();
seg_tx_done(tx, bit - 1);
bt_mesh_seg_tx_unlock();
}{...}
ack &= ~BIT(bit - 1);
}{...}
if (tx->nack_count) {
seg_tx_send_unacked(tx);
}{...} else {
BT_DBG("SDU TX complete");
seg_tx_complete(tx, 0);
}{...}
return 0;
}{ ... }
static int trans_heartbeat(struct bt_mesh_net_rx *rx,
struct net_buf_simple *buf)
{
uint8_t init_ttl = 0U, hops = 0U;
uint16_t feat = 0U;
if (buf->len != 3) {
BT_ERR("Malformed heartbeat message (len %u)", buf->len);
return -EINVAL;
}{...}
if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned() &&
rx->ctx.recv_dst != bt_mesh_get_hb_sub_dst()) {
BT_WARN("Ignoring heartbeat to non-subscribed destination");
return 0;
}{...}
init_ttl = (net_buf_simple_pull_u8(buf) & 0x7f);
feat = net_buf_simple_pull_be16(buf);
hops = (init_ttl - rx->ctx.recv_ttl + 1);
BT_INFO("src 0x%04x TTL %u InitTTL %u (%u hop%s) feat 0x%04x",
rx->ctx.addr, rx->ctx.recv_ttl, init_ttl, hops,
(hops == 1U) ? "" : "s", feat);
if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) {
bt_mesh_heartbeat_recv(rx->ctx.addr, rx->ctx.recv_dst, hops, feat);
}{...} else if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER_RECV_HB) && bt_mesh_is_provisioner_en()) {
bt_mesh_pvnr_heartbeat_recv(rx->ctx.addr, rx->ctx.recv_dst,
init_ttl, rx->ctx.recv_ttl,
hops, feat, rx->ctx.recv_rssi);
}{...}
return 0;
}{ ... }
static int ctl_recv(struct bt_mesh_net_rx *rx, uint8_t hdr,
struct net_buf_simple *buf, uint64_t *seq_auth)
{
uint8_t ctl_op = TRANS_CTL_OP(&hdr);
BT_DBG("OpCode 0x%02x len %u", ctl_op, buf->len);
BT_BQB(BLE_MESH_BQB_TEST_LOG_LEVEL_PRIMARY_ID_NODE | \
BLE_MESH_BQB_TEST_LOG_LEVEL_SUB_ID_TNPT,
"\nTNPTRecv: ctl: 0x%04x, ttl: 0x%04x, src: 0x%04x, dst: 0x%04x, payload: 0x%s",
rx->ctl, rx->ctx.recv_ttl, rx->ctx.addr, rx->ctx.recv_dst,
bt_hex(buf->data, buf->len));
switch (ctl_op) {
case TRANS_CTL_OP_ACK:
return trans_ack(rx, hdr, buf, seq_auth);...
case TRANS_CTL_OP_HEARTBEAT:
return trans_heartbeat(rx, buf);...
}{...}
if (!rx->local_match) {
return 0;
}{...}
#if CONFIG_BLE_MESH_DF_SRV
switch (ctl_op) {
case TRANS_CTL_OP_PATH_REQ:
case TRANS_CTL_OP_PATH_REPLY:
case TRANS_CTL_OP_PATH_CFM:
case TRANS_CTL_OP_PATH_ECHO_REQ:
case TRANS_CTL_OP_PATH_ECHO_REPLY:
case TRANS_CTL_OP_DEP_NODE_UPDATE:
case TRANS_CTL_OP_PATH_REQ_SOLIC:
return bt_mesh_directed_forwarding_ctl_recv(ctl_op, rx, buf);...
}{...}
#endif/* ... */
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && !bt_mesh_lpn_established()) {
switch (ctl_op) {
case TRANS_CTL_OP_FRIEND_POLL:
return bt_mesh_friend_poll(rx, buf);...
case TRANS_CTL_OP_FRIEND_REQ:
return bt_mesh_friend_req(rx, buf);...
case TRANS_CTL_OP_FRIEND_CLEAR:
return bt_mesh_friend_clear(rx, buf);...
case TRANS_CTL_OP_FRIEND_CLEAR_CFM:
return bt_mesh_friend_clear_cfm(rx, buf);...
case TRANS_CTL_OP_FRIEND_SUB_ADD:
return bt_mesh_friend_sub_add(rx, buf);...
case TRANS_CTL_OP_FRIEND_SUB_REM:
return bt_mesh_friend_sub_rem(rx, buf);...
}{...}
}{...}
#if CONFIG_BLE_MESH_LOW_POWER
if (ctl_op == TRANS_CTL_OP_FRIEND_OFFER) {
return bt_mesh_lpn_friend_offer(rx, buf);
}{...}
if (rx->ctx.addr == bt_mesh.lpn.frnd) {
if (ctl_op == TRANS_CTL_OP_FRIEND_CLEAR_CFM) {
return bt_mesh_lpn_friend_clear_cfm(rx, buf);
}{...}
if (rx->ctx.recv_cred != BLE_MESH_FRIENDSHIP_CRED) {
BT_WARN("Message from friend with wrong credentials");
return -EINVAL;
}{...}
switch (ctl_op) {
case TRANS_CTL_OP_FRIEND_UPDATE:
return bt_mesh_lpn_friend_update(rx, buf);...
case TRANS_CTL_OP_FRIEND_SUB_CFM:
return bt_mesh_lpn_friend_sub_cfm(rx, buf);...
}{...}
}{...}
#endif/* ... */
BT_WARN("Unhandled TransOpCode 0x%02x", ctl_op);
return -ENOENT;
}{ ... }
static int trans_unseg(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx,
uint64_t *seq_auth)
{
uint8_t hdr = 0U;
BT_DBG("AFK %u AID 0x%02x", AKF(buf->data), AID(buf->data));
if (buf->len < 1) {
BT_ERR("Too small unsegmented PDU");
return -EINVAL;
}{...}
if (bt_mesh_rpl_check(rx, NULL)) {
BT_WARN("Replay: src 0x%04x dst 0x%04x seq 0x%06x",
rx->ctx.addr, rx->ctx.recv_dst, rx->seq);
return -EINVAL;
}{...}
hdr = net_buf_simple_pull_u8(buf);
if (rx->ctl) {
return ctl_recv(rx, hdr, buf, seq_auth);
}{...}
if (!rx->local_match && !rx->friend_match) {
return 0;
}{...}
return sdu_recv(rx, rx->seq, hdr, 0, buf);
}{ ... }
static inline int32_t ack_timeout(struct seg_rx *rx)
{
int32_t to = 0;
uint8_t ttl = 0U;
if (rx->ttl == BLE_MESH_TTL_DEFAULT) {
ttl = bt_mesh_default_ttl_get();
}{...} else {
ttl = rx->ttl;
}{...}
/* ... */
to = K_MSEC(150 + (ttl * 50U));
to += K_MSEC(((rx->seg_n + 1) - popcount(rx->block)) * 100U);
/* ... */
return MAX(to, K_MSEC(400));
}{ ... }
int bt_mesh_ctl_send(struct bt_mesh_net_tx *tx, uint8_t ctl_op, void *data,
size_t data_len, const struct bt_mesh_send_cb *cb,
void *cb_data)
{
struct net_buf_simple buf = {0};
net_buf_simple_init_with_data(&buf, data, data_len);
if (data_len > BLE_MESH_SDU_UNSEG_MAX) {
tx->ctx->send_tag |= BLE_MESH_TAG_SEND_SEGMENTED;
}{...}
tx->ctx->app_idx = BLE_MESH_KEY_UNUSED;
BT_DBG("src 0x%04x dst 0x%04x ttl 0x%02x ctl 0x%02x", tx->src,
tx->ctx->addr, tx->ctx->send_ttl, ctl_op);
BT_DBG("len %zu: %s", data_len, bt_hex(data, data_len));
if (bt_mesh_tag_send_segmented(tx->ctx->send_tag)) {
return send_seg(tx, &buf, cb, cb_data, &ctl_op);
}{...}
return send_unseg(tx, &buf, cb, cb_data, &ctl_op);
}{ ... }
static int send_ack(struct bt_mesh_subnet *sub, uint16_t src, uint16_t dst,
uint8_t ttl, uint64_t *seq_auth, uint32_t block, uint8_t obo)
{
struct bt_mesh_msg_ctx ctx = {
.net_idx = sub->net_idx,
.app_idx = BLE_MESH_KEY_UNUSED,
.addr = dst,
.send_ttl = ttl,
/* ... */
.send_cred = BLE_MESH_FLOODING_CRED,
}{...};
struct bt_mesh_net_tx tx = {
.sub = sub,
.ctx = &ctx,
.src = obo ? bt_mesh_primary_addr() : src,
.xmit = bt_mesh_net_transmit_get(),
}{...};
uint16_t seq_zero = *seq_auth & TRANS_SEQ_ZERO_MASK;
uint8_t buf[6] = {0};
BT_DBG("SeqZero 0x%04x Block 0x%08x OBO %u", seq_zero, block, obo);
if (bt_mesh_lpn_established()) {
BT_WARN("Not sending ack when LPN is enabled");
return 0;
}{...}
/* ... */
if (!BLE_MESH_ADDR_IS_UNICAST(src)) {
BT_INFO("Not sending ack for non-unicast address");
return 0;
}{...}
sys_put_be16(((seq_zero << 2) & 0x7ffc) | (obo << 15), buf);
sys_put_be32(block, &buf[2]);
return bt_mesh_ctl_send(&tx, TRANS_CTL_OP_ACK, buf, sizeof(buf),
NULL, NULL);
}{ ... }
static void seg_rx_reset(struct seg_rx *rx, bool full_reset)
{
bt_mesh_seg_rx_lock();
k_delayed_work_cancel(&rx->ack_timer);
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && rx->obo &&
rx->block != BLOCK_COMPLETE(rx->seg_n)) {
BT_WARN("Clearing incomplete buffers from Friend queue");
bt_mesh_friend_clear_incomplete(rx->sub, rx->src, rx->dst,
&rx->seq_auth);
}{...}
rx->in_use = 0U;
/* ... */
if (full_reset) {
rx->seq_auth = 0U;
rx->sub = NULL;
rx->src = BLE_MESH_ADDR_UNASSIGNED;
rx->dst = BLE_MESH_ADDR_UNASSIGNED;
}{...}
bt_mesh_seg_rx_unlock();
}{ ... }
static uint32_t incomplete_timeout(struct seg_rx *rx)
{
uint32_t timeout = 0U;
uint8_t ttl = 0U;
if (rx->ttl == BLE_MESH_TTL_DEFAULT) {
ttl = bt_mesh_default_ttl_get();
}{...} else {
ttl = rx->ttl;
}{...}
timeout = K_SECONDS(10);
timeout += K_MSEC(ttl * popcount(rx->block) * 100U);
return MIN(timeout, K_SECONDS(60));
}{ ... }
static void seg_ack(struct k_work *work)
{
struct seg_rx *rx = CONTAINER_OF(work, struct seg_rx, ack_timer);
bt_mesh_seg_rx_lock();
if (k_uptime_get_32() - rx->last > incomplete_timeout(rx)) {
BT_WARN("Incomplete timer expired");
bt_mesh_seg_rx_unlock();
seg_rx_reset(rx, false);
return;
}{...}
/* ... */
if (rx->sub == NULL) {
bt_mesh_seg_rx_unlock();
return;
}{...}
send_ack(rx->sub, rx->dst, rx->src, rx->ttl, &rx->seq_auth,
rx->block, rx->obo);
k_delayed_work_submit(&rx->ack_timer, ack_timeout(rx));
bt_mesh_seg_rx_unlock();
}{ ... }
static inline bool sdu_len_is_ok(bool ctl, uint8_t seg_n)
{
return ((seg_n + 1) * seg_len(ctl) <= CONFIG_BLE_MESH_RX_SDU_MAX);
}{ ... }
static struct seg_rx *seg_rx_find(struct bt_mesh_net_rx *net_rx,
const uint64_t *seq_auth)
{
int i;
for (i = 0; i < ARRAY_SIZE(seg_rx); i++) {
struct seg_rx *rx = &seg_rx[i];
if (rx->src != net_rx->ctx.addr ||
rx->dst != net_rx->ctx.recv_dst) {
continue;
}{...}
/* ... */
#if CONFIG_BLE_MESH_DISCARD_OLD_SEQ_AUTH
if (rx->seq_auth >= *seq_auth) {
#else
if (rx->seq_auth == *seq_auth) {
#endif
return rx;
}{...}
if (rx->in_use) {
BT_WARN("Duplicate SDU from src 0x%04x", net_rx->ctx.addr);
/* ... */
seg_rx_reset(rx, true);
return NULL;
}{...}
}{...}
return NULL;
}{ ... }
static bool seg_rx_is_valid(struct seg_rx *rx, struct bt_mesh_net_rx *net_rx,
const uint8_t *hdr, uint8_t seg_n)
{
if (rx->hdr != *hdr || rx->seg_n != seg_n) {
BT_ERR("Invalid segment for ongoing session");
return false;
}{...}
if (rx->src != net_rx->ctx.addr || rx->dst != net_rx->ctx.recv_dst) {
BT_ERR("Invalid source or destination for segment");
return false;
}{...}
if (rx->ctl != net_rx->ctl) {
BT_ERR("Inconsistent CTL in segment");
return false;
}{...}
return true;
}{ ... }
static struct seg_rx *seg_rx_alloc(struct bt_mesh_net_rx *net_rx,
const uint8_t *hdr, const uint64_t *seq_auth,
uint8_t seg_n)
{
int i;
for (i = 0; i < ARRAY_SIZE(seg_rx); i++) {
struct seg_rx *rx = &seg_rx[i];
if (rx->in_use) {
continue;
}{...}
rx->in_use = 1U;
net_buf_simple_reset(&rx->buf);
rx->sub = net_rx->sub;
rx->ctl = net_rx->ctl;
rx->seq_auth = *seq_auth;
rx->seg_n = seg_n;
rx->hdr = *hdr;
rx->ttl = net_rx->ctx.send_ttl;
rx->src = net_rx->ctx.addr;
rx->dst = net_rx->ctx.recv_dst;
rx->block = 0U;
BT_DBG("New RX context. Block Complete 0x%08x",
BLOCK_COMPLETE(seg_n));
return rx;
}{...}
return NULL;
}{ ... }
static int trans_seg(struct net_buf_simple *buf, struct bt_mesh_net_rx *net_rx,
enum bt_mesh_friend_pdu_type *pdu_type, uint64_t *seq_auth,
uint8_t *seg_count)
{
struct bt_mesh_rpl *rpl = NULL;
struct seg_rx *rx = NULL;
uint8_t *hdr = buf->data;
uint16_t seq_zero = 0U;
uint8_t seg_n = 0U;
uint8_t seg_o = 0U;
int err = 0;
if (buf->len < 5) {
BT_ERR("Too short segmented message (len %u)", buf->len);
return -EINVAL;
}{...}
if (bt_mesh_rpl_check(net_rx, &rpl)) {
BT_WARN("Replay: src 0x%04x dst 0x%04x seq 0x%06x",
net_rx->ctx.addr, net_rx->ctx.recv_dst, net_rx->seq);
return -EINVAL;
}{...}
BT_DBG("ASZMIC %u AKF %u AID 0x%02x", ASZMIC(hdr), AKF(hdr), AID(hdr));
net_buf_simple_pull(buf, 1);
seq_zero = net_buf_simple_pull_be16(buf);
seg_o = (seq_zero & 0x03) << 3;
seq_zero = (seq_zero >> 2) & TRANS_SEQ_ZERO_MASK;
seg_n = net_buf_simple_pull_u8(buf);
seg_o |= seg_n >> 5;
seg_n &= 0x1f;
BT_DBG("SeqZero 0x%04x SegO %u SegN %u", seq_zero, seg_o, seg_n);
if (seg_o > seg_n) {
BT_ERR("SegO greater than SegN (%u > %u)", seg_o, seg_n);
return -EINVAL;
}{...}
/* ... */
*seq_auth = SEQ_AUTH(BLE_MESH_NET_IVI_RX(net_rx),
(net_rx->seq -
((((net_rx->seq & BIT_MASK(14)) - seq_zero)) & BIT_MASK(13))));
*seg_count = seg_n + 1;
rx = seg_rx_find(net_rx, seq_auth);
if (rx) {
if (rx->seq_auth > *seq_auth) {
BT_WARN("Ignoring old SeqAuth, src 0x%04x, dst 0x%04x",
rx->src, rx->dst);
return -EINVAL;
}{...}
if (!seg_rx_is_valid(rx, net_rx, hdr, seg_n)) {
return -EINVAL;
}{...}
if (rx->in_use) {
BT_DBG("Existing RX context. Block 0x%08x", rx->block);
goto found_rx;
}{...}
if (rx->block == BLOCK_COMPLETE(rx->seg_n)) {
BT_INFO("Got segment for already complete SDU");
send_ack(net_rx->sub, net_rx->ctx.recv_dst,
net_rx->ctx.addr, net_rx->ctx.send_ttl,
seq_auth, rx->block, rx->obo);
if (rpl) {
bt_mesh_update_rpl(rpl, net_rx);
}{...}
return -EALREADY;
}{...}
/* ... */
BT_WARN("Got segment for canceled SDU");
return -EINVAL;
}{...}
if (!sdu_len_is_ok(net_rx->ctl, seg_n)) {
BT_ERR("Too big incoming SDU length");
send_ack(net_rx->sub, net_rx->ctx.recv_dst, net_rx->ctx.addr,
net_rx->ctx.send_ttl, seq_auth, 0,
net_rx->friend_match);
return -EMSGSIZE;
}{...}
/* ... */
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) &&
net_rx->friend_match && !net_rx->local_match &&
!bt_mesh_friend_queue_has_space(net_rx->sub->net_idx,
net_rx->ctx.addr,
net_rx->ctx.recv_dst, seq_auth,
*seg_count)) {
BT_ERR("No space in Friend Queue for %u segments", *seg_count);
send_ack(net_rx->sub, net_rx->ctx.recv_dst, net_rx->ctx.addr,
net_rx->ctx.send_ttl, seq_auth, 0,
net_rx->friend_match);
return -ENOBUFS;
}{...}
rx = seg_rx_alloc(net_rx, hdr, seq_auth, seg_n);
if (!rx) {
/* ... */
BT_WARN("No free slots for new incoming segmented messages");
return -ENOMEM;
}{...}
rx->obo = net_rx->friend_match;
found_rx:
if (BIT(seg_o) & rx->block) {
BT_INFO("Received already received fragment");
return -EALREADY;
}{...}
/* ... */
if (seg_o == seg_n) {
rx->buf.len = seg_n * seg_len(rx->ctl) + buf->len;
BT_DBG("Target len %u * %u + %u = %u", seg_n, seg_len(rx->ctl),
buf->len, rx->buf.len);
if (rx->buf.len > CONFIG_BLE_MESH_RX_SDU_MAX) {
BT_ERR("Too large SDU len");
send_ack(net_rx->sub, net_rx->ctx.recv_dst,
net_rx->ctx.addr, net_rx->ctx.send_ttl,
seq_auth, 0, rx->obo);
seg_rx_reset(rx, true);
return -EMSGSIZE;
}{...}
}{...} else {
if (buf->len != seg_len(rx->ctl)) {
BT_ERR("Incorrect segment size for message type");
return -EINVAL;
}{...}
}{...}
rx->last = k_uptime_get_32();
if (!k_delayed_work_remaining_get(&rx->ack_timer) &&
!bt_mesh_lpn_established()) {
k_delayed_work_submit(&rx->ack_timer, ack_timeout(rx));
}{...}
memcpy(rx->buf.data + (seg_o * seg_len(rx->ctl)), buf->data, buf->len);
BT_INFO("Seg %u/%u received", seg_o, seg_n);
rx->block |= BIT(seg_o);
if (rx->block != BLOCK_COMPLETE(seg_n)) {
*pdu_type = BLE_MESH_FRIEND_PDU_PARTIAL;
return 0;
}{...}
BT_DBG("Complete SDU");
if (rpl) {
bt_mesh_update_rpl(rpl, net_rx);
}{...}
*pdu_type = BLE_MESH_FRIEND_PDU_COMPLETE;
k_delayed_work_cancel(&rx->ack_timer);
send_ack(net_rx->sub, net_rx->ctx.recv_dst, net_rx->ctx.addr,
net_rx->ctx.send_ttl, seq_auth, rx->block, rx->obo);
if (net_rx->ctl) {
err = ctl_recv(net_rx, *hdr, &rx->buf, seq_auth);
}{...} else {
err = sdu_recv(net_rx, (rx->seq_auth & 0xffffff), *hdr,
ASZMIC(hdr), &rx->buf);
}{...}
seg_rx_reset(rx, false);
return err;
}{ ... }
int bt_mesh_trans_recv(struct net_buf_simple *buf, struct bt_mesh_net_rx *rx)
{
uint64_t seq_auth = TRANS_SEQ_AUTH_NVAL;
enum bt_mesh_friend_pdu_type pdu_type = BLE_MESH_FRIEND_PDU_SINGLE;
struct net_buf_simple_state state = {0};
uint8_t seg_count = 0U;
int err = 0;
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND)) {
rx->friend_match = bt_mesh_friend_match(rx->sub->net_idx,
rx->ctx.recv_dst);
}{...} else {
rx->friend_match = false;
}{...}
BT_DBG("src 0x%04x dst 0x%04x seq 0x%08x friend_match %u",
rx->ctx.addr, rx->ctx.recv_dst, rx->seq, rx->friend_match);
net_buf_simple_pull(buf, BLE_MESH_NET_HDR_LEN);
BT_DBG("Payload %s", bt_hex(buf->data, buf->len));
/* ... */
if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) &&
bt_mesh_lpn_established() && rx->net_if == BLE_MESH_NET_IF_ADV &&
(!bt_mesh_lpn_waiting_update() ||
rx->ctx.recv_cred != BLE_MESH_FRIENDSHIP_CRED)) {
BT_WARN("Ignoring unexpected message in Low Power mode");
return -EAGAIN;
}{...}
/* ... */
net_buf_simple_save(buf, &state);
if (SEG(buf->data)) {
/* ... */
if (!rx->local_match && !rx->friend_match) {
return 0;
}{...}
err = trans_seg(buf, rx, &pdu_type, &seq_auth, &seg_count);
}{...} else {
seg_count = 1U;
err = trans_unseg(buf, rx, &seq_auth);
}{...}
/* ... */
if (IS_ENABLED(CONFIG_BLE_MESH_LOW_POWER) &&
(bt_mesh_lpn_timer() ||
(bt_mesh_lpn_established() && bt_mesh_lpn_waiting_update()))) {
bt_mesh_lpn_msg_received(rx);
}{...}
net_buf_simple_restore(buf, &state);
if (IS_ENABLED(CONFIG_BLE_MESH_FRIEND) && rx->friend_match && !err) {
if (seq_auth == TRANS_SEQ_AUTH_NVAL) {
bt_mesh_friend_enqueue_rx(rx, pdu_type, NULL,
seg_count, buf);
}{...} else {
bt_mesh_friend_enqueue_rx(rx, pdu_type, &seq_auth,
seg_count, buf);
}{...}
}{...}
return err;
}{ ... }
void bt_mesh_rx_reset(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(seg_rx); i++) {
seg_rx_reset(&seg_rx[i], true);
}{...}
}{ ... }
void bt_mesh_tx_reset(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(seg_tx); i++) {
seg_tx_reset(&seg_tx[i]);
}{...}
}{ ... }
void bt_mesh_rx_reset_single(uint16_t src)
{
int i;
if (!BLE_MESH_ADDR_IS_UNICAST(src)) {
return;
}{...}
for (i = 0; i < ARRAY_SIZE(seg_rx); i++) {
struct seg_rx *rx = &seg_rx[i];
if (src == rx->src) {
seg_rx_reset(rx, true);
}{...}
}{...}
}{ ... }
void bt_mesh_tx_reset_single(uint16_t dst)
{
int i;
if (!BLE_MESH_ADDR_IS_UNICAST(dst)) {
return;
}{...}
for (i = 0; i < ARRAY_SIZE(seg_tx); i++) {
struct seg_tx *tx = &seg_tx[i];
if (dst == tx->dst) {
seg_tx_reset(tx);
}{...}
}{...}
}{ ... }
void bt_mesh_trans_init(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(seg_tx); i++) {
k_delayed_work_init(&seg_tx[i].rtx_timer, seg_retransmit);
}{...}
for (i = 0; i < ARRAY_SIZE(seg_rx); i++) {
k_delayed_work_init(&seg_rx[i].ack_timer, seg_ack);
seg_rx[i].buf.__buf = (seg_rx_buf_data +
(i * CONFIG_BLE_MESH_RX_SDU_MAX));
seg_rx[i].buf.data = seg_rx[i].buf.__buf;
}{...}
bt_mesh_mutex_create(&seg_tx_lock);
bt_mesh_mutex_create(&seg_rx_lock);
}{ ... }
#if CONFIG_BLE_MESH_DEINIT
void bt_mesh_trans_deinit(bool erase)
{
int i;
bt_mesh_rx_reset();
bt_mesh_tx_reset();
bt_mesh_rpl_reset(erase);
for (i = 0; i < ARRAY_SIZE(seg_tx); i++) {
k_delayed_work_free(&seg_tx[i].rtx_timer);
}{...}
for (i = 0; i < ARRAY_SIZE(seg_rx); i++) {
k_delayed_work_free(&seg_rx[i].ack_timer);
}{...}
bt_mesh_mutex_free(&seg_tx_lock);
bt_mesh_mutex_free(&seg_rx_lock);
}{ ... }
/* ... */#endif