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 "common/bidi_queue.h"
24 #include "common/callback.h"
25 #include "hci/acl_manager/connection_callbacks.h"
26 #include "hci/acl_manager/le_connection_callbacks.h"
27 #include "hci/address.h"
28 #include "hci/address_with_type.h"
29 #include "hci/hci_layer.h"
30 #include "hci/hci_packets.h"
31 #include "hci/le_address_manager.h"
32 #include "module.h"
33 #include "os/handler.h"
34 
35 namespace bluetooth {
36 
37 namespace security {
38 class SecurityModule;
39 }
40 namespace shim {
41 namespace legacy {
42 class Acl;
43 }
44 
45 class Btm;
46 void L2CA_UseLegacySecurityModule();
47 bool L2CA_SetAclPriority(uint16_t, bool);
48 }
49 
50 namespace hci {
51 
52 class AclManager : public Module {
53  friend class bluetooth::shim::Btm;
54  friend class bluetooth::shim::legacy::Acl;
55  friend void bluetooth::shim::L2CA_UseLegacySecurityModule();
56  friend bool bluetooth::shim::L2CA_SetAclPriority(uint16_t, bool);
57 
58 public:
59  AclManager();
60  // NOTE: It is necessary to forward declare a default destructor that overrides the base class one, because
61  // "struct impl" is forwarded declared in .cc and compiler needs a concrete definition of "struct impl" when
62  // compiling AclManager's destructor. Hence we need to forward declare the destructor for AclManager to delay
63  // compiling AclManager's destructor until 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(acl_manager::ConnectionCallbacks* callbacks, std::promise<void> promise);
70 
71  // Should register only once when user module starts.
72  virtual void RegisterLeCallbacks(acl_manager::LeConnectionCallbacks* callbacks, os::Handler* handler);
73  virtual void UnregisterLeCallbacks(acl_manager::LeConnectionCallbacks* callbacks, std::promise<void> promise);
74 
75  // Generates OnConnectSuccess if connected, or OnConnectFail otherwise
76  virtual void CreateConnection(Address address);
77 
78  // Generates OnLeConnectSuccess if connected, or OnLeConnectFail otherwise
79  virtual void CreateLeConnection(AddressWithType address_with_type, bool is_direct);
80 
81  // Ask the controller for specific data parameters
82  virtual void SetLeSuggestedDefaultDataParameters(uint16_t octets, uint16_t time);
83 
84  virtual void SetPrivacyPolicyForInitiatorAddress(
85      LeAddressManager::AddressPolicy address_policy,
86      AddressWithType fixed_address,
87      std::chrono::milliseconds minimum_rotation_time,
88      std::chrono::milliseconds maximum_rotation_time);
89 
90  // TODO(jpawlowski): remove once we have config file abstraction in cert tests
91  virtual void SetPrivacyPolicyForInitiatorAddressForTest(
92      LeAddressManager::AddressPolicy address_policy,
93      AddressWithType fixed_address,
94      crypto_toolbox::Octet16 rotation_irk,
95      std::chrono::milliseconds minimum_rotation_time,
96      std::chrono::milliseconds maximum_rotation_time);
97 
98  // Generates OnConnectFail with error code "terminated by local host 0x16" if cancelled, or OnConnectSuccess if not
99  // successfully cancelled and already connected
100  virtual void CancelConnect(Address address);
101 
102  virtual void CancelLeConnect(AddressWithType address_with_type);
103  virtual void AddDeviceToConnectList(AddressWithType address_with_type);
104  virtual void AddDeviceToResolvingList(
105      AddressWithType address_with_type,
106      const std::array<uint8_t, 16>& peer_irk,
107      const std::array<uint8_t, 16>& local_irk);
108  virtual void RemoveDeviceFromConnectList(AddressWithType address_with_type);
109  virtual void RemoveDeviceFromResolvingList(AddressWithType address_with_type);
110 
111  virtual void CentralLinkKey(KeyFlag key_flag);
112  virtual void SwitchRole(Address address, Role role);
113  virtual uint16_t ReadDefaultLinkPolicySettings();
114  virtual void WriteDefaultLinkPolicySettings(uint16_t default_link_policy_settings);
115 
116  // Callback from Advertising Manager to notify the advitiser (local) address
117  virtual void OnAdvertisingSetTerminated(ErrorCode status, uint16_t conn_handle, hci::AddressWithType adv_address);
118 
119  // In order to avoid circular dependency use setter rather than module dependency.
120  virtual void SetSecurityModule(security::SecurityModule* security_module);
121 
122  virtual LeAddressManager* GetLeAddressManager();
123 
124  static const ModuleFactory Factory;
125 
126 protected:
127  void ListDependencies(ModuleList* list) override;
128 
129  void Start() override;
130 
131  void Stop() override;
132 
133  std::string ToString() const override;
134 
135  DumpsysDataFinisher GetDumpsysData(flatbuffers::FlatBufferBuilder* builder) const override;  // Module
136 
137 private:
138  virtual uint16_t HACK_GetHandle(const Address address);
139  virtual uint16_t HACK_GetLeHandle(const Address address);
140 
141  // Hack for the shim to get SCO disconnect callback.  Shim needs to post to their handler!
142  virtual void HACK_SetScoDisconnectCallback(std::function<void(uint16_t /* handle */, uint8_t /* reason */)>);
143 
144  virtual void HACK_SetAclTxPriority(uint8_t handle, bool high_priority);
145 
146  struct impl;
147  std::unique_ptr<impl> pimpl_;
148 
149  DISALLOW_COPY_AND_ASSIGN(AclManager);
150 };
151 
152 }  // namespace hci
153 }  // namespace bluetooth
154