• 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,tBLE_ADDR_TYPE addr_type,tBTA_TRANSPORT transport)213 void BTA_DmBond(const RawAddress& bd_addr, tBLE_ADDR_TYPE addr_type,
214                 tBTA_TRANSPORT transport) {
215   do_in_main_thread(FROM_HERE,
216                     base::Bind(bta_dm_bond, bd_addr, addr_type, transport));
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_DmSetBleConnScanParams
550  *
551  * Description      This function is called to set scan parameters used in
552  *                  BLE connection request
553  *
554  * Parameters:      scan_interval    - scan interval
555  *                  scan_window      - scan window
556  *
557  * Returns          void
558  *
559  ******************************************************************************/
BTA_DmSetBleConnScanParams(uint32_t scan_interval,uint32_t scan_window)560 void BTA_DmSetBleConnScanParams(uint32_t scan_interval, uint32_t scan_window) {
561   do_in_main_thread(FROM_HERE, base::Bind(bta_dm_ble_set_conn_scan_params,
562                                           scan_interval, scan_window));
563 }
564 
565 /*******************************************************************************
566  *
567  * Function         bta_dm_discover_send_msg
568  *
569  * Description      This function send discover message to BTA task.
570  *
571  * Returns          void
572  *
573  ******************************************************************************/
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)574 static void bta_dm_discover_send_msg(const RawAddress& bd_addr,
575                                      tBTA_SERVICE_MASK_EXT* p_services,
576                                      tBTA_DM_SEARCH_CBACK* p_cback,
577                                      bool sdp_search,
578                                      tBTA_TRANSPORT transport) {
579   const size_t len =
580       p_services
581           ? (sizeof(tBTA_DM_API_DISCOVER) + sizeof(Uuid) * p_services->num_uuid)
582           : sizeof(tBTA_DM_API_DISCOVER);
583   tBTA_DM_API_DISCOVER* p_msg = (tBTA_DM_API_DISCOVER*)osi_calloc(len);
584 
585   p_msg->hdr.event = BTA_DM_API_DISCOVER_EVT;
586   p_msg->bd_addr = bd_addr;
587   p_msg->p_cback = p_cback;
588   p_msg->sdp_search = sdp_search;
589   p_msg->transport = transport;
590 
591   if (p_services != NULL) {
592     p_msg->services = p_services->srvc_mask;
593     p_msg->num_uuid = p_services->num_uuid;
594     if (p_services->num_uuid != 0) {
595       p_msg->p_uuid = (Uuid*)(p_msg + 1);
596       memcpy(p_msg->p_uuid, p_services->p_uuid,
597              sizeof(Uuid) * p_services->num_uuid);
598     }
599   }
600 
601   bta_sys_sendmsg(p_msg);
602 }
603 
604 /*******************************************************************************
605  *
606  * Function         BTA_DmDiscoverByTransport
607  *
608  * Description      This function does service discovery on particular transport
609  *                  for services of a
610  *                  peer device. When services.num_uuid is 0, it indicates all
611  *                  GATT based services are to be searched; otherwise a list of
612  *                  UUID of interested services should be provided through
613  *                  p_services->p_uuid.
614  *
615  *
616  *
617  * Returns          void
618  *
619  ******************************************************************************/
BTA_DmDiscoverByTransport(const RawAddress & bd_addr,tBTA_SERVICE_MASK_EXT * p_services,tBTA_DM_SEARCH_CBACK * p_cback,bool sdp_search,tBTA_TRANSPORT transport)620 void BTA_DmDiscoverByTransport(const RawAddress& bd_addr,
621                                tBTA_SERVICE_MASK_EXT* p_services,
622                                tBTA_DM_SEARCH_CBACK* p_cback, bool sdp_search,
623                                tBTA_TRANSPORT transport) {
624   bta_dm_discover_send_msg(bd_addr, p_services, p_cback, sdp_search, transport);
625 }
626 
627 /*******************************************************************************
628  *
629  * Function         BTA_DmDiscoverExt
630  *
631  * Description      This function does service discovery for services of a
632  *                  peer device. When services.num_uuid is 0, it indicates all
633  *                  GATT based services are to be searched; other wise a list of
634  *                  UUID of interested services should be provided through
635  *                  p_services->p_uuid.
636  *
637  *
638  *
639  * Returns          void
640  *
641  ******************************************************************************/
BTA_DmDiscoverExt(const RawAddress & bd_addr,tBTA_SERVICE_MASK_EXT * p_services,tBTA_DM_SEARCH_CBACK * p_cback,bool sdp_search)642 void BTA_DmDiscoverExt(const RawAddress& bd_addr,
643                        tBTA_SERVICE_MASK_EXT* p_services,
644                        tBTA_DM_SEARCH_CBACK* p_cback, bool sdp_search) {
645   bta_dm_discover_send_msg(bd_addr, p_services, p_cback, sdp_search,
646                            BTA_TRANSPORT_UNKNOWN);
647 }
648 
649 /*******************************************************************************
650  *
651  * Function         BTA_DmSearchExt
652  *
653  * Description      This function searches for peer Bluetooth devices. It
654  *                  performs an inquiry and gets the remote name for devices.
655  *                  Service discovery is done if services is non zero
656  *
657  * Parameters       p_dm_inq: inquiry conditions
658  *                  p_services: if service is not empty, service discovery will
659  *                              be done. For all GATT based service conditions,
660  *                              put num_uuid, and p_uuid is the pointer to the
661  *                              list of UUID values.
662  *                  p_cback: callback function when search is completed.
663  *
664  *
665  *
666  * Returns          void
667  *
668  ******************************************************************************/
BTA_DmSearchExt(tBTA_DM_INQ * p_dm_inq,tBTA_SERVICE_MASK_EXT * p_services,tBTA_DM_SEARCH_CBACK * p_cback)669 void BTA_DmSearchExt(tBTA_DM_INQ* p_dm_inq, tBTA_SERVICE_MASK_EXT* p_services,
670                      tBTA_DM_SEARCH_CBACK* p_cback) {
671   const size_t len =
672       p_services
673           ? (sizeof(tBTA_DM_API_SEARCH) + sizeof(Uuid) * p_services->num_uuid)
674           : sizeof(tBTA_DM_API_SEARCH);
675   tBTA_DM_API_SEARCH* p_msg = (tBTA_DM_API_SEARCH*)osi_calloc(len);
676 
677   p_msg->hdr.event = BTA_DM_API_SEARCH_EVT;
678   memcpy(&p_msg->inq_params, p_dm_inq, sizeof(tBTA_DM_INQ));
679   p_msg->p_cback = p_cback;
680   p_msg->rs_res = BTA_DM_RS_NONE;
681 
682   if (p_services != NULL) {
683     p_msg->services = p_services->srvc_mask;
684     p_msg->num_uuid = p_services->num_uuid;
685 
686     if (p_services->num_uuid != 0) {
687       p_msg->p_uuid = (Uuid*)(p_msg + 1);
688       memcpy(p_msg->p_uuid, p_services->p_uuid,
689              sizeof(Uuid) * p_services->num_uuid);
690     } else {
691       p_msg->p_uuid = NULL;
692     }
693   }
694 
695   bta_sys_sendmsg(p_msg);
696 }
697 
698 /*******************************************************************************
699  *
700  * Function         BTA_DmBleUpdateConnectionParam
701  *
702  * Description      Update connection parameters, can only be used when
703  *                  connection is up.
704  *
705  * Parameters:      bd_addr          - BD address of the peer
706  *                  min_int   -     minimum connection interval,
707  *                                  [0x0004 ~ 0x4000]
708  *                  max_int   -     maximum connection interval,
709  *                                  [0x0004 ~ 0x4000]
710  *                  latency   -     slave latency [0 ~ 500]
711  *                  timeout   -     supervision timeout [0x000a ~ 0xc80]
712  *
713  * Returns          void
714  *
715  ******************************************************************************/
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)716 void BTA_DmBleUpdateConnectionParams(const RawAddress& bd_addr,
717                                      uint16_t min_int, uint16_t max_int,
718                                      uint16_t latency, uint16_t timeout,
719                                      uint16_t min_ce_len, uint16_t max_ce_len) {
720   do_in_main_thread(
721       FROM_HERE, base::Bind(bta_dm_ble_update_conn_params, bd_addr, min_int,
722                             max_int, latency, timeout, min_ce_len, max_ce_len));
723 }
724 
725 /*******************************************************************************
726  *
727  * Function         BTA_DmBleConfigLocalPrivacy
728  *
729  * Description      Enable/disable privacy on the local device
730  *
731  * Parameters:      privacy_enable   - enable/disabe privacy on remote device.
732  *
733  * Returns          void
734  *
735  ******************************************************************************/
BTA_DmBleConfigLocalPrivacy(bool privacy_enable)736 void BTA_DmBleConfigLocalPrivacy(bool privacy_enable) {
737 #if (BLE_PRIVACY_SPT == TRUE)
738   do_in_main_thread(
739       FROM_HERE, base::Bind(bta_dm_ble_config_local_privacy, privacy_enable));
740 #else
741   UNUSED(privacy_enable);
742 #endif
743 }
744 
745 /*******************************************************************************
746  *
747  * Function         BTA_DmBleGetEnergyInfo
748  *
749  * Description      This function is called to obtain the energy info
750  *
751  * Parameters       p_cmpl_cback - Command complete callback
752  *
753  * Returns          void
754  *
755  ******************************************************************************/
BTA_DmBleGetEnergyInfo(tBTA_BLE_ENERGY_INFO_CBACK * p_cmpl_cback)756 void BTA_DmBleGetEnergyInfo(tBTA_BLE_ENERGY_INFO_CBACK* p_cmpl_cback) {
757   do_in_main_thread(FROM_HERE,
758                     base::Bind(bta_dm_ble_get_energy_info, p_cmpl_cback));
759 }
760 
761 /** This function is to set maximum LE data packet size */
BTA_DmBleSetDataLength(const RawAddress & remote_device,uint16_t tx_data_length)762 void BTA_DmBleSetDataLength(const RawAddress& remote_device,
763                             uint16_t tx_data_length) {
764   do_in_main_thread(FROM_HERE, base::Bind(bta_dm_ble_set_data_length,
765                                           remote_device, tx_data_length));
766 }
767 
768 /*******************************************************************************
769  *
770  * Function         BTA_DmSetEncryption
771  *
772  * Description      This function is called to ensure that connection is
773  *                  encrypted.  Should be called only on an open connection.
774  *                  Typically only needed for connections that first want to
775  *                  bring up unencrypted links, then later encrypt them.
776  *
777  * Parameters:      bd_addr       - Address of the peer device
778  *                  transport     - transport of the link to be encruypted
779  *                  p_callback    - Pointer to callback function to indicat the
780  *                                  link encryption status
781  *                  sec_act       - This is the security action to indicate
782  *                                  what kind of BLE security level is required
783  *                                  for the BLE link if BLE is supported.
784  *                                  Note: This parameter is ignored for the
785  *                                        BR/EDR or if BLE is not supported.
786  *
787  * Returns          void
788  *
789  ******************************************************************************/
BTA_DmSetEncryption(const RawAddress & bd_addr,tBTA_TRANSPORT transport,tBTA_DM_ENCRYPT_CBACK * p_callback,tBTA_DM_BLE_SEC_ACT sec_act)790 void BTA_DmSetEncryption(const RawAddress& bd_addr, tBTA_TRANSPORT transport,
791                          tBTA_DM_ENCRYPT_CBACK* p_callback,
792                          tBTA_DM_BLE_SEC_ACT sec_act) {
793   APPL_TRACE_API("%s", __func__);
794   do_in_main_thread(FROM_HERE, base::Bind(bta_dm_set_encryption, bd_addr,
795                                           transport, p_callback, sec_act));
796 }
797 
798 /*******************************************************************************
799  *
800  * Function         BTA_DmCloseACL
801  *
802  * Description      This function force to close an ACL connection and remove
803  *                  the device from the security database list of known devices.
804  *
805  * Parameters:      bd_addr       - Address of the peer device
806  *                  remove_dev    - remove device or not after link down
807  *
808  * Returns          void
809  *
810  ******************************************************************************/
BTA_DmCloseACL(const RawAddress & bd_addr,bool remove_dev,tBTA_TRANSPORT transport)811 void BTA_DmCloseACL(const RawAddress& bd_addr, bool remove_dev,
812                     tBTA_TRANSPORT transport) {
813   do_in_main_thread(
814       FROM_HERE, base::Bind(bta_dm_close_acl, bd_addr, remove_dev, transport));
815 }
816 
817 /*******************************************************************************
818  *
819  * Function         BTA_DmBleObserve
820  *
821  * Description      This procedure keep the device listening for advertising
822  *                  events from a broadcast device.
823  *
824  * Parameters       start: start or stop observe.
825  *
826  * Returns          void
827 
828  *
829  * Returns          void.
830  *
831  ******************************************************************************/
BTA_DmBleObserve(bool start,uint8_t duration,tBTA_DM_SEARCH_CBACK * p_results_cb)832 extern void BTA_DmBleObserve(bool start, uint8_t duration,
833                              tBTA_DM_SEARCH_CBACK* p_results_cb) {
834   APPL_TRACE_API("%s:start = %d ", __func__, start);
835   do_in_main_thread(
836       FROM_HERE, base::Bind(bta_dm_ble_observe, start, duration, p_results_cb));
837 }
838 
839 /*******************************************************************************
840  *
841  * Function         BTA_VendorInit
842  *
843  * Description      This function initializes vendor specific
844  *
845  * Returns          void
846  *
847  ******************************************************************************/
BTA_VendorInit(void)848 void BTA_VendorInit(void) { APPL_TRACE_API("BTA_VendorInit"); }
849 
850 /*******************************************************************************
851  *
852  * Function         BTA_VendorCleanup
853  *
854  * Description      This function frees up Broadcom specific VS specific dynamic
855  *                  memory
856  *
857  * Returns          void
858  *
859  ******************************************************************************/
BTA_VendorCleanup(void)860 void BTA_VendorCleanup(void) {
861   tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
862   BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
863 
864   if (cmn_ble_vsc_cb.max_filter > 0) {
865     btm_ble_adv_filter_cleanup();
866 #if (BLE_PRIVACY_SPT == TRUE)
867     btm_ble_resolving_list_cleanup();
868 #endif
869   }
870 
871   if (cmn_ble_vsc_cb.tot_scan_results_strg > 0) btm_ble_batchscan_cleanup();
872 
873   if (cmn_ble_vsc_cb.adv_inst_max > 0) btm_ble_multi_adv_cleanup();
874 }
875