• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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   virtual void SetPrivacyPolicyForInitiatorAddress(LeAddressManager::AddressPolicy address_policy,
90                                                    AddressWithType fixed_address,
91                                                    std::chrono::milliseconds minimum_rotation_time,
92                                                    std::chrono::milliseconds maximum_rotation_time);
93 
94   // TODO(jpawlowski): remove once we have config file abstraction in cert tests
95   virtual void SetPrivacyPolicyForInitiatorAddressForTest(
96           LeAddressManager::AddressPolicy address_policy, AddressWithType fixed_address,
97           Octet16 rotation_irk, std::chrono::milliseconds minimum_rotation_time,
98           std::chrono::milliseconds maximum_rotation_time);
99 
100   // Generates OnConnectFail with error code "terminated by local host 0x16" if
101   // cancelled, or OnConnectSuccess if not successfully cancelled and already
102   // connected
103   virtual void CancelConnect(Address address);
104   virtual void RemoveFromBackgroundList(AddressWithType address_with_type);
105 
106   virtual void CancelLeConnect(AddressWithType address_with_type);
107 
108   virtual void ClearFilterAcceptList();
109 
110   virtual void AddDeviceToResolvingList(AddressWithType address_with_type,
111                                         const std::array<uint8_t, 16>& peer_irk,
112                                         const std::array<uint8_t, 16>& local_irk);
113   virtual void RemoveDeviceFromResolvingList(AddressWithType address_with_type);
114   virtual void ClearResolvingList();
115 
116   virtual void CentralLinkKey(KeyFlag key_flag);
117   virtual void SwitchRole(Address address, Role role);
118   virtual uint16_t ReadDefaultLinkPolicySettings();
119   virtual void WriteDefaultLinkPolicySettings(uint16_t default_link_policy_settings);
120 
121   // Callback from Advertising Manager to notify the advitiser (local) address
122   virtual void OnAdvertisingSetTerminated(ErrorCode status, uint16_t conn_handle,
123                                           uint8_t adv_set_id, hci::AddressWithType adv_address,
124                                           bool is_discoverable);
125 
126   virtual LeAddressManager* GetLeAddressManager();
127 
128   // Virtual ACL disconnect emitted during suspend.
129   virtual void OnClassicSuspendInitiatedDisconnect(uint16_t handle, ErrorCode reason);
130   virtual void OnLeSuspendInitiatedDisconnect(uint16_t handle, ErrorCode reason);
131   virtual void SetSystemSuspendState(bool suspended);
132   virtual void AddDeviceToRelaxedConnectionIntervalList(const Address address);
133 
134   static const ModuleFactory Factory;
135 
136 protected:
137   void ListDependencies(ModuleList* list) const override;
138 
139   void Start() override;
140   void Stop() override;
141 
142   std::string ToString() const override;
143 
144 private:
145   virtual uint16_t HACK_GetHandle(const Address address);
146   virtual uint16_t HACK_GetLeHandle(const Address address);
147   virtual Address HACK_GetLeAddress(uint16_t connection_handle);
148 
149   virtual void HACK_SetAclTxPriority(uint8_t handle, bool high_priority);
150 
151   struct impl;
152   std::unique_ptr<impl> pimpl_;
153 };
154 
155 }  // namespace hci
156 }  // namespace bluetooth
157