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