bta_dm_api.cc revision 0595ca0018ac9980d1dd7e192301f8494a01f9cb
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, size_t eir_len,
498                       tBTA_SERVICE_MASK* p_services) {
499  uint8_t xx, yy;
500  uint8_t num_uuid, max_num_uuid = 32;
501  uint8_t uuid_list[32 * LEN_UUID_16];
502  uint16_t* p_uuid16 = (uint16_t*)uuid_list;
503  tBTA_SERVICE_MASK mask;
504
505  BTM_GetEirUuidList(p_eir, eir_len, LEN_UUID_16, &num_uuid, uuid_list,
506                     max_num_uuid);
507  for (xx = 0; xx < num_uuid; xx++) {
508    mask = 1;
509    for (yy = 0; yy < BTA_MAX_SERVICE_ID; yy++) {
510      if (*(p_uuid16 + xx) == bta_service_id_to_uuid_lkup_tbl[yy]) {
511        *p_services |= mask;
512        break;
513      }
514      mask <<= 1;
515    }
516
517    /* for HSP v1.2 only device */
518    if (*(p_uuid16 + xx) == UUID_SERVCLASS_HEADSET_HS)
519      *p_services |= BTA_HSP_SERVICE_MASK;
520
521    if (*(p_uuid16 + xx) == UUID_SERVCLASS_HDP_SOURCE)
522      *p_services |= BTA_HL_SERVICE_MASK;
523
524    if (*(p_uuid16 + xx) == UUID_SERVCLASS_HDP_SINK)
525      *p_services |= BTA_HL_SERVICE_MASK;
526  }
527}
528
529/*******************************************************************************
530 *
531 * Function         BTA_DmGetConnectionState
532 *
533 * Description      Returns whether the remote device is currently connected.
534 *
535 * Returns          0 if the device is NOT connected.
536 *
537 ******************************************************************************/
538uint16_t BTA_DmGetConnectionState(const BD_ADDR bd_addr) {
539  tBTA_DM_PEER_DEVICE* p_dev = bta_dm_find_peer_device(bd_addr);
540  return (p_dev && p_dev->conn_state == BTA_DM_CONNECTED);
541}
542
543/*******************************************************************************
544 *                   Device Identification (DI) Server Functions
545 ******************************************************************************/
546/*******************************************************************************
547 *
548 * Function         BTA_DmSetLocalDiRecord
549 *
550 * Description      This function adds a DI record to the local SDP database.
551 *
552 * Returns          BTA_SUCCESS if record set sucessfully, otherwise error code.
553 *
554 ******************************************************************************/
555tBTA_STATUS BTA_DmSetLocalDiRecord(tBTA_DI_RECORD* p_device_info,
556                                   uint32_t* p_handle) {
557  tBTA_STATUS status = BTA_FAILURE;
558
559  if (bta_dm_di_cb.di_num < BTA_DI_NUM_MAX) {
560    if (SDP_SetLocalDiRecord((tSDP_DI_RECORD*)p_device_info, p_handle) ==
561        SDP_SUCCESS) {
562      if (!p_device_info->primary_record) {
563        bta_dm_di_cb.di_handle[bta_dm_di_cb.di_num] = *p_handle;
564        bta_dm_di_cb.di_num++;
565      }
566
567      bta_sys_add_uuid(UUID_SERVCLASS_PNP_INFORMATION);
568      status = BTA_SUCCESS;
569    }
570  }
571
572  return status;
573}
574
575/*******************************************************************************
576 *
577 * Function         bta_dmexecutecallback
578 *
579 * Description      This function will request BTA to execute a call back in the
580 *                  context of BTU task.
581 *                  This API was named in lower case because it is only intended
582 *                  for the internal customers(like BTIF).
583 *
584 * Returns          void
585 *
586 ******************************************************************************/
587void bta_dmexecutecallback(tBTA_DM_EXEC_CBACK* p_callback, void* p_param) {
588  tBTA_DM_API_EXECUTE_CBACK* p_msg =
589      (tBTA_DM_API_EXECUTE_CBACK*)osi_malloc(sizeof(tBTA_DM_MSG));
590
591  p_msg->hdr.event = BTA_DM_API_EXECUTE_CBACK_EVT;
592  p_msg->p_param = p_param;
593  p_msg->p_exec_cback = p_callback;
594
595  bta_sys_sendmsg(p_msg);
596}
597
598/*******************************************************************************
599 *
600 * Function         BTA_DmAddBleKey
601 *
602 * Description      Add/modify LE device information.  This function will be
603 *                  normally called during host startup to restore all required
604 *                  information stored in the NVRAM.
605 *
606 * Parameters:      bd_addr          - BD address of the peer
607 *                  p_le_key         - LE key values.
608 *                  key_type         - LE SMP key type.
609 *
610 * Returns          BTA_SUCCESS if successful
611 *                  BTA_FAIL if operation failed.
612 *
613 ******************************************************************************/
614void BTA_DmAddBleKey(BD_ADDR bd_addr, tBTA_LE_KEY_VALUE* p_le_key,
615                     tBTA_LE_KEY_TYPE key_type) {
616  tBTA_DM_API_ADD_BLEKEY* p_msg =
617      (tBTA_DM_API_ADD_BLEKEY*)osi_calloc(sizeof(tBTA_DM_API_ADD_BLEKEY));
618
619  p_msg->hdr.event = BTA_DM_API_ADD_BLEKEY_EVT;
620  p_msg->key_type = key_type;
621  bdcpy(p_msg->bd_addr, bd_addr);
622  memcpy(&p_msg->blekey, p_le_key, sizeof(tBTA_LE_KEY_VALUE));
623
624  bta_sys_sendmsg(p_msg);
625}
626
627/*******************************************************************************
628 *
629 * Function         BTA_DmAddBleDevice
630 *
631 * Description      Add a BLE device.  This function will be normally called
632 *                  during host startup to restore all required information
633 *                  for a LE device stored in the NVRAM.
634 *
635 * Parameters:      bd_addr          - BD address of the peer
636 *                  dev_type         - Remote device's device type.
637 *                  addr_type        - LE device address type.
638 *
639 * Returns          void
640 *
641 ******************************************************************************/
642void BTA_DmAddBleDevice(BD_ADDR bd_addr, tBLE_ADDR_TYPE addr_type,
643                        tBT_DEVICE_TYPE dev_type) {
644  tBTA_DM_API_ADD_BLE_DEVICE* p_msg = (tBTA_DM_API_ADD_BLE_DEVICE*)osi_calloc(
645      sizeof(tBTA_DM_API_ADD_BLE_DEVICE));
646
647  p_msg->hdr.event = BTA_DM_API_ADD_BLEDEVICE_EVT;
648  bdcpy(p_msg->bd_addr, bd_addr);
649  p_msg->addr_type = addr_type;
650  p_msg->dev_type = dev_type;
651
652  bta_sys_sendmsg(p_msg);
653}
654
655/*******************************************************************************
656 *
657 * Function         BTA_DmBlePasskeyReply
658 *
659 * Description      Send BLE SMP passkey reply.
660 *
661 * Parameters:      bd_addr          - BD address of the peer
662 *                  accept           - passkey entry sucessful or declined.
663 *                  passkey          - passkey value, must be a 6 digit number,
664 *                                     can be lead by 0.
665 *
666 * Returns          void
667 *
668 ******************************************************************************/
669void BTA_DmBlePasskeyReply(BD_ADDR bd_addr, bool accept, uint32_t passkey) {
670  tBTA_DM_API_PASSKEY_REPLY* p_msg =
671      (tBTA_DM_API_PASSKEY_REPLY*)osi_calloc(sizeof(tBTA_DM_API_PASSKEY_REPLY));
672
673  p_msg->hdr.event = BTA_DM_API_BLE_PASSKEY_REPLY_EVT;
674  bdcpy(p_msg->bd_addr, bd_addr);
675  p_msg->accept = accept;
676
677  if (accept) p_msg->passkey = passkey;
678
679  bta_sys_sendmsg(p_msg);
680}
681
682/*******************************************************************************
683 *
684 * Function         BTA_DmBleConfirmReply
685 *
686 * Description      Send BLE SMP SC user confirmation reply.
687 *
688 * Parameters:      bd_addr          - BD address of the peer
689 *                  accept           - numbers to compare are the same or
690 *                                     different.
691 *
692 * Returns          void
693 *
694 ******************************************************************************/
695void BTA_DmBleConfirmReply(BD_ADDR bd_addr, bool accept) {
696  tBTA_DM_API_CONFIRM* p_msg =
697      (tBTA_DM_API_CONFIRM*)osi_calloc(sizeof(tBTA_DM_API_CONFIRM));
698
699  p_msg->hdr.event = BTA_DM_API_BLE_CONFIRM_REPLY_EVT;
700  bdcpy(p_msg->bd_addr, bd_addr);
701  p_msg->accept = accept;
702
703  bta_sys_sendmsg(p_msg);
704}
705
706/*******************************************************************************
707 *
708 * Function         BTA_DmBleSecurityGrant
709 *
710 * Description      Grant security request access.
711 *
712 * Parameters:      bd_addr          - BD address of the peer
713 *                  res              - security grant status.
714 *
715 * Returns          void
716 *
717 ******************************************************************************/
718void BTA_DmBleSecurityGrant(BD_ADDR bd_addr, tBTA_DM_BLE_SEC_GRANT res) {
719  tBTA_DM_API_BLE_SEC_GRANT* p_msg =
720      (tBTA_DM_API_BLE_SEC_GRANT*)osi_calloc(sizeof(tBTA_DM_API_BLE_SEC_GRANT));
721
722  p_msg->hdr.event = BTA_DM_API_BLE_SEC_GRANT_EVT;
723  bdcpy(p_msg->bd_addr, bd_addr);
724  p_msg->res = res;
725
726  bta_sys_sendmsg(p_msg);
727}
728
729/*******************************************************************************
730 *
731 * Function         BTA_DmSetBlePrefConnParams
732 *
733 * Description      This function is called to set the preferred connection
734 *                  parameters when default connection parameter is not desired.
735 *
736 * Parameters:      bd_addr          - BD address of the peripheral
737 *                  scan_interval    - scan interval
738 *                  scan_window      - scan window
739 *                  min_conn_int     - minimum preferred connection interval
740 *                  max_conn_int     - maximum preferred connection interval
741 *                  slave_latency    - preferred slave latency
742 *                  supervision_tout - preferred supervision timeout
743 *
744 *
745 * Returns          void
746 *
747 ******************************************************************************/
748void BTA_DmSetBlePrefConnParams(const BD_ADDR bd_addr, uint16_t min_conn_int,
749                                uint16_t max_conn_int, uint16_t slave_latency,
750                                uint16_t supervision_tout) {
751  tBTA_DM_API_BLE_CONN_PARAMS* p_msg = (tBTA_DM_API_BLE_CONN_PARAMS*)osi_calloc(
752      sizeof(tBTA_DM_API_BLE_CONN_PARAMS));
753
754  p_msg->hdr.event = BTA_DM_API_BLE_CONN_PARAM_EVT;
755  memcpy(p_msg->peer_bda, bd_addr, BD_ADDR_LEN);
756  p_msg->conn_int_max = max_conn_int;
757  p_msg->conn_int_min = min_conn_int;
758  p_msg->slave_latency = slave_latency;
759  p_msg->supervision_tout = supervision_tout;
760
761  bta_sys_sendmsg(p_msg);
762}
763
764/*******************************************************************************
765 *
766 * Function         BTA_DmSetBleConnScanParams
767 *
768 * Description      This function is called to set scan parameters used in
769 *                  BLE connection request
770 *
771 * Parameters:      scan_interval    - scan interval
772 *                  scan_window      - scan window
773 *
774 * Returns          void
775 *
776 ******************************************************************************/
777void BTA_DmSetBleConnScanParams(uint32_t scan_interval, uint32_t scan_window) {
778  tBTA_DM_API_BLE_SCAN_PARAMS* p_msg = (tBTA_DM_API_BLE_SCAN_PARAMS*)osi_calloc(
779      sizeof(tBTA_DM_API_BLE_SCAN_PARAMS));
780
781  p_msg->hdr.event = BTA_DM_API_BLE_CONN_SCAN_PARAM_EVT;
782  p_msg->scan_int = scan_interval;
783  p_msg->scan_window = scan_window;
784
785  bta_sys_sendmsg(p_msg);
786}
787
788/**
789 * Set BLE connectable mode to auto connect
790 */
791void BTA_DmBleStartAutoConn() {
792  tBTA_DM_API_SET_NAME* p_msg =
793      (tBTA_DM_API_SET_NAME*)osi_calloc(sizeof(tBTA_DM_API_SET_NAME));
794
795  p_msg->hdr.event = BTA_DM_API_BLE_SET_BG_CONN_TYPE;
796  bta_sys_sendmsg(p_msg);
797}
798
799/*******************************************************************************
800 *
801 * Function         bta_dm_discover_send_msg
802 *
803 * Description      This function send discover message to BTA task.
804 *
805 * Returns          void
806 *
807 ******************************************************************************/
808static void bta_dm_discover_send_msg(BD_ADDR bd_addr,
809                                     tBTA_SERVICE_MASK_EXT* p_services,
810                                     tBTA_DM_SEARCH_CBACK* p_cback,
811                                     bool sdp_search,
812                                     tBTA_TRANSPORT transport) {
813  const size_t len = p_services ? (sizeof(tBTA_DM_API_DISCOVER) +
814                                   sizeof(tBT_UUID) * p_services->num_uuid)
815                                : sizeof(tBTA_DM_API_DISCOVER);
816  tBTA_DM_API_DISCOVER* p_msg = (tBTA_DM_API_DISCOVER*)osi_calloc(len);
817
818  p_msg->hdr.event = BTA_DM_API_DISCOVER_EVT;
819  bdcpy(p_msg->bd_addr, bd_addr);
820  p_msg->p_cback = p_cback;
821  p_msg->sdp_search = sdp_search;
822  p_msg->transport = transport;
823
824  if (p_services != NULL) {
825    p_msg->services = p_services->srvc_mask;
826    p_msg->num_uuid = p_services->num_uuid;
827    if (p_services->num_uuid != 0) {
828      p_msg->p_uuid = (tBT_UUID*)(p_msg + 1);
829      memcpy(p_msg->p_uuid, p_services->p_uuid,
830             sizeof(tBT_UUID) * p_services->num_uuid);
831    }
832  }
833
834  bta_sys_sendmsg(p_msg);
835}
836
837/*******************************************************************************
838 *
839 * Function         BTA_DmDiscoverByTransport
840 *
841 * Description      This function does service discovery on particular transport
842 *                  for services of a
843 *                  peer device. When services.num_uuid is 0, it indicates all
844 *                  GATT based services are to be searched; otherwise a list of
845 *                  UUID of interested services should be provided through
846 *                  p_services->p_uuid.
847 *
848 *
849 *
850 * Returns          void
851 *
852 ******************************************************************************/
853void BTA_DmDiscoverByTransport(BD_ADDR bd_addr,
854                               tBTA_SERVICE_MASK_EXT* p_services,
855                               tBTA_DM_SEARCH_CBACK* p_cback, bool sdp_search,
856                               tBTA_TRANSPORT transport) {
857  bta_dm_discover_send_msg(bd_addr, p_services, p_cback, sdp_search, transport);
858}
859
860/*******************************************************************************
861 *
862 * Function         BTA_DmDiscoverExt
863 *
864 * Description      This function does service discovery for services of a
865 *                  peer device. When services.num_uuid is 0, it indicates all
866 *                  GATT based services are to be searched; other wise a list of
867 *                  UUID of interested services should be provided through
868 *                  p_services->p_uuid.
869 *
870 *
871 *
872 * Returns          void
873 *
874 ******************************************************************************/
875void BTA_DmDiscoverExt(BD_ADDR bd_addr, tBTA_SERVICE_MASK_EXT* p_services,
876                       tBTA_DM_SEARCH_CBACK* p_cback, bool sdp_search) {
877  bta_dm_discover_send_msg(bd_addr, p_services, p_cback, sdp_search,
878                           BTA_TRANSPORT_UNKNOWN);
879}
880
881/*******************************************************************************
882 *
883 * Function         BTA_DmSearchExt
884 *
885 * Description      This function searches for peer Bluetooth devices. It
886 *                  performs an inquiry and gets the remote name for devices.
887 *                  Service discovery is done if services is non zero
888 *
889 * Parameters       p_dm_inq: inquiry conditions
890 *                  p_services: if service is not empty, service discovery will
891 *                              be done. For all GATT based service conditions,
892 *                              put num_uuid, and p_uuid is the pointer to the
893 *                              list of UUID values.
894 *                  p_cback: callback function when search is completed.
895 *
896 *
897 *
898 * Returns          void
899 *
900 ******************************************************************************/
901void BTA_DmSearchExt(tBTA_DM_INQ* p_dm_inq, tBTA_SERVICE_MASK_EXT* p_services,
902                     tBTA_DM_SEARCH_CBACK* p_cback) {
903  const size_t len = p_services ? (sizeof(tBTA_DM_API_SEARCH) +
904                                   sizeof(tBT_UUID) * p_services->num_uuid)
905                                : sizeof(tBTA_DM_API_SEARCH);
906  tBTA_DM_API_SEARCH* p_msg = (tBTA_DM_API_SEARCH*)osi_calloc(len);
907
908  p_msg->hdr.event = BTA_DM_API_SEARCH_EVT;
909  memcpy(&p_msg->inq_params, p_dm_inq, sizeof(tBTA_DM_INQ));
910  p_msg->p_cback = p_cback;
911  p_msg->rs_res = BTA_DM_RS_NONE;
912
913  if (p_services != NULL) {
914    p_msg->services = p_services->srvc_mask;
915    p_msg->num_uuid = p_services->num_uuid;
916
917    if (p_services->num_uuid != 0) {
918      p_msg->p_uuid = (tBT_UUID*)(p_msg + 1);
919      memcpy(p_msg->p_uuid, p_services->p_uuid,
920             sizeof(tBT_UUID) * p_services->num_uuid);
921    } else {
922      p_msg->p_uuid = NULL;
923    }
924  }
925
926  bta_sys_sendmsg(p_msg);
927}
928
929/*******************************************************************************
930 *
931 * Function         BTA_DmBleUpdateConnectionParam
932 *
933 * Description      Update connection parameters, can only be used when
934 *                  connection is up.
935 *
936 * Parameters:      bd_addr          - BD address of the peer
937 *                  min_int   -     minimum connection interval,
938 *                                  [0x0004 ~ 0x4000]
939 *                  max_int   -     maximum connection interval,
940 *                                  [0x0004 ~ 0x4000]
941 *                  latency   -     slave latency [0 ~ 500]
942 *                  timeout   -     supervision timeout [0x000a ~ 0xc80]
943 *
944 * Returns          void
945 *
946 ******************************************************************************/
947void BTA_DmBleUpdateConnectionParam(BD_ADDR bd_addr, uint16_t min_int,
948                                    uint16_t max_int, uint16_t latency,
949                                    uint16_t timeout) {
950  tBTA_DM_API_UPDATE_CONN_PARAM* p_msg =
951      (tBTA_DM_API_UPDATE_CONN_PARAM*)osi_calloc(
952          sizeof(tBTA_DM_API_UPDATE_CONN_PARAM));
953
954  p_msg->hdr.event = BTA_DM_API_UPDATE_CONN_PARAM_EVT;
955  bdcpy(p_msg->bd_addr, bd_addr);
956  p_msg->min_int = min_int;
957  p_msg->max_int = max_int;
958  p_msg->latency = latency;
959  p_msg->timeout = timeout;
960
961  bta_sys_sendmsg(p_msg);
962}
963
964/*******************************************************************************
965 *
966 * Function         BTA_DmBleConfigLocalPrivacy
967 *
968 * Description      Enable/disable privacy on the local device
969 *
970 * Parameters:      privacy_enable   - enable/disabe privacy on remote device.
971 *
972 * Returns          void
973 *
974 ******************************************************************************/
975void BTA_DmBleConfigLocalPrivacy(bool privacy_enable) {
976#if (BLE_PRIVACY_SPT == TRUE)
977  tBTA_DM_API_LOCAL_PRIVACY* p_msg = (tBTA_DM_API_LOCAL_PRIVACY*)osi_calloc(
978      sizeof(tBTA_DM_API_ENABLE_PRIVACY));
979
980  p_msg->hdr.event = BTA_DM_API_LOCAL_PRIVACY_EVT;
981  p_msg->privacy_enable = privacy_enable;
982
983  bta_sys_sendmsg(p_msg);
984#else
985  UNUSED(privacy_enable);
986#endif
987}
988
989/*******************************************************************************
990 *
991 * Function         BTA_DmBleGetEnergyInfo
992 *
993 * Description      This function is called to obtain the energy info
994 *
995 * Parameters       p_cmpl_cback - Command complete callback
996 *
997 * Returns          void
998 *
999 ******************************************************************************/
1000void BTA_DmBleGetEnergyInfo(tBTA_BLE_ENERGY_INFO_CBACK* p_cmpl_cback) {
1001  const size_t len = sizeof(tBTA_DM_API_ENERGY_INFO) + sizeof(tBLE_BD_ADDR);
1002  tBTA_DM_API_ENERGY_INFO* p_msg = (tBTA_DM_API_ENERGY_INFO*)osi_calloc(len);
1003
1004  APPL_TRACE_API("%s", __func__);
1005
1006  p_msg->hdr.event = BTA_DM_API_BLE_ENERGY_INFO_EVT;
1007  p_msg->p_energy_info_cback = p_cmpl_cback;
1008
1009  bta_sys_sendmsg(p_msg);
1010}
1011
1012/*******************************************************************************
1013 *
1014 * Function         BTA_DmBleUpdateConnectionParams
1015 *
1016 * Description      Update connection parameters, can only be used when
1017 *                  connection is up.
1018 *
1019 * Parameters:      bd_addr   - BD address of the peer
1020 *                  min_int   -     minimum connection interval,
1021 *                                  [0x0004 ~ 0x4000]
1022 *                  max_int   -     maximum connection interval,
1023 *                                  [0x0004 ~ 0x4000]
1024 *                  latency   -     slave latency [0 ~ 500]
1025 *                  timeout   -     supervision timeout [0x000a ~ 0xc80]
1026 *
1027 * Returns          void
1028 *
1029 ******************************************************************************/
1030void BTA_DmBleUpdateConnectionParams(const BD_ADDR bd_addr, uint16_t min_int,
1031                                     uint16_t max_int, uint16_t latency,
1032                                     uint16_t timeout) {
1033  tBTA_DM_API_UPDATE_CONN_PARAM* p_msg =
1034      (tBTA_DM_API_UPDATE_CONN_PARAM*)osi_calloc(
1035          sizeof(tBTA_DM_API_UPDATE_CONN_PARAM));
1036
1037  p_msg->hdr.event = BTA_DM_API_UPDATE_CONN_PARAM_EVT;
1038  bdcpy(p_msg->bd_addr, bd_addr);
1039  p_msg->min_int = min_int;
1040  p_msg->max_int = max_int;
1041  p_msg->latency = latency;
1042  p_msg->timeout = timeout;
1043
1044  bta_sys_sendmsg(p_msg);
1045}
1046
1047/*******************************************************************************
1048 *
1049 * Function         BTA_DmBleSetDataLength
1050 *
1051 * Description      This function is to set maximum LE data packet size
1052 *
1053 * Returns          void
1054 *
1055 *
1056 ******************************************************************************/
1057void BTA_DmBleSetDataLength(BD_ADDR remote_device, uint16_t tx_data_length) {
1058  tBTA_DM_API_BLE_SET_DATA_LENGTH* p_msg =
1059      (tBTA_DM_API_BLE_SET_DATA_LENGTH*)osi_malloc(
1060          sizeof(tBTA_DM_API_BLE_SET_DATA_LENGTH));
1061
1062  bdcpy(p_msg->remote_bda, remote_device);
1063  p_msg->hdr.event = BTA_DM_API_SET_DATA_LENGTH_EVT;
1064  p_msg->tx_data_length = tx_data_length;
1065
1066  bta_sys_sendmsg(p_msg);
1067}
1068
1069/*******************************************************************************
1070 *
1071 * Function         BTA_DmSetEncryption
1072 *
1073 * Description      This function is called to ensure that connection is
1074 *                  encrypted.  Should be called only on an open connection.
1075 *                  Typically only needed for connections that first want to
1076 *                  bring up unencrypted links, then later encrypt them.
1077 *
1078 * Parameters:      bd_addr       - Address of the peer device
1079 *                  transport     - transport of the link to be encruypted
1080 *                  p_callback    - Pointer to callback function to indicat the
1081 *                                  link encryption status
1082 *                  sec_act       - This is the security action to indicate
1083 *                                  what kind of BLE security level is required
1084 *                                  for the BLE link if BLE is supported.
1085 *                                  Note: This parameter is ignored for the
1086 *                                        BR/EDR or if BLE is not supported.
1087 *
1088 * Returns          void
1089 *
1090 ******************************************************************************/
1091void BTA_DmSetEncryption(BD_ADDR bd_addr, tBTA_TRANSPORT transport,
1092                         tBTA_DM_ENCRYPT_CBACK* p_callback,
1093                         tBTA_DM_BLE_SEC_ACT sec_act) {
1094  tBTA_DM_API_SET_ENCRYPTION* p_msg = (tBTA_DM_API_SET_ENCRYPTION*)osi_calloc(
1095      sizeof(tBTA_DM_API_SET_ENCRYPTION));
1096
1097  APPL_TRACE_API("%s", __func__);
1098
1099  p_msg->hdr.event = BTA_DM_API_SET_ENCRYPTION_EVT;
1100  memcpy(p_msg->bd_addr, bd_addr, BD_ADDR_LEN);
1101  p_msg->transport = transport;
1102  p_msg->p_callback = p_callback;
1103  p_msg->sec_act = sec_act;
1104
1105  bta_sys_sendmsg(p_msg);
1106}
1107
1108/*******************************************************************************
1109 *
1110 * Function         BTA_DmCloseACL
1111 *
1112 * Description      This function force to close an ACL connection and remove
1113 *                  the device from the security database list of known devices.
1114 *
1115 * Parameters:      bd_addr       - Address of the peer device
1116 *                  remove_dev    - remove device or not after link down
1117 *
1118 * Returns          void
1119 *
1120 ******************************************************************************/
1121void BTA_DmCloseACL(BD_ADDR bd_addr, bool remove_dev,
1122                    tBTA_TRANSPORT transport) {
1123  tBTA_DM_API_REMOVE_ACL* p_msg =
1124      (tBTA_DM_API_REMOVE_ACL*)osi_calloc(sizeof(tBTA_DM_API_REMOVE_ACL));
1125
1126  APPL_TRACE_API("%s", __func__);
1127
1128  p_msg->hdr.event = BTA_DM_API_REMOVE_ACL_EVT;
1129  memcpy(p_msg->bd_addr, bd_addr, BD_ADDR_LEN);
1130  p_msg->remove_dev = remove_dev;
1131  p_msg->transport = transport;
1132
1133  bta_sys_sendmsg(p_msg);
1134}
1135
1136/*******************************************************************************
1137 *
1138 * Function         BTA_DmBleObserve
1139 *
1140 * Description      This procedure keep the device listening for advertising
1141 *                  events from a broadcast device.
1142 *
1143 * Parameters       start: start or stop observe.
1144 *
1145 * Returns          void
1146
1147 *
1148 * Returns          void.
1149 *
1150 ******************************************************************************/
1151extern void BTA_DmBleObserve(bool start, uint8_t duration,
1152                             tBTA_DM_SEARCH_CBACK* p_results_cb) {
1153  tBTA_DM_API_BLE_OBSERVE* p_msg =
1154      (tBTA_DM_API_BLE_OBSERVE*)osi_calloc(sizeof(tBTA_DM_API_BLE_OBSERVE));
1155
1156  APPL_TRACE_API("%s:start = %d ", __func__, start);
1157
1158  p_msg->hdr.event = BTA_DM_API_BLE_OBSERVE_EVT;
1159  p_msg->start = start;
1160  p_msg->duration = duration;
1161  p_msg->p_cback = p_results_cb;
1162
1163  bta_sys_sendmsg(p_msg);
1164}
1165
1166/*******************************************************************************
1167 *
1168 * Function         BTA_VendorInit
1169 *
1170 * Description      This function initializes vendor specific
1171 *
1172 * Returns          void
1173 *
1174 ******************************************************************************/
1175void BTA_VendorInit(void) { APPL_TRACE_API("BTA_VendorInit"); }
1176
1177/*******************************************************************************
1178 *
1179 * Function         BTA_VendorCleanup
1180 *
1181 * Description      This function frees up Broadcom specific VS specific dynamic
1182 *                  memory
1183 *
1184 * Returns          void
1185 *
1186 ******************************************************************************/
1187void BTA_VendorCleanup(void) {
1188  tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
1189  BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
1190
1191  if (cmn_ble_vsc_cb.max_filter > 0) {
1192    btm_ble_adv_filter_cleanup();
1193#if (BLE_PRIVACY_SPT == TRUE)
1194    btm_ble_resolving_list_cleanup();
1195#endif
1196  }
1197
1198  if (cmn_ble_vsc_cb.tot_scan_results_strg > 0) btm_ble_batchscan_cleanup();
1199
1200  if (cmn_ble_vsc_cb.adv_inst_max > 0) btm_ble_multi_adv_cleanup();
1201}
1202