1 /*
2 * Copyright 2020 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 <cstdint>
20 #include <string>
21 #include <vector>
22
23 #include "internal_include/bt_target.h"
24 #include "internal_include/bt_trace.h"
25 #include "stack/acl/peer_packet_types.h"
26 #include "stack/btm/power_mode.h"
27 #include "stack/include/btm_status.h"
28 #include "stack/include/hcimsgs.h"
29 #include "types/bt_transport.h"
30 #include "types/hci_role.h"
31 #include "types/raw_address.h"
32 #include "types/remote_version_type.h"
33
34 enum btm_acl_encrypt_state_t {
35 BTM_ACL_ENCRYPT_STATE_IDLE = 0,
36 BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF = 1,
37 BTM_ACL_ENCRYPT_STATE_TEMP_FUNC = 2,
38 BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON = 3,
39 };
40
41 enum btm_acl_swkey_state_t {
42 BTM_ACL_SWKEY_STATE_IDLE = 0,
43 BTM_ACL_SWKEY_STATE_MODE_CHANGE = 1,
44 BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF = 2,
45 BTM_ACL_SWKEY_STATE_SWITCHING = 3,
46 BTM_ACL_SWKEY_STATE_ENCRYPTION_ON = 4,
47 BTM_ACL_SWKEY_STATE_IN_PROGRESS = 5,
48 };
49
50 /* Policy settings status */
51 typedef enum : uint16_t {
52 HCI_DISABLE_ALL_LM_MODES = 0,
53 HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH = (1u << 0),
54 HCI_ENABLE_HOLD_MODE = (1u << 1),
55 HCI_ENABLE_SNIFF_MODE = (1u << 2),
56 HCI_ENABLE_PARK_MODE = (1u << 3),
57 } tLINK_POLICY_BITMASK;
58 typedef uint16_t tLINK_POLICY;
59
60 constexpr tLINK_POLICY kAllLinkPoliciesEnabled =
61 (HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH | HCI_ENABLE_HOLD_MODE |
62 HCI_ENABLE_SNIFF_MODE);
63
64 static const char* link_policy_string[] = {
65 " role_switch ",
66 " hold_mode ",
67 " sniff_mode ",
68 " park_mode ",
69 };
70
link_policy_text(tLINK_POLICY policy)71 inline std::string link_policy_text(tLINK_POLICY policy) {
72 std::ostringstream os;
73 os << "0x" << loghex(static_cast<uint16_t>(policy)) << " :";
74 std::string s = os.str();
75 for (uint16_t i = 0; i < 4; i++) {
76 if (policy & (0x1 << i)) s += link_policy_string[i];
77 }
78 return s;
79 }
80
81 // Power mode states.
82 // Used as both value and bitmask
83 enum : uint8_t {
84 BTM_PM_ST_ACTIVE = HCI_MODE_ACTIVE, // 0x00
85 BTM_PM_ST_HOLD = HCI_MODE_HOLD, // 0x01
86 BTM_PM_ST_SNIFF = HCI_MODE_SNIFF, // 0x02
87 BTM_PM_ST_PARK = HCI_MODE_PARK, // 0x03
88 BTM_PM_ST_UNUSED, // 0x04
89 BTM_PM_ST_PENDING = BTM_PM_STS_PENDING, // 0x05
90 BTM_PM_ST_INVALID = 0x7F,
91 BTM_PM_STORED_MASK = 0x80, /* set this mask if the command is stored */
92 };
93 typedef uint8_t tBTM_PM_STATE;
94
power_mode_state_text(tBTM_PM_STATE state)95 inline std::string power_mode_state_text(tBTM_PM_STATE state) {
96 std::string s =
97 std::string((state & BTM_PM_STORED_MASK) ? "stored:" : "immediate:");
98 switch (state & ~BTM_PM_STORED_MASK) {
99 case BTM_PM_ST_ACTIVE:
100 return s + std::string("active");
101 case BTM_PM_ST_HOLD:
102 return s + std::string("hold");
103 case BTM_PM_ST_SNIFF:
104 return s + std::string("sniff");
105 case BTM_PM_ST_PARK:
106 return s + std::string("park");
107 case BTM_PM_ST_UNUSED:
108 return s + std::string("WARN:UNUSED");
109 case BTM_PM_ST_PENDING:
110 return s + std::string("pending");
111 case BTM_PM_ST_INVALID:
112 return s + std::string("invalid");
113 default:
114 return s + std::string("UNKNOWN");
115 }
116 }
117
118 namespace bluetooth {
119 namespace shim {
120 tBTM_STATUS BTM_SetPowerMode(uint16_t handle, const tBTM_PM_PWR_MD& new_mode);
121 tBTM_STATUS BTM_SetSsrParams(uint16_t handle, uint16_t max_lat,
122 uint16_t min_rmt_to, uint16_t min_loc_to);
123 void btm_pm_on_mode_change(tHCI_STATUS status, uint16_t handle,
124 tHCI_MODE hci_mode, uint16_t interval);
125 void btm_pm_on_sniff_subrating(tHCI_STATUS status, uint16_t handle,
126 uint16_t maximum_transmit_latency,
127 uint16_t maximum_receive_latency,
128 uint16_t minimum_remote_timeout,
129 uint16_t minimum_local_timeout);
130 } // namespace shim
131 } // namespace bluetooth
132
133 typedef struct {
134 uint16_t max_xmit_latency;
135 uint16_t max_recv_latency;
136 uint16_t min_remote_timeout;
137 uint16_t min_local_timeout;
138 } tSSR_PARAMS;
139
140 #define BTM_PM_REC_NOT_USED 0
141 typedef struct tBTM_PM_RCB {
142 tBTM_PM_STATUS_CBACK* cback =
143 nullptr; /* to notify the registered party of mode change event */
144 uint8_t mask = 0; /* registered request mask. 0, if this entry is not used */
145 } tBTM_PM_RCB;
146
147 /* Structure returned with Role Switch information (in tBTM_CMPL_CB callback
148 * function) in response to BTM_SwitchRoleToCentral call.
149 */
150 typedef struct {
151 RawAddress remote_bd_addr; /* Remote BD addr involved with the switch */
152 tHCI_STATUS hci_status; /* HCI status returned with the event */
153 tHCI_ROLE role; /* HCI_ROLE_CENTRAL or HCI_ROLE_PERIPHERAL */
154 } tBTM_ROLE_SWITCH_CMPL;
155
156 struct tBTM_PM_MCB {
157 bool chg_ind = false;
158 tBTM_PM_PWR_MD req_mode;
159 tBTM_PM_PWR_MD set_mode;
160 tBTM_PM_STATE state = BTM_PM_ST_ACTIVE; // 0
161 uint16_t interval = 0;
162 uint16_t max_lat = 0;
163 uint16_t min_loc_to = 0;
164 uint16_t min_rmt_to = 0;
InittBTM_PM_MCB165 void Init(RawAddress bda, uint16_t handle) {
166 bda_ = bda;
167 handle_ = handle;
168 }
169 RawAddress bda_;
170 uint16_t handle_;
171 };
172
173 struct tACL_CONN {
174 BD_FEATURES peer_le_features;
175 bool peer_le_features_valid;
176 BD_FEATURES peer_lmp_feature_pages[HCI_EXT_FEATURES_PAGE_MAX + 1];
177 bool peer_lmp_feature_valid[HCI_EXT_FEATURES_PAGE_MAX + 1];
178
179 /* Whether "Read Remote Version Information Complete" was received */
180 bool remote_version_received{false};
181
182 RawAddress active_remote_addr;
183 tBLE_ADDR_TYPE active_remote_addr_type;
184
185 RawAddress remote_addr;
186 bool in_use{false};
187
188 public:
InUsetACL_CONN189 bool InUse() const { return in_use; }
RemoteAddresstACL_CONN190 const RawAddress RemoteAddress() const { return remote_addr; }
191
192 bool link_up_issued;
193 tBT_TRANSPORT transport;
is_transport_br_edrtACL_CONN194 bool is_transport_br_edr() const { return transport == BT_TRANSPORT_BR_EDR; }
is_transport_bletACL_CONN195 bool is_transport_ble() const { return transport == BT_TRANSPORT_LE; }
is_transport_validtACL_CONN196 bool is_transport_valid() const {
197 return is_transport_ble() || is_transport_br_edr();
198 }
199
200 uint16_t flush_timeout_in_ticks;
201 uint16_t hci_handle;
202 tLINK_POLICY link_policy;
203
204 public:
HandletACL_CONN205 uint16_t Handle() const { return hci_handle; }
206 uint16_t link_super_tout;
207 uint16_t pkt_types_mask;
208 uint8_t disconnect_reason;
209
210 private:
211 btm_acl_encrypt_state_t encrypt_state_;
212
213 public:
set_encryption_offtACL_CONN214 void set_encryption_off() {
215 if (encrypt_state_ != BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF) {
216 btsnd_hcic_set_conn_encrypt(hci_handle, false);
217 encrypt_state_ = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
218 }
219 }
set_encryption_ontACL_CONN220 void set_encryption_on() {
221 if (encrypt_state_ != BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON) {
222 btsnd_hcic_set_conn_encrypt(hci_handle, true);
223 encrypt_state_ = BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON;
224 }
225 }
set_encryption_idletACL_CONN226 void set_encryption_idle() { encrypt_state_ = BTM_ACL_ENCRYPT_STATE_IDLE; }
227
set_encryption_switchingtACL_CONN228 void set_encryption_switching() {
229 encrypt_state_ = BTM_ACL_ENCRYPT_STATE_TEMP_FUNC;
230 }
231
232 public:
233 bool is_encrypted = false;
234 tHCI_ROLE link_role;
235 uint8_t switch_role_failed_attempts;
236
237 tREMOTE_VERSION_INFO remote_version_info;
238
239 #define BTM_SEC_RS_NOT_PENDING 0 /* Role Switch not in progress */
240 #define BTM_SEC_RS_PENDING 1 /* Role Switch in progress */
241 #define BTM_SEC_DISC_PENDING 2 /* Disconnect is pending */
242 private:
243 uint8_t rs_disc_pending = BTM_SEC_RS_NOT_PENDING;
244 friend struct StackAclBtmAcl;
245 friend tBTM_STATUS btm_remove_acl(const RawAddress& bd_addr,
246 tBT_TRANSPORT transport);
247 friend void acl_disconnect_after_role_switch(uint16_t conn_handle,
248 tHCI_STATUS reason, std::string);
249 friend void bluetooth::shim::btm_pm_on_mode_change(tHCI_STATUS status,
250 uint16_t handle,
251 tHCI_MODE hci_mode,
252 uint16_t interval);
253 friend void btm_acl_encrypt_change(uint16_t handle, uint8_t status,
254 uint8_t encr_enable);
255
256 public:
is_disconnect_pendingtACL_CONN257 bool is_disconnect_pending() const {
258 return rs_disc_pending == BTM_SEC_DISC_PENDING;
259 }
is_role_switch_pendingtACL_CONN260 bool is_role_switch_pending() const {
261 return rs_disc_pending == BTM_SEC_RS_PENDING;
262 }
263
264 private:
265 uint8_t switch_role_state_;
266
267 public:
reset_switch_roletACL_CONN268 void reset_switch_role() { switch_role_state_ = BTM_ACL_SWKEY_STATE_IDLE; }
set_switch_role_changingtACL_CONN269 void set_switch_role_changing() {
270 switch_role_state_ = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
271 }
set_switch_role_encryption_offtACL_CONN272 void set_switch_role_encryption_off() {
273 switch_role_state_ = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
274 }
set_switch_role_encryption_ontACL_CONN275 void set_switch_role_encryption_on() {
276 switch_role_state_ = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
277 }
set_switch_role_in_progresstACL_CONN278 void set_switch_role_in_progress() {
279 switch_role_state_ = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
280 }
set_switch_role_switchingtACL_CONN281 void set_switch_role_switching() {
282 switch_role_state_ = BTM_ACL_SWKEY_STATE_SWITCHING;
283 }
284
is_switch_role_idletACL_CONN285 bool is_switch_role_idle() const {
286 return switch_role_state_ == BTM_ACL_SWKEY_STATE_IDLE;
287 }
is_switch_role_encryption_offtACL_CONN288 bool is_switch_role_encryption_off() const {
289 return switch_role_state_ == BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
290 }
is_switch_role_encryption_ontACL_CONN291 bool is_switch_role_encryption_on() const {
292 return switch_role_state_ == BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
293 }
is_switch_role_switchingtACL_CONN294 bool is_switch_role_switching() const {
295 return switch_role_state_ == BTM_ACL_SWKEY_STATE_SWITCHING;
296 }
is_switch_role_in_progresstACL_CONN297 bool is_switch_role_in_progress() const {
298 return switch_role_state_ == BTM_ACL_SWKEY_STATE_IN_PROGRESS;
299 }
is_switch_role_mode_changetACL_CONN300 bool is_switch_role_mode_change() const {
301 return switch_role_state_ == BTM_ACL_SWKEY_STATE_MODE_CHANGE;
302 }
is_switch_role_switching_or_in_progresstACL_CONN303 bool is_switch_role_switching_or_in_progress() const {
304 return is_switch_role_switching() || is_switch_role_in_progress();
305 }
306
307 public:
308 uint8_t sca; /* Sleep clock accuracy */
309
310 void Reset();
311 };
312
313 /****************************************************
314 ** ACL Management API
315 ****************************************************/
316 constexpr uint16_t kDefaultPacketTypeMask =
317 HCI_PKT_TYPES_MASK_DH1 | HCI_PKT_TYPES_MASK_DM1 | HCI_PKT_TYPES_MASK_DH3 |
318 HCI_PKT_TYPES_MASK_DM3 | HCI_PKT_TYPES_MASK_DH5 | HCI_PKT_TYPES_MASK_DM5;
319
320 struct tACL_CB {
321 private:
322 friend uint8_t btm_handle_to_acl_index(uint16_t hci_handle);
323 friend void btm_acl_device_down(void);
324 friend void btm_acl_encrypt_change(uint16_t handle, uint8_t status,
325 uint8_t encr_enable);
326
327 friend void DumpsysAcl(int fd);
328 friend struct StackAclBtmAcl;
329
330 tACL_CONN acl_db[MAX_L2CAP_LINKS];
331 tBTM_ROLE_SWITCH_CMPL switch_role_ref_data;
332 uint16_t btm_acl_pkt_types_supported = kDefaultPacketTypeMask;
333 uint16_t btm_def_link_policy;
334 tHCI_STATUS acl_disc_reason = HCI_ERR_UNDEFINED;
335 bool locally_initiated;
336
337 public:
SetDefaultPacketTypeMasktACL_CB338 void SetDefaultPacketTypeMask(uint16_t packet_type_mask) {
339 btm_acl_pkt_types_supported = packet_type_mask;
340 }
341
get_disconnect_reasontACL_CB342 tHCI_STATUS get_disconnect_reason() const { return acl_disc_reason; }
set_disconnect_reasontACL_CB343 void set_disconnect_reason(tHCI_STATUS reason) { acl_disc_reason = reason; }
is_locally_initiatedtACL_CB344 bool is_locally_initiated() const { return locally_initiated; }
set_locally_initiatedtACL_CB345 void set_locally_initiated(bool value) { locally_initiated = value; }
DefaultPacketTypestACL_CB346 uint16_t DefaultPacketTypes() const { return btm_acl_pkt_types_supported; }
DefaultLinkPolicytACL_CB347 uint16_t DefaultLinkPolicy() const { return btm_def_link_policy; }
348
349 struct {
350 std::vector<tBTM_PM_STATUS_CBACK*> clients;
351 } link_policy;
352
NumberOfActiveLinkstACL_CB353 unsigned NumberOfActiveLinks() const {
354 unsigned cnt = 0;
355 for (int i = 0; i < MAX_L2CAP_LINKS; i++) {
356 if (acl_db[i].InUse()) ++cnt;
357 }
358 return cnt;
359 }
360 };
361
362 tACL_CONN* btm_acl_for_bda(const RawAddress& bd_addr, tBT_TRANSPORT transport);
363
364 void btm_acl_encrypt_change(uint16_t handle, uint8_t status,
365 uint8_t encr_enable);