• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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 <functional>
20 #include <future>
21 #include <memory>
22 
23 #include "hci/acl_manager/connection_callbacks.h"
24 #include "hci/acl_manager/le_acceptlist_callbacks.h"
25 #include "hci/acl_manager/le_connection_callbacks.h"
26 #include "hci/address.h"
27 #include "hci/address_with_type.h"
28 #include "hci/distance_measurement_manager.h"
29 #include "hci/hci_packets.h"
30 #include "hci/le_address_manager.h"
31 #include "hci/le_scanning_manager.h"
32 #include "module.h"
33 #include "os/handler.h"
34 
35 namespace bluetooth {
36 namespace shim {
37 namespace legacy {
38 class Acl;
39 }  // namespace legacy
40 
41 class Btm;
42 bool L2CA_SetAclPriority(uint16_t, bool);
43 }  // namespace shim
44 
45 namespace hci {
46 
47 class AclManager : public Module {
48   friend class bluetooth::shim::legacy::Acl;
49   friend bool bluetooth::shim::L2CA_SetAclPriority(uint16_t, bool);
50   friend class bluetooth::hci::LeScanningManager;
51   friend class bluetooth::hci::DistanceMeasurementManager;
52 
53  public:
54   AclManager();
55   AclManager(const AclManager&) = delete;
56   AclManager& operator=(const AclManager&) = delete;
57 
58   // NOTE: It is necessary to forward declare a default destructor that
59   // overrides the base class one, because "struct impl" is forwarded declared
60   // in .cc and compiler needs a concrete definition of "struct impl" when
61   // compiling AclManager's destructor. Hence we need to forward declare the
62   // destructor for AclManager to delay compiling AclManager's destructor until
63   // it starts linking the .cc file.
64   ~AclManager();
65 
66   // Should register only once when user module starts.
67   // Generates OnConnectSuccess when an incoming connection is established.
68   virtual void RegisterCallbacks(acl_manager::ConnectionCallbacks* callbacks, os::Handler* handler);
69   virtual void UnregisterCallbacks(
70       acl_manager::ConnectionCallbacks* callbacks, std::promise<void> promise);
71 
72   // Should register only once when user module starts.
73   virtual void RegisterLeCallbacks(
74       acl_manager::LeConnectionCallbacks* callbacks, os::Handler* handler);
75   virtual void UnregisterLeCallbacks(
76       acl_manager::LeConnectionCallbacks* callbacks, std::promise<void> promise);
77   void RegisterLeAcceptlistCallbacks(acl_manager::LeAcceptlistCallbacks* callbacks);
78   void UnregisterLeAcceptlistCallbacks(
79       acl_manager::LeAcceptlistCallbacks* callbacks, std::promise<void> promise);
80 
81   // Generates OnConnectSuccess if connected, or OnConnectFail otherwise
82   virtual void CreateConnection(Address address);
83 
84   // Generates OnLeConnectSuccess if connected, or OnLeConnectFail otherwise
85   virtual void CreateLeConnection(AddressWithType address_with_type, bool is_direct);
86 
87   // Ask the controller for specific data parameters
88   virtual void SetLeSuggestedDefaultDataParameters(uint16_t octets, uint16_t time);
89 
90   virtual void LeSetDefaultSubrate(
91       uint16_t subrate_min,
92       uint16_t subrate_max,
93       uint16_t max_latency,
94       uint16_t cont_num,
95       uint16_t sup_tout);
96 
97   virtual void SetPrivacyPolicyForInitiatorAddress(
98       LeAddressManager::AddressPolicy address_policy,
99       AddressWithType fixed_address,
100       std::chrono::milliseconds minimum_rotation_time,
101       std::chrono::milliseconds maximum_rotation_time);
102 
103   // TODO(jpawlowski): remove once we have config file abstraction in cert tests
104   virtual void SetPrivacyPolicyForInitiatorAddressForTest(
105       LeAddressManager::AddressPolicy address_policy,
106       AddressWithType fixed_address,
107       Octet16 rotation_irk,
108       std::chrono::milliseconds minimum_rotation_time,
109       std::chrono::milliseconds maximum_rotation_time);
110 
111   // Generates OnConnectFail with error code "terminated by local host 0x16" if
112   // cancelled, or OnConnectSuccess if not successfully cancelled and already
113   // connected
114   virtual void CancelConnect(Address address);
115   virtual void RemoveFromBackgroundList(AddressWithType address_with_type);
116   virtual void IsOnBackgroundList(AddressWithType address_with_type, std::promise<bool> promise);
117 
118   virtual void CancelLeConnect(AddressWithType address_with_type);
119 
120   virtual void ClearFilterAcceptList();
121 
122   virtual void AddDeviceToResolvingList(
123       AddressWithType address_with_type,
124       const std::array<uint8_t, 16>& peer_irk,
125       const std::array<uint8_t, 16>& local_irk);
126   virtual void RemoveDeviceFromResolvingList(AddressWithType address_with_type);
127   virtual void ClearResolvingList();
128 
129   virtual void CentralLinkKey(KeyFlag key_flag);
130   virtual void SwitchRole(Address address, Role role);
131   virtual uint16_t ReadDefaultLinkPolicySettings();
132   virtual void WriteDefaultLinkPolicySettings(uint16_t default_link_policy_settings);
133 
134   // Callback from Advertising Manager to notify the advitiser (local) address
135   virtual void OnAdvertisingSetTerminated(
136       ErrorCode status,
137       uint16_t conn_handle,
138       uint8_t adv_set_id,
139       hci::AddressWithType adv_address,
140       bool is_discoverable);
141 
142   virtual LeAddressManager* GetLeAddressManager();
143 
144   // Virtual ACL disconnect emitted during suspend.
145   virtual void OnClassicSuspendInitiatedDisconnect(uint16_t handle, ErrorCode reason);
146   virtual void OnLeSuspendInitiatedDisconnect(uint16_t handle, ErrorCode reason);
147   virtual void SetSystemSuspendState(bool suspended);
148 
149   static const ModuleFactory Factory;
150 
151  protected:
152   void ListDependencies(ModuleList* list) const override;
153 
154   void Start() override;
155 
156   void Stop() override;
157 
158   std::string ToString() const override;
159 
160   DumpsysDataFinisher GetDumpsysData(
161       flatbuffers::FlatBufferBuilder* builder) const override;  // Module
162 
163  private:
164   virtual uint16_t HACK_GetHandle(const Address address);
165   virtual uint16_t HACK_GetLeHandle(const Address address);
166   virtual Address HACK_GetLeAddress(uint16_t connection_handle);
167 
168   virtual void HACK_SetAclTxPriority(uint8_t handle, bool high_priority);
169 
170   struct impl;
171   std::unique_ptr<impl> pimpl_;
172 };
173 
174 }  // namespace hci
175 }  // namespace bluetooth
176