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