summaryrefslogtreecommitdiff
path: root/system/gd/hci/acl_manager.h
blob: 4f9aaf7db5918142a65a79c29aa8e5a0a93bc0bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <functional>
#include <future>
#include <memory>

#include "common/bidi_queue.h"
#include "common/callback.h"
#include "hci/acl_manager/connection_callbacks.h"
#include "hci/acl_manager/le_connection_callbacks.h"
#include "hci/address.h"
#include "hci/address_with_type.h"
#include "hci/hci_layer.h"
#include "hci/hci_packets.h"
#include "hci/le_address_manager.h"
#include "hci/le_scanning_manager.h"
#include "module.h"
#include "os/handler.h"

namespace bluetooth {

namespace security {
class SecurityModule;
}
namespace shim {
namespace legacy {
class Acl;
}

class Btm;
void L2CA_UseLegacySecurityModule();
bool L2CA_SetAclPriority(uint16_t, bool);
}

namespace hci {

class AclManager : public Module {
 friend class bluetooth::shim::Btm;
 friend class bluetooth::shim::legacy::Acl;
 friend void bluetooth::shim::L2CA_UseLegacySecurityModule();
 friend bool bluetooth::shim::L2CA_SetAclPriority(uint16_t, bool);
 friend class bluetooth::hci::LeScanningManager;

public:
 AclManager();
 AclManager(const AclManager&) = delete;
 AclManager& operator=(const AclManager&) = delete;

 // NOTE: It is necessary to forward declare a default destructor that overrides the base class one, because
 // "struct impl" is forwarded declared in .cc and compiler needs a concrete definition of "struct impl" when
 // compiling AclManager's destructor. Hence we need to forward declare the destructor for AclManager to delay
 // compiling AclManager's destructor until it starts linking the .cc file.
 ~AclManager();

 // Should register only once when user module starts.
 // Generates OnConnectSuccess when an incoming connection is established.
 virtual void RegisterCallbacks(acl_manager::ConnectionCallbacks* callbacks, os::Handler* handler);
 virtual void UnregisterCallbacks(acl_manager::ConnectionCallbacks* callbacks, std::promise<void> promise);

 // Should register only once when user module starts.
 virtual void RegisterLeCallbacks(acl_manager::LeConnectionCallbacks* callbacks, os::Handler* handler);
 virtual void UnregisterLeCallbacks(acl_manager::LeConnectionCallbacks* callbacks, std::promise<void> promise);

 // Generates OnConnectSuccess if connected, or OnConnectFail otherwise
 virtual void CreateConnection(Address address);

 // Generates OnLeConnectSuccess if connected, or OnLeConnectFail otherwise
 virtual void CreateLeConnection(AddressWithType address_with_type, bool is_direct);

 // Ask the controller for specific data parameters
 virtual void SetLeSuggestedDefaultDataParameters(uint16_t octets, uint16_t time);

 virtual void SetPrivacyPolicyForInitiatorAddress(
     LeAddressManager::AddressPolicy address_policy,
     AddressWithType fixed_address,
     std::chrono::milliseconds minimum_rotation_time,
     std::chrono::milliseconds maximum_rotation_time);

 // TODO(jpawlowski): remove once we have config file abstraction in cert tests
 virtual void SetPrivacyPolicyForInitiatorAddressForTest(
     LeAddressManager::AddressPolicy address_policy,
     AddressWithType fixed_address,
     crypto_toolbox::Octet16 rotation_irk,
     std::chrono::milliseconds minimum_rotation_time,
     std::chrono::milliseconds maximum_rotation_time);

 // Generates OnConnectFail with error code "terminated by local host 0x16" if cancelled, or OnConnectSuccess if not
 // successfully cancelled and already connected
 virtual void CancelConnect(Address address);
 virtual void RemoveFromBackgroundList(AddressWithType address_with_type);
 virtual void IsOnBackgroundList(AddressWithType address_with_type, std::promise<bool> promise);

 virtual void CancelLeConnect(AddressWithType address_with_type);
 virtual void CancelLeConnectAndRemoveFromBackgroundList(AddressWithType address_with_type);

 virtual void AddDeviceToFilterAcceptList(AddressWithType address_with_type);
 virtual void RemoveDeviceFromFilterAcceptList(AddressWithType address_with_type);
 virtual void ClearFilterAcceptList();

 virtual void AddDeviceToResolvingList(
     AddressWithType address_with_type,
     const std::array<uint8_t, 16>& peer_irk,
     const std::array<uint8_t, 16>& local_irk);
 virtual void RemoveDeviceFromResolvingList(AddressWithType address_with_type);
 virtual void ClearResolvingList();

 virtual void CentralLinkKey(KeyFlag key_flag);
 virtual void SwitchRole(Address address, Role role);
 virtual uint16_t ReadDefaultLinkPolicySettings();
 virtual void WriteDefaultLinkPolicySettings(uint16_t default_link_policy_settings);

 // Callback from Advertising Manager to notify the advitiser (local) address
 virtual void OnAdvertisingSetTerminated(ErrorCode status, uint16_t conn_handle, hci::AddressWithType adv_address);

 // In order to avoid circular dependency use setter rather than module dependency.
 virtual void SetSecurityModule(security::SecurityModule* security_module);

 virtual LeAddressManager* GetLeAddressManager();

 static const ModuleFactory Factory;

protected:
 void ListDependencies(ModuleList* list) const override;

 void Start() override;

 void Stop() override;

 std::string ToString() const override;

 DumpsysDataFinisher GetDumpsysData(flatbuffers::FlatBufferBuilder* builder) const override;  // Module

private:
 virtual uint16_t HACK_GetHandle(const Address address);
 virtual uint16_t HACK_GetLeHandle(const Address address);

 // Hack for the shim to get non-acl disconnect callback. Shim needs to post to their handler!
 virtual void HACK_SetNonAclDisconnectCallback(std::function<void(uint16_t /* handle */, uint8_t /* reason */)>);

 virtual void HACK_SetAclTxPriority(uint8_t handle, bool high_priority);

 struct impl;
 std::unique_ptr<impl> pimpl_;
};

}  // namespace hci
}  // namespace bluetooth