• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "main/shim/acl.h"
18 
19 #include <base/location.h>
20 #include <base/strings/stringprintf.h>
21 #include <time.h>
22 #include <unordered_set>
23 
24 #include <chrono>
25 #include <cstdint>
26 #include <functional>
27 #include <future>
28 #include <map>
29 #include <memory>
30 #include <string>
31 
32 #include "btif/include/btif_hh.h"
33 #include "device/include/controller.h"
34 #include "gd/common/bidi_queue.h"
35 #include "gd/common/bind.h"
36 #include "gd/common/strings.h"
37 #include "gd/hci/acl_manager.h"
38 #include "gd/hci/acl_manager/acl_connection.h"
39 #include "gd/hci/acl_manager/classic_acl_connection.h"
40 #include "gd/hci/acl_manager/connection_management_callbacks.h"
41 #include "gd/hci/acl_manager/le_acl_connection.h"
42 #include "gd/hci/acl_manager/le_connection_management_callbacks.h"
43 #include "gd/hci/address.h"
44 #include "gd/hci/class_of_device.h"
45 #include "gd/hci/controller.h"
46 #include "gd/os/handler.h"
47 #include "gd/os/queue.h"
48 #include "main/shim/btm.h"
49 #include "main/shim/dumpsys.h"
50 #include "main/shim/entry.h"
51 #include "main/shim/helpers.h"
52 #include "main/shim/stack.h"
53 #include "stack/acl/acl.h"
54 #include "stack/btm/btm_int_types.h"
55 #include "stack/include/acl_hci_link_interface.h"
56 #include "stack/include/ble_acl_interface.h"
57 #include "stack/include/btm_api.h"
58 #include "stack/include/btm_status.h"
59 #include "stack/include/sec_hci_link_interface.h"
60 #include "stack/l2cap/l2c_int.h"
61 
62 extern tBTM_CB btm_cb;
63 
64 bt_status_t do_in_main_thread(const base::Location& from_here,
65                               base::OnceClosure task);
66 
67 using namespace bluetooth;
68 
69 namespace {
70 
71 using HciHandle = uint16_t;
72 using PageNumber = uint8_t;
73 
74 using CreationTime = std::chrono::time_point<std::chrono::system_clock>;
75 using TeardownTime = std::chrono::time_point<std::chrono::system_clock>;
76 
77 constexpr char kBtmLogTag[] = "ACL";
78 
79 using SendDataUpwards = void (*const)(BT_HDR*);
80 using OnDisconnect = std::function<void(HciHandle, hci::ErrorCode reason)>;
81 
82 constexpr char kConnectionDescriptorTimeFormat[] = "%Y-%m-%d %H:%M:%S";
83 
84 class ShadowAcceptlist {
85  public:
ShadowAcceptlist(uint8_t max_acceptlist_size)86   ShadowAcceptlist(uint8_t max_acceptlist_size)
87       : max_acceptlist_size_(max_acceptlist_size) {}
88 
Add(const hci::AddressWithType & address_with_type)89   bool Add(const hci::AddressWithType& address_with_type) {
90     if (acceptlist_set_.size() == max_acceptlist_size_) {
91       LOG_ERROR("Acceptlist is full size:%zu", acceptlist_set_.size());
92       return false;
93     }
94     if (!acceptlist_set_.insert(address_with_type).second) {
95       LOG_WARN("Attempted to add duplicate le address to acceptlist:%s",
96                PRIVATE_ADDRESS(address_with_type));
97     }
98     return true;
99   }
100 
Remove(const hci::AddressWithType & address_with_type)101   bool Remove(const hci::AddressWithType& address_with_type) {
102     auto iter = acceptlist_set_.find(address_with_type);
103     if (iter == acceptlist_set_.end()) {
104       LOG_WARN("Unknown device being removed from acceptlist:%s",
105                PRIVATE_ADDRESS(address_with_type));
106       return false;
107     }
108     acceptlist_set_.erase(iter);
109     return true;
110   }
111 
GetCopy() const112   std::unordered_set<hci::AddressWithType> GetCopy() const {
113     return acceptlist_set_;
114   }
115 
IsFull() const116   bool IsFull() const {
117     return acceptlist_set_.size() == static_cast<size_t>(max_acceptlist_size_);
118   }
119 
Clear()120   void Clear() { acceptlist_set_.clear(); }
121 
122  private:
123   uint8_t max_acceptlist_size_{0};
124   std::unordered_set<hci::AddressWithType> acceptlist_set_;
125 };
126 
127 struct ConnectionDescriptor {
128   CreationTime creation_time_;
129   TeardownTime teardown_time_;
130   uint16_t handle_;
131   bool is_locally_initiated_;
132   hci::ErrorCode disconnect_reason_;
ConnectionDescriptor__anon6e21abd70111::ConnectionDescriptor133   ConnectionDescriptor(CreationTime creation_time, TeardownTime teardown_time,
134                        uint16_t handle, bool is_locally_initiated,
135                        hci::ErrorCode disconnect_reason)
136       : creation_time_(creation_time),
137         teardown_time_(teardown_time),
138         handle_(handle),
139         is_locally_initiated_(is_locally_initiated),
140         disconnect_reason_(disconnect_reason) {}
141   virtual std::string GetPrivateRemoteAddress() const = 0;
~ConnectionDescriptor__anon6e21abd70111::ConnectionDescriptor142   virtual ~ConnectionDescriptor() {}
ToString__anon6e21abd70111::ConnectionDescriptor143   std::string ToString() const {
144     return base::StringPrintf(
145         "peer:%s handle:0x%04x is_locally_initiated:%s"
146         " creation_time:%s teardown_time:%s disconnect_reason:%s",
147         GetPrivateRemoteAddress().c_str(), handle_,
148         logbool(is_locally_initiated_).c_str(),
149         common::StringFormatTimeWithMilliseconds(
150             kConnectionDescriptorTimeFormat, creation_time_)
151             .c_str(),
152         common::StringFormatTimeWithMilliseconds(
153             kConnectionDescriptorTimeFormat, teardown_time_)
154             .c_str(),
155         hci::ErrorCodeText(disconnect_reason_).c_str());
156   }
157 };
158 
159 struct ClassicConnectionDescriptor : public ConnectionDescriptor {
160   const hci::Address remote_address_;
ClassicConnectionDescriptor__anon6e21abd70111::ClassicConnectionDescriptor161   ClassicConnectionDescriptor(const hci::Address& remote_address,
162                               CreationTime creation_time,
163                               TeardownTime teardown_time, uint16_t handle,
164                               bool is_locally_initiated,
165                               hci::ErrorCode disconnect_reason)
166       : ConnectionDescriptor(creation_time, teardown_time, handle,
167                              is_locally_initiated, disconnect_reason),
168         remote_address_(remote_address) {}
GetPrivateRemoteAddress__anon6e21abd70111::ClassicConnectionDescriptor169   virtual std::string GetPrivateRemoteAddress() const {
170     return PRIVATE_ADDRESS(remote_address_);
171   }
172 };
173 
174 struct LeConnectionDescriptor : public ConnectionDescriptor {
175   const hci::AddressWithType remote_address_with_type_;
LeConnectionDescriptor__anon6e21abd70111::LeConnectionDescriptor176   LeConnectionDescriptor(hci::AddressWithType& remote_address_with_type,
177                          CreationTime creation_time, TeardownTime teardown_time,
178                          uint16_t handle, bool is_locally_initiated,
179                          hci::ErrorCode disconnect_reason)
180       : ConnectionDescriptor(creation_time, teardown_time, handle,
181                              is_locally_initiated, disconnect_reason),
182         remote_address_with_type_(remote_address_with_type) {}
GetPrivateRemoteAddress__anon6e21abd70111::LeConnectionDescriptor183   std::string GetPrivateRemoteAddress() const {
184     return PRIVATE_ADDRESS(remote_address_with_type_);
185   }
186 };
187 
188 template <typename T>
189 class FixedQueue {
190  public:
FixedQueue(size_t max_size)191   explicit FixedQueue(size_t max_size) : max_size_(max_size) {}
Push(T element)192   void Push(T element) {
193     if (queue_.size() == max_size_) {
194       queue_.pop_front();
195     }
196     queue_.push_back(std::move(element));
197   }
198 
ReadElementsAsString() const199   std::vector<std::string> ReadElementsAsString() const {
200     std::vector<std::string> vector;
201     for (auto& entry : queue_) {
202       vector.push_back(entry->ToString());
203     }
204     return vector;
205   }
206 
207  private:
208   size_t max_size_{1};
209   std::deque<T> queue_;
210 };
211 
212 constexpr size_t kConnectionHistorySize = 40;
213 
LowByte(uint16_t val)214 inline uint8_t LowByte(uint16_t val) { return val & 0xff; }
HighByte(uint16_t val)215 inline uint8_t HighByte(uint16_t val) { return val >> 8; }
216 
ValidateAclInterface(const shim::legacy::acl_interface_t & acl_interface)217 void ValidateAclInterface(const shim::legacy::acl_interface_t& acl_interface) {
218   ASSERT_LOG(acl_interface.on_send_data_upwards != nullptr,
219              "Must provide to receive data on acl links");
220   ASSERT_LOG(acl_interface.on_packets_completed != nullptr,
221              "Must provide to receive completed packet indication");
222 
223   ASSERT_LOG(acl_interface.connection.classic.on_connected != nullptr,
224              "Must provide to respond to successful classic connections");
225   ASSERT_LOG(acl_interface.connection.classic.on_failed != nullptr,
226              "Must provide to respond when classic connection attempts fail");
227   ASSERT_LOG(
228       acl_interface.connection.classic.on_disconnected != nullptr,
229       "Must provide to respond when active classic connection disconnects");
230 
231   ASSERT_LOG(acl_interface.connection.le.on_connected != nullptr,
232              "Must provide to respond to successful le connections");
233   ASSERT_LOG(acl_interface.connection.le.on_failed != nullptr,
234              "Must provide to respond when le connection attempts fail");
235   ASSERT_LOG(acl_interface.connection.le.on_disconnected != nullptr,
236              "Must provide to respond when active le connection disconnects");
237 }
238 
239 }  // namespace
240 
241 #define TRY_POSTING_ON_MAIN(cb, ...)                               \
242   do {                                                             \
243     if (cb == nullptr) {                                           \
244       LOG_WARN("Dropping ACL event with no callback");             \
245     } else {                                                       \
246       do_in_main_thread(FROM_HERE, base::Bind(cb, ##__VA_ARGS__)); \
247     }                                                              \
248   } while (0)
249 
250 constexpr HciHandle kInvalidHciHandle = 0xffff;
251 
252 class ShimAclConnection {
253  public:
ShimAclConnection(const HciHandle handle,SendDataUpwards send_data_upwards,os::Handler * handler,hci::acl_manager::AclConnection::QueueUpEnd * queue_up_end,CreationTime creation_time)254   ShimAclConnection(const HciHandle handle, SendDataUpwards send_data_upwards,
255                     os::Handler* handler,
256                     hci::acl_manager::AclConnection::QueueUpEnd* queue_up_end,
257                     CreationTime creation_time)
258       : handle_(handle),
259         handler_(handler),
260         send_data_upwards_(send_data_upwards),
261         queue_up_end_(queue_up_end),
262         creation_time_(creation_time) {
263     queue_up_end_->RegisterDequeue(
264         handler_, common::Bind(&ShimAclConnection::data_ready_callback,
265                                common::Unretained(this)));
266   }
267 
~ShimAclConnection()268   virtual ~ShimAclConnection() {
269     if (!queue_.empty())
270       LOG_ERROR(
271           "ACL cleaned up with non-empty queue handle:0x%04x stranded_pkts:%zu",
272           handle_, queue_.size());
273     ASSERT_LOG(is_disconnected_,
274                "Shim Acl was not properly disconnected handle:0x%04x", handle_);
275   }
276 
EnqueuePacket(std::unique_ptr<packet::RawBuilder> packet)277   void EnqueuePacket(std::unique_ptr<packet::RawBuilder> packet) {
278     // TODO Handle queue size exceeds some threshold
279     queue_.push(std::move(packet));
280     RegisterEnqueue();
281   }
282 
handle_enqueue()283   std::unique_ptr<packet::BasePacketBuilder> handle_enqueue() {
284     auto packet = std::move(queue_.front());
285     queue_.pop();
286     if (queue_.empty()) {
287       UnregisterEnqueue();
288     }
289     return packet;
290   }
291 
data_ready_callback()292   void data_ready_callback() {
293     auto packet = queue_up_end_->TryDequeue();
294     uint16_t length = packet->size();
295     std::vector<uint8_t> preamble;
296     preamble.push_back(LowByte(handle_));
297     preamble.push_back(HighByte(handle_));
298     preamble.push_back(LowByte(length));
299     preamble.push_back(HighByte(length));
300     BT_HDR* p_buf = MakeLegacyBtHdrPacket(std::move(packet), preamble);
301     ASSERT_LOG(p_buf != nullptr,
302                "Unable to allocate BT_HDR legacy packet handle:%04x", handle_);
303     if (send_data_upwards_ == nullptr) {
304       LOG_WARN("Dropping ACL data with no callback");
305       osi_free(p_buf);
306     } else if (do_in_main_thread(FROM_HERE,
307                                  base::Bind(send_data_upwards_, p_buf)) !=
308                BT_STATUS_SUCCESS) {
309       osi_free(p_buf);
310     }
311   }
312 
313   virtual void InitiateDisconnect(hci::DisconnectReason reason) = 0;
314   virtual bool IsLocallyInitiated() const = 0;
315 
GetCreationTime() const316   CreationTime GetCreationTime() const { return creation_time_; }
Handle() const317   uint16_t Handle() const { return handle_; }
318 
Shutdown()319   void Shutdown() {
320     Disconnect();
321     LOG_INFO("Shutdown and disconnect ACL connection handle:0x%04x", handle_);
322   }
323 
324  protected:
325   const uint16_t handle_{kInvalidHciHandle};
326   os::Handler* handler_;
327 
UnregisterEnqueue()328   void UnregisterEnqueue() {
329     if (!is_enqueue_registered_) return;
330     is_enqueue_registered_ = false;
331     queue_up_end_->UnregisterEnqueue();
332   }
333 
Disconnect()334   void Disconnect() {
335     ASSERT_LOG(!is_disconnected_,
336                "Cannot disconnect ACL multiple times handle:%04x", handle_);
337     is_disconnected_ = true;
338     UnregisterEnqueue();
339     queue_up_end_->UnregisterDequeue();
340     if (!queue_.empty())
341       LOG_WARN(
342           "ACL disconnect with non-empty queue handle:%04x stranded_pkts::%zu",
343           handle_, queue_.size());
344   }
345 
346   virtual void ReadRemoteControllerInformation() = 0;
347 
348  private:
349   SendDataUpwards send_data_upwards_;
350   hci::acl_manager::AclConnection::QueueUpEnd* queue_up_end_;
351 
352   std::queue<std::unique_ptr<packet::RawBuilder>> queue_;
353   bool is_enqueue_registered_{false};
354   bool is_disconnected_{false};
355   CreationTime creation_time_;
356 
RegisterEnqueue()357   void RegisterEnqueue() {
358     ASSERT_LOG(!is_disconnected_,
359                "Unable to send data over disconnected channel handle:%04x",
360                handle_);
361     if (is_enqueue_registered_) return;
362     is_enqueue_registered_ = true;
363     queue_up_end_->RegisterEnqueue(
364         handler_, common::Bind(&ShimAclConnection::handle_enqueue,
365                                common::Unretained(this)));
366   }
367 
368   virtual void RegisterCallbacks() = 0;
369 };
370 
371 class ClassicShimAclConnection
372     : public ShimAclConnection,
373       public hci::acl_manager::ConnectionManagementCallbacks {
374  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)375   ClassicShimAclConnection(
376       SendDataUpwards send_data_upwards, OnDisconnect on_disconnect,
377       const shim::legacy::acl_classic_link_interface_t& interface,
378       os::Handler* handler,
379       std::unique_ptr<hci::acl_manager::ClassicAclConnection> connection,
380       CreationTime creation_time)
381       : ShimAclConnection(connection->GetHandle(), send_data_upwards, handler,
382                           connection->GetAclQueueEnd(), creation_time),
383         on_disconnect_(on_disconnect),
384         interface_(interface),
385         connection_(std::move(connection)) {}
386 
RegisterCallbacks()387   void RegisterCallbacks() override {
388     connection_->RegisterCallbacks(this, handler_);
389   }
390 
ReadRemoteControllerInformation()391   void ReadRemoteControllerInformation() override {
392     connection_->ReadRemoteVersionInformation();
393     connection_->ReadRemoteSupportedFeatures();
394   }
395 
OnConnectionPacketTypeChanged(uint16_t packet_type)396   void OnConnectionPacketTypeChanged(uint16_t packet_type) override {
397     TRY_POSTING_ON_MAIN(interface_.on_packet_type_changed, packet_type);
398   }
399 
OnAuthenticationComplete(hci::ErrorCode hci_status)400   void OnAuthenticationComplete(hci::ErrorCode hci_status) override {
401     TRY_POSTING_ON_MAIN(interface_.on_authentication_complete, handle_,
402                         ToLegacyHciErrorCode(hci_status));
403   }
404 
OnEncryptionChange(hci::EncryptionEnabled enabled)405   void OnEncryptionChange(hci::EncryptionEnabled enabled) override {
406     bool is_enabled = (enabled == hci::EncryptionEnabled::ON ||
407                        enabled == hci::EncryptionEnabled::BR_EDR_AES_CCM);
408     TRY_POSTING_ON_MAIN(interface_.on_encryption_change, is_enabled);
409   }
410 
OnChangeConnectionLinkKeyComplete()411   void OnChangeConnectionLinkKeyComplete() override {
412     TRY_POSTING_ON_MAIN(interface_.on_change_connection_link_key_complete);
413   }
414 
OnReadClockOffsetComplete(uint16_t clock_offset)415   void OnReadClockOffsetComplete(uint16_t clock_offset) override {
416     LOG_INFO("UNIMPLEMENTED");
417   }
418 
OnModeChange(hci::ErrorCode status,hci::Mode current_mode,uint16_t interval)419   void OnModeChange(hci::ErrorCode status, hci::Mode current_mode,
420                     uint16_t interval) override {
421     TRY_POSTING_ON_MAIN(interface_.on_mode_change, ToLegacyHciErrorCode(status),
422                         handle_, ToLegacyHciMode(current_mode), interval);
423   }
424 
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)425   void OnSniffSubrating(hci::ErrorCode hci_status,
426                         uint16_t maximum_transmit_latency,
427                         uint16_t maximum_receive_latency,
428                         uint16_t minimum_remote_timeout,
429                         uint16_t minimum_local_timeout) {
430     TRY_POSTING_ON_MAIN(interface_.on_sniff_subrating,
431                         ToLegacyHciErrorCode(hci_status), handle_,
432                         maximum_transmit_latency, maximum_receive_latency,
433                         minimum_remote_timeout, minimum_local_timeout);
434   }
435 
OnQosSetupComplete(hci::ServiceType service_type,uint32_t token_rate,uint32_t peak_bandwidth,uint32_t latency,uint32_t delay_variation)436   void OnQosSetupComplete(hci::ServiceType service_type, uint32_t token_rate,
437                           uint32_t peak_bandwidth, uint32_t latency,
438                           uint32_t delay_variation) override {
439     LOG_INFO("UNIMPLEMENTED");
440   }
441 
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)442   void OnFlowSpecificationComplete(hci::FlowDirection flow_direction,
443                                    hci::ServiceType service_type,
444                                    uint32_t token_rate,
445                                    uint32_t token_bucket_size,
446                                    uint32_t peak_bandwidth,
447                                    uint32_t access_latency) override {
448     LOG_INFO("UNIMPLEMENTED");
449   }
450 
OnFlushOccurred()451   void OnFlushOccurred() override { LOG_INFO("UNIMPLEMENTED"); }
452 
OnRoleDiscoveryComplete(hci::Role current_role)453   void OnRoleDiscoveryComplete(hci::Role current_role) override {
454     LOG_INFO("UNIMPLEMENTED");
455   }
456 
OnReadLinkPolicySettingsComplete(uint16_t link_policy_settings)457   void OnReadLinkPolicySettingsComplete(
458       uint16_t link_policy_settings) override {
459     LOG_INFO("UNIMPLEMENTED");
460   }
461 
OnReadAutomaticFlushTimeoutComplete(uint16_t flush_timeout)462   void OnReadAutomaticFlushTimeoutComplete(uint16_t flush_timeout) override {
463     LOG_INFO("UNIMPLEMENTED");
464   }
465 
OnReadTransmitPowerLevelComplete(uint8_t transmit_power_level)466   void OnReadTransmitPowerLevelComplete(uint8_t transmit_power_level) override {
467     LOG_INFO("UNIMPLEMENTED");
468   }
469 
OnReadLinkSupervisionTimeoutComplete(uint16_t link_supervision_timeout)470   void OnReadLinkSupervisionTimeoutComplete(
471       uint16_t link_supervision_timeout) override {
472     LOG_INFO("UNIMPLEMENTED");
473   }
474 
OnReadFailedContactCounterComplete(uint16_t failed_contact_counter)475   void OnReadFailedContactCounterComplete(
476       uint16_t failed_contact_counter) override {
477     LOG_INFO("UNIMPLEMENTED");
478   }
479 
OnReadLinkQualityComplete(uint8_t link_quality)480   void OnReadLinkQualityComplete(uint8_t link_quality) override {
481     LOG_INFO("UNIMPLEMENTED");
482   }
483 
OnReadAfhChannelMapComplete(hci::AfhMode afh_mode,std::array<uint8_t,10> afh_channel_map)484   void OnReadAfhChannelMapComplete(
485       hci::AfhMode afh_mode, std::array<uint8_t, 10> afh_channel_map) override {
486     LOG_INFO("UNIMPLEMENTED");
487   }
488 
OnReadRssiComplete(uint8_t rssi)489   void OnReadRssiComplete(uint8_t rssi) override { LOG_INFO("UNIMPLEMENTED"); }
490 
OnReadClockComplete(uint32_t clock,uint16_t accuracy)491   void OnReadClockComplete(uint32_t clock, uint16_t accuracy) override {
492     LOG_INFO("UNIMPLEMENTED");
493   }
494 
OnCentralLinkKeyComplete(hci::KeyFlag key_flag)495   void OnCentralLinkKeyComplete(hci::KeyFlag key_flag) override {
496     LOG_INFO("%s UNIMPLEMENTED", __func__);
497   }
498 
OnRoleChange(hci::ErrorCode hci_status,hci::Role new_role)499   void OnRoleChange(hci::ErrorCode hci_status, hci::Role new_role) override {
500     TRY_POSTING_ON_MAIN(
501         interface_.on_role_change, ToLegacyHciErrorCode(hci_status),
502         ToRawAddress(connection_->GetAddress()), ToLegacyRole(new_role));
503     BTM_LogHistory(kBtmLogTag, ToRawAddress(connection_->GetAddress()),
504                    "Role change",
505                    base::StringPrintf("classic  status:%s new_role:%s",
506                                       hci::ErrorCodeText(hci_status).c_str(),
507                                       hci::RoleText(new_role).c_str()));
508   }
509 
OnDisconnection(hci::ErrorCode reason)510   void OnDisconnection(hci::ErrorCode reason) override {
511     Disconnect();
512     on_disconnect_(handle_, reason);
513   }
514 
OnReadRemoteVersionInformationComplete(hci::ErrorCode hci_status,uint8_t lmp_version,uint16_t manufacturer_name,uint16_t sub_version)515   void OnReadRemoteVersionInformationComplete(hci::ErrorCode hci_status,
516                                               uint8_t lmp_version,
517                                               uint16_t manufacturer_name,
518                                               uint16_t sub_version) override {
519     TRY_POSTING_ON_MAIN(interface_.on_read_remote_version_information_complete,
520                         ToLegacyHciErrorCode(hci_status), handle_, lmp_version,
521                         manufacturer_name, sub_version);
522   }
523 
OnReadRemoteSupportedFeaturesComplete(uint64_t features)524   void OnReadRemoteSupportedFeaturesComplete(uint64_t features) override {
525     TRY_POSTING_ON_MAIN(interface_.on_read_remote_supported_features_complete,
526                         handle_, features);
527 
528     if (features & ((uint64_t(1) << 63))) {
529       connection_->ReadRemoteExtendedFeatures(1);
530       return;
531     }
532     LOG_DEBUG("Device does not support extended features");
533   }
534 
OnReadRemoteExtendedFeaturesComplete(uint8_t page_number,uint8_t max_page_number,uint64_t features)535   void OnReadRemoteExtendedFeaturesComplete(uint8_t page_number,
536                                             uint8_t max_page_number,
537                                             uint64_t features) override {
538     TRY_POSTING_ON_MAIN(interface_.on_read_remote_extended_features_complete,
539                         handle_, page_number, max_page_number, features);
540 
541     // Supported features aliases to extended features page 0
542     if (page_number == 0 && !(features & ((uint64_t(1) << 63)))) {
543       LOG_DEBUG("Device does not support extended features");
544       return;
545     }
546 
547     if (page_number != max_page_number)
548       connection_->ReadRemoteExtendedFeatures(page_number + 1);
549   }
550 
GetRemoteAddress() const551   hci::Address GetRemoteAddress() const { return connection_->GetAddress(); }
552 
InitiateDisconnect(hci::DisconnectReason reason)553   void InitiateDisconnect(hci::DisconnectReason reason) override {
554     connection_->Disconnect(reason);
555   }
556 
HoldMode(uint16_t max_interval,uint16_t min_interval)557   void HoldMode(uint16_t max_interval, uint16_t min_interval) {
558     ASSERT(connection_->HoldMode(max_interval, min_interval));
559   }
560 
SniffMode(uint16_t max_interval,uint16_t min_interval,uint16_t attempt,uint16_t timeout)561   void SniffMode(uint16_t max_interval, uint16_t min_interval, uint16_t attempt,
562                  uint16_t timeout) {
563     ASSERT(
564         connection_->SniffMode(max_interval, min_interval, attempt, timeout));
565   }
566 
ExitSniffMode()567   void ExitSniffMode() { ASSERT(connection_->ExitSniffMode()); }
568 
SniffSubrating(uint16_t maximum_latency,uint16_t minimum_remote_timeout,uint16_t minimum_local_timeout)569   void SniffSubrating(uint16_t maximum_latency, uint16_t minimum_remote_timeout,
570                       uint16_t minimum_local_timeout) {
571     ASSERT(connection_->SniffSubrating(maximum_latency, minimum_remote_timeout,
572                                        minimum_local_timeout));
573   }
574 
SetConnectionEncryption(hci::Enable is_encryption_enabled)575   void SetConnectionEncryption(hci::Enable is_encryption_enabled) {
576     ASSERT(connection_->SetConnectionEncryption(is_encryption_enabled));
577   }
578 
IsLocallyInitiated() const579   bool IsLocallyInitiated() const override {
580     return connection_->locally_initiated_;
581   }
582 
583  private:
584   OnDisconnect on_disconnect_;
585   const shim::legacy::acl_classic_link_interface_t interface_;
586   std::unique_ptr<hci::acl_manager::ClassicAclConnection> connection_;
587 };
588 
589 class LeShimAclConnection
590     : public ShimAclConnection,
591       public hci::acl_manager::LeConnectionManagementCallbacks {
592  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)593   LeShimAclConnection(
594       SendDataUpwards send_data_upwards, OnDisconnect on_disconnect,
595       const shim::legacy::acl_le_link_interface_t& interface,
596       os::Handler* handler,
597       std::unique_ptr<hci::acl_manager::LeAclConnection> connection,
598       std::chrono::time_point<std::chrono::system_clock> creation_time)
599       : ShimAclConnection(connection->GetHandle(), send_data_upwards, handler,
600                           connection->GetAclQueueEnd(), creation_time),
601         on_disconnect_(on_disconnect),
602         interface_(interface),
603         connection_(std::move(connection)) {}
604 
RegisterCallbacks()605   void RegisterCallbacks() override {
606     connection_->RegisterCallbacks(this, handler_);
607   }
608 
ReadRemoteControllerInformation()609   void ReadRemoteControllerInformation() override {
610     // TODO Issue LeReadRemoteFeatures Command
611   }
612 
GetLocalAddressWithType()613   bluetooth::hci::AddressWithType GetLocalAddressWithType() {
614     return connection_->GetLocalAddress();
615   }
616 
OnConnectionUpdate(hci::ErrorCode hci_status,uint16_t connection_interval,uint16_t connection_latency,uint16_t supervision_timeout)617   void OnConnectionUpdate(hci::ErrorCode hci_status,
618                           uint16_t connection_interval,
619                           uint16_t connection_latency,
620                           uint16_t supervision_timeout) {
621     TRY_POSTING_ON_MAIN(
622         interface_.on_connection_update, ToLegacyHciErrorCode(hci_status),
623         handle_, connection_interval, connection_latency, supervision_timeout);
624   }
OnDataLengthChange(uint16_t tx_octets,uint16_t tx_time,uint16_t rx_octets,uint16_t rx_time)625   void OnDataLengthChange(uint16_t tx_octets, uint16_t tx_time,
626                           uint16_t rx_octets, uint16_t rx_time) {
627     TRY_POSTING_ON_MAIN(interface_.on_data_length_change, tx_octets, tx_time,
628                         rx_octets, rx_time);
629   }
630 
OnReadRemoteVersionInformationComplete(hci::ErrorCode hci_status,uint8_t lmp_version,uint16_t manufacturer_name,uint16_t sub_version)631   void OnReadRemoteVersionInformationComplete(hci::ErrorCode hci_status,
632                                               uint8_t lmp_version,
633                                               uint16_t manufacturer_name,
634                                               uint16_t sub_version) override {
635     TRY_POSTING_ON_MAIN(interface_.on_read_remote_version_information_complete,
636                         ToLegacyHciErrorCode(hci_status), handle_, lmp_version,
637                         manufacturer_name, sub_version);
638   }
639 
OnPhyUpdate(hci::ErrorCode hci_status,uint8_t tx_phy,uint8_t rx_phy)640   void OnPhyUpdate(hci::ErrorCode hci_status, uint8_t tx_phy,
641                    uint8_t rx_phy) override {
642     TRY_POSTING_ON_MAIN(interface_.on_phy_update,
643                         ToLegacyHciErrorCode(hci_status), handle_, tx_phy,
644                         rx_phy);
645   }
646 
OnLocalAddressUpdate(hci::AddressWithType address_with_type)647   void OnLocalAddressUpdate(hci::AddressWithType address_with_type) override {
648     connection_->UpdateLocalAddress(address_with_type);
649   }
650 
OnDisconnection(hci::ErrorCode reason)651   void OnDisconnection(hci::ErrorCode reason) {
652     Disconnect();
653     on_disconnect_(handle_, reason);
654   }
655 
GetRemoteAddressWithType() const656   hci::AddressWithType GetRemoteAddressWithType() const {
657     return connection_->GetRemoteAddress();
658   }
659 
InitiateDisconnect(hci::DisconnectReason reason)660   void InitiateDisconnect(hci::DisconnectReason reason) override {
661     connection_->Disconnect(reason);
662   }
663 
IsLocallyInitiated() const664   bool IsLocallyInitiated() const override {
665     return connection_->locally_initiated_;
666   }
667 
668  private:
669   OnDisconnect on_disconnect_;
670   const shim::legacy::acl_le_link_interface_t interface_;
671   std::unique_ptr<hci::acl_manager::LeAclConnection> connection_;
672 };
673 
674 struct shim::legacy::Acl::impl {
implshim::legacy::Acl::impl675   impl(uint8_t max_acceptlist_size)
676       : shadow_acceptlist_(ShadowAcceptlist(max_acceptlist_size)) {}
677 
678   std::map<HciHandle, std::unique_ptr<ClassicShimAclConnection>>
679       handle_to_classic_connection_map_;
680   std::map<HciHandle, std::unique_ptr<LeShimAclConnection>>
681       handle_to_le_connection_map_;
682 
683   FixedQueue<std::unique_ptr<ConnectionDescriptor>> connection_history_ =
684       FixedQueue<std::unique_ptr<ConnectionDescriptor>>(kConnectionHistorySize);
685 
686   ShadowAcceptlist shadow_acceptlist_;
687 
IsClassicAclshim::legacy::Acl::impl688   bool IsClassicAcl(HciHandle handle) {
689     return handle_to_classic_connection_map_.find(handle) !=
690            handle_to_classic_connection_map_.end();
691   }
692 
EnqueueClassicPacketshim::legacy::Acl::impl693   void EnqueueClassicPacket(HciHandle handle,
694                             std::unique_ptr<packet::RawBuilder> packet) {
695     ASSERT_LOG(IsClassicAcl(handle), "handle %d is not a classic connection",
696                handle);
697     handle_to_classic_connection_map_[handle]->EnqueuePacket(std::move(packet));
698   }
699 
IsLeAclshim::legacy::Acl::impl700   bool IsLeAcl(HciHandle handle) {
701     return handle_to_le_connection_map_.find(handle) !=
702            handle_to_le_connection_map_.end();
703   }
704 
EnqueueLePacketshim::legacy::Acl::impl705   void EnqueueLePacket(HciHandle handle,
706                        std::unique_ptr<packet::RawBuilder> packet) {
707     ASSERT_LOG(IsLeAcl(handle), "handle %d is not a LE connection", handle);
708     handle_to_le_connection_map_[handle]->EnqueuePacket(std::move(packet));
709   }
710 
ShutdownClassicConnectionsshim::legacy::Acl::impl711   void ShutdownClassicConnections(std::promise<void> promise) {
712     LOG_INFO("Shutdown gd acl shim classic connections");
713     for (auto& connection : handle_to_classic_connection_map_) {
714       connection.second->Shutdown();
715     }
716     handle_to_classic_connection_map_.clear();
717     promise.set_value();
718   }
719 
ShutdownLeConnectionsshim::legacy::Acl::impl720   void ShutdownLeConnections(std::promise<void> promise) {
721     LOG_INFO("Shutdown gd acl shim le connections");
722     for (auto& connection : handle_to_le_connection_map_) {
723       connection.second->Shutdown();
724     }
725     handle_to_le_connection_map_.clear();
726     promise.set_value();
727   }
728 
FinalShutdownshim::legacy::Acl::impl729   void FinalShutdown(std::promise<void> promise) {
730     if (!handle_to_classic_connection_map_.empty()) {
731       for (auto& connection : handle_to_classic_connection_map_) {
732         connection.second->Shutdown();
733       }
734       handle_to_classic_connection_map_.clear();
735       LOG_INFO("Cleared all classic connections count:%zu",
736                handle_to_classic_connection_map_.size());
737     }
738 
739     if (!handle_to_le_connection_map_.empty()) {
740       for (auto& connection : handle_to_le_connection_map_) {
741         connection.second->Shutdown();
742       }
743       handle_to_le_connection_map_.clear();
744       LOG_INFO("Cleared all le connections count:%zu",
745                handle_to_le_connection_map_.size());
746     }
747     promise.set_value();
748   }
749 
HoldModeshim::legacy::Acl::impl750   void HoldMode(HciHandle handle, uint16_t max_interval,
751                 uint16_t min_interval) {
752     ASSERT_LOG(IsClassicAcl(handle), "handle %d is not a classic connection",
753                handle);
754     handle_to_classic_connection_map_[handle]->HoldMode(max_interval,
755                                                         min_interval);
756   }
757 
ExitSniffModeshim::legacy::Acl::impl758   void ExitSniffMode(HciHandle handle) {
759     ASSERT_LOG(IsClassicAcl(handle), "handle %d is not a classic connection",
760                handle);
761     handle_to_classic_connection_map_[handle]->ExitSniffMode();
762   }
763 
SniffModeshim::legacy::Acl::impl764   void SniffMode(HciHandle handle, uint16_t max_interval, uint16_t min_interval,
765                  uint16_t attempt, uint16_t timeout) {
766     ASSERT_LOG(IsClassicAcl(handle), "handle %d is not a classic connection",
767                handle);
768     handle_to_classic_connection_map_[handle]->SniffMode(
769         max_interval, min_interval, attempt, timeout);
770   }
771 
SniffSubratingshim::legacy::Acl::impl772   void SniffSubrating(HciHandle handle, uint16_t maximum_latency,
773                       uint16_t minimum_remote_timeout,
774                       uint16_t minimum_local_timeout) {
775     ASSERT_LOG(IsClassicAcl(handle), "handle %d is not a classic connection",
776                handle);
777     handle_to_classic_connection_map_[handle]->SniffSubrating(
778         maximum_latency, minimum_remote_timeout, minimum_local_timeout);
779   }
780 
SetConnectionEncryptionshim::legacy::Acl::impl781   void SetConnectionEncryption(HciHandle handle, hci::Enable enable) {
782     ASSERT_LOG(IsClassicAcl(handle), "handle %d is not a classic connection",
783                handle);
784     handle_to_classic_connection_map_[handle]->SetConnectionEncryption(enable);
785   }
786 
disconnect_classicshim::legacy::Acl::impl787   void disconnect_classic(uint16_t handle, tHCI_STATUS reason) {
788     auto connection = handle_to_classic_connection_map_.find(handle);
789     if (connection != handle_to_classic_connection_map_.end()) {
790       auto remote_address = connection->second->GetRemoteAddress();
791       connection->second->InitiateDisconnect(
792           ToDisconnectReasonFromLegacy(reason));
793       LOG_DEBUG("Disconnection initiated classic remote:%s handle:%hu",
794                 PRIVATE_ADDRESS(remote_address), handle);
795       BTM_LogHistory(kBtmLogTag, ToRawAddress(remote_address),
796                      "Disconnection initiated", "classic");
797     } else {
798       LOG_WARN("Unable to disconnect unknown classic connection handle:0x%04x",
799                handle);
800     }
801   }
802 
disconnect_leshim::legacy::Acl::impl803   void disconnect_le(uint16_t handle, tHCI_STATUS reason) {
804     auto connection = handle_to_le_connection_map_.find(handle);
805     if (connection != handle_to_le_connection_map_.end()) {
806       auto remote_address_with_type =
807           connection->second->GetRemoteAddressWithType();
808       connection->second->InitiateDisconnect(
809           ToDisconnectReasonFromLegacy(reason));
810       LOG_DEBUG("Disconnection initiated le remote:%s handle:%hu",
811                 PRIVATE_ADDRESS(remote_address_with_type), handle);
812       BTM_LogHistory(kBtmLogTag,
813                      ToLegacyAddressWithType(remote_address_with_type),
814                      "Disconnection initiated", "Le");
815     } else {
816       LOG_WARN("Unable to disconnect unknown le connection handle:0x%04x",
817                handle);
818     }
819   }
820 
accept_le_connection_fromshim::legacy::Acl::impl821   void accept_le_connection_from(const hci::AddressWithType& address_with_type,
822                                  bool is_direct, std::promise<bool> promise) {
823     if (shadow_acceptlist_.IsFull()) {
824       LOG_ERROR("Acceptlist is full preventing new Le connection");
825       promise.set_value(false);
826       return;
827     }
828     shadow_acceptlist_.Add(address_with_type);
829     promise.set_value(true);
830     GetAclManager()->CreateLeConnection(address_with_type, is_direct);
831     LOG_DEBUG("Allow Le connection from remote:%s",
832               PRIVATE_ADDRESS(address_with_type));
833     BTM_LogHistory(kBtmLogTag, ToLegacyAddressWithType(address_with_type),
834                    "Allow connection from", "Le");
835   }
836 
ignore_le_connection_fromshim::legacy::Acl::impl837   void ignore_le_connection_from(
838       const hci::AddressWithType& address_with_type) {
839     shadow_acceptlist_.Remove(address_with_type);
840     GetAclManager()->CancelLeConnect(address_with_type);
841     LOG_DEBUG("Ignore Le connection from remote:%s",
842               PRIVATE_ADDRESS(address_with_type));
843     BTM_LogHistory(kBtmLogTag, ToLegacyAddressWithType(address_with_type),
844                    "Ignore connection from", "Le");
845   }
846 
clear_acceptlistshim::legacy::Acl::impl847   void clear_acceptlist() {
848     auto shadow_acceptlist = shadow_acceptlist_.GetCopy();
849     size_t count = shadow_acceptlist.size();
850     for (auto address_with_type : shadow_acceptlist) {
851       ignore_le_connection_from(address_with_type);
852     }
853     shadow_acceptlist_.Clear();
854     LOG_DEBUG("Cleared entire Le address acceptlist count:%zu", count);
855   }
856 
DumpConnectionHistoryshim::legacy::Acl::impl857   void DumpConnectionHistory() const {
858     std::vector<std::string> history =
859         connection_history_.ReadElementsAsString();
860     for (auto& entry : history) {
861       LOG_DEBUG("%s", entry.c_str());
862     }
863     const auto acceptlist = shadow_acceptlist_.GetCopy();
864     LOG_DEBUG("Shadow le accept list  size:%-3zu controller_max_size:%hhu",
865               acceptlist.size(),
866               controller_get_interface()->get_ble_acceptlist_size());
867     for (auto& entry : acceptlist) {
868       LOG_DEBUG("acceptlist:%s", entry.ToString().c_str());
869     }
870   }
871 
872 #define DUMPSYS_TAG "shim::acl"
DumpConnectionHistoryshim::legacy::Acl::impl873   void DumpConnectionHistory(int fd) const {
874     std::vector<std::string> history =
875         connection_history_.ReadElementsAsString();
876     for (auto& entry : history) {
877       LOG_DUMPSYS(fd, "%s", entry.c_str());
878     }
879     auto acceptlist = shadow_acceptlist_.GetCopy();
880     LOG_DUMPSYS(fd,
881                 "Shadow le accept list  size:%-3zu controller_max_size:%hhu",
882                 acceptlist.size(),
883                 controller_get_interface()->get_ble_acceptlist_size());
884     unsigned cnt = 0;
885     for (auto& entry : acceptlist) {
886       LOG_DUMPSYS(fd, "%03u le acceptlist:%s", ++cnt, entry.ToString().c_str());
887     }
888   }
889 #undef DUMPSYS_TAG
890 };
891 
892 #define DUMPSYS_TAG "shim::legacy::l2cap"
893 extern tL2C_CB l2cb;
DumpsysL2cap(int fd)894 void DumpsysL2cap(int fd) {
895   LOG_DUMPSYS_TITLE(fd, DUMPSYS_TAG);
896   for (int i = 0; i < MAX_L2CAP_LINKS; i++) {
897     const tL2C_LCB& lcb = l2cb.lcb_pool[i];
898     if (!lcb.in_use) continue;
899     LOG_DUMPSYS(fd, "link_state:%s", link_state_text(lcb.link_state).c_str());
900     LOG_DUMPSYS(fd, "handle:0x%04x", lcb.Handle());
901 
902     const tL2C_CCB* ccb = lcb.ccb_queue.p_first_ccb;
903     while (ccb != nullptr) {
904       LOG_DUMPSYS(
905           fd, "  active channel lcid:0x%04x rcid:0x%04x is_ecoc:%s in_use:%s",
906           ccb->local_cid, ccb->remote_cid, common::ToString(ccb->ecoc).c_str(),
907           common::ToString(ccb->in_use).c_str());
908       ccb = ccb->p_next_ccb;
909     }
910   }
911 }
912 
913 #undef DUMPSYS_TAG
914 #define DUMPSYS_TAG "shim::legacy::acl"
DumpsysAcl(int fd)915 void DumpsysAcl(int fd) {
916   const tACL_CB& acl_cb = btm_cb.acl_cb_;
917 
918   LOG_DUMPSYS_TITLE(fd, DUMPSYS_TAG);
919 
920   shim::Stack::GetInstance()->GetAcl()->DumpConnectionHistory(fd);
921 
922   for (int i = 0; i < MAX_L2CAP_LINKS; i++) {
923     const tACL_CONN& link = acl_cb.acl_db[i];
924     if (!link.in_use) continue;
925 
926     LOG_DUMPSYS(fd, "remote_addr:%s handle:0x%04x transport:%s",
927                 link.remote_addr.ToString().c_str(), link.hci_handle,
928                 bt_transport_text(link.transport).c_str());
929     LOG_DUMPSYS(fd, "    link_up_issued:%5s",
930                 (link.link_up_issued) ? "true" : "false");
931     LOG_DUMPSYS(fd, "    flush_timeout:0x%04x", link.flush_timeout_in_ticks);
932     LOG_DUMPSYS(fd, "    link_supervision_timeout:%.3f sec",
933                 ticks_to_seconds(link.link_super_tout));
934     LOG_DUMPSYS(fd, "    disconnect_reason:0x%02x", link.disconnect_reason);
935 
936     if (link.is_transport_br_edr()) {
937       for (int j = 0; j < HCI_EXT_FEATURES_PAGE_MAX + 1; j++) {
938         LOG_DUMPSYS(fd, "    peer_lmp_features[%d] valid:%s data:%s", j,
939                     common::ToString(link.peer_lmp_feature_valid[j]).c_str(),
940                     bd_features_text(link.peer_lmp_feature_pages[j]).c_str());
941       }
942       LOG_DUMPSYS(fd, "    [classic] link_policy:%s",
943                   link_policy_text(static_cast<tLINK_POLICY>(link.link_policy))
944                       .c_str());
945       LOG_DUMPSYS(fd, "    [classic] sniff_subrating:%s",
946                   common::ToString(HCI_SNIFF_SUB_RATE_SUPPORTED(
947                                        link.peer_lmp_feature_pages[0]))
948                       .c_str());
949 
950       LOG_DUMPSYS(fd, "    pkt_types_mask:0x%04x", link.pkt_types_mask);
951       LOG_DUMPSYS(fd, "    role:%s", RoleText(link.link_role).c_str());
952     } else if (link.is_transport_ble()) {
953       LOG_DUMPSYS(fd, "    [le] peer_features valid:%s data:%s",
954                   common::ToString(link.peer_le_features_valid).c_str(),
955                   bd_features_text(link.peer_le_features).c_str());
956 
957       LOG_DUMPSYS(fd, "    [le] active_remote_addr:%s",
958                   link.active_remote_addr.ToString().c_str());
959       LOG_DUMPSYS(fd, "    [le] conn_addr:%s",
960                   link.conn_addr.ToString().c_str());
961     }
962   }
963 }
964 #undef DUMPSYS_TAG
965 
966 using Record = common::TimestampedEntry<std::string>;
967 const std::string kTimeFormat("%Y-%m-%d %H:%M:%S");
968 
969 #define DUMPSYS_TAG "shim::legacy::hid"
970 extern btif_hh_cb_t btif_hh_cb;
971 
DumpsysHid(int fd)972 void DumpsysHid(int fd) {
973   LOG_DUMPSYS_TITLE(fd, DUMPSYS_TAG);
974   LOG_DUMPSYS(fd, "status:%s num_devices:%u",
975               btif_hh_status_text(btif_hh_cb.status).c_str(),
976               btif_hh_cb.device_num);
977   LOG_DUMPSYS(fd, "status:%s", btif_hh_status_text(btif_hh_cb.status).c_str());
978   for (unsigned i = 0; i < BTIF_HH_MAX_HID; i++) {
979     const btif_hh_device_t* p_dev = &btif_hh_cb.devices[i];
980     if (p_dev->bd_addr != RawAddress::kEmpty) {
981       LOG_DUMPSYS(fd, "  %u: addr:%s fd:%d state:%s ready:%s thread_id:%d", i,
982                   PRIVATE_ADDRESS(p_dev->bd_addr), p_dev->fd,
983                   bthh_connection_state_text(p_dev->dev_status).c_str(),
984                   (p_dev->ready_for_data) ? ("T") : ("F"),
985                   static_cast<int>(p_dev->hh_poll_thread_id));
986     }
987   }
988   for (unsigned i = 0; i < BTIF_HH_MAX_ADDED_DEV; i++) {
989     const btif_hh_added_device_t* p_dev = &btif_hh_cb.added_devices[i];
990     if (p_dev->bd_addr != RawAddress::kEmpty) {
991       LOG_DUMPSYS(fd, "  %u: addr:%s", i, PRIVATE_ADDRESS(p_dev->bd_addr));
992     }
993   }
994 }
995 #undef DUMPSYS_TAG
996 
997 #define DUMPSYS_TAG "shim::legacy::btm"
DumpsysBtm(int fd)998 void DumpsysBtm(int fd) {
999   LOG_DUMPSYS_TITLE(fd, DUMPSYS_TAG);
1000   if (btm_cb.history_ != nullptr) {
1001     std::vector<Record> history = btm_cb.history_->Pull();
1002     for (auto& record : history) {
1003       time_t then = record.timestamp / 1000;
1004       struct tm tm;
1005       localtime_r(&then, &tm);
1006       auto s2 = common::StringFormatTime(kTimeFormat, tm);
1007       LOG_DUMPSYS(fd, " %s.%03u %s", s2.c_str(),
1008                   static_cast<unsigned int>(record.timestamp % 1000),
1009                   record.entry.c_str());
1010     }
1011   }
1012 }
1013 #undef DUMPSYS_TAG
1014 
1015 #define DUMPSYS_TAG "shim::legacy::record"
DumpsysRecord(int fd)1016 void DumpsysRecord(int fd) {
1017   LOG_DUMPSYS_TITLE(fd, DUMPSYS_TAG);
1018 
1019   if (btm_cb.sec_dev_rec == nullptr) {
1020     LOG_DUMPSYS(fd, "Record is empty - no devices");
1021     return;
1022   }
1023 
1024   unsigned cnt = 0;
1025   list_node_t* end = list_end(btm_cb.sec_dev_rec);
1026   for (list_node_t* node = list_begin(btm_cb.sec_dev_rec); node != end;
1027        node = list_next(node)) {
1028     tBTM_SEC_DEV_REC* p_dev_rec =
1029         static_cast<tBTM_SEC_DEV_REC*>(list_node(node));
1030     LOG_DUMPSYS(fd, "%03u %s", ++cnt, p_dev_rec->ToString().c_str());
1031   }
1032 }
1033 #undef DUMPSYS_TAG
1034 
Dump(int fd) const1035 void shim::legacy::Acl::Dump(int fd) const {
1036   DumpsysHid(fd);
1037   DumpsysRecord(fd);
1038   DumpsysAcl(fd);
1039   DumpsysL2cap(fd);
1040   DumpsysBtm(fd);
1041 }
1042 
Acl(os::Handler * handler,const acl_interface_t & acl_interface,uint8_t max_acceptlist_size)1043 shim::legacy::Acl::Acl(os::Handler* handler,
1044                        const acl_interface_t& acl_interface,
1045                        uint8_t max_acceptlist_size)
1046     : handler_(handler), acl_interface_(acl_interface) {
1047   ASSERT(handler_ != nullptr);
1048   ValidateAclInterface(acl_interface_);
1049   pimpl_ = std::make_unique<Acl::impl>(max_acceptlist_size);
1050   GetAclManager()->RegisterCallbacks(this, handler_);
1051   GetAclManager()->RegisterLeCallbacks(this, handler_);
1052   GetController()->RegisterCompletedMonitorAclPacketsCallback(
1053       handler->BindOn(this, &Acl::on_incoming_acl_credits));
1054   shim::RegisterDumpsysFunction(static_cast<void*>(this),
1055                                 [this](int fd) { Dump(fd); });
1056 
1057   GetAclManager()->HACK_SetScoDisconnectCallback(
1058       [this](uint16_t handle, uint8_t reason) {
1059         TRY_POSTING_ON_MAIN(acl_interface_.connection.sco.on_disconnected,
1060                             handle, static_cast<tHCI_REASON>(reason));
1061       });
1062 }
1063 
~Acl()1064 shim::legacy::Acl::~Acl() {
1065   shim::UnregisterDumpsysFunction(static_cast<void*>(this));
1066   GetController()->UnregisterCompletedMonitorAclPacketsCallback();
1067 
1068   if (CheckForOrphanedAclConnections()) {
1069     pimpl_->DumpConnectionHistory();
1070   }
1071 }
1072 
CheckForOrphanedAclConnections() const1073 bool shim::legacy::Acl::CheckForOrphanedAclConnections() const {
1074   bool orphaned_acl_connections = false;
1075 
1076   if (!pimpl_->handle_to_classic_connection_map_.empty()) {
1077     LOG_ERROR("About to destroy classic active ACL");
1078     for (const auto& connection : pimpl_->handle_to_classic_connection_map_) {
1079       LOG_ERROR("  Orphaned classic ACL handle:0x%04x bd_addr:%s created:%s",
1080                 connection.second->Handle(),
1081                 PRIVATE_ADDRESS(connection.second->GetRemoteAddress()),
1082                 common::StringFormatTimeWithMilliseconds(
1083                     kConnectionDescriptorTimeFormat,
1084                     connection.second->GetCreationTime())
1085                     .c_str());
1086     }
1087     orphaned_acl_connections = true;
1088   }
1089 
1090   if (!pimpl_->handle_to_le_connection_map_.empty()) {
1091     LOG_ERROR("About to destroy le active ACL");
1092     for (const auto& connection : pimpl_->handle_to_le_connection_map_) {
1093       LOG_ERROR("  Orphaned le ACL handle:0x%04x bd_addr:%s created:%s",
1094                 connection.second->Handle(),
1095                 PRIVATE_ADDRESS(connection.second->GetRemoteAddressWithType()),
1096                 common::StringFormatTimeWithMilliseconds(
1097                     kConnectionDescriptorTimeFormat,
1098                     connection.second->GetCreationTime())
1099                     .c_str());
1100     }
1101     orphaned_acl_connections = true;
1102   }
1103   return orphaned_acl_connections;
1104 }
1105 
on_incoming_acl_credits(uint16_t handle,uint16_t credits)1106 void shim::legacy::Acl::on_incoming_acl_credits(uint16_t handle,
1107                                                 uint16_t credits) {
1108   TRY_POSTING_ON_MAIN(acl_interface_.on_packets_completed, handle, credits);
1109 }
1110 
write_data_sync(HciHandle handle,std::unique_ptr<packet::RawBuilder> packet)1111 void shim::legacy::Acl::write_data_sync(
1112     HciHandle handle, std::unique_ptr<packet::RawBuilder> packet) {
1113   if (pimpl_->IsClassicAcl(handle)) {
1114     pimpl_->EnqueueClassicPacket(handle, std::move(packet));
1115   } else if (pimpl_->IsLeAcl(handle)) {
1116     pimpl_->EnqueueLePacket(handle, std::move(packet));
1117   } else {
1118     LOG_ERROR("Unable to find destination to write data\n");
1119   }
1120 }
1121 
WriteData(HciHandle handle,std::unique_ptr<packet::RawBuilder> packet)1122 void shim::legacy::Acl::WriteData(HciHandle handle,
1123                                   std::unique_ptr<packet::RawBuilder> packet) {
1124   handler_->Post(common::BindOnce(&Acl::write_data_sync,
1125                                   common::Unretained(this), handle,
1126                                   std::move(packet)));
1127 }
1128 
CreateClassicConnection(const hci::Address & address)1129 void shim::legacy::Acl::CreateClassicConnection(const hci::Address& address) {
1130   GetAclManager()->CreateConnection(address);
1131   LOG_DEBUG("Connection initiated for classic to remote:%s",
1132             PRIVATE_ADDRESS(address));
1133   BTM_LogHistory(kBtmLogTag, ToRawAddress(address), "Initiated connection",
1134                  "classic");
1135 }
1136 
CancelClassicConnection(const hci::Address & address)1137 void shim::legacy::Acl::CancelClassicConnection(const hci::Address& address) {
1138   GetAclManager()->CancelConnect(address);
1139   LOG_DEBUG("Connection cancelled for classic to remote:%s",
1140             PRIVATE_ADDRESS(address));
1141   BTM_LogHistory(kBtmLogTag, ToRawAddress(address), "Cancelled connection",
1142                  "classic");
1143 }
1144 
AcceptLeConnectionFrom(const hci::AddressWithType & address_with_type,bool is_direct,std::promise<bool> promise)1145 void shim::legacy::Acl::AcceptLeConnectionFrom(
1146     const hci::AddressWithType& address_with_type, bool is_direct,
1147     std::promise<bool> promise) {
1148   handler_->CallOn(pimpl_.get(), &Acl::impl::accept_le_connection_from,
1149                    address_with_type, is_direct, std::move(promise));
1150 }
1151 
IgnoreLeConnectionFrom(const hci::AddressWithType & address_with_type)1152 void shim::legacy::Acl::IgnoreLeConnectionFrom(
1153     const hci::AddressWithType& address_with_type) {
1154   handler_->CallOn(pimpl_.get(), &Acl::impl::ignore_le_connection_from,
1155                    address_with_type);
1156 }
1157 
OnClassicLinkDisconnected(HciHandle handle,hci::ErrorCode reason)1158 void shim::legacy::Acl::OnClassicLinkDisconnected(HciHandle handle,
1159                                                   hci::ErrorCode reason) {
1160   hci::Address remote_address =
1161       pimpl_->handle_to_classic_connection_map_[handle]->GetRemoteAddress();
1162   CreationTime creation_time =
1163       pimpl_->handle_to_classic_connection_map_[handle]->GetCreationTime();
1164   bool is_locally_initiated =
1165       pimpl_->handle_to_classic_connection_map_[handle]->IsLocallyInitiated();
1166 
1167   TeardownTime teardown_time = std::chrono::system_clock::now();
1168 
1169   pimpl_->handle_to_classic_connection_map_.erase(handle);
1170   TRY_POSTING_ON_MAIN(acl_interface_.connection.classic.on_disconnected,
1171                       ToLegacyHciErrorCode(hci::ErrorCode::SUCCESS), handle,
1172                       ToLegacyHciErrorCode(reason));
1173   LOG_DEBUG("Disconnected classic link remote:%s handle:%hu reason:%s",
1174             PRIVATE_ADDRESS(remote_address), handle,
1175             ErrorCodeText(reason).c_str());
1176   BTM_LogHistory(
1177       kBtmLogTag, ToRawAddress(remote_address), "Disconnected",
1178       base::StringPrintf("classic reason:%s", ErrorCodeText(reason).c_str()));
1179   pimpl_->connection_history_.Push(
1180       std::move(std::make_unique<ClassicConnectionDescriptor>(
1181           remote_address, creation_time, teardown_time, handle,
1182           is_locally_initiated, reason)));
1183 }
1184 
GetConnectionLocalAddress(const RawAddress & remote_bda)1185 bluetooth::hci::AddressWithType shim::legacy::Acl::GetConnectionLocalAddress(
1186     const RawAddress& remote_bda) {
1187   bluetooth::hci::AddressWithType address_with_type;
1188   auto remote_address = ToGdAddress(remote_bda);
1189   for (auto& [handle, connection] : pimpl_->handle_to_le_connection_map_) {
1190     if (connection->GetRemoteAddressWithType().GetAddress() == remote_address) {
1191       return connection->GetLocalAddressWithType();
1192     }
1193   }
1194   LOG_WARN("address not found!");
1195   return address_with_type;
1196 }
1197 
OnLeLinkDisconnected(HciHandle handle,hci::ErrorCode reason)1198 void shim::legacy::Acl::OnLeLinkDisconnected(HciHandle handle,
1199                                              hci::ErrorCode reason) {
1200   hci::AddressWithType remote_address_with_type =
1201       pimpl_->handle_to_le_connection_map_[handle]->GetRemoteAddressWithType();
1202   CreationTime creation_time =
1203       pimpl_->handle_to_le_connection_map_[handle]->GetCreationTime();
1204   bool is_locally_initiated =
1205       pimpl_->handle_to_le_connection_map_[handle]->IsLocallyInitiated();
1206 
1207   TeardownTime teardown_time = std::chrono::system_clock::now();
1208 
1209   pimpl_->handle_to_le_connection_map_.erase(handle);
1210   TRY_POSTING_ON_MAIN(acl_interface_.connection.le.on_disconnected,
1211                       ToLegacyHciErrorCode(hci::ErrorCode::SUCCESS), handle,
1212                       ToLegacyHciErrorCode(reason));
1213   LOG_DEBUG("Disconnected le link remote:%s handle:%hu reason:%s",
1214             PRIVATE_ADDRESS(remote_address_with_type), handle,
1215             ErrorCodeText(reason).c_str());
1216   BTM_LogHistory(
1217       kBtmLogTag, ToLegacyAddressWithType(remote_address_with_type),
1218       "Disconnected",
1219       base::StringPrintf("Le reason:%s", ErrorCodeText(reason).c_str()));
1220   pimpl_->connection_history_.Push(
1221       std::move(std::make_unique<LeConnectionDescriptor>(
1222           remote_address_with_type, creation_time, teardown_time, handle,
1223           is_locally_initiated, reason)));
1224 }
1225 
OnConnectSuccess(std::unique_ptr<hci::acl_manager::ClassicAclConnection> connection)1226 void shim::legacy::Acl::OnConnectSuccess(
1227     std::unique_ptr<hci::acl_manager::ClassicAclConnection> connection) {
1228   ASSERT(connection != nullptr);
1229   auto handle = connection->GetHandle();
1230   bool locally_initiated = connection->locally_initiated_;
1231   const hci::Address remote_address = connection->GetAddress();
1232   const RawAddress bd_addr = ToRawAddress(remote_address);
1233 
1234   pimpl_->handle_to_classic_connection_map_.emplace(
1235       handle, std::make_unique<ClassicShimAclConnection>(
1236                   acl_interface_.on_send_data_upwards,
1237                   std::bind(&shim::legacy::Acl::OnClassicLinkDisconnected, this,
1238                             std::placeholders::_1, std::placeholders::_2),
1239                   acl_interface_.link.classic, handler_, std::move(connection),
1240                   std::chrono::system_clock::now()));
1241   pimpl_->handle_to_classic_connection_map_[handle]->RegisterCallbacks();
1242   pimpl_->handle_to_classic_connection_map_[handle]
1243       ->ReadRemoteControllerInformation();
1244 
1245   TRY_POSTING_ON_MAIN(acl_interface_.connection.classic.on_connected, bd_addr,
1246                       handle, false);
1247   LOG_DEBUG("Connection successful classic remote:%s handle:%hu initiator:%s",
1248             PRIVATE_ADDRESS(remote_address), handle,
1249             (locally_initiated) ? "local" : "remote");
1250   BTM_LogHistory(kBtmLogTag, ToRawAddress(remote_address),
1251                  "Connection successful",
1252                  (locally_initiated) ? "Local initiated" : "Remote initiated");
1253 }
1254 
OnConnectFail(hci::Address address,hci::ErrorCode reason)1255 void shim::legacy::Acl::OnConnectFail(hci::Address address,
1256                                       hci::ErrorCode reason) {
1257   const RawAddress bd_addr = ToRawAddress(address);
1258   TRY_POSTING_ON_MAIN(acl_interface_.connection.classic.on_failed, bd_addr,
1259                       ToLegacyHciErrorCode(reason));
1260   LOG_WARN("Connection failed classic remote:%s reason:%s",
1261            PRIVATE_ADDRESS(address), hci::ErrorCodeText(reason).c_str());
1262   BTM_LogHistory(kBtmLogTag, ToRawAddress(address), "Connection failed",
1263                  base::StringPrintf("classic reason:%s",
1264                                     hci::ErrorCodeText(reason).c_str()));
1265 }
1266 
HACK_OnEscoConnectRequest(hci::Address address,hci::ClassOfDevice cod)1267 void shim::legacy::Acl::HACK_OnEscoConnectRequest(hci::Address address,
1268                                                   hci::ClassOfDevice cod) {
1269   const RawAddress bd_addr = ToRawAddress(address);
1270   types::ClassOfDevice legacy_cod;
1271   types::ClassOfDevice::FromString(cod.ToLegacyConfigString(), legacy_cod);
1272 
1273   TRY_POSTING_ON_MAIN(acl_interface_.connection.sco.on_esco_connect_request,
1274                       bd_addr, legacy_cod);
1275   LOG_DEBUG("Received ESCO connect request remote:%s",
1276             PRIVATE_ADDRESS(address));
1277   BTM_LogHistory(kBtmLogTag, ToRawAddress(address), "ESCO Connection request");
1278 }
1279 
HACK_OnScoConnectRequest(hci::Address address,hci::ClassOfDevice cod)1280 void shim::legacy::Acl::HACK_OnScoConnectRequest(hci::Address address,
1281                                                  hci::ClassOfDevice cod) {
1282   const RawAddress bd_addr = ToRawAddress(address);
1283   types::ClassOfDevice legacy_cod;
1284   types::ClassOfDevice::FromString(cod.ToLegacyConfigString(), legacy_cod);
1285 
1286   TRY_POSTING_ON_MAIN(acl_interface_.connection.sco.on_sco_connect_request,
1287                       bd_addr, legacy_cod);
1288   LOG_DEBUG("Received SCO connect request remote:%s", PRIVATE_ADDRESS(address));
1289   BTM_LogHistory(kBtmLogTag, ToRawAddress(address), "SCO Connection request");
1290 }
1291 
OnLeConnectSuccess(hci::AddressWithType address_with_type,std::unique_ptr<hci::acl_manager::LeAclConnection> connection)1292 void shim::legacy::Acl::OnLeConnectSuccess(
1293     hci::AddressWithType address_with_type,
1294     std::unique_ptr<hci::acl_manager::LeAclConnection> connection) {
1295   ASSERT(connection != nullptr);
1296   auto handle = connection->GetHandle();
1297 
1298   hci::Role connection_role = connection->GetRole();
1299   bool locally_initiated = connection->locally_initiated_;
1300 
1301   pimpl_->handle_to_le_connection_map_.emplace(
1302       handle, std::make_unique<LeShimAclConnection>(
1303                   acl_interface_.on_send_data_upwards,
1304                   std::bind(&shim::legacy::Acl::OnLeLinkDisconnected, this,
1305                             std::placeholders::_1, std::placeholders::_2),
1306                   acl_interface_.link.le, handler_, std::move(connection),
1307                   std::chrono::system_clock::now()));
1308   pimpl_->handle_to_le_connection_map_[handle]->RegisterCallbacks();
1309 
1310   pimpl_->handle_to_le_connection_map_[handle]
1311       ->ReadRemoteControllerInformation();
1312 
1313   tBLE_BD_ADDR legacy_address_with_type =
1314       ToLegacyAddressWithType(address_with_type);
1315 
1316   uint16_t conn_interval = 36; /* TODO Default to 45 msec*/
1317   uint16_t conn_latency = 0;   /* TODO Default to zero events */
1318   uint16_t conn_timeout = 500; /* TODO Default to 5s */
1319 
1320   RawAddress local_rpa = RawAddress::kEmpty; /* TODO enhanced */
1321   RawAddress peer_rpa = RawAddress::kEmpty;  /* TODO enhanced */
1322   uint8_t peer_addr_type = 0;                /* TODO public */
1323 
1324   // Once an le connection has successfully been established
1325   // the device address is removed from the controller accept list.
1326   pimpl_->shadow_acceptlist_.Remove(address_with_type);
1327 
1328   TRY_POSTING_ON_MAIN(
1329       acl_interface_.connection.le.on_connected, legacy_address_with_type,
1330       handle, ToLegacyRole(connection_role), conn_interval, conn_latency,
1331       conn_timeout, local_rpa, peer_rpa, peer_addr_type);
1332 
1333   LOG_DEBUG("Connection successful le remote:%s handle:%hu initiator:%s",
1334             PRIVATE_ADDRESS(address_with_type), handle,
1335             (locally_initiated) ? "local" : "remote");
1336   BTM_LogHistory(kBtmLogTag, ToLegacyAddressWithType(address_with_type),
1337                  "Connection successful", "Le");
1338 }
1339 
OnLeConnectFail(hci::AddressWithType address_with_type,hci::ErrorCode reason)1340 void shim::legacy::Acl::OnLeConnectFail(hci::AddressWithType address_with_type,
1341                                         hci::ErrorCode reason) {
1342   tBLE_BD_ADDR legacy_address_with_type =
1343       ToLegacyAddressWithType(address_with_type);
1344 
1345   uint16_t handle = 0;  /* TODO Unneeded */
1346   bool enhanced = true; /* TODO logging metrics only */
1347   tHCI_STATUS status = ToLegacyHciErrorCode(reason);
1348 
1349   TRY_POSTING_ON_MAIN(acl_interface_.connection.le.on_failed,
1350                       legacy_address_with_type, handle, enhanced, status);
1351   LOG_WARN("Connection failed le remote:%s",
1352            PRIVATE_ADDRESS(address_with_type));
1353   BTM_LogHistory(
1354       kBtmLogTag, ToLegacyAddressWithType(address_with_type),
1355       "Connection failed",
1356       base::StringPrintf("le reason:%s", hci::ErrorCodeText(reason).c_str()));
1357 }
1358 
ConfigureLePrivacy(bool is_le_privacy_enabled)1359 void shim::legacy::Acl::ConfigureLePrivacy(bool is_le_privacy_enabled) {
1360   LOG_INFO("Configuring Le privacy:%s",
1361            (is_le_privacy_enabled) ? "true" : "false");
1362   ASSERT_LOG(is_le_privacy_enabled,
1363              "Gd shim does not support unsecure le privacy");
1364 
1365   // TODO(b/161543441): read the privacy policy from device-specific
1366   // configuration, and IRK from config file.
1367   hci::LeAddressManager::AddressPolicy address_policy =
1368       hci::LeAddressManager::AddressPolicy::USE_RESOLVABLE_ADDRESS;
1369   hci::AddressWithType empty_address_with_type(
1370       hci::Address{}, hci::AddressType::RANDOM_DEVICE_ADDRESS);
1371   /* 7 minutes minimum, 15 minutes maximum for random address refreshing */
1372   auto minimum_rotation_time = std::chrono::minutes(7);
1373   auto maximum_rotation_time = std::chrono::minutes(15);
1374 
1375   GetAclManager()->SetPrivacyPolicyForInitiatorAddress(
1376       address_policy, empty_address_with_type, minimum_rotation_time,
1377       maximum_rotation_time);
1378 }
1379 
DisconnectClassic(uint16_t handle,tHCI_STATUS reason)1380 void shim::legacy::Acl::DisconnectClassic(uint16_t handle, tHCI_STATUS reason) {
1381   handler_->CallOn(pimpl_.get(), &Acl::impl::disconnect_classic, handle,
1382                    reason);
1383 }
1384 
DisconnectLe(uint16_t handle,tHCI_STATUS reason)1385 void shim::legacy::Acl::DisconnectLe(uint16_t handle, tHCI_STATUS reason) {
1386   handler_->CallOn(pimpl_.get(), &Acl::impl::disconnect_le, handle, reason);
1387 }
1388 
HoldMode(uint16_t hci_handle,uint16_t max_interval,uint16_t min_interval)1389 bool shim::legacy::Acl::HoldMode(uint16_t hci_handle, uint16_t max_interval,
1390                                  uint16_t min_interval) {
1391   handler_->CallOn(pimpl_.get(), &Acl::impl::HoldMode, hci_handle, max_interval,
1392                    min_interval);
1393   return false;  // TODO void
1394 }
1395 
SniffMode(uint16_t hci_handle,uint16_t max_interval,uint16_t min_interval,uint16_t attempt,uint16_t timeout)1396 bool shim::legacy::Acl::SniffMode(uint16_t hci_handle, uint16_t max_interval,
1397                                   uint16_t min_interval, uint16_t attempt,
1398                                   uint16_t timeout) {
1399   handler_->CallOn(pimpl_.get(), &Acl::impl::SniffMode, hci_handle,
1400                    max_interval, min_interval, attempt, timeout);
1401   return false;
1402 }
1403 
ExitSniffMode(uint16_t hci_handle)1404 bool shim::legacy::Acl::ExitSniffMode(uint16_t hci_handle) {
1405   handler_->CallOn(pimpl_.get(), &Acl::impl::ExitSniffMode, hci_handle);
1406   return false;
1407 }
1408 
SniffSubrating(uint16_t hci_handle,uint16_t maximum_latency,uint16_t minimum_remote_timeout,uint16_t minimum_local_timeout)1409 bool shim::legacy::Acl::SniffSubrating(uint16_t hci_handle,
1410                                        uint16_t maximum_latency,
1411                                        uint16_t minimum_remote_timeout,
1412                                        uint16_t minimum_local_timeout) {
1413   handler_->CallOn(pimpl_.get(), &Acl::impl::SniffSubrating, hci_handle,
1414                    maximum_latency, minimum_remote_timeout,
1415                    minimum_local_timeout);
1416   return false;
1417 }
1418 
HACK_OnScoDisconnected(uint16_t handle,uint8_t reason)1419 void shim::legacy::Acl::HACK_OnScoDisconnected(uint16_t handle,
1420                                                uint8_t reason) {
1421   TRY_POSTING_ON_MAIN(acl_interface_.connection.sco.on_disconnected, handle,
1422                       static_cast<tHCI_REASON>(reason));
1423 }
1424 
DumpConnectionHistory(int fd) const1425 void shim::legacy::Acl::DumpConnectionHistory(int fd) const {
1426   pimpl_->DumpConnectionHistory(fd);
1427 }
1428 
Shutdown()1429 void shim::legacy::Acl::Shutdown() {
1430   if (CheckForOrphanedAclConnections()) {
1431     std::promise<void> shutdown_promise;
1432     auto shutdown_future = shutdown_promise.get_future();
1433     handler_->CallOn(pimpl_.get(), &Acl::impl::ShutdownClassicConnections,
1434                      std::move(shutdown_promise));
1435     shutdown_future.wait();
1436 
1437     shutdown_promise = std::promise<void>();
1438 
1439     shutdown_future = shutdown_promise.get_future();
1440     handler_->CallOn(pimpl_.get(), &Acl::impl::ShutdownLeConnections,
1441                      std::move(shutdown_promise));
1442     shutdown_future.wait();
1443     LOG_WARN("Flushed open ACL connections");
1444   } else {
1445     LOG_INFO("All ACL connections have been previously closed");
1446   }
1447 }
1448 
FinalShutdown()1449 void shim::legacy::Acl::FinalShutdown() {
1450   std::promise<void> promise;
1451   auto future = promise.get_future();
1452   GetAclManager()->UnregisterCallbacks(this, std::move(promise));
1453   future.wait();
1454   LOG_DEBUG("Unregistered classic callbacks from gd acl manager");
1455 
1456   promise = std::promise<void>();
1457   future = promise.get_future();
1458   GetAclManager()->UnregisterLeCallbacks(this, std::move(promise));
1459   future.wait();
1460   LOG_DEBUG("Unregistered le callbacks from gd acl manager");
1461 
1462   promise = std::promise<void>();
1463   future = promise.get_future();
1464   handler_->CallOn(pimpl_.get(), &Acl::impl::FinalShutdown, std::move(promise));
1465   future.wait();
1466   LOG_INFO("Unregistered and cleared any orphaned ACL connections");
1467 }
1468 
ClearAcceptList()1469 void shim::legacy::Acl::ClearAcceptList() {
1470   handler_->CallOn(pimpl_.get(), &Acl::impl::clear_acceptlist);
1471 }
1472