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_dm_int.h"
30 #include "bta_sys.h"
31 #include "bta_sys_int.h"
32 #include "btm_api.h"
33 #include "btm_int.h"
34 #include "osi/include/osi.h"
35 #include "stack/include/btu.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_main_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_main_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_main_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_main_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_main_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_main_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,tBLE_ADDR_TYPE addr_type,tBTA_TRANSPORT transport,int device_type)213 void BTA_DmBond(const RawAddress& bd_addr, tBLE_ADDR_TYPE addr_type,
214                 tBTA_TRANSPORT transport, int device_type) {
215   do_in_main_thread(FROM_HERE, base::Bind(bta_dm_bond, bd_addr, addr_type,
216                                           transport, device_type));
217 }
218 
219 /** This function cancels the bonding procedure with a peer device
220  */
BTA_DmBondCancel(const RawAddress & bd_addr)221 void BTA_DmBondCancel(const RawAddress& bd_addr) {
222   do_in_main_thread(FROM_HERE, base::Bind(bta_dm_bond_cancel, bd_addr));
223 }
224 
225 /*******************************************************************************
226  *
227  * Function         BTA_DmPinReply
228  *
229  * Description      This function provides a pincode for a remote device when
230  *                  one is requested by DM through BTA_DM_PIN_REQ_EVT
231  *
232  *
233  * Returns          void
234  *
235  ******************************************************************************/
BTA_DmPinReply(const RawAddress & bd_addr,bool accept,uint8_t pin_len,uint8_t * p_pin)236 void BTA_DmPinReply(const RawAddress& bd_addr, bool accept, uint8_t pin_len,
237                     uint8_t* p_pin) {
238   std::unique_ptr<tBTA_DM_API_PIN_REPLY> msg =
239       std::make_unique<tBTA_DM_API_PIN_REPLY>();
240 
241   msg->bd_addr = bd_addr;
242   msg->accept = accept;
243   if (accept) {
244     msg->pin_len = pin_len;
245     memcpy(msg->p_pin, p_pin, pin_len);
246   }
247 
248   do_in_main_thread(FROM_HERE,
249                     base::Bind(bta_dm_pin_reply, base::Passed(&msg)));
250 }
251 
252 /*******************************************************************************
253  *
254  * Function         BTA_DmLocalOob
255  *
256  * Description      This function retrieves the OOB data from local controller.
257  *                  The result is reported by:
258  *                  - bta_dm_co_loc_oob_ext() if device supports secure
259  *                    connections (SC)
260  *                  - bta_dm_co_loc_oob() if device doesn't support SC
261  *
262  * Returns          void
263  *
264  ******************************************************************************/
BTA_DmLocalOob(void)265 void BTA_DmLocalOob(void) {
266   do_in_main_thread(FROM_HERE, base::Bind(BTM_ReadLocalOobData));
267 }
268 
269 /*******************************************************************************
270  *
271  * Function         BTA_DmConfirm
272  *
273  * Description      This function accepts or rejects the numerical value of the
274  *                  Simple Pairing process on BTA_DM_SP_CFM_REQ_EVT
275  *
276  * Returns          void
277  *
278  ******************************************************************************/
BTA_DmConfirm(const RawAddress & bd_addr,bool accept)279 void BTA_DmConfirm(const RawAddress& bd_addr, bool accept) {
280   do_in_main_thread(FROM_HERE, base::Bind(bta_dm_confirm, bd_addr, accept));
281 }
282 
283 /*******************************************************************************
284  *
285  * Function         BTA_DmAddDevice
286  *
287  * Description      This function adds a device to the security database list of
288  *                  peer device
289  *
290  *
291  * Returns          void
292  *
293  ******************************************************************************/
BTA_DmAddDevice(const RawAddress & bd_addr,DEV_CLASS dev_class,const LinkKey & link_key,tBTA_SERVICE_MASK trusted_mask,bool is_trusted,uint8_t key_type,tBTA_IO_CAP io_cap,uint8_t pin_length)294 void BTA_DmAddDevice(const RawAddress& bd_addr, DEV_CLASS dev_class,
295                      const LinkKey& link_key, tBTA_SERVICE_MASK trusted_mask,
296                      bool is_trusted, uint8_t key_type, tBTA_IO_CAP io_cap,
297                      uint8_t pin_length) {
298   std::unique_ptr<tBTA_DM_API_ADD_DEVICE> msg =
299       std::make_unique<tBTA_DM_API_ADD_DEVICE>();
300 
301   msg->bd_addr = bd_addr;
302   msg->tm = trusted_mask;
303   msg->is_trusted = is_trusted;
304   msg->io_cap = io_cap;
305   msg->link_key_known = true;
306   msg->key_type = key_type;
307   msg->link_key = link_key;
308 
309   /* Load device class if specified */
310   if (dev_class) {
311     msg->dc_known = true;
312     memcpy(msg->dc, dev_class, DEV_CLASS_LEN);
313   }
314 
315   memset(msg->bd_name, 0, BD_NAME_LEN + 1);
316   memset(msg->features, 0, sizeof(msg->features));
317   msg->pin_length = pin_length;
318 
319   do_in_main_thread(FROM_HERE,
320                     base::Bind(bta_dm_add_device, base::Passed(&msg)));
321 }
322 
323 /** This function removes a device fromthe security database list of peer
324  * device. It manages unpairing even while connected */
BTA_DmRemoveDevice(const RawAddress & bd_addr)325 tBTA_STATUS BTA_DmRemoveDevice(const RawAddress& bd_addr) {
326   do_in_main_thread(FROM_HERE, base::Bind(bta_dm_remove_device, bd_addr));
327   return BTA_SUCCESS;
328 }
329 
330 /*******************************************************************************
331  *
332  * Function         BTA_GetEirService
333  *
334  * Description      This function is called to get BTA service mask from EIR.
335  *
336  * Parameters       p_eir - pointer of EIR significant part
337  *                  p_services - return the BTA service mask
338  *
339  * Returns          None
340  *
341  ******************************************************************************/
342 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)343 void BTA_GetEirService(uint8_t* p_eir, size_t eir_len,
344                        tBTA_SERVICE_MASK* p_services) {
345   uint8_t xx, yy;
346   uint8_t num_uuid, max_num_uuid = 32;
347   uint8_t uuid_list[32 * Uuid::kNumBytes16];
348   uint16_t* p_uuid16 = (uint16_t*)uuid_list;
349   tBTA_SERVICE_MASK mask;
350 
351   BTM_GetEirUuidList(p_eir, eir_len, Uuid::kNumBytes16, &num_uuid, uuid_list,
352                      max_num_uuid);
353   for (xx = 0; xx < num_uuid; xx++) {
354     mask = 1;
355     for (yy = 0; yy < BTA_MAX_SERVICE_ID; yy++) {
356       if (*(p_uuid16 + xx) == bta_service_id_to_uuid_lkup_tbl[yy]) {
357         *p_services |= mask;
358         break;
359       }
360       mask <<= 1;
361     }
362 
363     /* for HSP v1.2 only device */
364     if (*(p_uuid16 + xx) == UUID_SERVCLASS_HEADSET_HS)
365       *p_services |= BTA_HSP_SERVICE_MASK;
366 
367     if (*(p_uuid16 + xx) == UUID_SERVCLASS_HDP_SOURCE)
368       *p_services |= BTA_HL_SERVICE_MASK;
369 
370     if (*(p_uuid16 + xx) == UUID_SERVCLASS_HDP_SINK)
371       *p_services |= BTA_HL_SERVICE_MASK;
372   }
373 }
374 
375 /*******************************************************************************
376  *
377  * Function         BTA_DmGetConnectionState
378  *
379  * Description      Returns whether the remote device is currently connected.
380  *
381  * Returns          0 if the device is NOT connected.
382  *
383  ******************************************************************************/
BTA_DmGetConnectionState(const RawAddress & bd_addr)384 uint16_t BTA_DmGetConnectionState(const RawAddress& bd_addr) {
385   tBTA_DM_PEER_DEVICE* p_dev = bta_dm_find_peer_device(bd_addr);
386   return (p_dev && p_dev->conn_state == BTA_DM_CONNECTED);
387 }
388 
389 /*******************************************************************************
390  *                   Device Identification (DI) Server Functions
391  ******************************************************************************/
392 /*******************************************************************************
393  *
394  * Function         BTA_DmSetLocalDiRecord
395  *
396  * Description      This function adds a DI record to the local SDP database.
397  *
398  * Returns          BTA_SUCCESS if record set sucessfully, otherwise error code.
399  *
400  ******************************************************************************/
BTA_DmSetLocalDiRecord(tBTA_DI_RECORD * p_device_info,uint32_t * p_handle)401 tBTA_STATUS BTA_DmSetLocalDiRecord(tBTA_DI_RECORD* p_device_info,
402                                    uint32_t* p_handle) {
403   tBTA_STATUS status = BTA_FAILURE;
404 
405   if (bta_dm_di_cb.di_num < BTA_DI_NUM_MAX) {
406     if (SDP_SetLocalDiRecord((tSDP_DI_RECORD*)p_device_info, p_handle) ==
407         SDP_SUCCESS) {
408       if (!p_device_info->primary_record) {
409         bta_dm_di_cb.di_handle[bta_dm_di_cb.di_num] = *p_handle;
410         bta_dm_di_cb.di_num++;
411       }
412 
413       bta_sys_add_uuid(UUID_SERVCLASS_PNP_INFORMATION);
414       status = BTA_SUCCESS;
415     }
416   }
417 
418   return status;
419 }
420 
421 /*******************************************************************************
422  *
423  * Function         BTA_DmAddBleKey
424  *
425  * Description      Add/modify LE device information.  This function will be
426  *                  normally called during host startup to restore all required
427  *                  information stored in the NVRAM.
428  *
429  * Parameters:      bd_addr          - BD address of the peer
430  *                  p_le_key         - LE key values.
431  *                  key_type         - LE SMP key type.
432  *
433  * Returns          BTA_SUCCESS if successful
434  *                  BTA_FAIL if operation failed.
435  *
436  ******************************************************************************/
BTA_DmAddBleKey(const RawAddress & bd_addr,tBTA_LE_KEY_VALUE * p_le_key,tBTA_LE_KEY_TYPE key_type)437 void BTA_DmAddBleKey(const RawAddress& bd_addr, tBTA_LE_KEY_VALUE* p_le_key,
438                      tBTA_LE_KEY_TYPE key_type) {
439   do_in_main_thread(
440       FROM_HERE, base::Bind(bta_dm_add_blekey, bd_addr, *p_le_key, key_type));
441 }
442 
443 /*******************************************************************************
444  *
445  * Function         BTA_DmAddBleDevice
446  *
447  * Description      Add a BLE device.  This function will be normally called
448  *                  during host startup to restore all required information
449  *                  for a LE device stored in the NVRAM.
450  *
451  * Parameters:      bd_addr          - BD address of the peer
452  *                  dev_type         - Remote device's device type.
453  *                  addr_type        - LE device address type.
454  *
455  * Returns          void
456  *
457  ******************************************************************************/
BTA_DmAddBleDevice(const RawAddress & bd_addr,tBLE_ADDR_TYPE addr_type,tBT_DEVICE_TYPE dev_type)458 void BTA_DmAddBleDevice(const RawAddress& bd_addr, tBLE_ADDR_TYPE addr_type,
459                         tBT_DEVICE_TYPE dev_type) {
460   do_in_main_thread(FROM_HERE, base::Bind(bta_dm_add_ble_device, bd_addr,
461                                           addr_type, dev_type));
462 }
463 
464 /*******************************************************************************
465  *
466  * Function         BTA_DmBlePasskeyReply
467  *
468  * Description      Send BLE SMP passkey reply.
469  *
470  * Parameters:      bd_addr          - BD address of the peer
471  *                  accept           - passkey entry sucessful or declined.
472  *                  passkey          - passkey value, must be a 6 digit number,
473  *                                     can be lead by 0.
474  *
475  * Returns          void
476  *
477  ******************************************************************************/
BTA_DmBlePasskeyReply(const RawAddress & bd_addr,bool accept,uint32_t passkey)478 void BTA_DmBlePasskeyReply(const RawAddress& bd_addr, bool accept,
479                            uint32_t passkey) {
480   do_in_main_thread(FROM_HERE, base::Bind(bta_dm_ble_passkey_reply, bd_addr,
481                                           accept, accept ? passkey : 0));
482 }
483 
484 /*******************************************************************************
485  *
486  * Function         BTA_DmBleConfirmReply
487  *
488  * Description      Send BLE SMP SC user confirmation reply.
489  *
490  * Parameters:      bd_addr          - BD address of the peer
491  *                  accept           - numbers to compare are the same or
492  *                                     different.
493  *
494  * Returns          void
495  *
496  ******************************************************************************/
BTA_DmBleConfirmReply(const RawAddress & bd_addr,bool accept)497 void BTA_DmBleConfirmReply(const RawAddress& bd_addr, bool accept) {
498   do_in_main_thread(FROM_HERE,
499                     base::Bind(bta_dm_ble_confirm_reply, bd_addr, accept));
500 }
501 
502 /*******************************************************************************
503  *
504  * Function         BTA_DmBleSecurityGrant
505  *
506  * Description      Grant security request access.
507  *
508  * Parameters:      bd_addr          - BD address of the peer
509  *                  res              - security grant status.
510  *
511  * Returns          void
512  *
513  ******************************************************************************/
BTA_DmBleSecurityGrant(const RawAddress & bd_addr,tBTA_DM_BLE_SEC_GRANT res)514 void BTA_DmBleSecurityGrant(const RawAddress& bd_addr,
515                             tBTA_DM_BLE_SEC_GRANT res) {
516   do_in_main_thread(FROM_HERE, base::Bind(BTM_SecurityGrant, bd_addr, res));
517 }
518 
519 /*******************************************************************************
520  *
521  * Function         BTA_DmSetBlePrefConnParams
522  *
523  * Description      This function is called to set the preferred connection
524  *                  parameters when default connection parameter is not desired.
525  *
526  * Parameters:      bd_addr          - BD address of the peripheral
527  *                  scan_interval    - scan interval
528  *                  scan_window      - scan window
529  *                  min_conn_int     - minimum preferred connection interval
530  *                  max_conn_int     - maximum preferred connection interval
531  *                  slave_latency    - preferred slave latency
532  *                  supervision_tout - preferred supervision timeout
533  *
534  *
535  * Returns          void
536  *
537  ******************************************************************************/
BTA_DmSetBlePrefConnParams(const RawAddress & bd_addr,uint16_t min_conn_int,uint16_t max_conn_int,uint16_t slave_latency,uint16_t supervision_tout)538 void BTA_DmSetBlePrefConnParams(const RawAddress& bd_addr,
539                                 uint16_t min_conn_int, uint16_t max_conn_int,
540                                 uint16_t slave_latency,
541                                 uint16_t supervision_tout) {
542   do_in_main_thread(
543       FROM_HERE, base::Bind(bta_dm_ble_set_conn_params, bd_addr, min_conn_int,
544                             max_conn_int, slave_latency, supervision_tout));
545 }
546 
547 /*******************************************************************************
548  *
549  * Function         bta_dm_discover_send_msg
550  *
551  * Description      This function send discover message to BTA task.
552  *
553  * Returns          void
554  *
555  ******************************************************************************/
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)556 static void bta_dm_discover_send_msg(const RawAddress& bd_addr,
557                                      tBTA_SERVICE_MASK_EXT* p_services,
558                                      tBTA_DM_SEARCH_CBACK* p_cback,
559                                      bool sdp_search,
560                                      tBTA_TRANSPORT transport) {
561   const size_t len =
562       p_services
563           ? (sizeof(tBTA_DM_API_DISCOVER) + sizeof(Uuid) * p_services->num_uuid)
564           : sizeof(tBTA_DM_API_DISCOVER);
565   tBTA_DM_API_DISCOVER* p_msg = (tBTA_DM_API_DISCOVER*)osi_calloc(len);
566 
567   p_msg->hdr.event = BTA_DM_API_DISCOVER_EVT;
568   p_msg->bd_addr = bd_addr;
569   p_msg->p_cback = p_cback;
570   p_msg->sdp_search = sdp_search;
571   p_msg->transport = transport;
572 
573   if (p_services != NULL) {
574     p_msg->services = p_services->srvc_mask;
575     p_msg->num_uuid = p_services->num_uuid;
576     if (p_services->num_uuid != 0) {
577       p_msg->p_uuid = (Uuid*)(p_msg + 1);
578       memcpy(p_msg->p_uuid, p_services->p_uuid,
579              sizeof(Uuid) * p_services->num_uuid);
580     }
581   }
582 
583   bta_sys_sendmsg(p_msg);
584 }
585 
586 /*******************************************************************************
587  *
588  * Function         BTA_DmDiscoverByTransport
589  *
590  * Description      This function does service discovery on particular transport
591  *                  for services of a
592  *                  peer device. When services.num_uuid is 0, it indicates all
593  *                  GATT based services are to be searched; otherwise a list of
594  *                  UUID of interested services should be provided through
595  *                  p_services->p_uuid.
596  *
597  *
598  *
599  * Returns          void
600  *
601  ******************************************************************************/
BTA_DmDiscoverByTransport(const RawAddress & bd_addr,tBTA_SERVICE_MASK_EXT * p_services,tBTA_DM_SEARCH_CBACK * p_cback,bool sdp_search,tBTA_TRANSPORT transport)602 void BTA_DmDiscoverByTransport(const RawAddress& bd_addr,
603                                tBTA_SERVICE_MASK_EXT* p_services,
604                                tBTA_DM_SEARCH_CBACK* p_cback, bool sdp_search,
605                                tBTA_TRANSPORT transport) {
606   bta_dm_discover_send_msg(bd_addr, p_services, p_cback, sdp_search, transport);
607 }
608 
609 /*******************************************************************************
610  *
611  * Function         BTA_DmBleUpdateConnectionParam
612  *
613  * Description      Update connection parameters, can only be used when
614  *                  connection is up.
615  *
616  * Parameters:      bd_addr          - BD address of the peer
617  *                  min_int   -     minimum connection interval,
618  *                                  [0x0004 ~ 0x4000]
619  *                  max_int   -     maximum connection interval,
620  *                                  [0x0004 ~ 0x4000]
621  *                  latency   -     slave latency [0 ~ 500]
622  *                  timeout   -     supervision timeout [0x000a ~ 0xc80]
623  *
624  * Returns          void
625  *
626  ******************************************************************************/
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)627 void BTA_DmBleUpdateConnectionParams(const RawAddress& bd_addr,
628                                      uint16_t min_int, uint16_t max_int,
629                                      uint16_t latency, uint16_t timeout,
630                                      uint16_t min_ce_len, uint16_t max_ce_len) {
631   do_in_main_thread(
632       FROM_HERE, base::Bind(bta_dm_ble_update_conn_params, bd_addr, min_int,
633                             max_int, latency, timeout, min_ce_len, max_ce_len));
634 }
635 
636 /*******************************************************************************
637  *
638  * Function         BTA_DmBleConfigLocalPrivacy
639  *
640  * Description      Enable/disable privacy on the local device
641  *
642  * Parameters:      privacy_enable   - enable/disabe privacy on remote device.
643  *
644  * Returns          void
645  *
646  ******************************************************************************/
BTA_DmBleConfigLocalPrivacy(bool privacy_enable)647 void BTA_DmBleConfigLocalPrivacy(bool privacy_enable) {
648 #if (BLE_PRIVACY_SPT == TRUE)
649   do_in_main_thread(
650       FROM_HERE, base::Bind(bta_dm_ble_config_local_privacy, privacy_enable));
651 #else
652   UNUSED(privacy_enable);
653 #endif
654 }
655 
656 /*******************************************************************************
657  *
658  * Function         BTA_DmBleGetEnergyInfo
659  *
660  * Description      This function is called to obtain the energy info
661  *
662  * Parameters       p_cmpl_cback - Command complete callback
663  *
664  * Returns          void
665  *
666  ******************************************************************************/
BTA_DmBleGetEnergyInfo(tBTA_BLE_ENERGY_INFO_CBACK * p_cmpl_cback)667 void BTA_DmBleGetEnergyInfo(tBTA_BLE_ENERGY_INFO_CBACK* p_cmpl_cback) {
668   do_in_main_thread(FROM_HERE,
669                     base::Bind(bta_dm_ble_get_energy_info, p_cmpl_cback));
670 }
671 
672 /** This function is to set maximum LE data packet size */
BTA_DmBleSetDataLength(const RawAddress & remote_device,uint16_t tx_data_length)673 void BTA_DmBleSetDataLength(const RawAddress& remote_device,
674                             uint16_t tx_data_length) {
675   do_in_main_thread(FROM_HERE, base::Bind(bta_dm_ble_set_data_length,
676                                           remote_device, tx_data_length));
677 }
678 
679 /*******************************************************************************
680  *
681  * Function         BTA_DmSetEncryption
682  *
683  * Description      This function is called to ensure that connection is
684  *                  encrypted.  Should be called only on an open connection.
685  *                  Typically only needed for connections that first want to
686  *                  bring up unencrypted links, then later encrypt them.
687  *
688  * Parameters:      bd_addr       - Address of the peer device
689  *                  transport     - transport of the link to be encruypted
690  *                  p_callback    - Pointer to callback function to indicat the
691  *                                  link encryption status
692  *                  sec_act       - This is the security action to indicate
693  *                                  what kind of BLE security level is required
694  *                                  for the BLE link if BLE is supported.
695  *                                  Note: This parameter is ignored for the
696  *                                        BR/EDR or if BLE is not supported.
697  *
698  * Returns          void
699  *
700  ******************************************************************************/
BTA_DmSetEncryption(const RawAddress & bd_addr,tBTA_TRANSPORT transport,tBTA_DM_ENCRYPT_CBACK * p_callback,tBTA_DM_BLE_SEC_ACT sec_act)701 void BTA_DmSetEncryption(const RawAddress& bd_addr, tBTA_TRANSPORT transport,
702                          tBTA_DM_ENCRYPT_CBACK* p_callback,
703                          tBTA_DM_BLE_SEC_ACT sec_act) {
704   APPL_TRACE_API("%s", __func__);
705   do_in_main_thread(FROM_HERE, base::Bind(bta_dm_set_encryption, bd_addr,
706                                           transport, p_callback, sec_act));
707 }
708 
709 /*******************************************************************************
710  *
711  * Function         BTA_DmCloseACL
712  *
713  * Description      This function force to close an ACL connection and remove
714  *                  the device from the security database list of known devices.
715  *
716  * Parameters:      bd_addr       - Address of the peer device
717  *                  remove_dev    - remove device or not after link down
718  *
719  * Returns          void
720  *
721  ******************************************************************************/
BTA_DmCloseACL(const RawAddress & bd_addr,bool remove_dev,tBTA_TRANSPORT transport)722 void BTA_DmCloseACL(const RawAddress& bd_addr, bool remove_dev,
723                     tBTA_TRANSPORT transport) {
724   do_in_main_thread(
725       FROM_HERE, base::Bind(bta_dm_close_acl, bd_addr, remove_dev, transport));
726 }
727 
728 /*******************************************************************************
729  *
730  * Function         BTA_DmBleObserve
731  *
732  * Description      This procedure keep the device listening for advertising
733  *                  events from a broadcast device.
734  *
735  * Parameters       start: start or stop observe.
736  *
737  * Returns          void
738 
739  *
740  * Returns          void.
741  *
742  ******************************************************************************/
BTA_DmBleObserve(bool start,uint8_t duration,tBTA_DM_SEARCH_CBACK * p_results_cb)743 extern void BTA_DmBleObserve(bool start, uint8_t duration,
744                              tBTA_DM_SEARCH_CBACK* p_results_cb) {
745   APPL_TRACE_API("%s:start = %d ", __func__, start);
746   do_in_main_thread(
747       FROM_HERE, base::Bind(bta_dm_ble_observe, start, duration, p_results_cb));
748 }
749 
750 /*******************************************************************************
751  *
752  * Function         BTA_VendorInit
753  *
754  * Description      This function initializes vendor specific
755  *
756  * Returns          void
757  *
758  ******************************************************************************/
BTA_VendorInit(void)759 void BTA_VendorInit(void) { APPL_TRACE_API("BTA_VendorInit"); }
760 
761 /*******************************************************************************
762  *
763  * Function         BTA_VendorCleanup
764  *
765  * Description      This function frees up Broadcom specific VS specific dynamic
766  *                  memory
767  *
768  * Returns          void
769  *
770  ******************************************************************************/
BTA_VendorCleanup(void)771 void BTA_VendorCleanup(void) {
772   tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
773   BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
774 
775   if (cmn_ble_vsc_cb.max_filter > 0) {
776     btm_ble_adv_filter_cleanup();
777 #if (BLE_PRIVACY_SPT == TRUE)
778     btm_ble_resolving_list_cleanup();
779 #endif
780   }
781 
782   if (cmn_ble_vsc_cb.tot_scan_results_strg > 0) btm_ble_batchscan_cleanup();
783 
784   if (cmn_ble_vsc_cb.adv_inst_max > 0) btm_ble_multi_adv_cleanup();
785 }
786