1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
24
25
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
118
119
120
121
122
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
156
157
163
164
165
166
171
172
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
229
230
231
232
233
234
240
241
244
245
248
249
254
255
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
497
498
499
502
503
504
505
506
507
508
509
510
511
512
513
514
515
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
549
550
551
554
555
556
557
558
559
560
561
562
563
564
565
567
568
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
595
596
603
604
605
606
607
608
609
610
611
612
613
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
640
645
646
647
648
649
650
651
652
655
656
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
686
687
688
689
690
691
694
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
732
733
734
735
736
739
740
744
745
746
747
748
749
750
751
752
753
754
755
761
762
768
769
770
771
772
773
774
775
776
780
781
784
785
786
787
788
789
790
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
819
820
821
826
827
828
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
858
859
860
861
862
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
889
890
891
892
893
894
895
896
897
898
902
903
904
905
908
911
912
913
914
915
916
954
955
956
961
962
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1039
1040
1041
1042
1043
1044
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1073
1074
1075
1076
1077
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1139
1140
1141
1151
1152
1153
1154
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1170
1171
1177
1178
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1195
1196
1197
1198
1201
1202
1203
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1249
1250
1254
1255
1256
1257
1258
1259
1260
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1286
1287
1295
1296
1299
1300
1301
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1326
1327
1330
1331
1332
1333
1334
1335
1336
1343
1344
1345
1346
1347
1348
1349
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1383
1384
1385
1388
1389
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1471
1472
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1497
1498
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1523
1524
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1551
1552
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1585
1586
1587
1588
1589
1590
1591
1592
1598
1599
1603
1604
1605
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1632
1633
1634
1635
1636
1637
1638
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1671
1672
1673
1674
1680
1681
1682
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1716
1717
1718
1719
1720
/* ... */
/* ... */
#include <string.h>
#include "common/bt_target.h"
#include "stack/l2cdefs.h"
#include "l2c_int.h"
#include "stack/btu.h"
#include "btm_int.h"
#include "stack/hcimsgs.h"
#include "device/controller.h"8 includes
#if (BLE_INCLUDED == TRUE)
#if (BLE_50_FEATURE_SUPPORT == TRUE)
#define EXT_CONN_INT_DEF_1M MAX(((MAX_ACL_CONNECTIONS + 1) * 4), 12)
#define EXT_CONN_INT_DEF_2M MAX(((MAX_ACL_CONNECTIONS + 1) * 2), 12)
#define EXT_CONN_INT_DEF_CODED (320)
const static tHCI_ExtConnParams ext_conn_params_1m_phy = {
.scan_interval = 0x40,
.scan_window = 0x40,
.conn_interval_min = EXT_CONN_INT_DEF_1M,
.conn_interval_max = EXT_CONN_INT_DEF_1M,
.conn_latency = 0,
.sup_timeout = 600,
.min_ce_len = 0,
.max_ce_len = 0,
}{...};
const static tHCI_ExtConnParams ext_conn_params_2m_phy = {
.scan_interval = 0x40,
.scan_window = 0x40,
.conn_interval_min = EXT_CONN_INT_DEF_2M,
.conn_interval_max = EXT_CONN_INT_DEF_2M,
.conn_latency = 0,
.sup_timeout = 600,
.min_ce_len = 0,
.max_ce_len = 0,
}{...};
const static tHCI_ExtConnParams ext_conn_params_coded_phy = {
.scan_interval = 0x40,
.scan_window = 0x40,
.conn_interval_min = EXT_CONN_INT_DEF_CODED,
.conn_interval_max = EXT_CONN_INT_DEF_CODED,
.conn_latency = 0,
.sup_timeout = 600,
.min_ce_len = 0,
.max_ce_len = 0,
}{...};
#define BLE_PHY_NO_PREF 0
#define BLE_PHY_PREF_MASK ((1 << 2) | (1 << 1) | (1 << 0))
/* ... */
#endif
static BOOLEAN l2cble_start_conn_update (tL2C_LCB *p_lcb);
extern int64_t esp_system_get_time(void);
/* ... */
BOOLEAN L2CA_CancelBleConnectReq (BD_ADDR rem_bda)
{
tL2C_LCB *p_lcb;
if (btm_ble_get_conn_st() == BLE_CONN_IDLE) {
L2CAP_TRACE_WARNING ("L2CA_CancelBleConnectReq - no connection pending");
return (FALSE);
}{...}
if (memcmp (rem_bda, l2cb.ble_connecting_bda, BD_ADDR_LEN)) {
L2CAP_TRACE_WARNING ("L2CA_CancelBleConnectReq - different BDA Connecting: %08x%04x Cancel: %08x%04x",
(l2cb.ble_connecting_bda[0] << 24) + (l2cb.ble_connecting_bda[1] << 16) + (l2cb.ble_connecting_bda[2] << 8) + l2cb.ble_connecting_bda[3],
(l2cb.ble_connecting_bda[4] << 8) + l2cb.ble_connecting_bda[5],
(rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3], (rem_bda[4] << 8) + rem_bda[5]);
return (FALSE);
}{...}
if (btsnd_hcic_ble_create_conn_cancel()) {
p_lcb = l2cu_find_lcb_by_bd_addr(rem_bda, BT_TRANSPORT_LE);
if (p_lcb != NULL &&
!(p_lcb->link_role == HCI_ROLE_SLAVE && BTM_LE_ACL_IS_CONNECTED(rem_bda))) {
p_lcb->disc_reason = L2CAP_CONN_CANCEL;
l2cu_release_lcb (p_lcb);
}{...}
btm_ble_set_conn_st (BLE_CONN_CANCEL);
return (TRUE);
}{...} else {
return (FALSE);
}{...}
}{ ... }
/* ... */
BOOLEAN L2CA_UpdateBleConnParams (BD_ADDR rem_bda, UINT16 min_int, UINT16 max_int,
UINT16 latency, UINT16 timeout)
{
tL2C_LCB *p_lcb;
tACL_CONN *p_acl_cb = btm_bda_to_acl(rem_bda, BT_TRANSPORT_LE);
UINT8 status = HCI_SUCCESS;
BOOLEAN need_cb = false;
p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, BT_TRANSPORT_LE);
if (!p_lcb || !p_acl_cb) {
L2CAP_TRACE_WARNING ("L2CA_UpdateBleConnParams - unknown BD_ADDR %08x%04x",
(rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3],
(rem_bda[4] << 8) + rem_bda[5]);
return (FALSE);
}{...}
if (p_lcb->transport != BT_TRANSPORT_LE) {
L2CAP_TRACE_WARNING ("L2CA_UpdateBleConnParams - BD_ADDR %08x%04x not LE",
(rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3],
(rem_bda[4] << 8) + rem_bda[5]);
return (FALSE);
}{...}
if ((max_int == p_lcb->current_used_conn_interval) && (latency == p_lcb->current_used_conn_latency) &&
(timeout == p_lcb->current_used_conn_timeout)) {
status = HCI_SUCCESS;
need_cb = true;
L2CAP_TRACE_WARNING("%s connection parameter already set", __func__);
}{...}
if (p_lcb->conn_update_mask & L2C_BLE_UPDATE_PARAM_FULL){
status = HCI_ERR_ILLEGAL_COMMAND;
need_cb = true;
L2CAP_TRACE_ERROR("There are two connection parameter requests that are being updated, please try later ");
}{...}
if ((need_cb == TRUE) && (conn_callback_func.update_conn_param_cb != NULL)) {
tBTM_LE_UPDATE_CONN_PRAMS update_param;
update_param.max_conn_int = max_int;
update_param.min_conn_int = min_int;
update_param.conn_int = p_lcb->current_used_conn_interval;
update_param.slave_latency = p_lcb->current_used_conn_latency;
update_param.supervision_tout = p_lcb->current_used_conn_timeout;
(conn_callback_func.update_conn_param_cb)(status, p_lcb->remote_bd_addr, &update_param);
return (status == HCI_SUCCESS);
}{...}
p_lcb->waiting_update_conn_min_interval = min_int;
p_lcb->waiting_update_conn_max_interval = max_int;
p_lcb->waiting_update_conn_latency = latency;
p_lcb->waiting_update_conn_timeout = timeout;
p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
if(l2cble_start_conn_update(p_lcb) == TRUE) {
UINT32 time = CalConnectParamTimeout(p_lcb);
btu_start_timer(&p_lcb->upda_con_timer, BTU_TTYPE_L2CAP_UPDA_CONN_PARAMS, time);
}{...}
return (TRUE);
}{ ... }
/* ... */
BOOLEAN L2CA_EnableUpdateBleConnParams (BD_ADDR rem_bda, BOOLEAN enable)
{
tL2C_LCB *p_lcb;
p_lcb = l2cu_find_lcb_by_bd_addr (rem_bda, BT_TRANSPORT_LE);
if (!p_lcb) {
L2CAP_TRACE_WARNING ("L2CA_EnableUpdateBleConnParams - unknown BD_ADDR %08x%04x",
(rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3],
(rem_bda[4] << 8) + rem_bda[5]);
return (FALSE);
}{...}
L2CAP_TRACE_API ("%s - BD_ADDR %08x%04x enable %d current upd state 0x%02x", __FUNCTION__,
(rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3],
(rem_bda[4] << 8) + rem_bda[5], enable, p_lcb->conn_update_mask);
if (p_lcb->transport != BT_TRANSPORT_LE) {
L2CAP_TRACE_WARNING ("%s - BD_ADDR %08x%04x not LE (link role %d)", __FUNCTION__,
(rem_bda[0] << 24) + (rem_bda[1] << 16) + (rem_bda[2] << 8) + rem_bda[3],
(rem_bda[4] << 8) + rem_bda[5], p_lcb->link_role);
return (FALSE);
}{...}
if (p_lcb->current_used_conn_interval <= BTM_BLE_CONN_INT_MAX_DEF && (p_lcb->conn_update_mask & L2C_BLE_CONN_UPDATE_DISABLE) == 0){
return (FALSE);
}{...}
bool is_disable = (p_lcb->conn_update_mask & L2C_BLE_CONN_UPDATE_DISABLE);
if(l2cu_plcb_active_count() >1 && !(enable && is_disable)) {
return FALSE;
}{...}
if (enable) {
p_lcb->conn_update_mask &= ~L2C_BLE_CONN_UPDATE_DISABLE;
}{...} else {
p_lcb->conn_update_mask |= L2C_BLE_CONN_UPDATE_DISABLE;
}{...}
if (l2cble_start_conn_update(p_lcb) == TRUE) {
UINT32 time = CalConnectParamTimeout(p_lcb);
btu_start_timer(&p_lcb->upda_con_timer, BTU_TTYPE_L2CAP_UPDA_CONN_PARAMS, time);
}{...}
return (TRUE);
}{ ... }
/* ... */
UINT8 L2CA_GetBleConnRole (BD_ADDR bd_addr)
{
UINT8 role = HCI_ROLE_UNKNOWN;
tL2C_LCB *p_lcb;
if ((p_lcb = l2cu_find_lcb_by_bd_addr (bd_addr, BT_TRANSPORT_LE)) != NULL) {
role = p_lcb->link_role;
}{...}
return role;
}{ ... }
/* ... */
void l2cble_notify_le_connection (BD_ADDR bda)
{
tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
tACL_CONN *p_acl = btm_bda_to_acl(bda, BT_TRANSPORT_LE) ;
if (p_lcb != NULL && p_acl != NULL && p_lcb->link_state != LST_CONNECTED) {
if(p_acl->link_role == HCI_ROLE_SLAVE) {
tBTM_BLE_INQ_CB *p_cb = &btm_cb.ble_ctr_cb.inq_var;
if(p_cb) {
p_cb->adv_mode = BTM_BLE_ADV_DISABLE;
p_cb->state &= ~BTM_BLE_ADVERTISING;
}{...}
}{...}
btm_establish_continue(p_acl);
p_lcb->link_state = LST_CONNECTED;
l2cu_process_fixed_chnl_resp (p_lcb);
}{...}
}{ ... }
/* ... */
void l2cble_scanner_conn_comp (UINT16 handle, BD_ADDR bda, tBLE_ADDR_TYPE type,
UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
{
tL2C_LCB *p_lcb;
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev (bda);
L2CAP_TRACE_DEBUG ("l2cble_scanner_conn_comp: HANDLE=%d addr_type=%d conn_interval=%d slave_latency=%d supervision_tout=%d",
handle, type, conn_interval, conn_latency, conn_timeout);
l2cb.is_ble_connecting = FALSE;
p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
if (!p_lcb) {
p_lcb = l2cu_allocate_lcb (bda, FALSE, BT_TRANSPORT_LE);
if (!p_lcb) {
#if (SMP_INCLUDED == TRUE)
btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
L2CAP_TRACE_ERROR ("l2cble_scanner_conn_comp - failed to allocate LCB");/* ... */
#endif
return;
}{...} else {
if (!l2cu_initialize_fixed_ccb (p_lcb, L2CAP_ATT_CID, &l2cb.fixed_reg[L2CAP_ATT_CID - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts)) {
#if (SMP_INCLUDED == TRUE)
btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
L2CAP_TRACE_WARNING ("l2cble_scanner_conn_comp - LCB but no CCB");/* ... */
#endif
return ;
}{...}
}{...}
}{...} else if (p_lcb->link_state != LST_CONNECTING) {
L2CAP_TRACE_ERROR ("L2CAP got BLE scanner conn_comp in bad state: %d", p_lcb->link_state);
return;
}{...}
btu_stop_timer(&p_lcb->timer_entry);
p_lcb->handle = handle;
p_lcb->link_role = HCI_ROLE_MASTER;
p_lcb->transport = BT_TRANSPORT_LE;
p_lcb->waiting_update_conn_min_interval = p_lcb->waiting_update_conn_max_interval = p_lcb->current_used_conn_interval = conn_interval;
p_lcb->waiting_update_conn_timeout = p_lcb->current_used_conn_timeout = conn_timeout;
p_lcb->waiting_update_conn_latency = p_lcb->current_used_conn_latency = conn_latency;
p_lcb->conn_update_mask = L2C_BLE_NOT_DEFAULT_PARAM;
p_lcb->updating_param_flag = false;
p_lcb->ble_addr_type = type;
if ( (p_dev_rec->conn_params.min_conn_int >= BTM_BLE_CONN_INT_MIN ) &&
(p_dev_rec->conn_params.min_conn_int <= BTM_BLE_CONN_INT_MAX ) &&
(p_dev_rec->conn_params.max_conn_int >= BTM_BLE_CONN_INT_MIN ) &&
(p_dev_rec->conn_params.max_conn_int <= BTM_BLE_CONN_INT_MAX ) &&
(p_dev_rec->conn_params.slave_latency <= BTM_BLE_CONN_LATENCY_MAX ) &&
(p_dev_rec->conn_params.supervision_tout >= BTM_BLE_CONN_SUP_TOUT_MIN) &&
(p_dev_rec->conn_params.supervision_tout <= BTM_BLE_CONN_SUP_TOUT_MAX) &&
((conn_interval < p_dev_rec->conn_params.min_conn_int &&
p_dev_rec->conn_params.min_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ||
(conn_interval > p_dev_rec->conn_params.max_conn_int) ||
(conn_latency > p_dev_rec->conn_params.slave_latency) ||
(conn_timeout > p_dev_rec->conn_params.supervision_tout))) {
L2CAP_TRACE_ERROR ("upd_ll_conn_params: HANDLE=%d min_conn_int=%d max_conn_int=%d slave_latency=%d supervision_tout=%d",
handle, p_dev_rec->conn_params.min_conn_int, p_dev_rec->conn_params.max_conn_int,
p_dev_rec->conn_params.slave_latency, p_dev_rec->conn_params.supervision_tout);
p_lcb->waiting_update_conn_min_interval = p_dev_rec->conn_params.min_conn_int;
p_lcb->waiting_update_conn_max_interval = p_dev_rec->conn_params.max_conn_int;
p_lcb->waiting_update_conn_timeout = p_dev_rec->conn_params.supervision_tout;
p_lcb->waiting_update_conn_latency = p_dev_rec->conn_params.slave_latency;
btsnd_hcic_ble_upd_ll_conn_params (handle,
p_dev_rec->conn_params.min_conn_int,
p_dev_rec->conn_params.max_conn_int,
p_dev_rec->conn_params.slave_latency,
p_dev_rec->conn_params.supervision_tout,
BLE_CE_LEN_MIN, BLE_CE_LEN_MIN);
}{...}
btm_acl_created (bda, NULL, p_dev_rec->sec_bd_name, handle, p_lcb->link_role, BT_TRANSPORT_LE);
p_lcb->peer_chnl_mask[0] = L2CAP_FIXED_CHNL_ATT_BIT | L2CAP_FIXED_CHNL_BLE_SIG_BIT | L2CAP_FIXED_CHNL_SMP_BIT;
btm_ble_set_conn_st(BLE_CONN_IDLE);
#if BLE_PRIVACY_SPT == TRUE
btm_ble_disable_resolving_list(BTM_BLE_RL_INIT, TRUE);
#endif
}{ ... }
/* ... */
void l2cble_advertiser_conn_comp (UINT16 handle, BD_ADDR bda, tBLE_ADDR_TYPE type,
UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
{
tL2C_LCB *p_lcb;
tBTM_SEC_DEV_REC *p_dev_rec;
UNUSED(type);
UNUSED(conn_interval);
UNUSED(conn_latency);
UNUSED(conn_timeout);
p_lcb = l2cu_find_lcb_by_bd_addr (bda, BT_TRANSPORT_LE);
if (!p_lcb) {
p_lcb = l2cu_allocate_lcb (bda, FALSE, BT_TRANSPORT_LE);
if (!p_lcb) {
#if (SMP_INCLUDED == TRUE)
btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
#endif
L2CAP_TRACE_ERROR ("l2cble_advertiser_conn_comp - failed to allocate LCB");
return;
}{...} else {
if (!l2cu_initialize_fixed_ccb (p_lcb, L2CAP_ATT_CID, &l2cb.fixed_reg[L2CAP_ATT_CID - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts)) {
#if (SMP_INCLUDED == TRUE)
btm_sec_disconnect (handle, HCI_ERR_NO_CONNECTION);
#endif
L2CAP_TRACE_WARNING ("l2cble_scanner_conn_comp - LCB but no CCB");
return ;
}{...}
}{...}
}{...}
p_lcb->handle = handle;
p_lcb->link_role = HCI_ROLE_SLAVE;
p_lcb->transport = BT_TRANSPORT_LE;
p_lcb->waiting_update_conn_min_interval = p_lcb->waiting_update_conn_max_interval = p_lcb->current_used_conn_interval = conn_interval;
p_lcb->waiting_update_conn_timeout = p_lcb->current_used_conn_timeout = conn_timeout;
p_lcb->waiting_update_conn_latency = p_lcb->current_used_conn_latency = conn_latency;
p_lcb->conn_update_mask = L2C_BLE_NOT_DEFAULT_PARAM;
p_lcb->updating_param_flag = false;
p_lcb->ble_addr_type = type;
p_dev_rec = btm_find_or_alloc_dev (bda);
btm_acl_created (bda, NULL, p_dev_rec->sec_bd_name, handle, p_lcb->link_role, BT_TRANSPORT_LE);
#if BLE_PRIVACY_SPT == TRUE
btm_ble_disable_resolving_list(BTM_BLE_RL_ADV, TRUE);
#endif
p_lcb->peer_chnl_mask[0] = L2CAP_FIXED_CHNL_ATT_BIT | L2CAP_FIXED_CHNL_BLE_SIG_BIT | L2CAP_FIXED_CHNL_SMP_BIT;
if (!HCI_LE_SLAVE_INIT_FEAT_EXC_SUPPORTED(controller_get_interface()->get_features_ble()->as_array)) {
p_lcb->link_state = LST_CONNECTED;
l2cu_process_fixed_chnl_resp (p_lcb);
}{...}
if (l2cb.is_ble_connecting && memcmp(bda, l2cb.ble_connecting_bda, BD_ADDR_LEN) == 0) {
L2CA_CancelBleConnectReq(bda);
}{...}
}{ ... }
/* ... */
void l2cble_conn_comp(UINT16 handle, UINT8 role, BD_ADDR bda, tBLE_ADDR_TYPE type,
UINT16 conn_interval, UINT16 conn_latency, UINT16 conn_timeout)
{
btm_ble_update_link_topology_mask(role, TRUE);
if (role == HCI_ROLE_MASTER) {
l2cble_scanner_conn_comp(handle, bda, type, conn_interval, conn_latency, conn_timeout);
}{...} else {
l2cble_advertiser_conn_comp(handle, bda, type, conn_interval, conn_latency, conn_timeout);
}{...}
}{ ... }
/* ... */
static BOOLEAN l2cble_start_conn_update (tL2C_LCB *p_lcb)
{
UINT16 min_conn_int, max_conn_int, slave_latency, supervision_tout;
#if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE) && (BLE_SLAVE_UPD_CONN_PARAMS == TRUE)
tACL_CONN *p_acl_cb = btm_bda_to_acl(p_lcb->remote_bd_addr, BT_TRANSPORT_LE);
#endif
if (p_lcb->conn_update_mask & L2C_BLE_UPDATE_PENDING) {
L2CAP_TRACE_WARNING("%s, the last connection update command still pending.", __func__);
p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PARAM_FULL;
return FALSE;
}{...}
if (p_lcb->conn_update_mask & L2C_BLE_CONN_UPDATE_DISABLE) {
/* ... */
if (p_lcb->conn_update_mask & L2C_BLE_NOT_DEFAULT_PARAM &&
p_lcb->current_used_conn_interval > BTM_BLE_CONN_INT_MAX_DEF) {
min_conn_int = max_conn_int = BTM_BLE_CONN_INT_MIN;
slave_latency = BTM_BLE_CONN_SLAVE_LATENCY_DEF;
supervision_tout = BTM_BLE_CONN_TIMEOUT_DEF;
if (p_lcb->link_role == HCI_ROLE_MASTER
#if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE) && (BLE_SLAVE_UPD_CONN_PARAMS == TRUE)
|| (HCI_LE_CONN_PARAM_REQ_SUPPORTED(controller_get_interface()->get_features_ble()->as_array) &&
HCI_LE_CONN_PARAM_REQ_SUPPORTED(p_acl_cb->peer_le_features))/* ... */
#endif
) {
btsnd_hcic_ble_upd_ll_conn_params(p_lcb->handle, min_conn_int, max_conn_int,
slave_latency, supervision_tout, BLE_CE_LEN_MIN, BLE_CE_LEN_MIN);
}{...} else {
l2cu_send_peer_ble_par_req (p_lcb, min_conn_int, max_conn_int, slave_latency, supervision_tout);
}{...}
p_lcb->updating_conn_min_interval = min_conn_int;
p_lcb->updating_conn_max_interval = max_conn_int;
p_lcb->updating_param_flag = true;
p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PENDING;
p_lcb->conn_update_mask &= ~L2C_BLE_NOT_DEFAULT_PARAM;
p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
return TRUE;
}{...}else {
return FALSE;
}
}{...} else {
if (p_lcb->conn_update_mask & L2C_BLE_NEW_CONN_PARAM) {
if (p_lcb->link_role == HCI_ROLE_MASTER
#if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE) && (BLE_SLAVE_UPD_CONN_PARAMS == TRUE)
|| (HCI_LE_CONN_PARAM_REQ_SUPPORTED(controller_get_interface()->get_features_ble()->as_array) &&
HCI_LE_CONN_PARAM_REQ_SUPPORTED(p_acl_cb->peer_le_features))/* ... */
#endif
) {
btsnd_hcic_ble_upd_ll_conn_params(p_lcb->handle, p_lcb->waiting_update_conn_min_interval,
p_lcb->waiting_update_conn_max_interval, p_lcb->waiting_update_conn_latency, p_lcb->waiting_update_conn_timeout, BLE_CE_LEN_MIN, BLE_CE_LEN_MIN);
}{...} else {
l2cu_send_peer_ble_par_req (p_lcb, p_lcb->waiting_update_conn_min_interval, p_lcb->waiting_update_conn_max_interval,
p_lcb->waiting_update_conn_latency, p_lcb->waiting_update_conn_timeout);
}{...}
p_lcb->updating_conn_min_interval = p_lcb->waiting_update_conn_min_interval;
p_lcb->updating_conn_max_interval = p_lcb->waiting_update_conn_max_interval;
p_lcb->updating_param_flag = true;
p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PENDING;
p_lcb->conn_update_mask &= ~L2C_BLE_NEW_CONN_PARAM;
p_lcb->conn_update_mask |= L2C_BLE_NOT_DEFAULT_PARAM;
return TRUE;
}{...} else {
return FALSE;
}{...}
}{...}
}{ ... }
/* ... */
void l2cble_process_conn_update_evt (UINT16 handle, UINT8 status, UINT16 conn_interval,
UINT16 conn_latency, UINT16 conn_timeout)
{
tL2C_LCB *p_lcb;
p_lcb = l2cu_find_lcb_by_handle(handle);
if (!p_lcb) {
L2CAP_TRACE_WARNING("le con upd: inv hdl=%d", handle);
return;
}{...}
if (status == HCI_SUCCESS){
p_lcb->current_used_conn_interval = conn_interval;
p_lcb->current_used_conn_latency = conn_latency;
p_lcb->current_used_conn_timeout = conn_timeout;
}{...}else{
L2CAP_TRACE_WARNING("le con upd: err_stat=0x%x", status);
}
p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PENDING;
p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PARAM_FULL;
btu_stop_timer(&p_lcb->upda_con_timer);
if (conn_callback_func.update_conn_param_cb != NULL) {
l2c_send_update_conn_params_cb(p_lcb, status);
}{...}
if (l2cble_start_conn_update(p_lcb) == TRUE) {
UINT32 time = CalConnectParamTimeout(p_lcb);
btu_start_timer(&p_lcb->upda_con_timer, BTU_TTYPE_L2CAP_UPDA_CONN_PARAMS, time);
}{...}
btu_stop_timer (&p_lcb->timer_entry);
L2CAP_TRACE_DEBUG("le con upd: conn_update_mask=%d", p_lcb->conn_update_mask);
}{ ... }
/* ... */
void l2cble_get_conn_param_format_err_from_contoller (UINT8 status, UINT16 handle)
{
tL2C_LCB *p_lcb;
p_lcb = l2cu_find_lcb_by_handle(handle);
if (!p_lcb) {
L2CAP_TRACE_ERROR("%s: Invalid handle: %d", __FUNCTION__, handle);
return;
}{...}
p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PENDING;
btu_stop_timer (&p_lcb->upda_con_timer);
if (conn_callback_func.update_conn_param_cb != NULL) {
l2c_send_update_conn_params_cb(p_lcb, status);
}{...}
if ((p_lcb->conn_update_mask & L2C_BLE_UPDATE_PARAM_FULL) != 0){
p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PARAM_FULL;
if (l2cble_start_conn_update(p_lcb) == TRUE) {
UINT32 time = CalConnectParamTimeout(p_lcb);
btu_start_timer(&p_lcb->upda_con_timer, BTU_TTYPE_L2CAP_UPDA_CONN_PARAMS, time);
}{...}
}{...}
}{ ... }
/* ... */
void l2cble_process_sig_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len)
{
UINT8 *p_pkt_end;
UINT8 cmd_code, id;
UINT16 cmd_len;
UINT16 min_interval, max_interval, latency, timeout;
p_pkt_end = p + pkt_len;
STREAM_TO_UINT8 (cmd_code, p);
STREAM_TO_UINT8 (id, p);
STREAM_TO_UINT16 (cmd_len, p);
if ((p + cmd_len) > p_pkt_end) {
L2CAP_TRACE_WARNING ("L2CAP - LE - format error, pkt_len: %d cmd_len: %d code: %d", pkt_len, cmd_len, cmd_code);
return;
}{...}
switch (cmd_code) {
case L2CAP_CMD_REJECT:
case L2CAP_CMD_ECHO_RSP:
case L2CAP_CMD_INFO_RSP:
p += 2;
break;...
case L2CAP_CMD_ECHO_REQ:
case L2CAP_CMD_INFO_REQ:
l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
break;
...
case L2CAP_CMD_BLE_UPDATE_REQ:
STREAM_TO_UINT16 (min_interval, p);
STREAM_TO_UINT16 (max_interval, p);
STREAM_TO_UINT16 (latency, p);
STREAM_TO_UINT16 (timeout, p);
if (p_lcb->link_role == HCI_ROLE_MASTER) {
if (min_interval < BTM_BLE_CONN_INT_MIN || min_interval > BTM_BLE_CONN_INT_MAX ||
max_interval < BTM_BLE_CONN_INT_MIN || max_interval > BTM_BLE_CONN_INT_MAX ||
latency > BTM_BLE_CONN_LATENCY_MAX ||
timeout < BTM_BLE_CONN_SUP_TOUT_MIN || timeout > BTM_BLE_CONN_SUP_TOUT_MAX ||
/* ... */
((timeout * 10) < ((1 + latency) *((max_interval * 5) >> 1))) ||
max_interval < min_interval) {
l2cu_send_peer_ble_par_rsp (p_lcb, L2CAP_CFG_UNACCEPTABLE_PARAMS, id);
L2CAP_TRACE_ERROR("slave connection parameters update failed, the parameters are out of range");
}{...} else {
l2cu_send_peer_ble_par_rsp (p_lcb, L2CAP_CFG_OK, id);
p_lcb->waiting_update_conn_min_interval = min_interval;
p_lcb->waiting_update_conn_max_interval = max_interval;
p_lcb->waiting_update_conn_latency = latency;
p_lcb->waiting_update_conn_timeout = timeout;
p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
if (l2cble_start_conn_update(p_lcb) == TRUE) {
UINT32 time = CalConnectParamTimeout(p_lcb);
btu_start_timer(&p_lcb->upda_con_timer, BTU_TTYPE_L2CAP_UPDA_CONN_PARAMS, time);
}{...}
}{...}
}{...} else {
l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
}{...}
break;
...
case L2CAP_CMD_BLE_UPDATE_RSP: {
UINT16 result = 0;
STREAM_TO_UINT16(result, p);
UINT8 status = (result == 0) ? HCI_SUCCESS : HCI_ERR_PARAM_OUT_OF_RANGE;
if (status != HCI_SUCCESS) {
btu_stop_timer(&p_lcb->upda_con_timer);
p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PENDING;
p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PARAM_FULL;
l2c_send_update_conn_params_cb(p_lcb, status);
}{...}
break;
}{...}
... case L2CAP_CMD_BLE_CREDIT_BASED_CONN_REQ: {
tL2C_CCB *p_ccb = NULL;
tL2C_RCB *p_rcb = NULL;
UINT16 spsm;
UINT16 scid;
UINT16 mtu;
UINT16 mps;
UINT16 credits;
STREAM_TO_UINT16(spsm, p);
STREAM_TO_UINT16(scid, p);
STREAM_TO_UINT16(mtu, p);
STREAM_TO_UINT16(mps, p);
STREAM_TO_UINT16(credits, p);
L2CAP_TRACE_DEBUG("%s spsm %x, scid %x", __func__, spsm, scid);
UNUSED(spsm);
p_ccb = l2cu_find_ccb_by_remote_cid(p_lcb, scid);
if (p_ccb) {
l2cu_reject_ble_connection(p_lcb, id, L2CAP_LE_RESULT_SOURCE_CID_ALREADY_ALLOCATED);
break;
}{...}
#if 0
p_rcb = l2cu_find_ble_rcb_by_psm(spsm);
if (p_rcb == NULL) {
break;
}{...}
/* ... */#endif
p_ccb = l2cu_allocate_ccb(p_lcb, 0);
if (p_ccb == NULL) {
l2cu_reject_ble_connection(p_lcb, id, L2CAP_LE_RESULT_NO_RESOURCES);
break;
}{...}
p_ccb->remote_id = id;
p_ccb->p_rcb = p_rcb;
p_ccb->remote_cid = scid;
p_ccb->local_conn_cfg.mtu = mtu;
p_ccb->local_conn_cfg.mps = controller_get_interface()->get_acl_data_size_ble();
p_ccb->local_conn_cfg.credits = credits;
p_ccb->peer_conn_cfg.mtu = mtu;
p_ccb->peer_conn_cfg.mps = mps;
p_ccb->peer_conn_cfg.credits = credits;
l2cu_send_peer_ble_credit_based_conn_res(p_ccb, L2CAP_LE_RESULT_CONN_OK);
break;
}{...}
... case L2CAP_CMD_DISC_REQ: {
tL2C_CCB *p_ccb = NULL;
UINT16 lcid;
UINT16 rcid;
STREAM_TO_UINT16(lcid, p);
STREAM_TO_UINT16(rcid, p);
p_ccb = l2cu_find_ccb_by_cid(p_lcb, lcid);
if (p_ccb) {
p_ccb->remote_id = id;
}{...}
l2cu_send_peer_disc_rsp(p_lcb, id, lcid, rcid);
break;
}{...}
... default:
L2CAP_TRACE_WARNING ("L2CAP - LE - unknown cmd code: %d", cmd_code);
l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
return;...
}{...}
}{ ... }
/* ... */
BOOLEAN l2cble_init_direct_conn (tL2C_LCB *p_lcb)
{
#if ( (defined BLE_PRIVACY_SPT) && (BLE_PRIVACY_SPT == TRUE) && (!CONTROLLER_RPA_LIST_ENABLE))
bool dev_rec_exist = true;
tBTM_SEC_DEV_REC *find_dev_rec = btm_find_dev (p_lcb->remote_bd_addr);
if(find_dev_rec == NULL) {
dev_rec_exist = false;
}{...}
/* ... */#endif
tBTM_SEC_DEV_REC *p_dev_rec = btm_find_or_alloc_dev (p_lcb->remote_bd_addr);
tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
UINT16 scan_int;
UINT16 scan_win;
BD_ADDR peer_addr;
UINT8 peer_addr_type = BLE_ADDR_PUBLIC;
UINT8 own_addr_type = BLE_ADDR_PUBLIC;
if (p_dev_rec == NULL) {
L2CAP_TRACE_WARNING ("unknown device, can not initiate connection");
return (FALSE);
}{...}
scan_int = (p_cb->scan_int == BTM_BLE_SCAN_PARAM_UNDEF) ? BTM_BLE_SCAN_FAST_INT : p_cb->scan_int;
scan_win = (p_cb->scan_win == BTM_BLE_SCAN_PARAM_UNDEF) ? BTM_BLE_SCAN_FAST_WIN : p_cb->scan_win;
if (p_dev_rec->conn_params.scan_interval && p_dev_rec->conn_params.scan_interval != BTM_BLE_CONN_PARAM_UNDEF) {
scan_int = p_dev_rec->conn_params.scan_interval;
}{...}
if (p_dev_rec->conn_params.scan_window && p_dev_rec->conn_params.scan_window != BTM_BLE_CONN_PARAM_UNDEF) {
scan_win = p_dev_rec->conn_params.scan_window;
}{...}
peer_addr_type = p_lcb->ble_addr_type;
memcpy(peer_addr, p_lcb->remote_bd_addr, BD_ADDR_LEN);
#if ( (defined BLE_PRIVACY_SPT) && (BLE_PRIVACY_SPT == TRUE))
own_addr_type = btm_cb.ble_ctr_cb.addr_mgnt_cb.own_addr_type;
#if (!CONTROLLER_RPA_LIST_ENABLE)
if(dev_rec_exist) {
if(p_dev_rec->ble.current_addr_valid) {
peer_addr_type = p_dev_rec->ble.current_addr_type;
memcpy(peer_addr, p_dev_rec->ble.current_addr, 6);
}{...} else {
/* ... */
}{...}
}{...} else {
}{...}
/* ... */
/* ... */
#endif
#if (CONTROLLER_RPA_LIST_ENABLE)
if (p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) {
if (btm_cb.ble_ctr_cb.privacy_mode >= BTM_PRIVACY_1_2) {
own_addr_type |= BLE_ADDR_TYPE_ID_BIT;
}{...}
btm_random_pseudo_to_identity_addr(peer_addr, &peer_addr_type);
}{...} else {
btm_ble_disable_resolving_list(BTM_BLE_RL_INIT, TRUE);
}{...}
/* ... */#endif /* ... */
#endif
if (!btm_ble_topology_check(BTM_BLE_STATE_INIT)) {
l2cu_release_lcb (p_lcb);
L2CAP_TRACE_ERROR("initiate direct connection fail, topology limitation");
return FALSE;
}{...}
uint32_t link_timeout = L2CAP_BLE_LINK_CONNECT_TOUT;
if(GATTC_CONNECT_RETRY_COUNT) {
if(!p_lcb->retry_create_con) {
p_lcb->start_time_s = (esp_system_get_time()/1000);
}{...}
uint32_t current_time = (esp_system_get_time()/1000);
link_timeout = (L2CAP_BLE_LINK_CONNECT_TOUT*1000 - (current_time - p_lcb->start_time_s))/1000;
if(link_timeout == 0 || link_timeout > L2CAP_BLE_LINK_CONNECT_TOUT) {
link_timeout = L2CAP_BLE_LINK_CONNECT_TOUT;
}{...}
}{...}
if (!p_lcb->is_aux) {
if (!btsnd_hcic_ble_create_ll_conn (scan_int,
scan_win,
FALSE,
peer_addr_type,
peer_addr,
own_addr_type,
(UINT16) ((p_dev_rec->conn_params.min_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ?
p_dev_rec->conn_params.min_conn_int : BTM_BLE_CONN_INT_MIN_DEF),
(UINT16) ((p_dev_rec->conn_params.max_conn_int != BTM_BLE_CONN_PARAM_UNDEF) ?
p_dev_rec->conn_params.max_conn_int : BTM_BLE_CONN_INT_MAX_DEF),
(UINT16) ((p_dev_rec->conn_params.slave_latency != BTM_BLE_CONN_PARAM_UNDEF) ?
p_dev_rec->conn_params.slave_latency : BTM_BLE_CONN_SLAVE_LATENCY_DEF),
(UINT16) ((p_dev_rec->conn_params.supervision_tout != BTM_BLE_CONN_PARAM_UNDEF) ?
p_dev_rec->conn_params.supervision_tout : BTM_BLE_CONN_TIMEOUT_DEF),
(UINT16) ((p_dev_rec->conn_params.min_ce_len != BTM_BLE_CONN_PARAM_UNDEF) ?
p_dev_rec->conn_params.min_ce_len : BLE_CE_LEN_MIN),
(UINT16) ((p_dev_rec->conn_params.max_ce_len != BTM_BLE_CONN_PARAM_UNDEF) ?
p_dev_rec->conn_params.max_ce_len : BLE_CE_LEN_MIN) )) {
l2cu_release_lcb (p_lcb);
L2CAP_TRACE_ERROR("initiate direct connection fail, no resources");
return (FALSE);
}{...} else {
p_lcb->link_state = LST_CONNECTING;
l2cb.is_ble_connecting = TRUE;
memcpy (l2cb.ble_connecting_bda, p_lcb->remote_bd_addr, BD_ADDR_LEN);
btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, link_timeout);
btm_ble_set_conn_st (BLE_DIR_CONN);
return (TRUE);
}{...}
}{...} else {
#if (BLE_50_FEATURE_SUPPORT == TRUE)
/* ... */
if ((peer_addr_type & BLE_ADDR_RANDOM) == BLE_ADDR_RANDOM) {
peer_addr_type = BLE_ADDR_RANDOM;
}{...} else {
peer_addr_type = BLE_ADDR_PUBLIC;
}{...}
tHCI_CreatExtConn aux_conn = {0};
aux_conn.filter_policy = FALSE;
aux_conn.own_addr_type = own_addr_type;
aux_conn.peer_addr_type = peer_addr_type;
memcpy(aux_conn.peer_addr, peer_addr, sizeof(BD_ADDR));
if (p_dev_rec->ext_conn_params.phy_mask == BLE_PHY_NO_PREF) {
L2CAP_TRACE_WARNING("No extend connection parameters set, use default parameters");
aux_conn.init_phy_mask = BLE_PHY_PREF_MASK;
memcpy(&aux_conn.params[0], &ext_conn_params_1m_phy, sizeof(tHCI_ExtConnParams));
memcpy(&aux_conn.params[1], &ext_conn_params_2m_phy, sizeof(tHCI_ExtConnParams));
memcpy(&aux_conn.params[2], &ext_conn_params_coded_phy, sizeof(tHCI_ExtConnParams));
}{...} else {
aux_conn.init_phy_mask = p_dev_rec->ext_conn_params.phy_mask;
memcpy(&aux_conn.params[0], &p_dev_rec->ext_conn_params.phy_1m_conn_params, sizeof(tHCI_ExtConnParams));
memcpy(&aux_conn.params[1], &p_dev_rec->ext_conn_params.phy_2m_conn_params, sizeof(tHCI_ExtConnParams));
memcpy(&aux_conn.params[2], &p_dev_rec->ext_conn_params.phy_coded_conn_params, sizeof(tHCI_ExtConnParams));
}{...}
p_lcb->link_state = LST_CONNECTING;
l2cb.is_ble_connecting = TRUE;
memcpy (l2cb.ble_connecting_bda, p_lcb->remote_bd_addr, BD_ADDR_LEN);
btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, link_timeout);
btm_ble_set_conn_st (BLE_DIR_CONN);
if(!btsnd_hcic_ble_create_ext_conn(&aux_conn)) {
l2cu_release_lcb (p_lcb);
L2CAP_TRACE_ERROR("initiate Aux connection failed, no resources");
}{...}
#else/* ... */
L2CAP_TRACE_ERROR("BLE 5.0 not support!\n");
#endif
return (TRUE);
}{...}
}{ ... }
/* ... */
BOOLEAN l2cble_create_conn (tL2C_LCB *p_lcb)
{
tBTM_BLE_CONN_ST conn_st = btm_ble_get_conn_st();
BOOLEAN rt = FALSE;
if (conn_st == BLE_CONN_IDLE) {
rt = l2cble_init_direct_conn(p_lcb);
}{...} else {
L2CAP_TRACE_WARNING ("L2CAP - LE - cannot start new connection at conn st: %d", conn_st);
btm_ble_enqueue_direct_conn_req(p_lcb);
if (conn_st == BLE_BG_CONN) {
btm_ble_suspend_bg_conn();
}{...}
rt = TRUE;
}{...}
return rt;
}{ ... }
/* ... */
void l2c_link_processs_ble_num_bufs (UINT16 num_lm_ble_bufs)
{
if (num_lm_ble_bufs == 0) {
num_lm_ble_bufs = L2C_DEF_NUM_BLE_BUF_SHARED;
l2cb.num_lm_acl_bufs -= L2C_DEF_NUM_BLE_BUF_SHARED;
}{...}
L2CAP_TRACE_DEBUG("num_lm_ble_bufs = %d",num_lm_ble_bufs);
l2cb.num_lm_ble_bufs = l2cb.controller_le_xmit_window = num_lm_ble_bufs;
}{ ... }
/* ... */
void l2c_ble_link_adjust_allocation (void)
{
UINT16 qq, qq_remainder;
tL2C_LCB *p_lcb;
UINT16 hi_quota, low_quota;
UINT16 num_lowpri_links = 0;
UINT16 num_hipri_links = 0;
UINT16 controller_xmit_quota = l2cb.num_lm_ble_bufs;
UINT16 high_pri_link_quota = L2CAP_HIGH_PRI_MIN_XMIT_QUOTA_A;
list_node_t *p_node = NULL;
if (l2cb.num_ble_links_active == 0) {
l2cb.controller_le_xmit_window = l2cb.num_lm_ble_bufs;
l2cb.ble_round_robin_quota = l2cb.ble_round_robin_unacked = 0;
return;
}{...}
for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
p_lcb = list_node(p_node);
if (p_lcb->in_use && p_lcb->transport == BT_TRANSPORT_LE) {
if (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH) {
num_hipri_links++;
}{...} else {
num_lowpri_links++;
}{...}
}{...}
}{...}
low_quota = num_lowpri_links ? 1 : 0;
while ( (num_hipri_links * high_pri_link_quota + low_quota) > controller_xmit_quota ) {
high_pri_link_quota--;
}{...}
hi_quota = num_hipri_links * high_pri_link_quota;
low_quota = (hi_quota < controller_xmit_quota) ? controller_xmit_quota - hi_quota : 1;
if (num_lowpri_links > low_quota) {
l2cb.ble_round_robin_quota = low_quota;
qq = qq_remainder = 0;
}{...}
else if (num_lowpri_links > 0) {
l2cb.ble_round_robin_quota = 0;
l2cb.ble_round_robin_unacked = 0;
qq = low_quota / num_lowpri_links;
qq_remainder = low_quota % num_lowpri_links;
}{...}
else {
l2cb.ble_round_robin_quota = 0;
l2cb.ble_round_robin_unacked = 0;
qq = qq_remainder = 0;
}{...}
L2CAP_TRACE_EVENT ("l2c_ble_link_adjust_allocation num_hipri: %u num_lowpri: %u low_quota: %u round_robin_quota: %u qq: %u",
num_hipri_links, num_lowpri_links, low_quota,
l2cb.ble_round_robin_quota, qq);
p_node = NULL;
for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
p_lcb = list_node(p_node);
if (p_lcb->in_use && p_lcb->transport == BT_TRANSPORT_LE) {
if (p_lcb->acl_priority == L2CAP_PRIORITY_HIGH) {
p_lcb->link_xmit_quota = high_pri_link_quota;
}{...} else {
if (( p_lcb->link_xmit_quota > 0 ) && ( qq == 0 )) {
l2cb.ble_round_robin_unacked += p_lcb->sent_not_acked;
}{...}
p_lcb->link_xmit_quota = qq;
if (qq_remainder > 0) {
p_lcb->link_xmit_quota++;
qq_remainder--;
}{...}
}{...}
L2CAP_TRACE_EVENT("l2c_ble_link_adjust_allocation Priority: %d XmitQuota: %d",
p_lcb->acl_priority, p_lcb->link_xmit_quota);
L2CAP_TRACE_EVENT(" SentNotAcked: %d RRUnacked: %d",
p_lcb->sent_not_acked, l2cb.round_robin_unacked);
if ( (p_lcb->link_state == LST_CONNECTED)
&& (!list_is_empty(p_lcb->link_xmit_data_q))
&& (p_lcb->sent_not_acked < p_lcb->link_xmit_quota) ) {
btu_start_timer (&p_lcb->timer_entry, BTU_TTYPE_L2CAP_LINK, L2CAP_LINK_FLOW_CONTROL_TOUT);
}{...}
}{...}
}{...}
}{ ... }
#if (defined BLE_LLT_INCLUDED) && (BLE_LLT_INCLUDED == TRUE)
/* ... */
void l2cble_process_rc_param_request_evt(UINT16 handle, UINT16 int_min, UINT16 int_max,
UINT16 latency, UINT16 timeout)
{
tL2C_LCB *p_lcb = l2cu_find_lcb_by_handle (handle);
if (p_lcb != NULL) {
if ((p_lcb->conn_update_mask & L2C_BLE_CONN_UPDATE_DISABLE) == 0) {
p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PENDING;
btsnd_hcic_ble_rc_param_req_reply(handle, int_min, int_max, latency, timeout, BLE_CE_LEN_MIN, BLE_CE_LEN_MIN);
}{...}else {
if (int_max == BTM_BLE_CONN_INT_MIN) {
p_lcb->conn_update_mask |= L2C_BLE_UPDATE_PENDING;
btsnd_hcic_ble_rc_param_req_reply(handle, int_min, int_max, latency, timeout, BLE_CE_LEN_MIN, BLE_CE_LEN_MIN);
}{...}else {
L2CAP_TRACE_EVENT ("L2CAP - LE - update currently disabled");
p_lcb->conn_update_mask |= L2C_BLE_NEW_CONN_PARAM;
btsnd_hcic_ble_rc_param_req_neg_reply (handle, HCI_ERR_UNACCEPT_CONN_INTERVAL);
}
}
}{...} else {
L2CAP_TRACE_WARNING("No link to update connection parameter")
}{...}
}{ ... }
#endif/* ... */
/* ... */
void l2cble_update_data_length(tL2C_LCB *p_lcb)
{
UINT16 tx_mtu = 0;
UINT16 i = 0;
L2CAP_TRACE_DEBUG("%s", __FUNCTION__);
if (p_lcb == NULL) {
return;
}{...}
for (i = 0; i < L2CAP_NUM_FIXED_CHNLS; i++) {
if (i + L2CAP_FIRST_FIXED_CHNL != L2CAP_BLE_SIGNALLING_CID) {
if ((p_lcb->p_fixed_ccbs[i] != NULL) &&
(tx_mtu < (p_lcb->p_fixed_ccbs[i]->tx_data_len + L2CAP_PKT_OVERHEAD))) {
tx_mtu = p_lcb->p_fixed_ccbs[i]->tx_data_len + L2CAP_PKT_OVERHEAD;
}{...}
}{...}
}{...}
if (tx_mtu > BTM_BLE_DATA_SIZE_MAX) {
tx_mtu = BTM_BLE_DATA_SIZE_MAX;
}{...}
if (p_lcb->tx_data_len != tx_mtu) {
BTM_SetBleDataLength(p_lcb->remote_bd_addr, tx_mtu);
}{...}
}{ ... }
/* ... */
void l2cble_process_data_length_change_event(UINT16 handle, UINT16 tx_data_len, UINT16 rx_data_len)
{
tL2C_LCB *p_lcb = l2cu_find_lcb_by_handle(handle);
tACL_CONN *p_acl = btm_handle_to_acl(handle);
tBTM_LE_SET_PKT_DATA_LENGTH_PARAMS data_length_params;
L2CAP_TRACE_DEBUG("%s TX data len = %d", __FUNCTION__, tx_data_len);
if (p_lcb == NULL) {
return;
}{...}
if (tx_data_len > 0) {
p_lcb->tx_data_len = tx_data_len;
}{...}
data_length_params.rx_len = rx_data_len;
data_length_params.tx_len = tx_data_len;
if(p_acl) {
p_acl->data_length_params = data_length_params;
if (p_acl->p_set_pkt_data_cback) {
(*p_acl->p_set_pkt_data_cback)(BTM_SUCCESS, &data_length_params);
}{...} else {
(*conn_callback_func.set_pkt_data_length_cb)(BTM_SUCCESS, &data_length_params);
}{...}
p_acl->data_len_updating = false;
if(p_acl->data_len_waiting) {
p_acl->data_len_waiting = false;
p_acl->p_set_pkt_data_cback = p_acl->p_set_data_len_cback_waiting;
p_acl->p_set_data_len_cback_waiting = NULL;
if(p_acl->tx_len_waiting == p_acl->data_length_params.tx_len) {
if(p_acl->p_set_pkt_data_cback) {
(*p_acl->p_set_pkt_data_cback)(BTM_SUCCESS, &p_acl->data_length_params);
}{...}
return;
}{...}
p_acl->data_len_updating = true;
btsnd_hcic_ble_set_data_length(handle, p_acl->tx_len_waiting,
BTM_BLE_DATA_TX_TIME_MAX);
}{...}
}{...}
}{ ... }
/* ... */
void l2cble_set_fixed_channel_tx_data_length(BD_ADDR remote_bda, UINT16 fix_cid, UINT16 tx_mtu)
{
tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(remote_bda, BT_TRANSPORT_LE);
UINT16 cid = fix_cid - L2CAP_FIRST_FIXED_CHNL;
L2CAP_TRACE_DEBUG("%s TX MTU = %d", __FUNCTION__, tx_mtu);
if (!controller_get_interface()->supports_ble_packet_extension()) {
L2CAP_TRACE_WARNING("%s, request not supported", __FUNCTION__);
return;
}{...}
if (p_lcb == NULL) {
return;
}{...}
if (p_lcb->p_fixed_ccbs[cid] != NULL) {
if (tx_mtu > BTM_BLE_DATA_SIZE_MAX) {
tx_mtu = BTM_BLE_DATA_SIZE_MAX;
}{...}
p_lcb->p_fixed_ccbs[cid]->tx_data_len = tx_mtu;
}{...}
l2cble_update_data_length(p_lcb);
}{ ... }
/* ... */
void l2c_send_update_conn_params_cb(tL2C_LCB *p_lcb, UINT8 status)
{
if(conn_callback_func.update_conn_param_cb != NULL){
tBTM_LE_UPDATE_CONN_PRAMS update_param;
if (p_lcb->updating_param_flag){
update_param.max_conn_int = p_lcb->updating_conn_max_interval;
update_param.min_conn_int = p_lcb->updating_conn_min_interval;
p_lcb->updating_param_flag = false;
}{...}else{
update_param.max_conn_int = update_param.min_conn_int = 0;
}
update_param.conn_int = p_lcb->current_used_conn_interval;
update_param.slave_latency = p_lcb->current_used_conn_latency;
update_param.supervision_tout = p_lcb->current_used_conn_timeout;
(conn_callback_func.update_conn_param_cb)(status, p_lcb->remote_bd_addr, &update_param);
}{...}
}{ ... }
/* ... */
UINT32 CalConnectParamTimeout(tL2C_LCB *p_lcb)
{
UINT32 timeout = 6;
if (p_lcb != NULL){
timeout = (40 * ( 1 + p_lcb->current_used_conn_latency) * p_lcb->current_used_conn_interval + 1.25 * p_lcb->waiting_update_conn_max_interval + 1000) / 1000;
if (timeout < 1){
timeout = 1;
}{...}else if (timeout > 120){
timeout = 120;
}{...}
}{...}
return timeout;
}{ ... }
/* ... */
void l2cble_credit_based_conn_req (tL2C_CCB *p_ccb)
{
if (!p_ccb) {
return;
}{...}
if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
{
L2CAP_TRACE_WARNING ("LE link doesn't exist");
return;
}{...}
l2cu_send_peer_ble_credit_based_conn_req (p_ccb);
return;
}{ ... }
/* ... */
void l2cble_credit_based_conn_res (tL2C_CCB *p_ccb, UINT16 result)
{
if (!p_ccb) {
return;
}{...}
if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
{
L2CAP_TRACE_WARNING ("LE link doesn't exist");
return;
}{...}
l2cu_send_peer_ble_credit_based_conn_res (p_ccb, result);
return;
}{ ... }
/* ... */
void l2cble_send_flow_control_credit(tL2C_CCB *p_ccb, UINT16 credit_value)
{
if (!p_ccb) {
return;
}{...}
if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
{
L2CAP_TRACE_WARNING ("LE link doesn't exist");
return;
}{...}
l2cu_send_peer_ble_flow_control_credit(p_ccb, credit_value);
return;
}{ ... }
/* ... */
void l2cble_send_peer_disc_req(tL2C_CCB *p_ccb)
{
L2CAP_TRACE_DEBUG ("%s",__func__);
if (!p_ccb) {
return;
}{...}
if (p_ccb->p_lcb && p_ccb->p_lcb->transport != BT_TRANSPORT_LE)
{
L2CAP_TRACE_WARNING ("LE link doesn't exist");
return;
}{...}
l2cu_send_peer_ble_credit_based_disconn_req(p_ccb);
return;
}{ ... }
#if (SMP_INCLUDED == TRUE)
/* ... */
void l2cble_sec_comp(BD_ADDR p_bda, tBT_TRANSPORT transport, void *p_ref_data, UINT8 status)
{
tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(p_bda, BT_TRANSPORT_LE);
tL2CAP_SEC_DATA *p_buf = NULL;
UINT8 sec_flag;
UINT8 sec_act;
if (!p_lcb)
{
L2CAP_TRACE_WARNING ("%s security complete for unknown device", __func__);
return;
}{...}
sec_act = p_lcb->sec_act;
p_lcb->sec_act = 0;
if (!fixed_queue_is_empty(p_lcb->le_sec_pending_q))
{
p_buf = (tL2CAP_SEC_DATA*) fixed_queue_dequeue(p_lcb->le_sec_pending_q, FIXED_QUEUE_MAX_TIMEOUT);
if (!p_buf)
{
L2CAP_TRACE_WARNING ("%s Security complete for request not initiated from L2CAP",
__func__);
return;
}{...}
if (status != BTM_SUCCESS)
{
(*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
}{...}
else
{
if (sec_act == BTM_SEC_ENCRYPT_MITM)
{
BTM_GetSecurityFlagsByTransport(p_bda, &sec_flag, transport);
if (sec_flag & BTM_SEC_FLAG_LKEY_AUTHED) {
(*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
}{...}
else
{
L2CAP_TRACE_DEBUG ("%s MITM Protection Not present", __func__);
(*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data,
BTM_FAILED_ON_SECURITY);
}{...}
}{...}
else
{
L2CAP_TRACE_DEBUG ("%s MITM Protection not required sec_act = %d",
__func__, p_lcb->sec_act);
(*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
}{...}
}{...}
}{...}
else
{
L2CAP_TRACE_WARNING ("%s Security complete for request not initiated from L2CAP", __func__);
return;
}{...}
osi_free(p_buf);
while (!fixed_queue_is_empty(p_lcb->le_sec_pending_q))
{
p_buf = (tL2CAP_SEC_DATA*) fixed_queue_dequeue(p_lcb->le_sec_pending_q, FIXED_QUEUE_MAX_TIMEOUT);
if (status != BTM_SUCCESS) {
(*(p_buf->p_callback))(p_bda, BT_TRANSPORT_LE, p_buf->p_ref_data, status);
}{...} else {
l2ble_sec_access_req(p_bda, p_buf->psm, p_buf->is_originator,
p_buf->p_callback, p_buf->p_ref_data);
}{...}
osi_free(p_buf);
}{...}
}{ ... }
/* ... */
BOOLEAN l2ble_sec_access_req(BD_ADDR bd_addr, UINT16 psm, BOOLEAN is_originator, tL2CAP_SEC_CBACK *p_callback, void *p_ref_data)
{
L2CAP_TRACE_DEBUG ("%s", __func__);
BOOLEAN status;
tL2C_LCB *p_lcb = NULL;
if (!p_callback)
{
L2CAP_TRACE_ERROR("%s No callback function", __func__);
return FALSE;
}{...}
p_lcb = l2cu_find_lcb_by_bd_addr(bd_addr, BT_TRANSPORT_LE);
if (!p_lcb)
{
L2CAP_TRACE_ERROR ("%s Security check for unknown device", __func__);
p_callback(bd_addr, BT_TRANSPORT_LE, p_ref_data, BTM_UNKNOWN_ADDR);
return FALSE;
}{...}
tL2CAP_SEC_DATA *p_buf = (tL2CAP_SEC_DATA*) osi_malloc((UINT16)sizeof(tL2CAP_SEC_DATA));
if (!p_buf)
{
p_callback(bd_addr, BT_TRANSPORT_LE, p_ref_data, BTM_NO_RESOURCES);
return FALSE;
}{...}
p_buf->psm = psm;
p_buf->is_originator = is_originator;
p_buf->p_callback = p_callback;
p_buf->p_ref_data = p_ref_data;
fixed_queue_enqueue(p_lcb->le_sec_pending_q, p_buf, FIXED_QUEUE_MAX_TIMEOUT);
status = btm_ble_start_sec_check(bd_addr, psm, is_originator, &l2cble_sec_comp, p_ref_data);
return status;
}{ ... }
#endif/* ... */ /* ... */
#endif
/* ... */
UINT16 L2CA_GetDisconnectReason (BD_ADDR remote_bda, tBT_TRANSPORT transport)
{
tL2C_LCB *p_lcb;
UINT16 reason = 0;
if ((p_lcb = l2cu_find_lcb_by_bd_addr (remote_bda, transport)) != NULL) {
reason = p_lcb->disc_reason;
}{...}
L2CAP_TRACE_DEBUG ("L2CA_GetDisconnectReason=%d ", reason);
return reason;
}{ ... }