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 #include <base/bind_helpers.h>
25 #include <string.h>
26 
27 #include "bt_common.h"
28 #include "bta_api.h"
29 #include "bta_closure_api.h"
30 #include "bta_dm_int.h"
31 #include "bta_sys.h"
32 #include "bta_sys_int.h"
33 #include "btm_api.h"
34 #include "btm_int.h"
35 #include "osi/include/osi.h"
36 #include "utl.h"
37 
38 using bluetooth::Uuid;
39 
40 /*****************************************************************************
41  *  Constants
42  ****************************************************************************/
43 
44 static const tBTA_SYS_REG bta_dm_search_reg = {bta_dm_search_sm_execute,
45                                                bta_dm_search_sm_disable};
46 
47 /*******************************************************************************
48  *
49  * Function         BTA_EnableBluetooth
50  *
51  * Description      Enables bluetooth service.  This function must be
52  *                  called before any other functions in the BTA API are called.
53  *
54  *
55  * Returns          tBTA_STATUS
56  *
57  ******************************************************************************/
BTA_EnableBluetooth(tBTA_DM_SEC_CBACK * p_cback)58 tBTA_STATUS BTA_EnableBluetooth(tBTA_DM_SEC_CBACK* p_cback) {
59   /* Bluetooth disabling is in progress */
60   if (bta_dm_cb.disabling) return BTA_FAILURE;
61 
62   bta_sys_register(BTA_ID_DM_SEARCH, &bta_dm_search_reg);
63 
64   /* if UUID list is not provided as static data */
65   bta_sys_eir_register(bta_dm_eir_update_uuid);
66 
67   do_in_bta_thread(FROM_HERE, base::Bind(bta_dm_enable, p_cback));
68   return BTA_SUCCESS;
69 }
70 
71 /*******************************************************************************
72  *
73  * Function         BTA_DisableBluetooth
74  *
75  * Description      Disables bluetooth service.  This function is called when
76  *                  the application no longer needs bluetooth service
77  *
78  * Returns          void
79  *
80  ******************************************************************************/
BTA_DisableBluetooth(void)81 tBTA_STATUS BTA_DisableBluetooth(void) {
82   do_in_bta_thread(FROM_HERE, base::Bind(bta_dm_disable));
83   return BTA_SUCCESS;
84 }
85 
86 /** Enables bluetooth device under test mode */
BTA_EnableTestMode(void)87 void BTA_EnableTestMode(void) {
88   do_in_bta_thread(FROM_HERE,
89                    base::Bind(base::IgnoreResult(BTM_EnableTestMode)));
90 }
91 
92 /** Disable bluetooth device under test mode */
BTA_DisableTestMode(void)93 void BTA_DisableTestMode(void) {
94   do_in_bta_thread(FROM_HERE, base::Bind(BTM_DeviceReset, nullptr));
95 }
96 
97 /** This function sets the Bluetooth name of local device */
BTA_DmSetDeviceName(char * p_name)98 void BTA_DmSetDeviceName(char* p_name) {
99   std::vector<uint8_t> name(BD_NAME_LEN);
100   strlcpy((char*)name.data(), p_name, BD_NAME_LEN);
101 
102   do_in_bta_thread(FROM_HERE, base::Bind(bta_dm_set_dev_name, name));
103 }
104 
105 /** This function sets the Bluetooth connectable, discoverable, pairable and
106  * conn paired only modes of local device
107  */
BTA_DmSetVisibility(tBTA_DM_DISC disc_mode,tBTA_DM_CONN conn_mode,uint8_t pairable_mode,uint8_t conn_paired_only)108 void BTA_DmSetVisibility(tBTA_DM_DISC disc_mode, tBTA_DM_CONN conn_mode,
109                          uint8_t pairable_mode, uint8_t conn_paired_only) {
110   do_in_bta_thread(FROM_HERE,
111                    base::Bind(bta_dm_set_visibility, disc_mode, conn_mode,
112                               pairable_mode, conn_paired_only));
113 }
114 
115 /*******************************************************************************
116  *
117  * Function         BTA_DmSearch
118  *
119  * Description      This function searches for peer Bluetooth devices. It
120  *                  performs an inquiry and gets the remote name for devices.
121  *                  Service discovery is done if services is non zero
122  *
123  *
124  * Returns          void
125  *
126  ******************************************************************************/
BTA_DmSearch(tBTA_DM_INQ * p_dm_inq,tBTA_SERVICE_MASK services,tBTA_DM_SEARCH_CBACK * p_cback)127 void BTA_DmSearch(tBTA_DM_INQ* p_dm_inq, tBTA_SERVICE_MASK services,
128                   tBTA_DM_SEARCH_CBACK* p_cback) {
129   tBTA_DM_API_SEARCH* p_msg =
130       (tBTA_DM_API_SEARCH*)osi_calloc(sizeof(tBTA_DM_API_SEARCH));
131 
132   p_msg->hdr.event = BTA_DM_API_SEARCH_EVT;
133   memcpy(&p_msg->inq_params, p_dm_inq, sizeof(tBTA_DM_INQ));
134   p_msg->services = services;
135   p_msg->p_cback = p_cback;
136   p_msg->rs_res = BTA_DM_RS_NONE;
137 
138   bta_sys_sendmsg(p_msg);
139 }
140 
141 /*******************************************************************************
142  *
143  * Function         BTA_DmSearchCancel
144  *
145  * Description      This function  cancels a search initiated by BTA_DmSearch
146  *
147  *
148  * Returns          void
149  *
150  ******************************************************************************/
BTA_DmSearchCancel(void)151 void BTA_DmSearchCancel(void) {
152   BT_HDR* p_msg = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
153 
154   p_msg->event = BTA_DM_API_SEARCH_CANCEL_EVT;
155   bta_sys_sendmsg(p_msg);
156 }
157 
158 /*******************************************************************************
159  *
160  * Function         BTA_DmDiscover
161  *
162  * Description      This function does service discovery for services of a
163  *                  peer device
164  *
165  *
166  * Returns          void
167  *
168  ******************************************************************************/
BTA_DmDiscover(const RawAddress & bd_addr,tBTA_SERVICE_MASK services,tBTA_DM_SEARCH_CBACK * p_cback,bool sdp_search)169 void BTA_DmDiscover(const RawAddress& bd_addr, tBTA_SERVICE_MASK services,
170                     tBTA_DM_SEARCH_CBACK* p_cback, bool sdp_search) {
171   tBTA_DM_API_DISCOVER* p_msg =
172       (tBTA_DM_API_DISCOVER*)osi_calloc(sizeof(tBTA_DM_API_DISCOVER));
173 
174   p_msg->hdr.event = BTA_DM_API_DISCOVER_EVT;
175   p_msg->bd_addr = bd_addr;
176   p_msg->services = services;
177   p_msg->p_cback = p_cback;
178   p_msg->sdp_search = sdp_search;
179 
180   bta_sys_sendmsg(p_msg);
181 }
182 
183 /*******************************************************************************
184  *
185  * Function         BTA_DmDiscoverUUID
186  *
187  * Description      This function does service discovery for services of a
188  *                  peer device
189  *
190  *
191  * Returns          void
192  *
193  ******************************************************************************/
BTA_DmDiscoverUUID(const RawAddress & bd_addr,const Uuid & uuid,tBTA_DM_SEARCH_CBACK * p_cback,bool sdp_search)194 void BTA_DmDiscoverUUID(const RawAddress& bd_addr, const Uuid& uuid,
195                         tBTA_DM_SEARCH_CBACK* p_cback, bool sdp_search) {
196   tBTA_DM_API_DISCOVER* p_msg =
197       (tBTA_DM_API_DISCOVER*)osi_malloc(sizeof(tBTA_DM_API_DISCOVER));
198 
199   p_msg->hdr.event = BTA_DM_API_DISCOVER_EVT;
200   p_msg->bd_addr = bd_addr;
201   p_msg->services = BTA_USER_SERVICE_MASK;  // Not exposed at API level
202   p_msg->p_cback = p_cback;
203   p_msg->sdp_search = sdp_search;
204 
205   p_msg->num_uuid = 0;
206   p_msg->p_uuid = NULL;
207   p_msg->uuid = uuid;
208 
209   bta_sys_sendmsg(p_msg);
210 }
211 
212 /** This function initiates a bonding procedure with a peer device */
BTA_DmBond(const RawAddress & bd_addr)213 void BTA_DmBond(const RawAddress& bd_addr) {
214   do_in_bta_thread(FROM_HERE,
215                    base::Bind(bta_dm_bond, bd_addr, BTA_TRANSPORT_UNKNOWN));
216 }
217 
218 /** This function initiates a bonding procedure with a peer device */
BTA_DmBondByTransport(const RawAddress & bd_addr,tBTA_TRANSPORT transport)219 void BTA_DmBondByTransport(const RawAddress& bd_addr,
220                            tBTA_TRANSPORT transport) {
221   do_in_bta_thread(FROM_HERE, base::Bind(bta_dm_bond, bd_addr, transport));
222 }
223 
224 /** This function cancels the bonding procedure with a peer device
225  */
BTA_DmBondCancel(const RawAddress & bd_addr)226 void BTA_DmBondCancel(const RawAddress& bd_addr) {
227   do_in_bta_thread(FROM_HERE, base::Bind(bta_dm_bond_cancel, bd_addr));
228 }
229 
230 /*******************************************************************************
231  *
232  * Function         BTA_DmPinReply
233  *
234  * Description      This function provides a pincode for a remote device when
235  *                  one is requested by DM through BTA_DM_PIN_REQ_EVT
236  *
237  *
238  * Returns          void
239  *
240  ******************************************************************************/
BTA_DmPinReply(const RawAddress & bd_addr,bool accept,uint8_t pin_len,uint8_t * p_pin)241 void BTA_DmPinReply(const RawAddress& bd_addr, bool accept, uint8_t pin_len,
242                     uint8_t* p_pin) {
243   std::unique_ptr<tBTA_DM_API_PIN_REPLY> msg =
244       std::make_unique<tBTA_DM_API_PIN_REPLY>();
245 
246   msg->bd_addr = bd_addr;
247   msg->accept = accept;
248   if (accept) {
249     msg->pin_len = pin_len;
250     memcpy(msg->p_pin, p_pin, pin_len);
251   }
252 
253   do_in_bta_thread(FROM_HERE, base::Bind(bta_dm_pin_reply, base::Passed(&msg)));
254 }
255 
256 /*******************************************************************************
257  *
258  * Function         BTA_DmLocalOob
259  *
260  * Description      This function retrieves the OOB data from local controller.
261  *                  The result is reported by:
262  *                  - bta_dm_co_loc_oob_ext() if device supports secure
263  *                    connections (SC)
264  *                  - bta_dm_co_loc_oob() if device doesn't support SC
265  *
266  * Returns          void
267  *
268  ******************************************************************************/
BTA_DmLocalOob(void)269 void BTA_DmLocalOob(void) {
270   do_in_bta_thread(FROM_HERE, base::Bind(BTM_ReadLocalOobData));
271 }
272 
273 /*******************************************************************************
274  *
275  * Function         BTA_DmConfirm
276  *
277  * Description      This function accepts or rejects the numerical value of the
278  *                  Simple Pairing process on BTA_DM_SP_CFM_REQ_EVT
279  *
280  * Returns          void
281  *
282  ******************************************************************************/
BTA_DmConfirm(const RawAddress & bd_addr,bool accept)283 void BTA_DmConfirm(const RawAddress& bd_addr, bool accept) {
284   do_in_bta_thread(FROM_HERE, base::Bind(bta_dm_confirm, bd_addr, accept));
285 }
286 
287 /*******************************************************************************
288  *
289  * Function         BTA_DmAddDevice
290  *
291  * Description      This function adds a device to the security database list of
292  *                  peer device
293  *
294  *
295  * Returns          void
296  *
297  ******************************************************************************/
BTA_DmAddDevice(const RawAddress & bd_addr,DEV_CLASS dev_class,LINK_KEY link_key,tBTA_SERVICE_MASK trusted_mask,bool is_trusted,uint8_t key_type,tBTA_IO_CAP io_cap,uint8_t pin_length)298 void BTA_DmAddDevice(const RawAddress& bd_addr, DEV_CLASS dev_class,
299                      LINK_KEY link_key, tBTA_SERVICE_MASK trusted_mask,
300                      bool is_trusted, uint8_t key_type, tBTA_IO_CAP io_cap,
301                      uint8_t pin_length) {
302   std::unique_ptr<tBTA_DM_API_ADD_DEVICE> msg =
303       std::make_unique<tBTA_DM_API_ADD_DEVICE>();
304 
305   msg->bd_addr = bd_addr;
306   msg->tm = trusted_mask;
307   msg->is_trusted = is_trusted;
308   msg->io_cap = io_cap;
309 
310   if (link_key) {
311     msg->link_key_known = true;
312     msg->key_type = key_type;
313     memcpy(msg->link_key, link_key, LINK_KEY_LEN);
314   }
315 
316   /* Load device class if specified */
317   if (dev_class) {
318     msg->dc_known = true;
319     memcpy(msg->dc, dev_class, DEV_CLASS_LEN);
320   }
321 
322   memset(msg->bd_name, 0, BD_NAME_LEN + 1);
323   memset(msg->features, 0, sizeof(msg->features));
324   msg->pin_length = pin_length;
325 
326   do_in_bta_thread(FROM_HERE,
327                    base::Bind(bta_dm_add_device, base::Passed(&msg)));
328 }
329 
330 /** This function removes a device fromthe security database list of peer
331  * device. It manages unpairing even while connected */
BTA_DmRemoveDevice(const RawAddress & bd_addr)332 tBTA_STATUS BTA_DmRemoveDevice(const RawAddress& bd_addr) {
333   do_in_bta_thread(FROM_HERE, base::Bind(bta_dm_remove_device, bd_addr));
334   return BTA_SUCCESS;
335 }
336 
337 /*******************************************************************************
338  *
339  * Function         BTA_GetEirService
340  *
341  * Description      This function is called to get BTA service mask from EIR.
342  *
343  * Parameters       p_eir - pointer of EIR significant part
344  *                  p_services - return the BTA service mask
345  *
346  * Returns          None
347  *
348  ******************************************************************************/
349 extern const uint16_t bta_service_id_to_uuid_lkup_tbl[];
BTA_GetEirService(uint8_t * p_eir,size_t eir_len,tBTA_SERVICE_MASK * p_services)350 void BTA_GetEirService(uint8_t* p_eir, size_t eir_len,
351                        tBTA_SERVICE_MASK* p_services) {
352   uint8_t xx, yy;
353   uint8_t num_uuid, max_num_uuid = 32;
354   uint8_t uuid_list[32 * Uuid::kNumBytes16];
355   uint16_t* p_uuid16 = (uint16_t*)uuid_list;
356   tBTA_SERVICE_MASK mask;
357 
358   BTM_GetEirUuidList(p_eir, eir_len, Uuid::kNumBytes16, &num_uuid, uuid_list,
359                      max_num_uuid);
360   for (xx = 0; xx < num_uuid; xx++) {
361     mask = 1;
362     for (yy = 0; yy < BTA_MAX_SERVICE_ID; yy++) {
363       if (*(p_uuid16 + xx) == bta_service_id_to_uuid_lkup_tbl[yy]) {
364         *p_services |= mask;
365         break;
366       }
367       mask <<= 1;
368     }
369 
370     /* for HSP v1.2 only device */
371     if (*(p_uuid16 + xx) == UUID_SERVCLASS_HEADSET_HS)
372       *p_services |= BTA_HSP_SERVICE_MASK;
373 
374     if (*(p_uuid16 + xx) == UUID_SERVCLASS_HDP_SOURCE)
375       *p_services |= BTA_HL_SERVICE_MASK;
376 
377     if (*(p_uuid16 + xx) == UUID_SERVCLASS_HDP_SINK)
378       *p_services |= BTA_HL_SERVICE_MASK;
379   }
380 }
381 
382 /*******************************************************************************
383  *
384  * Function         BTA_DmGetConnectionState
385  *
386  * Description      Returns whether the remote device is currently connected.
387  *
388  * Returns          0 if the device is NOT connected.
389  *
390  ******************************************************************************/
BTA_DmGetConnectionState(const RawAddress & bd_addr)391 uint16_t BTA_DmGetConnectionState(const RawAddress& bd_addr) {
392   tBTA_DM_PEER_DEVICE* p_dev = bta_dm_find_peer_device(bd_addr);
393   return (p_dev && p_dev->conn_state == BTA_DM_CONNECTED);
394 }
395 
396 /*******************************************************************************
397  *                   Device Identification (DI) Server Functions
398  ******************************************************************************/
399 /*******************************************************************************
400  *
401  * Function         BTA_DmSetLocalDiRecord
402  *
403  * Description      This function adds a DI record to the local SDP database.
404  *
405  * Returns          BTA_SUCCESS if record set sucessfully, otherwise error code.
406  *
407  ******************************************************************************/
BTA_DmSetLocalDiRecord(tBTA_DI_RECORD * p_device_info,uint32_t * p_handle)408 tBTA_STATUS BTA_DmSetLocalDiRecord(tBTA_DI_RECORD* p_device_info,
409                                    uint32_t* p_handle) {
410   tBTA_STATUS status = BTA_FAILURE;
411 
412   if (bta_dm_di_cb.di_num < BTA_DI_NUM_MAX) {
413     if (SDP_SetLocalDiRecord((tSDP_DI_RECORD*)p_device_info, p_handle) ==
414         SDP_SUCCESS) {
415       if (!p_device_info->primary_record) {
416         bta_dm_di_cb.di_handle[bta_dm_di_cb.di_num] = *p_handle;
417         bta_dm_di_cb.di_num++;
418       }
419 
420       bta_sys_add_uuid(UUID_SERVCLASS_PNP_INFORMATION);
421       status = BTA_SUCCESS;
422     }
423   }
424 
425   return status;
426 }
427 
428 /*******************************************************************************
429  *
430  * Function         BTA_DmAddBleKey
431  *
432  * Description      Add/modify LE device information.  This function will be
433  *                  normally called during host startup to restore all required
434  *                  information stored in the NVRAM.
435  *
436  * Parameters:      bd_addr          - BD address of the peer
437  *                  p_le_key         - LE key values.
438  *                  key_type         - LE SMP key type.
439  *
440  * Returns          BTA_SUCCESS if successful
441  *                  BTA_FAIL if operation failed.
442  *
443  ******************************************************************************/
BTA_DmAddBleKey(const RawAddress & bd_addr,tBTA_LE_KEY_VALUE * p_le_key,tBTA_LE_KEY_TYPE key_type)444 void BTA_DmAddBleKey(const RawAddress& bd_addr, tBTA_LE_KEY_VALUE* p_le_key,
445                      tBTA_LE_KEY_TYPE key_type) {
446   do_in_bta_thread(FROM_HERE,
447                    base::Bind(bta_dm_add_blekey, bd_addr, *p_le_key, key_type));
448 }
449 
450 /*******************************************************************************
451  *
452  * Function         BTA_DmAddBleDevice
453  *
454  * Description      Add a BLE device.  This function will be normally called
455  *                  during host startup to restore all required information
456  *                  for a LE device stored in the NVRAM.
457  *
458  * Parameters:      bd_addr          - BD address of the peer
459  *                  dev_type         - Remote device's device type.
460  *                  addr_type        - LE device address type.
461  *
462  * Returns          void
463  *
464  ******************************************************************************/
BTA_DmAddBleDevice(const RawAddress & bd_addr,tBLE_ADDR_TYPE addr_type,tBT_DEVICE_TYPE dev_type)465 void BTA_DmAddBleDevice(const RawAddress& bd_addr, tBLE_ADDR_TYPE addr_type,
466                         tBT_DEVICE_TYPE dev_type) {
467   do_in_bta_thread(FROM_HERE, base::Bind(bta_dm_add_ble_device, bd_addr,
468                                          addr_type, dev_type));
469 }
470 
471 /*******************************************************************************
472  *
473  * Function         BTA_DmBlePasskeyReply
474  *
475  * Description      Send BLE SMP passkey reply.
476  *
477  * Parameters:      bd_addr          - BD address of the peer
478  *                  accept           - passkey entry sucessful or declined.
479  *                  passkey          - passkey value, must be a 6 digit number,
480  *                                     can be lead by 0.
481  *
482  * Returns          void
483  *
484  ******************************************************************************/
BTA_DmBlePasskeyReply(const RawAddress & bd_addr,bool accept,uint32_t passkey)485 void BTA_DmBlePasskeyReply(const RawAddress& bd_addr, bool accept,
486                            uint32_t passkey) {
487   do_in_bta_thread(FROM_HERE, base::Bind(bta_dm_ble_passkey_reply, bd_addr,
488                                          accept, accept ? passkey : 0));
489 }
490 
491 /*******************************************************************************
492  *
493  * Function         BTA_DmBleConfirmReply
494  *
495  * Description      Send BLE SMP SC user confirmation reply.
496  *
497  * Parameters:      bd_addr          - BD address of the peer
498  *                  accept           - numbers to compare are the same or
499  *                                     different.
500  *
501  * Returns          void
502  *
503  ******************************************************************************/
BTA_DmBleConfirmReply(const RawAddress & bd_addr,bool accept)504 void BTA_DmBleConfirmReply(const RawAddress& bd_addr, bool accept) {
505   do_in_bta_thread(FROM_HERE,
506                    base::Bind(bta_dm_ble_confirm_reply, bd_addr, accept));
507 }
508 
509 /*******************************************************************************
510  *
511  * Function         BTA_DmBleSecurityGrant
512  *
513  * Description      Grant security request access.
514  *
515  * Parameters:      bd_addr          - BD address of the peer
516  *                  res              - security grant status.
517  *
518  * Returns          void
519  *
520  ******************************************************************************/
BTA_DmBleSecurityGrant(const RawAddress & bd_addr,tBTA_DM_BLE_SEC_GRANT res)521 void BTA_DmBleSecurityGrant(const RawAddress& bd_addr,
522                             tBTA_DM_BLE_SEC_GRANT res) {
523   do_in_bta_thread(FROM_HERE, base::Bind(BTM_SecurityGrant, bd_addr, res));
524 }
525 
526 /*******************************************************************************
527  *
528  * Function         BTA_DmSetBlePrefConnParams
529  *
530  * Description      This function is called to set the preferred connection
531  *                  parameters when default connection parameter is not desired.
532  *
533  * Parameters:      bd_addr          - BD address of the peripheral
534  *                  scan_interval    - scan interval
535  *                  scan_window      - scan window
536  *                  min_conn_int     - minimum preferred connection interval
537  *                  max_conn_int     - maximum preferred connection interval
538  *                  slave_latency    - preferred slave latency
539  *                  supervision_tout - preferred supervision timeout
540  *
541  *
542  * Returns          void
543  *
544  ******************************************************************************/
BTA_DmSetBlePrefConnParams(const RawAddress & bd_addr,uint16_t min_conn_int,uint16_t max_conn_int,uint16_t slave_latency,uint16_t supervision_tout)545 void BTA_DmSetBlePrefConnParams(const RawAddress& bd_addr,
546                                 uint16_t min_conn_int, uint16_t max_conn_int,
547                                 uint16_t slave_latency,
548                                 uint16_t supervision_tout) {
549   do_in_bta_thread(FROM_HERE,
550                    base::Bind(bta_dm_ble_set_conn_params, bd_addr, min_conn_int,
551                               max_conn_int, slave_latency, supervision_tout));
552 }
553 
554 /*******************************************************************************
555  *
556  * Function         BTA_DmSetBleConnScanParams
557  *
558  * Description      This function is called to set scan parameters used in
559  *                  BLE connection request
560  *
561  * Parameters:      scan_interval    - scan interval
562  *                  scan_window      - scan window
563  *
564  * Returns          void
565  *
566  ******************************************************************************/
BTA_DmSetBleConnScanParams(uint32_t scan_interval,uint32_t scan_window)567 void BTA_DmSetBleConnScanParams(uint32_t scan_interval, uint32_t scan_window) {
568   do_in_bta_thread(FROM_HERE, base::Bind(bta_dm_ble_set_conn_scan_params,
569                                          scan_interval, scan_window));
570 }
571 
572 /** Set BLE connectable mode to auto connect */
BTA_DmBleStartAutoConn()573 void BTA_DmBleStartAutoConn() {
574   do_in_bta_thread(FROM_HERE, base::Bind(BTM_BleStartAutoConn));
575 }
576 
577 /*******************************************************************************
578  *
579  * Function         bta_dm_discover_send_msg
580  *
581  * Description      This function send discover message to BTA task.
582  *
583  * Returns          void
584  *
585  ******************************************************************************/
bta_dm_discover_send_msg(const RawAddress & bd_addr,tBTA_SERVICE_MASK_EXT * p_services,tBTA_DM_SEARCH_CBACK * p_cback,bool sdp_search,tBTA_TRANSPORT transport)586 static void bta_dm_discover_send_msg(const RawAddress& bd_addr,
587                                      tBTA_SERVICE_MASK_EXT* p_services,
588                                      tBTA_DM_SEARCH_CBACK* p_cback,
589                                      bool sdp_search,
590                                      tBTA_TRANSPORT transport) {
591   const size_t len =
592       p_services
593           ? (sizeof(tBTA_DM_API_DISCOVER) + sizeof(Uuid) * p_services->num_uuid)
594           : sizeof(tBTA_DM_API_DISCOVER);
595   tBTA_DM_API_DISCOVER* p_msg = (tBTA_DM_API_DISCOVER*)osi_calloc(len);
596 
597   p_msg->hdr.event = BTA_DM_API_DISCOVER_EVT;
598   p_msg->bd_addr = bd_addr;
599   p_msg->p_cback = p_cback;
600   p_msg->sdp_search = sdp_search;
601   p_msg->transport = transport;
602 
603   if (p_services != NULL) {
604     p_msg->services = p_services->srvc_mask;
605     p_msg->num_uuid = p_services->num_uuid;
606     if (p_services->num_uuid != 0) {
607       p_msg->p_uuid = (Uuid*)(p_msg + 1);
608       memcpy(p_msg->p_uuid, p_services->p_uuid,
609              sizeof(Uuid) * p_services->num_uuid);
610     }
611   }
612 
613   bta_sys_sendmsg(p_msg);
614 }
615 
616 /*******************************************************************************
617  *
618  * Function         BTA_DmDiscoverByTransport
619  *
620  * Description      This function does service discovery on particular transport
621  *                  for services of a
622  *                  peer device. When services.num_uuid is 0, it indicates all
623  *                  GATT based services are to be searched; otherwise a list of
624  *                  UUID of interested services should be provided through
625  *                  p_services->p_uuid.
626  *
627  *
628  *
629  * Returns          void
630  *
631  ******************************************************************************/
BTA_DmDiscoverByTransport(const RawAddress & bd_addr,tBTA_SERVICE_MASK_EXT * p_services,tBTA_DM_SEARCH_CBACK * p_cback,bool sdp_search,tBTA_TRANSPORT transport)632 void BTA_DmDiscoverByTransport(const RawAddress& bd_addr,
633                                tBTA_SERVICE_MASK_EXT* p_services,
634                                tBTA_DM_SEARCH_CBACK* p_cback, bool sdp_search,
635                                tBTA_TRANSPORT transport) {
636   bta_dm_discover_send_msg(bd_addr, p_services, p_cback, sdp_search, transport);
637 }
638 
639 /*******************************************************************************
640  *
641  * Function         BTA_DmDiscoverExt
642  *
643  * Description      This function does service discovery for services of a
644  *                  peer device. When services.num_uuid is 0, it indicates all
645  *                  GATT based services are to be searched; other wise a list of
646  *                  UUID of interested services should be provided through
647  *                  p_services->p_uuid.
648  *
649  *
650  *
651  * Returns          void
652  *
653  ******************************************************************************/
BTA_DmDiscoverExt(const RawAddress & bd_addr,tBTA_SERVICE_MASK_EXT * p_services,tBTA_DM_SEARCH_CBACK * p_cback,bool sdp_search)654 void BTA_DmDiscoverExt(const RawAddress& bd_addr,
655                        tBTA_SERVICE_MASK_EXT* p_services,
656                        tBTA_DM_SEARCH_CBACK* p_cback, bool sdp_search) {
657   bta_dm_discover_send_msg(bd_addr, p_services, p_cback, sdp_search,
658                            BTA_TRANSPORT_UNKNOWN);
659 }
660 
661 /*******************************************************************************
662  *
663  * Function         BTA_DmSearchExt
664  *
665  * Description      This function searches for peer Bluetooth devices. It
666  *                  performs an inquiry and gets the remote name for devices.
667  *                  Service discovery is done if services is non zero
668  *
669  * Parameters       p_dm_inq: inquiry conditions
670  *                  p_services: if service is not empty, service discovery will
671  *                              be done. For all GATT based service conditions,
672  *                              put num_uuid, and p_uuid is the pointer to the
673  *                              list of UUID values.
674  *                  p_cback: callback function when search is completed.
675  *
676  *
677  *
678  * Returns          void
679  *
680  ******************************************************************************/
BTA_DmSearchExt(tBTA_DM_INQ * p_dm_inq,tBTA_SERVICE_MASK_EXT * p_services,tBTA_DM_SEARCH_CBACK * p_cback)681 void BTA_DmSearchExt(tBTA_DM_INQ* p_dm_inq, tBTA_SERVICE_MASK_EXT* p_services,
682                      tBTA_DM_SEARCH_CBACK* p_cback) {
683   const size_t len =
684       p_services
685           ? (sizeof(tBTA_DM_API_SEARCH) + sizeof(Uuid) * p_services->num_uuid)
686           : sizeof(tBTA_DM_API_SEARCH);
687   tBTA_DM_API_SEARCH* p_msg = (tBTA_DM_API_SEARCH*)osi_calloc(len);
688 
689   p_msg->hdr.event = BTA_DM_API_SEARCH_EVT;
690   memcpy(&p_msg->inq_params, p_dm_inq, sizeof(tBTA_DM_INQ));
691   p_msg->p_cback = p_cback;
692   p_msg->rs_res = BTA_DM_RS_NONE;
693 
694   if (p_services != NULL) {
695     p_msg->services = p_services->srvc_mask;
696     p_msg->num_uuid = p_services->num_uuid;
697 
698     if (p_services->num_uuid != 0) {
699       p_msg->p_uuid = (Uuid*)(p_msg + 1);
700       memcpy(p_msg->p_uuid, p_services->p_uuid,
701              sizeof(Uuid) * p_services->num_uuid);
702     } else {
703       p_msg->p_uuid = NULL;
704     }
705   }
706 
707   bta_sys_sendmsg(p_msg);
708 }
709 
710 /*******************************************************************************
711  *
712  * Function         BTA_DmBleUpdateConnectionParam
713  *
714  * Description      Update connection parameters, can only be used when
715  *                  connection is up.
716  *
717  * Parameters:      bd_addr          - BD address of the peer
718  *                  min_int   -     minimum connection interval,
719  *                                  [0x0004 ~ 0x4000]
720  *                  max_int   -     maximum connection interval,
721  *                                  [0x0004 ~ 0x4000]
722  *                  latency   -     slave latency [0 ~ 500]
723  *                  timeout   -     supervision timeout [0x000a ~ 0xc80]
724  *
725  * Returns          void
726  *
727  ******************************************************************************/
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)728 void BTA_DmBleUpdateConnectionParams(const RawAddress& bd_addr,
729                                      uint16_t min_int, uint16_t max_int,
730                                      uint16_t latency, uint16_t timeout,
731                                      uint16_t min_ce_len, uint16_t max_ce_len) {
732   do_in_bta_thread(
733       FROM_HERE, base::Bind(bta_dm_ble_update_conn_params, bd_addr, min_int,
734                             max_int, latency, timeout, min_ce_len, max_ce_len));
735 }
736 
737 /*******************************************************************************
738  *
739  * Function         BTA_DmBleConfigLocalPrivacy
740  *
741  * Description      Enable/disable privacy on the local device
742  *
743  * Parameters:      privacy_enable   - enable/disabe privacy on remote device.
744  *
745  * Returns          void
746  *
747  ******************************************************************************/
BTA_DmBleConfigLocalPrivacy(bool privacy_enable)748 void BTA_DmBleConfigLocalPrivacy(bool privacy_enable) {
749 #if (BLE_PRIVACY_SPT == TRUE)
750   do_in_bta_thread(FROM_HERE,
751                    base::Bind(bta_dm_ble_config_local_privacy, privacy_enable));
752 #else
753   UNUSED(privacy_enable);
754 #endif
755 }
756 
757 /*******************************************************************************
758  *
759  * Function         BTA_DmBleGetEnergyInfo
760  *
761  * Description      This function is called to obtain the energy info
762  *
763  * Parameters       p_cmpl_cback - Command complete callback
764  *
765  * Returns          void
766  *
767  ******************************************************************************/
BTA_DmBleGetEnergyInfo(tBTA_BLE_ENERGY_INFO_CBACK * p_cmpl_cback)768 void BTA_DmBleGetEnergyInfo(tBTA_BLE_ENERGY_INFO_CBACK* p_cmpl_cback) {
769   do_in_bta_thread(FROM_HERE,
770                    base::Bind(bta_dm_ble_get_energy_info, p_cmpl_cback));
771 }
772 
773 /** This function is to set maximum LE data packet size */
BTA_DmBleSetDataLength(const RawAddress & remote_device,uint16_t tx_data_length)774 void BTA_DmBleSetDataLength(const RawAddress& remote_device,
775                             uint16_t tx_data_length) {
776   do_in_bta_thread(FROM_HERE, base::Bind(bta_dm_ble_set_data_length,
777                                          remote_device, tx_data_length));
778 }
779 
780 /*******************************************************************************
781  *
782  * Function         BTA_DmSetEncryption
783  *
784  * Description      This function is called to ensure that connection is
785  *                  encrypted.  Should be called only on an open connection.
786  *                  Typically only needed for connections that first want to
787  *                  bring up unencrypted links, then later encrypt them.
788  *
789  * Parameters:      bd_addr       - Address of the peer device
790  *                  transport     - transport of the link to be encruypted
791  *                  p_callback    - Pointer to callback function to indicat the
792  *                                  link encryption status
793  *                  sec_act       - This is the security action to indicate
794  *                                  what kind of BLE security level is required
795  *                                  for the BLE link if BLE is supported.
796  *                                  Note: This parameter is ignored for the
797  *                                        BR/EDR or if BLE is not supported.
798  *
799  * Returns          void
800  *
801  ******************************************************************************/
BTA_DmSetEncryption(const RawAddress & bd_addr,tBTA_TRANSPORT transport,tBTA_DM_ENCRYPT_CBACK * p_callback,tBTA_DM_BLE_SEC_ACT sec_act)802 void BTA_DmSetEncryption(const RawAddress& bd_addr, tBTA_TRANSPORT transport,
803                          tBTA_DM_ENCRYPT_CBACK* p_callback,
804                          tBTA_DM_BLE_SEC_ACT sec_act) {
805   APPL_TRACE_API("%s", __func__);
806   do_in_bta_thread(FROM_HERE, base::Bind(bta_dm_set_encryption, bd_addr,
807                                          transport, p_callback, sec_act));
808 }
809 
810 /*******************************************************************************
811  *
812  * Function         BTA_DmCloseACL
813  *
814  * Description      This function force to close an ACL connection and remove
815  *                  the device from the security database list of known devices.
816  *
817  * Parameters:      bd_addr       - Address of the peer device
818  *                  remove_dev    - remove device or not after link down
819  *
820  * Returns          void
821  *
822  ******************************************************************************/
BTA_DmCloseACL(const RawAddress & bd_addr,bool remove_dev,tBTA_TRANSPORT transport)823 void BTA_DmCloseACL(const RawAddress& bd_addr, bool remove_dev,
824                     tBTA_TRANSPORT transport) {
825   do_in_bta_thread(
826       FROM_HERE, base::Bind(bta_dm_close_acl, bd_addr, remove_dev, transport));
827 }
828 
829 /*******************************************************************************
830  *
831  * Function         BTA_DmBleObserve
832  *
833  * Description      This procedure keep the device listening for advertising
834  *                  events from a broadcast device.
835  *
836  * Parameters       start: start or stop observe.
837  *
838  * Returns          void
839 
840  *
841  * Returns          void.
842  *
843  ******************************************************************************/
BTA_DmBleObserve(bool start,uint8_t duration,tBTA_DM_SEARCH_CBACK * p_results_cb)844 extern void BTA_DmBleObserve(bool start, uint8_t duration,
845                              tBTA_DM_SEARCH_CBACK* p_results_cb) {
846   APPL_TRACE_API("%s:start = %d ", __func__, start);
847   do_in_bta_thread(
848       FROM_HERE, base::Bind(bta_dm_ble_observe, start, duration, p_results_cb));
849 }
850 
851 /*******************************************************************************
852  *
853  * Function         BTA_VendorInit
854  *
855  * Description      This function initializes vendor specific
856  *
857  * Returns          void
858  *
859  ******************************************************************************/
BTA_VendorInit(void)860 void BTA_VendorInit(void) { APPL_TRACE_API("BTA_VendorInit"); }
861 
862 /*******************************************************************************
863  *
864  * Function         BTA_VendorCleanup
865  *
866  * Description      This function frees up Broadcom specific VS specific dynamic
867  *                  memory
868  *
869  * Returns          void
870  *
871  ******************************************************************************/
BTA_VendorCleanup(void)872 void BTA_VendorCleanup(void) {
873   tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
874   BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
875 
876   if (cmn_ble_vsc_cb.max_filter > 0) {
877     btm_ble_adv_filter_cleanup();
878 #if (BLE_PRIVACY_SPT == TRUE)
879     btm_ble_resolving_list_cleanup();
880 #endif
881   }
882 
883   if (cmn_ble_vsc_cb.tot_scan_results_strg > 0) btm_ble_batchscan_cleanup();
884 
885   if (cmn_ble_vsc_cb.adv_inst_max > 0) btm_ble_multi_adv_cleanup();
886 }
887