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   void Dump(int fd) const;
67 
68   // Should register only once when user module starts.
69   // Generates OnConnectSuccess when an incoming connection is established.
70   virtual void RegisterCallbacks(acl_manager::ConnectionCallbacks* callbacks, os::Handler* handler);
71   virtual void UnregisterCallbacks(acl_manager::ConnectionCallbacks* callbacks,
72                                    std::promise<void> promise);
73 
74   // Should register only once when user module starts.
75   virtual void RegisterLeCallbacks(acl_manager::LeConnectionCallbacks* callbacks,
76                                    os::Handler* handler);
77   virtual void UnregisterLeCallbacks(acl_manager::LeConnectionCallbacks* callbacks,
78                                      std::promise<void> promise);
79   void RegisterLeAcceptlistCallbacks(acl_manager::LeAcceptlistCallbacks* callbacks);
80   void UnregisterLeAcceptlistCallbacks(acl_manager::LeAcceptlistCallbacks* callbacks,
81                                        std::promise<void> promise);
82 
83   // Generates OnConnectSuccess if connected, or OnConnectFail otherwise
84   virtual void CreateConnection(Address address);
85 
86   // Generates OnLeConnectSuccess if connected, or OnLeConnectFail otherwise
87   virtual void CreateLeConnection(AddressWithType address_with_type, bool is_direct);
88 
89   // Ask the controller for specific data parameters
90   virtual void SetLeSuggestedDefaultDataParameters(uint16_t octets, uint16_t time);
91 
92   virtual void LeSetDefaultSubrate(uint16_t subrate_min, uint16_t subrate_max, uint16_t max_latency,
93                                    uint16_t cont_num, uint16_t sup_tout);
94 
95   virtual void SetPrivacyPolicyForInitiatorAddress(LeAddressManager::AddressPolicy address_policy,
96                                                    AddressWithType fixed_address,
97                                                    std::chrono::milliseconds minimum_rotation_time,
98                                                    std::chrono::milliseconds maximum_rotation_time);
99 
100   // TODO(jpawlowski): remove once we have config file abstraction in cert tests
101   virtual void SetPrivacyPolicyForInitiatorAddressForTest(
102           LeAddressManager::AddressPolicy address_policy, AddressWithType fixed_address,
103           Octet16 rotation_irk, std::chrono::milliseconds minimum_rotation_time,
104           std::chrono::milliseconds maximum_rotation_time);
105 
106   // Generates OnConnectFail with error code "terminated by local host 0x16" if
107   // cancelled, or OnConnectSuccess if not successfully cancelled and already
108   // connected
109   virtual void CancelConnect(Address address);
110   virtual void RemoveFromBackgroundList(AddressWithType address_with_type);
111   virtual void IsOnBackgroundList(AddressWithType address_with_type, std::promise<bool> promise);
112 
113   virtual void CancelLeConnect(AddressWithType address_with_type);
114 
115   virtual void ClearFilterAcceptList();
116 
117   virtual void AddDeviceToResolvingList(AddressWithType address_with_type,
118                                         const std::array<uint8_t, 16>& peer_irk,
119                                         const std::array<uint8_t, 16>& local_irk);
120   virtual void RemoveDeviceFromResolvingList(AddressWithType address_with_type);
121   virtual void ClearResolvingList();
122 
123   virtual void CentralLinkKey(KeyFlag key_flag);
124   virtual void SwitchRole(Address address, Role role);
125   virtual uint16_t ReadDefaultLinkPolicySettings();
126   virtual void WriteDefaultLinkPolicySettings(uint16_t default_link_policy_settings);
127 
128   // Callback from Advertising Manager to notify the advitiser (local) address
129   virtual void OnAdvertisingSetTerminated(ErrorCode status, uint16_t conn_handle,
130                                           uint8_t adv_set_id, hci::AddressWithType adv_address,
131                                           bool is_discoverable);
132 
133   virtual LeAddressManager* GetLeAddressManager();
134 
135   // Virtual ACL disconnect emitted during suspend.
136   virtual void OnClassicSuspendInitiatedDisconnect(uint16_t handle, ErrorCode reason);
137   virtual void OnLeSuspendInitiatedDisconnect(uint16_t handle, ErrorCode reason);
138   virtual void SetSystemSuspendState(bool suspended);
139 
140   static const ModuleFactory Factory;
141 
142 protected:
143   void ListDependencies(ModuleList* list) const override;
144 
145   void Start() override;
146   void Stop() override;
147 
148   std::string ToString() const override;
149 
150 private:
151   virtual uint16_t HACK_GetHandle(const Address address);
152   virtual uint16_t HACK_GetLeHandle(const Address address);
153   virtual Address HACK_GetLeAddress(uint16_t connection_handle);
154 
155   virtual void HACK_SetAclTxPriority(uint8_t handle, bool high_priority);
156 
157   struct impl;
158   std::unique_ptr<impl> pimpl_;
159 };
160 
161 }  // namespace hci
162 }  // namespace bluetooth
163