• 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 #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 #include "types/raw_address.h"
34 
35 using LeRandCallback = base::Callback<void(uint64_t)>;
36 
37 namespace bluetooth {
38 namespace shim {
39 namespace legacy {
40 
41 class Acl : public hci::acl_manager::ConnectionCallbacks,
42             public hci::acl_manager::LeConnectionCallbacks,
43             public LinkConnectionInterface,
44             public LinkPolicyInterface {
45  public:
46   Acl(os::Handler* handler, const acl_interface_t& acl_interface,
47       uint8_t max_acceptlist_size, uint8_t max_address_resolution_size);
48 
49   Acl(const Acl&) = delete;
50   Acl& operator=(const Acl&) = delete;
51 
52   ~Acl();
53 
54   // hci::acl_manager::ConnectionCallbacks
55   void OnConnectSuccess(
56       std::unique_ptr<hci::acl_manager::ClassicAclConnection>) override;
57   void OnConnectRequest(hci::Address, hci::ClassOfDevice) override;
58   void OnConnectFail(hci::Address, hci::ErrorCode reason,
59                      bool locally_initiated) override;
60 
61   void HACK_OnEscoConnectRequest(hci::Address, hci::ClassOfDevice) override;
62   void HACK_OnScoConnectRequest(hci::Address, hci::ClassOfDevice) override;
63 
64   void OnClassicLinkDisconnected(uint16_t handle, hci::ErrorCode reason);
65 
66   // hci::acl_manager::LeConnectionCallbacks
67   void OnLeConnectSuccess(
68       hci::AddressWithType,
69       std::unique_ptr<hci::acl_manager::LeAclConnection>) override;
70   void OnLeConnectFail(hci::AddressWithType, hci::ErrorCode reason) override;
71   void OnLeLinkDisconnected(uint16_t handle, hci::ErrorCode reason);
72   bluetooth::hci::AddressWithType GetConnectionLocalAddress(
73       const RawAddress& remote_bda);
74   std::optional<uint8_t> GetAdvertisingSetConnectedTo(
75       const RawAddress& remote_bda);
76 
77   // LinkConnectionInterface
78   void CreateClassicConnection(const hci::Address& address) override;
79   void CancelClassicConnection(const hci::Address& address) override;
80   void AcceptLeConnectionFrom(const hci::AddressWithType& address_with_type,
81                               bool is_direct,
82                               std::promise<bool> promise) override;
83   void IgnoreLeConnectionFrom(
84       const hci::AddressWithType& address_with_type) override;
85   void DisconnectClassic(uint16_t handle, tHCI_REASON reason,
86                          std::string comment) override;
87   void DisconnectLe(uint16_t handle, tHCI_REASON reason,
88                     std::string comment) override;
89 
90   // Address Resolution List
91   void AddToAddressResolution(const hci::AddressWithType& address_with_type,
92                               const std::array<uint8_t, 16>& peer_irk,
93                               const std::array<uint8_t, 16>& local_irk);
94   void RemoveFromAddressResolution(
95       const hci::AddressWithType& address_with_type);
96   void ClearAddressResolution();
97 
98   // LinkPolicyInterface
99   bool HoldMode(uint16_t hci_handle, uint16_t max_interval,
100                 uint16_t min_interval) override;
101   bool SniffMode(uint16_t hci_handle, uint16_t max_interval,
102                  uint16_t min_interval, uint16_t attempt,
103                  uint16_t timeout) override;
104   bool ExitSniffMode(uint16_t hci_handle) override;
105   bool SniffSubrating(uint16_t hci_handle, uint16_t maximum_latency,
106                       uint16_t minimum_remote_timeout,
107                       uint16_t minimum_local_timeout) override;
108   void LeSetDefaultSubrate(uint16_t subrate_min, uint16_t subrate_max,
109                            uint16_t max_latency, uint16_t cont_num,
110                            uint16_t sup_tout);
111   void LeSubrateRequest(uint16_t hci_handle, uint16_t subrate_min,
112                         uint16_t subrate_max, uint16_t max_latency,
113                         uint16_t cont_num, uint16_t sup_tout);
114 
115   void WriteData(uint16_t hci_handle,
116                  std::unique_ptr<packet::RawBuilder> packet);
117 
118   void Dump(int fd) const;
119   void DumpConnectionHistory(int fd) const;
120 
121   void Shutdown();
122   void FinalShutdown();
123 
124   void ClearFilterAcceptList();
125   void DisconnectAllForSuspend();
126   void LeRand(LeRandCallback cb);
127   void SetSystemSuspendState(bool suspended);
128 
129  protected:
130   void on_incoming_acl_credits(uint16_t handle, uint16_t credits);
131   void write_data_sync(uint16_t hci_handle,
132                        std::unique_ptr<packet::RawBuilder> packet);
133 
134  private:
135   os::Handler* handler_;
136   const acl_interface_t acl_interface_;
137 
138   bool CheckForOrphanedAclConnections() const;
139 
140   struct impl;
141   std::unique_ptr<impl> pimpl_;
142 };
143 
144 }  // namespace legacy
145 }  // namespace shim
146 }  // namespace bluetooth
147