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 <unordered_set>
22 #include <vector>
23
24 #include "stack/include/acl_api_types.h"
25 #include "stack/include/bt_types.h"
26 #include "stack/include/btm_api_types.h"
27 #include "stack/include/hcidefs.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
33 enum btm_acl_encrypt_state_t {
34 BTM_ACL_ENCRYPT_STATE_IDLE = 0,
35 BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF = 1,
36 BTM_ACL_ENCRYPT_STATE_TEMP_FUNC = 2,
37 BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON = 3,
38 };
39
40 enum btm_acl_swkey_state_t {
41 BTM_ACL_SWKEY_STATE_IDLE = 0,
42 BTM_ACL_SWKEY_STATE_MODE_CHANGE = 1,
43 BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF = 2,
44 BTM_ACL_SWKEY_STATE_SWITCHING = 3,
45 BTM_ACL_SWKEY_STATE_ENCRYPTION_ON = 4,
46 BTM_ACL_SWKEY_STATE_IN_PROGRESS = 5,
47 };
48
49 /* Policy settings status */
50 typedef enum : uint16_t {
51 HCI_DISABLE_ALL_LM_MODES = 0,
52 HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH = (1u << 0),
53 HCI_ENABLE_HOLD_MODE = (1u << 1),
54 HCI_ENABLE_SNIFF_MODE = (1u << 2),
55 HCI_ENABLE_PARK_MODE = (1u << 3),
56 } tLINK_POLICY_BITMASK;
57 typedef uint16_t tLINK_POLICY;
58
59 constexpr tLINK_POLICY kAllLinkPoliciesEnabled =
60 (HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH | HCI_ENABLE_HOLD_MODE |
61 HCI_ENABLE_SNIFF_MODE);
62
63 static const char* link_policy_string[] = {
64 " role_switch ",
65 " hold_mode ",
66 " sniff_mode ",
67 " park_mode ",
68 };
69
link_policy_text(tLINK_POLICY policy)70 inline std::string link_policy_text(tLINK_POLICY policy) {
71 std::ostringstream os;
72 os << "0x" << loghex(static_cast<uint16_t>(policy)) << " :";
73 std::string s = os.str();
74 for (uint16_t i = 0; i < 4; i++) {
75 if (policy & (0x1 << i)) s += link_policy_string[i];
76 }
77 return s;
78 }
79
80 // Power mode states.
81 // Used as both value and bitmask
82 enum : uint8_t {
83 BTM_PM_ST_ACTIVE = HCI_MODE_ACTIVE, // 0x00
84 BTM_PM_ST_HOLD = HCI_MODE_HOLD, // 0x01
85 BTM_PM_ST_SNIFF = HCI_MODE_SNIFF, // 0x02
86 BTM_PM_ST_PARK = HCI_MODE_PARK, // 0x03
87 BTM_PM_ST_UNUSED, // 0x04
88 BTM_PM_ST_PENDING = BTM_PM_STS_PENDING, // 0x05
89 BTM_PM_ST_INVALID = 0x7F,
90 BTM_PM_STORED_MASK = 0x80, /* set this mask if the command is stored */
91 };
92 typedef uint8_t tBTM_PM_STATE;
93
power_mode_state_text(tBTM_PM_STATE state)94 inline std::string power_mode_state_text(tBTM_PM_STATE state) {
95 std::string s =
96 std::string((state & BTM_PM_STORED_MASK) ? "stored:" : "immediate:");
97 switch (state & ~BTM_PM_STORED_MASK) {
98 case BTM_PM_ST_ACTIVE:
99 return s + std::string("active");
100 case BTM_PM_ST_HOLD:
101 return s + std::string("hold");
102 case BTM_PM_ST_SNIFF:
103 return s + std::string("sniff");
104 case BTM_PM_ST_PARK:
105 return s + std::string("park");
106 case BTM_PM_ST_UNUSED:
107 return s + std::string("WARN:UNUSED");
108 case BTM_PM_ST_PENDING:
109 return s + std::string("pending");
110 case BTM_PM_ST_INVALID:
111 return s + std::string("invalid");
112 default:
113 return s + std::string("UNKNOWN");
114 }
115 }
116
117 namespace bluetooth {
118 namespace shim {
119 tBTM_STATUS BTM_SetPowerMode(uint16_t handle, const tBTM_PM_PWR_MD& new_mode);
120 tBTM_STATUS BTM_SetSsrParams(uint16_t handle, uint16_t max_lat,
121 uint16_t min_rmt_to, uint16_t min_loc_to);
122 void btm_pm_on_mode_change(tHCI_STATUS status, uint16_t handle,
123 tHCI_MODE hci_mode, uint16_t interval);
124 void btm_pm_on_sniff_subrating(tHCI_STATUS status, uint16_t handle,
125 uint16_t maximum_transmit_latency,
126 uint16_t maximum_receive_latency,
127 uint16_t minimum_remote_timeout,
128 uint16_t minimum_local_timeout);
129 } // namespace shim
130 } // namespace bluetooth
131
132 typedef struct {
133 uint16_t max_xmit_latency;
134 uint16_t max_recv_latency;
135 uint16_t min_remote_timeout;
136 uint16_t min_local_timeout;
137 } tSSR_PARAMS;
138
139 #define BTM_PM_REC_NOT_USED 0
140 typedef struct {
141 tBTM_PM_STATUS_CBACK* cback =
142 nullptr; /* to notify the registered party of mode change event */
143 uint8_t mask = 0; /* registered request mask. 0, if this entry is not used */
144 } tBTM_PM_RCB;
145
146 /* Structure returned with Role Switch information (in tBTM_CMPL_CB callback
147 * function) in response to BTM_SwitchRoleToCentral call.
148 */
149 typedef struct {
150 RawAddress remote_bd_addr; /* Remote BD addr involved with the switch */
151 tHCI_STATUS hci_status; /* HCI status returned with the event */
152 tHCI_ROLE role; /* HCI_ROLE_CENTRAL or HCI_ROLE_PERIPHERAL */
153 } tBTM_ROLE_SWITCH_CMPL;
154
155 struct tBTM_PM_MCB {
156 bool chg_ind = false;
157 tBTM_PM_PWR_MD req_mode;
158 tBTM_PM_PWR_MD set_mode;
159 tBTM_PM_STATE state = BTM_PM_ST_ACTIVE; // 0
160 uint16_t interval = 0;
161 uint16_t max_lat = 0;
162 uint16_t min_loc_to = 0;
163 uint16_t min_rmt_to = 0;
InittBTM_PM_MCB164 void Init(RawAddress bda, uint16_t handle) {
165 bda_ = bda;
166 handle_ = handle;
167 }
168 RawAddress bda_;
169 uint16_t handle_;
170 };
171
172 struct tACL_CONN {
173 BD_FEATURES peer_le_features;
174 bool peer_le_features_valid;
175 BD_FEATURES peer_lmp_feature_pages[HCI_EXT_FEATURES_PAGE_MAX + 1];
176 bool peer_lmp_feature_valid[HCI_EXT_FEATURES_PAGE_MAX + 1];
177
178 RawAddress active_remote_addr;
179 RawAddress conn_addr;
180 RawAddress remote_addr;
181 bool in_use{false};
182
183 public:
InUsetACL_CONN184 bool InUse() const { return in_use; }
RemoteAddresstACL_CONN185 const RawAddress RemoteAddress() const { return remote_addr; }
186
187 bool link_up_issued;
188 tBT_TRANSPORT transport;
is_transport_br_edrtACL_CONN189 bool is_transport_br_edr() const { return transport == BT_TRANSPORT_BR_EDR; }
is_transport_bletACL_CONN190 bool is_transport_ble() const { return transport == BT_TRANSPORT_LE; }
is_transport_validtACL_CONN191 bool is_transport_valid() const {
192 return is_transport_ble() || is_transport_br_edr();
193 }
194
195 uint16_t flush_timeout_in_ticks;
196 uint16_t hci_handle;
197 tLINK_POLICY link_policy;
198
199 public:
HandletACL_CONN200 uint16_t Handle() const { return hci_handle; }
201 uint16_t link_super_tout;
202 uint16_t pkt_types_mask;
203 tBLE_ADDR_TYPE active_remote_addr_type;
204 tBLE_ADDR_TYPE conn_addr_type;
205 uint8_t disconnect_reason;
206
207 private:
208 btm_acl_encrypt_state_t encrypt_state_;
209
210 public:
set_encryption_offtACL_CONN211 void set_encryption_off() {
212 if (encrypt_state_ != BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF) {
213 btsnd_hcic_set_conn_encrypt(hci_handle, false);
214 encrypt_state_ = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
215 }
216 }
set_encryption_ontACL_CONN217 void set_encryption_on() {
218 if (encrypt_state_ != BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON) {
219 btsnd_hcic_set_conn_encrypt(hci_handle, true);
220 encrypt_state_ = BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON;
221 }
222 }
set_encryption_idletACL_CONN223 void set_encryption_idle() { encrypt_state_ = BTM_ACL_ENCRYPT_STATE_IDLE; }
224
set_encryption_switchingtACL_CONN225 void set_encryption_switching() {
226 encrypt_state_ = BTM_ACL_ENCRYPT_STATE_TEMP_FUNC;
227 }
228
229 public:
230 bool is_encrypted = false;
231 tHCI_ROLE link_role;
232 uint8_t switch_role_failed_attempts;
233
234 tREMOTE_VERSION_INFO remote_version_info;
235
236 #define BTM_SEC_RS_NOT_PENDING 0 /* Role Switch not in progress */
237 #define BTM_SEC_RS_PENDING 1 /* Role Switch in progress */
238 #define BTM_SEC_DISC_PENDING 2 /* Disconnect is pending */
239 private:
240 uint8_t rs_disc_pending = BTM_SEC_RS_NOT_PENDING;
241 friend struct StackAclBtmAcl;
242 friend tBTM_STATUS btm_remove_acl(const RawAddress& bd_addr,
243 tBT_TRANSPORT transport);
244 friend void acl_disconnect_after_role_switch(uint16_t conn_handle,
245 tHCI_STATUS reason);
246 friend void bluetooth::shim::btm_pm_on_mode_change(tHCI_STATUS status,
247 uint16_t handle,
248 tHCI_MODE hci_mode,
249 uint16_t interval);
250 friend void btm_acl_encrypt_change(uint16_t handle, uint8_t status,
251 uint8_t encr_enable);
252
253 public:
is_disconnect_pendingtACL_CONN254 bool is_disconnect_pending() const {
255 return rs_disc_pending == BTM_SEC_DISC_PENDING;
256 }
is_role_switch_pendingtACL_CONN257 bool is_role_switch_pending() const {
258 return rs_disc_pending == BTM_SEC_RS_PENDING;
259 }
260
261 private:
262 uint8_t switch_role_state_;
263
264 public:
reset_switch_roletACL_CONN265 void reset_switch_role() { switch_role_state_ = BTM_ACL_SWKEY_STATE_IDLE; }
set_switch_role_changingtACL_CONN266 void set_switch_role_changing() {
267 switch_role_state_ = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
268 }
set_switch_role_encryption_offtACL_CONN269 void set_switch_role_encryption_off() {
270 switch_role_state_ = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
271 }
set_switch_role_encryption_ontACL_CONN272 void set_switch_role_encryption_on() {
273 switch_role_state_ = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
274 }
set_switch_role_in_progresstACL_CONN275 void set_switch_role_in_progress() {
276 switch_role_state_ = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
277 }
set_switch_role_switchingtACL_CONN278 void set_switch_role_switching() {
279 switch_role_state_ = BTM_ACL_SWKEY_STATE_SWITCHING;
280 }
281
is_switch_role_idletACL_CONN282 bool is_switch_role_idle() const {
283 return switch_role_state_ == BTM_ACL_SWKEY_STATE_IDLE;
284 }
is_switch_role_encryption_offtACL_CONN285 bool is_switch_role_encryption_off() const {
286 return switch_role_state_ == BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
287 }
is_switch_role_encryption_ontACL_CONN288 bool is_switch_role_encryption_on() const {
289 return switch_role_state_ == BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
290 }
is_switch_role_switchingtACL_CONN291 bool is_switch_role_switching() const {
292 return switch_role_state_ == BTM_ACL_SWKEY_STATE_SWITCHING;
293 }
is_switch_role_in_progresstACL_CONN294 bool is_switch_role_in_progress() const {
295 return switch_role_state_ == BTM_ACL_SWKEY_STATE_IN_PROGRESS;
296 }
is_switch_role_mode_changetACL_CONN297 bool is_switch_role_mode_change() const {
298 return switch_role_state_ == BTM_ACL_SWKEY_STATE_MODE_CHANGE;
299 }
is_switch_role_switching_or_in_progresstACL_CONN300 bool is_switch_role_switching_or_in_progress() const {
301 return is_switch_role_switching() || is_switch_role_in_progress();
302 }
303
304 friend void DumpsysL2cap(int fd);
305
306 public:
307 uint8_t sca; /* Sleep clock accuracy */
308
309 void Reset();
310
311 struct tPolicy {
ModetACL_CONN::tPolicy312 tBTM_PM_MODE Mode() const { return this->mode.mode_; }
313 struct {
IsPendingtACL_CONN::tPolicy::__anonf72334a90608314 bool IsPending() const { return pending_ != BTM_PM_MD_UNKNOWN; }
PendingtACL_CONN::tPolicy::__anonf72334a90608315 tBTM_PM_MODE Pending() const { return pending_; }
IntervaltACL_CONN::tPolicy::__anonf72334a90608316 uint16_t Interval() const { return interval_; }
317
318 private:
319 tBTM_PM_MODE mode_{BTM_PM_MD_ACTIVE};
320 tBTM_PM_MODE pending_{BTM_PM_MD_UNKNOWN};
321 uint16_t interval_{0};
322 friend tBTM_STATUS bluetooth::shim::BTM_SetPowerMode(
323 uint16_t, const tBTM_PM_PWR_MD& new_mode);
324 friend void bluetooth::shim::btm_pm_on_mode_change(tHCI_STATUS status,
325 uint16_t handle,
326 tHCI_MODE hci_mode,
327 uint16_t interval);
328 friend void tACL_CONN::Reset();
329 friend tBTM_PM_MODE tACL_CONN::tPolicy::Mode() const;
330 } mode;
331
RoletACL_CONN::tPolicy332 hci_role_t Role() const { return this->role.role_; }
333 struct {
RoleSwitchFailedCounttACL_CONN::tPolicy::__anonf72334a90708334 unsigned RoleSwitchFailedCount() const { return role_switch_failed_cnt_; }
335
336 private:
337 hci_role_t role_{HCI_ROLE_CENTRAL};
338 unsigned role_switch_failed_cnt_{0};
339 friend void tACL_CONN::Reset();
340 friend hci_role_t tACL_CONN::tPolicy::Role() const;
341 } role;
342
343 struct {
IsPendingtACL_CONN::tPolicy::__anonf72334a90808344 bool IsPending() const { return pending_; }
345
346 private:
347 bool pending_{false};
348 friend tBTM_STATUS bluetooth::shim::BTM_SetSsrParams(uint16_t handle,
349 uint16_t max_lat,
350 uint16_t min_rmt_to,
351 uint16_t min_loc_to);
352 friend void bluetooth::shim::btm_pm_on_sniff_subrating(
353 tHCI_STATUS status, uint16_t handle,
354 uint16_t maximum_transmit_latency, uint16_t maximum_receive_latency,
355 uint16_t minimum_remote_timeout, uint16_t minimum_local_timeout);
356 friend void tACL_CONN::Reset();
357 } sniff_subrating;
358
SettingstACL_CONN::tPolicy359 tLINK_POLICY Settings() const { return settings_; }
360
361 private:
362 tLINK_POLICY settings_{kAllLinkPoliciesEnabled};
363 friend void btm_set_link_policy(tACL_CONN* conn, tLINK_POLICY policy);
364 friend void tACL_CONN::Reset();
365 } policy;
366 };
367
368 struct controller_t;
369
370 /****************************************************
371 ** ACL Management API
372 ****************************************************/
373 constexpr uint16_t kDefaultPacketTypeMask =
374 HCI_PKT_TYPES_MASK_DH1 | HCI_PKT_TYPES_MASK_DM1 | HCI_PKT_TYPES_MASK_DH3 |
375 HCI_PKT_TYPES_MASK_DM3 | HCI_PKT_TYPES_MASK_DH5 | HCI_PKT_TYPES_MASK_DM5;
376
377 struct tACL_CB {
378 private:
379 friend uint8_t btm_handle_to_acl_index(uint16_t hci_handle);
380 friend void btm_acl_device_down(void);
381 friend void btm_acl_encrypt_change(uint16_t handle, uint8_t status,
382 uint8_t encr_enable);
383
384 friend void DumpsysL2cap(int fd);
385 friend void DumpsysAcl(int fd);
386 friend struct StackAclBtmAcl;
387
388 tACL_CONN acl_db[MAX_L2CAP_LINKS];
389 tBTM_ROLE_SWITCH_CMPL switch_role_ref_data;
390 uint16_t btm_acl_pkt_types_supported = kDefaultPacketTypeMask;
391 uint16_t btm_def_link_policy;
392 tHCI_STATUS acl_disc_reason = HCI_ERR_UNDEFINED;
393
394 public:
SetDefaultPacketTypeMasktACL_CB395 void SetDefaultPacketTypeMask(uint16_t packet_type_mask) {
396 btm_acl_pkt_types_supported = packet_type_mask;
397 }
398
get_disconnect_reasontACL_CB399 tHCI_STATUS get_disconnect_reason() const { return acl_disc_reason; }
set_disconnect_reasontACL_CB400 void set_disconnect_reason(tHCI_STATUS reason) { acl_disc_reason = reason; }
DefaultPacketTypestACL_CB401 uint16_t DefaultPacketTypes() const { return btm_acl_pkt_types_supported; }
DefaultLinkPolicytACL_CB402 uint16_t DefaultLinkPolicy() const { return btm_def_link_policy; }
403
404 struct {
405 std::vector<tBTM_PM_STATUS_CBACK*> clients;
406 } link_policy;
407
NumberOfActiveLinkstACL_CB408 unsigned NumberOfActiveLinks() const {
409 unsigned cnt = 0;
410 for (int i = 0; i < MAX_L2CAP_LINKS; i++) {
411 if (acl_db[i].InUse()) ++cnt;
412 }
413 return cnt;
414 }
415
416 private:
417 std::unordered_set<RawAddress> ignore_auto_connect_after_disconnect_set_;
418
419 public:
420 void AddToIgnoreAutoConnectAfterDisconnect(const RawAddress& bd_addr);
421 bool CheckAndClearIgnoreAutoConnectAfterDisconnect(const RawAddress& bd_addr);
422 void ClearAllIgnoreAutoConnectAfterDisconnect();
423 };
424