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