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 #pragma once
18 
19 #include <future>
20 #include <memory>
21 
22 #include "gd/hci/acl_manager/connection_callbacks.h"
23 #include "gd/hci/acl_manager/le_connection_callbacks.h"
24 #include "gd/hci/address.h"
25 #include "gd/hci/address_with_type.h"
26 #include "gd/hci/class_of_device.h"
27 #include "gd/os/handler.h"
28 #include "gd/packet/raw_builder.h"
29 #include "main/shim/acl_legacy_interface.h"
30 #include "main/shim/link_connection_interface.h"
31 #include "main/shim/link_policy_interface.h"
32 #include "stack/include/bt_types.h"
33 
34 namespace bluetooth {
35 namespace shim {
36 namespace legacy {
37 
38 class Acl : public hci::acl_manager::ConnectionCallbacks,
39             public hci::acl_manager::LeConnectionCallbacks,
40             public LinkConnectionInterface,
41             public LinkPolicyInterface {
42  public:
43   Acl(os::Handler* handler, const acl_interface_t& acl_interface,
44       uint8_t max_acceptlist_size);
45   ~Acl();
46 
47   // hci::acl_manager::ConnectionCallbacks
48   void OnConnectSuccess(
49       std::unique_ptr<hci::acl_manager::ClassicAclConnection>) override;
50   void OnConnectFail(hci::Address, hci::ErrorCode reason) override;
51 
52   void HACK_OnEscoConnectRequest(hci::Address, hci::ClassOfDevice) override;
53   void HACK_OnScoConnectRequest(hci::Address, hci::ClassOfDevice) override;
54 
55   void OnClassicLinkDisconnected(uint16_t handle, hci::ErrorCode reason);
56 
57   // hci::acl_manager::LeConnectionCallbacks
58   void OnLeConnectSuccess(
59       hci::AddressWithType,
60       std::unique_ptr<hci::acl_manager::LeAclConnection>) override;
61   void OnLeConnectFail(hci::AddressWithType, hci::ErrorCode reason) override;
62   void OnLeLinkDisconnected(uint16_t handle, hci::ErrorCode reason);
63   bluetooth::hci::AddressWithType GetConnectionLocalAddress(
64       const RawAddress& remote_bda);
65 
66   // LinkConnectionInterface
67   void CreateClassicConnection(const hci::Address& address) override;
68   void CancelClassicConnection(const hci::Address& address) override;
69   void AcceptLeConnectionFrom(const hci::AddressWithType& address_with_type,
70                               bool is_direct,
71                               std::promise<bool> promise) override;
72   void IgnoreLeConnectionFrom(
73       const hci::AddressWithType& address_with_type) override;
74   void DisconnectClassic(uint16_t handle, tHCI_REASON reason) override;
75   void DisconnectLe(uint16_t handle, tHCI_REASON reason) override;
76 
77   // LinkPolicyInterface
78   bool HoldMode(uint16_t hci_handle, uint16_t max_interval,
79                 uint16_t min_interval) override;
80   bool SniffMode(uint16_t hci_handle, uint16_t max_interval,
81                  uint16_t min_interval, uint16_t attempt,
82                  uint16_t timeout) override;
83   bool ExitSniffMode(uint16_t hci_handle) override;
84   bool SniffSubrating(uint16_t hci_handle, uint16_t maximum_latency,
85                       uint16_t minimum_remote_timeout,
86                       uint16_t minimum_local_timeout) override;
87 
88   void HACK_OnScoDisconnected(uint16_t handle, uint8_t reason);
89 
90   void WriteData(uint16_t hci_handle,
91                  std::unique_ptr<packet::RawBuilder> packet);
92 
93   void ConfigureLePrivacy(bool is_le_privacy_enabled);
94 
95   void Dump(int fd) const;
96   void DumpConnectionHistory(int fd) const;
97 
98   void Shutdown();
99   void FinalShutdown();
100 
101   void ClearAcceptList();
102 
103  protected:
104   void on_incoming_acl_credits(uint16_t handle, uint16_t credits);
105   void write_data_sync(uint16_t hci_handle,
106                        std::unique_ptr<packet::RawBuilder> packet);
107 
108  private:
109   os::Handler* handler_;
110   const acl_interface_t acl_interface_;
111 
112   bool CheckForOrphanedAclConnections() const;
113 
114   struct impl;
115   std::unique_ptr<impl> pimpl_;
116   DISALLOW_COPY_AND_ASSIGN(Acl);
117 };
118 
119 }  // namespace legacy
120 }  // namespace shim
121 }  // namespace bluetooth
122