bta_dm_api.cc revision bced7f0c0b1155e3619644b1d2db1f7fe9a12703
1/******************************************************************************
2 *
3 *  Copyright (C) 2003-2014 Broadcom Corporation
4 *
5 *  Licensed under the Apache License, Version 2.0 (the "License");
6 *  you may not use this file except in compliance with the License.
7 *  You may obtain a copy of the License at:
8 *
9 *  http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 *
17 ******************************************************************************/
18
19/******************************************************************************
20 *
21 *  This is the API implementation file for the BTA device manager.
22 *
23 ******************************************************************************/
24#include <base/bind_helpers.h>
25#include <string.h>
26
27#include "bt_common.h"
28#include "bta_api.h"
29#include "bta_closure_api.h"
30#include "bta_dm_int.h"
31#include "bta_sys.h"
32#include "bta_sys_int.h"
33#include "btm_api.h"
34#include "btm_int.h"
35#include "osi/include/osi.h"
36#include "utl.h"
37
38/*****************************************************************************
39 *  Constants
40 ****************************************************************************/
41
42static const tBTA_SYS_REG bta_dm_reg = {bta_dm_sm_execute, bta_dm_sm_disable};
43
44static 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 ******************************************************************************/
58tBTA_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, &bta_dm_reg);
63  bta_sys_register(BTA_ID_DM_SEARCH, &bta_dm_search_reg);
64
65  /* if UUID list is not provided as static data */
66  bta_sys_eir_register(bta_dm_eir_update_uuid);
67
68  tBTA_DM_API_ENABLE* p_msg =
69      (tBTA_DM_API_ENABLE*)osi_malloc(sizeof(tBTA_DM_API_ENABLE));
70  p_msg->hdr.event = BTA_DM_API_ENABLE_EVT;
71  p_msg->p_sec_cback = p_cback;
72
73  bta_sys_sendmsg(p_msg);
74
75  return BTA_SUCCESS;
76}
77
78/*******************************************************************************
79 *
80 * Function         BTA_DisableBluetooth
81 *
82 * Description      Disables bluetooth service.  This function is called when
83 *                  the application no longer needs bluetooth service
84 *
85 * Returns          void
86 *
87 ******************************************************************************/
88tBTA_STATUS BTA_DisableBluetooth(void) {
89  BT_HDR* p_msg = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
90
91  p_msg->event = BTA_DM_API_DISABLE_EVT;
92
93  bta_sys_sendmsg(p_msg);
94
95  return BTA_SUCCESS;
96}
97
98/*******************************************************************************
99 *
100 * Function         BTA_EnableTestMode
101 *
102 * Description      Enables bluetooth device under test mode
103 *
104 *
105 * Returns          tBTA_STATUS
106 *
107 ******************************************************************************/
108tBTA_STATUS BTA_EnableTestMode(void) {
109  BT_HDR* p_msg = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
110
111  APPL_TRACE_API("%s", __func__);
112
113  p_msg->event = BTA_DM_API_ENABLE_TEST_MODE_EVT;
114  bta_sys_sendmsg(p_msg);
115
116  return BTA_SUCCESS;
117}
118
119/*******************************************************************************
120 *
121 * Function         BTA_DisableTestMode
122 *
123 * Description      Disable bluetooth device under test mode
124 *
125 *
126 * Returns          None
127 *
128 ******************************************************************************/
129void BTA_DisableTestMode(void) {
130  BT_HDR* p_msg = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
131
132  APPL_TRACE_API("%s", __func__);
133
134  p_msg->event = BTA_DM_API_DISABLE_TEST_MODE_EVT;
135  bta_sys_sendmsg(p_msg);
136}
137
138/*******************************************************************************
139 *
140 * Function         BTA_DmSetDeviceName
141 *
142 * Description      This function sets the Bluetooth name of local device
143 *
144 *
145 * Returns          void
146 *
147 ******************************************************************************/
148void BTA_DmSetDeviceName(char* p_name) {
149  tBTA_DM_API_SET_NAME* p_msg =
150      (tBTA_DM_API_SET_NAME*)osi_malloc(sizeof(tBTA_DM_API_SET_NAME));
151
152  p_msg->hdr.event = BTA_DM_API_SET_NAME_EVT;
153  strlcpy((char*)p_msg->name, p_name, BD_NAME_LEN);
154
155  bta_sys_sendmsg(p_msg);
156}
157
158/*******************************************************************************
159 *
160 * Function         BTA_DmSetVisibility
161 *
162 * Description      This function sets the Bluetooth connectable,
163 *                  discoverable, pairable and conn paired only modes of local
164 *                  device
165 *
166 *
167 * Returns          void
168 *
169 ******************************************************************************/
170void BTA_DmSetVisibility(tBTA_DM_DISC disc_mode, tBTA_DM_CONN conn_mode,
171                         uint8_t pairable_mode, uint8_t conn_filter) {
172  tBTA_DM_API_SET_VISIBILITY* p_msg =
173      (tBTA_DM_API_SET_VISIBILITY*)osi_malloc(sizeof(tBTA_DM_MSG));
174
175  p_msg->hdr.event = BTA_DM_API_SET_VISIBILITY_EVT;
176  p_msg->disc_mode = disc_mode;
177  p_msg->conn_mode = conn_mode;
178  p_msg->pair_mode = pairable_mode;
179  p_msg->conn_paired_only = conn_filter;
180
181  bta_sys_sendmsg(p_msg);
182}
183
184/*******************************************************************************
185 *
186 * Function         BTA_DmSearch
187 *
188 * Description      This function searches for peer Bluetooth devices. It
189 *                  performs an inquiry and gets the remote name for devices.
190 *                  Service discovery is done if services is non zero
191 *
192 *
193 * Returns          void
194 *
195 ******************************************************************************/
196void BTA_DmSearch(tBTA_DM_INQ* p_dm_inq, tBTA_SERVICE_MASK services,
197                  tBTA_DM_SEARCH_CBACK* p_cback) {
198  tBTA_DM_API_SEARCH* p_msg =
199      (tBTA_DM_API_SEARCH*)osi_calloc(sizeof(tBTA_DM_API_SEARCH));
200
201  p_msg->hdr.event = BTA_DM_API_SEARCH_EVT;
202  memcpy(&p_msg->inq_params, p_dm_inq, sizeof(tBTA_DM_INQ));
203  p_msg->services = services;
204  p_msg->p_cback = p_cback;
205  p_msg->rs_res = BTA_DM_RS_NONE;
206
207  bta_sys_sendmsg(p_msg);
208}
209
210/*******************************************************************************
211 *
212 * Function         BTA_DmSearchCancel
213 *
214 * Description      This function  cancels a search initiated by BTA_DmSearch
215 *
216 *
217 * Returns          void
218 *
219 ******************************************************************************/
220void BTA_DmSearchCancel(void) {
221  BT_HDR* p_msg = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
222
223  p_msg->event = BTA_DM_API_SEARCH_CANCEL_EVT;
224  bta_sys_sendmsg(p_msg);
225}
226
227/*******************************************************************************
228 *
229 * Function         BTA_DmDiscover
230 *
231 * Description      This function does service discovery for services of a
232 *                  peer device
233 *
234 *
235 * Returns          void
236 *
237 ******************************************************************************/
238void BTA_DmDiscover(BD_ADDR bd_addr, tBTA_SERVICE_MASK services,
239                    tBTA_DM_SEARCH_CBACK* p_cback, bool sdp_search) {
240  tBTA_DM_API_DISCOVER* p_msg =
241      (tBTA_DM_API_DISCOVER*)osi_calloc(sizeof(tBTA_DM_API_DISCOVER));
242
243  p_msg->hdr.event = BTA_DM_API_DISCOVER_EVT;
244  bdcpy(p_msg->bd_addr, bd_addr);
245  p_msg->services = services;
246  p_msg->p_cback = p_cback;
247  p_msg->sdp_search = sdp_search;
248
249  bta_sys_sendmsg(p_msg);
250}
251
252/*******************************************************************************
253 *
254 * Function         BTA_DmDiscoverUUID
255 *
256 * Description      This function does service discovery for services of a
257 *                  peer device
258 *
259 *
260 * Returns          void
261 *
262 ******************************************************************************/
263void BTA_DmDiscoverUUID(BD_ADDR bd_addr, tSDP_UUID* uuid,
264                        tBTA_DM_SEARCH_CBACK* p_cback, bool sdp_search) {
265  tBTA_DM_API_DISCOVER* p_msg =
266      (tBTA_DM_API_DISCOVER*)osi_malloc(sizeof(tBTA_DM_API_DISCOVER));
267
268  p_msg->hdr.event = BTA_DM_API_DISCOVER_EVT;
269  bdcpy(p_msg->bd_addr, bd_addr);
270  p_msg->services = BTA_USER_SERVICE_MASK;  // Not exposed at API level
271  p_msg->p_cback = p_cback;
272  p_msg->sdp_search = sdp_search;
273
274  p_msg->num_uuid = 0;
275  p_msg->p_uuid = NULL;
276
277  memcpy(&p_msg->uuid, uuid, sizeof(tSDP_UUID));
278
279  bta_sys_sendmsg(p_msg);
280}
281
282/*******************************************************************************
283 *
284 * Function         BTA_DmBond
285 *
286 * Description      This function initiates a bonding procedure with a peer
287 *                  device
288 *
289 *
290 * Returns          void
291 *
292 ******************************************************************************/
293void BTA_DmBond(BD_ADDR bd_addr) {
294  tBTA_DM_API_BOND* p_msg =
295      (tBTA_DM_API_BOND*)osi_malloc(sizeof(tBTA_DM_API_BOND));
296
297  p_msg->hdr.event = BTA_DM_API_BOND_EVT;
298  bdcpy(p_msg->bd_addr, bd_addr);
299  p_msg->transport = BTA_TRANSPORT_UNKNOWN;
300
301  bta_sys_sendmsg(p_msg);
302}
303
304/*******************************************************************************
305 *
306 * Function         BTA_DmBondByTransports
307 *
308 * Description      This function initiates a bonding procedure with a peer
309 *                  device
310 *
311 *
312 * Returns          void
313 *
314 ******************************************************************************/
315void BTA_DmBondByTransport(BD_ADDR bd_addr, tBTA_TRANSPORT transport) {
316  tBTA_DM_API_BOND* p_msg =
317      (tBTA_DM_API_BOND*)osi_malloc(sizeof(tBTA_DM_API_BOND));
318
319  p_msg->hdr.event = BTA_DM_API_BOND_EVT;
320  bdcpy(p_msg->bd_addr, bd_addr);
321  p_msg->transport = transport;
322
323  bta_sys_sendmsg(p_msg);
324}
325
326/*******************************************************************************
327 *
328 * Function         BTA_DmBondCancel
329 *
330 * Description      This function cancels the bonding procedure with a peer
331 *                  device
332 *
333 *
334 * Returns          void
335 *
336 ******************************************************************************/
337void BTA_DmBondCancel(BD_ADDR bd_addr) {
338  tBTA_DM_API_BOND_CANCEL* p_msg =
339      (tBTA_DM_API_BOND_CANCEL*)osi_malloc(sizeof(tBTA_DM_API_BOND_CANCEL));
340
341  p_msg->hdr.event = BTA_DM_API_BOND_CANCEL_EVT;
342  bdcpy(p_msg->bd_addr, bd_addr);
343
344  bta_sys_sendmsg(p_msg);
345}
346
347/*******************************************************************************
348 *
349 * Function         BTA_DmPinReply
350 *
351 * Description      This function provides a pincode for a remote device when
352 *                  one is requested by DM through BTA_DM_PIN_REQ_EVT
353 *
354 *
355 * Returns          void
356 *
357 ******************************************************************************/
358void BTA_DmPinReply(BD_ADDR bd_addr, bool accept, uint8_t pin_len,
359                    uint8_t* p_pin)
360
361{
362  tBTA_DM_API_PIN_REPLY* p_msg =
363      (tBTA_DM_API_PIN_REPLY*)osi_malloc(sizeof(tBTA_DM_API_PIN_REPLY));
364
365  p_msg->hdr.event = BTA_DM_API_PIN_REPLY_EVT;
366  bdcpy(p_msg->bd_addr, bd_addr);
367  p_msg->accept = accept;
368  if (accept) {
369    p_msg->pin_len = pin_len;
370    memcpy(p_msg->p_pin, p_pin, pin_len);
371  }
372
373  bta_sys_sendmsg(p_msg);
374}
375
376/*******************************************************************************
377 *
378 * Function         BTA_DmLocalOob
379 *
380 * Description      This function retrieves the OOB data from local controller.
381 *                  The result is reported by:
382 *                  - bta_dm_co_loc_oob_ext() if device supports secure
383 *                    connections (SC)
384 *                  - bta_dm_co_loc_oob() if device doesn't support SC
385 *
386 * Returns          void
387 *
388 ******************************************************************************/
389void BTA_DmLocalOob(void) {
390  tBTA_DM_API_LOC_OOB* p_msg =
391      (tBTA_DM_API_LOC_OOB*)osi_malloc(sizeof(tBTA_DM_API_LOC_OOB));
392
393  p_msg->hdr.event = BTA_DM_API_LOC_OOB_EVT;
394  bta_sys_sendmsg(p_msg);
395}
396
397/*******************************************************************************
398 *
399 * Function         BTA_DmConfirm
400 *
401 * Description      This function accepts or rejects the numerical value of the
402 *                  Simple Pairing process on BTA_DM_SP_CFM_REQ_EVT
403 *
404 * Returns          void
405 *
406 ******************************************************************************/
407void BTA_DmConfirm(BD_ADDR bd_addr, bool accept) {
408  tBTA_DM_API_CONFIRM* p_msg =
409      (tBTA_DM_API_CONFIRM*)osi_malloc(sizeof(tBTA_DM_API_CONFIRM));
410
411  p_msg->hdr.event = BTA_DM_API_CONFIRM_EVT;
412  bdcpy(p_msg->bd_addr, bd_addr);
413  p_msg->accept = accept;
414
415  bta_sys_sendmsg(p_msg);
416}
417
418/*******************************************************************************
419 *
420 * Function         BTA_DmAddDevice
421 *
422 * Description      This function adds a device to the security database list of
423 *                  peer device
424 *
425 *
426 * Returns          void
427 *
428 ******************************************************************************/
429void BTA_DmAddDevice(BD_ADDR bd_addr, DEV_CLASS dev_class, LINK_KEY link_key,
430                     tBTA_SERVICE_MASK trusted_mask, bool is_trusted,
431                     uint8_t key_type, tBTA_IO_CAP io_cap, uint8_t pin_length) {
432  tBTA_DM_API_ADD_DEVICE* p_msg =
433      (tBTA_DM_API_ADD_DEVICE*)osi_calloc(sizeof(tBTA_DM_API_ADD_DEVICE));
434
435  p_msg->hdr.event = BTA_DM_API_ADD_DEVICE_EVT;
436  bdcpy(p_msg->bd_addr, bd_addr);
437  p_msg->tm = trusted_mask;
438  p_msg->is_trusted = is_trusted;
439  p_msg->io_cap = io_cap;
440
441  if (link_key) {
442    p_msg->link_key_known = true;
443    p_msg->key_type = key_type;
444    memcpy(p_msg->link_key, link_key, LINK_KEY_LEN);
445  }
446
447  /* Load device class if specified */
448  if (dev_class) {
449    p_msg->dc_known = true;
450    memcpy(p_msg->dc, dev_class, DEV_CLASS_LEN);
451  }
452
453  memset(p_msg->bd_name, 0, BD_NAME_LEN + 1);
454  memset(p_msg->features, 0, sizeof(p_msg->features));
455  p_msg->pin_length = pin_length;
456
457  bta_sys_sendmsg(p_msg);
458}
459
460/*******************************************************************************
461 *
462 * Function         BTA_DmRemoveDevice
463 *
464 * Description      This function removes a device fromthe security database
465 *                  list of peer device. It manages unpairing even while
466 *                  connected.
467 *
468 *
469 * Returns          void
470 *
471 ******************************************************************************/
472tBTA_STATUS BTA_DmRemoveDevice(BD_ADDR bd_addr) {
473  tBTA_DM_API_REMOVE_DEVICE* p_msg =
474      (tBTA_DM_API_REMOVE_DEVICE*)osi_calloc(sizeof(tBTA_DM_API_REMOVE_DEVICE));
475
476  p_msg->hdr.event = BTA_DM_API_REMOVE_DEVICE_EVT;
477  bdcpy(p_msg->bd_addr, bd_addr);
478
479  bta_sys_sendmsg(p_msg);
480
481  return BTA_SUCCESS;
482}
483
484/*******************************************************************************
485 *
486 * Function         BTA_GetEirService
487 *
488 * Description      This function is called to get BTA service mask from EIR.
489 *
490 * Parameters       p_eir - pointer of EIR significant part
491 *                  p_services - return the BTA service mask
492 *
493 * Returns          None
494 *
495 ******************************************************************************/
496extern const uint16_t bta_service_id_to_uuid_lkup_tbl[];
497void BTA_GetEirService(uint8_t* p_eir, tBTA_SERVICE_MASK* p_services) {
498  uint8_t xx, yy;
499  uint8_t num_uuid, max_num_uuid = 32;
500  uint8_t uuid_list[32 * LEN_UUID_16];
501  uint16_t* p_uuid16 = (uint16_t*)uuid_list;
502  tBTA_SERVICE_MASK mask;
503
504  BTM_GetEirUuidList(p_eir, LEN_UUID_16, &num_uuid, uuid_list, max_num_uuid);
505  for (xx = 0; xx < num_uuid; xx++) {
506    mask = 1;
507    for (yy = 0; yy < BTA_MAX_SERVICE_ID; yy++) {
508      if (*(p_uuid16 + xx) == bta_service_id_to_uuid_lkup_tbl[yy]) {
509        *p_services |= mask;
510        break;
511      }
512      mask <<= 1;
513    }
514
515    /* for HSP v1.2 only device */
516    if (*(p_uuid16 + xx) == UUID_SERVCLASS_HEADSET_HS)
517      *p_services |= BTA_HSP_SERVICE_MASK;
518
519    if (*(p_uuid16 + xx) == UUID_SERVCLASS_HDP_SOURCE)
520      *p_services |= BTA_HL_SERVICE_MASK;
521
522    if (*(p_uuid16 + xx) == UUID_SERVCLASS_HDP_SINK)
523      *p_services |= BTA_HL_SERVICE_MASK;
524  }
525}
526
527/*******************************************************************************
528 *
529 * Function         BTA_DmGetConnectionState
530 *
531 * Description      Returns whether the remote device is currently connected.
532 *
533 * Returns          0 if the device is NOT connected.
534 *
535 ******************************************************************************/
536uint16_t BTA_DmGetConnectionState(const BD_ADDR bd_addr) {
537  tBTA_DM_PEER_DEVICE* p_dev = bta_dm_find_peer_device(bd_addr);
538  return (p_dev && p_dev->conn_state == BTA_DM_CONNECTED);
539}
540
541/*******************************************************************************
542 *                   Device Identification (DI) Server Functions
543 ******************************************************************************/
544/*******************************************************************************
545 *
546 * Function         BTA_DmSetLocalDiRecord
547 *
548 * Description      This function adds a DI record to the local SDP database.
549 *
550 * Returns          BTA_SUCCESS if record set sucessfully, otherwise error code.
551 *
552 ******************************************************************************/
553tBTA_STATUS BTA_DmSetLocalDiRecord(tBTA_DI_RECORD* p_device_info,
554                                   uint32_t* p_handle) {
555  tBTA_STATUS status = BTA_FAILURE;
556
557  if (bta_dm_di_cb.di_num < BTA_DI_NUM_MAX) {
558    if (SDP_SetLocalDiRecord((tSDP_DI_RECORD*)p_device_info, p_handle) ==
559        SDP_SUCCESS) {
560      if (!p_device_info->primary_record) {
561        bta_dm_di_cb.di_handle[bta_dm_di_cb.di_num] = *p_handle;
562        bta_dm_di_cb.di_num++;
563      }
564
565      bta_sys_add_uuid(UUID_SERVCLASS_PNP_INFORMATION);
566      status = BTA_SUCCESS;
567    }
568  }
569
570  return status;
571}
572
573/*******************************************************************************
574 *
575 * Function         bta_dmexecutecallback
576 *
577 * Description      This function will request BTA to execute a call back in the
578 *                  context of BTU task.
579 *                  This API was named in lower case because it is only intended
580 *                  for the internal customers(like BTIF).
581 *
582 * Returns          void
583 *
584 ******************************************************************************/
585void bta_dmexecutecallback(tBTA_DM_EXEC_CBACK* p_callback, void* p_param) {
586  tBTA_DM_API_EXECUTE_CBACK* p_msg =
587      (tBTA_DM_API_EXECUTE_CBACK*)osi_malloc(sizeof(tBTA_DM_MSG));
588
589  p_msg->hdr.event = BTA_DM_API_EXECUTE_CBACK_EVT;
590  p_msg->p_param = p_param;
591  p_msg->p_exec_cback = p_callback;
592
593  bta_sys_sendmsg(p_msg);
594}
595
596/*******************************************************************************
597 *
598 * Function         BTA_DmAddBleKey
599 *
600 * Description      Add/modify LE device information.  This function will be
601 *                  normally called during host startup to restore all required
602 *                  information stored in the NVRAM.
603 *
604 * Parameters:      bd_addr          - BD address of the peer
605 *                  p_le_key         - LE key values.
606 *                  key_type         - LE SMP key type.
607 *
608 * Returns          BTA_SUCCESS if successful
609 *                  BTA_FAIL if operation failed.
610 *
611 ******************************************************************************/
612void BTA_DmAddBleKey(BD_ADDR bd_addr, tBTA_LE_KEY_VALUE* p_le_key,
613                     tBTA_LE_KEY_TYPE key_type) {
614  tBTA_DM_API_ADD_BLEKEY* p_msg =
615      (tBTA_DM_API_ADD_BLEKEY*)osi_calloc(sizeof(tBTA_DM_API_ADD_BLEKEY));
616
617  p_msg->hdr.event = BTA_DM_API_ADD_BLEKEY_EVT;
618  p_msg->key_type = key_type;
619  bdcpy(p_msg->bd_addr, bd_addr);
620  memcpy(&p_msg->blekey, p_le_key, sizeof(tBTA_LE_KEY_VALUE));
621
622  bta_sys_sendmsg(p_msg);
623}
624
625/*******************************************************************************
626 *
627 * Function         BTA_DmAddBleDevice
628 *
629 * Description      Add a BLE device.  This function will be normally called
630 *                  during host startup to restore all required information
631 *                  for a LE device stored in the NVRAM.
632 *
633 * Parameters:      bd_addr          - BD address of the peer
634 *                  dev_type         - Remote device's device type.
635 *                  addr_type        - LE device address type.
636 *
637 * Returns          void
638 *
639 ******************************************************************************/
640void BTA_DmAddBleDevice(BD_ADDR bd_addr, tBLE_ADDR_TYPE addr_type,
641                        tBT_DEVICE_TYPE dev_type) {
642  tBTA_DM_API_ADD_BLE_DEVICE* p_msg = (tBTA_DM_API_ADD_BLE_DEVICE*)osi_calloc(
643      sizeof(tBTA_DM_API_ADD_BLE_DEVICE));
644
645  p_msg->hdr.event = BTA_DM_API_ADD_BLEDEVICE_EVT;
646  bdcpy(p_msg->bd_addr, bd_addr);
647  p_msg->addr_type = addr_type;
648  p_msg->dev_type = dev_type;
649
650  bta_sys_sendmsg(p_msg);
651}
652
653/*******************************************************************************
654 *
655 * Function         BTA_DmBlePasskeyReply
656 *
657 * Description      Send BLE SMP passkey reply.
658 *
659 * Parameters:      bd_addr          - BD address of the peer
660 *                  accept           - passkey entry sucessful or declined.
661 *                  passkey          - passkey value, must be a 6 digit number,
662 *                                     can be lead by 0.
663 *
664 * Returns          void
665 *
666 ******************************************************************************/
667void BTA_DmBlePasskeyReply(BD_ADDR bd_addr, bool accept, uint32_t passkey) {
668  tBTA_DM_API_PASSKEY_REPLY* p_msg =
669      (tBTA_DM_API_PASSKEY_REPLY*)osi_calloc(sizeof(tBTA_DM_API_PASSKEY_REPLY));
670
671  p_msg->hdr.event = BTA_DM_API_BLE_PASSKEY_REPLY_EVT;
672  bdcpy(p_msg->bd_addr, bd_addr);
673  p_msg->accept = accept;
674
675  if (accept) p_msg->passkey = passkey;
676
677  bta_sys_sendmsg(p_msg);
678}
679
680/*******************************************************************************
681 *
682 * Function         BTA_DmBleConfirmReply
683 *
684 * Description      Send BLE SMP SC user confirmation reply.
685 *
686 * Parameters:      bd_addr          - BD address of the peer
687 *                  accept           - numbers to compare are the same or
688 *                                     different.
689 *
690 * Returns          void
691 *
692 ******************************************************************************/
693void BTA_DmBleConfirmReply(BD_ADDR bd_addr, bool accept) {
694  tBTA_DM_API_CONFIRM* p_msg =
695      (tBTA_DM_API_CONFIRM*)osi_calloc(sizeof(tBTA_DM_API_CONFIRM));
696
697  p_msg->hdr.event = BTA_DM_API_BLE_CONFIRM_REPLY_EVT;
698  bdcpy(p_msg->bd_addr, bd_addr);
699  p_msg->accept = accept;
700
701  bta_sys_sendmsg(p_msg);
702}
703
704/*******************************************************************************
705 *
706 * Function         BTA_DmBleSecurityGrant
707 *
708 * Description      Grant security request access.
709 *
710 * Parameters:      bd_addr          - BD address of the peer
711 *                  res              - security grant status.
712 *
713 * Returns          void
714 *
715 ******************************************************************************/
716void BTA_DmBleSecurityGrant(BD_ADDR bd_addr, tBTA_DM_BLE_SEC_GRANT res) {
717  tBTA_DM_API_BLE_SEC_GRANT* p_msg =
718      (tBTA_DM_API_BLE_SEC_GRANT*)osi_calloc(sizeof(tBTA_DM_API_BLE_SEC_GRANT));
719
720  p_msg->hdr.event = BTA_DM_API_BLE_SEC_GRANT_EVT;
721  bdcpy(p_msg->bd_addr, bd_addr);
722  p_msg->res = res;
723
724  bta_sys_sendmsg(p_msg);
725}
726
727/*******************************************************************************
728 *
729 * Function         BTA_DmSetBlePrefConnParams
730 *
731 * Description      This function is called to set the preferred connection
732 *                  parameters when default connection parameter is not desired.
733 *
734 * Parameters:      bd_addr          - BD address of the peripheral
735 *                  scan_interval    - scan interval
736 *                  scan_window      - scan window
737 *                  min_conn_int     - minimum preferred connection interval
738 *                  max_conn_int     - maximum preferred connection interval
739 *                  slave_latency    - preferred slave latency
740 *                  supervision_tout - preferred supervision timeout
741 *
742 *
743 * Returns          void
744 *
745 ******************************************************************************/
746void BTA_DmSetBlePrefConnParams(const BD_ADDR bd_addr, uint16_t min_conn_int,
747                                uint16_t max_conn_int, uint16_t slave_latency,
748                                uint16_t supervision_tout) {
749  tBTA_DM_API_BLE_CONN_PARAMS* p_msg = (tBTA_DM_API_BLE_CONN_PARAMS*)osi_calloc(
750      sizeof(tBTA_DM_API_BLE_CONN_PARAMS));
751
752  p_msg->hdr.event = BTA_DM_API_BLE_CONN_PARAM_EVT;
753  memcpy(p_msg->peer_bda, bd_addr, BD_ADDR_LEN);
754  p_msg->conn_int_max = max_conn_int;
755  p_msg->conn_int_min = min_conn_int;
756  p_msg->slave_latency = slave_latency;
757  p_msg->supervision_tout = supervision_tout;
758
759  bta_sys_sendmsg(p_msg);
760}
761
762/*******************************************************************************
763 *
764 * Function         BTA_DmSetBleConnScanParams
765 *
766 * Description      This function is called to set scan parameters used in
767 *                  BLE connection request
768 *
769 * Parameters:      scan_interval    - scan interval
770 *                  scan_window      - scan window
771 *
772 * Returns          void
773 *
774 ******************************************************************************/
775void BTA_DmSetBleConnScanParams(uint32_t scan_interval, uint32_t scan_window) {
776  tBTA_DM_API_BLE_SCAN_PARAMS* p_msg = (tBTA_DM_API_BLE_SCAN_PARAMS*)osi_calloc(
777      sizeof(tBTA_DM_API_BLE_SCAN_PARAMS));
778
779  p_msg->hdr.event = BTA_DM_API_BLE_CONN_SCAN_PARAM_EVT;
780  p_msg->scan_int = scan_interval;
781  p_msg->scan_window = scan_window;
782
783  bta_sys_sendmsg(p_msg);
784}
785
786/*******************************************************************************
787 *
788 * Function         BTA_DmSetBleScanParams
789 *
790 * Description      This function is called to set scan parameters
791 *
792 * Parameters:      client_if - Client IF
793 *                  scan_interval - scan interval
794 *                  scan_window - scan window
795 *                  scan_mode - scan mode
796 *                  scan_param_setup_status_cback - Set scan param status
797 *                                                  callback
798 *
799 * Returns          void
800 *
801 ******************************************************************************/
802
803void BTA_DmSetBleScanParams(
804    tGATT_IF client_if, uint32_t scan_interval, uint32_t scan_window,
805    tBLE_SCAN_MODE scan_mode,
806    tBLE_SCAN_PARAM_SETUP_CBACK scan_param_setup_cback) {
807  do_in_bta_thread(FROM_HERE, base::Bind(&bta_dm_ble_set_scan_params, client_if,
808                                         scan_interval, scan_window, scan_mode,
809                                         scan_param_setup_cback));
810}
811
812/*******************************************************************************
813 *                      BLE ADV data management API
814 ******************************************************************************/
815
816/*******************************************************************************
817 *
818 * Function         BTA_DmBleSetStorageParams
819 *
820 * Description      This function is called to override the BTA scan response.
821 *
822 * Parameters       batch_scan_full_max -Max storage space (in %) allocated to
823 *                                       full scanning
824 *                  batch_scan_trunc_max -Max storage space (in %) allocated to
825 *                                        truncated scanning
826 *                  batch_scan_notify_threshold -Setup notification level based
827 *                                               on total space
828 *                  p_setup_cback - Setup callback pointer
829 *                  p_thres_cback - Threshold callback pointer
830 *                  p_rep_cback - Reports callback pointer
831 *                  ref_value - Ref value
832 *
833 * Returns          None
834 *
835 ******************************************************************************/
836extern void BTA_DmBleSetStorageParams(
837    uint8_t batch_scan_full_max, uint8_t batch_scan_trunc_max,
838    uint8_t batch_scan_notify_threshold,
839    tBTA_BLE_SCAN_SETUP_CBACK* p_setup_cback,
840    tBTA_BLE_SCAN_THRESHOLD_CBACK* p_thres_cback,
841    tBTA_BLE_SCAN_REP_CBACK* p_rep_cback, tBTA_DM_BLE_REF_VALUE ref_value) {
842  bta_dm_cb.p_setup_cback = p_setup_cback;
843  do_in_bta_thread(
844      FROM_HERE,
845      base::Bind(&bta_dm_ble_setup_storage, batch_scan_full_max,
846                 batch_scan_trunc_max, batch_scan_notify_threshold,
847                 bta_ble_scan_setup_cb, p_thres_cback, p_rep_cback, ref_value));
848}
849
850/*******************************************************************************
851 *
852 * Function         BTA_DmBleEnableBatchScan
853 *
854 * Description      This function is called to enable the batch scan
855 *
856 * Parameters       scan_mode -Batch scan mode
857 *                  scan_interval - Scan interval
858 *                  scan_window - Scan window
859 *                  discard_rule -Discard rules
860 *                  addr_type - Address type
861 *                  ref_value - Reference value
862 *
863 * Returns          None
864 *
865 ******************************************************************************/
866extern void BTA_DmBleEnableBatchScan(tBTA_BLE_BATCH_SCAN_MODE scan_mode,
867                                     uint32_t scan_interval,
868                                     uint32_t scan_window,
869                                     tBTA_BLE_DISCARD_RULE discard_rule,
870                                     tBLE_ADDR_TYPE addr_type,
871                                     tBTA_DM_BLE_REF_VALUE ref_value) {
872  do_in_bta_thread(FROM_HERE, base::Bind(&bta_dm_ble_enable_batch_scan,
873                                         scan_mode, scan_interval, scan_window,
874                                         discard_rule, addr_type, ref_value));
875}
876
877/*******************************************************************************
878 *
879 * Function         BTA_DmBleDisableBatchScan
880 *
881 * Description      This function is called to disable the batch scan
882 *
883 * Parameters       ref_value - Reference value
884 *
885 * Returns          None
886 *
887 ******************************************************************************/
888extern void BTA_DmBleDisableBatchScan(tBTA_DM_BLE_REF_VALUE ref_value) {
889  do_in_bta_thread(FROM_HERE,
890                   base::Bind(&bta_dm_ble_disable_batch_scan, ref_value));
891}
892
893/*******************************************************************************
894 *
895 * Function         BTA_DmBleReadScanReports
896 *
897 * Description      This function is called to read scan reports
898 *
899 * Parameters       scan_type -Batch scan mode
900 *                  ref_value - Reference value
901 *
902 * Returns          None
903 *
904 ******************************************************************************/
905extern void BTA_DmBleReadScanReports(tBTA_BLE_BATCH_SCAN_MODE scan_type,
906                                     tBTA_DM_BLE_REF_VALUE ref_value) {
907  do_in_bta_thread(FROM_HERE, base::Bind(&bta_dm_ble_read_scan_reports,
908                                         scan_type, ref_value));
909}
910
911/*******************************************************************************
912 *
913 * Function         BTA_DmBleTrackAdvertiser
914 *
915 * Description      This function is called to track advertiser
916 *
917 * Parameters       ref_value - Reference value
918 *                  p_track_adv_cback - Track ADV callback
919 *
920 * Returns          None
921 *
922 ******************************************************************************/
923extern void BTA_DmBleTrackAdvertiser(
924    tBTA_DM_BLE_REF_VALUE ref_value,
925    tBTA_BLE_TRACK_ADV_CBACK* p_track_adv_cback) {
926  tBTA_DM_API_TRACK_ADVERTISER* p_msg =
927      (tBTA_DM_API_TRACK_ADVERTISER*)osi_malloc(
928          sizeof(tBTA_DM_API_TRACK_ADVERTISER));
929
930  p_msg->hdr.event = BTA_DM_API_BLE_TRACK_ADVERTISER_EVT;
931  p_msg->p_track_adv_cback = p_track_adv_cback;
932  p_msg->ref_value = ref_value;
933
934  bta_sys_sendmsg(p_msg);
935}
936
937/*******************************************************************************
938 *                      BLE ADV data management API
939 ******************************************************************************/
940
941/**
942 * Set BLE connectable mode to auto connect
943 */
944void BTA_DmBleStartAutoConn() {
945  tBTA_DM_API_SET_NAME* p_msg =
946      (tBTA_DM_API_SET_NAME*)osi_calloc(sizeof(tBTA_DM_API_SET_NAME));
947
948  p_msg->hdr.event = BTA_DM_API_BLE_SET_BG_CONN_TYPE;
949  bta_sys_sendmsg(p_msg);
950}
951
952/*******************************************************************************
953 *
954 * Function         bta_dm_discover_send_msg
955 *
956 * Description      This function send discover message to BTA task.
957 *
958 * Returns          void
959 *
960 ******************************************************************************/
961static void bta_dm_discover_send_msg(BD_ADDR bd_addr,
962                                     tBTA_SERVICE_MASK_EXT* p_services,
963                                     tBTA_DM_SEARCH_CBACK* p_cback,
964                                     bool sdp_search,
965                                     tBTA_TRANSPORT transport) {
966  const size_t len = p_services ? (sizeof(tBTA_DM_API_DISCOVER) +
967                                   sizeof(tBT_UUID) * p_services->num_uuid)
968                                : sizeof(tBTA_DM_API_DISCOVER);
969  tBTA_DM_API_DISCOVER* p_msg = (tBTA_DM_API_DISCOVER*)osi_calloc(len);
970
971  p_msg->hdr.event = BTA_DM_API_DISCOVER_EVT;
972  bdcpy(p_msg->bd_addr, bd_addr);
973  p_msg->p_cback = p_cback;
974  p_msg->sdp_search = sdp_search;
975  p_msg->transport = transport;
976
977  if (p_services != NULL) {
978    p_msg->services = p_services->srvc_mask;
979    p_msg->num_uuid = p_services->num_uuid;
980    if (p_services->num_uuid != 0) {
981      p_msg->p_uuid = (tBT_UUID*)(p_msg + 1);
982      memcpy(p_msg->p_uuid, p_services->p_uuid,
983             sizeof(tBT_UUID) * p_services->num_uuid);
984    }
985  }
986
987  bta_sys_sendmsg(p_msg);
988}
989
990/*******************************************************************************
991 *
992 * Function         BTA_DmDiscoverByTransport
993 *
994 * Description      This function does service discovery on particular transport
995 *                  for services of a
996 *                  peer device. When services.num_uuid is 0, it indicates all
997 *                  GATT based services are to be searched; otherwise a list of
998 *                  UUID of interested services should be provided through
999 *                  p_services->p_uuid.
1000 *
1001 *
1002 *
1003 * Returns          void
1004 *
1005 ******************************************************************************/
1006void BTA_DmDiscoverByTransport(BD_ADDR bd_addr,
1007                               tBTA_SERVICE_MASK_EXT* p_services,
1008                               tBTA_DM_SEARCH_CBACK* p_cback, bool sdp_search,
1009                               tBTA_TRANSPORT transport) {
1010  bta_dm_discover_send_msg(bd_addr, p_services, p_cback, sdp_search, transport);
1011}
1012
1013/*******************************************************************************
1014 *
1015 * Function         BTA_DmDiscoverExt
1016 *
1017 * Description      This function does service discovery for services of a
1018 *                  peer device. When services.num_uuid is 0, it indicates all
1019 *                  GATT based services are to be searched; other wise a list of
1020 *                  UUID of interested services should be provided through
1021 *                  p_services->p_uuid.
1022 *
1023 *
1024 *
1025 * Returns          void
1026 *
1027 ******************************************************************************/
1028void BTA_DmDiscoverExt(BD_ADDR bd_addr, tBTA_SERVICE_MASK_EXT* p_services,
1029                       tBTA_DM_SEARCH_CBACK* p_cback, bool sdp_search) {
1030  bta_dm_discover_send_msg(bd_addr, p_services, p_cback, sdp_search,
1031                           BTA_TRANSPORT_UNKNOWN);
1032}
1033
1034/*******************************************************************************
1035 *
1036 * Function         BTA_DmSearchExt
1037 *
1038 * Description      This function searches for peer Bluetooth devices. It
1039 *                  performs an inquiry and gets the remote name for devices.
1040 *                  Service discovery is done if services is non zero
1041 *
1042 * Parameters       p_dm_inq: inquiry conditions
1043 *                  p_services: if service is not empty, service discovery will
1044 *                              be done. For all GATT based service conditions,
1045 *                              put num_uuid, and p_uuid is the pointer to the
1046 *                              list of UUID values.
1047 *                  p_cback: callback function when search is completed.
1048 *
1049 *
1050 *
1051 * Returns          void
1052 *
1053 ******************************************************************************/
1054void BTA_DmSearchExt(tBTA_DM_INQ* p_dm_inq, tBTA_SERVICE_MASK_EXT* p_services,
1055                     tBTA_DM_SEARCH_CBACK* p_cback) {
1056  const size_t len = p_services ? (sizeof(tBTA_DM_API_SEARCH) +
1057                                   sizeof(tBT_UUID) * p_services->num_uuid)
1058                                : sizeof(tBTA_DM_API_SEARCH);
1059  tBTA_DM_API_SEARCH* p_msg = (tBTA_DM_API_SEARCH*)osi_calloc(len);
1060
1061  p_msg->hdr.event = BTA_DM_API_SEARCH_EVT;
1062  memcpy(&p_msg->inq_params, p_dm_inq, sizeof(tBTA_DM_INQ));
1063  p_msg->p_cback = p_cback;
1064  p_msg->rs_res = BTA_DM_RS_NONE;
1065
1066  if (p_services != NULL) {
1067    p_msg->services = p_services->srvc_mask;
1068    p_msg->num_uuid = p_services->num_uuid;
1069
1070    if (p_services->num_uuid != 0) {
1071      p_msg->p_uuid = (tBT_UUID*)(p_msg + 1);
1072      memcpy(p_msg->p_uuid, p_services->p_uuid,
1073             sizeof(tBT_UUID) * p_services->num_uuid);
1074    } else {
1075      p_msg->p_uuid = NULL;
1076    }
1077  }
1078
1079  bta_sys_sendmsg(p_msg);
1080}
1081
1082/*******************************************************************************
1083 *
1084 * Function         BTA_DmBleUpdateConnectionParam
1085 *
1086 * Description      Update connection parameters, can only be used when
1087 *                  connection is up.
1088 *
1089 * Parameters:      bd_addr          - BD address of the peer
1090 *                  min_int   -     minimum connection interval,
1091 *                                  [0x0004 ~ 0x4000]
1092 *                  max_int   -     maximum connection interval,
1093 *                                  [0x0004 ~ 0x4000]
1094 *                  latency   -     slave latency [0 ~ 500]
1095 *                  timeout   -     supervision timeout [0x000a ~ 0xc80]
1096 *
1097 * Returns          void
1098 *
1099 ******************************************************************************/
1100void BTA_DmBleUpdateConnectionParam(BD_ADDR bd_addr, uint16_t min_int,
1101                                    uint16_t max_int, uint16_t latency,
1102                                    uint16_t timeout) {
1103  tBTA_DM_API_UPDATE_CONN_PARAM* p_msg =
1104      (tBTA_DM_API_UPDATE_CONN_PARAM*)osi_calloc(
1105          sizeof(tBTA_DM_API_UPDATE_CONN_PARAM));
1106
1107  p_msg->hdr.event = BTA_DM_API_UPDATE_CONN_PARAM_EVT;
1108  bdcpy(p_msg->bd_addr, bd_addr);
1109  p_msg->min_int = min_int;
1110  p_msg->max_int = max_int;
1111  p_msg->latency = latency;
1112  p_msg->timeout = timeout;
1113
1114  bta_sys_sendmsg(p_msg);
1115}
1116
1117/*******************************************************************************
1118 *
1119 * Function         BTA_DmBleConfigLocalPrivacy
1120 *
1121 * Description      Enable/disable privacy on the local device
1122 *
1123 * Parameters:      privacy_enable   - enable/disabe privacy on remote device.
1124 *
1125 * Returns          void
1126 *
1127 ******************************************************************************/
1128void BTA_DmBleConfigLocalPrivacy(bool privacy_enable) {
1129#if (BLE_PRIVACY_SPT == TRUE)
1130  tBTA_DM_API_LOCAL_PRIVACY* p_msg = (tBTA_DM_API_LOCAL_PRIVACY*)osi_calloc(
1131      sizeof(tBTA_DM_API_ENABLE_PRIVACY));
1132
1133  p_msg->hdr.event = BTA_DM_API_LOCAL_PRIVACY_EVT;
1134  p_msg->privacy_enable = privacy_enable;
1135
1136  bta_sys_sendmsg(p_msg);
1137#else
1138  UNUSED(privacy_enable);
1139#endif
1140}
1141
1142/*******************************************************************************
1143 *
1144 * Function         BTA_DmBleCfgFilterCondition
1145 *
1146 * Description      This function is called to configure the adv data payload
1147 *                  filter condition.
1148 *
1149 * Parameters       action: to read/write/clear
1150 *                  cond_type: filter condition type
1151 *                  filt_index - Filter index
1152 *                  p_cond: filter condition parameter
1153 *                  p_cmpl_back - Command completed callback
1154 *                  ref_value - Reference value
1155 *
1156 * Returns          void
1157 *
1158 ******************************************************************************/
1159void BTA_DmBleCfgFilterCondition(tBTA_DM_BLE_SCAN_COND_OP action,
1160                                 tBTA_DM_BLE_PF_COND_TYPE cond_type,
1161                                 tBTA_DM_BLE_PF_FILT_INDEX filt_index,
1162                                 tBTA_DM_BLE_PF_COND_PARAM* p_cond,
1163                                 tBTA_DM_BLE_PF_CFG_CBACK* p_cmpl_cback,
1164                                 tBTA_DM_BLE_REF_VALUE ref_value) {
1165  APPL_TRACE_API("BTA_DmBleCfgFilterCondition: %d, %d", action, cond_type);
1166
1167  if (!p_cond) {
1168    do_in_bta_thread(FROM_HERE,
1169                     base::Bind(&bta_dm_cfg_filter_cond, action, cond_type,
1170                                filt_index, nullptr, p_cmpl_cback, ref_value));
1171  }
1172
1173  uint16_t len = sizeof(tBTA_DM_BLE_PF_COND_PARAM);
1174  uint8_t* p;
1175
1176  switch (cond_type) {
1177    case BTA_DM_BLE_PF_SRVC_DATA_PATTERN:
1178    case BTA_DM_BLE_PF_MANU_DATA:
1179      /* Length of pattern and pattern mask and other elements in */
1180      /* tBTA_DM_BLE_PF_MANU_COND */
1181      len += ((p_cond->manu_data.data_len) * 2) + sizeof(uint16_t) +
1182             sizeof(uint16_t) + sizeof(uint8_t);
1183      break;
1184
1185    case BTA_DM_BLE_PF_LOCAL_NAME:
1186      len += ((p_cond->local_name.data_len) + sizeof(uint8_t));
1187      break;
1188
1189    case BTM_BLE_PF_SRVC_UUID:
1190    case BTM_BLE_PF_SRVC_SOL_UUID:
1191      len += sizeof(tBLE_BD_ADDR) + sizeof(tBTA_DM_BLE_PF_COND_MASK);
1192      break;
1193
1194    default:
1195      break;
1196  }
1197
1198  // base::Owned will free it
1199  tBTA_DM_BLE_PF_COND_PARAM* p_cond_param = new tBTA_DM_BLE_PF_COND_PARAM;
1200  memcpy(p_cond_param, p_cond, sizeof(tBTA_DM_BLE_PF_COND_PARAM));
1201
1202  p = p_cond_param->additional_data;
1203
1204  if (cond_type == BTA_DM_BLE_PF_SRVC_DATA_PATTERN ||
1205      cond_type == BTA_DM_BLE_PF_MANU_DATA) {
1206    p += sizeof(tBTA_DM_BLE_PF_MANU_COND);
1207    p_cond_param->manu_data.p_pattern = p;
1208    p_cond_param->manu_data.data_len = p_cond->manu_data.data_len;
1209    memcpy(p_cond_param->manu_data.p_pattern, p_cond->manu_data.p_pattern,
1210           p_cond->manu_data.data_len);
1211    p += p_cond->manu_data.data_len;
1212
1213    if (cond_type == BTA_DM_BLE_PF_MANU_DATA) {
1214      p_cond_param->manu_data.company_id_mask =
1215          p_cond->manu_data.company_id_mask;
1216      if (p_cond->manu_data.p_pattern_mask != NULL) {
1217        p_cond_param->manu_data.p_pattern_mask = p;
1218        memcpy(p_cond_param->manu_data.p_pattern_mask,
1219               p_cond->manu_data.p_pattern_mask, p_cond->manu_data.data_len);
1220      }
1221    }
1222  } else if (cond_type == BTA_DM_BLE_PF_LOCAL_NAME) {
1223    p += sizeof(tBTA_DM_BLE_PF_LOCAL_NAME_COND);
1224    p_cond_param->local_name.p_data = p;
1225    p_cond_param->local_name.data_len = p_cond->local_name.data_len;
1226    memcpy(p_cond_param->local_name.p_data, p_cond->local_name.p_data,
1227           p_cond->local_name.data_len);
1228  } else if (cond_type == BTM_BLE_PF_SRVC_UUID ||
1229             cond_type == BTM_BLE_PF_SRVC_SOL_UUID) {
1230    p += sizeof(tBTA_DM_BLE_PF_SRVC_PATTERN_COND);
1231    if (p_cond->srvc_uuid.p_target_addr != NULL) {
1232      p_cond_param->srvc_uuid.p_target_addr = (tBLE_BD_ADDR*)(p);
1233      p_cond_param->srvc_uuid.p_target_addr->type =
1234          p_cond->srvc_uuid.p_target_addr->type;
1235      memcpy(p_cond_param->srvc_uuid.p_target_addr->bda,
1236             p_cond->srvc_uuid.p_target_addr->bda, BD_ADDR_LEN);
1237      p = (uint8_t*)(p_cond_param->srvc_uuid.p_target_addr + 1);
1238    }
1239    if (p_cond->srvc_uuid.p_uuid_mask) {
1240      p_cond_param->srvc_uuid.p_uuid_mask = (tBTA_DM_BLE_PF_COND_MASK*)p;
1241      memcpy(p_cond_param->srvc_uuid.p_uuid_mask, p_cond->srvc_uuid.p_uuid_mask,
1242             sizeof(tBTA_DM_BLE_PF_COND_MASK));
1243    }
1244  }
1245
1246  do_in_bta_thread(
1247      FROM_HERE,
1248      base::Bind(&bta_dm_cfg_filter_cond, action, cond_type, filt_index,
1249                 base::Owned(p_cond_param), p_cmpl_cback, ref_value));
1250}
1251
1252void BTA_DmBleScanFilterClear(tBTA_DM_BLE_REF_VALUE ref_value,
1253                              tBTA_DM_BLE_PF_FILT_INDEX filt_index,
1254                              tBTA_DM_BLE_PF_CFG_CBACK* p_cmpl_cback) {
1255  do_in_bta_thread(FROM_HERE, base::Bind(&bta_dm_scan_filter_clear, ref_value,
1256                                         filt_index, p_cmpl_cback));
1257}
1258
1259/*******************************************************************************
1260 *
1261 * Function         BTA_DmBleScanFilterSetup
1262 *
1263 * Description      This function is called to setup the adv data payload filter
1264 *                  param
1265 *
1266 * Parameters       p_target: enable the filter condition on a target device; if
1267 *                            NULL
1268 *                  filt_index - Filter index
1269 *                  p_filt_params -Filter parameters
1270 *                  ref_value - Reference value
1271 *                  action - Add, delete or clear
1272 *                  p_cmpl_back - Command completed callback
1273 *
1274 * Returns          void
1275 *
1276 ******************************************************************************/
1277void BTA_DmBleScanFilterSetup(
1278    uint8_t action, tBTA_DM_BLE_PF_FILT_INDEX filt_index,
1279    std::unique_ptr<btgatt_filt_param_setup_t> p_filt_params,
1280    std::unique_ptr<tBLE_BD_ADDR> p_target,
1281    tBTA_DM_BLE_PF_PARAM_CBACK p_cmpl_cback, tBTA_DM_BLE_REF_VALUE ref_value) {
1282  APPL_TRACE_API("%s: %d", __func__, action);
1283  do_in_bta_thread(
1284      FROM_HERE, base::Bind(&bta_dm_scan_filter_param_setup, action, filt_index,
1285                            base::Passed(&p_filt_params),
1286                            base::Passed(&p_target), p_cmpl_cback, ref_value));
1287}
1288
1289/*******************************************************************************
1290 *
1291 * Function         BTA_DmBleGetEnergyInfo
1292 *
1293 * Description      This function is called to obtain the energy info
1294 *
1295 * Parameters       p_cmpl_cback - Command complete callback
1296 *
1297 * Returns          void
1298 *
1299 ******************************************************************************/
1300void BTA_DmBleGetEnergyInfo(tBTA_BLE_ENERGY_INFO_CBACK* p_cmpl_cback) {
1301  const size_t len = sizeof(tBTA_DM_API_ENERGY_INFO) + sizeof(tBLE_BD_ADDR);
1302  tBTA_DM_API_ENERGY_INFO* p_msg = (tBTA_DM_API_ENERGY_INFO*)osi_calloc(len);
1303
1304  APPL_TRACE_API("%s", __func__);
1305
1306  p_msg->hdr.event = BTA_DM_API_BLE_ENERGY_INFO_EVT;
1307  p_msg->p_energy_info_cback = p_cmpl_cback;
1308
1309  bta_sys_sendmsg(p_msg);
1310}
1311
1312/*******************************************************************************
1313 *
1314 * Function         BTA_DmEnableScanFilter
1315 *
1316 * Description      This function is called to enable the adv data payload
1317 *                  filter
1318 *
1319 * Parameters       action - enable or disable the APCF feature
1320 *                  p_cmpl_cback - Command completed callback
1321 *                  ref_value - Reference value
1322 *
1323 * Returns          void
1324 *
1325 ******************************************************************************/
1326void BTA_DmEnableScanFilter(uint8_t action,
1327                            tBTA_DM_BLE_PF_STATUS_CBACK* p_cmpl_cback,
1328                            tBTA_DM_BLE_REF_VALUE ref_value) {
1329  APPL_TRACE_API("%s: %d", __func__, action);
1330  do_in_bta_thread(FROM_HERE, base::Bind(&bta_dm_enable_scan_filter, action,
1331                                         p_cmpl_cback, ref_value));
1332}
1333
1334/*******************************************************************************
1335 *
1336 * Function         BTA_DmBleUpdateConnectionParams
1337 *
1338 * Description      Update connection parameters, can only be used when
1339 *                  connection is up.
1340 *
1341 * Parameters:      bd_addr   - BD address of the peer
1342 *                  min_int   -     minimum connection interval,
1343 *                                  [0x0004 ~ 0x4000]
1344 *                  max_int   -     maximum connection interval,
1345 *                                  [0x0004 ~ 0x4000]
1346 *                  latency   -     slave latency [0 ~ 500]
1347 *                  timeout   -     supervision timeout [0x000a ~ 0xc80]
1348 *
1349 * Returns          void
1350 *
1351 ******************************************************************************/
1352void BTA_DmBleUpdateConnectionParams(const BD_ADDR bd_addr, uint16_t min_int,
1353                                     uint16_t max_int, uint16_t latency,
1354                                     uint16_t timeout) {
1355  tBTA_DM_API_UPDATE_CONN_PARAM* p_msg =
1356      (tBTA_DM_API_UPDATE_CONN_PARAM*)osi_calloc(
1357          sizeof(tBTA_DM_API_UPDATE_CONN_PARAM));
1358
1359  p_msg->hdr.event = BTA_DM_API_UPDATE_CONN_PARAM_EVT;
1360  bdcpy(p_msg->bd_addr, bd_addr);
1361  p_msg->min_int = min_int;
1362  p_msg->max_int = max_int;
1363  p_msg->latency = latency;
1364  p_msg->timeout = timeout;
1365
1366  bta_sys_sendmsg(p_msg);
1367}
1368
1369/*******************************************************************************
1370 *
1371 * Function         BTA_DmBleSetDataLength
1372 *
1373 * Description      This function is to set maximum LE data packet size
1374 *
1375 * Returns          void
1376 *
1377 *
1378 ******************************************************************************/
1379void BTA_DmBleSetDataLength(BD_ADDR remote_device, uint16_t tx_data_length) {
1380  tBTA_DM_API_BLE_SET_DATA_LENGTH* p_msg =
1381      (tBTA_DM_API_BLE_SET_DATA_LENGTH*)osi_malloc(
1382          sizeof(tBTA_DM_API_BLE_SET_DATA_LENGTH));
1383
1384  bdcpy(p_msg->remote_bda, remote_device);
1385  p_msg->hdr.event = BTA_DM_API_SET_DATA_LENGTH_EVT;
1386  p_msg->tx_data_length = tx_data_length;
1387
1388  bta_sys_sendmsg(p_msg);
1389}
1390
1391/*******************************************************************************
1392 *
1393 * Function         BTA_DmSetEncryption
1394 *
1395 * Description      This function is called to ensure that connection is
1396 *                  encrypted.  Should be called only on an open connection.
1397 *                  Typically only needed for connections that first want to
1398 *                  bring up unencrypted links, then later encrypt them.
1399 *
1400 * Parameters:      bd_addr       - Address of the peer device
1401 *                  transport     - transport of the link to be encruypted
1402 *                  p_callback    - Pointer to callback function to indicat the
1403 *                                  link encryption status
1404 *                  sec_act       - This is the security action to indicate
1405 *                                  what kind of BLE security level is required
1406 *                                  for the BLE link if BLE is supported.
1407 *                                  Note: This parameter is ignored for the
1408 *                                        BR/EDR or if BLE is not supported.
1409 *
1410 * Returns          void
1411 *
1412 ******************************************************************************/
1413void BTA_DmSetEncryption(BD_ADDR bd_addr, tBTA_TRANSPORT transport,
1414                         tBTA_DM_ENCRYPT_CBACK* p_callback,
1415                         tBTA_DM_BLE_SEC_ACT sec_act) {
1416  tBTA_DM_API_SET_ENCRYPTION* p_msg = (tBTA_DM_API_SET_ENCRYPTION*)osi_calloc(
1417      sizeof(tBTA_DM_API_SET_ENCRYPTION));
1418
1419  APPL_TRACE_API("%s", __func__);
1420
1421  p_msg->hdr.event = BTA_DM_API_SET_ENCRYPTION_EVT;
1422  memcpy(p_msg->bd_addr, bd_addr, BD_ADDR_LEN);
1423  p_msg->transport = transport;
1424  p_msg->p_callback = p_callback;
1425  p_msg->sec_act = sec_act;
1426
1427  bta_sys_sendmsg(p_msg);
1428}
1429
1430/*******************************************************************************
1431 *
1432 * Function         BTA_DmCloseACL
1433 *
1434 * Description      This function force to close an ACL connection and remove
1435 *                  the device from the security database list of known devices.
1436 *
1437 * Parameters:      bd_addr       - Address of the peer device
1438 *                  remove_dev    - remove device or not after link down
1439 *
1440 * Returns          void
1441 *
1442 ******************************************************************************/
1443void BTA_DmCloseACL(BD_ADDR bd_addr, bool remove_dev,
1444                    tBTA_TRANSPORT transport) {
1445  tBTA_DM_API_REMOVE_ACL* p_msg =
1446      (tBTA_DM_API_REMOVE_ACL*)osi_calloc(sizeof(tBTA_DM_API_REMOVE_ACL));
1447
1448  APPL_TRACE_API("%s", __func__);
1449
1450  p_msg->hdr.event = BTA_DM_API_REMOVE_ACL_EVT;
1451  memcpy(p_msg->bd_addr, bd_addr, BD_ADDR_LEN);
1452  p_msg->remove_dev = remove_dev;
1453  p_msg->transport = transport;
1454
1455  bta_sys_sendmsg(p_msg);
1456}
1457
1458/*******************************************************************************
1459 *
1460 * Function         BTA_DmBleObserve
1461 *
1462 * Description      This procedure keep the device listening for advertising
1463 *                  events from a broadcast device.
1464 *
1465 * Parameters       start: start or stop observe.
1466 *
1467 * Returns          void
1468
1469 *
1470 * Returns          void.
1471 *
1472 ******************************************************************************/
1473extern void BTA_DmBleObserve(bool start, uint8_t duration,
1474                             tBTA_DM_SEARCH_CBACK* p_results_cb) {
1475  tBTA_DM_API_BLE_OBSERVE* p_msg =
1476      (tBTA_DM_API_BLE_OBSERVE*)osi_calloc(sizeof(tBTA_DM_API_BLE_OBSERVE));
1477
1478  APPL_TRACE_API("%s:start = %d ", __func__, start);
1479
1480  p_msg->hdr.event = BTA_DM_API_BLE_OBSERVE_EVT;
1481  p_msg->start = start;
1482  p_msg->duration = duration;
1483  p_msg->p_cback = p_results_cb;
1484
1485  bta_sys_sendmsg(p_msg);
1486}
1487
1488/*******************************************************************************
1489 *
1490 * Function         BTA_VendorInit
1491 *
1492 * Description      This function initializes vendor specific
1493 *
1494 * Returns          void
1495 *
1496 ******************************************************************************/
1497void BTA_VendorInit(void) { APPL_TRACE_API("BTA_VendorInit"); }
1498
1499/*******************************************************************************
1500 *
1501 * Function         BTA_VendorCleanup
1502 *
1503 * Description      This function frees up Broadcom specific VS specific dynamic
1504 *                  memory
1505 *
1506 * Returns          void
1507 *
1508 ******************************************************************************/
1509void BTA_VendorCleanup(void) {
1510  tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
1511  BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
1512
1513  if (cmn_ble_vsc_cb.max_filter > 0) {
1514    btm_ble_adv_filter_cleanup();
1515#if (BLE_PRIVACY_SPT == TRUE)
1516    btm_ble_resolving_list_cleanup();
1517#endif
1518  }
1519
1520  if (cmn_ble_vsc_cb.tot_scan_results_strg > 0) btm_ble_batchscan_cleanup();
1521
1522  if (cmn_ble_vsc_cb.adv_inst_max > 0) btm_ble_multi_adv_cleanup();
1523}
1524