OSDN Git Service

DO NOT MERGE Fix unexpected behavior in smp_sm_event
[android-x86/system-bt.git] / bta / dm / bta_dm_api.cc
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
42 static const tBTA_SYS_REG bta_dm_reg = {bta_dm_sm_execute, bta_dm_sm_disable};
43
44 static const tBTA_SYS_REG bta_dm_search_reg = {bta_dm_search_sm_execute,
45                                                bta_dm_search_sm_disable};
46
47 /*******************************************************************************
48  *
49  * Function         BTA_EnableBluetooth
50  *
51  * Description      Enables bluetooth service.  This function must be
52  *                  called before any other functions in the BTA API are called.
53  *
54  *
55  * Returns          tBTA_STATUS
56  *
57  ******************************************************************************/
58 tBTA_STATUS BTA_EnableBluetooth(tBTA_DM_SEC_CBACK* p_cback) {
59   /* Bluetooth disabling is in progress */
60   if (bta_dm_cb.disabling) return BTA_FAILURE;
61
62   bta_sys_register(BTA_ID_DM, &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  ******************************************************************************/
88 tBTA_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  ******************************************************************************/
108 tBTA_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  ******************************************************************************/
129 void 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  ******************************************************************************/
148 void 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  ******************************************************************************/
170 void 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  ******************************************************************************/
196 void 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  ******************************************************************************/
220 void 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  ******************************************************************************/
238 void BTA_DmDiscover(const RawAddress& 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   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  ******************************************************************************/
263 void BTA_DmDiscoverUUID(const RawAddress& 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   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  ******************************************************************************/
293 void BTA_DmBond(const RawAddress& 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   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  ******************************************************************************/
315 void BTA_DmBondByTransport(const RawAddress& bd_addr,
316                            tBTA_TRANSPORT transport) {
317   tBTA_DM_API_BOND* p_msg =
318       (tBTA_DM_API_BOND*)osi_malloc(sizeof(tBTA_DM_API_BOND));
319
320   p_msg->hdr.event = BTA_DM_API_BOND_EVT;
321   p_msg->bd_addr = bd_addr;
322   p_msg->transport = transport;
323
324   bta_sys_sendmsg(p_msg);
325 }
326
327 /*******************************************************************************
328  *
329  * Function         BTA_DmBondCancel
330  *
331  * Description      This function cancels the bonding procedure with a peer
332  *                  device
333  *
334  *
335  * Returns          void
336  *
337  ******************************************************************************/
338 void BTA_DmBondCancel(const RawAddress& bd_addr) {
339   tBTA_DM_API_BOND_CANCEL* p_msg =
340       (tBTA_DM_API_BOND_CANCEL*)osi_malloc(sizeof(tBTA_DM_API_BOND_CANCEL));
341
342   p_msg->hdr.event = BTA_DM_API_BOND_CANCEL_EVT;
343   p_msg->bd_addr = bd_addr;
344
345   bta_sys_sendmsg(p_msg);
346 }
347
348 /*******************************************************************************
349  *
350  * Function         BTA_DmPinReply
351  *
352  * Description      This function provides a pincode for a remote device when
353  *                  one is requested by DM through BTA_DM_PIN_REQ_EVT
354  *
355  *
356  * Returns          void
357  *
358  ******************************************************************************/
359 void BTA_DmPinReply(const RawAddress& bd_addr, bool accept, uint8_t pin_len,
360                     uint8_t* p_pin)
361
362 {
363   tBTA_DM_API_PIN_REPLY* p_msg =
364       (tBTA_DM_API_PIN_REPLY*)osi_malloc(sizeof(tBTA_DM_API_PIN_REPLY));
365
366   p_msg->hdr.event = BTA_DM_API_PIN_REPLY_EVT;
367   p_msg->bd_addr = bd_addr;
368   p_msg->accept = accept;
369   if (accept) {
370     p_msg->pin_len = pin_len;
371     memcpy(p_msg->p_pin, p_pin, pin_len);
372   }
373
374   bta_sys_sendmsg(p_msg);
375 }
376
377 /*******************************************************************************
378  *
379  * Function         BTA_DmLocalOob
380  *
381  * Description      This function retrieves the OOB data from local controller.
382  *                  The result is reported by:
383  *                  - bta_dm_co_loc_oob_ext() if device supports secure
384  *                    connections (SC)
385  *                  - bta_dm_co_loc_oob() if device doesn't support SC
386  *
387  * Returns          void
388  *
389  ******************************************************************************/
390 void BTA_DmLocalOob(void) {
391   tBTA_DM_API_LOC_OOB* p_msg =
392       (tBTA_DM_API_LOC_OOB*)osi_malloc(sizeof(tBTA_DM_API_LOC_OOB));
393
394   p_msg->hdr.event = BTA_DM_API_LOC_OOB_EVT;
395   bta_sys_sendmsg(p_msg);
396 }
397
398 /*******************************************************************************
399  *
400  * Function         BTA_DmConfirm
401  *
402  * Description      This function accepts or rejects the numerical value of the
403  *                  Simple Pairing process on BTA_DM_SP_CFM_REQ_EVT
404  *
405  * Returns          void
406  *
407  ******************************************************************************/
408 void BTA_DmConfirm(const RawAddress& bd_addr, bool accept) {
409   tBTA_DM_API_CONFIRM* p_msg =
410       (tBTA_DM_API_CONFIRM*)osi_malloc(sizeof(tBTA_DM_API_CONFIRM));
411
412   p_msg->hdr.event = BTA_DM_API_CONFIRM_EVT;
413   p_msg->bd_addr = bd_addr;
414   p_msg->accept = accept;
415
416   bta_sys_sendmsg(p_msg);
417 }
418
419 /*******************************************************************************
420  *
421  * Function         BTA_DmAddDevice
422  *
423  * Description      This function adds a device to the security database list of
424  *                  peer device
425  *
426  *
427  * Returns          void
428  *
429  ******************************************************************************/
430 void BTA_DmAddDevice(const RawAddress& bd_addr, DEV_CLASS dev_class,
431                      LINK_KEY link_key, tBTA_SERVICE_MASK trusted_mask,
432                      bool is_trusted, uint8_t key_type, tBTA_IO_CAP io_cap,
433                      uint8_t pin_length) {
434   tBTA_DM_API_ADD_DEVICE* p_msg =
435       (tBTA_DM_API_ADD_DEVICE*)osi_calloc(sizeof(tBTA_DM_API_ADD_DEVICE));
436
437   p_msg->hdr.event = BTA_DM_API_ADD_DEVICE_EVT;
438   p_msg->bd_addr = bd_addr;
439   p_msg->tm = trusted_mask;
440   p_msg->is_trusted = is_trusted;
441   p_msg->io_cap = io_cap;
442
443   if (link_key) {
444     p_msg->link_key_known = true;
445     p_msg->key_type = key_type;
446     memcpy(p_msg->link_key, link_key, LINK_KEY_LEN);
447   }
448
449   /* Load device class if specified */
450   if (dev_class) {
451     p_msg->dc_known = true;
452     memcpy(p_msg->dc, dev_class, DEV_CLASS_LEN);
453   }
454
455   memset(p_msg->bd_name, 0, BD_NAME_LEN + 1);
456   memset(p_msg->features, 0, sizeof(p_msg->features));
457   p_msg->pin_length = pin_length;
458
459   bta_sys_sendmsg(p_msg);
460 }
461
462 /*******************************************************************************
463  *
464  * Function         BTA_DmRemoveDevice
465  *
466  * Description      This function removes a device fromthe security database
467  *                  list of peer device. It manages unpairing even while
468  *                  connected.
469  *
470  *
471  * Returns          void
472  *
473  ******************************************************************************/
474 tBTA_STATUS BTA_DmRemoveDevice(const RawAddress& bd_addr) {
475   tBTA_DM_API_REMOVE_DEVICE* p_msg =
476       (tBTA_DM_API_REMOVE_DEVICE*)osi_calloc(sizeof(tBTA_DM_API_REMOVE_DEVICE));
477
478   p_msg->hdr.event = BTA_DM_API_REMOVE_DEVICE_EVT;
479   p_msg->bd_addr = bd_addr;
480
481   bta_sys_sendmsg(p_msg);
482
483   return BTA_SUCCESS;
484 }
485
486 /*******************************************************************************
487  *
488  * Function         BTA_GetEirService
489  *
490  * Description      This function is called to get BTA service mask from EIR.
491  *
492  * Parameters       p_eir - pointer of EIR significant part
493  *                  p_services - return the BTA service mask
494  *
495  * Returns          None
496  *
497  ******************************************************************************/
498 extern const uint16_t bta_service_id_to_uuid_lkup_tbl[];
499 void BTA_GetEirService(uint8_t* p_eir, size_t eir_len,
500                        tBTA_SERVICE_MASK* p_services) {
501   uint8_t xx, yy;
502   uint8_t num_uuid, max_num_uuid = 32;
503   uint8_t uuid_list[32 * LEN_UUID_16];
504   uint16_t* p_uuid16 = (uint16_t*)uuid_list;
505   tBTA_SERVICE_MASK mask;
506
507   BTM_GetEirUuidList(p_eir, eir_len, LEN_UUID_16, &num_uuid, uuid_list,
508                      max_num_uuid);
509   for (xx = 0; xx < num_uuid; xx++) {
510     mask = 1;
511     for (yy = 0; yy < BTA_MAX_SERVICE_ID; yy++) {
512       if (*(p_uuid16 + xx) == bta_service_id_to_uuid_lkup_tbl[yy]) {
513         *p_services |= mask;
514         break;
515       }
516       mask <<= 1;
517     }
518
519     /* for HSP v1.2 only device */
520     if (*(p_uuid16 + xx) == UUID_SERVCLASS_HEADSET_HS)
521       *p_services |= BTA_HSP_SERVICE_MASK;
522
523     if (*(p_uuid16 + xx) == UUID_SERVCLASS_HDP_SOURCE)
524       *p_services |= BTA_HL_SERVICE_MASK;
525
526     if (*(p_uuid16 + xx) == UUID_SERVCLASS_HDP_SINK)
527       *p_services |= BTA_HL_SERVICE_MASK;
528   }
529 }
530
531 /*******************************************************************************
532  *
533  * Function         BTA_DmGetConnectionState
534  *
535  * Description      Returns whether the remote device is currently connected.
536  *
537  * Returns          0 if the device is NOT connected.
538  *
539  ******************************************************************************/
540 uint16_t BTA_DmGetConnectionState(const RawAddress& bd_addr) {
541   tBTA_DM_PEER_DEVICE* p_dev = bta_dm_find_peer_device(bd_addr);
542   return (p_dev && p_dev->conn_state == BTA_DM_CONNECTED);
543 }
544
545 /*******************************************************************************
546  *                   Device Identification (DI) Server Functions
547  ******************************************************************************/
548 /*******************************************************************************
549  *
550  * Function         BTA_DmSetLocalDiRecord
551  *
552  * Description      This function adds a DI record to the local SDP database.
553  *
554  * Returns          BTA_SUCCESS if record set sucessfully, otherwise error code.
555  *
556  ******************************************************************************/
557 tBTA_STATUS BTA_DmSetLocalDiRecord(tBTA_DI_RECORD* p_device_info,
558                                    uint32_t* p_handle) {
559   tBTA_STATUS status = BTA_FAILURE;
560
561   if (bta_dm_di_cb.di_num < BTA_DI_NUM_MAX) {
562     if (SDP_SetLocalDiRecord((tSDP_DI_RECORD*)p_device_info, p_handle) ==
563         SDP_SUCCESS) {
564       if (!p_device_info->primary_record) {
565         bta_dm_di_cb.di_handle[bta_dm_di_cb.di_num] = *p_handle;
566         bta_dm_di_cb.di_num++;
567       }
568
569       bta_sys_add_uuid(UUID_SERVCLASS_PNP_INFORMATION);
570       status = BTA_SUCCESS;
571     }
572   }
573
574   return status;
575 }
576
577 /*******************************************************************************
578  *
579  * Function         bta_dmexecutecallback
580  *
581  * Description      This function will request BTA to execute a call back in the
582  *                  context of BTU task.
583  *                  This API was named in lower case because it is only intended
584  *                  for the internal customers(like BTIF).
585  *
586  * Returns          void
587  *
588  ******************************************************************************/
589 void bta_dmexecutecallback(tBTA_DM_EXEC_CBACK* p_callback, void* p_param) {
590   tBTA_DM_API_EXECUTE_CBACK* p_msg =
591       (tBTA_DM_API_EXECUTE_CBACK*)osi_malloc(sizeof(tBTA_DM_MSG));
592
593   p_msg->hdr.event = BTA_DM_API_EXECUTE_CBACK_EVT;
594   p_msg->p_param = p_param;
595   p_msg->p_exec_cback = p_callback;
596
597   bta_sys_sendmsg(p_msg);
598 }
599
600 /*******************************************************************************
601  *
602  * Function         BTA_DmAddBleKey
603  *
604  * Description      Add/modify LE device information.  This function will be
605  *                  normally called during host startup to restore all required
606  *                  information stored in the NVRAM.
607  *
608  * Parameters:      bd_addr          - BD address of the peer
609  *                  p_le_key         - LE key values.
610  *                  key_type         - LE SMP key type.
611  *
612  * Returns          BTA_SUCCESS if successful
613  *                  BTA_FAIL if operation failed.
614  *
615  ******************************************************************************/
616 void BTA_DmAddBleKey(const RawAddress& bd_addr, tBTA_LE_KEY_VALUE* p_le_key,
617                      tBTA_LE_KEY_TYPE key_type) {
618   tBTA_DM_API_ADD_BLEKEY* p_msg =
619       (tBTA_DM_API_ADD_BLEKEY*)osi_calloc(sizeof(tBTA_DM_API_ADD_BLEKEY));
620
621   p_msg->hdr.event = BTA_DM_API_ADD_BLEKEY_EVT;
622   p_msg->key_type = key_type;
623   p_msg->bd_addr = bd_addr;
624   memcpy(&p_msg->blekey, p_le_key, sizeof(tBTA_LE_KEY_VALUE));
625
626   bta_sys_sendmsg(p_msg);
627 }
628
629 /*******************************************************************************
630  *
631  * Function         BTA_DmAddBleDevice
632  *
633  * Description      Add a BLE device.  This function will be normally called
634  *                  during host startup to restore all required information
635  *                  for a LE device stored in the NVRAM.
636  *
637  * Parameters:      bd_addr          - BD address of the peer
638  *                  dev_type         - Remote device's device type.
639  *                  addr_type        - LE device address type.
640  *
641  * Returns          void
642  *
643  ******************************************************************************/
644 void BTA_DmAddBleDevice(const RawAddress& bd_addr, tBLE_ADDR_TYPE addr_type,
645                         tBT_DEVICE_TYPE dev_type) {
646   tBTA_DM_API_ADD_BLE_DEVICE* p_msg = (tBTA_DM_API_ADD_BLE_DEVICE*)osi_calloc(
647       sizeof(tBTA_DM_API_ADD_BLE_DEVICE));
648
649   p_msg->hdr.event = BTA_DM_API_ADD_BLEDEVICE_EVT;
650   p_msg->bd_addr = bd_addr;
651   p_msg->addr_type = addr_type;
652   p_msg->dev_type = dev_type;
653
654   bta_sys_sendmsg(p_msg);
655 }
656
657 /*******************************************************************************
658  *
659  * Function         BTA_DmBlePasskeyReply
660  *
661  * Description      Send BLE SMP passkey reply.
662  *
663  * Parameters:      bd_addr          - BD address of the peer
664  *                  accept           - passkey entry sucessful or declined.
665  *                  passkey          - passkey value, must be a 6 digit number,
666  *                                     can be lead by 0.
667  *
668  * Returns          void
669  *
670  ******************************************************************************/
671 void BTA_DmBlePasskeyReply(const RawAddress& bd_addr, bool accept,
672                            uint32_t passkey) {
673   tBTA_DM_API_PASSKEY_REPLY* p_msg =
674       (tBTA_DM_API_PASSKEY_REPLY*)osi_calloc(sizeof(tBTA_DM_API_PASSKEY_REPLY));
675
676   p_msg->hdr.event = BTA_DM_API_BLE_PASSKEY_REPLY_EVT;
677   p_msg->bd_addr = bd_addr;
678   p_msg->accept = accept;
679
680   if (accept) p_msg->passkey = passkey;
681
682   bta_sys_sendmsg(p_msg);
683 }
684
685 /*******************************************************************************
686  *
687  * Function         BTA_DmBleConfirmReply
688  *
689  * Description      Send BLE SMP SC user confirmation reply.
690  *
691  * Parameters:      bd_addr          - BD address of the peer
692  *                  accept           - numbers to compare are the same or
693  *                                     different.
694  *
695  * Returns          void
696  *
697  ******************************************************************************/
698 void BTA_DmBleConfirmReply(const RawAddress& bd_addr, bool accept) {
699   tBTA_DM_API_CONFIRM* p_msg =
700       (tBTA_DM_API_CONFIRM*)osi_calloc(sizeof(tBTA_DM_API_CONFIRM));
701
702   p_msg->hdr.event = BTA_DM_API_BLE_CONFIRM_REPLY_EVT;
703   p_msg->bd_addr = bd_addr;
704   p_msg->accept = accept;
705
706   bta_sys_sendmsg(p_msg);
707 }
708
709 /*******************************************************************************
710  *
711  * Function         BTA_DmBleSecurityGrant
712  *
713  * Description      Grant security request access.
714  *
715  * Parameters:      bd_addr          - BD address of the peer
716  *                  res              - security grant status.
717  *
718  * Returns          void
719  *
720  ******************************************************************************/
721 void BTA_DmBleSecurityGrant(const RawAddress& bd_addr,
722                             tBTA_DM_BLE_SEC_GRANT res) {
723   tBTA_DM_API_BLE_SEC_GRANT* p_msg =
724       (tBTA_DM_API_BLE_SEC_GRANT*)osi_calloc(sizeof(tBTA_DM_API_BLE_SEC_GRANT));
725
726   p_msg->hdr.event = BTA_DM_API_BLE_SEC_GRANT_EVT;
727   p_msg->bd_addr = bd_addr;
728   p_msg->res = res;
729
730   bta_sys_sendmsg(p_msg);
731 }
732
733 /*******************************************************************************
734  *
735  * Function         BTA_DmSetBlePrefConnParams
736  *
737  * Description      This function is called to set the preferred connection
738  *                  parameters when default connection parameter is not desired.
739  *
740  * Parameters:      bd_addr          - BD address of the peripheral
741  *                  scan_interval    - scan interval
742  *                  scan_window      - scan window
743  *                  min_conn_int     - minimum preferred connection interval
744  *                  max_conn_int     - maximum preferred connection interval
745  *                  slave_latency    - preferred slave latency
746  *                  supervision_tout - preferred supervision timeout
747  *
748  *
749  * Returns          void
750  *
751  ******************************************************************************/
752 void BTA_DmSetBlePrefConnParams(const RawAddress& bd_addr,
753                                 uint16_t min_conn_int, uint16_t max_conn_int,
754                                 uint16_t slave_latency,
755                                 uint16_t supervision_tout) {
756   tBTA_DM_API_BLE_CONN_PARAMS* p_msg = (tBTA_DM_API_BLE_CONN_PARAMS*)osi_calloc(
757       sizeof(tBTA_DM_API_BLE_CONN_PARAMS));
758
759   p_msg->hdr.event = BTA_DM_API_BLE_CONN_PARAM_EVT;
760   p_msg->peer_bda = bd_addr;
761   p_msg->conn_int_max = max_conn_int;
762   p_msg->conn_int_min = min_conn_int;
763   p_msg->slave_latency = slave_latency;
764   p_msg->supervision_tout = supervision_tout;
765
766   bta_sys_sendmsg(p_msg);
767 }
768
769 /*******************************************************************************
770  *
771  * Function         BTA_DmSetBleConnScanParams
772  *
773  * Description      This function is called to set scan parameters used in
774  *                  BLE connection request
775  *
776  * Parameters:      scan_interval    - scan interval
777  *                  scan_window      - scan window
778  *
779  * Returns          void
780  *
781  ******************************************************************************/
782 void BTA_DmSetBleConnScanParams(uint32_t scan_interval, uint32_t scan_window) {
783   tBTA_DM_API_BLE_SCAN_PARAMS* p_msg = (tBTA_DM_API_BLE_SCAN_PARAMS*)osi_calloc(
784       sizeof(tBTA_DM_API_BLE_SCAN_PARAMS));
785
786   p_msg->hdr.event = BTA_DM_API_BLE_CONN_SCAN_PARAM_EVT;
787   p_msg->scan_int = scan_interval;
788   p_msg->scan_window = scan_window;
789
790   bta_sys_sendmsg(p_msg);
791 }
792
793 /**
794  * Set BLE connectable mode to auto connect
795  */
796 void BTA_DmBleStartAutoConn() {
797   tBTA_DM_API_SET_NAME* p_msg =
798       (tBTA_DM_API_SET_NAME*)osi_calloc(sizeof(tBTA_DM_API_SET_NAME));
799
800   p_msg->hdr.event = BTA_DM_API_BLE_SET_BG_CONN_TYPE;
801   bta_sys_sendmsg(p_msg);
802 }
803
804 /*******************************************************************************
805  *
806  * Function         bta_dm_discover_send_msg
807  *
808  * Description      This function send discover message to BTA task.
809  *
810  * Returns          void
811  *
812  ******************************************************************************/
813 static void bta_dm_discover_send_msg(const RawAddress& bd_addr,
814                                      tBTA_SERVICE_MASK_EXT* p_services,
815                                      tBTA_DM_SEARCH_CBACK* p_cback,
816                                      bool sdp_search,
817                                      tBTA_TRANSPORT transport) {
818   const size_t len = p_services ? (sizeof(tBTA_DM_API_DISCOVER) +
819                                    sizeof(tBT_UUID) * p_services->num_uuid)
820                                 : sizeof(tBTA_DM_API_DISCOVER);
821   tBTA_DM_API_DISCOVER* p_msg = (tBTA_DM_API_DISCOVER*)osi_calloc(len);
822
823   p_msg->hdr.event = BTA_DM_API_DISCOVER_EVT;
824   p_msg->bd_addr = bd_addr;
825   p_msg->p_cback = p_cback;
826   p_msg->sdp_search = sdp_search;
827   p_msg->transport = transport;
828
829   if (p_services != NULL) {
830     p_msg->services = p_services->srvc_mask;
831     p_msg->num_uuid = p_services->num_uuid;
832     if (p_services->num_uuid != 0) {
833       p_msg->p_uuid = (tBT_UUID*)(p_msg + 1);
834       memcpy(p_msg->p_uuid, p_services->p_uuid,
835              sizeof(tBT_UUID) * p_services->num_uuid);
836     }
837   }
838
839   bta_sys_sendmsg(p_msg);
840 }
841
842 /*******************************************************************************
843  *
844  * Function         BTA_DmDiscoverByTransport
845  *
846  * Description      This function does service discovery on particular transport
847  *                  for services of a
848  *                  peer device. When services.num_uuid is 0, it indicates all
849  *                  GATT based services are to be searched; otherwise a list of
850  *                  UUID of interested services should be provided through
851  *                  p_services->p_uuid.
852  *
853  *
854  *
855  * Returns          void
856  *
857  ******************************************************************************/
858 void BTA_DmDiscoverByTransport(const RawAddress& bd_addr,
859                                tBTA_SERVICE_MASK_EXT* p_services,
860                                tBTA_DM_SEARCH_CBACK* p_cback, bool sdp_search,
861                                tBTA_TRANSPORT transport) {
862   bta_dm_discover_send_msg(bd_addr, p_services, p_cback, sdp_search, transport);
863 }
864
865 /*******************************************************************************
866  *
867  * Function         BTA_DmDiscoverExt
868  *
869  * Description      This function does service discovery for services of a
870  *                  peer device. When services.num_uuid is 0, it indicates all
871  *                  GATT based services are to be searched; other wise a list of
872  *                  UUID of interested services should be provided through
873  *                  p_services->p_uuid.
874  *
875  *
876  *
877  * Returns          void
878  *
879  ******************************************************************************/
880 void BTA_DmDiscoverExt(const RawAddress& bd_addr,
881                        tBTA_SERVICE_MASK_EXT* p_services,
882                        tBTA_DM_SEARCH_CBACK* p_cback, bool sdp_search) {
883   bta_dm_discover_send_msg(bd_addr, p_services, p_cback, sdp_search,
884                            BTA_TRANSPORT_UNKNOWN);
885 }
886
887 /*******************************************************************************
888  *
889  * Function         BTA_DmSearchExt
890  *
891  * Description      This function searches for peer Bluetooth devices. It
892  *                  performs an inquiry and gets the remote name for devices.
893  *                  Service discovery is done if services is non zero
894  *
895  * Parameters       p_dm_inq: inquiry conditions
896  *                  p_services: if service is not empty, service discovery will
897  *                              be done. For all GATT based service conditions,
898  *                              put num_uuid, and p_uuid is the pointer to the
899  *                              list of UUID values.
900  *                  p_cback: callback function when search is completed.
901  *
902  *
903  *
904  * Returns          void
905  *
906  ******************************************************************************/
907 void BTA_DmSearchExt(tBTA_DM_INQ* p_dm_inq, tBTA_SERVICE_MASK_EXT* p_services,
908                      tBTA_DM_SEARCH_CBACK* p_cback) {
909   const size_t len = p_services ? (sizeof(tBTA_DM_API_SEARCH) +
910                                    sizeof(tBT_UUID) * p_services->num_uuid)
911                                 : sizeof(tBTA_DM_API_SEARCH);
912   tBTA_DM_API_SEARCH* p_msg = (tBTA_DM_API_SEARCH*)osi_calloc(len);
913
914   p_msg->hdr.event = BTA_DM_API_SEARCH_EVT;
915   memcpy(&p_msg->inq_params, p_dm_inq, sizeof(tBTA_DM_INQ));
916   p_msg->p_cback = p_cback;
917   p_msg->rs_res = BTA_DM_RS_NONE;
918
919   if (p_services != NULL) {
920     p_msg->services = p_services->srvc_mask;
921     p_msg->num_uuid = p_services->num_uuid;
922
923     if (p_services->num_uuid != 0) {
924       p_msg->p_uuid = (tBT_UUID*)(p_msg + 1);
925       memcpy(p_msg->p_uuid, p_services->p_uuid,
926              sizeof(tBT_UUID) * p_services->num_uuid);
927     } else {
928       p_msg->p_uuid = NULL;
929     }
930   }
931
932   bta_sys_sendmsg(p_msg);
933 }
934
935 /*******************************************************************************
936  *
937  * Function         BTA_DmBleUpdateConnectionParam
938  *
939  * Description      Update connection parameters, can only be used when
940  *                  connection is up.
941  *
942  * Parameters:      bd_addr          - BD address of the peer
943  *                  min_int   -     minimum connection interval,
944  *                                  [0x0004 ~ 0x4000]
945  *                  max_int   -     maximum connection interval,
946  *                                  [0x0004 ~ 0x4000]
947  *                  latency   -     slave latency [0 ~ 500]
948  *                  timeout   -     supervision timeout [0x000a ~ 0xc80]
949  *
950  * Returns          void
951  *
952  ******************************************************************************/
953 void BTA_DmBleUpdateConnectionParam(const RawAddress& bd_addr, uint16_t min_int,
954                                     uint16_t max_int, uint16_t latency,
955                                     uint16_t timeout) {
956   tBTA_DM_API_UPDATE_CONN_PARAM* p_msg =
957       (tBTA_DM_API_UPDATE_CONN_PARAM*)osi_calloc(
958           sizeof(tBTA_DM_API_UPDATE_CONN_PARAM));
959
960   p_msg->hdr.event = BTA_DM_API_UPDATE_CONN_PARAM_EVT;
961   p_msg->bd_addr = bd_addr;
962   p_msg->min_int = min_int;
963   p_msg->max_int = max_int;
964   p_msg->latency = latency;
965   p_msg->timeout = timeout;
966
967   bta_sys_sendmsg(p_msg);
968 }
969
970 /*******************************************************************************
971  *
972  * Function         BTA_DmBleConfigLocalPrivacy
973  *
974  * Description      Enable/disable privacy on the local device
975  *
976  * Parameters:      privacy_enable   - enable/disabe privacy on remote device.
977  *
978  * Returns          void
979  *
980  ******************************************************************************/
981 void BTA_DmBleConfigLocalPrivacy(bool privacy_enable) {
982 #if (BLE_PRIVACY_SPT == TRUE)
983   tBTA_DM_API_LOCAL_PRIVACY* p_msg = (tBTA_DM_API_LOCAL_PRIVACY*)osi_calloc(
984       sizeof(tBTA_DM_API_ENABLE_PRIVACY));
985
986   p_msg->hdr.event = BTA_DM_API_LOCAL_PRIVACY_EVT;
987   p_msg->privacy_enable = privacy_enable;
988
989   bta_sys_sendmsg(p_msg);
990 #else
991   UNUSED(privacy_enable);
992 #endif
993 }
994
995 /*******************************************************************************
996  *
997  * Function         BTA_DmBleGetEnergyInfo
998  *
999  * Description      This function is called to obtain the energy info
1000  *
1001  * Parameters       p_cmpl_cback - Command complete callback
1002  *
1003  * Returns          void
1004  *
1005  ******************************************************************************/
1006 void BTA_DmBleGetEnergyInfo(tBTA_BLE_ENERGY_INFO_CBACK* p_cmpl_cback) {
1007   const size_t len = sizeof(tBTA_DM_API_ENERGY_INFO) + sizeof(tBLE_BD_ADDR);
1008   tBTA_DM_API_ENERGY_INFO* p_msg = (tBTA_DM_API_ENERGY_INFO*)osi_calloc(len);
1009
1010   APPL_TRACE_API("%s", __func__);
1011
1012   p_msg->hdr.event = BTA_DM_API_BLE_ENERGY_INFO_EVT;
1013   p_msg->p_energy_info_cback = p_cmpl_cback;
1014
1015   bta_sys_sendmsg(p_msg);
1016 }
1017
1018 /*******************************************************************************
1019  *
1020  * Function         BTA_DmBleUpdateConnectionParams
1021  *
1022  * Description      Update connection parameters, can only be used when
1023  *                  connection is up.
1024  *
1025  * Parameters:      bd_addr   - BD address of the peer
1026  *                  min_int   -     minimum connection interval,
1027  *                                  [0x0004 ~ 0x4000]
1028  *                  max_int   -     maximum connection interval,
1029  *                                  [0x0004 ~ 0x4000]
1030  *                  latency   -     slave latency [0 ~ 500]
1031  *                  timeout   -     supervision timeout [0x000a ~ 0xc80]
1032  *
1033  * Returns          void
1034  *
1035  ******************************************************************************/
1036 void BTA_DmBleUpdateConnectionParams(const RawAddress& bd_addr,
1037                                      uint16_t min_int, uint16_t max_int,
1038                                      uint16_t latency, uint16_t timeout) {
1039   tBTA_DM_API_UPDATE_CONN_PARAM* p_msg =
1040       (tBTA_DM_API_UPDATE_CONN_PARAM*)osi_calloc(
1041           sizeof(tBTA_DM_API_UPDATE_CONN_PARAM));
1042
1043   p_msg->hdr.event = BTA_DM_API_UPDATE_CONN_PARAM_EVT;
1044   p_msg->bd_addr = bd_addr;
1045   p_msg->min_int = min_int;
1046   p_msg->max_int = max_int;
1047   p_msg->latency = latency;
1048   p_msg->timeout = timeout;
1049
1050   bta_sys_sendmsg(p_msg);
1051 }
1052
1053 /*******************************************************************************
1054  *
1055  * Function         BTA_DmBleSetDataLength
1056  *
1057  * Description      This function is to set maximum LE data packet size
1058  *
1059  * Returns          void
1060  *
1061  *
1062  ******************************************************************************/
1063 void BTA_DmBleSetDataLength(const RawAddress& remote_device,
1064                             uint16_t tx_data_length) {
1065   tBTA_DM_API_BLE_SET_DATA_LENGTH* p_msg =
1066       (tBTA_DM_API_BLE_SET_DATA_LENGTH*)osi_malloc(
1067           sizeof(tBTA_DM_API_BLE_SET_DATA_LENGTH));
1068
1069   p_msg->remote_bda = remote_device;
1070   p_msg->hdr.event = BTA_DM_API_SET_DATA_LENGTH_EVT;
1071   p_msg->tx_data_length = tx_data_length;
1072
1073   bta_sys_sendmsg(p_msg);
1074 }
1075
1076 /*******************************************************************************
1077  *
1078  * Function         BTA_DmSetEncryption
1079  *
1080  * Description      This function is called to ensure that connection is
1081  *                  encrypted.  Should be called only on an open connection.
1082  *                  Typically only needed for connections that first want to
1083  *                  bring up unencrypted links, then later encrypt them.
1084  *
1085  * Parameters:      bd_addr       - Address of the peer device
1086  *                  transport     - transport of the link to be encruypted
1087  *                  p_callback    - Pointer to callback function to indicat the
1088  *                                  link encryption status
1089  *                  sec_act       - This is the security action to indicate
1090  *                                  what kind of BLE security level is required
1091  *                                  for the BLE link if BLE is supported.
1092  *                                  Note: This parameter is ignored for the
1093  *                                        BR/EDR or if BLE is not supported.
1094  *
1095  * Returns          void
1096  *
1097  ******************************************************************************/
1098 void BTA_DmSetEncryption(const RawAddress& bd_addr, tBTA_TRANSPORT transport,
1099                          tBTA_DM_ENCRYPT_CBACK* p_callback,
1100                          tBTA_DM_BLE_SEC_ACT sec_act) {
1101   tBTA_DM_API_SET_ENCRYPTION* p_msg = (tBTA_DM_API_SET_ENCRYPTION*)osi_calloc(
1102       sizeof(tBTA_DM_API_SET_ENCRYPTION));
1103
1104   APPL_TRACE_API("%s", __func__);
1105
1106   p_msg->hdr.event = BTA_DM_API_SET_ENCRYPTION_EVT;
1107   p_msg->bd_addr = bd_addr;
1108   p_msg->transport = transport;
1109   p_msg->p_callback = p_callback;
1110   p_msg->sec_act = sec_act;
1111
1112   bta_sys_sendmsg(p_msg);
1113 }
1114
1115 /*******************************************************************************
1116  *
1117  * Function         BTA_DmCloseACL
1118  *
1119  * Description      This function force to close an ACL connection and remove
1120  *                  the device from the security database list of known devices.
1121  *
1122  * Parameters:      bd_addr       - Address of the peer device
1123  *                  remove_dev    - remove device or not after link down
1124  *
1125  * Returns          void
1126  *
1127  ******************************************************************************/
1128 void BTA_DmCloseACL(const RawAddress& bd_addr, bool remove_dev,
1129                     tBTA_TRANSPORT transport) {
1130   tBTA_DM_API_REMOVE_ACL* p_msg =
1131       (tBTA_DM_API_REMOVE_ACL*)osi_calloc(sizeof(tBTA_DM_API_REMOVE_ACL));
1132
1133   APPL_TRACE_API("%s", __func__);
1134
1135   p_msg->hdr.event = BTA_DM_API_REMOVE_ACL_EVT;
1136   p_msg->bd_addr = bd_addr;
1137   p_msg->remove_dev = remove_dev;
1138   p_msg->transport = transport;
1139
1140   bta_sys_sendmsg(p_msg);
1141 }
1142
1143 /*******************************************************************************
1144  *
1145  * Function         BTA_DmBleObserve
1146  *
1147  * Description      This procedure keep the device listening for advertising
1148  *                  events from a broadcast device.
1149  *
1150  * Parameters       start: start or stop observe.
1151  *
1152  * Returns          void
1153
1154  *
1155  * Returns          void.
1156  *
1157  ******************************************************************************/
1158 extern void BTA_DmBleObserve(bool start, uint8_t duration,
1159                              tBTA_DM_SEARCH_CBACK* p_results_cb) {
1160   tBTA_DM_API_BLE_OBSERVE* p_msg =
1161       (tBTA_DM_API_BLE_OBSERVE*)osi_calloc(sizeof(tBTA_DM_API_BLE_OBSERVE));
1162
1163   APPL_TRACE_API("%s:start = %d ", __func__, start);
1164
1165   p_msg->hdr.event = BTA_DM_API_BLE_OBSERVE_EVT;
1166   p_msg->start = start;
1167   p_msg->duration = duration;
1168   p_msg->p_cback = p_results_cb;
1169
1170   bta_sys_sendmsg(p_msg);
1171 }
1172
1173 /*******************************************************************************
1174  *
1175  * Function         BTA_VendorInit
1176  *
1177  * Description      This function initializes vendor specific
1178  *
1179  * Returns          void
1180  *
1181  ******************************************************************************/
1182 void BTA_VendorInit(void) { APPL_TRACE_API("BTA_VendorInit"); }
1183
1184 /*******************************************************************************
1185  *
1186  * Function         BTA_VendorCleanup
1187  *
1188  * Description      This function frees up Broadcom specific VS specific dynamic
1189  *                  memory
1190  *
1191  * Returns          void
1192  *
1193  ******************************************************************************/
1194 void BTA_VendorCleanup(void) {
1195   tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
1196   BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
1197
1198   if (cmn_ble_vsc_cb.max_filter > 0) {
1199     btm_ble_adv_filter_cleanup();
1200 #if (BLE_PRIVACY_SPT == TRUE)
1201     btm_ble_resolving_list_cleanup();
1202 #endif
1203   }
1204
1205   if (cmn_ble_vsc_cb.tot_scan_results_strg > 0) btm_ble_batchscan_cleanup();
1206
1207   if (cmn_ble_vsc_cb.adv_inst_max > 0) btm_ble_multi_adv_cleanup();
1208 }