btif_dm.c revision 8a05724400085c56bdb2c4d7293226a7aa1ace37
1/******************************************************************************
2 *
3 *  Copyright (C) 2009-2012 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 *  Filename:      btif_dm.c
22 *
23 *  Description:   Contains Device Management (DM) related functionality
24 *
25 *
26 ***********************************************************************************/
27
28#define LOG_TAG "bt_btif_dm"
29
30#include <assert.h>
31#include <signal.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <sys/types.h>
36#include <unistd.h>
37
38#include <hardware/bluetooth.h>
39
40#include <cutils/properties.h>
41#include "gki.h"
42#include "btu.h"
43#include "btcore/include/bdaddr.h"
44#include "bta_api.h"
45#include "btif_api.h"
46#include "btif_util.h"
47#include "btif_dm.h"
48#include "btif_storage.h"
49#include "btif_hh.h"
50#include "btif_config.h"
51#include "btif_sdp.h"
52
53#include "bta_gatt_api.h"
54#include "include/stack_config.h"
55
56#include "osi/include/log.h"
57#include "osi/include/allocator.h"
58
59/******************************************************************************
60**  Device specific workarounds
61******************************************************************************/
62
63/**
64 * The devices below have proven problematic during the pairing process, often
65 * requiring multiple retries to complete pairing. To avoid degrading the user
66 * experience for other devices, explicitely blacklist troubled devices here.
67 */
68static const UINT8 blacklist_pairing_retries[][3] = {
69    {0x9C, 0xDF, 0x03} // BMW car kits (Harman/Becker)
70};
71
72BOOLEAN blacklistPairingRetries(BD_ADDR bd_addr)
73{
74    const unsigned blacklist_size = sizeof(blacklist_pairing_retries)
75        / sizeof(blacklist_pairing_retries[0]);
76    for (unsigned i = 0; i != blacklist_size; ++i)
77    {
78        if (blacklist_pairing_retries[i][0] == bd_addr[0] &&
79            blacklist_pairing_retries[i][1] == bd_addr[1] &&
80            blacklist_pairing_retries[i][2] == bd_addr[2])
81            return TRUE;
82    }
83    return FALSE;
84}
85
86/******************************************************************************
87**  Constants & Macros
88******************************************************************************/
89
90#define COD_UNCLASSIFIED ((0x1F) << 8)
91#define COD_HID_KEYBOARD                    0x0540
92#define COD_HID_POINTING                    0x0580
93#define COD_HID_COMBO                       0x05C0
94#define COD_HID_MAJOR                       0x0500
95#define COD_AV_HEADSETS                     0x0404
96#define COD_AV_HANDSFREE                    0x0408
97#define COD_AV_HEADPHONES                   0x0418
98#define COD_AV_PORTABLE_AUDIO               0x041C
99#define COD_AV_HIFI_AUDIO                   0x0428
100
101
102#define BTIF_DM_DEFAULT_INQ_MAX_RESULTS     0
103#define BTIF_DM_DEFAULT_INQ_MAX_DURATION    10
104#define BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING 2
105
106#define NUM_TIMEOUT_RETRIES                 5
107
108#define PROPERTY_PRODUCT_MODEL "ro.product.model"
109#define DEFAULT_LOCAL_NAME_MAX  31
110#if (DEFAULT_LOCAL_NAME_MAX > BTM_MAX_LOC_BD_NAME_LEN)
111    #error "default btif local name size exceeds stack supported length"
112#endif
113
114#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
115#define BTIF_DM_INTERLEAVE_DURATION_BR_ONE    2
116#define BTIF_DM_INTERLEAVE_DURATION_LE_ONE    2
117#define BTIF_DM_INTERLEAVE_DURATION_BR_TWO    3
118#define BTIF_DM_INTERLEAVE_DURATION_LE_TWO    4
119#endif
120
121#define MAX_SDP_BL_ENTRIES 3
122
123#define BOND_TYPE_UNKNOWN     0
124#define BOND_TYPE_PERSISTENT  1
125#define BOND_TYPE_TEMPORARY   2
126
127#define ENCRYPTED_BREDR       2
128#define ENCRYPTED_LE          4
129
130typedef struct
131{
132    bt_bond_state_t state;
133    BD_ADDR bd_addr;
134    UINT8 bond_type;
135    UINT8 pin_code_len;
136    UINT8 is_ssp;
137    UINT8 auth_req;
138    UINT8 io_cap;
139    UINT8 autopair_attempts;
140    UINT8 timeout_retries;
141    UINT8 is_local_initiated;
142    UINT8 sdp_attempts;
143#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
144    BOOLEAN is_le_only;
145    BOOLEAN is_le_nc; /* LE Numeric comparison */
146    BD_ADDR static_bdaddr;
147    btif_dm_ble_cb_t ble;
148#endif
149} btif_dm_pairing_cb_t;
150
151
152typedef struct
153{
154    UINT8       ir[BT_OCTET16_LEN];
155    UINT8       irk[BT_OCTET16_LEN];
156    UINT8       dhk[BT_OCTET16_LEN];
157}btif_dm_local_key_id_t;
158
159typedef struct
160{
161    BOOLEAN                 is_er_rcvd;
162    UINT8                   er[BT_OCTET16_LEN];
163    BOOLEAN                 is_id_keys_rcvd;
164    btif_dm_local_key_id_t  id_keys;  /* ID kyes */
165
166}btif_dm_local_key_cb_t;
167
168typedef struct
169{
170    BD_ADDR bd_addr;
171    BD_NAME bd_name;
172} btif_dm_remote_name_t;
173
174typedef struct
175{
176    BT_OCTET16 sp_c;
177    BT_OCTET16 sp_r;
178    BD_ADDR  oob_bdaddr;  /* peer bdaddr*/
179} btif_dm_oob_cb_t;
180
181typedef struct
182{
183    bt_bdaddr_t  bdaddr;
184    UINT8        transport; /* 0=Unknown, 1=BR/EDR, 2=LE */
185} btif_dm_create_bond_cb_t;
186
187typedef struct
188{
189    uint8_t  status;
190    uint8_t  ctrl_state;
191    uint64_t tx_time;
192    uint64_t rx_time;
193    uint64_t idle_time;
194    uint64_t energy_used;
195} btif_activity_energy_info_cb_t;
196
197typedef struct
198{
199    unsigned int   manufact_id;
200}skip_sdp_entry_t;
201
202#define BTA_SERVICE_ID_TO_SERVICE_MASK(id)       (1 << (id))
203
204#define MAX_SDP_BL_ENTRIES 3
205#define UUID_HUMAN_INTERFACE_DEVICE "00001124-0000-1000-8000-00805f9b34fb"
206
207static skip_sdp_entry_t sdp_blacklist[] = {{76}}; //Apple Mouse and Keyboard
208
209/* This flag will be true if HCI_Inquiry is in progress */
210static BOOLEAN btif_dm_inquiry_in_progress = FALSE;
211
212
213
214/************************************************************************************
215**  Static variables
216************************************************************************************/
217static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'};
218
219/******************************************************************************
220**  Static functions
221******************************************************************************/
222static btif_dm_pairing_cb_t pairing_cb;
223static btif_dm_oob_cb_t     oob_cb;
224static void btif_dm_generic_evt(UINT16 event, char* p_param);
225static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport);
226static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name);
227static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
228                                          DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type);
229#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
230static btif_dm_local_key_cb_t ble_local_key_cb;
231static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif);
232static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl);
233static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req);
234static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF *p_notif_req) ;
235#endif
236
237static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
238                                           tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
239                                           tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status);
240
241static char* btif_get_default_local_name();
242/******************************************************************************
243**  Externs
244******************************************************************************/
245extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
246extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
247extern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
248extern bt_status_t btif_av_sink_execute_service(BOOLEAN b_enable);
249extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
250extern bt_status_t btif_hf_client_execute_service(BOOLEAN b_enable);
251extern bt_status_t btif_sdp_execute_service(BOOLEAN b_enable);
252extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
253extern void bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16);
254
255
256/******************************************************************************
257**  Functions
258******************************************************************************/
259
260static void btif_dm_data_copy(uint16_t event, char *dst, char *src)
261{
262    tBTA_DM_SEC *dst_dm_sec = (tBTA_DM_SEC*)dst;
263    tBTA_DM_SEC *src_dm_sec = (tBTA_DM_SEC*)src;
264
265    if (!src_dm_sec)
266        return;
267
268    assert(dst_dm_sec);
269    memcpy(dst_dm_sec, src_dm_sec, sizeof(tBTA_DM_SEC));
270
271    if (event == BTA_DM_BLE_KEY_EVT)
272    {
273        dst_dm_sec->ble_key.p_key_value = osi_malloc(sizeof(tBTM_LE_KEY_VALUE));
274        assert(src_dm_sec->ble_key.p_key_value);
275        assert(dst_dm_sec->ble_key.p_key_value);
276        memcpy(dst_dm_sec->ble_key.p_key_value, src_dm_sec->ble_key.p_key_value, sizeof(tBTM_LE_KEY_VALUE));
277    }
278}
279
280static void btif_dm_data_free(uint16_t event, tBTA_DM_SEC *dm_sec)
281{
282    if (event == BTA_DM_BLE_KEY_EVT)
283        osi_free(dm_sec->ble_key.p_key_value);
284}
285
286bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
287                                                BOOLEAN b_enable)
288{
289    BTIF_TRACE_DEBUG("%s service_id: %d", __FUNCTION__, service_id);
290    /* Check the service_ID and invoke the profile's BT state changed API */
291    switch (service_id)
292    {
293         case BTA_HFP_SERVICE_ID:
294         case BTA_HSP_SERVICE_ID:
295         {
296              btif_hf_execute_service(b_enable);
297         }break;
298         case BTA_A2DP_SOURCE_SERVICE_ID:
299         {
300              btif_av_execute_service(b_enable);
301         }break;
302         case BTA_A2DP_SINK_SERVICE_ID:
303         {
304            btif_av_sink_execute_service(b_enable);
305         }break;
306         case BTA_HID_SERVICE_ID:
307         {
308              btif_hh_execute_service(b_enable);
309         }break;
310         case BTA_HFP_HS_SERVICE_ID:
311         {
312             btif_hf_client_execute_service(b_enable);
313         }break;
314         case BTA_SDP_SERVICE_ID:
315         {
316             btif_sdp_execute_service(b_enable);
317         }break;
318         default:
319              BTIF_TRACE_ERROR("%s: Unknown service being enabled", __FUNCTION__);
320              return BT_STATUS_FAIL;
321    }
322    return BT_STATUS_SUCCESS;
323}
324
325/*******************************************************************************
326**
327** Function         check_eir_remote_name
328**
329** Description      Check if remote name is in the EIR data
330**
331** Returns          TRUE if remote name found
332**                  Populate p_remote_name, if provided and remote name found
333**
334*******************************************************************************/
335static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
336                            UINT8 *p_remote_name, UINT8 *p_remote_name_len)
337{
338    UINT8 *p_eir_remote_name = NULL;
339    UINT8 remote_name_len = 0;
340
341    /* Check EIR for remote name and services */
342    if (p_search_data->inq_res.p_eir)
343    {
344        p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
345                BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
346        if (!p_eir_remote_name)
347        {
348            p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
349                    BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
350        }
351
352        if (p_eir_remote_name)
353        {
354            if (remote_name_len > BD_NAME_LEN)
355                remote_name_len = BD_NAME_LEN;
356
357            if (p_remote_name && p_remote_name_len)
358            {
359                memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
360                *(p_remote_name + remote_name_len) = 0;
361                *p_remote_name_len = remote_name_len;
362            }
363
364            return TRUE;
365        }
366    }
367
368    return FALSE;
369
370}
371
372/*******************************************************************************
373**
374** Function         check_cached_remote_name
375**
376** Description      Check if remote name is in the NVRAM cache
377**
378** Returns          TRUE if remote name found
379**                  Populate p_remote_name, if provided and remote name found
380**
381*******************************************************************************/
382static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
383                                UINT8 *p_remote_name, UINT8 *p_remote_name_len)
384{
385    bt_bdname_t bdname;
386    bt_bdaddr_t remote_bdaddr;
387    bt_property_t prop_name;
388
389    /* check if we already have it in our btif_storage cache */
390    bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
391    BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
392                               sizeof(bt_bdname_t), &bdname);
393    if (btif_storage_get_remote_device_property(
394        &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
395    {
396        if (p_remote_name && p_remote_name_len)
397        {
398            strcpy((char *)p_remote_name, (char *)bdname.name);
399            *p_remote_name_len = strlen((char *)p_remote_name);
400        }
401        return TRUE;
402    }
403
404    return FALSE;
405}
406
407BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
408{
409    uint32_t    remote_cod;
410    bt_property_t prop_name;
411
412    /* check if we already have it in our btif_storage cache */
413    BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
414                               sizeof(uint32_t), &remote_cod);
415    if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
416    {
417        LOG_INFO("%s remote_cod = 0x%08x cod = 0x%08x", __func__, remote_cod, cod);
418        if ((remote_cod & 0x7ff) == cod)
419            return TRUE;
420    }
421
422    return FALSE;
423}
424
425BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
426{
427    uint32_t    remote_cod;
428    bt_property_t prop_name;
429
430    /* check if we already have it in our btif_storage cache */
431    BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
432                               sizeof(uint32_t), &remote_cod);
433    if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
434                                &prop_name) == BT_STATUS_SUCCESS)
435    {
436        BTIF_TRACE_DEBUG("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
437        if ((remote_cod & 0x700) == cod)
438            return TRUE;
439    }
440    return FALSE;
441}
442
443BOOLEAN check_hid_le(const bt_bdaddr_t *remote_bdaddr)
444{
445    uint32_t    remote_dev_type;
446    bt_property_t prop_name;
447
448    /* check if we already have it in our btif_storage cache */
449    BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE,
450                               sizeof(uint32_t), &remote_dev_type);
451    if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
452                                &prop_name) == BT_STATUS_SUCCESS)
453    {
454        if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE)
455        {
456            bdstr_t bdstr;
457            bdaddr_to_string(remote_bdaddr, bdstr, sizeof(bdstr));
458            if(btif_config_exist(bdstr, "HidAppId"))
459                return TRUE;
460        }
461    }
462    return FALSE;
463}
464
465/*****************************************************************************
466**
467** Function        check_sdp_bl
468**
469** Description     Checks if a given device is blacklisted to skip sdp
470**
471** Parameters     skip_sdp_entry
472**
473** Returns         TRUE if the device is present in blacklist, else FALSE
474**
475*******************************************************************************/
476BOOLEAN check_sdp_bl(const bt_bdaddr_t *remote_bdaddr)
477{
478    UINT16 manufacturer = 0;
479    UINT8 lmp_ver = 0;
480    UINT16 lmp_subver = 0;
481    bt_property_t prop_name;
482    bt_remote_version_t info;
483
484
485    if (remote_bdaddr == NULL)
486        return FALSE;
487
488/* fetch additional info about remote device used in iop query */
489    BTM_ReadRemoteVersion(*(BD_ADDR*)remote_bdaddr, &lmp_ver,
490                    &manufacturer, &lmp_subver);
491
492
493
494 /* if not available yet, try fetching from config database */
495    BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO,
496                            sizeof(bt_remote_version_t), &info);
497
498    if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
499                                              &prop_name) != BT_STATUS_SUCCESS)
500    {
501
502        return FALSE;
503    }
504    manufacturer = info.manufacturer;
505
506    for (int i = 0; i < MAX_SDP_BL_ENTRIES; i++)
507    {
508        if (manufacturer == sdp_blacklist[i].manufact_id)
509            return TRUE;
510    }
511    return FALSE;
512}
513
514
515static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
516{
517    // Send bonding state only once - based on outgoing/incoming we may receive duplicates
518    if ((pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING))
519    {
520        // Cross key pairing so send callback for static address
521        if (pairing_cb.static_bdaddr != NULL)
522            HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
523        return;
524    }
525
526    if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
527        state = BT_BOND_STATE_NONE;
528
529    BTIF_TRACE_DEBUG("%s: state=%d, prev_state=%d, sdp_attempts = %d", __func__,
530                      state, pairing_cb.state, pairing_cb.sdp_attempts);
531
532    HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
533
534    if (state == BT_BOND_STATE_BONDING)
535    {
536        pairing_cb.state = state;
537        bdcpy(pairing_cb.bd_addr, bd_addr->address);
538    } else {
539        if (!pairing_cb.sdp_attempts)
540            memset(&pairing_cb, 0, sizeof(pairing_cb));
541        else
542            BTIF_TRACE_DEBUG("%s: BR-EDR service discovery active", __func__);
543    }
544}
545
546/* store remote version in bt config to always have access
547   to it post pairing*/
548static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
549{
550    bt_property_t property;
551    UINT8 lmp_ver = 0;
552    UINT16 lmp_subver = 0;
553    UINT16 mfct_set = 0;
554    tBTM_STATUS btm_status;
555    bt_remote_version_t info;
556    bt_status_t status;
557    bdstr_t bdstr;
558
559    btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
560                          &mfct_set, &lmp_subver);
561
562    LOG_DEBUG("remote version info [%s]: %x, %x, %x", bdaddr_to_string(p_bd, bdstr, sizeof(bdstr)),
563               lmp_ver, mfct_set, lmp_subver);
564
565    if (btm_status == BTM_SUCCESS)
566    {
567        // Always update cache to ensure we have availability whenever BTM API is not populated
568        info.manufacturer = mfct_set;
569        info.sub_ver = lmp_subver;
570        info.version = lmp_ver;
571        BTIF_STORAGE_FILL_PROPERTY(&property,
572                            BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
573                            &info);
574        status = btif_storage_set_remote_device_property(p_bd, &property);
575        ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
576    }
577}
578
579
580static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
581                                          DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
582{
583    int num_properties = 0;
584    bt_property_t properties[3];
585    bt_bdaddr_t bdaddr;
586    bt_status_t status;
587    UINT32 cod;
588    bt_device_type_t dev_type;
589
590    memset(properties, 0, sizeof(properties));
591    bdcpy(bdaddr.address, bd_addr);
592
593    /* remote name */
594    if (strlen((const char *) bd_name))
595    {
596        BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
597                            BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
598        status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
599        ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
600        num_properties++;
601    }
602
603    /* class of device */
604    cod = devclass2uint(dev_class);
605    BTIF_TRACE_DEBUG("%s cod is 0x%06x", __func__, cod);
606    if ( cod == 0) {
607       /* Try to retrieve cod from storage */
608        BTIF_TRACE_DEBUG("%s cod is 0, checking cod from storage", __func__);
609        BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
610            BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
611        status = btif_storage_get_remote_device_property(&bdaddr, &properties[num_properties]);
612        BTIF_TRACE_DEBUG("%s cod retrieved from storage is 0x%06x", __func__, cod);
613        if ( cod == 0) {
614            BTIF_TRACE_DEBUG("%s cod is again 0, set as unclassified", __func__);
615            cod = COD_UNCLASSIFIED;
616        }
617    }
618
619    BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
620                        BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
621    status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
622    ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
623    num_properties++;
624
625    /* device type */
626    bt_property_t prop_name;
627    uint8_t remote_dev_type;
628    BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_TYPE_OF_DEVICE,
629                                sizeof(uint8_t), &remote_dev_type);
630    if (btif_storage_get_remote_device_property(&bdaddr, &prop_name) == BT_STATUS_SUCCESS)
631         dev_type = remote_dev_type | device_type;
632    else
633         dev_type = device_type;
634
635    BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
636                        BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
637    status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
638    ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
639    num_properties++;
640
641    HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
642                     status, &bdaddr, num_properties, properties);
643}
644
645/*******************************************************************************
646**
647** Function         btif_dm_cb_hid_remote_name
648**
649** Description      Remote name callback for HID device. Called in btif context
650**                  Special handling for HID devices
651**
652** Returns          void
653**
654*******************************************************************************/
655static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
656{
657    BTIF_TRACE_DEBUG("%s: status=%d pairing_cb.state=%d", __FUNCTION__, p_remote_name->status, pairing_cb.state);
658    if (pairing_cb.state == BT_BOND_STATE_BONDING)
659    {
660        bt_bdaddr_t remote_bd;
661
662        bdcpy(remote_bd.address, pairing_cb.bd_addr);
663
664        if (p_remote_name->status == BTM_SUCCESS)
665        {
666            bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
667        }
668        else
669            bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
670    }
671}
672
673/*******************************************************************************
674**
675** Function         btif_dm_cb_create_bond
676**
677** Description      Create bond initiated from the BTIF thread context
678**                  Special handling for HID devices
679**
680** Returns          void
681**
682*******************************************************************************/
683static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport)
684{
685    BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
686    bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
687
688#if BLE_INCLUDED == TRUE
689    int device_type;
690    int addr_type;
691    bdstr_t bdstr;
692    bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr));
693    if (transport == BT_TRANSPORT_LE)
694    {
695        if (!btif_config_get_int((char const *)&bdstr,"DevType", &device_type))
696        {
697            btif_config_set_int(bdstr, "DevType", BT_DEVICE_TYPE_BLE);
698        }
699        if (btif_storage_get_remote_addr_type(bd_addr, &addr_type) != BT_STATUS_SUCCESS)
700        {
701            btif_storage_set_remote_addr_type(bd_addr, BLE_ADDR_PUBLIC);
702        }
703    }
704    if((btif_config_get_int((char const *)&bdstr,"DevType", &device_type) &&
705       (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
706       (device_type & BT_DEVICE_TYPE_BLE) == BT_DEVICE_TYPE_BLE) || (transport == BT_TRANSPORT_LE))
707    {
708        BTA_DmAddBleDevice(bd_addr->address, addr_type, device_type);
709    }
710#endif
711
712#if BLE_INCLUDED == TRUE
713    if(is_hid && (device_type & BT_DEVICE_TYPE_BLE) == 0)
714#else
715    if(is_hid)
716#endif
717    {
718        int status;
719        status = btif_hh_connect(bd_addr);
720        if(status != BT_STATUS_SUCCESS)
721            bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
722    }
723    else
724    {
725        BTA_DmBondByTransport((UINT8 *)bd_addr->address, transport);
726    }
727    /*  Track  originator of bond creation  */
728    pairing_cb.is_local_initiated = TRUE;
729
730}
731
732/*******************************************************************************
733**
734** Function         btif_dm_cb_remove_bond
735**
736** Description      remove bond initiated from the BTIF thread context
737**                  Special handling for HID devices
738**
739** Returns          void
740**
741*******************************************************************************/
742void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
743{
744     /*special handling for HID devices */
745     /*  VUP needs to be sent if its a HID Device. The HID HOST module will check if there
746     is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
747#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
748    if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
749#endif
750    {
751         BTIF_TRACE_DEBUG("%s: Removing HH device", __func__);
752         BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
753    }
754}
755
756/*******************************************************************************
757**
758** Function         btif_dm_get_connection_state
759**
760** Description      Returns whether the remote device is currently connected
761**                  and whether encryption is active for the connection
762**
763** Returns          0 if not connected; 1 if connected and > 1 if connection is
764**                  encrypted
765**
766*******************************************************************************/
767uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr)
768{
769    uint8_t *bda = (uint8_t*)bd_addr->address;
770    uint16_t rc = BTA_DmGetConnectionState(bda);
771
772    if (rc != 0)
773    {
774        uint8_t flags = 0;
775
776        BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_BR_EDR);
777        BTIF_TRACE_DEBUG("%s: security flags (BR/EDR)=0x%02x", __FUNCTION__, flags);
778        if (flags & BTM_SEC_FLAG_ENCRYPTED)
779            rc |= ENCRYPTED_BREDR;
780
781        BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_LE);
782        BTIF_TRACE_DEBUG("%s: security flags (LE)=0x%02x", __FUNCTION__, flags);
783        if (flags & BTM_SEC_FLAG_ENCRYPTED)
784            rc |= ENCRYPTED_LE;
785    }
786
787    return rc;
788}
789
790/*******************************************************************************
791**
792** Function         search_devices_copy_cb
793**
794** Description      Deep copy callback for search devices event
795**
796** Returns          void
797**
798*******************************************************************************/
799static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
800{
801    tBTA_DM_SEARCH *p_dest_data =  (tBTA_DM_SEARCH *) p_dest;
802    tBTA_DM_SEARCH *p_src_data =  (tBTA_DM_SEARCH *) p_src;
803
804    if (!p_src)
805        return;
806
807    BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
808    memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
809    switch (event)
810    {
811        case BTA_DM_INQ_RES_EVT:
812        {
813            if (p_src_data->inq_res.p_eir)
814            {
815                p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
816                memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
817            }
818        }
819        break;
820
821        case BTA_DM_DISC_RES_EVT:
822        {
823            if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
824            {
825                p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
826                memcpy(p_dest_data->disc_res.p_raw_data,
827                    p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
828            }
829        }
830        break;
831    }
832}
833
834static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
835{
836    tBTA_DM_SEARCH *p_dest_data =  (tBTA_DM_SEARCH *) p_dest;
837    tBTA_DM_SEARCH *p_src_data =  (tBTA_DM_SEARCH *) p_src;
838
839    if (!p_src)
840        return;
841    memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
842    switch (event)
843    {
844         case BTA_DM_DISC_RES_EVT:
845         {
846              if (p_src_data->disc_res.result == BTA_SUCCESS)
847              {
848                  if (p_src_data->disc_res.num_uuids > 0)
849                  {
850                       p_dest_data->disc_res.p_uuid_list =
851                                                        (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
852                       memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
853                              p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
854                       GKI_freebuf(p_src_data->disc_res.p_uuid_list);
855                  }
856                  if (p_src_data->disc_res.p_raw_data != NULL)
857                  {
858                      GKI_freebuf(p_src_data->disc_res.p_raw_data);
859                  }
860              }
861         } break;
862    }
863}
864/******************************************************************************
865**
866**  BTIF DM callback events
867**
868*****************************************************************************/
869
870/*******************************************************************************
871**
872** Function         btif_dm_pin_req_evt
873**
874** Description      Executes pin request event in btif context
875**
876** Returns          void
877**
878*******************************************************************************/
879static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
880{
881    bt_bdaddr_t bd_addr;
882    bt_bdname_t bd_name;
883    UINT32 cod;
884    bt_pin_code_t pin_code;
885    int dev_type;
886
887    /* Remote properties update */
888    if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
889    {
890        dev_type = BT_DEVICE_TYPE_BREDR;
891    }
892    btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
893                                  p_pin_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
894
895    bdcpy(bd_addr.address, p_pin_req->bd_addr);
896    memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
897
898    bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
899
900    cod = devclass2uint(p_pin_req->dev_class);
901
902    if (cod == 0) {
903        BTIF_TRACE_DEBUG("%s cod is 0, set as unclassified", __func__);
904        cod = COD_UNCLASSIFIED;
905    }
906
907    /* check for auto pair possiblity only if bond was initiated by local device */
908    if (pairing_cb.is_local_initiated)
909    {
910        if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
911            check_cod(&bd_addr, COD_AV_HANDSFREE) ||
912            check_cod(&bd_addr, COD_AV_HEADPHONES) ||
913            check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
914            check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
915            check_cod(&bd_addr, COD_HID_POINTING))
916        {
917            BTIF_TRACE_DEBUG("%s()cod matches for auto pair", __FUNCTION__);
918            /*  Check if this device can be auto paired  */
919            if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) &&
920                (pairing_cb.autopair_attempts == 0))
921            {
922                BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
923                pin_code.pin[0] = 0x30;
924                pin_code.pin[1] = 0x30;
925                pin_code.pin[2] = 0x30;
926                pin_code.pin[3] = 0x30;
927
928                pairing_cb.autopair_attempts++;
929                BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
930                return;
931            }
932        }
933        else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
934                 check_cod(&bd_addr, COD_HID_COMBO))
935        {
936            if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) &&
937               (pairing_cb.autopair_attempts == 0))
938            {
939                BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
940                pin_code.pin[0] = 0x30;
941                pin_code.pin[1] = 0x30;
942                pin_code.pin[2] = 0x30;
943                pin_code.pin[3] = 0x30;
944
945                pairing_cb.autopair_attempts++;
946                BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
947                return;
948            }
949        }
950    }
951    HAL_CBACK(bt_hal_cbacks, pin_request_cb,
952                     &bd_addr, &bd_name, cod);
953}
954
955/*******************************************************************************
956**
957** Function         btif_dm_ssp_cfm_req_evt
958**
959** Description      Executes SSP confirm request event in btif context
960**
961** Returns          void
962**
963*******************************************************************************/
964static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
965{
966    bt_bdaddr_t bd_addr;
967    bt_bdname_t bd_name;
968    UINT32 cod;
969    BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
970    int dev_type;
971
972    BTIF_TRACE_DEBUG("%s", __FUNCTION__);
973
974    /* Remote properties update */
975    if (!btif_get_device_type(p_ssp_cfm_req->bd_addr, &dev_type))
976    {
977        dev_type = BT_DEVICE_TYPE_BREDR;
978    }
979    btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
980                                  p_ssp_cfm_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
981
982    bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
983    memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
984
985    /* Set the pairing_cb based on the local & remote authentication requirements */
986    bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
987
988    /* if just_works and bonding bit is not set treat this as temporary */
989    if (p_ssp_cfm_req->just_works && !(p_ssp_cfm_req->loc_auth_req & BTM_AUTH_BONDS) &&
990        !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
991        !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
992        pairing_cb.bond_type = BOND_TYPE_TEMPORARY;
993    else
994        pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
995
996    pairing_cb.is_ssp = TRUE;
997
998    /* If JustWorks auto-accept */
999    if (p_ssp_cfm_req->just_works)
1000    {
1001        /* Pairing consent for JustWorks needed if:
1002         * 1. Incoming pairing is detected AND
1003         * 2. local IO capabilities are DisplayYesNo AND
1004         * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
1005         */
1006        if ((is_incoming) && ((p_ssp_cfm_req->loc_io_caps == 0x01) &&
1007                (p_ssp_cfm_req->rmt_io_caps == 0x00 || p_ssp_cfm_req->rmt_io_caps == 0x03)))
1008        {
1009            BTIF_TRACE_EVENT("%s: User consent needed for incoming pairing request. loc_io_caps: %d, rmt_io_caps: %d",
1010                __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
1011        }
1012        else
1013        {
1014            BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __FUNCTION__);
1015            btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
1016            return;
1017        }
1018    }
1019
1020    cod = devclass2uint(p_ssp_cfm_req->dev_class);
1021
1022    if (cod == 0) {
1023        LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
1024        cod = COD_UNCLASSIFIED;
1025    }
1026
1027    pairing_cb.sdp_attempts = 0;
1028    HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
1029                     (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
1030                     p_ssp_cfm_req->num_val);
1031}
1032
1033static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
1034{
1035    bt_bdaddr_t bd_addr;
1036    bt_bdname_t bd_name;
1037    UINT32 cod;
1038    int dev_type;
1039
1040    BTIF_TRACE_DEBUG("%s", __FUNCTION__);
1041
1042    /* Remote properties update */
1043    if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
1044    {
1045        dev_type = BT_DEVICE_TYPE_BREDR;
1046    }
1047    btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
1048                                  p_ssp_key_notif->dev_class, (tBT_DEVICE_TYPE) dev_type);
1049
1050    bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
1051    memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
1052
1053    bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1054    pairing_cb.is_ssp = TRUE;
1055    cod = devclass2uint(p_ssp_key_notif->dev_class);
1056
1057    if (cod == 0) {
1058        LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
1059        cod = COD_UNCLASSIFIED;
1060    }
1061
1062    HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
1063                     cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
1064                     p_ssp_key_notif->passkey);
1065}
1066/*******************************************************************************
1067**
1068** Function         btif_dm_auth_cmpl_evt
1069**
1070** Description      Executes authentication complete event in btif context
1071**
1072** Returns          void
1073**
1074*******************************************************************************/
1075static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
1076{
1077    /* Save link key, if not temporary */
1078    bt_bdaddr_t bd_addr;
1079    bt_status_t status = BT_STATUS_FAIL;
1080    bt_bond_state_t state = BT_BOND_STATE_NONE;
1081    BOOLEAN skip_sdp = FALSE;
1082
1083    BTIF_TRACE_DEBUG("%s: bond state=%d", __func__, pairing_cb.state);
1084
1085    bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
1086    if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
1087    {
1088        if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB)  || (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
1089            (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) || pairing_cb.bond_type == BOND_TYPE_PERSISTENT)
1090        {
1091            bt_status_t ret;
1092            BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, bond_type=%d",
1093                __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1094            ret = btif_storage_add_bonded_device(&bd_addr,
1095                                p_auth_cmpl->key, p_auth_cmpl->key_type,
1096                                pairing_cb.pin_code_len);
1097            ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
1098        }
1099        else
1100        {
1101            BTIF_TRACE_DEBUG("%s: Temporary key. Not storing. key_type=0x%x, bond_type=%d",
1102                __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1103            if(pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
1104            {
1105                BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
1106                        __FUNCTION__);
1107                bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1108                return;
1109            }
1110        }
1111    }
1112
1113    // Skip SDP for certain  HID Devices
1114    if (p_auth_cmpl->success)
1115    {
1116        btif_storage_set_remote_addr_type(&bd_addr, p_auth_cmpl->addr_type);
1117        btif_update_remote_properties(p_auth_cmpl->bd_addr,
1118                                      p_auth_cmpl->bd_name, NULL, p_auth_cmpl->dev_type);
1119        pairing_cb.timeout_retries = 0;
1120        status = BT_STATUS_SUCCESS;
1121        state = BT_BOND_STATE_BONDED;
1122        bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
1123
1124        if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr, COD_HID_MAJOR))
1125        {
1126            LOG_WARN("%s:skip SDP", __FUNCTION__);
1127            skip_sdp = TRUE;
1128        }
1129        if(!pairing_cb.is_local_initiated && skip_sdp)
1130        {
1131            bond_state_changed(status, &bd_addr, state);
1132
1133            LOG_WARN("%s: Incoming HID Connection",__FUNCTION__);
1134            bt_property_t prop;
1135            bt_bdaddr_t bd_addr;
1136            bt_uuid_t  uuid;
1137            char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE;
1138
1139            string_to_uuid(uuid_str, &uuid);
1140
1141            prop.type = BT_PROPERTY_UUIDS;
1142            prop.val = uuid.uu;
1143            prop.len = MAX_UUID_SIZE;
1144
1145            /* Send the event to the BTIF */
1146            HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1147                             BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1148        }
1149        else
1150        {
1151            /* Trigger SDP on the device */
1152            pairing_cb.sdp_attempts = 1;;
1153            /* If bonded due to cross-key, save the static address too*/
1154            if(pairing_cb.state == BT_BOND_STATE_BONDING &&
1155              (bdcmp(p_auth_cmpl->bd_addr, pairing_cb.bd_addr) != 0))
1156            {
1157                BTIF_TRACE_DEBUG("%s: bonding initiated due to cross key, adding static address",
1158                                 __func__);
1159                bdcpy(pairing_cb.static_bdaddr, p_auth_cmpl->bd_addr);
1160            }
1161
1162            if(btif_dm_inquiry_in_progress)
1163                btif_dm_cancel_discovery();
1164
1165            btif_dm_get_remote_services(&bd_addr);
1166        }
1167        // Do not call bond_state_changed_cb yet. Wait until remote service discovery is complete
1168    }
1169    else
1170    {
1171        // Map the HCI fail reason  to  bt status
1172        switch(p_auth_cmpl->fail_reason)
1173        {
1174            case HCI_ERR_PAGE_TIMEOUT:
1175                if (blacklistPairingRetries(bd_addr.address) && pairing_cb.timeout_retries)
1176                {
1177                    BTIF_TRACE_WARNING("%s() - Pairing timeout; retrying (%d) ...", __FUNCTION__, pairing_cb.timeout_retries);
1178                    --pairing_cb.timeout_retries;
1179                    btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
1180                    return;
1181                }
1182                /* Fall-through */
1183            case HCI_ERR_CONNECTION_TOUT:
1184                status =  BT_STATUS_RMT_DEV_DOWN;
1185                break;
1186
1187            case HCI_ERR_PAIRING_NOT_ALLOWED:
1188                status = BT_STATUS_AUTH_REJECTED;
1189                break;
1190
1191            case HCI_ERR_LMP_RESPONSE_TIMEOUT:
1192                status =  BT_STATUS_AUTH_FAILURE;
1193                break;
1194
1195            /* map the auth failure codes, so we can retry pairing if necessary */
1196            case HCI_ERR_AUTH_FAILURE:
1197            case HCI_ERR_KEY_MISSING:
1198                btif_storage_remove_bonded_device(&bd_addr);
1199            case HCI_ERR_HOST_REJECT_SECURITY:
1200            case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
1201            case HCI_ERR_UNIT_KEY_USED:
1202            case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
1203            case HCI_ERR_INSUFFCIENT_SECURITY:
1204            case HCI_ERR_PEER_USER:
1205            case HCI_ERR_UNSPECIFIED:
1206                BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d",
1207                    __FUNCTION__, p_auth_cmpl->fail_reason);
1208                if (pairing_cb.autopair_attempts  == 1)
1209                {
1210                    BTIF_TRACE_DEBUG("%s(): Adding device to blacklist ", __FUNCTION__);
1211
1212                    /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class  */
1213                    if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
1214                        check_cod(&bd_addr, COD_AV_HANDSFREE) ||
1215                        check_cod(&bd_addr, COD_AV_HEADPHONES) ||
1216                        check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
1217                        check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
1218                        check_cod(&bd_addr, COD_HID_POINTING))
1219                    {
1220                        btif_storage_add_device_to_autopair_blacklist (&bd_addr);
1221                    }
1222                    pairing_cb.autopair_attempts++;
1223
1224                    /* Create the Bond once again */
1225                    BTIF_TRACE_DEBUG("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
1226                    btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
1227                    return;
1228                }
1229                else
1230                {
1231                    /* if autopair attempts are more than 1, or not attempted */
1232                    status =  BT_STATUS_AUTH_FAILURE;
1233                }
1234                break;
1235
1236            default:
1237                status =  BT_STATUS_FAIL;
1238        }
1239        /* Special Handling for HID Devices */
1240        if (check_cod(&bd_addr, COD_HID_POINTING)) {
1241            /* Remove Device as bonded in nvram as authentication failed */
1242            BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __FUNCTION__);
1243            btif_storage_remove_bonded_device(&bd_addr);
1244        }
1245        bond_state_changed(status, &bd_addr, state);
1246    }
1247}
1248
1249/******************************************************************************
1250**
1251** Function         btif_dm_search_devices_evt
1252**
1253** Description      Executes search devices callback events in btif context
1254**
1255** Returns          void
1256**
1257******************************************************************************/
1258static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
1259{
1260    tBTA_DM_SEARCH *p_search_data;
1261    BTIF_TRACE_EVENT("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
1262
1263    switch (event)
1264    {
1265        case BTA_DM_DISC_RES_EVT:
1266        {
1267            p_search_data = (tBTA_DM_SEARCH *)p_param;
1268            /* Remote name update */
1269            if (strlen((const char *) p_search_data->disc_res.bd_name))
1270            {
1271                bt_property_t properties[1];
1272                bt_bdaddr_t bdaddr;
1273                bt_status_t status;
1274
1275                properties[0].type = BT_PROPERTY_BDNAME;
1276                properties[0].val = p_search_data->disc_res.bd_name;
1277                properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
1278                bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
1279
1280                status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
1281                ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
1282                HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1283                                 status, &bdaddr, 1, properties);
1284            }
1285            /* TODO: Services? */
1286        }
1287        break;
1288
1289        case BTA_DM_INQ_RES_EVT:
1290        {
1291            /* inquiry result */
1292            UINT32 cod;
1293            bt_bdname_t bdname;
1294            bt_bdaddr_t bdaddr;
1295            UINT8 remote_name_len;
1296            tBTA_SERVICE_MASK services = 0;
1297            bdstr_t bdstr;
1298
1299            p_search_data = (tBTA_DM_SEARCH *)p_param;
1300            bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
1301
1302            BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __FUNCTION__, bdaddr_to_string(&bdaddr, bdstr, sizeof(bdstr)),
1303#if (BLE_INCLUDED == TRUE)
1304                    p_search_data->inq_res.device_type);
1305#else
1306                    BT_DEVICE_TYPE_BREDR);
1307#endif
1308            bdname.name[0] = 0;
1309
1310            cod = devclass2uint (p_search_data->inq_res.dev_class);
1311
1312            if (cod == 0) {
1313                LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
1314                cod = COD_UNCLASSIFIED;
1315            }
1316
1317            if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1318                check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1319
1320            /* Check EIR for remote name and services */
1321            if (p_search_data->inq_res.p_eir)
1322            {
1323                BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
1324                BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
1325                /* TODO:  Get the service list and check to see which uuids we got and send it back to the client. */
1326            }
1327
1328
1329            {
1330                bt_property_t properties[5];
1331                bt_device_type_t dev_type;
1332                UINT8 addr_type;
1333                uint32_t num_properties = 0;
1334                bt_status_t status;
1335
1336                memset(properties, 0, sizeof(properties));
1337                /* BD_ADDR */
1338                BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1339                                    BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1340                num_properties++;
1341                /* BD_NAME */
1342                /* Don't send BDNAME if it is empty */
1343                if (bdname.name[0])
1344                {
1345                    BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1346                                               BT_PROPERTY_BDNAME,
1347                                               strlen((char *)bdname.name), &bdname);
1348                    num_properties++;
1349                }
1350
1351                /* DEV_CLASS */
1352                BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1353                                    BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1354                num_properties++;
1355                /* DEV_TYPE */
1356#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1357                /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1358                dev_type = p_search_data->inq_res.device_type;
1359                addr_type = p_search_data->inq_res.ble_addr_type;
1360#else
1361                dev_type = BT_DEVICE_TYPE_BREDR;
1362#endif
1363                BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1364                                    BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1365                num_properties++;
1366                /* RSSI */
1367                BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1368                                    BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1369                                    &(p_search_data->inq_res.rssi));
1370                num_properties++;
1371
1372                status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1373                ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
1374#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1375                status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
1376                if (( dev_type == BT_DEVICE_TYPE_DUMO)&&
1377                   (p_search_data->inq_res.flag & BTA_BLE_DMT_CONTROLLER_SPT) &&
1378                   (p_search_data->inq_res.flag & BTA_BLE_DMT_HOST_SPT))
1379                 {
1380                    btif_storage_set_dmt_support_type (&bdaddr, TRUE);
1381                 }
1382                ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1383#endif
1384                /* Callback to notify upper layer of device */
1385                HAL_CBACK(bt_hal_cbacks, device_found_cb,
1386                                 num_properties, properties);
1387            }
1388        }
1389        break;
1390
1391        case BTA_DM_INQ_CMPL_EVT:
1392        {
1393#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1394            tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1395            memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1396            BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1397                                     bte_scan_filt_param_cfg_evt, 0);
1398#endif
1399        }
1400        break;
1401        case BTA_DM_DISC_CMPL_EVT:
1402        {
1403            HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1404        }
1405        break;
1406        case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1407        {
1408           /* if inquiry is not in progress and we get a cancel event, then
1409            * it means we are done with inquiry, but remote_name fetches are in
1410            * progress
1411            *
1412            * if inquiry  is in progress, then we don't want to act on this cancel_cmpl_evt
1413            * but instead wait for the cancel_cmpl_evt via the Busy Level
1414            *
1415            */
1416           if (btif_dm_inquiry_in_progress == FALSE)
1417           {
1418#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1419               tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1420               memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1421               BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1422                                        bte_scan_filt_param_cfg_evt, 0);
1423#endif
1424               HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1425           }
1426        }
1427        break;
1428    }
1429}
1430
1431/*******************************************************************************
1432**
1433** Function         btif_dm_search_services_evt
1434**
1435** Description      Executes search services event in btif context
1436**
1437** Returns          void
1438**
1439*******************************************************************************/
1440static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1441{
1442    tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1443
1444    BTIF_TRACE_EVENT("%s:  event = %d", __FUNCTION__, event);
1445    switch (event)
1446    {
1447        case BTA_DM_DISC_RES_EVT:
1448        {
1449            bt_property_t prop;
1450            uint32_t i = 0;
1451            bt_bdaddr_t bd_addr;
1452            bt_status_t ret;
1453
1454            bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1455
1456            BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
1457                    p_data->disc_res.result, p_data->disc_res.services);
1458            if  ((p_data->disc_res.result != BTA_SUCCESS) &&
1459                 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1460                 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1461            {
1462                BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__);
1463                pairing_cb.sdp_attempts++;
1464                btif_dm_get_remote_services(&bd_addr);
1465                return;
1466            }
1467            prop.type = BT_PROPERTY_UUIDS;
1468            prop.len = 0;
1469            if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1470            {
1471                 prop.val = p_data->disc_res.p_uuid_list;
1472                 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1473                 for (i=0; i < p_data->disc_res.num_uuids; i++)
1474                 {
1475                      char temp[256];
1476                      uuid_to_string_legacy((bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i*MAX_UUID_SIZE)), temp);
1477                      LOG_INFO("%s index:%d uuid:%s", __func__, i, temp);
1478                 }
1479            }
1480
1481            /* onUuidChanged requires getBondedDevices to be populated.
1482            ** bond_state_changed needs to be sent prior to remote_device_property
1483            */
1484            if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1485                ((bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0) ||
1486                 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr) == 0)) &&
1487                  pairing_cb.sdp_attempts > 0)
1488            {
1489                 BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
1490                                   __FUNCTION__);
1491                 pairing_cb.sdp_attempts  = 0;
1492
1493                 // If bonding occured due to cross-key pairing, send bonding callback
1494                 // for static address now
1495                 if (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr) == 0)
1496                    bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1497
1498                 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1499            }
1500
1501            if (p_data->disc_res.num_uuids != 0)
1502            {
1503                /* Also write this to the NVRAM */
1504                ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1505                ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1506                /* Send the event to the BTIF */
1507                HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1508                                 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1509            }
1510        }
1511        break;
1512
1513        case BTA_DM_DISC_CMPL_EVT:
1514            /* fixme */
1515        break;
1516
1517#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1518        case BTA_DM_DISC_BLE_RES_EVT:
1519             BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__,
1520                                p_data->disc_ble_res.service.uu.uuid16);
1521             bt_uuid_t  uuid;
1522             int i = 0;
1523             int j = 15;
1524             if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1525             {
1526                BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__);
1527                bt_property_t prop;
1528                bt_bdaddr_t bd_addr;
1529                char temp[256];
1530                bt_status_t ret;
1531
1532                bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1533
1534                while(i < j )
1535                {
1536                    unsigned char c = uuid.uu[j];
1537                    uuid.uu[j] = uuid.uu[i];
1538                    uuid.uu[i] = c;
1539                    i++;
1540                    j--;
1541                }
1542
1543                uuid_to_string_legacy(&uuid, temp);
1544                LOG_INFO("%s uuid:%s", __func__, temp);
1545
1546                bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1547                prop.type = BT_PROPERTY_UUIDS;
1548                prop.val = uuid.uu;
1549                prop.len = MAX_UUID_SIZE;
1550
1551                /* Also write this to the NVRAM */
1552                ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1553                ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1554
1555                /* Send the event to the BTIF */
1556                HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1557                                 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1558
1559            }
1560        break;
1561#endif /* BLE_INCLUDED */
1562
1563        default:
1564        {
1565            ASSERTC(0, "unhandled search services event", event);
1566        }
1567        break;
1568    }
1569}
1570
1571/*******************************************************************************
1572**
1573** Function         btif_dm_remote_service_record_evt
1574**
1575** Description      Executes search service record event in btif context
1576**
1577** Returns          void
1578**
1579*******************************************************************************/
1580static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1581{
1582    tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1583
1584    BTIF_TRACE_EVENT("%s:  event = %d", __FUNCTION__, event);
1585    switch (event)
1586    {
1587        case BTA_DM_DISC_RES_EVT:
1588        {
1589            bt_service_record_t rec;
1590            bt_property_t prop;
1591            bt_bdaddr_t bd_addr;
1592
1593            memset(&rec, 0, sizeof(bt_service_record_t));
1594            bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1595
1596            BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
1597                    p_data->disc_res.result, p_data->disc_res.services);
1598            prop.type = BT_PROPERTY_SERVICE_RECORD;
1599            prop.val = (void*)&rec;
1600            prop.len = sizeof(rec);
1601
1602            /* disc_res.result is overloaded with SCN. Cannot check result */
1603            p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1604            /* TODO: Get the UUID as well */
1605            rec.channel = p_data->disc_res.result - 3;
1606            /* TODO: Need to get the service name using p_raw_data */
1607            rec.name[0] = 0;
1608
1609            HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1610                             BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1611        }
1612        break;
1613
1614        default:
1615        {
1616           ASSERTC(0, "unhandled remote service record event", event);
1617        }
1618        break;
1619    }
1620}
1621
1622/*******************************************************************************
1623**
1624** Function         btif_dm_upstreams_cback
1625**
1626** Description      Executes UPSTREAMS events in btif context
1627**
1628** Returns          void
1629**
1630*******************************************************************************/
1631static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1632{
1633    tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1634    tBTA_SERVICE_MASK service_mask;
1635    uint32_t i;
1636    bt_bdaddr_t bd_addr;
1637
1638    BTIF_TRACE_EVENT("btif_dm_upstreams_cback  ev: %s", dump_dm_event(event));
1639
1640    switch (event)
1641    {
1642        case BTA_DM_ENABLE_EVT:
1643        {
1644             BD_NAME bdname;
1645             bt_status_t status;
1646             bt_property_t prop;
1647             prop.type = BT_PROPERTY_BDNAME;
1648             prop.len = BD_NAME_LEN;
1649             prop.val = (void*)bdname;
1650
1651             status = btif_storage_get_adapter_property(&prop);
1652             if (status == BT_STATUS_SUCCESS)
1653             {
1654                 /* A name exists in the storage. Make this the device name */
1655                 BTA_DmSetDeviceName((char*)prop.val);
1656             }
1657             else
1658             {
1659                 /* Storage does not have a name yet.
1660                  * Use the default name and write it to the chip
1661                  */
1662                 BTA_DmSetDeviceName(btif_get_default_local_name());
1663             }
1664
1665#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1666             /* Enable local privacy */
1667             BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED);
1668#endif
1669
1670             /* for each of the enabled services in the mask, trigger the profile
1671              * enable */
1672             service_mask = btif_get_enabled_services_mask();
1673             for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1674             {
1675                 if (service_mask &
1676                     (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1677                 {
1678                     btif_in_execute_service_request(i, TRUE);
1679                 }
1680             }
1681             /* clear control blocks */
1682             memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1683
1684             /* This function will also trigger the adapter_properties_cb
1685             ** and bonded_devices_info_cb
1686             */
1687             btif_storage_load_bonded_devices();
1688
1689             btif_storage_load_autopair_device_list();
1690
1691             btif_enable_bluetooth_evt(p_data->enable.status);
1692        }
1693        break;
1694
1695        case BTA_DM_DISABLE_EVT:
1696            /* for each of the enabled services in the mask, trigger the profile
1697             * disable */
1698            service_mask = btif_get_enabled_services_mask();
1699            for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1700            {
1701                if (service_mask &
1702                    (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1703                {
1704                    btif_in_execute_service_request(i, FALSE);
1705                }
1706            }
1707            btif_disable_bluetooth_evt();
1708            break;
1709
1710        case BTA_DM_PIN_REQ_EVT:
1711            btif_dm_pin_req_evt(&p_data->pin_req);
1712            break;
1713
1714        case BTA_DM_AUTH_CMPL_EVT:
1715            btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1716            break;
1717
1718        case BTA_DM_BOND_CANCEL_CMPL_EVT:
1719            if (pairing_cb.state == BT_BOND_STATE_BONDING)
1720            {
1721                bdcpy(bd_addr.address, pairing_cb.bd_addr);
1722                bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1723            }
1724            break;
1725
1726        case BTA_DM_SP_CFM_REQ_EVT:
1727            btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1728            break;
1729        case BTA_DM_SP_KEY_NOTIF_EVT:
1730            btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1731            break;
1732
1733        case BTA_DM_DEV_UNPAIRED_EVT:
1734            bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1735
1736            /*special handling for HID devices */
1737            #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
1738            btif_hh_remove_device(bd_addr);
1739            #endif
1740            #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1741            btif_storage_remove_ble_bonding_keys(&bd_addr);
1742            #endif
1743            btif_storage_remove_bonded_device(&bd_addr);
1744            bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1745            break;
1746
1747        case BTA_DM_BUSY_LEVEL_EVT:
1748        {
1749
1750            if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
1751            {
1752                if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
1753                {
1754                       HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1755                                                BT_DISCOVERY_STARTED);
1756                       btif_dm_inquiry_in_progress = TRUE;
1757                }
1758                else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
1759                {
1760                       HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1761                                                BT_DISCOVERY_STOPPED);
1762                       btif_dm_inquiry_in_progress = FALSE;
1763                }
1764                else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
1765                {
1766                       btif_dm_inquiry_in_progress = FALSE;
1767                }
1768            }
1769        }break;
1770
1771        case BTA_DM_LINK_UP_EVT:
1772            bdcpy(bd_addr.address, p_data->link_up.bd_addr);
1773            BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
1774
1775            btif_update_remote_version_property(&bd_addr);
1776
1777            HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1778                      &bd_addr, BT_ACL_STATE_CONNECTED);
1779            break;
1780
1781        case BTA_DM_LINK_DOWN_EVT:
1782            bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1783            BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
1784            HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1785                      &bd_addr, BT_ACL_STATE_DISCONNECTED);
1786            break;
1787
1788        case BTA_DM_HW_ERROR_EVT:
1789            BTIF_TRACE_ERROR("Received H/W Error. ");
1790            /* Flush storage data */
1791            btif_config_flush();
1792            usleep(100000); /* 100milliseconds */
1793            /* Killing the process to force a restart as part of fault tolerance */
1794            kill(getpid(), SIGKILL);
1795            break;
1796
1797#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1798        case BTA_DM_BLE_KEY_EVT:
1799            BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT key_type=0x%02x ", p_data->ble_key.key_type);
1800
1801            /* If this pairing is by-product of local initiated GATT client Read or Write,
1802            BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1803            have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1804            if (pairing_cb.state != BT_BOND_STATE_BONDING)
1805            {
1806                BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now");
1807                bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1808                                   BT_BOND_STATE_BONDING);
1809            }
1810            else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1811            {
1812                BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
1813                break;
1814            }
1815
1816            switch (p_data->ble_key.key_type)
1817            {
1818                case BTA_LE_KEY_PENC:
1819                    BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
1820                    pairing_cb.ble.is_penc_key_rcvd = TRUE;
1821                    memcpy(pairing_cb.ble.penc_key.ltk,
1822                           p_data->ble_key.p_key_value->penc_key.ltk, 16);
1823                    memcpy(pairing_cb.ble.penc_key.rand,
1824                           p_data->ble_key.p_key_value->penc_key.rand,8);
1825                    pairing_cb.ble.penc_key.ediv = p_data->ble_key.p_key_value->penc_key.ediv;
1826                    pairing_cb.ble.penc_key.sec_level =
1827                           p_data->ble_key.p_key_value->penc_key.sec_level;
1828
1829                    for (i=0; i<16; i++)
1830                    {
1831                        BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.ltk[%d]=0x%02x",
1832                                          i,pairing_cb.ble.penc_key.ltk[i]);
1833                    }
1834                    for (i=0; i<8; i++)
1835                    {
1836                        BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.rand[%d]=0x%02x",
1837                                          i,pairing_cb.ble.penc_key.rand[i]);
1838                    }
1839                    BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.ediv=0x%04x",
1840                        pairing_cb.ble.penc_key.ediv);
1841                    BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.sec_level=0x%02x",
1842                        pairing_cb.ble.penc_key.sec_level);
1843                    BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.key_size=0x%02x",
1844                        pairing_cb.ble.penc_key.key_size);
1845                    break;
1846
1847                case BTA_LE_KEY_PID:
1848                    BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
1849                    pairing_cb.ble.is_pid_key_rcvd = TRUE;
1850                    pairing_cb.ble.pid_key.addr_type =
1851                           p_data->ble_key.p_key_value->pid_key.addr_type;
1852                    memcpy(pairing_cb.ble.pid_key.irk,
1853                           p_data->ble_key.p_key_value->pid_key.irk, 16);
1854                    memcpy(pairing_cb.ble.pid_key.static_addr,
1855                           p_data->ble_key.p_key_value->pid_key.static_addr,BD_ADDR_LEN);
1856                    for (i=0; i<16; i++)
1857                    {
1858                        BTIF_TRACE_DEBUG("pairing_cb.ble.pid_key.irk[%d]=0x%02x"
1859                                            ,i,pairing_cb.ble.pid_key.irk[i]);
1860                    }
1861                    for (i=0; i<BD_ADDR_LEN; i++)
1862                    {
1863                        BTIF_TRACE_DEBUG("piaring_cb.ble.pid_address[%d] = %x"
1864                                            ,i, pairing_cb.ble.pid_key.static_addr[i]);
1865                    }
1866                    break;
1867
1868                case BTA_LE_KEY_PCSRK:
1869                    BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
1870                    pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1871                    pairing_cb.ble.pcsrk_key.counter =
1872                           p_data->ble_key.p_key_value->pcsrk_key.counter;
1873                    pairing_cb.ble.pcsrk_key.sec_level =
1874                           p_data->ble_key.p_key_value->pcsrk_key.sec_level;
1875                    memcpy(pairing_cb.ble.pcsrk_key.csrk,
1876                           p_data->ble_key.p_key_value->pcsrk_key.csrk,16);
1877
1878                    for (i=0; i<16; i++)
1879                    {
1880                        BTIF_TRACE_DEBUG("pairing_cb.ble.pcsrk_key.csrk[%d]=0x%02x",
1881                                          i,pairing_cb.ble.pcsrk_key.csrk[i]);
1882                    }
1883                    BTIF_TRACE_DEBUG("pairing_cb.ble.pcsrk_key.counter=0x%08x",
1884                                      pairing_cb.ble.pcsrk_key.counter);
1885                    BTIF_TRACE_DEBUG("pairing_cb.ble.pcsrk_key.sec_level=0x%02x",
1886                                      pairing_cb.ble.pcsrk_key.sec_level);
1887                    break;
1888
1889                case BTA_LE_KEY_LENC:
1890                    BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
1891                    pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1892                    pairing_cb.ble.lenc_key.div = p_data->ble_key.p_key_value->lenc_key.div;
1893                    pairing_cb.ble.lenc_key.key_size =
1894                           p_data->ble_key.p_key_value->lenc_key.key_size;
1895                    pairing_cb.ble.lenc_key.sec_level =
1896                           p_data->ble_key.p_key_value->lenc_key.sec_level;
1897
1898                    BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.div=0x%04x",
1899                                      pairing_cb.ble.lenc_key.div);
1900                    BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.key_size=0x%02x",
1901                                      pairing_cb.ble.lenc_key.key_size);
1902                    BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.sec_level=0x%02x",
1903                                      pairing_cb.ble.lenc_key.sec_level);
1904                    break;
1905
1906
1907
1908                case BTA_LE_KEY_LCSRK:
1909                    BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
1910                    pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1911                    pairing_cb.ble.lcsrk_key.counter =
1912                        p_data->ble_key.p_key_value->lcsrk_key.counter;
1913                    pairing_cb.ble.lcsrk_key.div =
1914                        p_data->ble_key.p_key_value->lcsrk_key.div;
1915                    pairing_cb.ble.lcsrk_key.sec_level =
1916                        p_data->ble_key.p_key_value->lcsrk_key.sec_level;
1917
1918                    BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.div=0x%04x",
1919                        pairing_cb.ble.lcsrk_key.div);
1920                    BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.counter=0x%08x",
1921                        pairing_cb.ble.lcsrk_key.counter);
1922                    BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.sec_level=0x%02x",
1923                        pairing_cb.ble.lcsrk_key.sec_level);
1924
1925                    break;
1926
1927                case BTA_LE_KEY_LID:
1928                    BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LID");
1929                    pairing_cb.ble.is_lidk_key_rcvd =  TRUE;
1930                    break;
1931
1932                default:
1933                    BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
1934                    break;
1935            }
1936
1937            break;
1938        case BTA_DM_BLE_SEC_REQ_EVT:
1939            BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
1940            btif_dm_ble_sec_req_evt(&p_data->ble_req);
1941            break;
1942        case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
1943            BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
1944            btif_dm_ble_key_notif_evt(&p_data->key_notif);
1945            break;
1946        case BTA_DM_BLE_PASSKEY_REQ_EVT:
1947            BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
1948            btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1949            break;
1950        case BTA_DM_BLE_NC_REQ_EVT:
1951            BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
1952            btif_dm_ble_key_nc_req_evt(&p_data->key_notif);
1953            break;
1954        case BTA_DM_BLE_OOB_REQ_EVT:
1955            BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
1956            break;
1957        case BTA_DM_BLE_LOCAL_IR_EVT:
1958            BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
1959            ble_local_key_cb.is_id_keys_rcvd = TRUE;
1960            memcpy(&ble_local_key_cb.id_keys.irk[0],
1961                   &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1962            memcpy(&ble_local_key_cb.id_keys.ir[0],
1963                   &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1964            memcpy(&ble_local_key_cb.id_keys.dhk[0],
1965                   &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1966            btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1967                                            BTIF_DM_LE_LOCAL_KEY_IR,
1968                                            BT_OCTET16_LEN);
1969            btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1970                                            BTIF_DM_LE_LOCAL_KEY_IRK,
1971                                            BT_OCTET16_LEN);
1972            btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1973                                            BTIF_DM_LE_LOCAL_KEY_DHK,
1974                                            BT_OCTET16_LEN);
1975            break;
1976        case BTA_DM_BLE_LOCAL_ER_EVT:
1977            BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
1978            ble_local_key_cb.is_er_rcvd = TRUE;
1979            memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1980            btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1981                                            BTIF_DM_LE_LOCAL_KEY_ER,
1982                                            BT_OCTET16_LEN);
1983            break;
1984
1985        case BTA_DM_BLE_AUTH_CMPL_EVT:
1986            BTIF_TRACE_DEBUG("BTA_DM_BLE_AUTH_CMPL_EVT. ");
1987            btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1988            break;
1989
1990        case BTA_DM_LE_FEATURES_READ:
1991        {
1992            tBTM_BLE_VSC_CB cmn_vsc_cb;
1993            bt_local_le_features_t local_le_features;
1994            char buf[512];
1995            bt_property_t prop;
1996            prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1997            prop.val = (void*)buf;
1998            prop.len = sizeof(buf);
1999
2000           /* LE features are not stored in storage. Should be retrived from stack */
2001            BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
2002            local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
2003
2004            prop.len = sizeof (bt_local_le_features_t);
2005            if (cmn_vsc_cb.filter_support == 1)
2006                local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
2007             else
2008                local_le_features.max_adv_filter_supported = 0;
2009            local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
2010            local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
2011            local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
2012            local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
2013            local_le_features.scan_result_storage_size = cmn_vsc_cb.tot_scan_results_strg;
2014            local_le_features.version_supported = cmn_vsc_cb.version_supported;
2015            local_le_features.total_trackable_advertisers =
2016                        cmn_vsc_cb.total_trackable_advertisers;
2017            memcpy(prop.val, &local_le_features, prop.len);
2018            HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
2019            break;
2020         }
2021
2022        case BTA_DM_ENER_INFO_READ:
2023        {
2024            btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param;
2025            bt_activity_energy_info energy_info;
2026            energy_info.status = p_ener_data->status;
2027            energy_info.ctrl_state = p_ener_data->ctrl_state;
2028            energy_info.rx_time = p_ener_data->rx_time;
2029            energy_info.tx_time = p_ener_data->tx_time;
2030            energy_info.idle_time = p_ener_data->idle_time;
2031            energy_info.energy_used = p_ener_data->energy_used;
2032            HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info);
2033            break;
2034        }
2035#endif
2036
2037        case BTA_DM_AUTHORIZE_EVT:
2038        case BTA_DM_SIG_STRENGTH_EVT:
2039        case BTA_DM_SP_RMT_OOB_EVT:
2040        case BTA_DM_SP_KEYPRESS_EVT:
2041        case BTA_DM_ROLE_CHG_EVT:
2042
2043        default:
2044            BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event );
2045            break;
2046    }
2047
2048    btif_dm_data_free(event, p_data);
2049}
2050
2051/*******************************************************************************
2052**
2053** Function         btif_dm_generic_evt
2054**
2055** Description      Executes non-BTA upstream events in BTIF context
2056**
2057** Returns          void
2058**
2059*******************************************************************************/
2060static void btif_dm_generic_evt(UINT16 event, char* p_param)
2061{
2062    BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
2063    switch(event)
2064    {
2065        case BTIF_DM_CB_DISCOVERY_STARTED:
2066        {
2067            HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
2068        }
2069        break;
2070
2071        case BTIF_DM_CB_CREATE_BOND:
2072        {
2073            pairing_cb.timeout_retries = NUM_TIMEOUT_RETRIES;
2074            btif_dm_create_bond_cb_t *create_bond_cb = (btif_dm_create_bond_cb_t*)p_param;
2075            btif_dm_cb_create_bond(&create_bond_cb->bdaddr, create_bond_cb->transport);
2076        }
2077        break;
2078
2079        case BTIF_DM_CB_REMOVE_BOND:
2080        {
2081            btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
2082        }
2083        break;
2084
2085        case BTIF_DM_CB_HID_REMOTE_NAME:
2086        {
2087            btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
2088        }
2089        break;
2090
2091        case BTIF_DM_CB_BOND_STATE_BONDING:
2092            {
2093                bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
2094            }
2095            break;
2096        case BTIF_DM_CB_LE_TX_TEST:
2097        case BTIF_DM_CB_LE_RX_TEST:
2098            {
2099                uint8_t status;
2100                STREAM_TO_UINT8(status, p_param);
2101                HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2102                      (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
2103            }
2104            break;
2105        case BTIF_DM_CB_LE_TEST_END:
2106            {
2107                uint8_t status;
2108                uint16_t count = 0;
2109                STREAM_TO_UINT8(status, p_param);
2110                if (status == 0)
2111                    STREAM_TO_UINT16(count, p_param);
2112                HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2113                      (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
2114            }
2115            break;
2116        default:
2117        {
2118            BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
2119        }
2120        break;
2121    }
2122}
2123
2124/*******************************************************************************
2125**
2126** Function         bte_dm_evt
2127**
2128** Description      Switches context from BTE to BTIF for all DM events
2129**
2130** Returns          void
2131**
2132*******************************************************************************/
2133
2134void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
2135{
2136    /* switch context to btif task context (copy full union size for convenience) */
2137    bt_status_t status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event,
2138                                (void*)p_data, sizeof(tBTA_DM_SEC), btif_dm_data_copy);
2139
2140    /* catch any failed context transfers */
2141    ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
2142}
2143
2144/*******************************************************************************
2145**
2146** Function         bte_search_devices_evt
2147**
2148** Description      Switches context from BTE to BTIF for DM search events
2149**
2150** Returns          void
2151**
2152*******************************************************************************/
2153static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2154{
2155    UINT16 param_len = 0;
2156
2157    if (p_data)
2158        param_len += sizeof(tBTA_DM_SEARCH);
2159    /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
2160    switch (event)
2161    {
2162        case BTA_DM_INQ_RES_EVT:
2163        {
2164            if (p_data->inq_res.p_eir)
2165                param_len += HCI_EXT_INQ_RESPONSE_LEN;
2166        }
2167        break;
2168
2169        case BTA_DM_DISC_RES_EVT:
2170        {
2171            if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
2172                param_len += p_data->disc_res.raw_data_size;
2173        }
2174        break;
2175    }
2176    BTIF_TRACE_DEBUG("%s event=%s param_len=%d", __FUNCTION__, dump_dm_search_event(event), param_len);
2177
2178    /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
2179    if (event == BTA_DM_INQ_RES_EVT)
2180        p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
2181
2182    btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
2183        (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
2184}
2185
2186/*******************************************************************************
2187**
2188** Function         bte_dm_search_services_evt
2189**
2190** Description      Switches context from BTE to BTIF for DM search services
2191**                  event
2192**
2193** Returns          void
2194**
2195*******************************************************************************/
2196static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2197{
2198    UINT16 param_len = 0;
2199   if (p_data)
2200       param_len += sizeof(tBTA_DM_SEARCH);
2201   switch (event)
2202   {
2203         case BTA_DM_DISC_RES_EVT:
2204         {
2205             if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
2206                  param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
2207             }
2208         } break;
2209   }
2210   /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
2211    * if raw_data is needed. */
2212   btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
2213         (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
2214}
2215
2216/*******************************************************************************
2217**
2218** Function         bte_dm_remote_service_record_evt
2219**
2220** Description      Switches context from BTE to BTIF for DM search service
2221**                  record event
2222**
2223** Returns          void
2224**
2225*******************************************************************************/
2226static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2227{
2228   /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
2229   btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
2230}
2231
2232#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2233/*******************************************************************************
2234**
2235** Function         bta_energy_info_cb
2236**
2237** Description      Switches context from BTE to BTIF for DM energy info event
2238**
2239** Returns          void
2240**
2241*******************************************************************************/
2242static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time,
2243                                    tBTA_DM_BLE_IDLE_TIME_MS idle_time,
2244                                    tBTA_DM_BLE_ENERGY_USED energy_used,
2245                                    tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status)
2246{
2247    BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld",
2248        status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
2249
2250    btif_activity_energy_info_cb_t btif_cb;
2251    btif_cb.status = status;
2252    btif_cb.ctrl_state = ctrl_state;
2253    btif_cb.tx_time = (uint64_t) tx_time;
2254    btif_cb.rx_time = (uint64_t) rx_time;
2255    btif_cb.idle_time =(uint64_t) idle_time;
2256    btif_cb.energy_used =(uint64_t) energy_used;
2257    btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
2258                          (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL);
2259}
2260#endif
2261
2262/*******************************************************************************
2263**
2264** Function         bte_scan_filt_param_cfg_evt
2265**
2266** Description      Scan filter param config event
2267**
2268** Returns          void
2269**
2270*******************************************************************************/
2271static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
2272                                        tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
2273                                        tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
2274{
2275    /* This event occurs on calling BTA_DmBleCfgFilterCondition internally,
2276    ** and that is why there is no HAL callback
2277    */
2278    if(BTA_SUCCESS != status)
2279    {
2280        BTIF_TRACE_ERROR("%s, %d", __FUNCTION__, status);
2281    }
2282    else
2283    {
2284        BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2285    }
2286}
2287
2288/*****************************************************************************
2289**
2290**   btif api functions (no context switch)
2291**
2292*****************************************************************************/
2293
2294/*******************************************************************************
2295**
2296** Function         btif_dm_start_discovery
2297**
2298** Description      Start device discovery/inquiry
2299**
2300** Returns          bt_status_t
2301**
2302*******************************************************************************/
2303bt_status_t btif_dm_start_discovery(void)
2304{
2305    tBTA_DM_INQ inq_params;
2306    tBTA_SERVICE_MASK services = 0;
2307    tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
2308
2309    BTIF_TRACE_EVENT("%s", __FUNCTION__);
2310
2311#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2312    memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
2313    /* Cleanup anything remaining on index 0 */
2314    BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
2315                             bte_scan_filt_param_cfg_evt, 0);
2316
2317    /* Add an allow-all filter on index 0*/
2318    adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE;
2319    adv_filt_param.feat_seln = ALLOW_ALL_FILTER;
2320    adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;
2321    adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;
2322    adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE;
2323    adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE;
2324    BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL,
2325                             bte_scan_filt_param_cfg_evt, 0);
2326
2327    /* TODO: Do we need to handle multiple inquiries at the same time? */
2328
2329    /* Set inquiry params and call API */
2330    inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
2331#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
2332    inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
2333    inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
2334    inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
2335    inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
2336#endif
2337#else
2338    inq_params.mode = BTA_DM_GENERAL_INQUIRY;
2339#endif
2340    inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
2341
2342    inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
2343    inq_params.report_dup = TRUE;
2344
2345    inq_params.filter_type = BTA_DM_INQ_CLR;
2346    /* TODO: Filter device by BDA needs to be implemented here */
2347
2348    /* Will be enabled to TRUE once inquiry busy level has been received */
2349    btif_dm_inquiry_in_progress = FALSE;
2350    /* find nearby devices */
2351    BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
2352
2353    return BT_STATUS_SUCCESS;
2354}
2355
2356/*******************************************************************************
2357**
2358** Function         btif_dm_cancel_discovery
2359**
2360** Description      Cancels search
2361**
2362** Returns          bt_status_t
2363**
2364*******************************************************************************/
2365bt_status_t btif_dm_cancel_discovery(void)
2366{
2367    BTIF_TRACE_EVENT("%s", __FUNCTION__);
2368    BTA_DmSearchCancel();
2369    return BT_STATUS_SUCCESS;
2370}
2371
2372/*******************************************************************************
2373**
2374** Function         btif_dm_create_bond
2375**
2376** Description      Initiate bonding with the specified device
2377**
2378** Returns          bt_status_t
2379**
2380*******************************************************************************/
2381bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr, int transport)
2382{
2383    btif_dm_create_bond_cb_t create_bond_cb;
2384    create_bond_cb.transport = transport;
2385    bdcpy(create_bond_cb.bdaddr.address, bd_addr->address);
2386
2387    bdstr_t bdstr;
2388    BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)), transport);
2389    if (pairing_cb.state != BT_BOND_STATE_NONE)
2390        return BT_STATUS_BUSY;
2391
2392    btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
2393                          (char *)&create_bond_cb, sizeof(btif_dm_create_bond_cb_t), NULL);
2394
2395    return BT_STATUS_SUCCESS;
2396}
2397
2398/*******************************************************************************
2399**
2400** Function         btif_dm_cancel_bond
2401**
2402** Description      Initiate bonding with the specified device
2403**
2404** Returns          bt_status_t
2405**
2406*******************************************************************************/
2407
2408bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
2409{
2410    bdstr_t bdstr;
2411
2412    BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)));
2413
2414    /* TODO:
2415    **  1. Restore scan modes
2416    **  2. special handling for HID devices
2417    */
2418    if (pairing_cb.state == BT_BOND_STATE_BONDING)
2419    {
2420
2421#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2422
2423        if (pairing_cb.is_ssp)
2424        {
2425            if (pairing_cb.is_le_only)
2426            {
2427                BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2428            }
2429            else
2430            {
2431                BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2432                BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2433                btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2434            }
2435        }
2436        else
2437        {
2438            if (pairing_cb.is_le_only)
2439            {
2440                BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2441            }
2442            else
2443            {
2444                BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2445            }
2446        /* Cancel bonding, in case it is in ACL connection setup state */
2447        BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2448        }
2449
2450#else
2451        if (pairing_cb.is_ssp)
2452        {
2453            BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2454        }
2455        else
2456        {
2457            BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2458        }
2459        /* Cancel bonding, in case it is in ACL connection setup state */
2460        BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2461        btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2462#endif
2463    }
2464
2465    return BT_STATUS_SUCCESS;
2466}
2467
2468/*******************************************************************************
2469**
2470** Function         btif_dm_hh_open_failed
2471**
2472** Description      informs the upper layers if the HH have failed during bonding
2473**
2474** Returns          none
2475**
2476*******************************************************************************/
2477
2478void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
2479{
2480    if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2481            bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2482    {
2483        bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2484    }
2485}
2486
2487/*******************************************************************************
2488**
2489** Function         btif_dm_remove_bond
2490**
2491** Description      Removes bonding with the specified device
2492**
2493** Returns          bt_status_t
2494**
2495*******************************************************************************/
2496
2497bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
2498{
2499    bdstr_t bdstr;
2500
2501    BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)));
2502    btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2503                          (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2504
2505    return BT_STATUS_SUCCESS;
2506}
2507
2508/*******************************************************************************
2509**
2510** Function         btif_dm_pin_reply
2511**
2512** Description      BT legacy pairing - PIN code reply
2513**
2514** Returns          bt_status_t
2515**
2516*******************************************************************************/
2517
2518bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2519                               uint8_t pin_len, bt_pin_code_t *pin_code)
2520{
2521    BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
2522    if (pin_code == NULL)
2523        return BT_STATUS_FAIL;
2524#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2525
2526    if (pairing_cb.is_le_only)
2527    {
2528        int i;
2529        UINT32 passkey = 0;
2530        int multi[] = {100000, 10000, 1000, 100, 10,1};
2531        BD_ADDR remote_bd_addr;
2532        bdcpy(remote_bd_addr, bd_addr->address);
2533        for (i = 0; i < 6; i++)
2534        {
2535            passkey += (multi[i] * (pin_code->pin[i] - '0'));
2536        }
2537        BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
2538        BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2539
2540    }
2541    else
2542    {
2543        BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2544        if (accept)
2545            pairing_cb.pin_code_len = pin_len;
2546    }
2547#else
2548    BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2549
2550    if (accept)
2551        pairing_cb.pin_code_len = pin_len;
2552#endif
2553    return BT_STATUS_SUCCESS;
2554}
2555
2556/*******************************************************************************
2557**
2558** Function         btif_dm_ssp_reply
2559**
2560** Description      BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2561**
2562** Returns          bt_status_t
2563**
2564*******************************************************************************/
2565bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2566                                 bt_ssp_variant_t variant, uint8_t accept,
2567                                 uint32_t passkey)
2568{
2569    UNUSED(passkey);
2570
2571    if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2572    {
2573        /* This is not implemented in the stack.
2574         * For devices with display, this is not needed
2575        */
2576        BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__);
2577        return BT_STATUS_FAIL;
2578    }
2579    /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
2580    BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
2581#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2582    if (pairing_cb.is_le_only)
2583    {
2584        if(pairing_cb.is_le_nc)
2585        {
2586            BTA_DmBleConfirmReply((UINT8 *)bd_addr->address,accept);
2587        } else {
2588            if (accept)
2589                BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2590            else
2591                BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2592        }
2593    } else {
2594        BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2595    }
2596#else
2597    BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2598#endif
2599    return BT_STATUS_SUCCESS;
2600}
2601
2602/*******************************************************************************
2603**
2604** Function         btif_dm_get_adapter_property
2605**
2606** Description     Queries the BTA for the adapter property
2607**
2608** Returns          bt_status_t
2609**
2610*******************************************************************************/
2611bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2612{
2613    BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type);
2614    switch (prop->type)
2615    {
2616        case BT_PROPERTY_BDNAME:
2617        {
2618            bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
2619            strncpy((char *)bd_name->name, (char *)btif_get_default_local_name(),
2620                   sizeof(bd_name->name) - 1);
2621            bd_name->name[sizeof(bd_name->name) - 1] = 0;
2622            prop->len = strlen((char *)bd_name->name);
2623        }
2624        break;
2625
2626        case BT_PROPERTY_ADAPTER_SCAN_MODE:
2627        {
2628            /* if the storage does not have it. Most likely app never set it. Default is NONE */
2629            bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2630            *mode = BT_SCAN_MODE_NONE;
2631            prop->len = sizeof(bt_scan_mode_t);
2632        }
2633        break;
2634
2635        case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2636        {
2637            uint32_t *tmt = (uint32_t*)prop->val;
2638            *tmt = 120; /* default to 120s, if not found in NV */
2639            prop->len = sizeof(uint32_t);
2640        }
2641        break;
2642
2643        default:
2644            prop->len = 0;
2645            return BT_STATUS_FAIL;
2646    }
2647    return BT_STATUS_SUCCESS;
2648}
2649
2650/*******************************************************************************
2651**
2652** Function         btif_dm_get_remote_services
2653**
2654** Description      Start SDP to get remote services
2655**
2656** Returns          bt_status_t
2657**
2658*******************************************************************************/
2659bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2660{
2661    bdstr_t bdstr;
2662
2663    BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr)));
2664
2665    BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2666                   bte_dm_search_services_evt, TRUE);
2667
2668    return BT_STATUS_SUCCESS;
2669}
2670
2671/*******************************************************************************
2672**
2673** Function         btif_dm_get_remote_service_record
2674**
2675** Description      Start SDP to get remote service record
2676**
2677**
2678** Returns          bt_status_t
2679*******************************************************************************/
2680bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2681                                                    bt_uuid_t *uuid)
2682{
2683    tSDP_UUID sdp_uuid;
2684    bdstr_t bdstr;
2685
2686    BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr)));
2687
2688    sdp_uuid.len = MAX_UUID_SIZE;
2689    memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2690
2691    BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2692                       bte_dm_remote_service_record_evt, TRUE);
2693
2694    return BT_STATUS_SUCCESS;
2695}
2696
2697void btif_dm_execute_service_request(UINT16 event, char *p_param)
2698{
2699    BOOLEAN b_enable = FALSE;
2700    bt_status_t status;
2701    if (event == BTIF_DM_ENABLE_SERVICE)
2702    {
2703        b_enable = TRUE;
2704    }
2705    status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2706    if (status == BT_STATUS_SUCCESS)
2707    {
2708        bt_property_t property;
2709        bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2710
2711        /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2712        BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2713                                    sizeof(local_uuids), local_uuids);
2714        btif_storage_get_adapter_property(&property);
2715        HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2716                          BT_STATUS_SUCCESS, 1, &property);
2717    }
2718    return;
2719}
2720
2721void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2722                      tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2723{
2724    UINT8   yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2725    /* if local initiated:
2726    **      1. set DD + MITM
2727    ** if remote initiated:
2728    **      1. Copy over the auth_req from peer's io_rsp
2729    **      2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2730    ** as a fallback set MITM+GB if peer had MITM set
2731    */
2732    UNUSED (bd_addr);
2733    UNUSED (p_io_cap);
2734    UNUSED (p_oob_data);
2735
2736
2737    BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
2738    if(pairing_cb.is_local_initiated)
2739    {
2740        /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2741        *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2742    }
2743    else if (!is_orig)
2744    {
2745        /* peer initiated paring. They probably know what they want.
2746        ** Copy the mitm from peer device.
2747        */
2748        BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
2749                __FUNCTION__, pairing_cb.auth_req);
2750        *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2751
2752        /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2753        if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2754            *p_auth_req |= BTA_AUTH_SP_YES;
2755    }
2756    else if (yes_no_bit)
2757    {
2758        /* set the general bonding bit for stored device */
2759        *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2760    }
2761    BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
2762}
2763
2764void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2765                      tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2766{
2767    UNUSED (bd_addr);
2768    UNUSED (oob_data);
2769
2770    if(auth_req & BTA_AUTH_BONDS)
2771    {
2772        BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
2773        pairing_cb.auth_req = auth_req;
2774        pairing_cb.io_cap = io_cap;
2775    }
2776}
2777
2778#if (BTM_OOB_INCLUDED == TRUE)
2779void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA  *p_oob_data)
2780{
2781    if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2782        oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2783    {
2784        *p_oob_data = FALSE;
2785    }
2786    else
2787    {
2788        *p_oob_data = TRUE;
2789    }
2790    BTIF_TRACE_DEBUG("btif_dm_set_oob_for_io_req *p_oob_data=%d", *p_oob_data);
2791}
2792#endif /* BTM_OOB_INCLUDED */
2793
2794#ifdef BTIF_DM_OOB_TEST
2795void btif_dm_load_local_oob(void)
2796{
2797    char prop_oob[PROPERTY_VALUE_MAX];
2798    property_get("service.brcm.bt.oob", prop_oob, "3");
2799    BTIF_TRACE_DEBUG("btif_dm_load_local_oob prop_oob = %s",prop_oob);
2800    if (prop_oob[0] != '3')
2801    {
2802#if (BTM_OOB_INCLUDED == TRUE)
2803        if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2804            oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2805        {
2806            BTIF_TRACE_DEBUG("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
2807            BTA_DmLocalOob();
2808        }
2809#else
2810        BTIF_TRACE_ERROR("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
2811#endif
2812    }
2813}
2814
2815void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2816{
2817    FILE *fp;
2818    char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2819    char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2820    char *path = NULL;
2821    char prop_oob[PROPERTY_VALUE_MAX];
2822    BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: valid=%d", valid);
2823    if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2824        oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2825        valid)
2826    {
2827        BTIF_TRACE_DEBUG("save local OOB data in memory");
2828        memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2829        memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2830        property_get("service.brcm.bt.oob", prop_oob, "3");
2831        BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
2832        if (prop_oob[0] == '1')
2833            path = path_a;
2834        else if (prop_oob[0] == '2')
2835            path = path_b;
2836        if (path)
2837        {
2838            fp = fopen(path, "wb+");
2839            if (fp == NULL)
2840            {
2841                BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: failed to save local OOB data to %s", path);
2842            }
2843            else
2844            {
2845                BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: save local OOB data into file %s",path);
2846                fwrite (c , 1 , BT_OCTET16_LEN , fp );
2847                fwrite (r , 1 , BT_OCTET16_LEN , fp );
2848                fclose(fp);
2849            }
2850        }
2851    }
2852}
2853BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr,  BT_OCTET16 p_c, BT_OCTET16 p_r)
2854{
2855    char t[128];
2856    FILE *fp;
2857    char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2858    char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2859    char *path = NULL;
2860    char prop_oob[PROPERTY_VALUE_MAX];
2861    BOOLEAN result = FALSE;
2862    bt_bdaddr_t bt_bd_addr;
2863    bdcpy(oob_cb.oob_bdaddr, bd_addr);
2864    property_get("service.brcm.bt.oob", prop_oob, "3");
2865    BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
2866    if (prop_oob[0] == '1')
2867        path = path_b;
2868    else if (prop_oob[0] == '2')
2869        path = path_a;
2870    if (path)
2871    {
2872        fp = fopen(path, "rb");
2873        if (fp == NULL)
2874        {
2875            BTIF_TRACE_DEBUG("btapp_dm_rmt_oob_reply: failed to read OOB keys from %s",path);
2876            return FALSE;
2877        }
2878        else
2879        {
2880            BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob: read OOB data from %s",path);
2881            fread (p_c , 1 , BT_OCTET16_LEN , fp );
2882            fread (p_r , 1 , BT_OCTET16_LEN , fp );
2883            fclose(fp);
2884        }
2885        BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: TRUE");
2886        sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2887                oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2888                oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
2889        BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
2890        sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2891                p_c[0], p_c[1], p_c[2],  p_c[3],  p_c[4],  p_c[5],  p_c[6],  p_c[7],
2892                p_c[8], p_c[9], p_c[10], p_c[11], p_c[12], p_c[13], p_c[14], p_c[15]);
2893        BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: c = %s",t);
2894        sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2895                p_r[0], p_r[1], p_r[2],  p_r[3],  p_r[4],  p_r[5],  p_r[6],  p_r[7],
2896                p_r[8], p_r[9], p_r[10], p_r[11], p_r[12], p_r[13], p_r[14], p_r[15]);
2897        BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: r = %s",t);
2898        bdcpy(bt_bd_addr.address, bd_addr);
2899        btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2900                              (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2901        result = TRUE;
2902    }
2903    BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob result=%d",result);
2904    return result;
2905}
2906#endif /*  BTIF_DM_OOB_TEST */
2907#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2908
2909static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2910{
2911    bt_bdaddr_t bd_addr;
2912    bt_bdname_t bd_name;
2913    UINT32 cod;
2914    int dev_type;
2915
2916    BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2917
2918    /* Remote name update */
2919    if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
2920    {
2921        dev_type = BT_DEVICE_TYPE_BLE;
2922    }
2923    btif_dm_update_ble_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2924                                         (tBT_DEVICE_TYPE) dev_type);
2925    bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2926    memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2927
2928    bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2929    pairing_cb.is_ssp = FALSE;
2930    cod = COD_UNCLASSIFIED;
2931
2932    HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2933              cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2934              p_ssp_key_notif->passkey);
2935}
2936
2937/*******************************************************************************
2938**
2939** Function         btif_dm_ble_auth_cmpl_evt
2940**
2941** Description      Executes authentication complete event in btif context
2942**
2943** Returns          void
2944**
2945*******************************************************************************/
2946static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2947{
2948    /* Save link key, if not temporary */
2949    bt_bdaddr_t bd_addr;
2950    bt_status_t status = BT_STATUS_FAIL;
2951    bt_bond_state_t state = BT_BOND_STATE_NONE;
2952
2953    bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2954    if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2955    {
2956        /* store keys */
2957    }
2958    if (p_auth_cmpl->success)
2959    {
2960        status = BT_STATUS_SUCCESS;
2961        state = BT_BOND_STATE_BONDED;
2962
2963        btif_dm_save_ble_bonding_keys();
2964        BTA_GATTC_Refresh(bd_addr.address);
2965        btif_dm_get_remote_services(&bd_addr);
2966    }
2967    else
2968    {
2969        /*Map the HCI fail reason  to  bt status  */
2970        switch (p_auth_cmpl->fail_reason)
2971        {
2972            case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
2973            case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
2974                btif_dm_remove_ble_bonding_keys();
2975                status = BT_STATUS_AUTH_FAILURE;
2976                break;
2977            case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
2978                status = BT_STATUS_AUTH_REJECTED;
2979                break;
2980            default:
2981                btif_dm_remove_ble_bonding_keys();
2982                status =  BT_STATUS_FAIL;
2983                break;
2984        }
2985    }
2986    bond_state_changed(status, &bd_addr, state);
2987}
2988
2989
2990
2991void    btif_dm_load_ble_local_keys(void)
2992{
2993    memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2994
2995    if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2996                                       BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2997    {
2998        ble_local_key_cb.is_er_rcvd = TRUE;
2999        BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
3000    }
3001
3002    if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
3003                                        BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
3004        (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
3005                                        BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
3006        (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
3007                                        BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
3008    {
3009        ble_local_key_cb.is_id_keys_rcvd = TRUE;
3010        BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
3011    }
3012
3013}
3014void    btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
3015                                   tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
3016{
3017    if (ble_local_key_cb.is_er_rcvd )
3018    {
3019        memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
3020        *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
3021    }
3022
3023    if (ble_local_key_cb.is_id_keys_rcvd)
3024    {
3025        memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
3026        memcpy(&p_id_keys->irk[0],  &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
3027        memcpy(&p_id_keys->dhk[0],  &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
3028        *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
3029    }
3030    BTIF_TRACE_DEBUG("%s  *p_key_mask=0x%02x",__FUNCTION__,   *p_key_mask);
3031}
3032
3033void btif_dm_save_ble_bonding_keys(void)
3034{
3035
3036    bt_bdaddr_t bd_addr;
3037
3038    BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
3039
3040    bdcpy(bd_addr.address, pairing_cb.bd_addr);
3041
3042    if (pairing_cb.ble.is_penc_key_rcvd)
3043    {
3044        btif_storage_add_ble_bonding_key(&bd_addr,
3045                                         (char *) &pairing_cb.ble.penc_key,
3046                                         BTIF_DM_LE_KEY_PENC,
3047                                         sizeof(btif_dm_ble_penc_keys_t));
3048    }
3049
3050    if (pairing_cb.ble.is_pid_key_rcvd)
3051    {
3052        btif_storage_add_ble_bonding_key(&bd_addr,
3053                                         (char *) &pairing_cb.ble.pid_key,
3054                                         BTIF_DM_LE_KEY_PID,
3055                                         sizeof(btif_dm_ble_pid_keys_t));
3056    }
3057
3058
3059    if (pairing_cb.ble.is_pcsrk_key_rcvd)
3060    {
3061        btif_storage_add_ble_bonding_key(&bd_addr,
3062                                         (char *) &pairing_cb.ble.pcsrk_key,
3063                                         BTIF_DM_LE_KEY_PCSRK,
3064                                         sizeof(btif_dm_ble_pcsrk_keys_t));
3065    }
3066
3067
3068    if (pairing_cb.ble.is_lenc_key_rcvd)
3069    {
3070        btif_storage_add_ble_bonding_key(&bd_addr,
3071                                         (char *) &pairing_cb.ble.lenc_key,
3072                                         BTIF_DM_LE_KEY_LENC,
3073                                         sizeof(btif_dm_ble_lenc_keys_t));
3074    }
3075
3076    if (pairing_cb.ble.is_lcsrk_key_rcvd)
3077    {
3078        btif_storage_add_ble_bonding_key(&bd_addr,
3079                                         (char *) &pairing_cb.ble.lcsrk_key,
3080                                         BTIF_DM_LE_KEY_LCSRK,
3081                                         sizeof(btif_dm_ble_lcsrk_keys_t));
3082    }
3083
3084    if (pairing_cb.ble.is_lidk_key_rcvd)
3085    {
3086        btif_storage_add_ble_bonding_key(&bd_addr,
3087                                         NULL,
3088                                         BTIF_DM_LE_KEY_LID,
3089                                         0);
3090    }
3091
3092}
3093
3094
3095void btif_dm_remove_ble_bonding_keys(void)
3096{
3097    bt_bdaddr_t bd_addr;
3098
3099    BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
3100
3101    bdcpy(bd_addr.address, pairing_cb.bd_addr);
3102    btif_storage_remove_ble_bonding_keys(&bd_addr);
3103}
3104
3105
3106/*******************************************************************************
3107**
3108** Function         btif_dm_ble_sec_req_evt
3109**
3110** Description      Eprocess security request event in btif context
3111**
3112** Returns          void
3113**
3114*******************************************************************************/
3115void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
3116{
3117    bt_bdaddr_t bd_addr;
3118    bt_bdname_t bd_name;
3119    UINT32 cod;
3120    int dev_type;
3121
3122    BTIF_TRACE_DEBUG("%s", __FUNCTION__);
3123
3124    if (pairing_cb.state == BT_BOND_STATE_BONDING)
3125    {
3126        BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
3127        return;
3128    }
3129
3130    /* Remote name update */
3131    if (!btif_get_device_type(p_ble_req->bd_addr, &dev_type))
3132    {
3133        dev_type = BT_DEVICE_TYPE_BLE;
3134    }
3135    btif_dm_update_ble_remote_properties(p_ble_req->bd_addr, p_ble_req->bd_name,
3136                                         (tBT_DEVICE_TYPE) dev_type);
3137
3138    bdcpy(bd_addr.address, p_ble_req->bd_addr);
3139    memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
3140
3141    bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3142
3143    pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
3144    pairing_cb.is_le_only = TRUE;
3145    pairing_cb.is_le_nc = FALSE;
3146    pairing_cb.is_ssp = TRUE;
3147
3148    cod = COD_UNCLASSIFIED;
3149
3150    HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
3151              BT_SSP_VARIANT_CONSENT, 0);
3152}
3153
3154
3155
3156/*******************************************************************************
3157**
3158** Function         btif_dm_ble_passkey_req_evt
3159**
3160** Description      Executes pin request event in btif context
3161**
3162** Returns          void
3163**
3164*******************************************************************************/
3165static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
3166{
3167    bt_bdaddr_t bd_addr;
3168    bt_bdname_t bd_name;
3169    UINT32 cod;
3170    int dev_type;
3171
3172    /* Remote name update */
3173    if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
3174    {
3175        dev_type = BT_DEVICE_TYPE_BLE;
3176    }
3177    btif_dm_update_ble_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,
3178                                         (tBT_DEVICE_TYPE) dev_type);
3179
3180    bdcpy(bd_addr.address, p_pin_req->bd_addr);
3181    memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
3182
3183    bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3184    pairing_cb.is_le_only = TRUE;
3185
3186    cod = COD_UNCLASSIFIED;
3187
3188    HAL_CBACK(bt_hal_cbacks, pin_request_cb,
3189              &bd_addr, &bd_name, cod);
3190}
3191static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF *p_notif_req)
3192{
3193    /* TODO implement key notification for numeric comparison */
3194    BTIF_TRACE_DEBUG("%s", __FUNCTION__);
3195
3196    /* Remote name update */
3197    btif_update_remote_properties(p_notif_req->bd_addr , p_notif_req->bd_name,
3198                                          NULL, BT_DEVICE_TYPE_BLE);
3199
3200    bt_bdaddr_t bd_addr;
3201    bdcpy(bd_addr.address, p_notif_req->bd_addr);
3202
3203    bt_bdname_t bd_name;
3204    memcpy(bd_name.name, p_notif_req->bd_name, BD_NAME_LEN);
3205
3206    bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3207    pairing_cb.is_ssp = FALSE;
3208    pairing_cb.is_le_only = TRUE;
3209    pairing_cb.is_le_nc = TRUE;
3210
3211    HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
3212              COD_UNCLASSIFIED, BT_SSP_VARIANT_PASSKEY_CONFIRMATION,
3213              p_notif_req->passkey);
3214}
3215
3216void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
3217                                           tBT_DEVICE_TYPE dev_type)
3218{
3219   btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
3220}
3221
3222static void btif_dm_ble_tx_test_cback(void *p)
3223{
3224    btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
3225                          (char *)p, 1, NULL);
3226}
3227
3228static void btif_dm_ble_rx_test_cback(void *p)
3229{
3230    btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
3231                          (char *)p, 1, NULL);
3232}
3233
3234static void btif_dm_ble_test_end_cback(void *p)
3235{
3236    btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
3237                          (char *)p, 3, NULL);
3238}
3239/*******************************************************************************
3240**
3241** Function         btif_le_test_mode
3242**
3243** Description     Sends a HCI BLE Test command to the Controller
3244**
3245** Returns          BT_STATUS_SUCCESS on success
3246**
3247*******************************************************************************/
3248bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
3249{
3250     switch (opcode) {
3251         case HCI_BLE_TRANSMITTER_TEST:
3252             if (len != 3) return BT_STATUS_PARM_INVALID;
3253             BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
3254             break;
3255         case HCI_BLE_RECEIVER_TEST:
3256             if (len != 1) return BT_STATUS_PARM_INVALID;
3257             BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
3258             break;
3259         case HCI_BLE_TEST_END:
3260             BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
3261             break;
3262         default:
3263             BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
3264             return BT_STATUS_UNSUPPORTED;
3265     }
3266     return BT_STATUS_SUCCESS;
3267}
3268#endif
3269
3270void btif_dm_on_disable()
3271{
3272    /* cancel any pending pairing requests */
3273    if (pairing_cb.state == BT_BOND_STATE_BONDING)
3274    {
3275        bt_bdaddr_t bd_addr;
3276
3277        BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
3278        bdcpy(bd_addr.address, pairing_cb.bd_addr);
3279        btif_dm_cancel_bond(&bd_addr);
3280    }
3281}
3282
3283/*******************************************************************************
3284**
3285** Function         btif_dm_read_energy_info
3286**
3287** Description     Reads the energy info from controller
3288**
3289** Returns         void
3290**
3291*******************************************************************************/
3292void btif_dm_read_energy_info()
3293{
3294#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
3295    BTA_DmBleGetEnergyInfo(bta_energy_info_cb);
3296#endif
3297}
3298
3299static char* btif_get_default_local_name() {
3300    if (btif_default_local_name[0] == '\0')
3301    {
3302        int max_len = sizeof(btif_default_local_name) - 1;
3303        if (BTM_DEF_LOCAL_NAME[0] != '\0')
3304        {
3305            strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
3306        }
3307        else
3308        {
3309            char prop_model[PROPERTY_VALUE_MAX];
3310            property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
3311            strncpy(btif_default_local_name, prop_model, max_len);
3312        }
3313        btif_default_local_name[max_len] = '\0';
3314    }
3315    return btif_default_local_name;
3316}
3317