]> granicus.if.org Git - esp-idf/blob - components/bt/bluedroid/bta/dm/bta_dm_api.c
component/bt: some miscellaneous modifications for BLE
[esp-idf] / components / bt / bluedroid / bta / dm / bta_dm_api.c
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
25 #include "bta/bta_sys.h"
26 #include "bta/bta_api.h"
27 #include "bta_dm_int.h"
28 #include "bta_sys_int.h"
29 #include "stack/btm_api.h"
30 #include "btm_int.h"
31 #include <string.h>
32 #include "bta/utl.h"
33 #include "osi/allocator.h"
34
35 /*****************************************************************************
36 **  Constants
37 *****************************************************************************/
38
39 static const tBTA_SYS_REG bta_dm_reg = {
40     bta_dm_sm_execute,
41     bta_dm_sm_disable
42 };
43
44 static const tBTA_SYS_REG bta_dm_search_reg = {
45     bta_dm_search_sm_execute,
46     bta_dm_search_sm_disable
47 };
48
49 /*******************************************************************************
50 **
51 ** Function         BTA_EnableBluetooth
52 **
53 ** Description      Enables bluetooth service.  This function must be
54 **                  called before any other functions in the BTA API are called.
55 **
56 **
57 ** Returns          tBTA_STATUS
58 **
59 *******************************************************************************/
60 tBTA_STATUS BTA_EnableBluetooth(tBTA_DM_SEC_CBACK *p_cback)
61 {
62
63     tBTA_DM_API_ENABLE    *p_msg;
64
65     /* Bluetooth disabling is in progress */
66     if (bta_dm_cb.disabling) {
67         return BTA_FAILURE;
68     }
69
70     memset(&bta_dm_cb, 0, sizeof(bta_dm_cb));
71
72     bta_sys_register (BTA_ID_DM, &bta_dm_reg );
73     bta_sys_register (BTA_ID_DM_SEARCH, &bta_dm_search_reg );
74
75     /* if UUID list is not provided as static data */
76     bta_sys_eir_register(bta_dm_eir_update_uuid);
77
78     if ((p_msg = (tBTA_DM_API_ENABLE *) osi_malloc(sizeof(tBTA_DM_API_ENABLE))) != NULL) {
79         p_msg->hdr.event = BTA_DM_API_ENABLE_EVT;
80         p_msg->p_sec_cback = p_cback;
81         bta_sys_sendmsg(p_msg);
82         return BTA_SUCCESS;
83     }
84     return BTA_FAILURE;
85
86 }
87
88 /*******************************************************************************
89 **
90 ** Function         BTA_DisableBluetooth
91 **
92 ** Description      Disables bluetooth service.  This function is called when
93 **                  the application no longer needs bluetooth service
94 **
95 ** Returns          void
96 **
97 *******************************************************************************/
98 tBTA_STATUS BTA_DisableBluetooth(void)
99 {
100
101     BT_HDR    *p_msg;
102
103     if ((p_msg = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
104         p_msg->event = BTA_DM_API_DISABLE_EVT;
105         bta_sys_sendmsg(p_msg);
106     } else {
107         return BTA_FAILURE;
108     }
109
110     return BTA_SUCCESS;
111 }
112
113 /*******************************************************************************
114 **
115 ** Function         BTA_EnableTestMode
116 **
117 ** Description      Enables bluetooth device under test mode
118 **
119 **
120 ** Returns          tBTA_STATUS
121 **
122 *******************************************************************************/
123 tBTA_STATUS BTA_EnableTestMode(void)
124 {
125     BT_HDR    *p_msg;
126
127     APPL_TRACE_API("BTA_EnableTestMode");
128
129     if ((p_msg = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
130         p_msg->event = BTA_DM_API_ENABLE_TEST_MODE_EVT;
131         bta_sys_sendmsg(p_msg);
132         return BTA_SUCCESS;
133     }
134     return BTA_FAILURE;
135 }
136
137 /*******************************************************************************
138 **
139 ** Function         BTA_DisableTestMode
140 **
141 ** Description      Disable bluetooth device under test mode
142 **
143 **
144 ** Returns          None
145 **
146 *******************************************************************************/
147 void BTA_DisableTestMode(void)
148 {
149     BT_HDR    *p_msg;
150
151     APPL_TRACE_API("BTA_DisableTestMode");
152
153     if ((p_msg = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
154         p_msg->event = BTA_DM_API_DISABLE_TEST_MODE_EVT;
155         bta_sys_sendmsg(p_msg);
156     }
157 }
158
159 /*******************************************************************************
160 **
161 ** Function         BTA_DmSetDeviceName
162 **
163 ** Description      This function sets the Bluetooth name of local device
164 **
165 **
166 ** Returns          void
167 **
168 *******************************************************************************/
169 void BTA_DmSetDeviceName(const char *p_name)
170 {
171
172     tBTA_DM_API_SET_NAME    *p_msg;
173
174     if ((p_msg = (tBTA_DM_API_SET_NAME *) osi_malloc(sizeof(tBTA_DM_API_SET_NAME))) != NULL) {
175         p_msg->hdr.event = BTA_DM_API_SET_NAME_EVT;
176         /* truncate the name if needed */
177         BCM_STRNCPY_S((char *)p_msg->name, sizeof(p_msg->name), p_name, BD_NAME_LEN - 1);
178         p_msg->name[BD_NAME_LEN - 1] = 0;
179
180         bta_sys_sendmsg(p_msg);
181     }
182
183
184 }
185
186 void BTA_DmUpdateWhiteList(BOOLEAN add_remove,  BD_ADDR remote_addr, tBTA_ADD_WHITELIST_CBACK *add_wl_cb)
187 {
188     tBTA_DM_API_UPDATE_WHITE_LIST *p_msg;
189     if ((p_msg = (tBTA_DM_API_UPDATE_WHITE_LIST *)osi_malloc(sizeof(tBTA_DM_API_UPDATE_WHITE_LIST))) != NULL) {
190         p_msg->hdr.event = BTA_DM_API_UPDATE_WHITE_LIST_EVT;
191         p_msg->add_remove = add_remove;
192         p_msg->add_wl_cb = add_wl_cb;
193         memcpy(p_msg->remote_addr, remote_addr, sizeof(BD_ADDR));
194
195         bta_sys_sendmsg(p_msg);
196     }
197 }
198
199 void BTA_DmBleReadAdvTxPower(tBTA_CMPL_CB *cmpl_cb)
200 {
201     tBTA_DM_API_READ_ADV_TX_POWER *p_msg;
202     if ((p_msg = (tBTA_DM_API_READ_ADV_TX_POWER *)osi_malloc(sizeof(tBTA_DM_API_READ_ADV_TX_POWER))) != NULL) {
203         p_msg->hdr.event = BTA_DM_API_BLE_READ_ADV_TX_POWER_EVT;
204         p_msg->read_tx_power_cb = cmpl_cb;
205         bta_sys_sendmsg(p_msg);
206     }
207 }
208
209 void BTA_DmBleReadRSSI(BD_ADDR remote_addr, tBTA_TRANSPORT transport, tBTA_CMPL_CB *cmpl_cb)
210 {
211     tBTA_DM_API_READ_RSSI *p_msg;
212     if ((p_msg = (tBTA_DM_API_READ_RSSI *)osi_malloc(sizeof(tBTA_DM_API_READ_RSSI))) != NULL) {
213         p_msg->hdr.event = BTA_DM_API_BLE_READ_RSSI_EVT;
214         memcpy(p_msg->remote_addr, remote_addr, sizeof(BD_ADDR));
215         p_msg->transport = transport;
216         p_msg->read_rssi_cb = cmpl_cb;
217         bta_sys_sendmsg(p_msg);
218     }
219 }
220
221 /*******************************************************************************
222 **
223 ** Function         BTA_DmSetVisibility
224 **
225 ** Description      This function sets the Bluetooth connectable,
226 **                  discoverable, pairable and conn paired only modes of local device
227 **
228 **
229 ** Returns          void
230 **
231 *******************************************************************************/
232 void BTA_DmSetVisibility(tBTA_DM_DISC disc_mode, tBTA_DM_CONN conn_mode, UINT8 pairable_mode, UINT8 conn_filter )
233 {
234
235     tBTA_DM_API_SET_VISIBILITY    *p_msg;
236
237     if ((p_msg = (tBTA_DM_API_SET_VISIBILITY *) osi_malloc(sizeof(tBTA_DM_API_SET_VISIBILITY))) != NULL) {
238         p_msg->hdr.event = BTA_DM_API_SET_VISIBILITY_EVT;
239         p_msg->disc_mode = disc_mode;
240         p_msg->conn_mode = conn_mode;
241         p_msg->pair_mode = pairable_mode;
242         p_msg->conn_paired_only = conn_filter;
243
244
245         bta_sys_sendmsg(p_msg);
246     }
247
248
249 }
250
251 /*******************************************************************************
252 **
253 ** Function         BTA_DmSearch
254 **
255 ** Description      This function searches for peer Bluetooth devices. It performs
256 **                  an inquiry and gets the remote name for devices. Service
257 **                  discovery is done if services is non zero
258 **
259 **
260 ** Returns          void
261 **
262 *******************************************************************************/
263 void BTA_DmSearch(tBTA_DM_INQ *p_dm_inq, tBTA_SERVICE_MASK services, tBTA_DM_SEARCH_CBACK *p_cback)
264 {
265
266     tBTA_DM_API_SEARCH    *p_msg;
267
268     if ((p_msg = (tBTA_DM_API_SEARCH *) osi_malloc(sizeof(tBTA_DM_API_SEARCH))) != NULL) {
269         memset(p_msg, 0, sizeof(tBTA_DM_API_SEARCH));
270
271         p_msg->hdr.event = BTA_DM_API_SEARCH_EVT;
272         memcpy(&p_msg->inq_params, p_dm_inq, sizeof(tBTA_DM_INQ));
273         p_msg->services = services;
274         p_msg->p_cback = p_cback;
275         p_msg->rs_res  = BTA_DM_RS_NONE;
276         bta_sys_sendmsg(p_msg);
277     }
278
279 }
280
281
282 /*******************************************************************************
283 **
284 ** Function         BTA_DmSearchCancel
285 **
286 ** Description      This function  cancels a search initiated by BTA_DmSearch
287 **
288 **
289 ** Returns          void
290 **
291 *******************************************************************************/
292 void BTA_DmSearchCancel(void)
293 {
294     BT_HDR    *p_msg;
295
296     if ((p_msg = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
297         p_msg->event = BTA_DM_API_SEARCH_CANCEL_EVT;
298         bta_sys_sendmsg(p_msg);
299     }
300
301 }
302
303 /*******************************************************************************
304 **
305 ** Function         BTA_DmDiscover
306 **
307 ** Description      This function does service discovery for services of a
308 **                  peer device
309 **
310 **
311 ** Returns          void
312 **
313 *******************************************************************************/
314 #if (SDP_INCLUDED == TRUE)
315 void BTA_DmDiscover(BD_ADDR bd_addr, tBTA_SERVICE_MASK services,
316                     tBTA_DM_SEARCH_CBACK *p_cback, BOOLEAN sdp_search)
317 {
318     tBTA_DM_API_DISCOVER    *p_msg;
319
320     if ((p_msg = (tBTA_DM_API_DISCOVER *) osi_malloc(sizeof(tBTA_DM_API_DISCOVER))) != NULL) {
321         memset(p_msg, 0, sizeof(tBTA_DM_API_DISCOVER));
322
323         p_msg->hdr.event = BTA_DM_API_DISCOVER_EVT;
324         bdcpy(p_msg->bd_addr, bd_addr);
325         p_msg->services = services;
326         p_msg->p_cback = p_cback;
327         p_msg->sdp_search = sdp_search;
328         bta_sys_sendmsg(p_msg);
329     }
330 }
331
332 /*******************************************************************************
333 **
334 ** Function         BTA_DmDiscoverUUID
335 **
336 ** Description      This function does service discovery for services of a
337 **                  peer device
338 **
339 **
340 ** Returns          void
341 **
342 *******************************************************************************/
343 void BTA_DmDiscoverUUID(BD_ADDR bd_addr, tSDP_UUID *uuid,
344                         tBTA_DM_SEARCH_CBACK *p_cback, BOOLEAN sdp_search)
345 {
346     tBTA_DM_API_DISCOVER    *p_msg;
347
348     if ((p_msg = (tBTA_DM_API_DISCOVER *) osi_malloc(sizeof(tBTA_DM_API_DISCOVER))) != NULL) {
349         p_msg->hdr.event = BTA_DM_API_DISCOVER_EVT;
350         bdcpy(p_msg->bd_addr, bd_addr);
351         p_msg->services = BTA_USER_SERVICE_MASK; //Not exposed at API level
352         p_msg->p_cback = p_cback;
353         p_msg->sdp_search = sdp_search;
354
355 #if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE
356         p_msg->num_uuid = 0;
357         p_msg->p_uuid = NULL;
358 #endif
359         memcpy( &p_msg->uuid, uuid, sizeof(tSDP_UUID) );
360         bta_sys_sendmsg(p_msg);
361     }
362 }
363 #endif  ///SDP_INCLUDED == TRUE
364
365 /*******************************************************************************
366 **
367 ** Function         BTA_DmBond
368 **
369 ** Description      This function initiates a bonding procedure with a peer
370 **                  device
371 **
372 **
373 ** Returns          void
374 **
375 *******************************************************************************/
376 #if (SMP_INCLUDED == TRUE)
377 void BTA_DmBond(BD_ADDR bd_addr)
378 {
379     tBTA_DM_API_BOND    *p_msg;
380
381     p_msg = (tBTA_DM_API_BOND *) osi_malloc(sizeof(tBTA_DM_API_BOND));
382     if (p_msg != NULL) {
383         p_msg->hdr.event = BTA_DM_API_BOND_EVT;
384         bdcpy(p_msg->bd_addr, bd_addr);
385         p_msg->transport = BTA_TRANSPORT_UNKNOWN;
386         bta_sys_sendmsg(p_msg);
387     }
388 }
389
390 /*******************************************************************************
391 **
392 ** Function         BTA_DmBondByTransports
393 **
394 ** Description      This function initiates a bonding procedure with a peer
395 **                  device
396 **
397 **
398 ** Returns          void
399 **
400 *******************************************************************************/
401 void BTA_DmBondByTransport(BD_ADDR bd_addr, tBTA_TRANSPORT transport)
402 {
403     tBTA_DM_API_BOND    *p_msg;
404
405     if ((p_msg = (tBTA_DM_API_BOND *) osi_malloc(sizeof(tBTA_DM_API_BOND))) != NULL) {
406         p_msg->hdr.event = BTA_DM_API_BOND_EVT;
407         bdcpy(p_msg->bd_addr, bd_addr);
408         p_msg->transport = transport;
409         bta_sys_sendmsg(p_msg);
410     }
411
412
413 }
414
415 /*******************************************************************************
416 **
417 ** Function         BTA_DmBondCancel
418 **
419 ** Description      This function cancels the bonding procedure with a peer
420 **                  device
421 **
422 **
423 ** Returns          void
424 **
425 *******************************************************************************/
426 void BTA_DmBondCancel(BD_ADDR bd_addr)
427 {
428     tBTA_DM_API_BOND_CANCEL    *p_msg;
429
430     if ((p_msg = (tBTA_DM_API_BOND_CANCEL *) osi_malloc(sizeof(tBTA_DM_API_BOND_CANCEL))) != NULL) {
431         p_msg->hdr.event = BTA_DM_API_BOND_CANCEL_EVT;
432         bdcpy(p_msg->bd_addr, bd_addr);
433         bta_sys_sendmsg(p_msg);
434     }
435
436
437 }
438
439 /*******************************************************************************
440 **
441 ** Function         BTA_DMSetPinType
442 **
443 ** Description      This function set pin type as BTM_PIN_TYPE_FIXED or BTM_PIN_TYPE_VARIABLE
444 **
445 **
446 ** Returns          void
447 **
448 *******************************************************************************/
449 void BTA_DMSetPinType (UINT8 pin_type, UINT8 *pin_code, UINT8 pin_code_len)
450 {
451     tBTA_DM_API_SET_PIN_TYPE    *p_msg;
452
453     if ((p_msg = (tBTA_DM_API_SET_PIN_TYPE *) osi_malloc(sizeof(tBTA_DM_API_SET_PIN_TYPE))) != NULL) {
454         p_msg->hdr.event = BTA_DM_API_SET_PIN_TYPE_EVT;
455         p_msg->pin_type = pin_type;
456         p_msg->pin_len = pin_code_len;
457         memcpy(p_msg->p_pin, pin_code, pin_code_len);
458         bta_sys_sendmsg(p_msg);
459     }
460 }
461
462 /*******************************************************************************
463 **
464 ** Function         BTA_DmPinReply
465 **
466 ** Description      This function provides a pincode for a remote device when
467 **                  one is requested by DM through BTA_DM_PIN_REQ_EVT
468 **
469 **
470 ** Returns          void
471 **
472 *******************************************************************************/
473 void BTA_DmPinReply(BD_ADDR bd_addr, BOOLEAN accept, UINT8 pin_len, UINT8 *p_pin)
474 {
475     tBTA_DM_API_PIN_REPLY    *p_msg;
476
477     if ((p_msg = (tBTA_DM_API_PIN_REPLY *) osi_malloc(sizeof(tBTA_DM_API_PIN_REPLY))) != NULL) {
478         p_msg->hdr.event = BTA_DM_API_PIN_REPLY_EVT;
479         bdcpy(p_msg->bd_addr, bd_addr);
480         p_msg->accept = accept;
481         if (accept) {
482             p_msg->pin_len = pin_len;
483             memcpy(p_msg->p_pin, p_pin, pin_len);
484         }
485         bta_sys_sendmsg(p_msg);
486     }
487
488 }
489
490 #if (BTM_OOB_INCLUDED == TRUE && SMP_INCLUDED == TRUE)
491 /*******************************************************************************
492 **
493 ** Function         BTA_DmLocalOob
494 **
495 ** Description      This function retrieves the OOB data from local controller.
496 **                  The result is reported by:
497 **                  - bta_dm_co_loc_oob_ext() if device supports secure
498 **                    connections (SC)
499 **                  - bta_dm_co_loc_oob() if device doesn't support SC
500 **
501 ** Returns          void
502 **
503 *******************************************************************************/
504 void BTA_DmLocalOob(void)
505 {
506     tBTA_DM_API_LOC_OOB    *p_msg;
507
508     if ((p_msg = (tBTA_DM_API_LOC_OOB *) osi_malloc(sizeof(tBTA_DM_API_LOC_OOB))) != NULL) {
509         p_msg->hdr.event = BTA_DM_API_LOC_OOB_EVT;
510         bta_sys_sendmsg(p_msg);
511     }
512 }
513
514 /*******************************************************************************
515 **
516 ** Function         BTA_DmOobReply
517 **
518 **                  This function is called to provide the OOB data for
519 **                  SMP in response to BTM_LE_OOB_REQ_EVT
520 **
521 ** Parameters:      bd_addr     - Address of the peer device
522 **                  len         - length of simple pairing Randomizer  C
523 **                  p_value     - simple pairing Randomizer  C.
524 **
525 ** Returns          void
526 **
527 *******************************************************************************/
528 void BTA_DmOobReply(BD_ADDR bd_addr, UINT8 len, UINT8 *p_value)
529 {
530     tBTA_DM_API_OOB_REPLY    *p_msg;
531
532     if ((p_msg = (tBTA_DM_API_OOB_REPLY *) osi_malloc(sizeof(tBTA_DM_API_OOB_REPLY))) != NULL) {
533         p_msg->hdr.event = BTA_DM_API_OOB_REPLY_EVT;
534         if(p_value == NULL || len > BT_OCTET16_LEN) {
535             return;
536         }
537         memcpy(p_msg->bd_addr, bd_addr, BD_ADDR_LEN);
538         p_msg->len = len;
539         memcpy(p_msg->value, p_value, len);
540         bta_sys_sendmsg(p_msg);
541     }
542 }
543 #endif /* BTM_OOB_INCLUDED */
544 /*******************************************************************************
545 **
546 ** Function         BTA_DmConfirm
547 **
548 ** Description      This function accepts or rejects the numerical value of the
549 **                  Simple Pairing process on BTA_DM_SP_CFM_REQ_EVT
550 **
551 ** Returns          void
552 **
553 *******************************************************************************/
554 void BTA_DmConfirm(BD_ADDR bd_addr, BOOLEAN accept)
555 {
556     tBTA_DM_API_CONFIRM    *p_msg;
557
558     if ((p_msg = (tBTA_DM_API_CONFIRM *) osi_malloc(sizeof(tBTA_DM_API_CONFIRM))) != NULL) {
559         p_msg->hdr.event = BTA_DM_API_CONFIRM_EVT;
560         bdcpy(p_msg->bd_addr, bd_addr);
561         p_msg->accept = accept;
562         bta_sys_sendmsg(p_msg);
563     }
564 }
565
566 /*******************************************************************************
567 **
568 ** Function         BTA_DmPasskeyReqReply
569 **
570 ** Description      This function is called to provide the passkey for
571 **                  Simple Pairing in response to BTA_DM_SP_KEY_REQ_EVT
572 **
573 ** Returns          void
574 **
575 *******************************************************************************/
576 #if (BT_SSP_INCLUDED == TRUE)
577 void BTA_DmPasskeyReqReply(BOOLEAN accept, BD_ADDR bd_addr, UINT32 passkey)
578 {
579     tBTA_DM_API_KEY_REQ    *p_msg;
580     if ((p_msg = (tBTA_DM_API_KEY_REQ *) osi_malloc(sizeof(tBTA_DM_API_KEY_REQ))) != NULL) {
581         p_msg->hdr.event = BTA_DM_API_KEY_REQ_EVT;
582         bdcpy(p_msg->bd_addr, bd_addr);
583         p_msg->accept = accept;
584         p_msg->passkey = passkey;
585         bta_sys_sendmsg(p_msg);
586     }
587 }
588 #endif ///BT_SSP_INCLUDED == TRUE
589 /*******************************************************************************
590 **
591 ** Function         BTA_DmAddDevice
592 **
593 ** Description      This function adds a device to the security database list of
594 **                  peer device
595 **
596 **
597 ** Returns          void
598 **
599 *******************************************************************************/
600 void BTA_DmAddDevice(BD_ADDR bd_addr, DEV_CLASS dev_class, LINK_KEY link_key,
601                      tBTA_SERVICE_MASK trusted_mask, BOOLEAN is_trusted,
602                      UINT8 key_type, tBTA_IO_CAP io_cap, UINT8 pin_length)
603 {
604
605     tBTA_DM_API_ADD_DEVICE *p_msg;
606
607     if ((p_msg = (tBTA_DM_API_ADD_DEVICE *) osi_malloc(sizeof(tBTA_DM_API_ADD_DEVICE))) != NULL) {
608         memset (p_msg, 0, sizeof(tBTA_DM_API_ADD_DEVICE));
609
610         p_msg->hdr.event = BTA_DM_API_ADD_DEVICE_EVT;
611         bdcpy(p_msg->bd_addr, bd_addr);
612         p_msg->tm = trusted_mask;
613         p_msg->is_trusted = is_trusted;
614         p_msg->io_cap = io_cap;
615
616         if (link_key) {
617             p_msg->link_key_known = TRUE;
618             p_msg->key_type = key_type;
619             memcpy(p_msg->link_key, link_key, LINK_KEY_LEN);
620         }
621
622         /* Load device class if specified */
623         if (dev_class) {
624             p_msg->dc_known = TRUE;
625             memcpy (p_msg->dc, dev_class, DEV_CLASS_LEN);
626         }
627
628         memset (p_msg->bd_name, 0, BD_NAME_LEN + 1);
629         memset (p_msg->features, 0, sizeof (p_msg->features));
630         p_msg->pin_length = pin_length;
631
632         bta_sys_sendmsg(p_msg);
633     }
634 }
635
636
637 /*******************************************************************************
638 **
639 ** Function         BTA_DmRemoveDevice
640 **
641 ** Description      This function removes a device fromthe security database list of
642 **                  peer device. It manages unpairing even while connected.
643 **
644 **
645 ** Returns          void
646 **
647 *******************************************************************************/
648 tBTA_STATUS BTA_DmRemoveDevice(BD_ADDR bd_addr, tBT_TRANSPORT transport)
649 {
650     tBTA_DM_API_REMOVE_DEVICE *p_msg;
651
652     if ((p_msg = (tBTA_DM_API_REMOVE_DEVICE *) osi_malloc(sizeof(tBTA_DM_API_REMOVE_DEVICE))) != NULL) {
653         memset (p_msg, 0, sizeof(tBTA_DM_API_REMOVE_DEVICE));
654
655         p_msg->hdr.event = BTA_DM_API_REMOVE_DEVICE_EVT;
656         bdcpy(p_msg->bd_addr, bd_addr);
657         p_msg->transport = transport;
658         bta_sys_sendmsg(p_msg);
659     } else {
660         return BTA_FAILURE;
661     }
662
663     return BTA_SUCCESS;
664 }
665 #endif  ///SMP_INCLUDED == TRUE
666
667 /*******************************************************************************
668 **
669 ** Function         BTA_GetEirService
670 **
671 ** Description      This function is called to get BTA service mask from EIR.
672 **
673 ** Parameters       p_eir - pointer of EIR significant part
674 **                  p_services - return the BTA service mask
675 **
676 ** Returns          None
677 **
678 *******************************************************************************/
679 extern const UINT16 bta_service_id_to_uuid_lkup_tbl [];
680 void BTA_GetEirService( UINT8 *p_eir, tBTA_SERVICE_MASK *p_services )
681 {
682     UINT8 xx, yy;
683     UINT8 num_uuid, max_num_uuid = 32;
684     UINT8 uuid_list[32 * LEN_UUID_16];
685     UINT16 *p_uuid16 = (UINT16 *)uuid_list;
686     tBTA_SERVICE_MASK mask;
687
688     BTM_GetEirUuidList( p_eir, LEN_UUID_16, &num_uuid, uuid_list, max_num_uuid);
689     for ( xx = 0; xx < num_uuid; xx++ ) {
690         mask = 1;
691         for ( yy = 0; yy < BTA_MAX_SERVICE_ID; yy++ ) {
692             if ( *(p_uuid16 + xx) == bta_service_id_to_uuid_lkup_tbl[yy] ) {
693                 *p_services |= mask;
694                 break;
695             }
696             mask <<= 1;
697         }
698
699         /* for HSP v1.2 only device */
700         if (*(p_uuid16 + xx) == UUID_SERVCLASS_HEADSET_HS) {
701             *p_services |= BTA_HSP_SERVICE_MASK;
702         }
703
704         if (*(p_uuid16 + xx) == UUID_SERVCLASS_HDP_SOURCE) {
705             *p_services |= BTA_HL_SERVICE_MASK;
706         }
707
708         if (*(p_uuid16 + xx) == UUID_SERVCLASS_HDP_SINK) {
709             *p_services |= BTA_HL_SERVICE_MASK;
710         }
711     }
712 }
713
714 /*******************************************************************************
715 **
716 ** Function         BTA_DmGetConnectionState
717 **
718 ** Description      Returns whether the remote device is currently connected.
719 **
720 ** Returns          0 if the device is NOT connected.
721 **
722 *******************************************************************************/
723 UINT16 BTA_DmGetConnectionState( BD_ADDR bd_addr )
724 {
725     tBTA_DM_PEER_DEVICE *p_dev = bta_dm_find_peer_device(bd_addr);
726     return (p_dev && p_dev->conn_state == BTA_DM_CONNECTED);
727 }
728
729 #if (SDP_INCLUDED == TRUE)
730 /*******************************************************************************
731 **                   Device Identification (DI) Server Functions
732 *******************************************************************************/
733 /*******************************************************************************
734 **
735 ** Function         BTA_DmSetLocalDiRecord
736 **
737 ** Description      This function adds a DI record to the local SDP database.
738 **
739 ** Returns          BTA_SUCCESS if record set sucessfully, otherwise error code.
740 **
741 *******************************************************************************/
742 tBTA_STATUS BTA_DmSetLocalDiRecord( tBTA_DI_RECORD *p_device_info,
743                                     UINT32 *p_handle )
744 {
745     tBTA_STATUS  status = BTA_FAILURE;
746
747     if (bta_dm_di_cb.di_num < BTA_DI_NUM_MAX) {
748         if (SDP_SetLocalDiRecord((tSDP_DI_RECORD *)p_device_info, p_handle) == SDP_SUCCESS) {
749             if (!p_device_info->primary_record) {
750                 bta_dm_di_cb.di_handle[bta_dm_di_cb.di_num] = *p_handle;
751                 bta_dm_di_cb.di_num ++;
752             }
753
754             bta_sys_add_uuid(UUID_SERVCLASS_PNP_INFORMATION);
755             status =  BTA_SUCCESS;
756         }
757     }
758
759     return status;
760 }
761 #endif  ///SDP_INCLUDED == TRUE
762 /*******************************************************************************
763 **
764 ** Function         bta_dmexecutecallback
765 **
766 ** Description      This function will request BTA to execute a call back in the context of BTU task
767 **                  This API was named in lower case because it is only intended
768 **                  for the internal customers(like BTIF).
769 **
770 ** Returns          void
771 **
772 *******************************************************************************/
773 void bta_dmexecutecallback (tBTA_DM_EXEC_CBACK *p_callback, void *p_param)
774 {
775     tBTA_DM_API_EXECUTE_CBACK *p_msg;
776
777     if ((p_msg = (tBTA_DM_API_EXECUTE_CBACK *) osi_malloc(sizeof(tBTA_DM_API_EXECUTE_CBACK))) != NULL) {
778         p_msg->hdr.event = BTA_DM_API_EXECUTE_CBACK_EVT;
779         p_msg->p_param = p_param;
780         p_msg->p_exec_cback = p_callback;
781         bta_sys_sendmsg(p_msg);
782     }
783 }
784
785 /*******************************************************************************
786 **
787 ** Function         BTA_DmAddBleKey
788 **
789 ** Description      Add/modify LE device information.  This function will be
790 **                  normally called during host startup to restore all required
791 **                  information stored in the NVRAM.
792 **
793 ** Parameters:      bd_addr          - BD address of the peer
794 **                  p_le_key         - LE key values.
795 **                  key_type         - LE SMP key type.
796 **
797 ** Returns          BTA_SUCCESS if successful
798 **                  BTA_FAIL if operation failed.
799 **
800 *******************************************************************************/
801 #if BLE_INCLUDED == TRUE
802 #if SMP_INCLUDED == TRUE
803 void BTA_DmAddBleKey (BD_ADDR bd_addr, tBTA_LE_KEY_VALUE *p_le_key, tBTA_LE_KEY_TYPE key_type)
804 {
805     tBTA_DM_API_ADD_BLEKEY *p_msg;
806
807     if ((p_msg = (tBTA_DM_API_ADD_BLEKEY *) osi_malloc(sizeof(tBTA_DM_API_ADD_BLEKEY))) != NULL) {
808         memset (p_msg, 0, sizeof(tBTA_DM_API_ADD_BLEKEY));
809
810         p_msg->hdr.event = BTA_DM_API_ADD_BLEKEY_EVT;
811         p_msg->key_type = key_type;
812         bdcpy(p_msg->bd_addr, bd_addr);
813         memcpy(&p_msg->blekey, p_le_key, sizeof(tBTA_LE_KEY_VALUE));
814
815         bta_sys_sendmsg(p_msg);
816     }
817
818 }
819
820 /*******************************************************************************
821 **
822 ** Function         BTA_DmAddBleDevice
823 **
824 ** Description      Add a BLE device.  This function will be normally called
825 **                  during host startup to restore all required information
826 **                  for a LE device stored in the NVRAM.
827 **
828 ** Parameters:      bd_addr          - BD address of the peer
829 **                  dev_type         - Remote device's device type.
830 **                  auth_mode        - auth mode
831 **                  addr_type        - LE device address type.
832 **
833 ** Returns          void
834 **
835 *******************************************************************************/
836 void BTA_DmAddBleDevice(BD_ADDR bd_addr, tBLE_ADDR_TYPE addr_type, int auth_mode, tBT_DEVICE_TYPE dev_type)
837 {
838     tBTA_DM_API_ADD_BLE_DEVICE *p_msg;
839
840     if ((p_msg = (tBTA_DM_API_ADD_BLE_DEVICE *) osi_malloc(sizeof(tBTA_DM_API_ADD_BLE_DEVICE))) != NULL) {
841         memset (p_msg, 0, sizeof(tBTA_DM_API_ADD_BLE_DEVICE));
842
843         p_msg->hdr.event = BTA_DM_API_ADD_BLEDEVICE_EVT;
844         bdcpy(p_msg->bd_addr, bd_addr);
845         p_msg->addr_type = addr_type;
846         p_msg->auth_mode = auth_mode;
847         p_msg->dev_type = dev_type;
848
849         bta_sys_sendmsg(p_msg);
850     }
851 }
852 /*******************************************************************************
853 **
854 ** Function         BTA_DmBlePasskeyReply
855 **
856 ** Description      Send BLE SMP passkey reply.
857 **
858 ** Parameters:      bd_addr          - BD address of the peer
859 **                  accept           - passkey entry sucessful or declined.
860 **                  passkey          - passkey value, must be a 6 digit number,
861 **                                     can be lead by 0.
862 **
863 ** Returns          void
864 **
865 *******************************************************************************/
866 void BTA_DmBlePasskeyReply(BD_ADDR bd_addr, BOOLEAN accept, UINT32 passkey)
867 {
868     tBTA_DM_API_PASSKEY_REPLY    *p_msg;
869
870     if ((p_msg = (tBTA_DM_API_PASSKEY_REPLY *) osi_malloc(sizeof(tBTA_DM_API_PASSKEY_REPLY))) != NULL) {
871         memset(p_msg, 0, sizeof(tBTA_DM_API_PASSKEY_REPLY));
872
873         p_msg->hdr.event = BTA_DM_API_BLE_PASSKEY_REPLY_EVT;
874         bdcpy(p_msg->bd_addr, bd_addr);
875         p_msg->accept = accept;
876
877         if (accept) {
878             p_msg->passkey = passkey;
879         }
880         bta_sys_sendmsg(p_msg);
881     }
882 }
883
884 void BTA_DmBleSetStaticPasskey(bool add, uint32_t passkey)
885 {
886     tBTA_DM_API_SET_DEFAULT_PASSKEY    *p_msg;
887
888     if ((p_msg = (tBTA_DM_API_SET_DEFAULT_PASSKEY *) osi_malloc(sizeof(tBTA_DM_API_SET_DEFAULT_PASSKEY))) != NULL) {
889         memset(p_msg, 0, sizeof(tBTA_DM_API_SET_DEFAULT_PASSKEY));
890
891         p_msg->hdr.event = BTA_DM_API_BLE_SET_STATIC_PASSKEY_EVT;
892         p_msg->add = add;
893         p_msg->static_passkey = passkey;
894         bta_sys_sendmsg(p_msg);
895     }
896 }
897
898 /*******************************************************************************
899 **
900 ** Function         BTA_DmBleConfirmReply
901 **
902 ** Description      Send BLE SMP SC user confirmation reply.
903 **
904 ** Parameters:      bd_addr          - BD address of the peer
905 **                  accept           - numbers to compare are the same or different.
906 **
907 ** Returns          void
908 **
909 *******************************************************************************/
910 void BTA_DmBleConfirmReply(BD_ADDR bd_addr, BOOLEAN accept)
911 {
912     tBTA_DM_API_CONFIRM *p_msg = (tBTA_DM_API_CONFIRM *)osi_malloc(sizeof(tBTA_DM_API_CONFIRM));
913     if (p_msg != NULL) {
914         memset(p_msg, 0, sizeof(tBTA_DM_API_CONFIRM));
915         p_msg->hdr.event = BTA_DM_API_BLE_CONFIRM_REPLY_EVT;
916         bdcpy(p_msg->bd_addr, bd_addr);
917         p_msg->accept = accept;
918         bta_sys_sendmsg(p_msg);
919     }
920 }
921
922 /*******************************************************************************
923 **
924 ** Function         BTA_DmBleSecurityGrant
925 **
926 ** Description      Grant security request access.
927 **
928 ** Parameters:      bd_addr          - BD address of the peer
929 **                  res              - security grant status.
930 **
931 ** Returns          void
932 **
933 *******************************************************************************/
934 void BTA_DmBleSecurityGrant(BD_ADDR bd_addr, tBTA_DM_BLE_SEC_GRANT res)
935 {
936     tBTA_DM_API_BLE_SEC_GRANT    *p_msg;
937
938     if ((p_msg = (tBTA_DM_API_BLE_SEC_GRANT *) osi_malloc(sizeof(tBTA_DM_API_BLE_SEC_GRANT))) != NULL) {
939         memset(p_msg, 0, sizeof(tBTA_DM_API_BLE_SEC_GRANT));
940
941         p_msg->hdr.event = BTA_DM_API_BLE_SEC_GRANT_EVT;
942         bdcpy(p_msg->bd_addr, bd_addr);
943         p_msg->res = res;
944
945         bta_sys_sendmsg(p_msg);
946     }
947 }
948 #endif  ///SMP_INCLUDED == TRUE
949 #endif  ///BLE_INCLUDED == TRUE
950
951
952 /*******************************************************************************
953 **
954 ** Function         BTA_DmSetBlePrefConnParams
955 **
956 ** Description      This function is called to set the preferred connection
957 **                  parameters when default connection parameter is not desired.
958 **
959 ** Parameters:      bd_addr          - BD address of the peripheral
960 **                  scan_interval    - scan interval
961 **                  scan_window      - scan window
962 **                  min_conn_int     - minimum preferred connection interval
963 **                  max_conn_int     - maximum preferred connection interval
964 **                  slave_latency    - preferred slave latency
965 **                  supervision_tout - preferred supervision timeout
966 **
967 **
968 ** Returns          void
969 **
970 *******************************************************************************/
971 void BTA_DmSetBlePrefConnParams(BD_ADDR bd_addr,
972                                 UINT16 min_conn_int, UINT16 max_conn_int,
973                                 UINT16 slave_latency, UINT16 supervision_tout )
974 {
975 #if BLE_INCLUDED == TRUE
976     tBTA_DM_API_BLE_CONN_PARAMS    *p_msg;
977
978     if ((p_msg = (tBTA_DM_API_BLE_CONN_PARAMS *) osi_malloc(sizeof(tBTA_DM_API_BLE_CONN_PARAMS))) != NULL) {
979         memset(p_msg, 0, sizeof(tBTA_DM_API_BLE_CONN_PARAMS));
980
981         p_msg->hdr.event = BTA_DM_API_BLE_CONN_PARAM_EVT;
982
983         memcpy(p_msg->peer_bda, bd_addr, BD_ADDR_LEN);
984
985         p_msg->conn_int_max     = max_conn_int;
986         p_msg->conn_int_min     = min_conn_int;
987         p_msg->slave_latency    = slave_latency;
988         p_msg->supervision_tout = supervision_tout;
989
990         bta_sys_sendmsg(p_msg);
991     }
992 #endif
993 }
994
995 /*******************************************************************************
996 **
997 ** Function         BTA_DmSetBleConnScanParams
998 **
999 ** Description      This function is called to set scan parameters used in
1000 **                  BLE connection request
1001 **
1002 ** Parameters:      scan_interval    - scan interval
1003 **                  scan_window      - scan window
1004 **
1005 ** Returns          void
1006 **
1007 *******************************************************************************/
1008 void BTA_DmSetBleConnScanParams(UINT32 scan_interval, UINT32 scan_window)
1009 {
1010     tBTA_DM_API_BLE_SCAN_PARAMS  *p_msg;
1011     if ((p_msg = (tBTA_DM_API_BLE_SCAN_PARAMS *)osi_malloc(sizeof(tBTA_DM_API_BLE_SCAN_PARAMS))) != NULL) {
1012         memset(p_msg, 0, sizeof(tBTA_DM_API_BLE_SCAN_PARAMS));
1013         p_msg->hdr.event = BTA_DM_API_BLE_CONN_SCAN_PARAM_EVT;
1014         p_msg->scan_int         = scan_interval;
1015         p_msg->scan_window      = scan_window;
1016         bta_sys_sendmsg(p_msg);
1017     }
1018 }
1019
1020 /*******************************************************************************
1021 **
1022 ** Function         BTA_DmSetBleScanParams
1023 **
1024 ** Description      This function is called to set scan parameters
1025 **
1026 ** Parameters:      client_if - Client IF
1027 **                  scan_interval - scan interval
1028 **                  scan_window - scan window
1029 **                  scan_mode - scan mode
1030 **                  scan_param_setup_status_cback - Set scan param status callback
1031 **
1032 ** Returns          void
1033 **
1034 *******************************************************************************/
1035 void BTA_DmSetBleScanParams(tGATT_IF client_if, UINT32 scan_interval,
1036                             UINT32 scan_window, tBLE_SCAN_MODE scan_mode,
1037                             tBLE_SCAN_PARAM_SETUP_CBACK scan_param_setup_cback)
1038 {
1039     tBTA_DM_API_BLE_SCAN_PARAMS *p_msg;
1040
1041     if ((p_msg = (tBTA_DM_API_BLE_SCAN_PARAMS *)osi_malloc(sizeof(tBTA_DM_API_BLE_SCAN_PARAMS))) != NULL) {
1042         memset(p_msg, 0, sizeof(tBTA_DM_API_BLE_SCAN_PARAMS));
1043         p_msg->hdr.event = BTA_DM_API_BLE_SCAN_PARAM_EVT;
1044         p_msg->client_if = client_if;
1045         p_msg->scan_int = scan_interval;
1046         p_msg->scan_window = scan_window;
1047         p_msg->scan_mode = scan_mode;
1048         p_msg->scan_param_setup_cback = scan_param_setup_cback;
1049
1050         bta_sys_sendmsg(p_msg);
1051     }
1052 }
1053
1054
1055 /*******************************************************************************
1056 **
1057 ** Function         BTA_DmSetBleScanFilterParams
1058 **
1059 ** Description      This function is called to set scan parameters
1060 **
1061 ** Parameters:      client_if - Client IF
1062 **                  scan_interval - scan interval
1063 **                  scan_window - scan window
1064 **                  scan_mode - scan mode
1065 **                  scan_duplicate_filter - scan duplicate filter
1066 **                  scan_param_setup_status_cback - Set scan param status callback
1067 **
1068 ** Returns          void
1069 **
1070 *******************************************************************************/
1071 void BTA_DmSetBleScanFilterParams(tGATT_IF client_if, UINT32 scan_interval,
1072                                   UINT32 scan_window, tBLE_SCAN_MODE scan_mode, UINT8 scan_fil_poilcy,
1073                                   UINT8 addr_type_own, UINT8 scan_duplicate_filter, tBLE_SCAN_PARAM_SETUP_CBACK scan_param_setup_cback)
1074 {
1075     tBTA_DM_API_BLE_SCAN_FILTER_PARAMS *p_msg;
1076
1077     if ((p_msg = (tBTA_DM_API_BLE_SCAN_FILTER_PARAMS *)osi_malloc(sizeof(tBTA_DM_API_BLE_SCAN_FILTER_PARAMS))) != NULL) {
1078         memset(p_msg, 0, sizeof(tBTA_DM_API_BLE_SCAN_FILTER_PARAMS));
1079         p_msg->hdr.event = BTA_DM_API_BLE_SCAN_FIL_PARAM_EVT;
1080         p_msg->client_if = client_if;
1081         p_msg->scan_int = scan_interval;
1082         p_msg->scan_window = scan_window;
1083         p_msg->scan_mode = scan_mode;
1084         p_msg->addr_type_own = addr_type_own;
1085         p_msg->scan_duplicate_filter = scan_duplicate_filter;
1086         p_msg->scan_filter_policy = scan_fil_poilcy;
1087         p_msg->scan_param_setup_cback = scan_param_setup_cback;
1088
1089         bta_sys_sendmsg(p_msg);
1090     }
1091
1092
1093 }
1094
1095 /*******************************************************************************
1096 **
1097 ** Function         BTA_DmSetBleAdvParams
1098 **
1099 ** Description      This function sets the advertising parameters BLE functionality.
1100 **                  It is to be called when device act in peripheral or broadcaster
1101 **                  role.
1102 **
1103 **
1104 ** Returns          void
1105 **
1106 *******************************************************************************/
1107 void BTA_DmSetBleAdvParams (UINT16 adv_int_min, UINT16 adv_int_max,
1108                             tBLE_BD_ADDR *p_dir_bda)
1109 {
1110 #if BLE_INCLUDED == TRUE
1111     tBTA_DM_API_BLE_ADV_PARAMS    *p_msg;
1112
1113     APPL_TRACE_API ("BTA_DmSetBleAdvParam: %d, %d\n", adv_int_min, adv_int_max);
1114
1115     if ((p_msg = (tBTA_DM_API_BLE_ADV_PARAMS *) osi_malloc(sizeof(tBTA_DM_API_BLE_ADV_PARAMS)
1116                  + sizeof(tBLE_BD_ADDR))) != NULL) {
1117         memset(p_msg, 0, sizeof(tBTA_DM_API_BLE_ADV_PARAMS) + sizeof(tBLE_BD_ADDR));
1118
1119         p_msg->hdr.event = BTA_DM_API_BLE_ADV_PARAM_EVT;
1120
1121         p_msg->adv_int_min      = adv_int_min;
1122         p_msg->adv_int_max      = adv_int_max;
1123
1124         if (p_dir_bda != NULL) {
1125             p_msg->p_dir_bda = (tBLE_BD_ADDR *)(p_msg + 1);
1126             memcpy(p_msg->p_dir_bda, p_dir_bda, sizeof(tBLE_BD_ADDR));
1127         }
1128
1129         bta_sys_sendmsg(p_msg);
1130     }
1131 #endif
1132 }
1133
1134 void BTA_DmSetBleAdvParamsAll (UINT16 adv_int_min, UINT16 adv_int_max,
1135                                UINT8 adv_type, tBLE_ADDR_TYPE addr_type_own,
1136                                tBTM_BLE_ADV_CHNL_MAP chnl_map, tBTM_BLE_AFP adv_fil_pol,
1137                                tBLE_BD_ADDR *p_dir_bda, tBTA_START_ADV_CMPL_CBACK p_start_adv_cb)
1138 {
1139 #if BLE_INCLUDED == TRUE
1140     tBTA_DM_API_BLE_ADV_PARAMS_ALL    *p_msg;
1141
1142     APPL_TRACE_API ("BTA_DmSetBleAdvParamsAll: %d, %d\n", adv_int_min, adv_int_max);
1143     APPL_TRACE_API ("adv_type = %d, addr_type_own = %d, chnl_map = %d, adv_fil_pol = %d\n",
1144                       adv_type, addr_type_own, chnl_map, adv_fil_pol);
1145     if ((p_msg = (tBTA_DM_API_BLE_ADV_PARAMS_ALL *) osi_malloc(sizeof(tBTA_DM_API_BLE_ADV_PARAMS_ALL)
1146                  + sizeof(tBLE_BD_ADDR))) != NULL) {
1147         memset(p_msg, 0, sizeof(tBTA_DM_API_BLE_ADV_PARAMS_ALL));
1148
1149         p_msg->hdr.event = BTA_DM_API_BLE_ADV_PARAM_All_EVT;
1150
1151         p_msg->adv_int_min      = adv_int_min;
1152         p_msg->adv_int_max      = adv_int_max;
1153         p_msg->adv_type         = adv_type;
1154         p_msg->addr_type_own    = addr_type_own;
1155         p_msg->channel_map      = chnl_map;
1156         p_msg->adv_filter_policy    = adv_fil_pol;
1157         p_msg->p_start_adv_cback    = p_start_adv_cb;
1158         if (p_dir_bda != NULL) {
1159             p_msg->p_dir_bda = (tBLE_BD_ADDR *)(p_msg + 1);
1160             memcpy(p_msg->p_dir_bda, p_dir_bda, sizeof(tBLE_BD_ADDR));
1161         }
1162
1163         bta_sys_sendmsg(p_msg);
1164     }
1165 #endif
1166 }
1167
1168
1169
1170 /*******************************************************************************
1171 **                      BLE ADV data management API
1172 ********************************************************************************/
1173
1174 #if BLE_INCLUDED == TRUE
1175 /*******************************************************************************
1176 **
1177 ** Function         BTA_DmBleSetAdvConfig
1178 **
1179 ** Description      This function is called to override the BTA default ADV parameters.
1180 **
1181 ** Parameters       data_mask: adv data mask.
1182 **                  p_adv_cfg: Pointer to User defined ADV data structure. This
1183 **                             memory space can not be freed until p_adv_data_cback
1184 **                             is received.
1185 **                  p_adv_data_cback: set adv data complete callback.
1186 **
1187 ** Returns          None
1188 **
1189 *******************************************************************************/
1190 void BTA_DmBleSetAdvConfig (tBTA_BLE_AD_MASK data_mask, tBTA_BLE_ADV_DATA *p_adv_cfg,
1191                             tBTA_SET_ADV_DATA_CMPL_CBACK *p_adv_data_cback)
1192 {
1193     tBTA_DM_API_SET_ADV_CONFIG  *p_msg;
1194
1195     if ((p_msg = (tBTA_DM_API_SET_ADV_CONFIG *)
1196                  osi_malloc(sizeof(tBTA_DM_API_SET_ADV_CONFIG))) != NULL) {
1197         p_msg->hdr.event = BTA_DM_API_BLE_SET_ADV_CONFIG_EVT;
1198         p_msg->data_mask = data_mask;
1199         p_msg->p_adv_data_cback = p_adv_data_cback;
1200         p_msg->p_adv_cfg = p_adv_cfg;
1201
1202         bta_sys_sendmsg(p_msg);
1203     }
1204 }
1205
1206 /*******************************************************************************
1207 **
1208 ** Function         BTA_DmBleSetAdvConfigRaw
1209 **
1210 ** Description      This function is called to set raw Advertising data
1211 **
1212 ** Parameters       p_raw_adv : raw advertising data.
1213 **                  raw_adv_len : raw advertising data length.
1214 **                  p_adv_data_cback : set adv data complete callback.
1215 **
1216 ** Returns          None
1217 **
1218 *******************************************************************************/
1219 void BTA_DmBleSetAdvConfigRaw (UINT8 *p_raw_adv, UINT32 raw_adv_len,
1220                             tBTA_SET_ADV_DATA_CMPL_CBACK *p_adv_data_cback)
1221 {
1222     tBTA_DM_API_SET_ADV_CONFIG_RAW  *p_msg;
1223
1224     if ((p_msg = (tBTA_DM_API_SET_ADV_CONFIG_RAW *)
1225                  osi_malloc(sizeof(tBTA_DM_API_SET_ADV_CONFIG_RAW) + raw_adv_len)) != NULL) {
1226         p_msg->hdr.event = BTA_DM_API_BLE_SET_ADV_CONFIG_RAW_EVT;
1227         p_msg->p_adv_data_cback = p_adv_data_cback;
1228         p_msg->p_raw_adv = (UINT8 *)(p_msg + 1);
1229         memcpy(p_msg->p_raw_adv, p_raw_adv, raw_adv_len);
1230         p_msg->raw_adv_len = raw_adv_len;
1231
1232         bta_sys_sendmsg(p_msg);
1233     }
1234 }
1235
1236 /*******************************************************************************
1237 **
1238 ** Function         BTA_DmBleSetLongAdv
1239 **
1240 ** Description      This function is called to set long Advertising data
1241 **
1242 ** Parameters       adv_data : long advertising data.
1243 **                  adv_data_len : long advertising data length.
1244 **                  p_adv_data_cback : set long adv data complete callback.
1245 **
1246 ** Returns          None
1247 **
1248 *******************************************************************************/
1249 void BTA_DmBleSetLongAdv (UINT8 *adv_data, UINT32 adv_data_len,
1250                             tBTA_SET_ADV_DATA_CMPL_CBACK *p_adv_data_cback)
1251 {
1252     tBTA_DM_API_SET_LONG_ADV  *p_msg;
1253
1254     if ((p_msg = (tBTA_DM_API_SET_LONG_ADV *)
1255                  osi_malloc(sizeof(tBTA_DM_API_SET_LONG_ADV))) != NULL) {
1256         p_msg->hdr.event = BTA_DM_API_BLE_SET_LONG_ADV_EVT;
1257         p_msg->p_adv_data_cback = p_adv_data_cback;
1258         p_msg->adv_data = adv_data;
1259         p_msg->adv_data_len = adv_data_len;
1260
1261         bta_sys_sendmsg(p_msg);
1262     }
1263 }
1264
1265 /*******************************************************************************
1266 **
1267 ** Function         BTA_DmBleSetScanRsp
1268 **
1269 ** Description      This function is called to override the BTA scan response.
1270 **
1271 ** Parameters       Pointer to User defined ADV data structure
1272 **
1273 ** Returns          None
1274 **
1275 *******************************************************************************/
1276 extern void BTA_DmBleSetScanRsp (tBTA_BLE_AD_MASK data_mask, tBTA_BLE_ADV_DATA *p_adv_cfg,
1277                                  tBTA_SET_ADV_DATA_CMPL_CBACK *p_adv_data_cback)
1278 {
1279     tBTA_DM_API_SET_ADV_CONFIG  *p_msg;
1280
1281     if ((p_msg = (tBTA_DM_API_SET_ADV_CONFIG *)
1282                  osi_malloc(sizeof(tBTA_DM_API_SET_ADV_CONFIG))) != NULL) {
1283         p_msg->hdr.event = BTA_DM_API_BLE_SET_SCAN_RSP_EVT;
1284         p_msg->data_mask = data_mask;
1285         p_msg->p_adv_data_cback = p_adv_data_cback;
1286         p_msg->p_adv_cfg = p_adv_cfg;
1287
1288         bta_sys_sendmsg(p_msg);
1289     }
1290 }
1291
1292 /*******************************************************************************
1293 **
1294 ** Function         BTA_DmBleSetScanRspRaw
1295 **
1296 ** Description      This function is called to set raw scan response data
1297 **
1298 ** Parameters       p_raw_scan_rsp : raw scan_rspertising data.
1299 **                  raw_scan_rsp_len : raw scan_rspertising data length.
1300 **                  p_scan_rsp_data_cback : set scan_rsp data complete callback.
1301 **
1302 ** Returns          None
1303 **
1304 *******************************************************************************/
1305 void BTA_DmBleSetScanRspRaw (UINT8 *p_raw_scan_rsp, UINT32 raw_scan_rsp_len,
1306                             tBTA_SET_ADV_DATA_CMPL_CBACK *p_scan_rsp_data_cback)
1307 {
1308     tBTA_DM_API_SET_ADV_CONFIG_RAW  *p_msg;
1309
1310     if ((p_msg = (tBTA_DM_API_SET_ADV_CONFIG_RAW *)
1311                  osi_malloc(sizeof(tBTA_DM_API_SET_ADV_CONFIG_RAW) + raw_scan_rsp_len)) != NULL) {
1312         p_msg->hdr.event = BTA_DM_API_BLE_SET_SCAN_RSP_RAW_EVT;
1313         p_msg->p_adv_data_cback = p_scan_rsp_data_cback;
1314         p_msg->p_raw_adv = (UINT8 *)(p_msg + 1);
1315         memcpy(p_msg->p_raw_adv, p_raw_scan_rsp, raw_scan_rsp_len);
1316         p_msg->raw_adv_len = raw_scan_rsp_len;
1317
1318         bta_sys_sendmsg(p_msg);
1319     }
1320 }
1321
1322 /*******************************************************************************
1323 **
1324 ** Function         BTA_DmUpdateDuplicateExceptionalList
1325 **
1326 ** Description      This function is called to update duplicate scan exceptional list
1327 **
1328 ** Parameters       subcode : add, remove or clean duplicate scan exceptional list.
1329 **                  type : device info type.
1330 **                  device_info:  device info
1331 **                  p_update_duplicate_ignore_list_cback :  update complete callback.
1332 **
1333 ** Returns          None
1334 **
1335 *******************************************************************************/
1336 void BTA_DmUpdateDuplicateExceptionalList(UINT8 subcode, UINT32 type, BD_ADDR device_info, tBTA_UPDATE_DUPLICATE_EXCEPTIONAL_LIST_CMPL_CBACK p_update_duplicate_exceptional_list_cback)
1337 {
1338     tBTA_DM_API_UPDATE_DUPLICATE_EXCEPTIONAL_LIST *p_msg;
1339     if ((p_msg = (tBTA_DM_API_UPDATE_DUPLICATE_EXCEPTIONAL_LIST *)osi_malloc(sizeof(tBTA_DM_API_UPDATE_DUPLICATE_EXCEPTIONAL_LIST))) != NULL) {
1340         p_msg->hdr.event = BTA_DM_API_UPDATE_DUPLICATE_EXCEPTIONAL_LIST_EVT;
1341         p_msg->subcode = subcode;
1342         p_msg->type = type;
1343         p_msg->exceptional_list_cb = p_update_duplicate_exceptional_list_cback;
1344         memcpy(p_msg->device_info, device_info, sizeof(BD_ADDR));
1345
1346         bta_sys_sendmsg(p_msg);
1347     }
1348 }
1349
1350 /*******************************************************************************
1351 **
1352 ** Function         BTA_DmBleSetStorageParams
1353 **
1354 ** Description      This function is called to override the BTA scan response.
1355 **
1356 ** Parameters       batch_scan_full_max -Max storage space (in %) allocated to full scanning
1357 **                  batch_scan_trunc_max -Max storage space (in %) allocated to truncated scanning
1358 **                  batch_scan_notify_threshold -Setup notification level based on total space
1359 **                  p_setup_cback - Setup callback pointer
1360 **                  p_thres_cback - Threshold callback pointer
1361 **                  p_rep_cback - Reports callback pointer
1362 **                  ref_value - Ref value
1363 **
1364 ** Returns          None
1365 **
1366 *******************************************************************************/
1367 extern void BTA_DmBleSetStorageParams(UINT8 batch_scan_full_max,
1368                                       UINT8 batch_scan_trunc_max,
1369                                       UINT8 batch_scan_notify_threshold,
1370                                       tBTA_BLE_SCAN_SETUP_CBACK *p_setup_cback,
1371                                       tBTA_BLE_SCAN_THRESHOLD_CBACK *p_thres_cback,
1372                                       tBTA_BLE_SCAN_REP_CBACK *p_rep_cback,
1373                                       tBTA_DM_BLE_REF_VALUE ref_value)
1374 {
1375     tBTA_DM_API_SET_STORAGE_CONFIG  *p_msg;
1376     bta_dm_cb.p_setup_cback = p_setup_cback;
1377     if ((p_msg = (tBTA_DM_API_SET_STORAGE_CONFIG *)
1378                  osi_malloc(sizeof(tBTA_DM_API_SET_STORAGE_CONFIG))) != NULL) {
1379         p_msg->hdr.event = BTA_DM_API_BLE_SETUP_STORAGE_EVT;
1380         p_msg->p_setup_cback = bta_ble_scan_setup_cb;
1381         p_msg->p_thres_cback = p_thres_cback;
1382         p_msg->p_read_rep_cback = p_rep_cback;
1383         p_msg->ref_value = ref_value;
1384         p_msg->batch_scan_full_max = batch_scan_full_max;
1385         p_msg->batch_scan_trunc_max = batch_scan_trunc_max;
1386         p_msg->batch_scan_notify_threshold = batch_scan_notify_threshold;
1387         bta_sys_sendmsg(p_msg);
1388     }
1389 }
1390
1391 /*******************************************************************************
1392 **
1393 ** Function         BTA_DmBleEnableBatchScan
1394 **
1395 ** Description      This function is called to enable the batch scan
1396 **
1397 ** Parameters       scan_mode -Batch scan mode
1398 **                  scan_interval - Scan interval
1399 **                  scan_window - Scan window
1400 **                  discard_rule -Discard rules
1401 **                  addr_type - Address type
1402 **                  ref_value - Reference value
1403 **
1404 ** Returns          None
1405 **
1406 *******************************************************************************/
1407 extern void BTA_DmBleEnableBatchScan(tBTA_BLE_BATCH_SCAN_MODE scan_mode,
1408                                      UINT32 scan_interval, UINT32 scan_window,
1409                                      tBTA_BLE_DISCARD_RULE discard_rule,
1410                                      tBLE_ADDR_TYPE        addr_type,
1411                                      tBTA_DM_BLE_REF_VALUE ref_value)
1412 {
1413     tBTA_DM_API_ENABLE_SCAN  *p_msg;
1414
1415     if ((p_msg = (tBTA_DM_API_ENABLE_SCAN *) osi_malloc(sizeof(tBTA_DM_API_ENABLE_SCAN))) != NULL) {
1416         p_msg->hdr.event = BTA_DM_API_BLE_ENABLE_BATCH_SCAN_EVT;
1417         p_msg->scan_mode = scan_mode;
1418         p_msg->scan_int = scan_interval;
1419         p_msg->scan_window = scan_window;
1420         p_msg->discard_rule = discard_rule;
1421         p_msg->addr_type = addr_type;
1422         p_msg->ref_value = ref_value;
1423         bta_sys_sendmsg(p_msg);
1424     }
1425 }
1426
1427 /*******************************************************************************
1428 **
1429 ** Function         BTA_DmBleDisableBatchScan
1430 **
1431 ** Description      This function is called to disable the batch scan
1432 **
1433 ** Parameters       ref_value - Reference value
1434 **
1435 ** Returns          None
1436 **
1437 *******************************************************************************/
1438 extern void BTA_DmBleDisableBatchScan(tBTA_DM_BLE_REF_VALUE ref_value)
1439 {
1440     tBTA_DM_API_DISABLE_SCAN  *p_msg;
1441
1442     if ((p_msg = (tBTA_DM_API_DISABLE_SCAN *)
1443                  osi_malloc(sizeof(tBTA_DM_API_DISABLE_SCAN))) != NULL) {
1444         p_msg->hdr.event = BTA_DM_API_BLE_DISABLE_BATCH_SCAN_EVT;
1445         p_msg->ref_value = ref_value;
1446         bta_sys_sendmsg(p_msg);
1447     }
1448 }
1449
1450 /*******************************************************************************
1451 **
1452 ** Function         BTA_DmBleReadScanReports
1453 **
1454 ** Description      This function is called to read scan reports
1455 **
1456 ** Parameters       scan_type -Batch scan mode
1457 **                  ref_value - Reference value
1458 **
1459 ** Returns          None
1460 **
1461 *******************************************************************************/
1462 extern void BTA_DmBleReadScanReports(tBTA_BLE_BATCH_SCAN_MODE scan_type,
1463                                      tBTA_DM_BLE_REF_VALUE ref_value)
1464 {
1465     tBTA_DM_API_READ_SCAN_REPORTS  *p_msg;
1466
1467     if ((p_msg = (tBTA_DM_API_READ_SCAN_REPORTS *)
1468                  osi_malloc(sizeof(tBTA_DM_API_READ_SCAN_REPORTS))) != NULL) {
1469         p_msg->hdr.event = BTA_DM_API_BLE_READ_SCAN_REPORTS_EVT;
1470         p_msg->scan_type = scan_type;
1471         p_msg->ref_value = ref_value;
1472         bta_sys_sendmsg(p_msg);
1473     }
1474 }
1475
1476 /*******************************************************************************
1477 **
1478 ** Function         BTA_DmBleTrackAdvertiser
1479 **
1480 ** Description      This function is called to track advertiser
1481 **
1482 ** Parameters       ref_value - Reference value
1483 **                  p_track_adv_cback - Track ADV callback
1484 **
1485 ** Returns          None
1486 **
1487 *******************************************************************************/
1488 extern void BTA_DmBleTrackAdvertiser(tBTA_DM_BLE_REF_VALUE ref_value,
1489                                      tBTA_BLE_TRACK_ADV_CBACK *p_track_adv_cback)
1490 {
1491     tBTA_DM_API_TRACK_ADVERTISER  *p_msg;
1492
1493     if ((p_msg = (tBTA_DM_API_TRACK_ADVERTISER *)
1494                  osi_malloc(sizeof(tBTA_DM_API_TRACK_ADVERTISER))) != NULL) {
1495         p_msg->hdr.event = BTA_DM_API_BLE_TRACK_ADVERTISER_EVT;
1496         p_msg->p_track_adv_cback = p_track_adv_cback;
1497         p_msg->ref_value = ref_value;
1498         bta_sys_sendmsg(p_msg);
1499     }
1500 }
1501
1502 #endif
1503
1504 /*******************************************************************************
1505 **                      BLE ADV data management API
1506 ********************************************************************************/
1507 #if BLE_INCLUDED == TRUE
1508
1509 /*******************************************************************************
1510 **
1511 ** Function         BTA_DmBleBroadcast
1512 **
1513 ** Description      This function starts or stops LE broadcasting.
1514 **
1515 ** Parameters       start: start or stop broadcast.
1516 **
1517 ** Returns          None
1518 **
1519 *******************************************************************************/
1520 extern void BTA_DmBleBroadcast (BOOLEAN start, tBTA_START_STOP_ADV_CMPL_CBACK *p_start_stop_adv_cb)
1521 {
1522     tBTA_DM_API_BLE_OBSERVE   *p_msg;
1523
1524     APPL_TRACE_API("BTA_DmBleBroadcast: start = %d \n", start);
1525
1526     if ((p_msg = (tBTA_DM_API_BLE_OBSERVE *) osi_malloc(sizeof(tBTA_DM_API_BLE_OBSERVE))) != NULL) {
1527         memset(p_msg, 0, sizeof(tBTA_DM_API_BLE_OBSERVE));
1528
1529         p_msg->hdr.event = BTA_DM_API_BLE_BROADCAST_EVT;
1530         p_msg->start = start;
1531         if (start == FALSE){
1532             p_msg->p_stop_adv_cback= p_start_stop_adv_cb;
1533         }
1534
1535         bta_sys_sendmsg(p_msg);
1536     }
1537 }
1538
1539 #endif
1540 /*******************************************************************************
1541 **
1542 ** Function         BTA_DmBleSetBgConnType
1543 **
1544 ** Description      This function is called to set BLE connectable mode for a
1545 **                  peripheral device.
1546 **
1547 ** Parameters       bg_conn_type: it can be auto connection, or selective connection.
1548 **                  p_select_cback: callback function when selective connection procedure
1549 **                              is being used.
1550 **
1551 ** Returns          void
1552 **
1553 *******************************************************************************/
1554 void BTA_DmBleSetBgConnType(tBTA_DM_BLE_CONN_TYPE bg_conn_type, tBTA_DM_BLE_SEL_CBACK *p_select_cback)
1555 {
1556 #if BLE_INCLUDED == TRUE
1557     tBTA_DM_API_BLE_SET_BG_CONN_TYPE    *p_msg;
1558
1559     if ((p_msg = (tBTA_DM_API_BLE_SET_BG_CONN_TYPE *) osi_malloc(sizeof(tBTA_DM_API_BLE_SET_BG_CONN_TYPE))) != NULL) {
1560         memset(p_msg, 0, sizeof(tBTA_DM_API_BLE_SET_BG_CONN_TYPE));
1561
1562         p_msg->hdr.event        = BTA_DM_API_BLE_SET_BG_CONN_TYPE;
1563         p_msg->bg_conn_type     = bg_conn_type;
1564         p_msg->p_select_cback   = p_select_cback;
1565
1566         bta_sys_sendmsg(p_msg);
1567     }
1568 #endif
1569 }
1570
1571 /*******************************************************************************
1572 **
1573 ** Function         bta_dm_discover_send_msg
1574 **
1575 ** Description      This function send discover message to BTA task.
1576 **
1577 ** Returns          void
1578 **
1579 *******************************************************************************/
1580 #if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE && SDP_INCLUDED == TRUE
1581 static void bta_dm_discover_send_msg(BD_ADDR bd_addr, tBTA_SERVICE_MASK_EXT *p_services,
1582                                      tBTA_DM_SEARCH_CBACK *p_cback, BOOLEAN sdp_search,
1583                                      tBTA_TRANSPORT transport)
1584 {
1585     tBTA_DM_API_DISCOVER    *p_msg;
1586     UINT16  len = p_services ? (sizeof(tBTA_DM_API_DISCOVER) +
1587                                 sizeof(tBT_UUID) * p_services->num_uuid) :
1588                   sizeof(tBTA_DM_API_DISCOVER);
1589
1590     if ((p_msg = (tBTA_DM_API_DISCOVER *) osi_malloc(len)) != NULL) {
1591         memset(p_msg, 0, len);
1592
1593         p_msg->hdr.event = BTA_DM_API_DISCOVER_EVT;
1594         bdcpy(p_msg->bd_addr, bd_addr);
1595         p_msg->p_cback = p_cback;
1596         p_msg->sdp_search = sdp_search;
1597         p_msg->transport    = transport;
1598
1599         if (p_services != NULL) {
1600 #if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE
1601             p_msg->services = p_services->srvc_mask;
1602             p_msg->num_uuid = p_services->num_uuid;
1603             if (p_services->num_uuid != 0) {
1604                 p_msg->p_uuid = (tBT_UUID *)(p_msg + 1);
1605                 memcpy(p_msg->p_uuid, p_services->p_uuid, sizeof(tBT_UUID) * p_services->num_uuid);
1606             }
1607 #endif
1608         }
1609
1610         bta_sys_sendmsg(p_msg);
1611     }
1612 }
1613 #endif
1614 /*******************************************************************************
1615 **
1616 ** Function         BTA_DmDiscoverByTransport
1617 **
1618 ** Description      This function does service discovery on particular transport
1619 **                  for services of a
1620 **                  peer device. When services.num_uuid is 0, it indicates all
1621 **                  GATT based services are to be searched; otherwise a list of
1622 **                  UUID of interested services should be provided through
1623 **                  p_services->p_uuid.
1624 **
1625 **
1626 **
1627 ** Returns          void
1628 **
1629 *******************************************************************************/
1630 void BTA_DmDiscoverByTransport(BD_ADDR bd_addr, tBTA_SERVICE_MASK_EXT *p_services,
1631                                tBTA_DM_SEARCH_CBACK *p_cback, BOOLEAN sdp_search,
1632                                tBTA_TRANSPORT transport)
1633 {
1634 #if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE && SDP_INCLUDED == TRUE
1635     bta_dm_discover_send_msg(bd_addr, p_services, p_cback, sdp_search, transport);
1636 #endif
1637 }
1638
1639
1640 /*******************************************************************************
1641 **
1642 ** Function         BTA_DmDiscoverExt
1643 **
1644 ** Description      This function does service discovery for services of a
1645 **                  peer device. When services.num_uuid is 0, it indicates all
1646 **                  GATT based services are to be searched; other wise a list of
1647 **                  UUID of interested services should be provided through
1648 **                  p_services->p_uuid.
1649 **
1650 **
1651 **
1652 ** Returns          void
1653 **
1654 *******************************************************************************/
1655 void BTA_DmDiscoverExt(BD_ADDR bd_addr, tBTA_SERVICE_MASK_EXT *p_services,
1656                        tBTA_DM_SEARCH_CBACK *p_cback, BOOLEAN sdp_search)
1657 {
1658 #if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE && SDP_INCLUDED == TRUE
1659     bta_dm_discover_send_msg(bd_addr, p_services, p_cback, sdp_search, BTA_TRANSPORT_UNKNOWN);
1660 #endif
1661
1662 }
1663
1664 /*******************************************************************************
1665 **
1666 ** Function         BTA_DmSearchExt
1667 **
1668 ** Description      This function searches for peer Bluetooth devices. It performs
1669 **                  an inquiry and gets the remote name for devices. Service
1670 **                  discovery is done if services is non zero
1671 **
1672 ** Parameters       p_dm_inq: inquiry conditions
1673 **                  p_services: if service is not empty, service discovery will be done.
1674 **                            for all GATT based service condition, put num_uuid, and
1675 **                            p_uuid is the pointer to the list of UUID values.
1676 **                  p_cback: callback functino when search is completed.
1677 **
1678 **
1679 **
1680 ** Returns          void
1681 **
1682 *******************************************************************************/
1683 void BTA_DmSearchExt(tBTA_DM_INQ *p_dm_inq, tBTA_SERVICE_MASK_EXT *p_services, tBTA_DM_SEARCH_CBACK *p_cback)
1684 {
1685 #if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE
1686     tBTA_DM_API_SEARCH    *p_msg;
1687     UINT16  len = p_services ? (sizeof(tBTA_DM_API_SEARCH) + sizeof(tBT_UUID) * p_services->num_uuid) :
1688                   sizeof(tBTA_DM_API_SEARCH);
1689
1690     if ((p_msg = (tBTA_DM_API_SEARCH *) osi_malloc(len)) != NULL) {
1691         memset(p_msg, 0, len);
1692
1693         p_msg->hdr.event = BTA_DM_API_SEARCH_EVT;
1694         memcpy(&p_msg->inq_params, p_dm_inq, sizeof(tBTA_DM_INQ));
1695         p_msg->p_cback = p_cback;
1696         p_msg->rs_res  = BTA_DM_RS_NONE;
1697
1698
1699         if (p_services != NULL) {
1700             p_msg->services = p_services->srvc_mask;
1701             p_msg->num_uuid = p_services->num_uuid;
1702
1703             if (p_services->num_uuid != 0) {
1704                 p_msg->p_uuid = (tBT_UUID *)(p_msg + 1);
1705                 memcpy(p_msg->p_uuid, p_services->p_uuid, sizeof(tBT_UUID) * p_services->num_uuid);
1706             } else {
1707                 p_msg->p_uuid = NULL;
1708             }
1709         }
1710
1711         bta_sys_sendmsg(p_msg);
1712     }
1713 #else
1714     UNUSED(p_dm_inq);
1715     UNUSED(p_services);
1716     UNUSED(p_cback);
1717 #endif
1718 }
1719 /*******************************************************************************
1720 **
1721 ** Function         BTA_DmBleUpdateConnectionParam
1722 **
1723 ** Description      Update connection parameters, can only be used when connection is up.
1724 **
1725 ** Parameters:      bd_addr          - BD address of the peer
1726 **                  min_int   -     minimum connection interval, [0x0004~ 0x4000]
1727 **                  max_int   -     maximum connection interval, [0x0004~ 0x4000]
1728 **                  latency   -     slave latency [0 ~ 500]
1729 **                  timeout   -     supervision timeout [0x000a ~ 0xc80]
1730 **
1731 ** Returns          void
1732 **
1733 *******************************************************************************/
1734 void BTA_DmBleUpdateConnectionParam(BD_ADDR bd_addr, UINT16 min_int,
1735                                     UINT16 max_int, UINT16 latency,
1736                                     UINT16 timeout)
1737 {
1738 #if BLE_INCLUDED == TRUE
1739     tBTA_DM_API_UPDATE_CONN_PARAM *p_msg;
1740
1741     p_msg = (tBTA_DM_API_UPDATE_CONN_PARAM *) osi_malloc(sizeof(tBTA_DM_API_UPDATE_CONN_PARAM));
1742     if (p_msg != NULL) {
1743         memset(p_msg, 0, sizeof(tBTA_DM_API_UPDATE_CONN_PARAM));
1744
1745         p_msg->hdr.event = BTA_DM_API_UPDATE_CONN_PARAM_EVT;
1746         bdcpy(p_msg->bd_addr, bd_addr);
1747         p_msg->min_int   = min_int;
1748         p_msg->max_int   = max_int;
1749         p_msg->latency   = latency;
1750         p_msg->timeout   = timeout;
1751
1752         bta_sys_sendmsg(p_msg);
1753     }
1754 #endif
1755 }
1756 /*******************************************************************************
1757 **
1758 ** Function         BTA_DmBleConfigLocalPrivacy
1759 **
1760 ** Description      Enable/disable privacy on the local device
1761 **
1762 ** Parameters:      privacy_enable   - enable/disabe privacy on remote device.
1763 **
1764 ** Returns          void
1765 **
1766 *******************************************************************************/
1767 void BTA_DmBleConfigLocalPrivacy(BOOLEAN privacy_enable, tBTA_SET_LOCAL_PRIVACY_CBACK *set_local_privacy_cback)
1768 {
1769     ///This function used the irk to generate the resolve address
1770 #if BLE_INCLUDED == TRUE && BLE_PRIVACY_SPT == TRUE
1771     tBTA_DM_API_LOCAL_PRIVACY *p_msg;
1772
1773     if ((p_msg = (tBTA_DM_API_LOCAL_PRIVACY *) osi_malloc(sizeof(tBTA_DM_API_ENABLE_PRIVACY))) != NULL) {
1774         memset (p_msg, 0, sizeof(tBTA_DM_API_LOCAL_PRIVACY));
1775
1776         p_msg->hdr.event = BTA_DM_API_LOCAL_PRIVACY_EVT;
1777         p_msg->privacy_enable   = privacy_enable;
1778         p_msg->set_local_privacy_cback = set_local_privacy_cback;
1779         bta_sys_sendmsg(p_msg);
1780     }
1781 #else
1782     UNUSED (privacy_enable);
1783 #endif
1784 }
1785
1786 #if BLE_INCLUDED == TRUE
1787 /*******************************************************************************
1788 **
1789 ** Function         BTA_DmBleConfigLocalIcon
1790 **
1791 ** Description      set gap local icon
1792 **
1793 ** Parameters:      icon   - appearance value.
1794 **
1795 ** Returns          void
1796 **
1797 *******************************************************************************/
1798 void BTA_DmBleConfigLocalIcon(uint16_t icon)
1799 {
1800     tBTA_DM_API_LOCAL_ICON *p_msg;
1801
1802     if ((p_msg = (tBTA_DM_API_LOCAL_ICON *) osi_malloc(sizeof(tBTA_DM_API_LOCAL_ICON))) != NULL) {
1803         memset (p_msg, 0, sizeof(tBTA_DM_API_LOCAL_ICON));
1804
1805         p_msg->hdr.event = BTA_DM_API_LOCAL_ICON_EVT;
1806         p_msg->icon   = icon;
1807         bta_sys_sendmsg(p_msg);
1808     }
1809 }
1810
1811 /*******************************************************************************
1812 **
1813 ** Function         BTA_BleEnableAdvInstance
1814 **
1815 ** Description      This function enable a Multi-ADV instance with the specififed
1816 **                  adv parameters
1817 **
1818 ** Parameters       p_params: pointer to the adv parameter structure.
1819 **                  p_cback: callback function associated to this adv instance.
1820 **                  p_ref: reference data pointer to this adv instance.
1821 **
1822 ** Returns          BTA_SUCCESS if command started sucessfully; otherwise failure.
1823 **
1824 *******************************************************************************/
1825 void BTA_BleEnableAdvInstance (tBTA_BLE_ADV_PARAMS *p_params,
1826                                tBTA_BLE_MULTI_ADV_CBACK *p_cback,
1827                                void *p_ref)
1828 {
1829     ///This function just used for vendor debug
1830     tBTA_DM_API_BLE_MULTI_ADV_ENB    *p_msg;
1831     UINT16 len = sizeof(tBTA_BLE_ADV_PARAMS) + sizeof(tBTA_DM_API_BLE_MULTI_ADV_ENB);
1832
1833     APPL_TRACE_API ("BTA_BleEnableAdvInstance");
1834
1835     if ((p_msg = (tBTA_DM_API_BLE_MULTI_ADV_ENB *) osi_malloc(len)) != NULL) {
1836         memset(p_msg, 0, sizeof(tBTA_DM_API_BLE_MULTI_ADV_ENB));
1837
1838         p_msg->hdr.event     = BTA_DM_API_BLE_MULTI_ADV_ENB_EVT;
1839         p_msg->p_cback      = (void *)p_cback;
1840         if (p_params != NULL) {
1841             p_msg->p_params =  (void *)(p_msg + 1);
1842             memcpy(p_msg->p_params, p_params, sizeof(tBTA_BLE_ADV_PARAMS));
1843         }
1844         p_msg->p_ref        = p_ref;
1845
1846         bta_sys_sendmsg(p_msg);
1847     }
1848 }
1849
1850 /*******************************************************************************
1851 **
1852 ** Function         BTA_BleUpdateAdvInstParam
1853 **
1854 ** Description      This function update a Multi-ADV instance with the specififed
1855 **                  adv parameters.
1856 **
1857 ** Parameters       inst_id: Adv instance to update the parameter.
1858 **                  p_params: pointer to the adv parameter structure.
1859 **
1860 ** Returns          BTA_SUCCESS if command started sucessfully; otherwise failure.
1861 **
1862 *******************************************************************************/
1863 void BTA_BleUpdateAdvInstParam (UINT8 inst_id, tBTA_BLE_ADV_PARAMS *p_params)
1864 {
1865     ///This function just used for vendor debug
1866     tBTA_DM_API_BLE_MULTI_ADV_PARAM    *p_msg;
1867     UINT16      len = sizeof(tBTA_BLE_ADV_PARAMS) + sizeof(tBTA_DM_API_BLE_MULTI_ADV_PARAM);
1868
1869     APPL_TRACE_API ("BTA_BleUpdateAdvInstParam");
1870     if ((p_msg = (tBTA_DM_API_BLE_MULTI_ADV_PARAM *) osi_malloc(len)) != NULL) {
1871         memset(p_msg, 0, sizeof(tBTA_DM_API_BLE_MULTI_ADV_PARAM));
1872         p_msg->hdr.event     = BTA_DM_API_BLE_MULTI_ADV_PARAM_UPD_EVT;
1873         p_msg->inst_id        = inst_id;
1874         p_msg->p_params =  (void *)(p_msg + 1);
1875         memcpy(p_msg->p_params, p_params, sizeof(tBTA_BLE_ADV_PARAMS));
1876
1877         bta_sys_sendmsg(p_msg);
1878     }
1879 }
1880
1881 /*******************************************************************************
1882 **
1883 ** Function         BTA_BleCfgAdvInstData
1884 **
1885 ** Description      This function configure a Multi-ADV instance with the specififed
1886 **                  adv data or scan response data.
1887 **
1888 ** Parameter        inst_id: Adv instance to configure the adv data or scan response.
1889 **                  is_scan_rsp: is the data scan response or adv data.
1890 **                  data_mask: adv data type as bit mask.
1891 **                  p_data: pointer to the ADV data structure tBTA_BLE_ADV_DATA. This
1892 **                  memory space can not be freed until BTA_BLE_MULTI_ADV_DATA_EVT
1893 **                  is sent to application.
1894 **
1895 ** Returns          BTA_SUCCESS if command started sucessfully; otherwise failure.
1896 **
1897 *******************************************************************************/
1898 void BTA_BleCfgAdvInstData (UINT8 inst_id, BOOLEAN is_scan_rsp,
1899                             tBTA_BLE_AD_MASK data_mask,
1900                             tBTA_BLE_ADV_DATA *p_data)
1901 {
1902     ///This function just used for vendor debug
1903     tBTA_DM_API_BLE_MULTI_ADV_DATA    *p_msg;
1904     UINT16      len =  sizeof(tBTA_DM_API_BLE_MULTI_ADV_DATA) ;
1905
1906     APPL_TRACE_API ("BTA_BleCfgAdvInstData");
1907
1908     if ((p_msg = (tBTA_DM_API_BLE_MULTI_ADV_DATA *) osi_malloc(len)) != NULL) {
1909         memset(p_msg, 0, len);
1910         p_msg->hdr.event     = BTA_DM_API_BLE_MULTI_ADV_DATA_EVT;
1911         p_msg->inst_id      = inst_id;
1912         p_msg->is_scan_rsp  = is_scan_rsp;
1913         p_msg->data_mask     = data_mask;
1914         p_msg->p_data        = p_data;
1915
1916         bta_sys_sendmsg(p_msg);
1917     }
1918 }
1919
1920 /*******************************************************************************
1921 **
1922 ** Function         BTA_BleDisableAdvInstance
1923 **
1924 ** Description      This function disable a Multi-ADV instance.
1925 **
1926 ** Parameter        inst_id: instance ID to disable.
1927 **
1928 ** Returns          BTA_SUCCESS if command started sucessfully; otherwise failure.
1929 **
1930 *******************************************************************************/
1931 void BTA_BleDisableAdvInstance (UINT8  inst_id)     //this function just used for vendor debug
1932 {
1933     tBTA_DM_API_BLE_MULTI_ADV_DISABLE    *p_msg;
1934
1935     APPL_TRACE_API ("BTA_BleDisableAdvInstance: %d", inst_id);
1936     if ((p_msg = (tBTA_DM_API_BLE_MULTI_ADV_DISABLE *)
1937                  osi_malloc(sizeof(tBTA_DM_API_BLE_MULTI_ADV_DISABLE))) != NULL) {
1938         memset(p_msg, 0, sizeof(tBTA_DM_API_BLE_MULTI_ADV_DISABLE));
1939         p_msg->hdr.event    = BTA_DM_API_BLE_MULTI_ADV_DISABLE_EVT;
1940         p_msg->inst_id      = inst_id;
1941         bta_sys_sendmsg(p_msg);
1942     }
1943 }
1944
1945 /*******************************************************************************
1946 **
1947 ** Function         BTA_DmBleCfgFilterCondition
1948 **
1949 ** Description      This function is called to configure the adv data payload filter
1950 **                  condition.
1951 **
1952 ** Parameters       action: to read/write/clear
1953 **                  cond_type: filter condition type
1954 **                  filt_index - Filter index
1955 **                  p_cond: filter condition parameter
1956 **                  p_cmpl_back - Command completed callback
1957 **                  ref_value - Reference value
1958 **
1959 ** Returns          void
1960 **
1961 *******************************************************************************/
1962 void BTA_DmBleCfgFilterCondition(tBTA_DM_BLE_SCAN_COND_OP action,
1963                                  tBTA_DM_BLE_PF_COND_TYPE cond_type,
1964                                  tBTA_DM_BLE_PF_FILT_INDEX filt_index,
1965                                  tBTA_DM_BLE_PF_COND_PARAM *p_cond,
1966                                  tBTA_DM_BLE_PF_CFG_CBACK *p_cmpl_cback,
1967                                  tBTA_DM_BLE_REF_VALUE ref_value)
1968 {
1969 #if BLE_ANDROID_CONTROLLER_SCAN_FILTER == TRUE
1970     tBTA_DM_API_CFG_FILTER_COND *p_msg;
1971     APPL_TRACE_API ("BTA_DmBleCfgFilterCondition: %d, %d", action, cond_type);
1972
1973     UINT16  len = sizeof(tBTA_DM_API_CFG_FILTER_COND) +
1974                   sizeof(tBTA_DM_BLE_PF_COND_PARAM);
1975     UINT8 *p;
1976
1977     if (NULL != p_cond) {
1978         switch (cond_type) {
1979         case BTA_DM_BLE_PF_SRVC_DATA_PATTERN:
1980         case BTA_DM_BLE_PF_MANU_DATA:
1981             /* Length of pattern and pattern mask and other elements in */
1982             /* tBTA_DM_BLE_PF_MANU_COND */
1983             len += ((p_cond->manu_data.data_len) * 2) +
1984                    sizeof(UINT16) + sizeof(UINT16) + sizeof(UINT8);
1985             break;
1986
1987         case BTA_DM_BLE_PF_LOCAL_NAME:
1988             len += ((p_cond->local_name.data_len) + sizeof(UINT8));
1989             break;
1990
1991         case BTM_BLE_PF_SRVC_UUID:
1992         case BTM_BLE_PF_SRVC_SOL_UUID:
1993             len += sizeof(tBLE_BD_ADDR) + sizeof(tBTA_DM_BLE_PF_COND_MASK);
1994             break;
1995
1996         default:
1997             break;
1998         }
1999     }
2000
2001     if ((p_msg = (tBTA_DM_API_CFG_FILTER_COND *) osi_malloc(len)) != NULL) {
2002         memset (p_msg, 0, len);
2003
2004         p_msg->hdr.event        = BTA_DM_API_CFG_FILTER_COND_EVT;
2005         p_msg->action           = action;
2006         p_msg->cond_type        = cond_type;
2007         p_msg->filt_index       = filt_index;
2008         p_msg->p_filt_cfg_cback = p_cmpl_cback;
2009         p_msg->ref_value        = ref_value;
2010         if (p_cond) {
2011             p_msg->p_cond_param = (tBTA_DM_BLE_PF_COND_PARAM *)(p_msg + 1);
2012             memcpy(p_msg->p_cond_param, p_cond, sizeof(tBTA_DM_BLE_PF_COND_PARAM));
2013
2014             p = (UINT8 *)(p_msg->p_cond_param + 1);
2015
2016             if (cond_type == BTA_DM_BLE_PF_SRVC_DATA_PATTERN ||
2017                     cond_type == BTA_DM_BLE_PF_MANU_DATA) {
2018                 p_msg->p_cond_param->manu_data.p_pattern = p;
2019                 p_msg->p_cond_param->manu_data.data_len = p_cond->manu_data.data_len;
2020                 memcpy(p_msg->p_cond_param->manu_data.p_pattern, p_cond->manu_data.p_pattern,
2021                        p_cond->manu_data.data_len);
2022                 p += p_cond->manu_data.data_len;
2023
2024                 if (cond_type == BTA_DM_BLE_PF_MANU_DATA) {
2025                     p_msg->p_cond_param->manu_data.company_id_mask =
2026                         p_cond->manu_data.company_id_mask;
2027                     if ( p_cond->manu_data.p_pattern_mask != NULL) {
2028                         p_msg->p_cond_param->manu_data.p_pattern_mask = p;
2029                         memcpy(p_msg->p_cond_param->manu_data.p_pattern_mask,
2030                                p_cond->manu_data.p_pattern_mask, p_cond->manu_data.data_len);
2031                     }
2032                 }
2033             } else if (cond_type == BTA_DM_BLE_PF_LOCAL_NAME) {
2034                 p_msg->p_cond_param->local_name.p_data = p;
2035                 p_msg->p_cond_param->local_name.data_len =
2036                     p_cond->local_name.data_len;
2037                 memcpy(p_msg->p_cond_param->local_name.p_data,
2038                        p_cond->local_name.p_data, p_cond->local_name.data_len);
2039             } else if ((cond_type == BTM_BLE_PF_SRVC_UUID
2040                         || cond_type == BTM_BLE_PF_SRVC_SOL_UUID)) {
2041                 if (p_cond->srvc_uuid.p_target_addr != NULL) {
2042                     p_msg->p_cond_param->srvc_uuid.p_target_addr = (tBLE_BD_ADDR *)(p);
2043                     p_msg->p_cond_param->srvc_uuid.p_target_addr->type =
2044                         p_cond->srvc_uuid.p_target_addr->type;
2045                     memcpy(p_msg->p_cond_param->srvc_uuid.p_target_addr->bda,
2046                            p_cond->srvc_uuid.p_target_addr->bda, BD_ADDR_LEN);
2047                     p = (UINT8 *)( p_msg->p_cond_param->srvc_uuid.p_target_addr + 1);
2048                 }
2049                 if (p_cond->srvc_uuid.p_uuid_mask) {
2050                     p_msg->p_cond_param->srvc_uuid.p_uuid_mask = (tBTA_DM_BLE_PF_COND_MASK *)p;
2051                     memcpy(p_msg->p_cond_param->srvc_uuid.p_uuid_mask,
2052                            p_cond->srvc_uuid.p_uuid_mask, sizeof(tBTA_DM_BLE_PF_COND_MASK));
2053                 }
2054             }
2055         }
2056
2057         bta_sys_sendmsg(p_msg);
2058     }
2059 #else
2060     UNUSED(action);
2061     UNUSED(cond_type);
2062     UNUSED(filt_index);
2063     UNUSED(p_cond);
2064     UNUSED(p_cmpl_cback);
2065     UNUSED(ref_value);
2066 #endif
2067 }
2068
2069 /*******************************************************************************
2070 **
2071 ** Function         BTA_DmBleScanFilterSetup
2072 **
2073 ** Description      This function is called to setup the adv data payload filter param
2074 **
2075 ** Parameters       p_target: enable the filter condition on a target device; if NULL
2076 **                  filt_index - Filter index
2077 **                  p_filt_params -Filter parameters
2078 **                  ref_value - Reference value
2079 **                  action - Add, delete or clear
2080 **                  p_cmpl_back - Command completed callback
2081 **
2082 ** Returns          void
2083 **
2084 *******************************************************************************/
2085 void BTA_DmBleScanFilterSetup(UINT8 action, tBTA_DM_BLE_PF_FILT_INDEX filt_index,
2086                               tBTA_DM_BLE_PF_FILT_PARAMS *p_filt_params,
2087                               tBLE_BD_ADDR *p_target,
2088                               tBTA_DM_BLE_PF_PARAM_CBACK *p_cmpl_cback,
2089                               tBTA_DM_BLE_REF_VALUE ref_value)
2090 {
2091 #if BLE_ANDROID_CONTROLLER_SCAN_FILTER == TRUE
2092     tBTA_DM_API_SCAN_FILTER_PARAM_SETUP *p_msg;
2093     APPL_TRACE_API ("BTA_DmBleScanFilterSetup: %d", action);
2094
2095     UINT16  len = sizeof(tBTA_DM_API_SCAN_FILTER_PARAM_SETUP) + sizeof(tBLE_BD_ADDR);
2096
2097     if ((p_msg = (tBTA_DM_API_SCAN_FILTER_PARAM_SETUP *) osi_malloc(len)) != NULL) {
2098         memset (p_msg, 0, len);
2099
2100         p_msg->hdr.event        = BTA_DM_API_SCAN_FILTER_SETUP_EVT;
2101         p_msg->action       = action;
2102         p_msg->filt_index = filt_index;
2103         if (p_filt_params) {
2104             memcpy(&p_msg->filt_params, p_filt_params, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
2105         }
2106         p_msg->p_filt_param_cback = p_cmpl_cback;
2107         p_msg->ref_value        = ref_value;
2108
2109         if (p_target) {
2110             p_msg->p_target = (tBLE_BD_ADDR *)(p_msg + 1);
2111             memcpy(p_msg->p_target, p_target, sizeof(tBLE_BD_ADDR));
2112         }
2113
2114         bta_sys_sendmsg(p_msg);
2115     }
2116 #else
2117     UNUSED(action);
2118     UNUSED(filt_index);
2119     UNUSED(p_filt_params);
2120     UNUSED(p_target);
2121     UNUSED(p_cmpl_cback);
2122     UNUSED(ref_value);
2123 #endif
2124 }
2125
2126 /*******************************************************************************
2127 **
2128 ** Function         BTA_DmBleGetEnergyInfo
2129 **
2130 ** Description      This function is called to obtain the energy info
2131 **
2132 ** Parameters       p_cmpl_cback - Command complete callback
2133 **
2134 ** Returns          void
2135 **
2136 *******************************************************************************/
2137 void BTA_DmBleGetEnergyInfo(tBTA_BLE_ENERGY_INFO_CBACK *p_cmpl_cback)
2138 {
2139     tBTA_DM_API_ENERGY_INFO *p_msg;
2140     APPL_TRACE_API ("BTA_DmBleGetEnergyInfo");
2141
2142     UINT16  len = sizeof(tBTA_DM_API_ENERGY_INFO) + sizeof(tBLE_BD_ADDR);
2143
2144     if ((p_msg = (tBTA_DM_API_ENERGY_INFO *) osi_malloc(len)) != NULL) {
2145         memset (p_msg, 0, len);
2146         p_msg->hdr.event        = BTA_DM_API_BLE_ENERGY_INFO_EVT;
2147         p_msg->p_energy_info_cback = p_cmpl_cback;
2148         bta_sys_sendmsg(p_msg);
2149     }
2150 }
2151
2152 /*******************************************************************************
2153 **
2154 ** Function         BTA_DmEnableScanFilter
2155 **
2156 ** Description      This function is called to enable the adv data payload filter
2157 **
2158 ** Parameters       action - enable or disable the APCF feature
2159 **                  p_cmpl_cback - Command completed callback
2160 **                  ref_value - Reference value
2161 **
2162 ** Returns          void
2163 **
2164 *******************************************************************************/
2165 void BTA_DmEnableScanFilter(UINT8 action, tBTA_DM_BLE_PF_STATUS_CBACK *p_cmpl_cback,
2166                             tBTA_DM_BLE_REF_VALUE ref_value)
2167 {
2168 #if BLE_ANDROID_CONTROLLER_SCAN_FILTER == TRUE
2169     tBTA_DM_API_ENABLE_SCAN_FILTER *p_msg;
2170     APPL_TRACE_API ("BTA_DmEnableScanFilter: %d\n", action);
2171
2172     UINT16  len = sizeof(tBTA_DM_API_ENABLE_SCAN_FILTER) + sizeof(tBLE_BD_ADDR);
2173
2174     if ((p_msg = (tBTA_DM_API_ENABLE_SCAN_FILTER *) osi_malloc(len)) != NULL) {
2175         memset (p_msg, 0, len);
2176
2177         p_msg->hdr.event        = BTA_DM_API_SCAN_FILTER_ENABLE_EVT;
2178         p_msg->action       = action;
2179         p_msg->ref_value    = ref_value;
2180         p_msg->p_filt_status_cback = p_cmpl_cback;
2181
2182         bta_sys_sendmsg(p_msg);
2183     }
2184 #else
2185     UNUSED(action);
2186     UNUSED(p_cmpl_cback);
2187     UNUSED(ref_value);
2188 #endif
2189 }
2190
2191 /*******************************************************************************
2192 **
2193 ** Function         BTA_DmBleUpdateConnectionParams
2194 **
2195 ** Description      Update connection parameters, can only be used when connection is up.
2196 **
2197 ** Parameters:      bd_addr   - BD address of the peer
2198 **                  min_int   -     minimum connection interval, [0x0004~ 0x4000]
2199 **                  max_int   -     maximum connection interval, [0x0004~ 0x4000]
2200 **                  latency   -     slave latency [0 ~ 500]
2201 **                  timeout   -     supervision timeout [0x000a ~ 0xc80]
2202 **
2203 ** Returns          void
2204 **
2205 *******************************************************************************/
2206 void BTA_DmBleUpdateConnectionParams(BD_ADDR bd_addr, UINT16 min_int, UINT16 max_int,
2207                                      UINT16 latency, UINT16 timeout)
2208 {
2209     tBTA_DM_API_UPDATE_CONN_PARAM *p_msg;
2210
2211     if ((p_msg = (tBTA_DM_API_UPDATE_CONN_PARAM *) osi_malloc(sizeof(tBTA_DM_API_UPDATE_CONN_PARAM))) != NULL) {
2212         memset (p_msg, 0, sizeof(tBTA_DM_API_UPDATE_CONN_PARAM));
2213
2214         p_msg->hdr.event = BTA_DM_API_UPDATE_CONN_PARAM_EVT;
2215         bdcpy(p_msg->bd_addr, bd_addr);
2216         p_msg->min_int   = min_int;
2217         p_msg->max_int   = max_int;
2218         p_msg->latency   = latency;
2219         p_msg->timeout   = timeout;
2220         bta_sys_sendmsg(p_msg);
2221     }
2222 }
2223 /*******************************************************************************
2224 **
2225 ** Function         BTA_DmBleDisconnect
2226 **
2227 ** Description      Disconnect the ble connection, can only be used when connection is up.
2228 **
2229 ** Parameters:      bd_addr   - BD address of the peer
2230 **
2231 ** Returns          void
2232 **
2233 *******************************************************************************/
2234 void BTA_DmBleDisconnect(BD_ADDR bd_addr)
2235 {
2236     tBTA_DM_API_BLE_DISCONNECT *p_msg;
2237
2238     if ((p_msg = (tBTA_DM_API_BLE_DISCONNECT *) osi_malloc(sizeof(tBTA_DM_API_BLE_DISCONNECT))) != NULL) {
2239         memset (p_msg, 0, sizeof(tBTA_DM_API_BLE_DISCONNECT));
2240
2241         p_msg->hdr.event = BTA_DM_API_BLE_DISCONNECT_EVT;
2242         bdcpy(p_msg->remote_bda, bd_addr);
2243
2244         bta_sys_sendmsg(p_msg);
2245     }
2246 }
2247 /*******************************************************************************
2248 **
2249 ** Function         BTA_DmBleSetDataLength
2250 **
2251 ** Description      This function is to set maximum LE data packet size
2252 **
2253 ** Returns          void
2254 **
2255 **
2256 *******************************************************************************/
2257 void BTA_DmBleSetDataLength(BD_ADDR remote_device, UINT16 tx_data_length, tBTA_SET_PKT_DATA_LENGTH_CBACK *p_set_pkt_data_cback)
2258 {
2259     tBTA_DM_API_BLE_SET_DATA_LENGTH *p_msg;
2260
2261     if ((p_msg = (tBTA_DM_API_BLE_SET_DATA_LENGTH *)osi_malloc(sizeof(tBTA_DM_API_BLE_SET_DATA_LENGTH)))
2262             != NULL) {
2263         bdcpy(p_msg->remote_bda, remote_device);
2264         p_msg->hdr.event = BTA_DM_API_SET_DATA_LENGTH_EVT;
2265         p_msg->tx_data_length = tx_data_length;
2266         p_msg->p_set_pkt_data_cback = p_set_pkt_data_cback;
2267
2268         bta_sys_sendmsg(p_msg);
2269     }
2270 }
2271
2272 #endif
2273
2274 /*******************************************************************************
2275 **
2276 ** Function         BTA_DmSetEncryption
2277 **
2278 ** Description      This function is called to ensure that connection is
2279 **                  encrypted.  Should be called only on an open connection.
2280 **                  Typically only needed for connections that first want to
2281 **                  bring up unencrypted links, then later encrypt them.
2282 **
2283 ** Parameters:      bd_addr       - Address of the peer device
2284 **                  transport     - transport of the link to be encruypted
2285 **                  p_callback    - Pointer to callback function to indicat the
2286 **                                  link encryption status
2287 **                  sec_act       - This is the security action to indicate
2288 **                                  what kind of BLE security level is required for
2289 **                                  the BLE link if the BLE is supported
2290 **                                  Note: This parameter is ignored for the BR/EDR link
2291 **                                        or the BLE is not supported
2292 **
2293 ** Returns          void
2294 **
2295 *******************************************************************************/
2296 #if (SMP_INCLUDED == TRUE)
2297 void BTA_DmSetEncryption(BD_ADDR bd_addr, tBTA_TRANSPORT transport, tBTA_DM_ENCRYPT_CBACK *p_callback,
2298                          tBTA_DM_BLE_SEC_ACT sec_act)
2299 {
2300     tBTA_DM_API_SET_ENCRYPTION   *p_msg;
2301
2302     APPL_TRACE_API("BTA_DmSetEncryption"); //todo
2303     if ((p_msg = (tBTA_DM_API_SET_ENCRYPTION *) osi_malloc(sizeof(tBTA_DM_API_SET_ENCRYPTION))) != NULL) {
2304         memset(p_msg, 0, sizeof(tBTA_DM_API_SET_ENCRYPTION));
2305
2306         p_msg->hdr.event = BTA_DM_API_SET_ENCRYPTION_EVT;
2307
2308         memcpy(p_msg->bd_addr, bd_addr, BD_ADDR_LEN);
2309         p_msg->transport    = transport;
2310         p_msg->p_callback      = p_callback;
2311         p_msg->sec_act         = sec_act;
2312
2313         bta_sys_sendmsg(p_msg);
2314     }
2315 }
2316 #endif  ///SMP_INCLUDED == TRUE
2317
2318 /*******************************************************************************
2319 **
2320 ** Function         BTA_DmCloseACL
2321 **
2322 ** Description      This function force to close an ACL connection and remove the
2323 **                  device from the security database list of known devices.
2324 **
2325 ** Parameters:      bd_addr       - Address of the peer device
2326 **                  remove_dev    - remove device or not after link down
2327 **
2328 ** Returns          void
2329 **
2330 *******************************************************************************/
2331 void BTA_DmCloseACL(BD_ADDR bd_addr, BOOLEAN remove_dev, tBTA_TRANSPORT transport)
2332 {
2333     tBTA_DM_API_REMOVE_ACL   *p_msg;
2334
2335     APPL_TRACE_API("BTA_DmCloseACL");
2336
2337     if ((p_msg = (tBTA_DM_API_REMOVE_ACL *) osi_malloc(sizeof(tBTA_DM_API_REMOVE_ACL))) != NULL) {
2338         memset(p_msg, 0, sizeof(tBTA_DM_API_REMOVE_ACL));
2339
2340         p_msg->hdr.event = BTA_DM_API_REMOVE_ACL_EVT;
2341
2342         memcpy(p_msg->bd_addr, bd_addr, BD_ADDR_LEN);
2343         p_msg->remove_dev      = remove_dev;
2344         p_msg->transport       = transport;
2345
2346         bta_sys_sendmsg(p_msg);
2347     }
2348 }
2349
2350 #if BLE_INCLUDED == TRUE
2351 /*******************************************************************************
2352 **
2353 ** Function         BTA_DmBleObserve
2354 **
2355 ** Description      This procedure keep the device listening for advertising
2356 **                  events from a broadcast device.
2357 **
2358 ** Parameters       start: start or stop observe.
2359 **
2360 ** Returns          void
2361
2362 **
2363 ** Returns          void.
2364 **
2365 *******************************************************************************/
2366 extern void BTA_DmBleObserve(BOOLEAN start, UINT32 duration,
2367                              tBTA_DM_SEARCH_CBACK *p_results_cb,
2368                              tBTA_START_STOP_SCAN_CMPL_CBACK *p_start_stop_scan_cb)
2369 {
2370     tBTA_DM_API_BLE_OBSERVE   *p_msg;
2371
2372     APPL_TRACE_API("BTA_DmBleObserve:start = %d ", start);
2373
2374     if ((p_msg = (tBTA_DM_API_BLE_OBSERVE *) osi_malloc(sizeof(tBTA_DM_API_BLE_OBSERVE))) != NULL) {
2375         memset(p_msg, 0, sizeof(tBTA_DM_API_BLE_OBSERVE));
2376
2377         p_msg->hdr.event = BTA_DM_API_BLE_OBSERVE_EVT;
2378         p_msg->start = start;
2379         p_msg->duration = duration;
2380         p_msg->p_cback = p_results_cb;
2381         if (start){
2382             p_msg->p_start_scan_cback = p_start_stop_scan_cb;
2383         }
2384         else {
2385             p_msg->p_stop_scan_cback = p_start_stop_scan_cb;
2386         }
2387
2388         bta_sys_sendmsg(p_msg);
2389     }
2390 }
2391
2392 /*******************************************************************************
2393 **
2394 ** Function         BTA_DmBleScan
2395 **
2396 ** Description      This procedure keep the device listening for advertising
2397 **                  events from a broadcast device.
2398 **
2399 ** Parameters       start: start or stop scan.
2400 **
2401 ** Returns          void
2402
2403 **
2404 ** Returns          void.
2405 **
2406 *******************************************************************************/
2407 extern void BTA_DmBleScan(BOOLEAN start, UINT32 duration,
2408                              tBTA_DM_SEARCH_CBACK *p_results_cb,
2409                              tBTA_START_STOP_SCAN_CMPL_CBACK *p_start_stop_scan_cb)
2410 {
2411     tBTA_DM_API_BLE_SCAN   *p_msg;
2412
2413     APPL_TRACE_API("BTA_DmBleScan:start = %d ", start);
2414
2415     if ((p_msg = (tBTA_DM_API_BLE_SCAN *) osi_malloc(sizeof(tBTA_DM_API_BLE_SCAN))) != NULL) {
2416         memset(p_msg, 0, sizeof(tBTA_DM_API_BLE_SCAN));
2417
2418         p_msg->hdr.event = BTA_DM_API_BLE_SCAN_EVT;
2419         p_msg->start = start;
2420         p_msg->duration = duration;
2421         p_msg->p_cback = p_results_cb;
2422         if (start){
2423             p_msg->p_start_scan_cback = p_start_stop_scan_cb;
2424         }
2425         else {
2426             p_msg->p_stop_scan_cback = p_start_stop_scan_cb;
2427         }
2428
2429         bta_sys_sendmsg(p_msg);
2430     }
2431 }
2432
2433 /*******************************************************************************
2434 **
2435 ** Function         BTA_DmBleStopAdvertising
2436 **
2437 ** Description      This function set the random address for the APP
2438 **
2439 ** Parameters       void
2440 **
2441 ** Returns          void
2442 **
2443 **
2444 *******************************************************************************/
2445 extern void BTA_DmBleStopAdvertising(void)
2446 {
2447     BT_HDR   *p_msg;
2448
2449     APPL_TRACE_API("BTA_DmBleStopAdvertising\n");
2450
2451     if ((p_msg = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
2452         memset(p_msg, 0, sizeof(BT_HDR));
2453         p_msg->event = BTA_DM_API_BLE_STOP_ADV_EVT;
2454         bta_sys_sendmsg(p_msg);
2455     }
2456 }
2457
2458
2459 /*******************************************************************************
2460 **
2461 ** Function         BTA_DmSetRandAddress
2462 **
2463 ** Description      This function set the random address for the APP
2464 **
2465 ** Parameters       rand_addr: the random address whith should be setting
2466 **                  p_set_rand_addr_cback: complete callback
2467 ** Returns          void
2468 **
2469 **
2470 *******************************************************************************/
2471 extern void BTA_DmSetRandAddress(BD_ADDR rand_addr, tBTA_SET_RAND_ADDR_CBACK *p_set_rand_addr_cback)
2472 {
2473     tBTA_DM_APT_SET_DEV_ADDR *p_msg;
2474     APPL_TRACE_API("set the random address ");
2475     if ((p_msg = (tBTA_DM_APT_SET_DEV_ADDR *) osi_malloc(sizeof(tBTA_DM_APT_SET_DEV_ADDR))) != NULL) {
2476         memset(p_msg, 0, sizeof(tBTA_DM_APT_SET_DEV_ADDR));
2477         memcpy(p_msg->address, rand_addr, BD_ADDR_LEN);
2478         p_msg->hdr.event = BTA_DM_API_SET_RAND_ADDR_EVT;
2479         p_msg->addr_type = BLE_ADDR_RANDOM;
2480         p_msg->p_set_rand_addr_cback = p_set_rand_addr_cback;
2481         //start sent the msg to the bta system control moudle
2482         bta_sys_sendmsg(p_msg);
2483     }
2484 }
2485
2486 void BTA_DmClearRandAddress(void)
2487 {
2488     tBTA_DM_APT_CLEAR_ADDR *p_msg;
2489     if ((p_msg = (tBTA_DM_APT_CLEAR_ADDR *) osi_malloc(sizeof(tBTA_DM_APT_CLEAR_ADDR))) != NULL) {
2490         memset(p_msg, 0, sizeof(tBTA_DM_APT_CLEAR_ADDR));
2491         p_msg->hdr.event = BTA_DM_API_CLEAR_RAND_ADDR_EVT;
2492         bta_sys_sendmsg(p_msg);
2493     }
2494 }
2495
2496 /*******************************************************************************
2497 **
2498 ** Function         BTA_VendorInit
2499 **
2500 ** Description      This function initializes vendor specific
2501 **
2502 ** Returns          void
2503 **
2504 *******************************************************************************/
2505 void BTA_VendorInit (void)
2506 {
2507     APPL_TRACE_API("BTA_VendorInit");
2508 }
2509
2510 /*******************************************************************************
2511 **
2512 ** Function         BTA_VendorCleanup
2513 **
2514 ** Description      This function frees up Broadcom specific VS specific dynamic memory
2515 **
2516 ** Returns          void
2517 **
2518 *******************************************************************************/
2519 void BTA_VendorCleanup (void)
2520 {
2521     tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
2522     BTM_BleGetVendorCapabilities(&cmn_ble_vsc_cb);
2523
2524 #if (BLE_INCLUDED == TRUE && BLE_ANDROID_CONTROLLER_SCAN_FILTER == TRUE)
2525     btm_ble_adv_filter_cleanup();       // when BLE_VND_INCLUDED is false, this function will be ignore, so move it out of "if"
2526
2527 #if 0                                   //by TH, comment out temporarily
2528     if (cmn_ble_vsc_cb.max_filter > 0) {
2529         btm_ble_adv_filter_cleanup();
2530 #if BLE_PRIVACY_SPT == TRUE
2531         btm_ble_resolving_list_cleanup ();
2532 #endif
2533     }
2534 #endif
2535
2536     if (cmn_ble_vsc_cb.tot_scan_results_strg > 0) {
2537         btm_ble_batchscan_cleanup();
2538     }
2539 #endif
2540
2541     if (cmn_ble_vsc_cb.adv_inst_max > 0) {
2542         btm_ble_multi_adv_cleanup();
2543     }
2544 }
2545
2546 #endif