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