1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
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
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
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
600
601
602
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
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
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
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
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
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
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
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
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
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
1696
1697
1698
|
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "main/shim/acl.h"
#include <base/location.h>
#include <base/strings/stringprintf.h>
#include <time.h>
#include <chrono>
#include <cstdint>
#include <functional>
#include <future>
#include <map>
#include <memory>
#include <string>
#include <unordered_set>
#include "btif/include/btif_hh.h"
#include "device/include/controller.h"
#include "gd/common/bidi_queue.h"
#include "gd/common/bind.h"
#include "gd/common/strings.h"
#include "gd/common/sync_map_count.h"
#include "gd/hci/acl_manager.h"
#include "gd/hci/acl_manager/acl_connection.h"
#include "gd/hci/acl_manager/classic_acl_connection.h"
#include "gd/hci/acl_manager/connection_management_callbacks.h"
#include "gd/hci/acl_manager/le_acl_connection.h"
#include "gd/hci/acl_manager/le_connection_management_callbacks.h"
#include "gd/hci/acl_manager/le_impl.h"
#include "gd/hci/address.h"
#include "gd/hci/address_with_type.h"
#include "gd/hci/class_of_device.h"
#include "gd/hci/controller.h"
#include "gd/os/handler.h"
#include "gd/os/queue.h"
#include "main/shim/btm.h"
#include "main/shim/dumpsys.h"
#include "main/shim/entry.h"
#include "main/shim/helpers.h"
#include "main/shim/stack.h"
#include "osi/include/allocator.h"
#include "stack/acl/acl.h"
#include "stack/btm/btm_int_types.h"
#include "stack/include/acl_hci_link_interface.h"
#include "stack/include/ble_acl_interface.h"
#include "stack/include/bt_hdr.h"
#include "stack/include/btm_api.h"
#include "stack/include/btm_status.h"
#include "stack/include/pan_api.h"
#include "stack/include/sec_hci_link_interface.h"
#include "stack/l2cap/l2c_int.h"
#include "types/ble_address_with_type.h"
#include "types/raw_address.h"
extern tBTM_CB btm_cb;
bt_status_t do_in_main_thread(const base::Location& from_here,
base::OnceClosure task);
using namespace bluetooth;
class ConnectAddressWithType {
public:
explicit ConnectAddressWithType(hci::AddressWithType address_with_type)
: address_(address_with_type.GetAddress()),
type_(address_with_type.ToFilterAcceptListAddressType()) {}
std::string const ToString() const {
std::stringstream ss;
ss << address_ << "[" << FilterAcceptListAddressTypeText(type_) << "]";
return ss.str();
}
bool operator==(const ConnectAddressWithType& rhs) const {
return address_ == rhs.address_ && type_ == rhs.type_;
}
private:
friend std::hash<ConnectAddressWithType>;
hci::Address address_;
hci::FilterAcceptListAddressType type_;
};
namespace std {
template <>
struct hash<ConnectAddressWithType> {
std::size_t operator()(const ConnectAddressWithType& val) const {
static_assert(sizeof(uint64_t) >=
(bluetooth::hci::Address::kLength +
sizeof(bluetooth::hci::FilterAcceptListAddressType)));
uint64_t int_addr = 0;
memcpy(reinterpret_cast<uint8_t*>(&int_addr), val.address_.data(),
bluetooth::hci::Address::kLength);
memcpy(reinterpret_cast<uint8_t*>(&int_addr) +
bluetooth::hci::Address::kLength,
&val.type_, sizeof(bluetooth::hci::FilterAcceptListAddressType));
return std::hash<uint64_t>{}(int_addr);
}
};
} // namespace std
namespace {
using HciHandle = uint16_t;
using PageNumber = uint8_t;
using CreationTime = std::chrono::time_point<std::chrono::system_clock>;
using TeardownTime = std::chrono::time_point<std::chrono::system_clock>;
constexpr char kBtmLogTag[] = "ACL";
using SendDataUpwards = void (*const)(BT_HDR*);
using OnDisconnect = std::function<void(HciHandle, hci::ErrorCode reason)>;
constexpr char kConnectionDescriptorTimeFormat[] = "%Y-%m-%d %H:%M:%S";
inline bool IsRpa(const hci::AddressWithType address_with_type) {
return address_with_type.GetAddressType() ==
hci::AddressType::RANDOM_DEVICE_ADDRESS &&
((address_with_type.GetAddress().address.data()[5] & 0xc0) == 0x40);
}
class ShadowAcceptlist {
public:
ShadowAcceptlist(uint8_t max_acceptlist_size)
: max_acceptlist_size_(max_acceptlist_size) {}
bool Add(const hci::AddressWithType& address_with_type) {
if (acceptlist_set_.size() == max_acceptlist_size_) {
LOG_ERROR("Acceptlist is full size:%zu", acceptlist_set_.size());
return false;
}
if (!acceptlist_set_.insert(ConnectAddressWithType(address_with_type))
.second) {
LOG_WARN("Attempted to add duplicate le address to acceptlist:%s",
PRIVATE_ADDRESS(address_with_type));
}
return true;
}
bool Remove(const hci::AddressWithType& address_with_type) {
auto iter = acceptlist_set_.find(ConnectAddressWithType(address_with_type));
if (iter == acceptlist_set_.end()) {
LOG_WARN("Unknown device being removed from acceptlist:%s",
PRIVATE_ADDRESS(address_with_type));
return false;
}
acceptlist_set_.erase(ConnectAddressWithType(*iter));
return true;
}
std::unordered_set<ConnectAddressWithType> GetCopy() const {
return acceptlist_set_;
}
bool IsFull() const {
return acceptlist_set_.size() == static_cast<size_t>(max_acceptlist_size_);
}
void Clear() { acceptlist_set_.clear(); }
private:
uint8_t max_acceptlist_size_{0};
std::unordered_set<ConnectAddressWithType> acceptlist_set_;
};
class ShadowAddressResolutionList {
public:
ShadowAddressResolutionList(uint8_t max_address_resolution_size)
: max_address_resolution_size_(max_address_resolution_size) {}
bool Add(const hci::AddressWithType& address_with_type) {
if (address_resolution_set_.size() == max_address_resolution_size_) {
LOG_ERROR("Address Resolution is full size:%zu",
address_resolution_set_.size());
return false;
}
if (!address_resolution_set_.insert(address_with_type).second) {
LOG_WARN("Attempted to add duplicate le address to address_resolution:%s",
PRIVATE_ADDRESS(address_with_type));
}
return true;
}
bool Remove(const hci::AddressWithType& address_with_type) {
auto iter = address_resolution_set_.find(address_with_type);
if (iter == address_resolution_set_.end()) {
LOG_WARN("Unknown device being removed from address_resolution:%s",
PRIVATE_ADDRESS(address_with_type));
return false;
}
address_resolution_set_.erase(iter);
return true;
}
std::unordered_set<hci::AddressWithType> GetCopy() const {
return address_resolution_set_;
}
bool IsFull() const {
return address_resolution_set_.size() ==
static_cast<size_t>(max_address_resolution_size_);
}
size_t Size() const { return address_resolution_set_.size(); }
void Clear() { address_resolution_set_.clear(); }
private:
uint8_t max_address_resolution_size_{0};
std::unordered_set<hci::AddressWithType> address_resolution_set_;
};
struct ConnectionDescriptor {
CreationTime creation_time_;
TeardownTime teardown_time_;
uint16_t handle_;
bool is_locally_initiated_;
hci::ErrorCode disconnect_reason_;
ConnectionDescriptor(CreationTime creation_time, TeardownTime teardown_time,
uint16_t handle, bool is_locally_initiated,
hci::ErrorCode disconnect_reason)
: creation_time_(creation_time),
teardown_time_(teardown_time),
handle_(handle),
is_locally_initiated_(is_locally_initiated),
disconnect_reason_(disconnect_reason) {}
virtual std::string GetPrivateRemoteAddress() const = 0;
virtual ~ConnectionDescriptor() {}
std::string ToString() const {
return base::StringPrintf(
"peer:%s handle:0x%04x is_locally_initiated:%s"
" creation_time:%s teardown_time:%s disconnect_reason:%s",
GetPrivateRemoteAddress().c_str(), handle_,
logbool(is_locally_initiated_).c_str(),
common::StringFormatTimeWithMilliseconds(
kConnectionDescriptorTimeFormat, creation_time_)
.c_str(),
common::StringFormatTimeWithMilliseconds(
kConnectionDescriptorTimeFormat, teardown_time_)
.c_str(),
hci::ErrorCodeText(disconnect_reason_).c_str());
}
};
struct ClassicConnectionDescriptor : public ConnectionDescriptor {
const hci::Address remote_address_;
ClassicConnectionDescriptor(const hci::Address& remote_address,
CreationTime creation_time,
TeardownTime teardown_time, uint16_t handle,
bool is_locally_initiated,
hci::ErrorCode disconnect_reason)
: ConnectionDescriptor(creation_time, teardown_time, handle,
is_locally_initiated, disconnect_reason),
remote_address_(remote_address) {}
virtual std::string GetPrivateRemoteAddress() const {
return PRIVATE_ADDRESS(remote_address_);
}
};
struct LeConnectionDescriptor : public ConnectionDescriptor {
const hci::AddressWithType remote_address_with_type_;
LeConnectionDescriptor(hci::AddressWithType& remote_address_with_type,
CreationTime creation_time, TeardownTime teardown_time,
uint16_t handle, bool is_locally_initiated,
hci::ErrorCode disconnect_reason)
: ConnectionDescriptor(creation_time, teardown_time, handle,
is_locally_initiated, disconnect_reason),
remote_address_with_type_(remote_address_with_type) {}
std::string GetPrivateRemoteAddress() const {
return PRIVATE_ADDRESS(remote_address_with_type_);
}
};
template <typename T>
class FixedQueue {
public:
explicit FixedQueue(size_t max_size) : max_size_(max_size) {}
void Push(T element) {
if (queue_.size() == max_size_) {
queue_.pop_front();
}
queue_.push_back(std::move(element));
}
std::vector<std::string> ReadElementsAsString() const {
std::vector<std::string> vector;
for (auto& entry : queue_) {
vector.push_back(entry->ToString());
}
return vector;
}
private:
size_t max_size_{1};
std::deque<T> queue_;
};
constexpr size_t kConnectionHistorySize = 40;
inline uint8_t LowByte(uint16_t val) { return val & 0xff; }
inline uint8_t HighByte(uint16_t val) { return val >> 8; }
void ValidateAclInterface(const shim::legacy::acl_interface_t& acl_interface) {
ASSERT_LOG(acl_interface.on_send_data_upwards != nullptr,
"Must provide to receive data on acl links");
ASSERT_LOG(acl_interface.on_packets_completed != nullptr,
"Must provide to receive completed packet indication");
ASSERT_LOG(acl_interface.connection.classic.on_connected != nullptr,
"Must provide to respond to successful classic connections");
ASSERT_LOG(acl_interface.connection.classic.on_failed != nullptr,
"Must provide to respond when classic connection attempts fail");
ASSERT_LOG(
acl_interface.connection.classic.on_disconnected != nullptr,
"Must provide to respond when active classic connection disconnects");
ASSERT_LOG(acl_interface.connection.le.on_connected != nullptr,
"Must provide to respond to successful le connections");
ASSERT_LOG(acl_interface.connection.le.on_failed != nullptr,
"Must provide to respond when le connection attempts fail");
ASSERT_LOG(acl_interface.connection.le.on_disconnected != nullptr,
"Must provide to respond when active le connection disconnects");
}
} // namespace
#define TRY_POSTING_ON_MAIN(cb, ...) \
do { \
if (cb == nullptr) { \
LOG_WARN("Dropping ACL event with no callback"); \
} else { \
do_in_main_thread(FROM_HERE, base::Bind(cb, ##__VA_ARGS__)); \
} \
} while (0)
constexpr HciHandle kInvalidHciHandle = 0xffff;
class ShimAclConnection {
public:
ShimAclConnection(const HciHandle handle, SendDataUpwards send_data_upwards,
os::Handler* handler,
hci::acl_manager::AclConnection::QueueUpEnd* queue_up_end,
CreationTime creation_time)
: handle_(handle),
handler_(handler),
send_data_upwards_(send_data_upwards),
queue_up_end_(queue_up_end),
creation_time_(creation_time) {
queue_up_end_->RegisterDequeue(
handler_, common::Bind(&ShimAclConnection::data_ready_callback,
common::Unretained(this)));
}
virtual ~ShimAclConnection() {
if (!queue_.empty())
LOG_ERROR(
"ACL cleaned up with non-empty queue handle:0x%04x stranded_pkts:%zu",
handle_, queue_.size());
ASSERT_LOG(is_disconnected_,
"Shim Acl was not properly disconnected handle:0x%04x", handle_);
}
void EnqueuePacket(std::unique_ptr<packet::RawBuilder> packet) {
// TODO Handle queue size exceeds some threshold
queue_.push(std::move(packet));
RegisterEnqueue();
}
std::unique_ptr<packet::BasePacketBuilder> handle_enqueue() {
auto packet = std::move(queue_.front());
queue_.pop();
if (queue_.empty()) {
UnregisterEnqueue();
}
return packet;
}
void data_ready_callback() {
auto packet = queue_up_end_->TryDequeue();
uint16_t length = packet->size();
std::vector<uint8_t> preamble;
preamble.push_back(LowByte(handle_));
preamble.push_back(HighByte(handle_));
preamble.push_back(LowByte(length));
preamble.push_back(HighByte(length));
BT_HDR* p_buf = MakeLegacyBtHdrPacket(std::move(packet), preamble);
ASSERT_LOG(p_buf != nullptr,
"Unable to allocate BT_HDR legacy packet handle:%04x", handle_);
if (send_data_upwards_ == nullptr) {
LOG_WARN("Dropping ACL data with no callback");
osi_free(p_buf);
} else if (do_in_main_thread(FROM_HERE,
base::Bind(send_data_upwards_, p_buf)) !=
BT_STATUS_SUCCESS) {
osi_free(p_buf);
}
}
virtual void InitiateDisconnect(hci::DisconnectReason reason) = 0;
virtual bool IsLocallyInitiated() const = 0;
CreationTime GetCreationTime() const { return creation_time_; }
uint16_t Handle() const { return handle_; }
void Shutdown() {
Disconnect();
LOG_INFO("Shutdown and disconnect ACL connection handle:0x%04x", handle_);
}
protected:
const uint16_t handle_{kInvalidHciHandle};
os::Handler* handler_;
void UnregisterEnqueue() {
if (!is_enqueue_registered_) return;
is_enqueue_registered_ = false;
queue_up_end_->UnregisterEnqueue();
}
void Disconnect() {
if (is_disconnected_) {
LOG_ERROR(
"Cannot disconnect ACL multiple times handle:%04x creation_time:%s",
handle_,
common::StringFormatTimeWithMilliseconds(
kConnectionDescriptorTimeFormat, creation_time_)
.c_str());
return;
}
is_disconnected_ = true;
UnregisterEnqueue();
queue_up_end_->UnregisterDequeue();
if (!queue_.empty())
LOG_WARN(
"ACL disconnect with non-empty queue handle:%04x stranded_pkts::%zu",
handle_, queue_.size());
}
virtual void ReadRemoteControllerInformation() = 0;
private:
SendDataUpwards send_data_upwards_;
hci::acl_manager::AclConnection::QueueUpEnd* queue_up_end_;
std::queue<std::unique_ptr<packet::RawBuilder>> queue_;
bool is_enqueue_registered_{false};
bool is_disconnected_{false};
CreationTime creation_time_;
void RegisterEnqueue() {
ASSERT_LOG(!is_disconnected_,
"Unable to send data over disconnected channel handle:%04x",
handle_);
if (is_enqueue_registered_) return;
is_enqueue_registered_ = true;
queue_up_end_->RegisterEnqueue(
handler_, common::Bind(&ShimAclConnection::handle_enqueue,
common::Unretained(this)));
}
virtual void RegisterCallbacks() = 0;
};
class ClassicShimAclConnection
: public ShimAclConnection,
public hci::acl_manager::ConnectionManagementCallbacks {
public:
ClassicShimAclConnection(
SendDataUpwards send_data_upwards, OnDisconnect on_disconnect,
const shim::legacy::acl_classic_link_interface_t& interface,
os::Handler* handler,
std::unique_ptr<hci::acl_manager::ClassicAclConnection> connection,
CreationTime creation_time)
: ShimAclConnection(connection->GetHandle(), send_data_upwards, handler,
connection->GetAclQueueEnd(), creation_time),
on_disconnect_(on_disconnect),
interface_(interface),
connection_(std::move(connection)) {}
void RegisterCallbacks() override {
connection_->RegisterCallbacks(this, handler_);
}
void ReadRemoteControllerInformation() override {
connection_->ReadRemoteVersionInformation();
connection_->ReadRemoteSupportedFeatures();
}
void OnConnectionPacketTypeChanged(uint16_t packet_type) override {
TRY_POSTING_ON_MAIN(interface_.on_packet_type_changed, packet_type);
}
void OnAuthenticationComplete(hci::ErrorCode hci_status) override {
TRY_POSTING_ON_MAIN(interface_.on_authentication_complete, handle_,
ToLegacyHciErrorCode(hci_status));
}
void OnEncryptionChange(hci::EncryptionEnabled enabled) override {
bool is_enabled = (enabled == hci::EncryptionEnabled::ON ||
enabled == hci::EncryptionEnabled::BR_EDR_AES_CCM);
TRY_POSTING_ON_MAIN(interface_.on_encryption_change, is_enabled);
}
void OnChangeConnectionLinkKeyComplete() override {
TRY_POSTING_ON_MAIN(interface_.on_change_connection_link_key_complete);
}
void OnReadClockOffsetComplete(uint16_t clock_offset) override {
LOG_INFO("UNIMPLEMENTED");
}
void OnModeChange(hci::ErrorCode status, hci::Mode current_mode,
uint16_t interval) override {
TRY_POSTING_ON_MAIN(interface_.on_mode_change, ToLegacyHciErrorCode(status),
handle_, ToLegacyHciMode(current_mode), interval);
}
void OnSniffSubrating(hci::ErrorCode hci_status,
uint16_t maximum_transmit_latency,
uint16_t maximum_receive_latency,
uint16_t minimum_remote_timeout,
uint16_t minimum_local_timeout) {
TRY_POSTING_ON_MAIN(interface_.on_sniff_subrating,
ToLegacyHciErrorCode(hci_status), handle_,
maximum_transmit_latency, maximum_receive_latency,
minimum_remote_timeout, minimum_local_timeout);
}
void OnQosSetupComplete(hci::ServiceType service_type, uint32_t token_rate,
uint32_t peak_bandwidth, uint32_t latency,
uint32_t delay_variation) override {
LOG_INFO("UNIMPLEMENTED");
}
void OnFlowSpecificationComplete(hci::FlowDirection flow_direction,
hci::ServiceType service_type,
uint32_t token_rate,
uint32_t token_bucket_size,
uint32_t peak_bandwidth,
uint32_t access_latency) override {
LOG_INFO("UNIMPLEMENTED");
}
void OnFlushOccurred() override { LOG_INFO("UNIMPLEMENTED"); }
void OnRoleDiscoveryComplete(hci::Role current_role) override {
LOG_INFO("UNIMPLEMENTED");
}
void OnReadLinkPolicySettingsComplete(
uint16_t link_policy_settings) override {
LOG_INFO("UNIMPLEMENTED");
}
void OnReadAutomaticFlushTimeoutComplete(uint16_t flush_timeout) override {
LOG_INFO("UNIMPLEMENTED");
}
void OnReadTransmitPowerLevelComplete(uint8_t transmit_power_level) override {
LOG_INFO("UNIMPLEMENTED");
}
void OnReadLinkSupervisionTimeoutComplete(
uint16_t link_supervision_timeout) override {
LOG_INFO("UNIMPLEMENTED");
}
void OnReadFailedContactCounterComplete(
uint16_t failed_contact_counter) override {
LOG_INFO("UNIMPLEMENTED");
}
void OnReadLinkQualityComplete(uint8_t link_quality) override {
LOG_INFO("UNIMPLEMENTED");
}
void OnReadAfhChannelMapComplete(
hci::AfhMode afh_mode, std::array<uint8_t, 10> afh_channel_map) override {
LOG_INFO("UNIMPLEMENTED");
}
void OnReadRssiComplete(uint8_t rssi) override { LOG_INFO("UNIMPLEMENTED"); }
void OnReadClockComplete(uint32_t clock, uint16_t accuracy) override {
LOG_INFO("UNIMPLEMENTED");
}
void OnCentralLinkKeyComplete(hci::KeyFlag key_flag) override {
LOG_INFO("%s UNIMPLEMENTED", __func__);
}
void OnRoleChange(hci::ErrorCode hci_status, hci::Role new_role) override {
TRY_POSTING_ON_MAIN(
interface_.on_role_change, ToLegacyHciErrorCode(hci_status),
ToRawAddress(connection_->GetAddress()), ToLegacyRole(new_role));
BTM_LogHistory(kBtmLogTag, ToRawAddress(connection_->GetAddress()),
"Role change",
base::StringPrintf("classic New_role:%s status:%s",
hci::RoleText(new_role).c_str(),
hci::ErrorCodeText(hci_status).c_str()));
}
void OnDisconnection(hci::ErrorCode reason) override {
Disconnect();
on_disconnect_(handle_, reason);
}
void OnReadRemoteVersionInformationComplete(hci::ErrorCode hci_status,
uint8_t lmp_version,
uint16_t manufacturer_name,
uint16_t sub_version) override {
TRY_POSTING_ON_MAIN(interface_.on_read_remote_version_information_complete,
ToLegacyHciErrorCode(hci_status), handle_, lmp_version,
manufacturer_name, sub_version);
}
void OnReadRemoteSupportedFeaturesComplete(uint64_t features) override {
TRY_POSTING_ON_MAIN(interface_.on_read_remote_supported_features_complete,
handle_, features);
if (features & ((uint64_t(1) << 63))) {
connection_->ReadRemoteExtendedFeatures(1);
return;
}
LOG_DEBUG("Device does not support extended features");
}
void OnReadRemoteExtendedFeaturesComplete(uint8_t page_number,
uint8_t max_page_number,
uint64_t features) override {
TRY_POSTING_ON_MAIN(interface_.on_read_remote_extended_features_complete,
handle_, page_number, max_page_number, features);
// Supported features aliases to extended features page 0
if (page_number == 0 && !(features & ((uint64_t(1) << 63)))) {
LOG_DEBUG("Device does not support extended features");
return;
}
if (max_page_number != 0 && page_number != max_page_number)
connection_->ReadRemoteExtendedFeatures(page_number + 1);
}
hci::Address GetRemoteAddress() const { return connection_->GetAddress(); }
void InitiateDisconnect(hci::DisconnectReason reason) override {
connection_->Disconnect(reason);
}
void HoldMode(uint16_t max_interval, uint16_t min_interval) {
ASSERT(connection_->HoldMode(max_interval, min_interval));
}
void SniffMode(uint16_t max_interval, uint16_t min_interval, uint16_t attempt,
uint16_t timeout) {
ASSERT(
connection_->SniffMode(max_interval, min_interval, attempt, timeout));
}
void ExitSniffMode() { ASSERT(connection_->ExitSniffMode()); }
void SniffSubrating(uint16_t maximum_latency, uint16_t minimum_remote_timeout,
uint16_t minimum_local_timeout) {
ASSERT(connection_->SniffSubrating(maximum_latency, minimum_remote_timeout,
minimum_local_timeout));
}
void SetConnectionEncryption(hci::Enable is_encryption_enabled) {
ASSERT(connection_->SetConnectionEncryption(is_encryption_enabled));
}
bool IsLocallyInitiated() const override {
return connection_->locally_initiated_;
}
private:
OnDisconnect on_disconnect_;
const shim::legacy::acl_classic_link_interface_t interface_;
std::unique_ptr<hci::acl_manager::ClassicAclConnection> connection_;
};
class LeShimAclConnection
: public ShimAclConnection,
public hci::acl_manager::LeConnectionManagementCallbacks {
public:
LeShimAclConnection(
SendDataUpwards send_data_upwards, OnDisconnect on_disconnect,
const shim::legacy::acl_le_link_interface_t& interface,
os::Handler* handler,
std::unique_ptr<hci::acl_manager::LeAclConnection> connection,
std::chrono::time_point<std::chrono::system_clock> creation_time)
: ShimAclConnection(connection->GetHandle(), send_data_upwards, handler,
connection->GetAclQueueEnd(), creation_time),
on_disconnect_(on_disconnect),
interface_(interface),
connection_(std::move(connection)) {}
void RegisterCallbacks() override {
connection_->RegisterCallbacks(this, handler_);
}
void ReadRemoteControllerInformation() override {
// TODO Issue LeReadRemoteFeatures Command
}
bluetooth::hci::AddressWithType GetLocalAddressWithType() {
return connection_->GetLocalAddress();
}
void OnConnectionUpdate(hci::ErrorCode hci_status,
uint16_t connection_interval,
uint16_t connection_latency,
uint16_t supervision_timeout) {
TRY_POSTING_ON_MAIN(
interface_.on_connection_update, ToLegacyHciErrorCode(hci_status),
handle_, connection_interval, connection_latency, supervision_timeout);
}
void OnDataLengthChange(uint16_t max_tx_octets, uint16_t max_tx_time,
uint16_t max_rx_octets, uint16_t max_rx_time) {
TRY_POSTING_ON_MAIN(interface_.on_data_length_change, handle_,
max_tx_octets, max_tx_time, max_rx_octets, max_rx_time);
}
void OnReadRemoteVersionInformationComplete(hci::ErrorCode hci_status,
uint8_t lmp_version,
uint16_t manufacturer_name,
uint16_t sub_version) override {
TRY_POSTING_ON_MAIN(interface_.on_read_remote_version_information_complete,
ToLegacyHciErrorCode(hci_status), handle_, lmp_version,
manufacturer_name, sub_version);
}
void OnLeReadRemoteFeaturesComplete(hci::ErrorCode hci_status,
uint64_t features) {
// TODO
}
void OnPhyUpdate(hci::ErrorCode hci_status, uint8_t tx_phy,
uint8_t rx_phy) override {
TRY_POSTING_ON_MAIN(interface_.on_phy_update,
ToLegacyHciErrorCode(hci_status), handle_, tx_phy,
rx_phy);
}
void OnLocalAddressUpdate(hci::AddressWithType address_with_type) override {
connection_->UpdateLocalAddress(address_with_type);
}
void OnDisconnection(hci::ErrorCode reason) {
Disconnect();
on_disconnect_(handle_, reason);
}
hci::AddressWithType GetRemoteAddressWithType() const {
return connection_->GetRemoteAddress();
}
void InitiateDisconnect(hci::DisconnectReason reason) override {
connection_->Disconnect(reason);
}
bool IsLocallyInitiated() const override {
return connection_->locally_initiated_;
}
bool IsInFilterAcceptList() const {
return connection_->IsInFilterAcceptList();
}
private:
OnDisconnect on_disconnect_;
const shim::legacy::acl_le_link_interface_t interface_;
std::unique_ptr<hci::acl_manager::LeAclConnection> connection_;
};
struct shim::legacy::Acl::impl {
impl(uint8_t max_acceptlist_size, uint8_t max_address_resolution_size)
: shadow_acceptlist_(ShadowAcceptlist(max_acceptlist_size)),
shadow_address_resolution_list_(
ShadowAddressResolutionList(max_address_resolution_size)) {}
std::map<HciHandle, std::unique_ptr<ClassicShimAclConnection>>
handle_to_classic_connection_map_;
std::map<HciHandle, std::unique_ptr<LeShimAclConnection>>
handle_to_le_connection_map_;
SyncMapCount<std::string> classic_acl_disconnect_reason_;
SyncMapCount<std::string> le_acl_disconnect_reason_;
FixedQueue<std::unique_ptr<ConnectionDescriptor>> connection_history_ =
FixedQueue<std::unique_ptr<ConnectionDescriptor>>(kConnectionHistorySize);
ShadowAcceptlist shadow_acceptlist_;
ShadowAddressResolutionList shadow_address_resolution_list_;
bool IsClassicAcl(HciHandle handle) {
return handle_to_classic_connection_map_.find(handle) !=
handle_to_classic_connection_map_.end();
}
void EnqueueClassicPacket(HciHandle handle,
std::unique_ptr<packet::RawBuilder> packet) {
ASSERT_LOG(IsClassicAcl(handle), "handle %d is not a classic connection",
handle);
handle_to_classic_connection_map_[handle]->EnqueuePacket(std::move(packet));
}
bool IsLeAcl(HciHandle handle) {
return handle_to_le_connection_map_.find(handle) !=
handle_to_le_connection_map_.end();
}
void EnqueueLePacket(HciHandle handle,
std::unique_ptr<packet::RawBuilder> packet) {
ASSERT_LOG(IsLeAcl(handle), "handle %d is not a LE connection", handle);
handle_to_le_connection_map_[handle]->EnqueuePacket(std::move(packet));
}
void ShutdownClassicConnections(std::promise<void> promise) {
LOG_INFO("Shutdown gd acl shim classic connections");
for (auto& connection : handle_to_classic_connection_map_) {
connection.second->Shutdown();
}
handle_to_classic_connection_map_.clear();
promise.set_value();
}
void ShutdownLeConnections(std::promise<void> promise) {
LOG_INFO("Shutdown gd acl shim le connections");
for (auto& connection : handle_to_le_connection_map_) {
connection.second->Shutdown();
}
handle_to_le_connection_map_.clear();
promise.set_value();
}
void FinalShutdown(std::promise<void> promise) {
if (!handle_to_classic_connection_map_.empty()) {
for (auto& connection : handle_to_classic_connection_map_) {
connection.second->Shutdown();
}
handle_to_classic_connection_map_.clear();
LOG_INFO("Cleared all classic connections count:%zu",
handle_to_classic_connection_map_.size());
}
if (!handle_to_le_connection_map_.empty()) {
for (auto& connection : handle_to_le_connection_map_) {
connection.second->Shutdown();
}
handle_to_le_connection_map_.clear();
LOG_INFO("Cleared all le connections count:%zu",
handle_to_le_connection_map_.size());
}
promise.set_value();
}
void HoldMode(HciHandle handle, uint16_t max_interval,
uint16_t min_interval) {
ASSERT_LOG(IsClassicAcl(handle), "handle %d is not a classic connection",
handle);
handle_to_classic_connection_map_[handle]->HoldMode(max_interval,
min_interval);
}
void ExitSniffMode(HciHandle handle) {
ASSERT_LOG(IsClassicAcl(handle), "handle %d is not a classic connection",
handle);
handle_to_classic_connection_map_[handle]->ExitSniffMode();
}
void SniffMode(HciHandle handle, uint16_t max_interval, uint16_t min_interval,
uint16_t attempt, uint16_t timeout) {
ASSERT_LOG(IsClassicAcl(handle), "handle %d is not a classic connection",
handle);
handle_to_classic_connection_map_[handle]->SniffMode(
max_interval, min_interval, attempt, timeout);
}
void SniffSubrating(HciHandle handle, uint16_t maximum_latency,
uint16_t minimum_remote_timeout,
uint16_t minimum_local_timeout) {
ASSERT_LOG(IsClassicAcl(handle), "handle %d is not a classic connection",
handle);
handle_to_classic_connection_map_[handle]->SniffSubrating(
maximum_latency, minimum_remote_timeout, minimum_local_timeout);
}
void SetConnectionEncryption(HciHandle handle, hci::Enable enable) {
ASSERT_LOG(IsClassicAcl(handle), "handle %d is not a classic connection",
handle);
handle_to_classic_connection_map_[handle]->SetConnectionEncryption(enable);
}
void disconnect_classic(uint16_t handle, tHCI_STATUS reason,
std::string comment) {
auto connection = handle_to_classic_connection_map_.find(handle);
if (connection != handle_to_classic_connection_map_.end()) {
auto remote_address = connection->second->GetRemoteAddress();
connection->second->InitiateDisconnect(
ToDisconnectReasonFromLegacy(reason));
LOG_DEBUG("Disconnection initiated classic remote:%s handle:%hu",
PRIVATE_ADDRESS(remote_address), handle);
BTM_LogHistory(kBtmLogTag, ToRawAddress(remote_address),
"Disconnection initiated",
base::StringPrintf("classic reason:%s comment:%s",
hci_status_code_text(reason).c_str(),
comment.c_str()));
classic_acl_disconnect_reason_.Put(comment);
} else {
LOG_WARN("Unable to disconnect unknown classic connection handle:0x%04x",
handle);
}
}
void disconnect_le(uint16_t handle, tHCI_STATUS reason, std::string comment) {
auto connection = handle_to_le_connection_map_.find(handle);
if (connection != handle_to_le_connection_map_.end()) {
auto remote_address_with_type =
connection->second->GetRemoteAddressWithType();
GetAclManager()->RemoveFromBackgroundList(remote_address_with_type);
connection->second->InitiateDisconnect(
ToDisconnectReasonFromLegacy(reason));
LOG_DEBUG("Disconnection initiated le remote:%s handle:%hu",
PRIVATE_ADDRESS(remote_address_with_type), handle);
BTM_LogHistory(kBtmLogTag,
ToLegacyAddressWithType(remote_address_with_type),
"Disconnection initiated",
base::StringPrintf("Le reason:%s comment:%s",
hci_status_code_text(reason).c_str(),
comment.c_str()));
le_acl_disconnect_reason_.Put(comment);
} else {
LOG_WARN("Unable to disconnect unknown le connection handle:0x%04x",
handle);
}
}
void accept_le_connection_from(const hci::AddressWithType& address_with_type,
bool is_direct, std::promise<bool> promise) {
if (shadow_acceptlist_.IsFull()) {
LOG_ERROR("Acceptlist is full preventing new Le connection");
promise.set_value(false);
return;
}
shadow_acceptlist_.Add(address_with_type);
promise.set_value(true);
GetAclManager()->CreateLeConnection(address_with_type, is_direct);
LOG_DEBUG("Allow Le connection from remote:%s",
PRIVATE_ADDRESS(address_with_type));
BTM_LogHistory(kBtmLogTag, ToLegacyAddressWithType(address_with_type),
"Allow connection from", "Le");
}
void ignore_le_connection_from(
const hci::AddressWithType& address_with_type) {
shadow_acceptlist_.Remove(address_with_type);
GetAclManager()->CancelLeConnectAndRemoveFromBackgroundList(
address_with_type);
LOG_DEBUG("Ignore Le connection from remote:%s",
PRIVATE_ADDRESS(address_with_type));
BTM_LogHistory(kBtmLogTag, ToLegacyAddressWithType(address_with_type),
"Ignore connection from", "Le");
}
void clear_acceptlist() {
auto shadow_acceptlist = shadow_acceptlist_.GetCopy();
size_t count = shadow_acceptlist.size();
GetAclManager()->ClearFilterAcceptList();
shadow_acceptlist_.Clear();
LOG_DEBUG("Cleared entire Le address acceptlist count:%zu", count);
}
void AddToAddressResolution(const hci::AddressWithType& address_with_type,
const std::array<uint8_t, 16>& peer_irk,
const std::array<uint8_t, 16>& local_irk) {
if (shadow_address_resolution_list_.IsFull()) {
LOG_WARN("Le Address Resolution list is full size:%zu",
shadow_address_resolution_list_.Size());
return;
}
// TODO This should really be added upon successful completion
shadow_address_resolution_list_.Add(address_with_type);
GetAclManager()->AddDeviceToResolvingList(address_with_type, peer_irk,
local_irk);
}
void RemoveFromAddressResolution(
const hci::AddressWithType& address_with_type) {
// TODO This should really be removed upon successful removal
if (!shadow_address_resolution_list_.Remove(address_with_type)) {
LOG_WARN("Unable to remove from Le Address Resolution list device:%s",
PRIVATE_ADDRESS(address_with_type));
}
GetAclManager()->RemoveDeviceFromResolvingList(address_with_type);
}
void ClearResolvingList() {
GetAclManager()->ClearResolvingList();
// TODO This should really be cleared after successful clear status
shadow_address_resolution_list_.Clear();
}
void DumpConnectionHistory() const {
std::vector<std::string> history =
connection_history_.ReadElementsAsString();
for (auto& entry : history) {
LOG_DEBUG("%s", entry.c_str());
}
const auto acceptlist = shadow_acceptlist_.GetCopy();
LOG_DEBUG("Shadow le accept list size:%-3zu controller_max_size:%hhu",
acceptlist.size(),
controller_get_interface()->get_ble_acceptlist_size());
for (auto& entry : acceptlist) {
LOG_DEBUG("acceptlist:%s", entry.ToString().c_str());
}
}
#define DUMPSYS_TAG "shim::acl"
void DumpConnectionHistory(int fd) const {
std::vector<std::string> history =
connection_history_.ReadElementsAsString();
for (auto& entry : history) {
LOG_DUMPSYS(fd, "%s", entry.c_str());
}
if (classic_acl_disconnect_reason_.Size() > 0) {
LOG_DUMPSYS(fd, "Classic sources of initiated disconnects");
for (const auto& item :
classic_acl_disconnect_reason_.GetSortedHighToLow()) {
LOG_DUMPSYS(fd, " %s:%zu", item.item.c_str(), item.count);
}
}
if (le_acl_disconnect_reason_.Size() > 0) {
LOG_DUMPSYS(fd, "Le sources of initiated disconnects");
for (const auto& item : le_acl_disconnect_reason_.GetSortedHighToLow()) {
LOG_DUMPSYS(fd, " %s:%zu", item.item.c_str(), item.count);
}
}
auto acceptlist = shadow_acceptlist_.GetCopy();
LOG_DUMPSYS(fd,
"Shadow le accept list size:%-3zu "
"controller_max_size:%hhu",
acceptlist.size(),
controller_get_interface()->get_ble_acceptlist_size());
unsigned cnt = 0;
for (auto& entry : acceptlist) {
LOG_DUMPSYS(fd, " %03u %s", ++cnt, entry.ToString().c_str());
}
auto address_resolution_list = shadow_address_resolution_list_.GetCopy();
LOG_DUMPSYS(fd,
"Shadow le address resolution list size:%-3zu "
"controller_max_size:%hhu",
address_resolution_list.size(),
controller_get_interface()->get_ble_resolving_list_max_size());
cnt = 0;
for (auto& entry : address_resolution_list) {
LOG_DUMPSYS(fd, " %03u %s", ++cnt, entry.ToString().c_str());
}
}
#undef DUMPSYS_TAG
};
#define DUMPSYS_TAG "shim::legacy::l2cap"
extern tL2C_CB l2cb;
void DumpsysL2cap(int fd) {
LOG_DUMPSYS_TITLE(fd, DUMPSYS_TAG);
for (int i = 0; i < MAX_L2CAP_LINKS; i++) {
const tL2C_LCB& lcb = l2cb.lcb_pool[i];
if (!lcb.in_use) continue;
LOG_DUMPSYS(fd, "link_state:%s", link_state_text(lcb.link_state).c_str());
LOG_DUMPSYS(fd, "handle:0x%04x", lcb.Handle());
const tL2C_CCB* ccb = lcb.ccb_queue.p_first_ccb;
while (ccb != nullptr) {
LOG_DUMPSYS(
fd, " active channel lcid:0x%04x rcid:0x%04x is_ecoc:%s in_use:%s",
ccb->local_cid, ccb->remote_cid, common::ToString(ccb->ecoc).c_str(),
common::ToString(ccb->in_use).c_str());
ccb = ccb->p_next_ccb;
}
}
}
#undef DUMPSYS_TAG
#define DUMPSYS_TAG "shim::legacy::acl"
void DumpsysAcl(int fd) {
const tACL_CB& acl_cb = btm_cb.acl_cb_;
LOG_DUMPSYS_TITLE(fd, DUMPSYS_TAG);
shim::Stack::GetInstance()->GetAcl()->DumpConnectionHistory(fd);
for (int i = 0; i < MAX_L2CAP_LINKS; i++) {
const tACL_CONN& link = acl_cb.acl_db[i];
if (!link.in_use) continue;
LOG_DUMPSYS(fd, "remote_addr:%s handle:0x%04x transport:%s",
link.remote_addr.ToString().c_str(), link.hci_handle,
bt_transport_text(link.transport).c_str());
LOG_DUMPSYS(fd, " link_up_issued:%5s",
(link.link_up_issued) ? "true" : "false");
LOG_DUMPSYS(fd, " flush_timeout:0x%04x", link.flush_timeout_in_ticks);
LOG_DUMPSYS(fd, " link_supervision_timeout:%.3f sec",
ticks_to_seconds(link.link_super_tout));
LOG_DUMPSYS(fd, " disconnect_reason:0x%02x", link.disconnect_reason);
if (link.is_transport_br_edr()) {
for (int j = 0; j < HCI_EXT_FEATURES_PAGE_MAX + 1; j++) {
if (!link.peer_lmp_feature_valid[j]) continue;
LOG_DUMPSYS(fd, " peer_lmp_features[%d] valid:%s data:%s", j,
common::ToString(link.peer_lmp_feature_valid[j]).c_str(),
bd_features_text(link.peer_lmp_feature_pages[j]).c_str());
}
LOG_DUMPSYS(fd, " [classic] link_policy:%s",
link_policy_text(static_cast<tLINK_POLICY>(link.link_policy))
.c_str());
LOG_DUMPSYS(fd, " [classic] sniff_subrating:%s",
common::ToString(HCI_SNIFF_SUB_RATE_SUPPORTED(
link.peer_lmp_feature_pages[0]))
.c_str());
LOG_DUMPSYS(fd, " pkt_types_mask:0x%04x", link.pkt_types_mask);
LOG_DUMPSYS(fd, " role:%s", RoleText(link.link_role).c_str());
} else if (link.is_transport_ble()) {
LOG_DUMPSYS(fd, " [le] peer_features valid:%s data:%s",
common::ToString(link.peer_le_features_valid).c_str(),
bd_features_text(link.peer_le_features).c_str());
LOG_DUMPSYS(fd, " [le] active_remote_addr:%s[%s]",
link.active_remote_addr.ToString().c_str(),
AddressTypeText(link.active_remote_addr_type).c_str());
LOG_DUMPSYS(fd, " [le] conn_addr:%s[%s]",
link.conn_addr.ToString().c_str(),
AddressTypeText(link.conn_addr_type).c_str());
}
}
}
#undef DUMPSYS_TAG
using Record = common::TimestampedEntry<std::string>;
const std::string kTimeFormat("%Y-%m-%d %H:%M:%S");
#define DUMPSYS_TAG "shim::legacy::hid"
extern btif_hh_cb_t btif_hh_cb;
void DumpsysHid(int fd) {
LOG_DUMPSYS_TITLE(fd, DUMPSYS_TAG);
LOG_DUMPSYS(fd, "status:%s num_devices:%u",
btif_hh_status_text(btif_hh_cb.status).c_str(),
btif_hh_cb.device_num);
LOG_DUMPSYS(fd, "status:%s", btif_hh_status_text(btif_hh_cb.status).c_str());
for (unsigned i = 0; i < BTIF_HH_MAX_HID; i++) {
const btif_hh_device_t* p_dev = &btif_hh_cb.devices[i];
if (p_dev->bd_addr != RawAddress::kEmpty) {
LOG_DUMPSYS(fd, " %u: addr:%s fd:%d state:%s ready:%s thread_id:%d", i,
PRIVATE_ADDRESS(p_dev->bd_addr), p_dev->fd,
bthh_connection_state_text(p_dev->dev_status).c_str(),
(p_dev->ready_for_data) ? ("T") : ("F"),
static_cast<int>(p_dev->hh_poll_thread_id));
}
}
for (unsigned i = 0; i < BTIF_HH_MAX_ADDED_DEV; i++) {
const btif_hh_added_device_t* p_dev = &btif_hh_cb.added_devices[i];
if (p_dev->bd_addr != RawAddress::kEmpty) {
LOG_DUMPSYS(fd, " %u: addr:%s", i, PRIVATE_ADDRESS(p_dev->bd_addr));
}
}
}
#undef DUMPSYS_TAG
#define DUMPSYS_TAG "shim::legacy::btm"
void DumpsysBtm(int fd) {
LOG_DUMPSYS_TITLE(fd, DUMPSYS_TAG);
if (btm_cb.history_ != nullptr) {
std::vector<Record> history = btm_cb.history_->Pull();
for (auto& record : history) {
time_t then = record.timestamp / 1000;
struct tm tm;
localtime_r(&then, &tm);
auto s2 = common::StringFormatTime(kTimeFormat, tm);
LOG_DUMPSYS(fd, " %s.%03u %s", s2.c_str(),
static_cast<unsigned int>(record.timestamp % 1000),
record.entry.c_str());
}
}
}
#undef DUMPSYS_TAG
#define DUMPSYS_TAG "shim::legacy::record"
void DumpsysRecord(int fd) {
LOG_DUMPSYS_TITLE(fd, DUMPSYS_TAG);
if (btm_cb.sec_dev_rec == nullptr) {
LOG_DUMPSYS(fd, "Record is empty - no devices");
return;
}
unsigned cnt = 0;
list_node_t* end = list_end(btm_cb.sec_dev_rec);
for (list_node_t* node = list_begin(btm_cb.sec_dev_rec); node != end;
node = list_next(node)) {
tBTM_SEC_DEV_REC* p_dev_rec =
static_cast<tBTM_SEC_DEV_REC*>(list_node(node));
LOG_DUMPSYS(fd, "%03u %s", ++cnt, p_dev_rec->ToString().c_str());
}
}
#undef DUMPSYS_TAG
void shim::legacy::Acl::Dump(int fd) const {
PAN_Dumpsys(fd);
DumpsysHid(fd);
DumpsysRecord(fd);
DumpsysAcl(fd);
DumpsysL2cap(fd);
DumpsysBtm(fd);
}
shim::legacy::Acl::Acl(os::Handler* handler,
const acl_interface_t& acl_interface,
uint8_t max_acceptlist_size,
uint8_t max_address_resolution_size)
: handler_(handler), acl_interface_(acl_interface) {
ASSERT(handler_ != nullptr);
ValidateAclInterface(acl_interface_);
pimpl_ = std::make_unique<Acl::impl>(max_acceptlist_size,
max_address_resolution_size);
GetAclManager()->RegisterCallbacks(this, handler_);
GetAclManager()->RegisterLeCallbacks(this, handler_);
GetController()->RegisterCompletedMonitorAclPacketsCallback(
handler->BindOn(this, &Acl::on_incoming_acl_credits));
shim::RegisterDumpsysFunction(static_cast<void*>(this),
[this](int fd) { Dump(fd); });
GetAclManager()->HACK_SetNonAclDisconnectCallback(
[this](uint16_t handle, uint8_t reason) {
TRY_POSTING_ON_MAIN(acl_interface_.connection.sco.on_disconnected,
handle, static_cast<tHCI_REASON>(reason));
// HACKCEPTION! LE ISO connections, just like SCO are not registered in
// GD, so ISO can use same hack to get notified about disconnections
TRY_POSTING_ON_MAIN(acl_interface_.connection.le.on_iso_disconnected,
handle, static_cast<tHCI_REASON>(reason));
});
}
shim::legacy::Acl::~Acl() {
shim::UnregisterDumpsysFunction(static_cast<void*>(this));
GetController()->UnregisterCompletedMonitorAclPacketsCallback();
if (CheckForOrphanedAclConnections()) {
pimpl_->DumpConnectionHistory();
}
}
bool shim::legacy::Acl::CheckForOrphanedAclConnections() const {
bool orphaned_acl_connections = false;
if (!pimpl_->handle_to_classic_connection_map_.empty()) {
LOG_ERROR("About to destroy classic active ACL");
for (const auto& connection : pimpl_->handle_to_classic_connection_map_) {
LOG_ERROR(" Orphaned classic ACL handle:0x%04x bd_addr:%s created:%s",
connection.second->Handle(),
PRIVATE_ADDRESS(connection.second->GetRemoteAddress()),
common::StringFormatTimeWithMilliseconds(
kConnectionDescriptorTimeFormat,
connection.second->GetCreationTime())
.c_str());
}
orphaned_acl_connections = true;
}
if (!pimpl_->handle_to_le_connection_map_.empty()) {
LOG_ERROR("About to destroy le active ACL");
for (const auto& connection : pimpl_->handle_to_le_connection_map_) {
LOG_ERROR(" Orphaned le ACL handle:0x%04x bd_addr:%s created:%s",
connection.second->Handle(),
PRIVATE_ADDRESS(connection.second->GetRemoteAddressWithType()),
common::StringFormatTimeWithMilliseconds(
kConnectionDescriptorTimeFormat,
connection.second->GetCreationTime())
.c_str());
}
orphaned_acl_connections = true;
}
return orphaned_acl_connections;
}
void shim::legacy::Acl::on_incoming_acl_credits(uint16_t handle,
uint16_t credits) {
TRY_POSTING_ON_MAIN(acl_interface_.on_packets_completed, handle, credits);
}
void shim::legacy::Acl::write_data_sync(
HciHandle handle, std::unique_ptr<packet::RawBuilder> packet) {
if (pimpl_->IsClassicAcl(handle)) {
pimpl_->EnqueueClassicPacket(handle, std::move(packet));
} else if (pimpl_->IsLeAcl(handle)) {
pimpl_->EnqueueLePacket(handle, std::move(packet));
} else {
LOG_ERROR("Unable to find destination to write data\n");
}
}
void shim::legacy::Acl::WriteData(HciHandle handle,
std::unique_ptr<packet::RawBuilder> packet) {
handler_->Post(common::BindOnce(&Acl::write_data_sync,
common::Unretained(this), handle,
std::move(packet)));
}
void shim::legacy::Acl::CreateClassicConnection(const hci::Address& address) {
GetAclManager()->CreateConnection(address);
LOG_DEBUG("Connection initiated for classic to remote:%s",
PRIVATE_ADDRESS(address));
BTM_LogHistory(kBtmLogTag, ToRawAddress(address), "Initiated connection",
"classic");
}
void shim::legacy::Acl::CancelClassicConnection(const hci::Address& address) {
GetAclManager()->CancelConnect(address);
LOG_DEBUG("Connection cancelled for classic to remote:%s",
PRIVATE_ADDRESS(address));
BTM_LogHistory(kBtmLogTag, ToRawAddress(address), "Cancelled connection",
"classic");
}
void shim::legacy::Acl::AcceptLeConnectionFrom(
const hci::AddressWithType& address_with_type, bool is_direct,
std::promise<bool> promise) {
LOG_DEBUG("AcceptLeConnectionFrom %s",
PRIVATE_ADDRESS(address_with_type.GetAddress()));
handler_->CallOn(pimpl_.get(), &Acl::impl::accept_le_connection_from,
address_with_type, is_direct, std::move(promise));
}
void shim::legacy::Acl::IgnoreLeConnectionFrom(
const hci::AddressWithType& address_with_type) {
LOG_DEBUG("IgnoreLeConnectionFrom %s",
PRIVATE_ADDRESS(address_with_type.GetAddress()));
handler_->CallOn(pimpl_.get(), &Acl::impl::ignore_le_connection_from,
address_with_type);
}
void shim::legacy::Acl::OnClassicLinkDisconnected(HciHandle handle,
hci::ErrorCode reason) {
hci::Address remote_address =
pimpl_->handle_to_classic_connection_map_[handle]->GetRemoteAddress();
CreationTime creation_time =
pimpl_->handle_to_classic_connection_map_[handle]->GetCreationTime();
bool is_locally_initiated =
pimpl_->handle_to_classic_connection_map_[handle]->IsLocallyInitiated();
TeardownTime teardown_time = std::chrono::system_clock::now();
pimpl_->handle_to_classic_connection_map_.erase(handle);
TRY_POSTING_ON_MAIN(acl_interface_.connection.classic.on_disconnected,
ToLegacyHciErrorCode(hci::ErrorCode::SUCCESS), handle,
ToLegacyHciErrorCode(reason));
LOG_DEBUG("Disconnected classic link remote:%s handle:%hu reason:%s",
PRIVATE_ADDRESS(remote_address), handle,
ErrorCodeText(reason).c_str());
BTM_LogHistory(
kBtmLogTag, ToRawAddress(remote_address), "Disconnected",
base::StringPrintf("classic reason:%s", ErrorCodeText(reason).c_str()));
pimpl_->connection_history_.Push(
std::move(std::make_unique<ClassicConnectionDescriptor>(
remote_address, creation_time, teardown_time, handle,
is_locally_initiated, reason)));
}
bluetooth::hci::AddressWithType shim::legacy::Acl::GetConnectionLocalAddress(
const RawAddress& remote_bda) {
bluetooth::hci::AddressWithType address_with_type;
auto remote_address = ToGdAddress(remote_bda);
for (auto& [handle, connection] : pimpl_->handle_to_le_connection_map_) {
if (connection->GetRemoteAddressWithType().GetAddress() == remote_address) {
return connection->GetLocalAddressWithType();
}
}
LOG_WARN("address not found!");
return address_with_type;
}
void shim::legacy::Acl::OnLeLinkDisconnected(HciHandle handle,
hci::ErrorCode reason) {
hci::AddressWithType remote_address_with_type =
pimpl_->handle_to_le_connection_map_[handle]->GetRemoteAddressWithType();
CreationTime creation_time =
pimpl_->handle_to_le_connection_map_[handle]->GetCreationTime();
bool is_locally_initiated =
pimpl_->handle_to_le_connection_map_[handle]->IsLocallyInitiated();
TeardownTime teardown_time = std::chrono::system_clock::now();
pimpl_->handle_to_le_connection_map_.erase(handle);
TRY_POSTING_ON_MAIN(acl_interface_.connection.le.on_disconnected,
ToLegacyHciErrorCode(hci::ErrorCode::SUCCESS), handle,
ToLegacyHciErrorCode(reason));
LOG_DEBUG("Disconnected le link remote:%s handle:%hu reason:%s",
PRIVATE_ADDRESS(remote_address_with_type), handle,
ErrorCodeText(reason).c_str());
BTM_LogHistory(
kBtmLogTag, ToLegacyAddressWithType(remote_address_with_type),
"Disconnected",
base::StringPrintf("Le reason:%s", ErrorCodeText(reason).c_str()));
pimpl_->connection_history_.Push(
std::move(std::make_unique<LeConnectionDescriptor>(
remote_address_with_type, creation_time, teardown_time, handle,
is_locally_initiated, reason)));
}
void shim::legacy::Acl::OnConnectSuccess(
std::unique_ptr<hci::acl_manager::ClassicAclConnection> connection) {
ASSERT(connection != nullptr);
auto handle = connection->GetHandle();
bool locally_initiated = connection->locally_initiated_;
const hci::Address remote_address = connection->GetAddress();
const RawAddress bd_addr = ToRawAddress(remote_address);
pimpl_->handle_to_classic_connection_map_.emplace(
handle, std::make_unique<ClassicShimAclConnection>(
acl_interface_.on_send_data_upwards,
std::bind(&shim::legacy::Acl::OnClassicLinkDisconnected, this,
std::placeholders::_1, std::placeholders::_2),
acl_interface_.link.classic, handler_, std::move(connection),
std::chrono::system_clock::now()));
pimpl_->handle_to_classic_connection_map_[handle]->RegisterCallbacks();
pimpl_->handle_to_classic_connection_map_[handle]
->ReadRemoteControllerInformation();
TRY_POSTING_ON_MAIN(acl_interface_.connection.classic.on_connected, bd_addr,
handle, false);
LOG_DEBUG("Connection successful classic remote:%s handle:%hu initiator:%s",
PRIVATE_ADDRESS(remote_address), handle,
(locally_initiated) ? "local" : "remote");
BTM_LogHistory(kBtmLogTag, ToRawAddress(remote_address),
"Connection successful",
(locally_initiated) ? "classic Local initiated"
: "classic Remote initiated");
}
void shim::legacy::Acl::OnConnectFail(hci::Address address,
hci::ErrorCode reason) {
const RawAddress bd_addr = ToRawAddress(address);
TRY_POSTING_ON_MAIN(acl_interface_.connection.classic.on_failed, bd_addr,
ToLegacyHciErrorCode(reason));
LOG_WARN("Connection failed classic remote:%s reason:%s",
PRIVATE_ADDRESS(address), hci::ErrorCodeText(reason).c_str());
BTM_LogHistory(kBtmLogTag, ToRawAddress(address), "Connection failed",
base::StringPrintf("classic reason:%s",
hci::ErrorCodeText(reason).c_str()));
}
void shim::legacy::Acl::HACK_OnEscoConnectRequest(hci::Address address,
hci::ClassOfDevice cod) {
const RawAddress bd_addr = ToRawAddress(address);
types::ClassOfDevice legacy_cod;
types::ClassOfDevice::FromString(cod.ToLegacyConfigString(), legacy_cod);
TRY_POSTING_ON_MAIN(acl_interface_.connection.sco.on_esco_connect_request,
bd_addr, legacy_cod);
LOG_DEBUG("Received ESCO connect request remote:%s",
PRIVATE_ADDRESS(address));
BTM_LogHistory(kBtmLogTag, ToRawAddress(address), "ESCO Connection request");
}
void shim::legacy::Acl::HACK_OnScoConnectRequest(hci::Address address,
hci::ClassOfDevice cod) {
const RawAddress bd_addr = ToRawAddress(address);
types::ClassOfDevice legacy_cod;
types::ClassOfDevice::FromString(cod.ToLegacyConfigString(), legacy_cod);
TRY_POSTING_ON_MAIN(acl_interface_.connection.sco.on_sco_connect_request,
bd_addr, legacy_cod);
LOG_DEBUG("Received SCO connect request remote:%s", PRIVATE_ADDRESS(address));
BTM_LogHistory(kBtmLogTag, ToRawAddress(address), "SCO Connection request");
}
void shim::legacy::Acl::OnLeConnectSuccess(
hci::AddressWithType address_with_type,
std::unique_ptr<hci::acl_manager::LeAclConnection> connection) {
ASSERT(connection != nullptr);
auto handle = connection->GetHandle();
// Save the peer address, if any
hci::AddressWithType peer_address_with_type =
connection->peer_address_with_type_;
hci::Role connection_role = connection->GetRole();
bool locally_initiated = connection->locally_initiated_;
uint16_t conn_interval = connection->interval_;
uint16_t conn_latency = connection->latency_;
uint16_t conn_timeout = connection->supervision_timeout_;
RawAddress local_rpa =
ToRawAddress(connection->local_resolvable_private_address_);
RawAddress peer_rpa =
ToRawAddress(connection->peer_resolvable_private_address_);
tBLE_ADDR_TYPE peer_addr_type =
(tBLE_ADDR_TYPE)connection->peer_address_with_type_.GetAddressType();
pimpl_->handle_to_le_connection_map_.emplace(
handle, std::make_unique<LeShimAclConnection>(
acl_interface_.on_send_data_upwards,
std::bind(&shim::legacy::Acl::OnLeLinkDisconnected, this,
std::placeholders::_1, std::placeholders::_2),
acl_interface_.link.le, handler_, std::move(connection),
std::chrono::system_clock::now()));
pimpl_->handle_to_le_connection_map_[handle]->RegisterCallbacks();
// Once an le connection has successfully been established
// the device address is removed from the controller accept list.
if (IsRpa(address_with_type)) {
LOG_DEBUG("Connection address is rpa:%s identity_addr:%s",
PRIVATE_ADDRESS(address_with_type),
PRIVATE_ADDRESS(peer_address_with_type));
pimpl_->shadow_acceptlist_.Remove(peer_address_with_type);
} else {
LOG_DEBUG("Connection address is not rpa addr:%s",
PRIVATE_ADDRESS(address_with_type));
pimpl_->shadow_acceptlist_.Remove(address_with_type);
}
if (!pimpl_->handle_to_le_connection_map_[handle]->IsInFilterAcceptList() &&
connection_role == hci::Role::CENTRAL) {
pimpl_->handle_to_le_connection_map_[handle]->InitiateDisconnect(
hci::DisconnectReason::REMOTE_USER_TERMINATED_CONNECTION);
LOG_INFO("Disconnected ACL after connection canceled");
BTM_LogHistory(kBtmLogTag, ToLegacyAddressWithType(address_with_type),
"Connection canceled", "Le");
return;
}
pimpl_->handle_to_le_connection_map_[handle]
->ReadRemoteControllerInformation();
tBLE_BD_ADDR legacy_address_with_type =
ToLegacyAddressWithType(address_with_type);
TRY_POSTING_ON_MAIN(
acl_interface_.connection.le.on_connected, legacy_address_with_type,
handle, ToLegacyRole(connection_role), conn_interval, conn_latency,
conn_timeout, local_rpa, peer_rpa, peer_addr_type);
LOG_DEBUG("Connection successful le remote:%s handle:%hu initiator:%s",
PRIVATE_ADDRESS(address_with_type), handle,
(locally_initiated) ? "local" : "remote");
BTM_LogHistory(kBtmLogTag, ToLegacyAddressWithType(address_with_type),
"Connection successful", "Le");
}
void shim::legacy::Acl::OnLeConnectFail(hci::AddressWithType address_with_type,
hci::ErrorCode reason) {
tBLE_BD_ADDR legacy_address_with_type =
ToLegacyAddressWithType(address_with_type);
uint16_t handle = 0; /* TODO Unneeded */
bool enhanced = true; /* TODO logging metrics only */
tHCI_STATUS status = ToLegacyHciErrorCode(reason);
pimpl_->shadow_acceptlist_.Remove(address_with_type);
TRY_POSTING_ON_MAIN(acl_interface_.connection.le.on_failed,
legacy_address_with_type, handle, enhanced, status);
LOG_WARN("Connection failed le remote:%s",
PRIVATE_ADDRESS(address_with_type));
BTM_LogHistory(
kBtmLogTag, ToLegacyAddressWithType(address_with_type),
"Connection failed",
base::StringPrintf("le reason:%s", hci::ErrorCodeText(reason).c_str()));
}
void shim::legacy::Acl::DisconnectClassic(uint16_t handle, tHCI_STATUS reason,
std::string comment) {
handler_->CallOn(pimpl_.get(), &Acl::impl::disconnect_classic, handle, reason,
comment);
}
void shim::legacy::Acl::DisconnectLe(uint16_t handle, tHCI_STATUS reason,
std::string comment) {
handler_->CallOn(pimpl_.get(), &Acl::impl::disconnect_le, handle, reason,
comment);
}
bool shim::legacy::Acl::HoldMode(uint16_t hci_handle, uint16_t max_interval,
uint16_t min_interval) {
handler_->CallOn(pimpl_.get(), &Acl::impl::HoldMode, hci_handle, max_interval,
min_interval);
return false; // TODO void
}
bool shim::legacy::Acl::SniffMode(uint16_t hci_handle, uint16_t max_interval,
uint16_t min_interval, uint16_t attempt,
uint16_t timeout) {
handler_->CallOn(pimpl_.get(), &Acl::impl::SniffMode, hci_handle,
max_interval, min_interval, attempt, timeout);
return false;
}
bool shim::legacy::Acl::ExitSniffMode(uint16_t hci_handle) {
handler_->CallOn(pimpl_.get(), &Acl::impl::ExitSniffMode, hci_handle);
return false;
}
bool shim::legacy::Acl::SniffSubrating(uint16_t hci_handle,
uint16_t maximum_latency,
uint16_t minimum_remote_timeout,
uint16_t minimum_local_timeout) {
handler_->CallOn(pimpl_.get(), &Acl::impl::SniffSubrating, hci_handle,
maximum_latency, minimum_remote_timeout,
minimum_local_timeout);
return false;
}
void shim::legacy::Acl::DumpConnectionHistory(int fd) const {
pimpl_->DumpConnectionHistory(fd);
}
void shim::legacy::Acl::Shutdown() {
if (CheckForOrphanedAclConnections()) {
std::promise<void> shutdown_promise;
auto shutdown_future = shutdown_promise.get_future();
handler_->CallOn(pimpl_.get(), &Acl::impl::ShutdownClassicConnections,
std::move(shutdown_promise));
shutdown_future.wait();
shutdown_promise = std::promise<void>();
shutdown_future = shutdown_promise.get_future();
handler_->CallOn(pimpl_.get(), &Acl::impl::ShutdownLeConnections,
std::move(shutdown_promise));
shutdown_future.wait();
LOG_WARN("Flushed open ACL connections");
} else {
LOG_INFO("All ACL connections have been previously closed");
}
}
void shim::legacy::Acl::FinalShutdown() {
std::promise<void> promise;
auto future = promise.get_future();
GetAclManager()->UnregisterCallbacks(this, std::move(promise));
future.wait();
LOG_DEBUG("Unregistered classic callbacks from gd acl manager");
promise = std::promise<void>();
future = promise.get_future();
GetAclManager()->UnregisterLeCallbacks(this, std::move(promise));
future.wait();
LOG_DEBUG("Unregistered le callbacks from gd acl manager");
promise = std::promise<void>();
future = promise.get_future();
handler_->CallOn(pimpl_.get(), &Acl::impl::FinalShutdown, std::move(promise));
future.wait();
LOG_INFO("Unregistered and cleared any orphaned ACL connections");
}
void shim::legacy::Acl::ClearAcceptList() {
handler_->CallOn(pimpl_.get(), &Acl::impl::clear_acceptlist);
}
void shim::legacy::Acl::AddToAddressResolution(
const hci::AddressWithType& address_with_type,
const std::array<uint8_t, 16>& peer_irk,
const std::array<uint8_t, 16>& local_irk) {
handler_->CallOn(pimpl_.get(), &Acl::impl::AddToAddressResolution,
address_with_type, peer_irk, local_irk);
}
void shim::legacy::Acl::RemoveFromAddressResolution(
const hci::AddressWithType& address_with_type) {
handler_->CallOn(pimpl_.get(), &Acl::impl::RemoveFromAddressResolution,
address_with_type);
}
void shim::legacy::Acl::ClearAddressResolution() {
handler_->CallOn(pimpl_.get(), &Acl::impl::ClearResolvingList);
}
|