• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/acl/peer_packet_types.h"
25 #include "stack/include/acl_api_types.h"
26 #include "stack/include/bt_types.h"
27 #include "stack/include/btm_api_types.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 enum btm_data_direction {
50   HOST_TO_CONTROLLER = 0,
51   CONTROLLER_TO_HOST = 1,
52 };
53 
54 /* Policy settings status */
55 typedef enum : uint16_t {
56   HCI_DISABLE_ALL_LM_MODES = 0,
57   HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH = (1u << 0),
58   HCI_ENABLE_HOLD_MODE = (1u << 1),
59   HCI_ENABLE_SNIFF_MODE = (1u << 2),
60   HCI_ENABLE_PARK_MODE = (1u << 3),
61 } tLINK_POLICY_BITMASK;
62 typedef uint16_t tLINK_POLICY;
63 
64 constexpr tLINK_POLICY kAllLinkPoliciesEnabled =
65     (HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH | HCI_ENABLE_HOLD_MODE |
66      HCI_ENABLE_SNIFF_MODE);
67 
68 static const char* link_policy_string[] = {
69     " role_switch ",
70     " hold_mode ",
71     " sniff_mode ",
72     " park_mode ",
73 };
74 
link_policy_text(tLINK_POLICY policy)75 inline std::string link_policy_text(tLINK_POLICY policy) {
76   std::ostringstream os;
77   os << "0x" << loghex(static_cast<uint16_t>(policy)) << " :";
78   std::string s = os.str();
79   for (uint16_t i = 0; i < 4; i++) {
80     if (policy & (0x1 << i)) s += link_policy_string[i];
81   }
82   return s;
83 }
84 
85 // Power mode states.
86 // Used as both value and bitmask
87 enum : uint8_t {
88   BTM_PM_ST_ACTIVE = HCI_MODE_ACTIVE,      // 0x00
89   BTM_PM_ST_HOLD = HCI_MODE_HOLD,          // 0x01
90   BTM_PM_ST_SNIFF = HCI_MODE_SNIFF,        // 0x02
91   BTM_PM_ST_PARK = HCI_MODE_PARK,          // 0x03
92   BTM_PM_ST_UNUSED,                        // 0x04
93   BTM_PM_ST_PENDING = BTM_PM_STS_PENDING,  // 0x05
94   BTM_PM_ST_INVALID = 0x7F,
95   BTM_PM_STORED_MASK = 0x80, /* set this mask if the command is stored */
96 };
97 typedef uint8_t tBTM_PM_STATE;
98 
power_mode_state_text(tBTM_PM_STATE state)99 inline std::string power_mode_state_text(tBTM_PM_STATE state) {
100   std::string s =
101       std::string((state & BTM_PM_STORED_MASK) ? "stored:" : "immediate:");
102   switch (state & ~BTM_PM_STORED_MASK) {
103     case BTM_PM_ST_ACTIVE:
104       return s + std::string("active");
105     case BTM_PM_ST_HOLD:
106       return s + std::string("hold");
107     case BTM_PM_ST_SNIFF:
108       return s + std::string("sniff");
109     case BTM_PM_ST_PARK:
110       return s + std::string("park");
111     case BTM_PM_ST_UNUSED:
112       return s + std::string("WARN:UNUSED");
113     case BTM_PM_ST_PENDING:
114       return s + std::string("pending");
115     case BTM_PM_ST_INVALID:
116       return s + std::string("invalid");
117     default:
118       return s + std::string("UNKNOWN");
119   }
120 }
121 
122 namespace bluetooth {
123 namespace shim {
124 tBTM_STATUS BTM_SetPowerMode(uint16_t handle, const tBTM_PM_PWR_MD& new_mode);
125 tBTM_STATUS BTM_SetSsrParams(uint16_t handle, uint16_t max_lat,
126                              uint16_t min_rmt_to, uint16_t min_loc_to);
127 void btm_pm_on_mode_change(tHCI_STATUS status, uint16_t handle,
128                            tHCI_MODE hci_mode, uint16_t interval);
129 void btm_pm_on_sniff_subrating(tHCI_STATUS status, uint16_t handle,
130                                uint16_t maximum_transmit_latency,
131                                uint16_t maximum_receive_latency,
132                                uint16_t minimum_remote_timeout,
133                                uint16_t minimum_local_timeout);
134 }  // namespace shim
135 }  // namespace bluetooth
136 
137 typedef struct {
138   uint16_t max_xmit_latency;
139   uint16_t max_recv_latency;
140   uint16_t min_remote_timeout;
141   uint16_t min_local_timeout;
142 } tSSR_PARAMS;
143 
144 #define BTM_PM_REC_NOT_USED 0
145 typedef struct {
146   tBTM_PM_STATUS_CBACK* cback =
147       nullptr;      /* to notify the registered party of mode change event */
148   uint8_t mask = 0; /* registered request mask. 0, if this entry is not used */
149 } tBTM_PM_RCB;
150 
151 /* Structure returned with Role Switch information (in tBTM_CMPL_CB callback
152  * function) in response to BTM_SwitchRoleToCentral call.
153  */
154 typedef struct {
155   RawAddress remote_bd_addr; /* Remote BD addr involved with the switch */
156   tHCI_STATUS hci_status;    /* HCI status returned with the event */
157   tHCI_ROLE role;            /* HCI_ROLE_CENTRAL or HCI_ROLE_PERIPHERAL */
158 } tBTM_ROLE_SWITCH_CMPL;
159 
160 struct tBTM_PM_MCB {
161   bool chg_ind = false;
162   tBTM_PM_PWR_MD req_mode;
163   tBTM_PM_PWR_MD set_mode;
164   tBTM_PM_STATE state = BTM_PM_ST_ACTIVE;  // 0
165   uint16_t interval = 0;
166   uint16_t max_lat = 0;
167   uint16_t min_loc_to = 0;
168   uint16_t min_rmt_to = 0;
InittBTM_PM_MCB169   void Init(RawAddress bda, uint16_t handle) {
170     bda_ = bda;
171     handle_ = handle;
172   }
173   RawAddress bda_;
174   uint16_t handle_;
175 };
176 
177 struct tACL_CONN {
178   BD_FEATURES peer_le_features;
179   bool peer_le_features_valid;
180   BD_FEATURES peer_lmp_feature_pages[HCI_EXT_FEATURES_PAGE_MAX + 1];
181   bool peer_lmp_feature_valid[HCI_EXT_FEATURES_PAGE_MAX + 1];
182 
183   RawAddress active_remote_addr;
184   tBLE_ADDR_TYPE active_remote_addr_type;
185   RawAddress conn_addr;
186   tBLE_ADDR_TYPE conn_addr_type;
187 
188   RawAddress remote_addr;
189   bool in_use{false};
190 
191  public:
InUsetACL_CONN192   bool InUse() const { return in_use; }
RemoteAddresstACL_CONN193   const RawAddress RemoteAddress() const { return remote_addr; }
194 
195   bool link_up_issued;
196   tBT_TRANSPORT transport;
is_transport_br_edrtACL_CONN197   bool is_transport_br_edr() const { return transport == BT_TRANSPORT_BR_EDR; }
is_transport_bletACL_CONN198   bool is_transport_ble() const { return transport == BT_TRANSPORT_LE; }
is_transport_validtACL_CONN199   bool is_transport_valid() const {
200     return is_transport_ble() || is_transport_br_edr();
201   }
202 
203   uint16_t flush_timeout_in_ticks;
204   uint16_t hci_handle;
205   tLINK_POLICY link_policy;
206 
207  public:
HandletACL_CONN208   uint16_t Handle() const { return hci_handle; }
209   uint16_t link_super_tout;
210   uint16_t pkt_types_mask;
211   uint8_t disconnect_reason;
212 
213  private:
214   btm_acl_encrypt_state_t encrypt_state_;
215 
216  public:
set_encryption_offtACL_CONN217   void set_encryption_off() {
218     if (encrypt_state_ != BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF) {
219       btsnd_hcic_set_conn_encrypt(hci_handle, false);
220       encrypt_state_ = BTM_ACL_ENCRYPT_STATE_ENCRYPT_OFF;
221     }
222   }
set_encryption_ontACL_CONN223   void set_encryption_on() {
224     if (encrypt_state_ != BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON) {
225       btsnd_hcic_set_conn_encrypt(hci_handle, true);
226       encrypt_state_ = BTM_ACL_ENCRYPT_STATE_ENCRYPT_ON;
227     }
228   }
set_encryption_idletACL_CONN229   void set_encryption_idle() { encrypt_state_ = BTM_ACL_ENCRYPT_STATE_IDLE; }
230 
set_encryption_switchingtACL_CONN231   void set_encryption_switching() {
232     encrypt_state_ = BTM_ACL_ENCRYPT_STATE_TEMP_FUNC;
233   }
234 
235  public:
236   bool is_encrypted = false;
237   tHCI_ROLE link_role;
238   uint8_t switch_role_failed_attempts;
239 
240   tREMOTE_VERSION_INFO remote_version_info;
241 
242 #define BTM_SEC_RS_NOT_PENDING 0 /* Role Switch not in progress */
243 #define BTM_SEC_RS_PENDING 1     /* Role Switch in progress */
244 #define BTM_SEC_DISC_PENDING 2   /* Disconnect is pending */
245  private:
246   uint8_t rs_disc_pending = BTM_SEC_RS_NOT_PENDING;
247   friend struct StackAclBtmAcl;
248   friend tBTM_STATUS btm_remove_acl(const RawAddress& bd_addr,
249                                     tBT_TRANSPORT transport);
250   friend void acl_disconnect_after_role_switch(uint16_t conn_handle,
251                                                tHCI_STATUS reason, std::string);
252   friend void bluetooth::shim::btm_pm_on_mode_change(tHCI_STATUS status,
253                                                      uint16_t handle,
254                                                      tHCI_MODE hci_mode,
255                                                      uint16_t interval);
256   friend void btm_acl_encrypt_change(uint16_t handle, uint8_t status,
257                                      uint8_t encr_enable);
258 
259  public:
is_disconnect_pendingtACL_CONN260   bool is_disconnect_pending() const {
261     return rs_disc_pending == BTM_SEC_DISC_PENDING;
262   }
is_role_switch_pendingtACL_CONN263   bool is_role_switch_pending() const {
264     return rs_disc_pending == BTM_SEC_RS_PENDING;
265   }
266 
267  private:
268   uint8_t switch_role_state_;
269 
270  public:
reset_switch_roletACL_CONN271   void reset_switch_role() { switch_role_state_ = BTM_ACL_SWKEY_STATE_IDLE; }
set_switch_role_changingtACL_CONN272   void set_switch_role_changing() {
273     switch_role_state_ = BTM_ACL_SWKEY_STATE_MODE_CHANGE;
274   }
set_switch_role_encryption_offtACL_CONN275   void set_switch_role_encryption_off() {
276     switch_role_state_ = BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
277   }
set_switch_role_encryption_ontACL_CONN278   void set_switch_role_encryption_on() {
279     switch_role_state_ = BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
280   }
set_switch_role_in_progresstACL_CONN281   void set_switch_role_in_progress() {
282     switch_role_state_ = BTM_ACL_SWKEY_STATE_IN_PROGRESS;
283   }
set_switch_role_switchingtACL_CONN284   void set_switch_role_switching() {
285     switch_role_state_ = BTM_ACL_SWKEY_STATE_SWITCHING;
286   }
287 
is_switch_role_idletACL_CONN288   bool is_switch_role_idle() const {
289     return switch_role_state_ == BTM_ACL_SWKEY_STATE_IDLE;
290   }
is_switch_role_encryption_offtACL_CONN291   bool is_switch_role_encryption_off() const {
292     return switch_role_state_ == BTM_ACL_SWKEY_STATE_ENCRYPTION_OFF;
293   }
is_switch_role_encryption_ontACL_CONN294   bool is_switch_role_encryption_on() const {
295     return switch_role_state_ == BTM_ACL_SWKEY_STATE_ENCRYPTION_ON;
296   }
is_switch_role_switchingtACL_CONN297   bool is_switch_role_switching() const {
298     return switch_role_state_ == BTM_ACL_SWKEY_STATE_SWITCHING;
299   }
is_switch_role_in_progresstACL_CONN300   bool is_switch_role_in_progress() const {
301     return switch_role_state_ == BTM_ACL_SWKEY_STATE_IN_PROGRESS;
302   }
is_switch_role_mode_changetACL_CONN303   bool is_switch_role_mode_change() const {
304     return switch_role_state_ == BTM_ACL_SWKEY_STATE_MODE_CHANGE;
305   }
is_switch_role_switching_or_in_progresstACL_CONN306   bool is_switch_role_switching_or_in_progress() const {
307     return is_switch_role_switching() || is_switch_role_in_progress();
308   }
309 
310   friend void DumpsysL2cap(int fd);
311 
312  public:
313   uint8_t sca; /* Sleep clock accuracy */
314 
315   void Reset();
316 
317   struct tPolicy {
ModetACL_CONN::tPolicy318     tBTM_PM_MODE Mode() const { return this->mode.mode_; }
319     struct {
IsPendingtACL_CONN::tPolicy::__anondc75c9640608320       bool IsPending() const { return pending_ != BTM_PM_MD_UNKNOWN; }
PendingtACL_CONN::tPolicy::__anondc75c9640608321       tBTM_PM_MODE Pending() const { return pending_; }
IntervaltACL_CONN::tPolicy::__anondc75c9640608322       uint16_t Interval() const { return interval_; }
323 
324      private:
325       tBTM_PM_MODE mode_{BTM_PM_MD_ACTIVE};
326       tBTM_PM_MODE pending_{BTM_PM_MD_UNKNOWN};
327       uint16_t interval_{0};
328       friend tBTM_STATUS bluetooth::shim::BTM_SetPowerMode(
329           uint16_t, const tBTM_PM_PWR_MD& new_mode);
330       friend void bluetooth::shim::btm_pm_on_mode_change(tHCI_STATUS status,
331                                                          uint16_t handle,
332                                                          tHCI_MODE hci_mode,
333                                                          uint16_t interval);
334       friend void tACL_CONN::Reset();
335       friend tBTM_PM_MODE tACL_CONN::tPolicy::Mode() const;
336     } mode;
337 
RoletACL_CONN::tPolicy338     hci_role_t Role() const { return this->role.role_; }
339     struct {
RoleSwitchFailedCounttACL_CONN::tPolicy::__anondc75c9640708340       unsigned RoleSwitchFailedCount() const { return role_switch_failed_cnt_; }
341 
342      private:
343       hci_role_t role_{HCI_ROLE_CENTRAL};
344       unsigned role_switch_failed_cnt_{0};
345       friend void tACL_CONN::Reset();
346       friend hci_role_t tACL_CONN::tPolicy::Role() const;
347     } role;
348 
349     struct {
IsPendingtACL_CONN::tPolicy::__anondc75c9640808350       bool IsPending() const { return pending_; }
351 
352      private:
353       bool pending_{false};
354       friend tBTM_STATUS bluetooth::shim::BTM_SetSsrParams(uint16_t handle,
355                                                            uint16_t max_lat,
356                                                            uint16_t min_rmt_to,
357                                                            uint16_t min_loc_to);
358       friend void bluetooth::shim::btm_pm_on_sniff_subrating(
359           tHCI_STATUS status, uint16_t handle,
360           uint16_t maximum_transmit_latency, uint16_t maximum_receive_latency,
361           uint16_t minimum_remote_timeout, uint16_t minimum_local_timeout);
362       friend void tACL_CONN::Reset();
363     } sniff_subrating;
364 
SettingstACL_CONN::tPolicy365     tLINK_POLICY Settings() const { return settings_; }
366 
367    private:
368     tLINK_POLICY settings_{kAllLinkPoliciesEnabled};
369     friend void btm_set_link_policy(tACL_CONN* conn, tLINK_POLICY policy);
370     friend void tACL_CONN::Reset();
371   } policy;
372 };
373 
374 struct controller_t;
375 
376 /****************************************************
377  **      ACL Management API
378  ****************************************************/
379 constexpr uint16_t kDefaultPacketTypeMask =
380     HCI_PKT_TYPES_MASK_DH1 | HCI_PKT_TYPES_MASK_DM1 | HCI_PKT_TYPES_MASK_DH3 |
381     HCI_PKT_TYPES_MASK_DM3 | HCI_PKT_TYPES_MASK_DH5 | HCI_PKT_TYPES_MASK_DM5;
382 
383 struct tACL_CB {
384  private:
385   friend uint8_t btm_handle_to_acl_index(uint16_t hci_handle);
386   friend void btm_acl_device_down(void);
387   friend void btm_acl_encrypt_change(uint16_t handle, uint8_t status,
388                                      uint8_t encr_enable);
389 
390   friend void DumpsysL2cap(int fd);
391   friend void DumpsysAcl(int fd);
392   friend struct StackAclBtmAcl;
393 
394   tACL_CONN acl_db[MAX_L2CAP_LINKS];
395   tBTM_ROLE_SWITCH_CMPL switch_role_ref_data;
396   uint16_t btm_acl_pkt_types_supported = kDefaultPacketTypeMask;
397   uint16_t btm_def_link_policy;
398   tHCI_STATUS acl_disc_reason = HCI_ERR_UNDEFINED;
399 
400  public:
SetDefaultPacketTypeMasktACL_CB401   void SetDefaultPacketTypeMask(uint16_t packet_type_mask) {
402     btm_acl_pkt_types_supported = packet_type_mask;
403   }
404 
get_disconnect_reasontACL_CB405   tHCI_STATUS get_disconnect_reason() const { return acl_disc_reason; }
set_disconnect_reasontACL_CB406   void set_disconnect_reason(tHCI_STATUS reason) { acl_disc_reason = reason; }
DefaultPacketTypestACL_CB407   uint16_t DefaultPacketTypes() const { return btm_acl_pkt_types_supported; }
DefaultLinkPolicytACL_CB408   uint16_t DefaultLinkPolicy() const { return btm_def_link_policy; }
409 
410   struct {
411     std::vector<tBTM_PM_STATUS_CBACK*> clients;
412   } link_policy;
413 
NumberOfActiveLinkstACL_CB414   unsigned NumberOfActiveLinks() const {
415     unsigned cnt = 0;
416     for (int i = 0; i < MAX_L2CAP_LINKS; i++) {
417       if (acl_db[i].InUse()) ++cnt;
418     }
419     return cnt;
420   }
421 };
422 
423 extern tACL_CONN* btm_acl_for_bda(const RawAddress& bd_addr,
424                                   tBT_TRANSPORT transport);
425