1 /******************************************************************************
2 *
3 * Copyright 2003-2014 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 API implementation file for the BTA device manager.
22 *
23 ******************************************************************************/
24
25 #include <base/functional/bind.h>
26 #include <bluetooth/log.h>
27 #include <com_android_bluetooth_flags.h>
28
29 #include <vector>
30
31 #include "bta/dm/bta_dm_device_search.h"
32 #include "bta/dm/bta_dm_disc.h"
33 #include "bta/dm/bta_dm_int.h"
34 #include "bta/dm/bta_dm_sec_int.h"
35 #include "hci/le_rand_callback.h"
36 #include "stack/include/bt_uuid16.h"
37 #include "stack/include/btm_api.h"
38 #include "stack/include/btm_client_interface.h"
39 #include "stack/include/main_thread.h"
40 #include "stack/include/sdp_api.h"
41 #include "types/bluetooth/uuid.h"
42 #include "types/raw_address.h"
43
44 using namespace bluetooth::legacy::stack::sdp;
45 using namespace bluetooth;
46
47 using bluetooth::Uuid;
48
49 /*****************************************************************************
50 * Constants
51 ****************************************************************************/
52
BTA_dm_init()53 void BTA_dm_init() {
54 /* if UUID list is not provided as static data */
55 bta_sys_eir_register(bta_dm_eir_update_uuid);
56 bta_sys_cust_eir_register(bta_dm_eir_update_cust_uuid);
57 BTM_SetConsolidationCallback(bta_dm_consolidate);
58 }
59
60 /** Enables bluetooth device under test mode */
BTA_EnableTestMode(void)61 void BTA_EnableTestMode(void) {
62 do_in_main_thread(FROM_HERE,
63 base::BindOnce(base::IgnoreResult(BTM_EnableTestMode)));
64 }
65
66 /** This function sets the Bluetooth name of local device */
BTA_DmSetDeviceName(const char * p_name)67 void BTA_DmSetDeviceName(const char* p_name) {
68 std::vector<uint8_t> name(BD_NAME_LEN + 1);
69 bd_name_from_char_pointer(name.data(), p_name);
70
71 do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_set_dev_name, name));
72 }
73
74 /*******************************************************************************
75 *
76 * Function BTA_DmSearch
77 *
78 * Description This function searches for peer Bluetooth devices. It
79 * performs an inquiry and gets the remote name for devices.
80 * Service discovery is done if services is non zero
81 *
82 * Returns void
83 *
84 ******************************************************************************/
BTA_DmSearch(tBTA_DM_SEARCH_CBACK * p_cback)85 void BTA_DmSearch(tBTA_DM_SEARCH_CBACK* p_cback) {
86 bta_dm_disc_start_device_discovery(p_cback);
87 }
88
89 /*******************************************************************************
90 *
91 * Function BTA_DmSearchCancel
92 *
93 * Description This function cancels a search initiated by BTA_DmSearch
94 *
95 * Returns void
96 *
97 ******************************************************************************/
BTA_DmSearchCancel(void)98 void BTA_DmSearchCancel(void) { bta_dm_disc_stop_device_discovery(); }
99
100 /*******************************************************************************
101 *
102 * Function BTA_DmDiscover
103 *
104 * Description This function does service discovery for services of a
105 * peer device
106 *
107 *
108 * Returns void
109 *
110 ******************************************************************************/
BTA_DmDiscover(const RawAddress & bd_addr,service_discovery_callbacks cbacks,tBT_TRANSPORT transport)111 void BTA_DmDiscover(const RawAddress& bd_addr,
112 service_discovery_callbacks cbacks,
113 tBT_TRANSPORT transport) {
114 bta_dm_disc_start_service_discovery(cbacks, bd_addr, transport);
115 }
116
117 /*******************************************************************************
118 *
119 * Function BTA_DmGetConnectionState
120 *
121 * Description Returns whether the remote device is currently connected.
122 *
123 * Returns 0 if the device is NOT connected.
124 *
125 ******************************************************************************/
BTA_DmGetConnectionState(const RawAddress & bd_addr)126 bool BTA_DmGetConnectionState(const RawAddress& bd_addr) {
127 tBTA_DM_PEER_DEVICE* p_dev = bta_dm_find_peer_device(bd_addr);
128 return (p_dev && p_dev->conn_state == tBTA_DM_CONN_STATE::BTA_DM_CONNECTED);
129 }
130
131 /*******************************************************************************
132 * Device Identification (DI) Server Functions
133 ******************************************************************************/
134 /*******************************************************************************
135 *
136 * Function BTA_DmSetLocalDiRecord
137 *
138 * Description This function adds a DI record to the local SDP database.
139 *
140 * Returns BTA_SUCCESS if record set sucessfully, otherwise error code.
141 *
142 ******************************************************************************/
BTA_DmSetLocalDiRecord(tSDP_DI_RECORD * p_device_info,uint32_t * p_handle)143 tBTA_STATUS BTA_DmSetLocalDiRecord(tSDP_DI_RECORD* p_device_info,
144 uint32_t* p_handle) {
145 tBTA_STATUS status = BTA_FAILURE;
146
147 if (bta_dm_di_cb.di_num < BTA_DI_NUM_MAX) {
148 if (get_legacy_stack_sdp_api()->device_id.SDP_SetLocalDiRecord(
149 (tSDP_DI_RECORD*)p_device_info, p_handle) == SDP_SUCCESS) {
150 if (!p_device_info->primary_record) {
151 bta_dm_di_cb.di_handle[bta_dm_di_cb.di_num] = *p_handle;
152 bta_dm_di_cb.di_num++;
153 }
154
155 bta_sys_add_uuid(UUID_SERVCLASS_PNP_INFORMATION);
156 status = BTA_SUCCESS;
157 }
158 }
159
160 return status;
161 }
162
163 /*******************************************************************************
164 *
165 * Function BTA_DmSetBlePrefConnParams
166 *
167 * Description This function is called to set the preferred connection
168 * parameters when default connection parameter is not desired.
169 *
170 * Parameters: bd_addr - BD address of the peripheral
171 * scan_interval - scan interval
172 * scan_window - scan window
173 * min_conn_int - minimum preferred connection interval
174 * max_conn_int - maximum preferred connection interval
175 * peripheral_latency - preferred peripheral latency
176 * supervision_tout - preferred supervision timeout
177 *
178 *
179 * Returns void
180 *
181 ******************************************************************************/
BTA_DmSetBlePrefConnParams(const RawAddress & bd_addr,uint16_t min_conn_int,uint16_t max_conn_int,uint16_t peripheral_latency,uint16_t supervision_tout)182 void BTA_DmSetBlePrefConnParams(const RawAddress& bd_addr,
183 uint16_t min_conn_int, uint16_t max_conn_int,
184 uint16_t peripheral_latency,
185 uint16_t supervision_tout) {
186 do_in_main_thread(
187 FROM_HERE,
188 base::BindOnce(bta_dm_ble_set_conn_params, bd_addr, min_conn_int,
189 max_conn_int, peripheral_latency, supervision_tout));
190 }
191
192 /*******************************************************************************
193 *
194 * Function BTA_DmBleUpdateConnectionParam
195 *
196 * Description Update connection parameters, can only be used when
197 * connection is up.
198 *
199 * Parameters: bd_addr - BD address of the peer
200 * min_int - minimum connection interval,
201 * [0x0004 ~ 0x4000]
202 * max_int - maximum connection interval,
203 * [0x0004 ~ 0x4000]
204 * latency - peripheral latency [0 ~ 500]
205 * timeout - supervision timeout [0x000a ~ 0xc80]
206 *
207 * Returns void
208 *
209 ******************************************************************************/
BTA_DmBleUpdateConnectionParams(const RawAddress & bd_addr,uint16_t min_int,uint16_t max_int,uint16_t latency,uint16_t timeout,uint16_t min_ce_len,uint16_t max_ce_len)210 void BTA_DmBleUpdateConnectionParams(const RawAddress& bd_addr,
211 uint16_t min_int, uint16_t max_int,
212 uint16_t latency, uint16_t timeout,
213 uint16_t min_ce_len, uint16_t max_ce_len) {
214 do_in_main_thread(
215 FROM_HERE,
216 base::BindOnce(bta_dm_ble_update_conn_params, bd_addr, min_int, max_int,
217 latency, timeout, min_ce_len, max_ce_len));
218 }
219
220 /*******************************************************************************
221 *
222 * Function BTA_DmBleConfigLocalPrivacy
223 *
224 * Description Enable/disable privacy on the local device
225 *
226 * Parameters: privacy_enable - enable/disabe privacy on remote device.
227 *
228 * Returns void
229 *
230 ******************************************************************************/
BTA_DmBleConfigLocalPrivacy(bool privacy_enable)231 void BTA_DmBleConfigLocalPrivacy(bool privacy_enable) {
232 if (com::android::bluetooth::flags::synchronous_bta_sec()) {
233 bta_dm_ble_config_local_privacy(privacy_enable);
234 } else {
235 do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_ble_config_local_privacy,
236 privacy_enable));
237 }
238 }
239
240 /*******************************************************************************
241 *
242 * Function BTA_DmBleGetEnergyInfo
243 *
244 * Description This function is called to obtain the energy info
245 *
246 * Parameters p_cmpl_cback - Command complete callback
247 *
248 * Returns void
249 *
250 ******************************************************************************/
BTA_DmBleGetEnergyInfo(tBTA_BLE_ENERGY_INFO_CBACK * p_cmpl_cback)251 void BTA_DmBleGetEnergyInfo(tBTA_BLE_ENERGY_INFO_CBACK* p_cmpl_cback) {
252 do_in_main_thread(FROM_HERE,
253 base::BindOnce(bta_dm_ble_get_energy_info, p_cmpl_cback));
254 }
255
256 /** This function is to set maximum LE data packet size */
BTA_DmBleRequestMaxTxDataLength(const RawAddress & remote_device)257 void BTA_DmBleRequestMaxTxDataLength(const RawAddress& remote_device) {
258 do_in_main_thread(FROM_HERE,
259 base::BindOnce(bta_dm_ble_set_data_length, remote_device));
260 }
261
262 /*******************************************************************************
263 *
264 * Function BTA_DmBleScan
265 *
266 * Description Start or stop the scan procedure.
267 *
268 * Parameters start: start or stop the scan procedure,
269 * duration_sec: Duration of the scan. Continuous scan if 0 is
270 * passed,
271 * low_latency_scan: whether this is an low latency scan,
272 * default is false.
273 *
274 * Returns void
275 *
276 ******************************************************************************/
BTA_DmBleScan(bool start,uint8_t duration_sec,bool low_latency_scan)277 void BTA_DmBleScan(bool start, uint8_t duration_sec, bool low_latency_scan) {
278 log::verbose("start = {}", start);
279 do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_ble_scan, start,
280 duration_sec, low_latency_scan));
281 }
282
283 /*******************************************************************************
284 *
285 * Function BTA_DmBleCsisObserve
286 *
287 * Description This procedure keeps the external observer listening for
288 * advertising events from a CSIS grouped device.
289 *
290 * Parameters observe: enable or disable passive observe,
291 * p_results_cb: Callback to be called with scan results,
292 *
293 * Returns void
294 *
295 ******************************************************************************/
BTA_DmBleCsisObserve(bool observe,tBTA_DM_SEARCH_CBACK * p_results_cb)296 void BTA_DmBleCsisObserve(bool observe, tBTA_DM_SEARCH_CBACK* p_results_cb) {
297 log::verbose("enable = {}", observe);
298 do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_ble_csis_observe, observe,
299 p_results_cb));
300 }
301
302 /*******************************************************************************
303 *
304 * Function BTA_DmClearEventFilter
305 *
306 * Description This function clears the event filter
307 *
308 * Returns void
309 *
310 ******************************************************************************/
BTA_DmClearEventFilter(void)311 void BTA_DmClearEventFilter(void) {
312 log::verbose("BTA_DmClearEventFilter");
313 do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_clear_event_filter));
314 }
315
316 /*******************************************************************************
317 *
318 * Function BTA_DmClearEventMask
319 *
320 * Description This function clears the event mask
321 *
322 * Returns void
323 *
324 ******************************************************************************/
BTA_DmClearEventMask(void)325 void BTA_DmClearEventMask(void) {
326 log::verbose("BTA_DmClearEventMask");
327 do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_clear_event_mask));
328 }
329
330 /*******************************************************************************
331 *
332 * Function BTA_DmClearEventMask
333 *
334 * Description This function clears the filter accept list
335 *
336 * Returns void
337 *
338 ******************************************************************************/
BTA_DmClearFilterAcceptList(void)339 void BTA_DmClearFilterAcceptList(void) {
340 log::verbose("BTA_DmClearFilterAcceptList");
341 do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_clear_filter_accept_list));
342 }
343
344 /*******************************************************************************
345 *
346 * Function BTA_DmLeRand
347 *
348 * Description This function clears the event filter
349 *
350 * Returns cb: callback to receive the resulting random number
351 *
352 ******************************************************************************/
BTA_DmLeRand(bluetooth::hci::LeRandCallback cb)353 void BTA_DmLeRand(bluetooth::hci::LeRandCallback cb) {
354 log::verbose("BTA_DmLeRand");
355 do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_le_rand, std::move(cb)));
356 }
357
358 /*******************************************************************************
359 *
360 * Function BTA_DmDisconnectAllAcls
361 *
362 * Description This function will disconnect all LE and Classic ACLs.
363 *
364 * Returns void
365 *
366 ******************************************************************************/
BTA_DmDisconnectAllAcls()367 void BTA_DmDisconnectAllAcls() {
368 log::verbose("BTA_DmLeRand");
369 do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_disconnect_all_acls));
370 }
371
BTA_DmSetEventFilterConnectionSetupAllDevices()372 void BTA_DmSetEventFilterConnectionSetupAllDevices() {
373 log::verbose("BTA_DmSetEventFilterConnectionSetupAllDevices");
374 do_in_main_thread(
375 FROM_HERE,
376 base::BindOnce(bta_dm_set_event_filter_connection_setup_all_devices));
377 }
378
BTA_DmAllowWakeByHid(std::vector<RawAddress> classic_hid_devices,std::vector<std::pair<RawAddress,uint8_t>> le_hid_devices)379 void BTA_DmAllowWakeByHid(
380 std::vector<RawAddress> classic_hid_devices,
381 std::vector<std::pair<RawAddress, uint8_t>> le_hid_devices) {
382 log::verbose("BTA_DmAllowWakeByHid");
383 do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_allow_wake_by_hid,
384 std::move(classic_hid_devices),
385 std::move(le_hid_devices)));
386 }
387
BTA_DmRestoreFilterAcceptList(std::vector<std::pair<RawAddress,uint8_t>> le_devices)388 void BTA_DmRestoreFilterAcceptList(
389 std::vector<std::pair<RawAddress, uint8_t>> le_devices) {
390 log::verbose("BTA_DmRestoreFilterAcceptList");
391 do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_restore_filter_accept_list,
392 std::move(le_devices)));
393 }
394
BTA_DmSetDefaultEventMaskExcept(uint64_t mask,uint64_t le_mask)395 void BTA_DmSetDefaultEventMaskExcept(uint64_t mask, uint64_t le_mask) {
396 log::verbose("BTA_DmSetDefaultEventMaskExcept");
397 do_in_main_thread(
398 FROM_HERE,
399 base::BindOnce(bta_dm_set_default_event_mask_except, mask, le_mask));
400 }
401
BTA_DmSetEventFilterInquiryResultAllDevices()402 void BTA_DmSetEventFilterInquiryResultAllDevices() {
403 log::verbose("BTA_DmSetEventFilterInquiryResultAllDevices");
404 do_in_main_thread(
405 FROM_HERE,
406 base::BindOnce(bta_dm_set_event_filter_inquiry_result_all_devices));
407 }
408
409 /*******************************************************************************
410 *
411 * Function BTA_DmBleResetId
412 *
413 * Description This function resets the ble keys such as IRK
414 *
415 * Returns void
416 *
417 ******************************************************************************/
BTA_DmBleResetId(void)418 void BTA_DmBleResetId(void) {
419 log::verbose("BTA_DmBleResetId");
420 do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_ble_reset_id));
421 }
422
423 /*******************************************************************************
424 *
425 * Function BTA_DmBleSubrateRequest
426 *
427 * Description subrate request, can only be used when connection is up.
428 *
429 * Parameters: bd_addr - BD address of the peer
430 * subrate_min - subrate factor minimum, [0x0001 - 0x01F4]
431 * subrate_max - subrate factor maximum, [0x0001 - 0x01F4]
432 * max_latency - max peripheral latency [0x0000 - 01F3]
433 * cont_num - continuation number [0x0000 - 01F3]
434 * timeout - supervision timeout [0x000a - 0xc80]
435 *
436 * Returns void
437 *
438 ******************************************************************************/
BTA_DmBleSubrateRequest(const RawAddress & bd_addr,uint16_t subrate_min,uint16_t subrate_max,uint16_t max_latency,uint16_t cont_num,uint16_t timeout)439 void BTA_DmBleSubrateRequest(const RawAddress& bd_addr, uint16_t subrate_min,
440 uint16_t subrate_max, uint16_t max_latency,
441 uint16_t cont_num, uint16_t timeout) {
442 log::verbose("");
443 do_in_main_thread(FROM_HERE, base::BindOnce(bta_dm_ble_subrate_request,
444 bd_addr, subrate_min, subrate_max,
445 max_latency, cont_num, timeout));
446 }
447
BTA_DmCheckLeAudioCapable(const RawAddress & address)448 bool BTA_DmCheckLeAudioCapable(const RawAddress& address) {
449 for (tBTM_INQ_INFO* inq_ent = get_btm_client_interface().db.BTM_InqDbFirst();
450 inq_ent != nullptr;
451 inq_ent = get_btm_client_interface().db.BTM_InqDbNext(inq_ent)) {
452 if (inq_ent->results.remote_bd_addr != address) continue;
453
454 if (inq_ent->results.ble_ad_is_le_audio_capable) {
455 log::info("Device is LE Audio capable based on AD content");
456 return true;
457 }
458
459 return false;
460 }
461 return false;
462 }
463