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
30 #include <string>
31 #include <vector>
32
33 #include "bta/include/bta_api.h"
34 #include "bta/include/bta_sec_api.h"
35 #include "bta/sys/bta_sys.h"
36 #include "hci/le_rand_callback.h"
37 #include "internal_include/bt_target.h"
38 #include "internal_include/bt_trace.h"
39 #include "macros.h"
40 #include "types/raw_address.h"
41
42 /*****************************************************************************
43 * Constants and data types
44 ****************************************************************************/
45
46 #define BTA_DM_NUM_PEER_DEVICE 7
47
48 enum class tBTA_DM_CONN_STATE : uint8_t {
49 BTA_DM_NOT_CONNECTED = 0,
50 BTA_DM_CONNECTED = 1,
51 BTA_DM_UNPAIRING = 2,
52 };
53
bta_conn_state_text(tBTA_DM_CONN_STATE state)54 inline std::string bta_conn_state_text(tBTA_DM_CONN_STATE state) {
55 switch (state) {
56 CASE_RETURN_STRING(tBTA_DM_CONN_STATE::BTA_DM_NOT_CONNECTED);
57 CASE_RETURN_STRING(tBTA_DM_CONN_STATE::BTA_DM_CONNECTED);
58 CASE_RETURN_STRING(tBTA_DM_CONN_STATE::BTA_DM_UNPAIRING);
59 }
60 RETURN_UNKNOWN_TYPE_STRING(tBTA_DM_CONN_STATE, state);
61 }
62
63 typedef enum : uint8_t {
64 BTA_DM_DI_NONE = 0x00, /* nothing special */
65 BTA_DM_DI_SET_SNIFF = 0x01, /* set this bit if call BTM_SetPowerMode(sniff) */
66 BTA_DM_DI_INT_SNIFF = 0x02, /* set this bit if call BTM_SetPowerMode(sniff) &
67 enter sniff mode */
68 BTA_DM_DI_ACP_SNIFF = 0x04, /* set this bit if peer init sniff */
69 BTA_DM_DI_UNUSED = 0x08,
70 BTA_DM_DI_USE_SSR = 0x10, /* set this bit if ssr is supported for this link */
71 BTA_DM_DI_AV_ACTIVE = 0x20, /* set this bit if AV is active for this link */
72 } tBTA_DM_DEV_INFO_BITMASK;
73 typedef uint8_t tBTA_DM_DEV_INFO;
74
device_info_text(tBTA_DM_DEV_INFO info)75 inline std::string device_info_text(tBTA_DM_DEV_INFO info) {
76 const char* const device_info_text[] = {
77 ":set_sniff", ":int_sniff", ":acp_sniff",
78 ":unused", ":use_ssr", ":av_active",
79 };
80
81 std::string s = base::StringPrintf("0x%02x", info);
82 if (info == BTA_DM_DI_NONE) return s + std::string(":none");
83 for (size_t i = 0; i < sizeof(device_info_text) / sizeof(device_info_text[0]);
84 i++) {
85 if (info & (1u << i)) s += std::string(device_info_text[i]);
86 }
87 return s;
88 }
89
90 /* set power mode request type */
91 #define BTA_DM_PM_RESTART 1
92 #define BTA_DM_PM_NEW_REQ 2
93 #define BTA_DM_PM_EXECUTE 3
94 typedef uint8_t tBTA_DM_PM_REQ;
95
96 struct tBTA_DM_PEER_DEVICE {
97 RawAddress peer_bdaddr;
98 tBTA_DM_CONN_STATE conn_state{tBTA_DM_CONN_STATE::BTA_DM_NOT_CONNECTED};
99 tBTA_PREF_ROLES pref_role;
100 bool in_use;
101
102 private:
103 // Dynamic pieces of operational device information
104 tBTA_DM_DEV_INFO info{BTA_DM_DI_NONE};
105
106 public:
info_texttBTA_DM_PEER_DEVICE107 std::string info_text() const { return device_info_text(info); }
108
reset_device_infotBTA_DM_PEER_DEVICE109 void reset_device_info() { info = BTA_DM_DI_NONE; }
110
set_av_activetBTA_DM_PEER_DEVICE111 void set_av_active() { info |= BTA_DM_DI_AV_ACTIVE; }
reset_av_activetBTA_DM_PEER_DEVICE112 void reset_av_active() { info &= ~BTA_DM_DI_AV_ACTIVE; }
is_av_activetBTA_DM_PEER_DEVICE113 bool is_av_active() const { return info & BTA_DM_DI_AV_ACTIVE; }
114
set_local_init_snifftBTA_DM_PEER_DEVICE115 void set_local_init_sniff() { info |= BTA_DM_DI_INT_SNIFF; }
is_local_init_snifftBTA_DM_PEER_DEVICE116 bool is_local_init_sniff() const { return info & BTA_DM_DI_INT_SNIFF; }
set_remote_init_snifftBTA_DM_PEER_DEVICE117 void set_remote_init_sniff() { info |= BTA_DM_DI_ACP_SNIFF; }
is_remote_init_snifftBTA_DM_PEER_DEVICE118 bool is_remote_init_sniff() const { return info & BTA_DM_DI_ACP_SNIFF; }
119
set_sniff_command_senttBTA_DM_PEER_DEVICE120 void set_sniff_command_sent() { info |= BTA_DM_DI_SET_SNIFF; }
reset_sniff_command_senttBTA_DM_PEER_DEVICE121 void reset_sniff_command_sent() { info &= ~BTA_DM_DI_SET_SNIFF; }
is_sniff_command_senttBTA_DM_PEER_DEVICE122 bool is_sniff_command_sent() const { return info & BTA_DM_DI_SET_SNIFF; }
123
124 // NOTE: Why is this not used as a bitmask
set_both_device_ssr_capabletBTA_DM_PEER_DEVICE125 void set_both_device_ssr_capable() { info = BTA_DM_DI_USE_SSR; }
126
reset_sniff_flagstBTA_DM_PEER_DEVICE127 void reset_sniff_flags() {
128 info &= ~(BTA_DM_DI_INT_SNIFF | BTA_DM_DI_ACP_SNIFF | BTA_DM_DI_SET_SNIFF);
129 }
130
set_ssr_activetBTA_DM_PEER_DEVICE131 void set_ssr_active() { info |= BTA_DM_DI_USE_SSR; }
reset_ssr_activetBTA_DM_PEER_DEVICE132 void reset_ssr_active() { info &= ~BTA_DM_DI_USE_SSR; }
is_ssr_activetBTA_DM_PEER_DEVICE133 bool is_ssr_active() const { return info & BTA_DM_DI_USE_SSR; }
134
135 tBTA_DM_ENCRYPT_CBACK* p_encrypt_cback;
136 tBTM_PM_STATUS prev_low; /* previous low power mode used */
137 tBTA_DM_PM_ACTION pm_mode_attempted;
138 tBTA_DM_PM_ACTION pm_mode_failed;
139 bool remove_dev_pending;
140 tBT_TRANSPORT transport;
141 };
142
143 /* structure to store list of
144 active connections */
145 typedef struct {
146 tBTA_DM_PEER_DEVICE peer_device[BTA_DM_NUM_PEER_DEVICE];
147 uint8_t count;
148 uint8_t le_count;
149 } tBTA_DM_ACTIVE_LINK;
150
151 typedef struct {
152 RawAddress peer_bdaddr;
153 tBTA_SYS_ID id;
154 uint8_t app_id;
155 tBTA_SYS_CONN_STATUS state;
156 bool new_request;
157
ToString__anon4fb233bb1208158 std::string ToString() const {
159 return base::StringPrintf(
160 "peer:%s sys_name:%s app_id:%hhu state:%s new_request:%s",
161 ADDRESS_TO_LOGGABLE_CSTR(peer_bdaddr), BtaIdSysText(id).c_str(), app_id,
162 bta_sys_conn_status_text(state).c_str(),
163 new_request ? "true" : "false");
164 }
165
166 } tBTA_DM_SRVCS;
167
168 #ifndef BTA_DM_NUM_CONN_SRVS
169 #define BTA_DM_NUM_CONN_SRVS 30
170 #endif
171
172 typedef struct {
173 uint8_t count;
174 tBTA_DM_SRVCS conn_srvc[BTA_DM_NUM_CONN_SRVS];
175
176 } tBTA_DM_CONNECTED_SRVCS;
177
178 typedef struct {
179 #define BTA_DM_PM_SNIFF_TIMER_IDX 0
180 #define BTA_DM_PM_PARK_TIMER_IDX 1
181 #define BTA_DM_PM_SUSPEND_TIMER_IDX 2
182 #define BTA_DM_PM_MODE_TIMER_MAX 3
183 /*
184 * Keep three different timers for PARK, SNIFF and SUSPEND if TBFC is
185 * supported.
186 */
187 alarm_t* timer[BTA_DM_PM_MODE_TIMER_MAX];
188
189 uint8_t srvc_id[BTA_DM_PM_MODE_TIMER_MAX];
190 uint8_t pm_action[BTA_DM_PM_MODE_TIMER_MAX];
191 uint8_t active; /* number of active timer */
192
193 RawAddress peer_bdaddr;
194 bool in_use;
195 } tBTA_PM_TIMER;
196
197 extern tBTA_DM_CONNECTED_SRVCS bta_dm_conn_srvcs;
198
199 #define BTA_DM_NUM_PM_TIMER 7
200
201 typedef struct {
202 tBTA_DM_ACL_CBACK* p_acl_cback;
203 } tBTA_DM_ACL_CB;
204
205 /* DM control block */
206 typedef struct {
207 tBTA_DM_ACTIVE_LINK device_list;
208 tBTA_BLE_ENERGY_INFO_CBACK* p_energy_info_cback;
209 bool disabling;
210 alarm_t* disable_timer;
211 uint8_t pm_id;
212 tBTA_PM_TIMER pm_timer[BTA_DM_NUM_PM_TIMER];
213 uint8_t cur_av_count; /* current AV connecions */
214
215 /* store UUID list for EIR */
216 uint32_t eir_uuid[BTM_EIR_SERVICE_ARRAY_SIZE];
217 #if (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0)
218 tBTA_CUSTOM_UUID bta_custom_uuid[BTA_EIR_SERVER_NUM_CUSTOM_UUID];
219 #endif
220 alarm_t* switch_delay_timer;
221 } tBTA_DM_CB;
222
223 /* DI control block */
224 typedef struct {
225 uint8_t di_num; /* total local DI record number */
226 uint32_t di_handle[BTA_DI_NUM_MAX]; /* local DI record handle, the first one
227 is primary record */
228 } tBTA_DM_DI_CB;
229
230 typedef struct {
231 uint16_t page_timeout; /* timeout for page in slots */
232 bool avoid_scatter; /* true to avoid scatternet when av is streaming (be the
233 central) */
234
235 } tBTA_DM_CFG;
236
237 extern const uint32_t bta_service_id_to_btm_srv_id_lkup_tbl[];
238
239 typedef struct {
240 uint8_t id;
241 uint8_t app_id;
242 uint8_t cfg;
243
244 } tBTA_DM_RM;
245
246 extern const tBTA_DM_CFG* p_bta_dm_cfg;
247 extern const tBTA_DM_RM* p_bta_dm_rm_cfg;
248
249 typedef struct {
250 uint8_t id;
251 uint8_t app_id;
252 uint8_t spec_idx; /* index of spec table to use */
253
254 } tBTA_DM_PM_CFG;
255
256 typedef struct {
257 tBTA_DM_PM_ACTION power_mode;
258 uint16_t timeout;
259
260 } tBTA_DM_PM_ACTN;
261
262 typedef struct {
263 uint8_t allow_mask; /* mask of sniff/hold/park modes to allow */
264 uint8_t ssr; /* set SSR on conn open/unpark */
265 tBTA_DM_PM_ACTN actn_tbl[BTA_DM_PM_NUM_EVTS][2];
266
267 } tBTA_DM_PM_SPEC;
268
269 typedef struct {
270 uint16_t max_lat;
271 uint16_t min_rmt_to;
272 uint16_t min_loc_to;
273 const char* name{nullptr};
274 } tBTA_DM_SSR_SPEC;
275
276 typedef struct {
277 uint16_t manufacturer;
278 uint16_t lmp_sub_version;
279 uint8_t lmp_version;
280 } tBTA_DM_LMP_VER_INFO;
281
282 extern const uint16_t bta_service_id_to_uuid_lkup_tbl[];
283
284 /* For Insight, PM cfg lookup tables are runtime configurable (to allow tweaking
285 * of params for power consumption measurements) */
286 #ifndef BTE_SIM_APP
287 #define tBTA_DM_PM_TYPE_QUALIFIER const
288 #else
289 #define tBTA_DM_PM_TYPE_QUALIFIER
290 #endif
291
292 extern const tBTA_DM_PM_CFG* p_bta_dm_pm_cfg;
293 tBTA_DM_PM_TYPE_QUALIFIER tBTA_DM_PM_SPEC* get_bta_dm_pm_spec();
294 extern const tBTM_PM_PWR_MD* p_bta_dm_pm_md;
295 extern tBTA_DM_SSR_SPEC* p_bta_dm_ssr_spec;
296
297 /* update dynamic BRCM Aware EIR data */
298 extern const tBTA_DM_EIR_CONF bta_dm_eir_cfg;
299 extern const tBTA_DM_EIR_CONF* p_bta_dm_eir_cfg;
300
301 /* DM control block */
302 extern tBTA_DM_CB bta_dm_cb;
303
304 /* DM control block for ACL management */
305 extern tBTA_DM_ACL_CB bta_dm_acl_cb;
306
307 /* DI control block */
308 extern tBTA_DM_DI_CB bta_dm_di_cb;
309
310 void bta_dm_enable(tBTA_DM_SEC_CBACK*, tBTA_DM_ACL_CBACK*);
311 void bta_dm_disable();
312 void bta_dm_set_dev_name(const std::vector<uint8_t>&);
313
314 void bta_dm_ble_set_conn_params(const RawAddress&, uint16_t, uint16_t, uint16_t,
315 uint16_t);
316 void bta_dm_ble_update_conn_params(const RawAddress&, uint16_t, uint16_t,
317 uint16_t, uint16_t, uint16_t, uint16_t);
318
319 void bta_dm_ble_set_data_length(const RawAddress& bd_addr);
320
321 void bta_dm_ble_get_energy_info(tBTA_BLE_ENERGY_INFO_CBACK*);
322
323 void bta_dm_init_pm(void);
324 void bta_dm_disable_pm(void);
325
326 uint8_t bta_dm_get_av_count(void);
327 tBTA_DM_PEER_DEVICE* bta_dm_find_peer_device(const RawAddress& peer_addr);
328
329 void bta_dm_clear_event_filter(void);
330 void bta_dm_clear_event_mask(void);
331 void bta_dm_clear_filter_accept_list(void);
332 void bta_dm_disconnect_all_acls(void);
333 void bta_dm_le_rand(bluetooth::hci::LeRandCallback cb);
334 void bta_dm_set_event_filter_connection_setup_all_devices();
335 void bta_dm_allow_wake_by_hid(
336 std::vector<RawAddress> classic_hid_devices,
337 std::vector<std::pair<RawAddress, uint8_t>> le_hid_devices);
338 void bta_dm_restore_filter_accept_list(
339 std::vector<std::pair<RawAddress, uint8_t>> le_devices);
340 void bta_dm_set_default_event_mask_except(uint64_t mask, uint64_t le_mask);
341 void bta_dm_set_event_filter_inquiry_result_all_devices();
342
343 void bta_dm_ble_reset_id(void);
344
345 void bta_dm_eir_update_uuid(uint16_t uuid16, bool adding);
346 void bta_dm_eir_update_cust_uuid(const tBTA_CUSTOM_UUID &curr, bool adding);
347
348 void bta_dm_ble_subrate_request(const RawAddress& bd_addr, uint16_t subrate_min,
349 uint16_t subrate_max, uint16_t max_latency,
350 uint16_t cont_num, uint16_t timeout);
351
352 namespace fmt {
353 template <>
354 struct formatter<tBTA_DM_CONN_STATE> : enum_formatter<tBTA_DM_CONN_STATE> {};
355 } // namespace fmt
356
357 #endif /* BTA_DM_INT_H */
358