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