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 "hci/acl_manager/connection_callbacks.h"
23 #include "hci/acl_manager/le_connection_callbacks.h"
24 #include "hci/address.h"
25 #include "hci/address_with_type.h"
26 #include "hci/class_of_device.h"
27 #include "main/shim/acl_legacy_interface.h"
28 #include "main/shim/link_connection_interface.h"
29 #include "os/handler.h"
30 #include "packet/raw_builder.h"
31 #include "types/raw_address.h"
32 
33 namespace bluetooth {
34 namespace shim {
35 namespace legacy {
36 
37 class Acl : public hci::acl_manager::ConnectionCallbacks,
38             public hci::acl_manager::LeConnectionCallbacks,
39             public LinkConnectionInterface {
40  public:
41   Acl(os::Handler* handler, const acl_interface_t& acl_interface,
42       uint8_t max_acceptlist_size, uint8_t max_address_resolution_size);
43 
44   Acl(const Acl&) = delete;
45   Acl& operator=(const Acl&) = delete;
46 
47   ~Acl();
48 
49   // hci::acl_manager::ConnectionCallbacks
50   void OnConnectSuccess(
51       std::unique_ptr<hci::acl_manager::ClassicAclConnection>) override;
52   void OnConnectRequest(hci::Address, hci::ClassOfDevice) override;
53   void OnConnectFail(hci::Address, hci::ErrorCode reason,
54                      bool locally_initiated) override;
55 
56   void OnClassicLinkDisconnected(uint16_t handle, hci::ErrorCode reason);
57 
58   // hci::acl_manager::LeConnectionCallbacks
59   void OnLeConnectSuccess(
60       hci::AddressWithType,
61       std::unique_ptr<hci::acl_manager::LeAclConnection>) override;
62   void OnLeConnectFail(hci::AddressWithType, hci::ErrorCode reason) override;
63   void OnLeLinkDisconnected(uint16_t handle, hci::ErrorCode reason);
64   bluetooth::hci::AddressWithType GetConnectionLocalAddress(uint16_t handle,
65                                                             bool ota_address);
66   bluetooth::hci::AddressWithType GetConnectionPeerAddress(uint16_t handle,
67                                                            bool ota_address);
68   std::optional<uint8_t> GetAdvertisingSetConnectedTo(
69       const RawAddress& remote_bda);
70 
71   // LinkConnectionInterface
72   void CreateClassicConnection(const hci::Address& address) override;
73   void CancelClassicConnection(const hci::Address& address) override;
74   void AcceptLeConnectionFrom(const hci::AddressWithType& address_with_type,
75                               bool is_direct,
76                               std::promise<bool> promise) override;
77   void IgnoreLeConnectionFrom(
78       const hci::AddressWithType& address_with_type) override;
79   void DisconnectClassic(uint16_t handle, tHCI_REASON reason,
80                          std::string comment) override;
81   void DisconnectLe(uint16_t handle, tHCI_REASON reason,
82                     std::string comment) override;
83   void UpdateConnectionParameters(uint16_t handle, uint16_t conn_int_min,
84                                   uint16_t conn_int_max, uint16_t conn_latency,
85                                   uint16_t conn_timeout, uint16_t min_ce_len,
86                                   uint16_t max_ce_len) override;
87 
88   // Address Resolution List
89   void AddToAddressResolution(const hci::AddressWithType& address_with_type,
90                               const std::array<uint8_t, 16>& peer_irk,
91                               const std::array<uint8_t, 16>& local_irk);
92   void RemoveFromAddressResolution(
93       const hci::AddressWithType& address_with_type);
94   void ClearAddressResolution();
95 
96   void LeSetDefaultSubrate(uint16_t subrate_min, uint16_t subrate_max,
97                            uint16_t max_latency, uint16_t cont_num,
98                            uint16_t sup_tout);
99   void LeSubrateRequest(uint16_t hci_handle, uint16_t subrate_min,
100                         uint16_t subrate_max, uint16_t max_latency,
101                         uint16_t cont_num, uint16_t sup_tout);
102 
103   void WriteData(uint16_t hci_handle,
104                  std::unique_ptr<packet::RawBuilder> packet);
105 
106   void Flush(uint16_t hci_handle);
107 
108   void Dump(int fd) const;
109   void DumpConnectionHistory(int fd) const;
110 
111   void Shutdown();
112   void FinalShutdown();
113 
114   void ClearFilterAcceptList();
115   void DisconnectAllForSuspend();
116   void SetSystemSuspendState(bool suspended);
117 
118  protected:
119   void on_incoming_acl_credits(uint16_t handle, uint16_t credits);
120   void write_data_sync(uint16_t hci_handle,
121                        std::unique_ptr<packet::RawBuilder> packet);
122   void flush(uint16_t hci_handle);
123 
124  private:
125   os::Handler* handler_;
126   const acl_interface_t acl_interface_;
127 
128   bool CheckForOrphanedAclConnections() const;
129 
130   struct impl;
131   std::unique_ptr<impl> pimpl_;
132 };
133 
134 }  // namespace legacy
135 }  // namespace shim
136 }  // namespace bluetooth
137