1
2
3
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
33
35
36
37
38
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
67
68
75
76
77
78
79
80
81
85
86
91
92
100
101
102
103
104
105
106
107
108
109
110
111
114
115
118
119
122
123
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
181
182
183
184
196
197
198
199
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
232
233
234
235
236
237
238
239
240
241
242
243
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
280
281
282
286
287
288
289
290
291
292
293
297
298
304
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
335
336
337
338
339
340
341
342
343
349
350
351
352
353
364
365
366
367
368
369
370
371
372
373
376
377
378
379
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
416
417
418
419
420
421
425
426
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
480
481
482
485
486
489
492
493
494
498
499
500
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
521
522
532
533
534
535
536
537
538
544
545
546
547
548
549
550
554
555
556
557
561
562
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
634
635
638
639
643
644
645
646
647
648
654
655
656
657
658
659
660
661
665
666
667
671
672
676
677
678
679
680
681
682
683
684
685
686
690
691
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
717
718
719
723
724
728
729
730
731
732
733
734
735
736
737
738
742
743
744
745
746
747
748
749
750
751
756
757
758
759
760
761
762
766
767
768
769
770
771
772
773
774
775
779
780
781
785
786
790
791
795
796
797
798
799
800
807
808
824
825
826
827
828
829
830
831
832
839
840
841
842
843
844
845
846
847
848
849
850
851
852
855
856
857
858
859
860
861
862
863
864
865
866
867
873
877
878
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
926
927
928
932
933
934
939
940
941
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
974
975
978
979
980
981
984
985
986
987
988
989
994
995
996
997
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1032
1033
1037
1038
1039
1040
1044
1045
1049
1050
1054
1055
1059
1060
1066
1067
1068
1073
1074
1081
1082
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1157
1158
1161
1162
1163
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1183
1184
1185
1186
1187
1192
1193
1194
1195
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1219
1220
1221
1222
1223
1224
1225
1226
1227
1231
1232
1240
1241
1242
1243
1244
1245
1246
1250
1251
1255
1256
1257
1258
1259
1264
1265
1270
1271
1272
1277
1278
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1300
1301
1302
1303
1304
1305
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1369
1370
1371
1372
1373
1374
1377
1378
1379
1380
1383
1384
1385
1386
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
1423
1424
1425
1426
1427
1428
1429
1430
1431
1435
1438
1439
1440
1441
1444
1445
1446
1447
1448
1453
1454
1455
1456
1457
1458
1459
1460
1461
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1502
1503
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1524
1525
1528
1529
1532
1533
1538
1539
1540
1541
1542
1543
1544
1545
1546
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1569
1570
1571
1572
1573
1574
1575
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1593
1594
1595
1596
1597
1603
1604
1605
1606
1607
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1627
1628
1629
1630
1633
1634
1635
1638
1641
1642
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1659
1660
1661
1662
1663
1664
1665
1666
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1710
1711
1712
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1733
1734
1735
1736
1737
1738
1739
1740
1741
1744
1745
1746
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1770
1771
1772
1773
1774
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1795
1796
1797
/* ... */
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include "crypto.h"
#include "adv.h"
#include "mesh.h"
#include "transport.h"
#include "access.h"
#include "friend.h"
#include "foundation.h"
#include "mesh/main.h"
#include "mesh/common.h"
#include "pvnr_mgmt.h"13 includes
#if CONFIG_BLE_MESH_V11_SUPPORT
#include "mesh_v1.1/utils.h"
#endif
#ifdef CONFIG_BLE_MESH_FRIEND
/* ... */
#define FRIEND_BUF_COUNT ((CONFIG_BLE_MESH_FRIEND_QUEUE_SIZE + 1) * \
CONFIG_BLE_MESH_FRIEND_LPN_COUNT)...
#define FRIEND_ADV(buf) CONTAINER_OF(BLE_MESH_ADV(buf), struct friend_adv, adv)
/* ... */
#define FRIEND_XMIT BLE_MESH_TRANSMIT(0, 20)
struct friend_pdu_info {
uint16_t src;
uint16_t dst;
uint8_t seq[3];
uint8_t ttl:7,
ctl:1;
uint32_t iv_index;
}{...};
NET_BUF_POOL_FIXED_DEFINE(friend_buf_pool, FRIEND_BUF_COUNT,
BLE_MESH_ADV_DATA_SIZE, NULL);
static struct friend_adv {
struct bt_mesh_adv adv;
uint16_t app_idx;
}{...} adv_pool[FRIEND_BUF_COUNT];
enum {
BLE_MESH_FRIENDSHIP_TERMINATE_ESTABLISH_FAIL,
BLE_MESH_FRIENDSHIP_TERMINATE_POLL_TIMEOUT,
BLE_MESH_FRIENDSHIP_TERMINATE_RECV_FRND_REQ,
BLE_MESH_FRIENDSHIP_TERMINATE_RECV_FRND_CLEAR,
BLE_MESH_FRIENDSHIP_TERMINATE_DISABLE,
}{...};
static void (*friend_cb)(bool establish, uint16_t lpn_addr, uint8_t reason);
static void enqueue_update(struct bt_mesh_friend *frnd, uint8_t md);
static bool friend_init = false;
static struct bt_mesh_subnet *friend_subnet_get(uint16_t net_idx)
{
return bt_mesh_subnet_get(net_idx);
}{...}
static struct bt_mesh_adv *adv_alloc(int id)
{
adv_pool[id].app_idx = BLE_MESH_KEY_UNUSED;
return &adv_pool[id].adv;
}{...}
static bool is_lpn_unicast(struct bt_mesh_friend *frnd, uint16_t addr)
{
if (frnd->lpn == BLE_MESH_ADDR_UNASSIGNED) {
return false;
}{...}
return (addr >= frnd->lpn && addr < (frnd->lpn + frnd->num_elem));
}{...}
struct bt_mesh_friend *bt_mesh_friend_find(uint16_t net_idx, uint16_t lpn_addr,
bool valid, bool established)
{
int i;
BT_DBG("net_idx 0x%04x lpn_addr 0x%04x", net_idx, lpn_addr);
for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) {
struct bt_mesh_friend *frnd = &bt_mesh.frnd[i];
if (valid && !frnd->valid) {
continue;
}{...}
if (established && !frnd->established) {
continue;
}{...}
if (net_idx != BLE_MESH_KEY_ANY && frnd->net_idx != net_idx) {
continue;
}{...}
if (is_lpn_unicast(frnd, lpn_addr)) {
return frnd;
}{...}
}{...}
return NULL;
}{...}
static void purge_buffers(sys_slist_t *list)
{
while (!sys_slist_is_empty(list)) {
struct net_buf *buf = NULL;
buf = (void *)sys_slist_get_not_empty(list);
buf->frags = NULL;
buf->flags &= ~NET_BUF_FRAGS;
net_buf_unref(buf);
}{...}
}{...}
/* ... */
static int32_t recv_delay(struct bt_mesh_friend *frnd)
{
#if CONFIG_BLE_MESH_FRIEND_RECV_WIN > 50
return (int32_t)frnd->recv_delay + (CONFIG_BLE_MESH_FRIEND_RECV_WIN / 5);
#else
return frnd->recv_delay;
#endif
}{...}
static void friend_clear(struct bt_mesh_friend *frnd, uint8_t reason)
{
int i;
BT_DBG("LPN 0x%04x", frnd->lpn);
k_delayed_work_cancel(&frnd->timer);
if (frnd->established) {
if (friend_cb) {
friend_cb(false, frnd->lpn, reason);
}{...}
#if CONFIG_BLE_MESH_DF_SRV
struct bt_mesh_subnet *sub = friend_subnet_get(frnd->net_idx);
if (sub && sub->directed_friend == BLE_MESH_DIRECTED_FRIEND_ENABLED) {
if (bt_mesh_directed_update_dependent_node(sub, 0, frnd->lpn, frnd->num_elem)) {
BT_ERR("Friend failed to update dependent node");
}{...}
}{...}
/* ... */#endif
}{...}
friend_cred_del(frnd->net_idx, frnd->lpn);
if (frnd->last) {
if (frnd->pending_buf) {
bt_mesh_adv_buf_ref_debug(__func__, frnd->last, 2U, BLE_MESH_BUF_REF_EQUAL);
bt_mesh_atomic_set(&BLE_MESH_ADV_BUSY(frnd->last), 0);
}{...} else {
bt_mesh_adv_buf_ref_debug(__func__, frnd->last, 1U, BLE_MESH_BUF_REF_EQUAL);
}{...}
net_buf_unref(frnd->last);
frnd->last = NULL;
}{...}
purge_buffers(&frnd->queue);
for (i = 0; i < ARRAY_SIZE(frnd->seg); i++) {
struct bt_mesh_friend_seg *seg = &frnd->seg[i];
purge_buffers(&seg->queue);
seg->seg_count = 0U;
}{...}
frnd->valid = 0U;
frnd->established = 0U;
frnd->pending_buf = 0U;
frnd->fsn = 0U;
frnd->queue_size = 0U;
frnd->pending_req = 0U;
(void)memset(frnd->sub_list, 0, sizeof(frnd->sub_list));
}{...}
void bt_mesh_friend_clear_net_idx(uint16_t net_idx)
{
int i;
BT_DBG("net_idx 0x%04x", net_idx);
for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) {
struct bt_mesh_friend *frnd = &bt_mesh.frnd[i];
if (frnd->net_idx == BLE_MESH_KEY_UNUSED) {
continue;
}{...}
if (net_idx == BLE_MESH_KEY_ANY || frnd->net_idx == net_idx) {
friend_clear(frnd, BLE_MESH_FRIENDSHIP_TERMINATE_DISABLE);
}{...}
}{...}
}{...}
void bt_mesh_friend_sec_update(uint16_t net_idx)
{
int i;
BT_DBG("net_idx 0x%04x", net_idx);
for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) {
struct bt_mesh_friend *frnd = &bt_mesh.frnd[i];
if (frnd->net_idx == BLE_MESH_KEY_UNUSED) {
continue;
}{...}
if (net_idx == BLE_MESH_KEY_ANY || frnd->net_idx == net_idx) {
/* ... */
enqueue_update(frnd, 1);
}{...}
}{...}
}{...}
int bt_mesh_friend_clear(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf)
{
struct bt_mesh_ctl_friend_clear *msg = (void *)buf->data;
struct bt_mesh_friend *frnd = NULL;
uint16_t lpn_addr = 0U, lpn_counter = 0U;
struct bt_mesh_net_tx tx = {
.sub = rx->sub,
.ctx = &rx->ctx,
.src = bt_mesh_primary_addr(),
.xmit = bt_mesh_net_transmit_get(),
}{...};
struct bt_mesh_ctl_friend_clear_confirm cfm = {0};
if (buf->len < sizeof(*msg)) {
BT_WARN("Too short Friend Clear (len %d)", buf->len);
return -EINVAL;
}{...}
lpn_addr = sys_be16_to_cpu(msg->lpn_addr);
lpn_counter = sys_be16_to_cpu(msg->lpn_counter);
BT_DBG("LPN addr 0x%04x counter 0x%04x", lpn_addr, lpn_counter);
frnd = bt_mesh_friend_find(rx->sub->net_idx, lpn_addr, false, false);
if (!frnd) {
BT_WARN("No matching LPN addr 0x%04x", lpn_addr);
return 0;
}{...}
/* ... */
if (lpn_counter - frnd->lpn_counter > 255) {
BT_WARN("LPN Counter out of range (old %u new %u)",
frnd->lpn_counter, lpn_counter);
return 0;
}{...}
tx.ctx->send_ttl = BLE_MESH_TTL_MAX;
tx.ctx->send_cred = BLE_MESH_FLOODING_CRED;
tx.ctx->send_tag = BLE_MESH_TAG_IMMUTABLE_CRED;
cfm.lpn_addr = msg->lpn_addr;
cfm.lpn_counter = msg->lpn_counter;
bt_mesh_ctl_send(&tx, TRANS_CTL_OP_FRIEND_CLEAR_CFM, &cfm,
sizeof(cfm), NULL, NULL);
friend_clear(frnd, BLE_MESH_FRIENDSHIP_TERMINATE_RECV_FRND_CLEAR);
return 0;
}{...}
static bool friend_sub_exist(struct bt_mesh_friend *frnd, uint16_t addr)
{
int i;
for (i = 0; i < ARRAY_SIZE(frnd->sub_list); i++) {
if (frnd->sub_list[i] == addr) {
return true;
}{...}
}{...}
return false;
}{...}
static void friend_sub_add(struct bt_mesh_friend *frnd, uint16_t addr)
{
int i;
for (i = 0; i < ARRAY_SIZE(frnd->sub_list); i++) {
if (frnd->sub_list[i] == BLE_MESH_ADDR_UNASSIGNED) {
frnd->sub_list[i] = addr;
return;
}{...}
}{...}
BT_WARN("No space in friend subscription list");
}{...}
static void friend_sub_rem(struct bt_mesh_friend *frnd, uint16_t addr)
{
int i;
for (i = 0; i < ARRAY_SIZE(frnd->sub_list); i++) {
if (frnd->sub_list[i] == addr) {
frnd->sub_list[i] = BLE_MESH_ADDR_UNASSIGNED;
return;
}{...}
}{...}
}{...}
static struct net_buf *create_friend_pdu(struct bt_mesh_friend *frnd,
struct friend_pdu_info *info,
struct net_buf_simple *sdu)
{
struct net_buf *buf = NULL;
buf = bt_mesh_adv_create_from_pool(&friend_buf_pool, adv_alloc,
BLE_MESH_ADV_DATA, K_NO_WAIT);
if (!buf) {
return NULL;
}{...}
net_buf_add_u8(buf, (info->iv_index & 1) << 7);
if (info->ctl) {
net_buf_add_u8(buf, info->ttl | 0x80);
}{...} else {
net_buf_add_u8(buf, info->ttl);
}{...}
net_buf_add_mem(buf, info->seq, sizeof(info->seq));
net_buf_add_be16(buf, info->src);
net_buf_add_be16(buf, info->dst);
net_buf_add_mem(buf, sdu->data, sdu->len);
return buf;
}{...}
struct unseg_app_sdu_meta {
struct bt_mesh_net_rx net;
const uint8_t *key;
struct bt_mesh_subnet *subnet;
bool is_dev_key;
uint8_t aid;
uint8_t *ad;
}{...};
static int unseg_app_sdu_unpack(struct bt_mesh_friend *frnd,
struct net_buf *buf,
struct unseg_app_sdu_meta *meta)
{
uint16_t app_idx = FRIEND_ADV(buf)->app_idx;
int err = 0;
meta->subnet = friend_subnet_get(frnd->net_idx);
if (!meta->subnet) {
BT_ERR("Invalid subnet for unseg app sdu");
return -EINVAL;
}{...}
meta->is_dev_key = (app_idx == BLE_MESH_KEY_DEV);
bt_mesh_net_header_parse(&buf->b, &meta->net);
err = bt_mesh_upper_key_get(meta->subnet, app_idx, &meta->key,
&meta->aid, meta->net.ctx.addr);
if (err) {
BT_ERR("Failed to get AppKey");
return err;
}{...}
if (BLE_MESH_ADDR_IS_VIRTUAL(meta->net.ctx.recv_dst)) {
meta->ad = bt_mesh_label_uuid_get(meta->net.ctx.recv_dst);
if (!meta->ad) {
BT_ERR("Failed to get label uuid");
return -ENOENT;
}{...}
}{...} else {
meta->ad = NULL;
}{...}
return 0;
}{...}
static int unseg_app_sdu_decrypt(struct bt_mesh_friend *frnd,
struct net_buf *buf,
const struct unseg_app_sdu_meta *meta)
{
struct net_buf_simple sdu = {0};
net_buf_simple_clone(&buf->b, &sdu);
net_buf_simple_pull(&sdu, 10);
sdu.len -= 4;
return bt_mesh_app_decrypt(meta->key, meta->is_dev_key, 0, &sdu, &sdu,
meta->ad, meta->net.ctx.addr,
meta->net.ctx.recv_dst, meta->net.seq,
BLE_MESH_NET_IVI_TX);
}{...}
static int unseg_app_sdu_encrypt(struct bt_mesh_friend *frnd,
struct net_buf *buf,
const struct unseg_app_sdu_meta *meta)
{
struct net_buf_simple sdu = {0};
net_buf_simple_clone(&buf->b, &sdu);
net_buf_simple_pull(&sdu, 10);
sdu.len -= 4;
return bt_mesh_app_encrypt(meta->key, meta->is_dev_key, 0, &sdu,
meta->ad, meta->net.ctx.addr,
meta->net.ctx.recv_dst, bt_mesh.seq,
BLE_MESH_NET_IVI_TX);
}{...}
static int unseg_app_sdu_prepare(struct bt_mesh_friend *frnd,
struct net_buf *buf)
{
struct unseg_app_sdu_meta meta = {0};
int err = 0;
if (FRIEND_ADV(buf)->app_idx == BLE_MESH_KEY_UNUSED) {
return 0;
}{...}
err = unseg_app_sdu_unpack(frnd, buf, &meta);
if (err) {
return err;
}{...}
/* ... */
if (meta.net.seq == bt_mesh.seq) {
return 0;
}{...}
err = unseg_app_sdu_decrypt(frnd, buf, &meta);
if (err) {
BT_WARN("Decryption failed! %d", err);
return err;
}{...}
err = unseg_app_sdu_encrypt(frnd, buf, &meta);
if (err) {
BT_WARN("Re-encryption failed! %d", err);
}{...}
return err;
}{...}
static int encrypt_friend_pdu(struct bt_mesh_friend *frnd, struct net_buf *buf,
bool master_cred)
{
struct bt_mesh_subnet *sub = friend_subnet_get(frnd->net_idx);
const uint8_t *enc = NULL, *priv = NULL;
uint32_t iv_index = 0U;
uint16_t src = 0U;
uint8_t nid = 0U;
int err = 0;
if (!sub) {
BT_ERR("Invalid subnet to encrypt friend pdu");
return -EINVAL;
}{...}
if (master_cred) {
enc = sub->keys[sub->kr_flag].enc;
priv = sub->keys[sub->kr_flag].privacy;
nid = sub->keys[sub->kr_flag].nid;
}{...} else {
if (friend_cred_get(sub, frnd->lpn, &nid, &enc, &priv)) {
BT_ERR("friend_cred_get failed");
return -ENOENT;
}{...}
}{...}
src = sys_get_be16(&buf->data[5]);
if (bt_mesh_elem_find(src)) {
uint32_t seq;
if (FRIEND_ADV(buf)->app_idx != BLE_MESH_KEY_UNUSED) {
err = unseg_app_sdu_prepare(frnd, buf);
if (err) {
return err;
}{...}
}{...}
seq = bt_mesh_next_seq();
sys_put_be24(seq, &buf->data[2]);
iv_index = BLE_MESH_NET_IVI_TX;
FRIEND_ADV(buf)->app_idx = BLE_MESH_KEY_UNUSED;
}{...} else {
uint8_t ivi = (buf->data[0] >> 7);
iv_index = (bt_mesh.iv_index - ((bt_mesh.iv_index & 1) != ivi));
}{...}
buf->data[0] = (nid | (iv_index & 1) << 7);
if (bt_mesh_net_encrypt(enc, &buf->b, iv_index, false, false)) {
BT_ERR("Encrypting failed");
return -EINVAL;
}{...}
if (bt_mesh_net_obfuscate(buf->data, iv_index, priv)) {
BT_ERR("Obfuscating failed");
return -EINVAL;
}{...}
return 0;
}{...}
static struct net_buf *encode_friend_ctl(struct bt_mesh_friend *frnd,
uint8_t ctl_op,
struct net_buf_simple *sdu)
{
struct friend_pdu_info info = {0};
BT_DBG("LPN 0x%04x", frnd->lpn);
net_buf_simple_push_u8(sdu, TRANS_CTL_HDR(ctl_op, 0));
info.src = bt_mesh_primary_addr();
info.dst = frnd->lpn;
info.ctl = 1U;
info.ttl = 0U;
memset(info.seq, 0, sizeof(info.seq));
info.iv_index = BLE_MESH_NET_IVI_TX;
return create_friend_pdu(frnd, &info, sdu);
}{...}
static struct net_buf *encode_update(struct bt_mesh_friend *frnd, uint8_t md)
{
struct bt_mesh_ctl_friend_update *upd = NULL;
NET_BUF_SIMPLE_DEFINE(sdu, 1 + sizeof(*upd));
struct bt_mesh_subnet *sub = friend_subnet_get(frnd->net_idx);
if (!sub) {
BT_ERR("Friend subnet 0x%04x not found", frnd->net_idx);
return NULL;
}{...}
BT_DBG("lpn 0x%04x md 0x%02x", frnd->lpn, md);
net_buf_simple_reserve(&sdu, 1);
upd = net_buf_simple_add(&sdu, sizeof(*upd));
upd->flags = bt_mesh_net_flags(sub);
upd->iv_index = sys_cpu_to_be32(bt_mesh.iv_index);
upd->md = md;
return encode_friend_ctl(frnd, TRANS_CTL_OP_FRIEND_UPDATE, &sdu);
}{...}
static void enqueue_sub_cfm(struct bt_mesh_friend *frnd, uint8_t xact)
{
struct bt_mesh_ctl_friend_sub_confirm *cfm = NULL;
NET_BUF_SIMPLE_DEFINE(sdu, 1 + sizeof(*cfm));
struct net_buf *buf = NULL;
BT_DBG("lpn 0x%04x xact 0x%02x", frnd->lpn, xact);
net_buf_simple_reserve(&sdu, 1);
cfm = net_buf_simple_add(&sdu, sizeof(*cfm));
cfm->xact = xact;
buf = encode_friend_ctl(frnd, TRANS_CTL_OP_FRIEND_SUB_CFM, &sdu);
if (!buf) {
BT_ERR("Unable to encode Subscription List Confirmation");
return;
}{...}
if (encrypt_friend_pdu(frnd, buf, false)) {
return;
}{...}
if (frnd->last) {
BT_DBG("Discarding last PDU");
net_buf_unref(frnd->last);
}{...}
frnd->last = buf;
frnd->send_last = 1U;
}{...}
static void friend_recv_delay(struct bt_mesh_friend *frnd)
{
frnd->pending_req = 1U;
k_delayed_work_submit(&frnd->timer, recv_delay(frnd));
BT_INFO("Waiting RecvDelay of %d ms", recv_delay(frnd));
}{...}
int bt_mesh_friend_sub_add(struct bt_mesh_net_rx *rx,
struct net_buf_simple *buf)
{
struct bt_mesh_friend *frnd = NULL;
uint8_t xact = 0U;
if (buf->len < BLE_MESH_FRIEND_SUB_MIN_LEN) {
BT_WARN("Too short Friend Subscription Add (len %d)", buf->len);
return -EINVAL;
}{...}
frnd = bt_mesh_friend_find(rx->sub->net_idx, rx->ctx.addr, true, true);
if (!frnd) {
BT_WARN("No matching LPN addr 0x%04x", rx->ctx.addr);
return 0;
}{...}
if (frnd->pending_buf) {
BT_WARN("Previous buffer not yet sent!");
return 0;
}{...}
friend_recv_delay(frnd);
xact = net_buf_simple_pull_u8(buf);
while (buf->len >= 2U) {
uint16_t addr = net_buf_simple_pull_be16(buf);
if (!BLE_MESH_ADDR_IS_GROUP(addr) &&
!BLE_MESH_ADDR_IS_VIRTUAL(addr) &&
!BLE_MESH_ADDR_IS_FIXED_GROUP(addr)) {
BT_WARN("Invalid friend sub addr 0x%04x to add", addr);
continue;
}{...}
if (friend_sub_exist(frnd, addr)) {
continue;
}{...}
friend_sub_add(frnd, addr);
}{...}
enqueue_sub_cfm(frnd, xact);
#if CONFIG_BLE_MESH_DF_SRV
return bt_mesh_directed_friend_solicitation(frnd, rx->sub);
#else
return 0;
#endif
}{...}
int bt_mesh_friend_sub_rem(struct bt_mesh_net_rx *rx,
struct net_buf_simple *buf)
{
struct bt_mesh_friend *frnd = NULL;
uint8_t xact = 0U;
if (buf->len < BLE_MESH_FRIEND_SUB_MIN_LEN) {
BT_WARN("Too short Friend Subscription Remove (len %d)", buf->len);
return -EINVAL;
}{...}
frnd = bt_mesh_friend_find(rx->sub->net_idx, rx->ctx.addr, true, true);
if (!frnd) {
BT_WARN("No matching LPN addr 0x%04x", rx->ctx.addr);
return 0;
}{...}
if (frnd->pending_buf) {
BT_WARN("Previous buffer not yet sent!");
return 0;
}{...}
friend_recv_delay(frnd);
xact = net_buf_simple_pull_u8(buf);
while (buf->len >= 2U) {
uint16_t addr = net_buf_simple_pull_be16(buf);
if (!BLE_MESH_ADDR_IS_GROUP(addr) &&
!BLE_MESH_ADDR_IS_VIRTUAL(addr) &&
!BLE_MESH_ADDR_IS_FIXED_GROUP(addr)) {
BT_WARN("Invalid friend sub addr 0x%04x to remove", addr);
continue;
}{...}
friend_sub_rem(frnd, addr);
}{...}
enqueue_sub_cfm(frnd, xact);
return 0;
}{...}
static void enqueue_buf(struct bt_mesh_friend *frnd, struct net_buf *buf)
{
net_buf_slist_put(&frnd->queue, buf);
frnd->queue_size++;
}{...}
static void enqueue_update(struct bt_mesh_friend *frnd, uint8_t md)
{
struct net_buf *buf = NULL;
buf = encode_update(frnd, md);
if (!buf) {
BT_ERR("Unable to encode Friend Update");
return;
}{...}
enqueue_buf(frnd, buf);
}{...}
int bt_mesh_friend_poll(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf)
{
struct bt_mesh_ctl_friend_poll *msg = (void *)buf->data;
struct bt_mesh_friend *frnd = NULL;
if (buf->len < sizeof(*msg)) {
BT_WARN("Too short Friend Poll (len %d)", buf->len);
return -EINVAL;
}{...}
frnd = bt_mesh_friend_find(rx->sub->net_idx, rx->ctx.addr, true, false);
if (!frnd) {
BT_WARN("No matching LPN addr 0x%04x", rx->ctx.addr);
return 0;
}{...}
if (msg->fsn & ~1) {
BT_WARN("Prohibited (non-zero) padding bits");
return -EINVAL;
}{...}
if (frnd->pending_buf) {
BT_WARN("Previous buffer not yet sent!");
return 0;
}{...}
BT_DBG("msg->fsn %u frnd->fsn %u", (msg->fsn & 1), frnd->fsn);
friend_recv_delay(frnd);
if (!frnd->established) {
BT_INFO("Friendship established with 0x%04x", frnd->lpn);
frnd->established = 1U;
if (friend_cb) {
friend_cb(true, frnd->lpn, 0);
}{...}
}{...}
if (msg->fsn == frnd->fsn && frnd->last) {
BT_DBG("Re-sending last PDU");
frnd->send_last = 1U;
}{...} else {
if (frnd->last) {
net_buf_unref(frnd->last);
frnd->last = NULL;
}{...}
frnd->fsn = msg->fsn;
if (sys_slist_is_empty(&frnd->queue)) {
enqueue_update(frnd, 0);
BT_DBG("Enqueued Friend Update to empty queue");
}{...}
}{...}
return 0;
}{...}
static struct bt_mesh_friend *find_clear(uint16_t prev_friend)
{
int i;
for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) {
struct bt_mesh_friend *frnd = &bt_mesh.frnd[i];
if (frnd->clear.frnd == prev_friend) {
return frnd;
}{...}
}{...}
return NULL;
}{...}
static void friend_clear_sent(int err, void *user_data)
{
struct bt_mesh_friend *frnd = user_data;
k_delayed_work_submit(&frnd->clear.timer,
K_SECONDS(frnd->clear.repeat_sec));
frnd->clear.repeat_sec *= 2U;
}{...}
static const struct bt_mesh_send_cb clear_sent_cb = {
.end = friend_clear_sent,
}{...};
static void send_friend_clear(struct bt_mesh_friend *frnd)
{
struct bt_mesh_msg_ctx ctx = {
.net_idx = frnd->net_idx,
.app_idx = BLE_MESH_KEY_UNUSED,
.addr = frnd->clear.frnd,
.send_ttl = BLE_MESH_TTL_MAX,
.send_cred = BLE_MESH_FLOODING_CRED,
.send_tag = BLE_MESH_TAG_IMMUTABLE_CRED,
}{...};
struct bt_mesh_net_tx tx = {
.sub = friend_subnet_get(frnd->net_idx),
.ctx = &ctx,
.src = bt_mesh_primary_addr(),
.xmit = bt_mesh_net_transmit_get(),
}{...};
struct bt_mesh_ctl_friend_clear req = {
.lpn_addr = sys_cpu_to_be16(frnd->lpn),
.lpn_counter = sys_cpu_to_be16(frnd->lpn_counter),
}{...};
if (!tx.sub) {
BT_ERR("Invalid subnet for Friend Clear");
return;
}{...}
bt_mesh_ctl_send(&tx, TRANS_CTL_OP_FRIEND_CLEAR, &req,
sizeof(req), &clear_sent_cb, frnd);
}{...}
static void clear_timeout(struct k_work *work)
{
struct bt_mesh_friend *frnd = CONTAINER_OF(work, struct bt_mesh_friend,
clear.timer.work);
uint32_t duration = 0U;
BT_DBG("LPN 0x%04x (old) Friend 0x%04x", frnd->lpn, frnd->clear.frnd);
duration = k_uptime_get_32() - frnd->clear.start;
if (duration > 2 * frnd->poll_to) {
BT_DBG("Clear Procedure timer expired");
frnd->clear.frnd = BLE_MESH_ADDR_UNASSIGNED;
return;
}{...}
send_friend_clear(frnd);
}{...}
static void clear_procedure_start(struct bt_mesh_friend *frnd)
{
BT_DBG("LPN 0x%04x (old) Friend 0x%04x", frnd->lpn, frnd->clear.frnd);
frnd->clear.start = k_uptime_get_32();
frnd->clear.repeat_sec = 1U;
send_friend_clear(frnd);
}{...}
int bt_mesh_friend_clear_cfm(struct bt_mesh_net_rx *rx,
struct net_buf_simple *buf)
{
struct bt_mesh_ctl_friend_clear_confirm *msg = (void *)buf->data;
struct bt_mesh_friend *frnd = NULL;
uint16_t lpn_addr = 0U, lpn_counter = 0U;
if (buf->len < sizeof(*msg)) {
BT_WARN("Too short Friend Clear Confirm (len %d)", buf->len);
return -EINVAL;
}{...}
frnd = find_clear(rx->ctx.addr);
if (!frnd) {
BT_WARN("No pending clear procedure for 0x%02x", rx->ctx.addr);
return 0;
}{...}
lpn_addr = sys_be16_to_cpu(msg->lpn_addr);
if (lpn_addr != frnd->lpn) {
BT_WARN("LPN address mismatch (0x%04x != 0x%04x)",
lpn_addr, frnd->lpn);
return 0;
}{...}
lpn_counter = sys_be16_to_cpu(msg->lpn_counter);
if (lpn_counter != frnd->lpn_counter) {
BT_WARN("LPN counter mismatch (0x%04x != 0x%04x)",
lpn_counter, frnd->lpn_counter);
return 0;
}{...}
k_delayed_work_cancel(&frnd->clear.timer);
frnd->clear.frnd = BLE_MESH_ADDR_UNASSIGNED;
return 0;
}{...}
static void enqueue_offer(struct bt_mesh_friend *frnd, int8_t rssi)
{
struct bt_mesh_ctl_friend_offer *off = NULL;
NET_BUF_SIMPLE_DEFINE(sdu, 1 + sizeof(*off));
struct net_buf *buf = NULL;
net_buf_simple_reserve(&sdu, 1);
off = net_buf_simple_add(&sdu, sizeof(*off));
off->recv_win = CONFIG_BLE_MESH_FRIEND_RECV_WIN;
off->queue_size = CONFIG_BLE_MESH_FRIEND_QUEUE_SIZE;
off->sub_list_size = ARRAY_SIZE(frnd->sub_list);
off->rssi = rssi;
off->frnd_counter = sys_cpu_to_be16(frnd->counter);
buf = encode_friend_ctl(frnd, TRANS_CTL_OP_FRIEND_OFFER, &sdu);
if (!buf) {
BT_ERR("Unable to encode Friend Offer");
return;
}{...}
if (encrypt_friend_pdu(frnd, buf, true)) {
return;
}{...}
frnd->counter++;
if (frnd->last) {
net_buf_unref(frnd->last);
}{...}
frnd->last = buf;
frnd->send_last = 1U;
}{...}
#define RECV_WIN CONFIG_BLE_MESH_FRIEND_RECV_WIN
#define RSSI_FACT(crit) (((crit) >> 5) & (uint8_t)BIT_MASK(2))
#define RECV_WIN_FACT(crit) (((crit) >> 3) & (uint8_t)BIT_MASK(2))
#define MIN_QUEUE_SIZE_LOG(crit) ((crit) & (uint8_t)BIT_MASK(3))
#define MIN_QUEUE_SIZE(crit) ((uint32_t)BIT(MIN_QUEUE_SIZE_LOG(crit)))5 defines
static int32_t offer_delay(struct bt_mesh_friend *frnd, int8_t rssi, uint8_t crit)
{
/* ... */
static const uint8_t fact[] = { 10, 15, 20, 25 };
int32_t delay = 0;
BT_INFO("ReceiveWindowFactor %u ReceiveWindow %u RSSIFactor %u RSSI %d",
fact[RECV_WIN_FACT(crit)], RECV_WIN,
fact[RSSI_FACT(crit)], rssi);
delay = (int32_t)fact[RECV_WIN_FACT(crit)] * RECV_WIN;
delay -= (int32_t)fact[RSSI_FACT(crit)] * rssi;
delay /= 10;
BT_DBG("Local Delay calculated as %d ms", delay);
if (delay < 100) {
return K_MSEC(100);
}{...}
return K_MSEC(delay);
}{...}
int bt_mesh_friend_req(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf)
{
struct bt_mesh_ctl_friend_req *msg = (void *)buf->data;
struct bt_mesh_friend *frnd = NULL;
uint32_t poll_to = 0U;
int i;
if (buf->len < sizeof(*msg)) {
BT_WARN("Too short Friend Request (len %d)", buf->len);
return -EINVAL;
}{...}
if (msg->recv_delay <= 0x09) {
BT_WARN("Prohibited ReceiveDelay (0x%02x)", msg->recv_delay);
return -EINVAL;
}{...}
poll_to = sys_get_be24(msg->poll_to);
if (poll_to <= 0x000009 || poll_to >= 0x34bc00) {
BT_WARN("Prohibited PollTimeout (0x%06x)", poll_to);
return -EINVAL;
}{...}
if (msg->num_elem == 0x00) {
BT_WARN("Prohibited NumElements value (0x00)");
return -EINVAL;
}{...}
if (!BLE_MESH_ADDR_IS_UNICAST(rx->ctx.addr + msg->num_elem - 1)) {
BT_WARN("LPN elements stretch outside of unicast range");
return -EINVAL;
}{...}
if (!MIN_QUEUE_SIZE_LOG(msg->criteria)) {
BT_WARN("Prohibited Minimum Queue Size in Friend Request");
return -EINVAL;
}{...}
if (CONFIG_BLE_MESH_FRIEND_QUEUE_SIZE < MIN_QUEUE_SIZE(msg->criteria)) {
BT_WARN("We have a too small Friend Queue size (%u < %u)",
CONFIG_BLE_MESH_FRIEND_QUEUE_SIZE,
MIN_QUEUE_SIZE(msg->criteria));
return 0;
}{...}
frnd = bt_mesh_friend_find(rx->sub->net_idx, rx->ctx.addr, true, false);
if (frnd) {
BT_WARN("Existing LPN re-requesting Friendship");
friend_clear(frnd, BLE_MESH_FRIENDSHIP_TERMINATE_RECV_FRND_REQ);
goto init_friend;
}{...}
for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) {
if (!bt_mesh.frnd[i].valid) {
frnd = &bt_mesh.frnd[i];
frnd->valid = 1U;
break;
}{...}
}{...}
if (!frnd) {
BT_WARN("No free Friend contexts for new LPN");
return -ENOMEM;
}{...}
init_friend:
frnd->lpn = rx->ctx.addr;
frnd->num_elem = msg->num_elem;
frnd->net_idx = rx->sub->net_idx;
frnd->recv_delay = msg->recv_delay;
frnd->poll_to = poll_to * 100U;
frnd->lpn_counter = sys_be16_to_cpu(msg->lpn_counter);
frnd->clear.frnd = sys_be16_to_cpu(msg->prev_addr);
BT_INFO("LPN 0x%04x rssi %d recv_delay %u poll_to %ums",
frnd->lpn, rx->ctx.recv_rssi, frnd->recv_delay, frnd->poll_to);
/* ... */
if (BLE_MESH_ADDR_IS_UNICAST(frnd->clear.frnd) &&
!bt_mesh_elem_find(frnd->clear.frnd)) {
clear_procedure_start(frnd);
}{...}
k_delayed_work_submit(&frnd->timer,
offer_delay(frnd, rx->ctx.recv_rssi,
msg->criteria));
friend_cred_create(rx->sub, frnd->lpn, frnd->lpn_counter,
frnd->counter);
enqueue_offer(frnd, rx->ctx.recv_rssi);
return 0;
}{...}
static bool is_seg(struct bt_mesh_friend_seg *seg, uint16_t src, uint16_t seq_zero)
{
struct net_buf *buf = (void *)sys_slist_peek_head(&seg->queue);
struct net_buf_simple_state state = {0};
uint16_t buf_seq_zero = 0U;
uint16_t buf_src = 0U;
if (!buf) {
return false;
}{...}
net_buf_simple_save(&buf->b, &state);
net_buf_skip(buf, 5);
buf_src = net_buf_pull_be16(buf);
net_buf_skip(buf, 3);
buf_seq_zero = ((net_buf_pull_be16(buf) >> 2) & TRANS_SEQ_ZERO_MASK);
net_buf_simple_restore(&buf->b, &state);
return ((src == buf_src) && (seq_zero == buf_seq_zero));
}{...}
static struct bt_mesh_friend_seg *get_seg(struct bt_mesh_friend *frnd,
uint16_t src, uint16_t seq_zero,
uint8_t seg_count)
{
struct bt_mesh_friend_seg *unassigned = NULL;
int i;
for (i = 0; i < ARRAY_SIZE(frnd->seg); i++) {
struct bt_mesh_friend_seg *seg = &frnd->seg[i];
if (is_seg(seg, src, seq_zero)) {
return seg;
}{...}
if (!unassigned && !sys_slist_peek_head(&seg->queue)) {
unassigned = seg;
}{...}
}{...}
if (unassigned) {
unassigned->seg_count = seg_count;
}{...}
return unassigned;
}{...}
static void enqueue_friend_pdu(struct bt_mesh_friend *frnd,
enum bt_mesh_friend_pdu_type type,
uint16_t src, uint8_t seg_count,
struct net_buf *buf)
{
struct bt_mesh_friend_seg *seg = NULL;
BT_DBG("type %u", type);
if (type == BLE_MESH_FRIEND_PDU_SINGLE) {
enqueue_buf(frnd, buf);
return;
}{...}
uint16_t seq_zero = (((buf->data[10] << 8 | buf->data[11]) >> 2) & TRANS_SEQ_ZERO_MASK);
seg = get_seg(frnd, src, seq_zero, seg_count);
if (!seg) {
BT_ERR("No free friend segment RX contexts for 0x%04x", src);
net_buf_unref(buf);
return;
}{...}
net_buf_slist_put(&seg->queue, buf);
if (type == BLE_MESH_FRIEND_PDU_COMPLETE) {
sys_slist_merge_slist(&frnd->queue, &seg->queue);
frnd->queue_size += seg->seg_count;
seg->seg_count = 0U;
}{...} else {
buf->flags |= NET_BUF_FRAGS;
}{...}
}{...}
static void buf_send_start(uint16_t duration, int err, void *user_data)
{
struct bt_mesh_friend *frnd = user_data;
BT_DBG("err %d", err);
frnd->pending_buf = 0U;
if (!frnd->established) {
net_buf_unref(frnd->last);
frnd->last = NULL;
}{...}
}{...}
static void buf_send_end(int err, void *user_data)
{
struct bt_mesh_friend *frnd = user_data;
BT_DBG("err %d", err);
if (frnd->pending_req) {
BT_WARN("Another request before previous completed sending");
return;
}{...}
if (frnd->established) {
k_delayed_work_submit(&frnd->timer, frnd->poll_to);
BT_DBG("Waiting %u ms for next poll", frnd->poll_to);
}{...} else {
k_delayed_work_submit(&frnd->timer, K_SECONDS(1));
BT_DBG("Waiting for first poll");
}{...}
}{...}
static void friend_timeout(struct k_work *work)
{
struct bt_mesh_friend *frnd = CONTAINER_OF(work, struct bt_mesh_friend,
timer.work);
static const struct bt_mesh_send_cb buf_sent_cb = {
.start = buf_send_start,
.end = buf_send_end,
}{...};
if (frnd->pending_buf != 0U) {
BT_ERR("Previous buffer not yet sent!");
return;
}{...}
BT_DBG("lpn 0x%04x send_last %u last %p", frnd->lpn,
frnd->send_last, frnd->last);
if (frnd->send_last && frnd->last) {
BT_DBG("Sending frnd->last %p", frnd->last);
frnd->send_last = 0U;
goto send_last;
}{...}
if (frnd->established && !frnd->pending_req) {
BT_WARN("Friendship lost with 0x%04x", frnd->lpn);
friend_clear(frnd, BLE_MESH_FRIENDSHIP_TERMINATE_POLL_TIMEOUT);
return;
}{...}
frnd->last = (void *)sys_slist_get(&frnd->queue);
if (!frnd->last) {
BT_WARN("Friendship not established with 0x%04x", frnd->lpn);
friend_clear(frnd, BLE_MESH_FRIENDSHIP_TERMINATE_ESTABLISH_FAIL);
return;
}{...}
if (encrypt_friend_pdu(frnd, frnd->last, false)) {
return;
}{...}
frnd->last->flags &= ~NET_BUF_FRAGS;
frnd->last->frags = NULL;
BT_DBG("Sending buf %p from Friend Queue of LPN 0x%04x",
frnd->last, frnd->lpn);
frnd->queue_size--;
send_last:
frnd->pending_req = 0U;
frnd->pending_buf = 1U;
bt_mesh_adv_send(frnd->last, FRIEND_XMIT, &buf_sent_cb, frnd);
}{...}
void bt_mesh_friend_set_cb(void (*cb)(bool establish, uint16_t lpn_addr, uint8_t reason))
{
friend_cb = cb;
}{...}
int bt_mesh_friend_init(void)
{
int i;
if (friend_init == true) {
BT_WARN("%s, Already", __func__);
return -EALREADY;
}{...}
for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) {
struct bt_mesh_friend *frnd = &bt_mesh.frnd[i];
int j;
frnd->net_idx = BLE_MESH_KEY_UNUSED;
sys_slist_init(&frnd->queue);
k_delayed_work_init(&frnd->timer, friend_timeout);
k_delayed_work_init(&frnd->clear.timer, clear_timeout);
for (j = 0; j < ARRAY_SIZE(frnd->seg); j++) {
sys_slist_init(&frnd->seg[j].queue);
}{...}
}{...}
friend_init = true;
return 0;
}{...}
#if CONFIG_BLE_MESH_DEINIT
int bt_mesh_friend_deinit(void)
{
int i;
if (friend_init == false) {
BT_WARN("%s, Already", __func__);
return -EALREADY;
}{...}
bt_mesh_friend_clear_net_idx(BLE_MESH_KEY_ANY);
for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) {
struct bt_mesh_friend *frnd = &bt_mesh.frnd[i];
frnd->net_idx = BLE_MESH_KEY_UNUSED;
k_delayed_work_free(&frnd->timer);
k_delayed_work_free(&frnd->clear.timer);
}{...}
bt_mesh_unref_buf_from_pool(&friend_buf_pool);
memset(adv_pool, 0, sizeof(adv_pool));
friend_init = false;
return 0;
}{...}
/* ... */#endif
static bool is_segack(struct net_buf *buf, const uint64_t *seqauth, uint16_t src)
{
struct net_buf_simple_state state = {0};
bool found = false;
if (buf->len != 16) {
return false;
}{...}
net_buf_simple_save(&buf->b, &state);
net_buf_skip(buf, 1);
if (!(net_buf_pull_u8(buf) >> 7)) {
goto end;
}{...}
net_buf_pull(buf, 3);
if (src != net_buf_pull_be16(buf)) {
goto end;
}{...}
net_buf_skip(buf, 2);
if (TRANS_CTL_OP((uint8_t *) net_buf_pull_mem(buf, 1)) != TRANS_CTL_OP_ACK) {
goto end;
}{...}
found = ((net_buf_pull_be16(buf) >> 2) & TRANS_SEQ_ZERO_MASK) ==
(*seqauth & TRANS_SEQ_ZERO_MASK);
end:
net_buf_simple_restore(&buf->b, &state);
return found;
}{...}
static void friend_purge_old_ack(struct bt_mesh_friend *frnd,
const uint64_t *seq_auth, uint16_t src)
{
sys_snode_t *cur = NULL, *prev = NULL;
BT_DBG("SeqAuth %llx src 0x%04x", *seq_auth, src);
for (cur = sys_slist_peek_head(&frnd->queue);
cur != NULL; prev = cur, cur = sys_slist_peek_next(cur)) {
struct net_buf *buf = (void *)cur;
if (is_segack(buf, seq_auth, src)) {
BT_DBG("Removing old ack from Friend Queue");
sys_slist_remove(&frnd->queue, prev, cur);
frnd->queue_size--;
buf->frags = NULL;
net_buf_unref(buf);
break;
}{...}
}{...}
}{...}
static void friend_lpn_enqueue_rx(struct bt_mesh_friend *frnd,
struct bt_mesh_net_rx *rx,
enum bt_mesh_friend_pdu_type type,
const uint64_t *seq_auth, uint8_t seg_count,
struct net_buf_simple *sbuf)
{
struct friend_pdu_info info = {0};
struct net_buf *buf = NULL;
/* ... */
if (bt_mesh_elem_find(rx->ctx.addr)) {
return;
}{...}
BT_DBG("LPN 0x%04x queue_size %u", frnd->lpn, frnd->queue_size);
if (type == BLE_MESH_FRIEND_PDU_SINGLE && seq_auth) {
friend_purge_old_ack(frnd, seq_auth, rx->ctx.addr);
}{...}
info.src = rx->ctx.addr;
info.dst = rx->ctx.recv_dst;
if (rx->net_if == BLE_MESH_NET_IF_LOCAL) {
info.ttl = rx->ctx.recv_ttl;
}{...} else {
info.ttl = rx->ctx.recv_ttl - 1U;
}{...}
info.ctl = rx->ctl;
sys_put_be24(rx->seq, info.seq);
info.iv_index = BLE_MESH_NET_IVI_RX(rx);
buf = create_friend_pdu(frnd, &info, sbuf);
if (!buf) {
BT_ERR("Failed to encode Friend buffer");
return;
}{...}
enqueue_friend_pdu(frnd, type, info.src, seg_count, buf);
BT_DBG("Queued message for LPN 0x%04x, queue_size %u",
frnd->lpn, frnd->queue_size);
}{...}
static void friend_lpn_enqueue_tx(struct bt_mesh_friend *frnd,
struct bt_mesh_net_tx *tx,
enum bt_mesh_friend_pdu_type type,
const uint64_t *seq_auth, uint8_t seg_count,
struct net_buf_simple *sbuf)
{
struct friend_pdu_info info = {0};
struct net_buf *buf = NULL;
BT_DBG("LPN 0x%04x", frnd->lpn);
if (type == BLE_MESH_FRIEND_PDU_SINGLE && seq_auth) {
friend_purge_old_ack(frnd, seq_auth, tx->src);
}{...}
info.src = tx->src;
info.dst = tx->ctx->addr;
info.ttl = tx->ctx->send_ttl;
info.ctl = (tx->ctx->app_idx == BLE_MESH_KEY_UNUSED);
sys_put_be24(bt_mesh.seq, info.seq);
info.iv_index = BLE_MESH_NET_IVI_TX;
buf = create_friend_pdu(frnd, &info, sbuf);
if (!buf) {
BT_ERR("Failed to encode Friend buffer");
return;
}{...}
if (type == BLE_MESH_FRIEND_PDU_SINGLE && !info.ctl) {
/* ... */
FRIEND_ADV(buf)->app_idx = tx->ctx->app_idx;
}{...}
enqueue_friend_pdu(frnd, type, info.src, seg_count, buf);
BT_DBG("Queued message for LPN 0x%04x", frnd->lpn);
}{...}
static bool friend_lpn_matches(struct bt_mesh_friend *frnd, uint16_t net_idx,
uint16_t addr)
{
int i;
if (!frnd->established) {
return false;
}{...}
if (net_idx != frnd->net_idx) {
return false;
}{...}
if (BLE_MESH_ADDR_IS_UNICAST(addr)) {
return is_lpn_unicast(frnd, addr);
}{...}
for (i = 0; i < ARRAY_SIZE(frnd->sub_list); i++) {
if (frnd->sub_list[i] == addr) {
return true;
}{...}
}{...}
return false;
}{...}
bool bt_mesh_friend_match(uint16_t net_idx, uint16_t addr)
{
int i;
for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) {
struct bt_mesh_friend *frnd = &bt_mesh.frnd[i];
if (friend_lpn_matches(frnd, net_idx, addr)) {
BT_DBG("LPN 0x%04x matched address 0x%04x",
frnd->lpn, addr);
return true;
}{...}
}{...}
BT_DBG("No matching LPN for address 0x%04x", addr);
return false;
}{...}
bool bt_mesh_friend_unicast_match(uint16_t net_idx, uint16_t addr, uint8_t *selem)
{
int i;
if (!BLE_MESH_ADDR_IS_UNICAST(addr) || selem == NULL) {
BT_ERR("%s, Invalid parameter", __func__);
return false;
}{...}
for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) {
struct bt_mesh_friend *frnd = &bt_mesh.frnd[i];
if (frnd->established && net_idx == frnd->net_idx &&
BLE_MESH_ADDR_IS_UNICAST(frnd->lpn) &&
(addr >= frnd->lpn && addr < (frnd->lpn + frnd->num_elem))) {
*selem = frnd->num_elem - 1;
return true;
}{...}
}{...}
return false;
}{...}
static bool friend_queue_has_space(struct bt_mesh_friend *frnd, uint16_t addr,
const uint64_t *seq_auth, uint8_t seg_count)
{
uint32_t total = 0U;
int i;
if (seg_count > CONFIG_BLE_MESH_FRIEND_QUEUE_SIZE) {
return false;
}{...}
for (i = 0; i < ARRAY_SIZE(frnd->seg); i++) {
struct bt_mesh_friend_seg *seg = &frnd->seg[i];
if (seq_auth && is_seg(seg, addr, *seq_auth & TRANS_SEQ_ZERO_MASK)) {
/* ... */
return true;
}{...}
total += seg->seg_count;
}{...}
/* ... */
return (CONFIG_BLE_MESH_FRIEND_QUEUE_SIZE - total) > seg_count;
}{...}
bool bt_mesh_friend_queue_has_space(uint16_t net_idx, uint16_t src, uint16_t dst,
const uint64_t *seq_auth, uint8_t seg_count)
{
bool someone_has_space = false, friend_match = false;
int i;
for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) {
struct bt_mesh_friend *frnd = &bt_mesh.frnd[i];
if (!friend_lpn_matches(frnd, net_idx, dst)) {
continue;
}{...}
friend_match = true;
if (friend_queue_has_space(frnd, src, seq_auth, seg_count)) {
someone_has_space = true;
}{...}
}{...}
/* ... */
if (!friend_match) {
return true;
}{...}
/* ... */
return someone_has_space;
}{...}
static bool friend_queue_prepare_space(struct bt_mesh_friend *frnd, uint16_t addr,
const uint64_t *seq_auth, uint8_t seg_count)
{
bool pending_segments = false;
uint8_t avail_space = 0U;
if (!friend_queue_has_space(frnd, addr, seq_auth, seg_count)) {
return false;
}{...}
avail_space = CONFIG_BLE_MESH_FRIEND_QUEUE_SIZE - frnd->queue_size;
pending_segments = false;
while (pending_segments || avail_space < seg_count) {
struct net_buf *buf = (void *)sys_slist_get(&frnd->queue);
if (!buf) {
BT_ERR("Unable to free up enough buffers");
return false;
}{...}
frnd->queue_size--;
avail_space++;
pending_segments = (buf->flags & NET_BUF_FRAGS);
buf->frags = NULL;
buf->flags &= ~NET_BUF_FRAGS;
net_buf_unref(buf);
}{...}
return true;
}{...}
void bt_mesh_friend_enqueue_rx(struct bt_mesh_net_rx *rx,
enum bt_mesh_friend_pdu_type type,
const uint64_t *seq_auth, uint8_t seg_count,
struct net_buf_simple *sbuf)
{
int i;
if (!rx->friend_match ||
(rx->ctx.recv_ttl <= 1U && rx->net_if != BLE_MESH_NET_IF_LOCAL) ||
bt_mesh_friend_get() != BLE_MESH_FRIEND_ENABLED) {
return;
}{...}
BT_DBG("recv_ttl %u net_idx 0x%04x src 0x%04x dst 0x%04x",
rx->ctx.recv_ttl, rx->sub->net_idx, rx->ctx.addr,
rx->ctx.recv_dst);
for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) {
struct bt_mesh_friend *frnd = &bt_mesh.frnd[i];
if (!friend_lpn_matches(frnd, rx->sub->net_idx,
rx->ctx.recv_dst)) {
continue;
}{...}
if (!friend_queue_prepare_space(frnd, rx->ctx.addr, seq_auth,
seg_count)) {
continue;
}{...}
friend_lpn_enqueue_rx(frnd, rx, type, seq_auth, seg_count,
sbuf);
}{...}
}{...}
bool bt_mesh_friend_enqueue_tx(struct bt_mesh_net_tx *tx,
enum bt_mesh_friend_pdu_type type,
const uint64_t *seq_auth, uint8_t seg_count,
struct net_buf_simple *sbuf)
{
bool matched = false;
int i;
if (!bt_mesh_friend_match(tx->sub->net_idx, tx->ctx->addr) ||
bt_mesh_friend_get() != BLE_MESH_FRIEND_ENABLED) {
return matched;
}{...}
BT_DBG("net_idx 0x%04x dst 0x%04x src 0x%04x", tx->sub->net_idx,
tx->ctx->addr, tx->src);
for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) {
struct bt_mesh_friend *frnd = &bt_mesh.frnd[i];
if (!friend_lpn_matches(frnd, tx->sub->net_idx,
tx->ctx->addr)) {
continue;
}{...}
if (!friend_queue_prepare_space(frnd, tx->src, seq_auth,
seg_count)) {
continue;
}{...}
friend_lpn_enqueue_tx(frnd, tx, type, seq_auth, seg_count,
sbuf);
matched = true;
}{...}
return matched;
}{...}
void bt_mesh_friend_clear_incomplete(struct bt_mesh_subnet *sub, uint16_t src,
uint16_t dst, const uint64_t *seq_auth)
{
int i;
for (i = 0; i < ARRAY_SIZE(bt_mesh.frnd); i++) {
struct bt_mesh_friend *frnd = &bt_mesh.frnd[i];
int j;
if (!friend_lpn_matches(frnd, sub->net_idx, dst)) {
continue;
}{...}
for (j = 0; j < ARRAY_SIZE(frnd->seg); j++) {
struct bt_mesh_friend_seg *seg = &frnd->seg[j];
if (!is_seg(seg, src, *seq_auth & TRANS_SEQ_ZERO_MASK)) {
continue;
}{...}
BT_WARN("Clearing incomplete segments for 0x%04x", src);
purge_buffers(&seg->queue);
seg->seg_count = 0U;
break;
}{...}
}{...}
}{...}
void bt_mesh_friend_remove_lpn(uint16_t lpn_addr)
{
struct bt_mesh_friend *frnd = NULL;
frnd = bt_mesh_friend_find(BLE_MESH_KEY_ANY, lpn_addr, false, false);
if (frnd) {
friend_clear(frnd, BLE_MESH_FRIENDSHIP_TERMINATE_DISABLE);
}{...}
}{...}
/* ... */
#endif