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