1 /******************************************************************************
2 *
3 * Copyright 2003-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /******************************************************************************
20 *
21 * This is the private interface file for the BTA device manager.
22 *
23 ******************************************************************************/
24 #ifndef BTA_DM_INT_H
25 #define BTA_DM_INT_H
26
27 #include <base/strings/stringprintf.h>
28 #include <bluetooth/log.h>
29 #include <com_android_bluetooth_flags.h>
30
31 #include <list>
32 #include <string>
33 #include <vector>
34
35 #include "bta/include/bta_api.h"
36 #include "bta/include/bta_sec_api.h"
37 #include "bta/sys/bta_sys.h"
38 #include "hci/le_rand_callback.h"
39 #include "internal_include/bt_target.h"
40 #include "internal_include/bt_trace.h"
41 #include "macros.h"
42 #include "types/raw_address.h"
43
44 /*****************************************************************************
45 * Constants and data types
46 ****************************************************************************/
47
48 #define BTA_DM_NUM_PEER_DEVICE 7
49
50 // TODO: Remove when flag wait_for_disconnect_before_unbond is shipped
51 enum class tBTA_DM_CONN_STATE : uint8_t {
52 BTA_DM_CONNECTED = 0,
53 BTA_DM_UNPAIRING = 1,
54 };
55
56 // TODO: Remove when flag wait_for_disconnect_before_unbond is shipped
bta_conn_state_text(tBTA_DM_CONN_STATE state)57 inline std::string bta_conn_state_text(tBTA_DM_CONN_STATE state) {
58 switch (state) {
59 CASE_RETURN_STRING(tBTA_DM_CONN_STATE::BTA_DM_CONNECTED);
60 CASE_RETURN_STRING(tBTA_DM_CONN_STATE::BTA_DM_UNPAIRING);
61 }
62 RETURN_UNKNOWN_TYPE_STRING(tBTA_DM_CONN_STATE, state);
63 }
64
65 typedef enum : uint8_t {
66 BTA_DM_DI_NONE = 0x00, /* nothing special */
67 BTA_DM_DI_SET_SNIFF = 0x01, /* set this bit if call BTM_SetPowerMode(sniff) */
68 BTA_DM_DI_INT_SNIFF = 0x02, /* set this bit if call BTM_SetPowerMode(sniff) &
69 enter sniff mode */
70 BTA_DM_DI_ACP_SNIFF = 0x04, /* set this bit if peer init sniff */
71 BTA_DM_DI_UNUSED = 0x08,
72 BTA_DM_DI_USE_SSR = 0x10, /* set this bit if ssr is supported for this link */
73 BTA_DM_DI_AV_ACTIVE = 0x20, /* set this bit if AV is active for this link */
74 } tBTA_DM_DEV_INFO_BITMASK;
75 typedef uint8_t tBTA_DM_DEV_INFO;
76
device_info_text(tBTA_DM_DEV_INFO info)77 inline std::string device_info_text(tBTA_DM_DEV_INFO info) {
78 const char* const device_info_text[] = {
79 ":set_sniff", ":int_sniff", ":acp_sniff", ":unused", ":use_ssr", ":av_active",
80 };
81
82 std::string s = base::StringPrintf("0x%02x", info);
83 if (info == BTA_DM_DI_NONE) {
84 return s + std::string(":none");
85 }
86 for (size_t i = 0; i < sizeof(device_info_text) / sizeof(device_info_text[0]); i++) {
87 if (info & (1u << i)) {
88 s += std::string(device_info_text[i]);
89 }
90 }
91 return s;
92 }
93
94 /* set power mode request type */
95 #define BTA_DM_PM_RESTART 1
96 #define BTA_DM_PM_NEW_REQ 2
97 #define BTA_DM_PM_EXECUTE 3
98 typedef uint8_t tBTA_DM_PM_REQ;
99
100 struct tBTA_DM_REMOVE_PENDNIG {
101 RawAddress pseudo_addr;
102 RawAddress identity_addr;
103 bool le_connected;
104 bool bredr_connected;
105 };
106
107 bool bta_dm_removal_pending(const RawAddress& bd_addr);
108
109 struct tBTA_DM_PEER_DEVICE {
110 RawAddress peer_bdaddr;
111
112 // TODO: Remove when flag wait_for_disconnect_before_unbond is shipped
113 tBTA_DM_CONN_STATE conn_state{tBTA_DM_CONN_STATE::BTA_DM_CONNECTED};
114
115 tBTA_PREF_ROLES pref_role;
116 bool in_use;
117
118 private:
119 // Dynamic pieces of operational device information
120 tBTA_DM_DEV_INFO info{BTA_DM_DI_NONE};
121
122 public:
info_texttBTA_DM_PEER_DEVICE123 std::string info_text() const { return device_info_text(info); }
124
reset_device_infotBTA_DM_PEER_DEVICE125 void reset_device_info() { info = BTA_DM_DI_NONE; }
126
set_av_activetBTA_DM_PEER_DEVICE127 void set_av_active() { info |= BTA_DM_DI_AV_ACTIVE; }
reset_av_activetBTA_DM_PEER_DEVICE128 void reset_av_active() { info &= ~BTA_DM_DI_AV_ACTIVE; }
is_av_activetBTA_DM_PEER_DEVICE129 bool is_av_active() const { return info & BTA_DM_DI_AV_ACTIVE; }
130
set_local_init_snifftBTA_DM_PEER_DEVICE131 void set_local_init_sniff() { info |= BTA_DM_DI_INT_SNIFF; }
is_local_init_snifftBTA_DM_PEER_DEVICE132 bool is_local_init_sniff() const { return info & BTA_DM_DI_INT_SNIFF; }
set_remote_init_snifftBTA_DM_PEER_DEVICE133 void set_remote_init_sniff() { info |= BTA_DM_DI_ACP_SNIFF; }
is_remote_init_snifftBTA_DM_PEER_DEVICE134 bool is_remote_init_sniff() const { return info & BTA_DM_DI_ACP_SNIFF; }
135
set_sniff_command_senttBTA_DM_PEER_DEVICE136 void set_sniff_command_sent() { info |= BTA_DM_DI_SET_SNIFF; }
reset_sniff_command_senttBTA_DM_PEER_DEVICE137 void reset_sniff_command_sent() { info &= ~BTA_DM_DI_SET_SNIFF; }
is_sniff_command_senttBTA_DM_PEER_DEVICE138 bool is_sniff_command_sent() const { return info & BTA_DM_DI_SET_SNIFF; }
139
140 // NOTE: Why is this not used as a bitmask
set_both_device_ssr_capabletBTA_DM_PEER_DEVICE141 void set_both_device_ssr_capable() { info = BTA_DM_DI_USE_SSR; }
142
reset_sniff_flagstBTA_DM_PEER_DEVICE143 void reset_sniff_flags() {
144 info &= ~(BTA_DM_DI_INT_SNIFF | BTA_DM_DI_ACP_SNIFF | BTA_DM_DI_SET_SNIFF);
145 }
146
set_ssr_activetBTA_DM_PEER_DEVICE147 void set_ssr_active() { info |= BTA_DM_DI_USE_SSR; }
reset_ssr_activetBTA_DM_PEER_DEVICE148 void reset_ssr_active() { info &= ~BTA_DM_DI_USE_SSR; }
is_ssr_activetBTA_DM_PEER_DEVICE149 bool is_ssr_active() const { return info & BTA_DM_DI_USE_SSR; }
150
is_connectedtBTA_DM_PEER_DEVICE151 bool is_connected() const {
152 // Devices getting removed should be treated as disconnected
153 if (com::android::bluetooth::flags::wait_for_disconnect_before_unbond() &&
154 bta_dm_removal_pending(peer_bdaddr)) {
155 return false;
156 }
157 return (conn_state == tBTA_DM_CONN_STATE::BTA_DM_CONNECTED);
158 }
159
160 tBTA_DM_ENCRYPT_CBACK* p_encrypt_cback;
161 tBTM_PM_STATUS prev_low; /* previous low power mode used */
162 tBTA_DM_PM_ACTION pm_mode_attempted;
163 tBTA_DM_PM_ACTION pm_mode_failed;
164 bool remove_dev_pending;
165 tBT_TRANSPORT transport;
166 };
167
168 /* structure to store list of
169 active connections */
170 typedef struct {
171 tBTA_DM_PEER_DEVICE peer_device[BTA_DM_NUM_PEER_DEVICE];
172 uint8_t count;
173 uint8_t le_count;
174 } tBTA_DM_ACTIVE_LINK;
175
176 typedef struct {
177 RawAddress peer_bdaddr;
178 tBTA_SYS_ID id;
179 uint8_t app_id;
180 tBTA_SYS_CONN_STATUS state;
181 bool new_request;
182
ToString__anon51ec04b11208183 std::string ToString() const {
184 return base::StringPrintf("peer:%s sys_name:%s app_id:%hhu state:%s new_request:%s",
185 ADDRESS_TO_LOGGABLE_CSTR(peer_bdaddr), BtaIdSysText(id).c_str(),
186 app_id, bta_sys_conn_status_text(state).c_str(),
187 new_request ? "true" : "false");
188 }
189 } tBTA_DM_SRVCS;
190
191 #ifndef BTA_DM_NUM_CONN_SRVS
192 #define BTA_DM_NUM_CONN_SRVS 30
193 #endif
194
195 typedef struct {
196 uint8_t count;
197 tBTA_DM_SRVCS conn_srvc[BTA_DM_NUM_CONN_SRVS];
198 } tBTA_DM_CONNECTED_SRVCS;
199
200 typedef struct {
201 #define BTA_DM_PM_SNIFF_TIMER_IDX 0
202 #define BTA_DM_PM_PARK_TIMER_IDX 1
203 #define BTA_DM_PM_SUSPEND_TIMER_IDX 2
204 #define BTA_DM_PM_MODE_TIMER_MAX 3
205 /*
206 * Keep three different timers for PARK, SNIFF and SUSPEND if TBFC is
207 * supported.
208 */
209 alarm_t* timer[BTA_DM_PM_MODE_TIMER_MAX];
210
211 uint8_t srvc_id[BTA_DM_PM_MODE_TIMER_MAX];
212 uint8_t pm_action[BTA_DM_PM_MODE_TIMER_MAX];
213 uint8_t active; /* number of active timer */
214
215 RawAddress peer_bdaddr;
216 bool in_use;
217 } tBTA_PM_TIMER;
218
219 extern tBTA_DM_CONNECTED_SRVCS bta_dm_conn_srvcs;
220
221 #define BTA_DM_NUM_PM_TIMER 7
222
223 typedef struct {
224 tBTA_DM_ACL_CBACK* p_acl_cback;
225 } tBTA_DM_ACL_CB;
226
227 /* DM control block */
228 typedef struct {
229 tBTA_DM_ACTIVE_LINK device_list;
230 tBTA_BLE_ENERGY_INFO_CBACK* p_energy_info_cback;
231 bool disabling;
232 alarm_t* disable_timer;
233 uint8_t pm_id;
234 tBTA_PM_TIMER pm_timer[BTA_DM_NUM_PM_TIMER];
235 uint8_t cur_av_count; /* current AV connections */
236
237 /* store UUID list for EIR */
238 uint32_t eir_uuid[BTM_EIR_SERVICE_ARRAY_SIZE];
239 #if (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
240 tBTA_CUSTOM_UUID bta_custom_uuid[BTA_EIR_SERVER_NUM_CUSTOM_UUID];
241 #endif
242 alarm_t* switch_delay_timer;
243
244 std::list<tBTA_DM_REMOVE_PENDNIG> pending_removals;
245 } tBTA_DM_CB;
246
247 /* DI control block */
248 typedef struct {
249 uint8_t di_num; /* total local DI record number */
250 uint32_t di_handle[BTA_DI_NUM_MAX]; /* local DI record handle, the first one
251 is primary record */
252 } tBTA_DM_DI_CB;
253
254 typedef struct {
255 uint16_t page_timeout; /* timeout for page in slots */
256 bool avoid_scatter; /* true to avoid scatternet when av is streaming (be the
257 central) */
258 } tBTA_DM_CFG;
259
260 extern const uint32_t bta_service_id_to_btm_srv_id_lkup_tbl[];
261
262 typedef struct {
263 uint8_t id;
264 uint8_t app_id;
265 uint8_t cfg;
266 } tBTA_DM_RM;
267
268 extern const tBTA_DM_CFG* p_bta_dm_cfg;
269 extern const tBTA_DM_RM* p_bta_dm_rm_cfg;
270
271 typedef struct {
272 uint8_t id;
273 uint8_t app_id;
274 uint8_t spec_idx; /* index of spec table to use */
275 } tBTA_DM_PM_CFG;
276
277 typedef struct {
278 tBTA_DM_PM_ACTION power_mode;
279 uint16_t timeout;
280 } tBTA_DM_PM_ACTN;
281
282 typedef struct {
283 uint8_t allow_mask; /* mask of sniff/hold/park modes to allow */
284 uint8_t ssr; /* set SSR on conn open/unpark */
285 tBTA_DM_PM_ACTN actn_tbl[BTA_DM_PM_NUM_EVTS][2];
286 } tBTA_DM_PM_SPEC;
287
288 typedef struct {
289 uint16_t max_lat;
290 uint16_t min_rmt_to;
291 uint16_t min_loc_to;
292 const char* name{nullptr};
293 } tBTA_DM_SSR_SPEC;
294
295 typedef struct {
296 uint16_t manufacturer;
297 uint16_t lmp_sub_version;
298 uint8_t lmp_version;
299 } tBTA_DM_LMP_VER_INFO;
300
301 extern const uint16_t bta_service_id_to_uuid_lkup_tbl[];
302
303 /* For Insight, PM cfg lookup tables are runtime configurable (to allow tweaking
304 * of params for power consumption measurements) */
305 #ifndef BTE_SIM_APP
306 #define tBTA_DM_PM_TYPE_QUALIFIER const
307 #else
308 #define tBTA_DM_PM_TYPE_QUALIFIER
309 #endif
310
311 extern const tBTA_DM_PM_CFG* p_bta_dm_pm_cfg;
312 tBTA_DM_PM_TYPE_QUALIFIER tBTA_DM_PM_SPEC* get_bta_dm_pm_spec();
313 extern const tBTM_PM_PWR_MD* p_bta_dm_pm_md;
314 extern tBTA_DM_SSR_SPEC* p_bta_dm_ssr_spec;
315
316 /* update dynamic BRCM Aware EIR data */
317 extern const tBTA_DM_EIR_CONF bta_dm_eir_cfg;
318 extern const tBTA_DM_EIR_CONF* p_bta_dm_eir_cfg;
319
320 /* DM control block */
321 extern tBTA_DM_CB bta_dm_cb;
322
323 /* DM control block for ACL management */
324 extern tBTA_DM_ACL_CB bta_dm_acl_cb;
325
326 /* DI control block */
327 extern tBTA_DM_DI_CB bta_dm_di_cb;
328
329 void bta_dm_enable(tBTA_DM_SEC_CBACK*, tBTA_DM_ACL_CBACK*);
330 void bta_dm_disable();
331 void bta_dm_set_dev_name(const std::vector<uint8_t>&);
332
333 void bta_dm_ble_set_conn_params(const RawAddress&, uint16_t, uint16_t, uint16_t, uint16_t);
334 void bta_dm_ble_update_conn_params(const RawAddress&, uint16_t, uint16_t, uint16_t, uint16_t,
335 uint16_t, uint16_t);
336
337 void bta_dm_ble_set_data_length(const RawAddress& bd_addr);
338
339 void bta_dm_ble_get_energy_info(tBTA_BLE_ENERGY_INFO_CBACK*);
340
341 void bta_dm_init_pm(void);
342 void bta_dm_disable_pm(void);
343
344 uint8_t bta_dm_get_av_count(void);
345 tBTA_DM_PEER_DEVICE* bta_dm_find_peer_device(const RawAddress& peer_addr);
346
347 void bta_dm_clear_event_filter(void);
348 void bta_dm_clear_event_mask(void);
349 void bta_dm_clear_filter_accept_list(void);
350 void bta_dm_disconnect_all_acls(void);
351 void bta_dm_le_rand(bluetooth::hci::LeRandCallback cb);
352 void bta_dm_set_event_filter_connection_setup_all_devices();
353 void bta_dm_allow_wake_by_hid(std::vector<RawAddress> classic_hid_devices,
354 std::vector<std::pair<RawAddress, uint8_t>> le_hid_devices);
355 void bta_dm_restore_filter_accept_list(std::vector<std::pair<RawAddress, uint8_t>> le_devices);
356 void bta_dm_set_default_event_mask_except(uint64_t mask, uint64_t le_mask);
357 void bta_dm_set_event_filter_inquiry_result_all_devices();
358
359 void bta_dm_ble_reset_id(void);
360
361 void bta_dm_eir_update_uuid(uint16_t uuid16, bool adding);
362 void bta_dm_eir_update_cust_uuid(const tBTA_CUSTOM_UUID& curr, bool adding);
363
364 void bta_dm_ble_subrate_request(const RawAddress& bd_addr, uint16_t subrate_min,
365 uint16_t subrate_max, uint16_t max_latency, uint16_t cont_num,
366 uint16_t timeout);
367
368 namespace std {
369 template <>
370 struct formatter<tBTA_DM_CONN_STATE> : enum_formatter<tBTA_DM_CONN_STATE> {};
371 } // namespace std
372
373 #endif /* BTA_DM_INT_H */
374