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 **  Constants & Macros
     49 ******************************************************************************/
     50 
     51 #define COD_UNCLASSIFIED ((0x1F) << 8)
     52 #define COD_HID_KEYBOARD                    0x0540
     53 #define COD_HID_POINTING                    0x0580
     54 #define COD_HID_COMBO                       0x05C0
     55 #define COD_HID_MAJOR                       0x0500
     56 #define COD_AV_HEADSETS                     0x0404
     57 #define COD_AV_HANDSFREE                    0x0408
     58 #define COD_AV_HEADPHONES                   0x0418
     59 #define COD_AV_PORTABLE_AUDIO               0x041C
     60 #define COD_AV_HIFI_AUDIO                   0x0428
     61 
     62 
     63 #define BTIF_DM_DEFAULT_INQ_MAX_RESULTS     0
     64 #define BTIF_DM_DEFAULT_INQ_MAX_DURATION    10
     65 #define BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING 2
     66 
     67 
     68 typedef struct
     69 {
     70     bt_bond_state_t state;
     71     BD_ADDR bd_addr;
     72     UINT8   is_temp;
     73     UINT8   pin_code_len;
     74     UINT8   is_ssp;
     75     UINT8   autopair_attempts;
     76     UINT8   is_local_initiated;
     77     UINT8   sdp_attempts;
     78 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
     79     BOOLEAN          is_le_only;
     80     btif_dm_ble_cb_t ble;
     81 #endif
     82 } btif_dm_pairing_cb_t;
     83 
     84 
     85 typedef struct
     86 {
     87     UINT8       ir[BT_OCTET16_LEN];
     88     UINT8       irk[BT_OCTET16_LEN];
     89     UINT8       dhk[BT_OCTET16_LEN];
     90 }btif_dm_local_key_id_t;
     91 
     92 typedef struct
     93 {
     94     BOOLEAN                 is_er_rcvd;
     95     UINT8                   er[BT_OCTET16_LEN];
     96     BOOLEAN                 is_id_keys_rcvd;
     97     btif_dm_local_key_id_t  id_keys;  /* ID kyes */
     98 
     99 }btif_dm_local_key_cb_t;
    100 
    101 typedef struct
    102 {
    103     BD_ADDR bd_addr;
    104     BD_NAME bd_name;
    105 } btif_dm_remote_name_t;
    106 
    107 typedef struct
    108 {
    109     BT_OCTET16 sp_c;
    110     BT_OCTET16 sp_r;
    111     BD_ADDR  oob_bdaddr;  /* peer bdaddr*/
    112 } btif_dm_oob_cb_t;
    113 #define BTA_SERVICE_ID_TO_SERVICE_MASK(id)       (1 << (id))
    114 
    115 /* This flag will be true if HCI_Inquiry is in progress */
    116 static BOOLEAN btif_dm_inquiry_in_progress = FALSE;
    117 
    118 /******************************************************************************
    119 **  Static functions
    120 ******************************************************************************/
    121 static btif_dm_pairing_cb_t pairing_cb;
    122 static btif_dm_oob_cb_t     oob_cb;
    123 static void btif_dm_generic_evt(UINT16 event, char* p_param);
    124 static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr);
    125 static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name);
    126 static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
    127                                           DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type);
    128 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
    129 static btif_dm_local_key_cb_t ble_local_key_cb;
    130 static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif);
    131 static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl);
    132 static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req);
    133 #endif
    134 /******************************************************************************
    135 **  Externs
    136 ******************************************************************************/
    137 extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
    138 extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
    139 extern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
    140 extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
    141 extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
    142 
    143 
    144 /******************************************************************************
    145 **  Functions
    146 ******************************************************************************/
    147 
    148 bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
    149                                                 BOOLEAN b_enable)
    150 {
    151     /* Check the service_ID and invoke the profile's BT state changed API */
    152     switch (service_id)
    153     {
    154          case BTA_HFP_SERVICE_ID:
    155          case BTA_HSP_SERVICE_ID:
    156          {
    157               btif_hf_execute_service(b_enable);
    158          }break;
    159          case BTA_A2DP_SERVICE_ID:
    160          {
    161               btif_av_execute_service(b_enable);
    162          }break;
    163          case BTA_HID_SERVICE_ID:
    164          {
    165               btif_hh_execute_service(b_enable);
    166          }break;
    167 
    168          default:
    169               BTIF_TRACE_ERROR1("%s: Unknown service being enabled", __FUNCTION__);
    170               return BT_STATUS_FAIL;
    171     }
    172     return BT_STATUS_SUCCESS;
    173 }
    174 
    175 /*******************************************************************************
    176 **
    177 ** Function         check_eir_remote_name
    178 **
    179 ** Description      Check if remote name is in the EIR data
    180 **
    181 ** Returns          TRUE if remote name found
    182 **                  Populate p_remote_name, if provided and remote name found
    183 **
    184 *******************************************************************************/
    185 static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
    186                             UINT8 *p_remote_name, UINT8 *p_remote_name_len)
    187 {
    188     UINT8 *p_eir_remote_name = NULL;
    189     UINT8 remote_name_len = 0;
    190 
    191     /* Check EIR for remote name and services */
    192     if (p_search_data->inq_res.p_eir)
    193     {
    194         p_eir_remote_name = BTA_CheckEirData(p_search_data->inq_res.p_eir,
    195                 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
    196         if (!p_eir_remote_name)
    197         {
    198             p_eir_remote_name = BTA_CheckEirData(p_search_data->inq_res.p_eir,
    199                     BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
    200         }
    201 
    202         if (p_eir_remote_name)
    203         {
    204             if (remote_name_len > BD_NAME_LEN)
    205                 remote_name_len = BD_NAME_LEN;
    206 
    207             if (p_remote_name && p_remote_name_len)
    208             {
    209                 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
    210                 *(p_remote_name + remote_name_len) = 0;
    211                 *p_remote_name_len = remote_name_len;
    212             }
    213 
    214             return TRUE;
    215         }
    216     }
    217 
    218     return FALSE;
    219 
    220 }
    221 
    222 /*******************************************************************************
    223 **
    224 ** Function         check_cached_remote_name
    225 **
    226 ** Description      Check if remote name is in the NVRAM cache
    227 **
    228 ** Returns          TRUE if remote name found
    229 **                  Populate p_remote_name, if provided and remote name found
    230 **
    231 *******************************************************************************/
    232 static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
    233                                 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
    234 {
    235     bt_bdname_t bdname;
    236     bt_bdaddr_t remote_bdaddr;
    237     bt_property_t prop_name;
    238 
    239     /* check if we already have it in our btif_storage cache */
    240     bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
    241     BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
    242                                sizeof(bt_bdname_t), &bdname);
    243     if (btif_storage_get_remote_device_property(
    244         &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
    245     {
    246         if (p_remote_name && p_remote_name_len)
    247         {
    248             strcpy((char *)p_remote_name, (char *)bdname.name);
    249             *p_remote_name_len = strlen((char *)p_remote_name);
    250         }
    251         return TRUE;
    252     }
    253 
    254     return FALSE;
    255 }
    256 
    257 BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
    258 {
    259     uint32_t    remote_cod;
    260     bt_property_t prop_name;
    261 
    262     /* check if we already have it in our btif_storage cache */
    263     BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
    264                                sizeof(uint32_t), &remote_cod);
    265     if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
    266     {
    267         if ((remote_cod & 0x7ff) == cod)
    268             return TRUE;
    269     }
    270 
    271     return FALSE;
    272 }
    273 
    274 BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
    275 {
    276     uint32_t    remote_cod;
    277     bt_property_t prop_name;
    278 
    279     /* check if we already have it in our btif_storage cache */
    280     BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
    281                                sizeof(uint32_t), &remote_cod);
    282     if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
    283                                 &prop_name) == BT_STATUS_SUCCESS)
    284     {
    285         if ((remote_cod & 0x700) == cod)
    286             return TRUE;
    287     }
    288 
    289     return FALSE;
    290 }
    291 
    292 static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
    293 {
    294     /* Send bonding state only once - based on outgoing/incoming we may receive duplicates */
    295     if ( (pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING) )
    296         return;
    297 
    298     if (pairing_cb.is_temp)
    299     {
    300        state = BT_BOND_STATE_NONE;
    301     }
    302     BTIF_TRACE_DEBUG3("%s: state=%d prev_state=%d", __FUNCTION__, state, pairing_cb.state);
    303 
    304     HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
    305 
    306     if (state == BT_BOND_STATE_BONDING)
    307     {
    308         pairing_cb.state = state;
    309         bdcpy(pairing_cb.bd_addr, bd_addr->address);
    310     }
    311     else
    312     {
    313         memset(&pairing_cb, 0, sizeof(pairing_cb));
    314     }
    315 
    316 }
    317 
    318 /* store remote version in bt config to always have access
    319    to it post pairing*/
    320 static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
    321 {
    322     bt_property_t property;
    323     UINT8 lmp_ver = 0;
    324     UINT16 lmp_subver = 0;
    325     UINT16 mfct_set = 0;
    326     tBTM_STATUS btm_status;
    327     bt_remote_version_t info;
    328     bt_status_t status;
    329     bdstr_t bdstr;
    330 
    331     btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
    332                           &mfct_set, &lmp_subver);
    333 
    334     ALOGD("remote version info [%s]: %x, %x, %x", bd2str(p_bd, &bdstr),
    335                lmp_ver, mfct_set, lmp_subver);
    336 
    337     if (btm_status == BTM_SUCCESS)
    338     {
    339         /* always update cache to ensure we have availability whenever BTM API
    340            is not populated */
    341         info.manufacturer = mfct_set;
    342         info.sub_ver = lmp_subver;
    343         info.version = lmp_ver;
    344         BTIF_STORAGE_FILL_PROPERTY(&property,
    345                             BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
    346                             &info);
    347         status = btif_storage_set_remote_device_property(p_bd, &property);
    348         ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
    349     }
    350 }
    351 
    352 
    353 static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
    354                                           DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
    355 {
    356     int num_properties = 0;
    357     bt_property_t properties[3];
    358     bt_bdaddr_t bdaddr;
    359     bt_status_t status;
    360     UINT32 cod;
    361     bt_device_type_t dev_type;
    362 
    363     memset(properties, 0, sizeof(properties));
    364     bdcpy(bdaddr.address, bd_addr);
    365 
    366     /* remote name */
    367     if (strlen((const char *) bd_name))
    368     {
    369         BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
    370                             BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
    371         status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
    372         ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
    373         num_properties++;
    374     }
    375 
    376     /* class of device */
    377     cod = devclass2uint(dev_class);
    378     if ( cod == 0) {
    379         BTIF_TRACE_DEBUG1("%s():cod is 0, set as unclassified", __FUNCTION__);
    380         cod = COD_UNCLASSIFIED;
    381     }
    382 
    383     BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
    384                         BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
    385     status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
    386     ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
    387     num_properties++;
    388 
    389     /* device type */
    390     dev_type = device_type;
    391     BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
    392                         BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
    393     status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
    394     ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
    395     num_properties++;
    396 
    397     HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
    398                      status, &bdaddr, num_properties, properties);
    399 }
    400 /*******************************************************************************
    401 **
    402 ** Function         hid_remote_name_cback
    403 **
    404 ** Description      Remote name callback for HID device. Called in stack context
    405 **                  Special handling for HID devices
    406 **
    407 ** Returns          void
    408 **
    409 *******************************************************************************/
    410 static void hid_remote_name_cback(void *p_param)
    411 {
    412     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
    413 
    414     btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_HID_REMOTE_NAME,
    415         (char *)p_param, sizeof(tBTM_REMOTE_DEV_NAME), NULL);
    416 }
    417 
    418 /*******************************************************************************
    419 **
    420 ** Function         btif_dm_cb_hid_remote_name
    421 **
    422 ** Description      Remote name callback for HID device. Called in btif context
    423 **                  Special handling for HID devices
    424 **
    425 ** Returns          void
    426 **
    427 *******************************************************************************/
    428 static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
    429 {
    430     BTIF_TRACE_DEBUG3("%s: status=%d pairing_cb.state=%d", __FUNCTION__, p_remote_name->status, pairing_cb.state);
    431     if (pairing_cb.state == BT_BOND_STATE_BONDING)
    432     {
    433         bt_bdaddr_t remote_bd;
    434 
    435         bdcpy(remote_bd.address, pairing_cb.bd_addr);
    436 
    437         if (p_remote_name->status == BTM_SUCCESS)
    438         {
    439             bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
    440         }
    441         else
    442             bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
    443     }
    444 }
    445 
    446 int remove_hid_bond(bt_bdaddr_t *bd_addr)
    447 {
    448     /* For HID device, inorder to avoid the HID device from re-connecting again after unpairing,
    449          * we need to do virtual unplug
    450          */
    451     bdstr_t bdstr;
    452     BTIF_TRACE_DEBUG2("%s---Removing HID bond--%s", __FUNCTION__,bd2str((bt_bdaddr_t *)bd_addr, &bdstr));
    453     return btif_hh_virtual_unplug(bd_addr);
    454 
    455 }
    456 /*******************************************************************************
    457 **
    458 ** Function         btif_dm_cb_create_bond
    459 **
    460 ** Description      Create bond initiated from the BTIF thread context
    461 **                  Special handling for HID devices
    462 **
    463 ** Returns          void
    464 **
    465 *******************************************************************************/
    466 static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr)
    467 {
    468     BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
    469 
    470 
    471     bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
    472 
    473     if (is_hid){
    474 
    475             int status;
    476             status = btif_hh_connect(bd_addr);
    477             if(status != BT_STATUS_SUCCESS)
    478                 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
    479     }
    480     else
    481     {
    482 #if BLE_INCLUDED == TRUE
    483         int device_type;
    484         int addr_type;
    485         bdstr_t bdstr;
    486         bd2str(bd_addr, &bdstr);
    487         if(btif_config_get_int("Remote", (char const *)&bdstr,"DevType", &device_type) &&
    488            (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
    489            (device_type == BT_DEVICE_TYPE_BLE))
    490         {
    491             BTA_DmAddBleDevice(bd_addr->address, addr_type, BT_DEVICE_TYPE_BLE);
    492         }
    493 #endif
    494         BTA_DmBond ((UINT8 *)bd_addr->address);
    495     }
    496     /*  Track  originator of bond creation  */
    497     pairing_cb.is_local_initiated = TRUE;
    498 
    499 }
    500 
    501 /*******************************************************************************
    502 **
    503 ** Function         btif_dm_cb_remove_bond
    504 **
    505 ** Description      remove bond initiated from the BTIF thread context
    506 **                  Special handling for HID devices
    507 **
    508 ** Returns          void
    509 **
    510 *******************************************************************************/
    511 void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
    512 {
    513      bdstr_t bdstr;
    514      /*special handling for HID devices */
    515      if (check_cod_hid(bd_addr, COD_HID_MAJOR))
    516     {
    517         #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
    518         if(remove_hid_bond(bd_addr) != BTA_SUCCESS)
    519             BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
    520         #endif
    521     }
    522     else
    523     {
    524         if (BTA_DmRemoveDevice((UINT8 *)bd_addr->address) == BTA_SUCCESS)
    525         {
    526             BTIF_TRACE_DEBUG1("Successfully removed bonding with device: %s",
    527                                             bd2str((bt_bdaddr_t *)bd_addr, &bdstr));
    528         }
    529         else
    530             BTIF_TRACE_DEBUG1("Removed bonding with device failed: %s",
    531                                             bd2str((bt_bdaddr_t *)bd_addr, &bdstr));
    532     }
    533 }
    534 
    535 /*******************************************************************************
    536 **
    537 ** Function         search_devices_copy_cb
    538 **
    539 ** Description      Deep copy callback for search devices event
    540 **
    541 ** Returns          void
    542 **
    543 *******************************************************************************/
    544 static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
    545 {
    546     tBTA_DM_SEARCH *p_dest_data =  (tBTA_DM_SEARCH *) p_dest;
    547     tBTA_DM_SEARCH *p_src_data =  (tBTA_DM_SEARCH *) p_src;
    548 
    549     if (!p_src)
    550         return;
    551 
    552     BTIF_TRACE_DEBUG2("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
    553     memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
    554     switch (event)
    555     {
    556         case BTA_DM_INQ_RES_EVT:
    557         {
    558             if (p_src_data->inq_res.p_eir)
    559             {
    560                 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
    561                 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
    562             }
    563         }
    564         break;
    565 
    566         case BTA_DM_DISC_RES_EVT:
    567         {
    568             if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
    569             {
    570                 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
    571                 memcpy(p_dest_data->disc_res.p_raw_data,
    572                     p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
    573             }
    574         }
    575         break;
    576     }
    577 }
    578 
    579 static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
    580 {
    581     tBTA_DM_SEARCH *p_dest_data =  (tBTA_DM_SEARCH *) p_dest;
    582     tBTA_DM_SEARCH *p_src_data =  (tBTA_DM_SEARCH *) p_src;
    583 
    584     if (!p_src)
    585         return;
    586     memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
    587     switch (event)
    588     {
    589          case BTA_DM_DISC_RES_EVT:
    590          {
    591               if (p_src_data->disc_res.result == BTA_SUCCESS)
    592               {
    593                   if (p_src_data->disc_res.num_uuids > 0)
    594                   {
    595                        p_dest_data->disc_res.p_uuid_list =
    596                                                         (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
    597                        memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
    598                               p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
    599                        GKI_freebuf(p_src_data->disc_res.p_uuid_list);
    600                   }
    601                   if (p_src_data->disc_res.p_raw_data != NULL)
    602                   {
    603                       GKI_freebuf(p_src_data->disc_res.p_raw_data);
    604                   }
    605               }
    606          } break;
    607     }
    608 }
    609 /******************************************************************************
    610 **
    611 **  BTIF DM callback events
    612 **
    613 *****************************************************************************/
    614 
    615 /*******************************************************************************
    616 **
    617 ** Function         btif_dm_pin_req_evt
    618 **
    619 ** Description      Executes pin request event in btif context
    620 **
    621 ** Returns          void
    622 **
    623 *******************************************************************************/
    624 static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
    625 {
    626     bt_bdaddr_t bd_addr;
    627     bt_bdname_t bd_name;
    628     UINT32 cod;
    629     bt_pin_code_t pin_code;
    630 
    631     /* Remote properties update */
    632     btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
    633                                   p_pin_req->dev_class, BT_DEVICE_TYPE_BREDR);
    634 
    635     bdcpy(bd_addr.address, p_pin_req->bd_addr);
    636     memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
    637 
    638     bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
    639 
    640     cod = devclass2uint(p_pin_req->dev_class);
    641 
    642     if ( cod == 0) {
    643         BTIF_TRACE_DEBUG1("%s():cod is 0, set as unclassified", __FUNCTION__);
    644         cod = COD_UNCLASSIFIED;
    645     }
    646 
    647     /* check for auto pair possiblity only if bond was initiated by local device */
    648     if (pairing_cb.is_local_initiated)
    649     {
    650         if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
    651             check_cod(&bd_addr, COD_AV_HANDSFREE) ||
    652             check_cod(&bd_addr, COD_AV_HEADPHONES) ||
    653             check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
    654             check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
    655             check_cod(&bd_addr, COD_HID_POINTING))
    656         {
    657             BTIF_TRACE_DEBUG1("%s()cod matches for auto pair", __FUNCTION__);
    658             /*  Check if this device can be auto paired  */
    659             if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) &&
    660                 (pairing_cb.autopair_attempts == 0))
    661             {
    662                 BTIF_TRACE_DEBUG1("%s() Attempting auto pair", __FUNCTION__);
    663                 pin_code.pin[0] = 0x30;
    664                 pin_code.pin[1] = 0x30;
    665                 pin_code.pin[2] = 0x30;
    666                 pin_code.pin[3] = 0x30;
    667 
    668                 pairing_cb.autopair_attempts++;
    669                 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
    670                 return;
    671             }
    672         }
    673         else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
    674                  check_cod(&bd_addr, COD_HID_COMBO))
    675         {
    676             if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) &&
    677                (pairing_cb.autopair_attempts == 0))
    678             {
    679                 BTIF_TRACE_DEBUG1("%s() Attempting auto pair", __FUNCTION__);
    680                 pin_code.pin[0] = 0x30;
    681                 pin_code.pin[1] = 0x30;
    682                 pin_code.pin[2] = 0x30;
    683                 pin_code.pin[3] = 0x30;
    684 
    685                 pairing_cb.autopair_attempts++;
    686                 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
    687                 return;
    688             }
    689         }
    690     }
    691     HAL_CBACK(bt_hal_cbacks, pin_request_cb,
    692                      &bd_addr, &bd_name, cod);
    693 }
    694 
    695 /*******************************************************************************
    696 **
    697 ** Function         btif_dm_ssp_cfm_req_evt
    698 **
    699 ** Description      Executes SSP confirm request event in btif context
    700 **
    701 ** Returns          void
    702 **
    703 *******************************************************************************/
    704 static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
    705 {
    706     bt_bdaddr_t bd_addr;
    707     bt_bdname_t bd_name;
    708     UINT32 cod;
    709     BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
    710 
    711     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
    712 
    713     /* Remote properties update */
    714     btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
    715                                   p_ssp_cfm_req->dev_class, BT_DEVICE_TYPE_BREDR);
    716 
    717     bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
    718     memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
    719 
    720     /* Set the pairing_cb based on the local & remote authentication requirements */
    721     bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
    722 
    723     /* if just_works and bonding bit is not set treat this as temporary */
    724     if (p_ssp_cfm_req->just_works && !(p_ssp_cfm_req->loc_auth_req & BTM_AUTH_BONDS) &&
    725         !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
    726         !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
    727         pairing_cb.is_temp = TRUE;
    728     else
    729         pairing_cb.is_temp = FALSE;
    730 
    731     pairing_cb.is_ssp = TRUE;
    732 
    733     /* If JustWorks auto-accept */
    734     if (p_ssp_cfm_req->just_works)
    735     {
    736         /* Pairing consent for JustWorks needed if:
    737          * 1. Incoming pairing is detected AND
    738          * 2. local IO capabilities are DisplayYesNo AND
    739          * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
    740          */
    741         if ((is_incoming) && ((p_ssp_cfm_req->loc_io_caps == 0x01) &&
    742                 (p_ssp_cfm_req->rmt_io_caps == 0x00 || p_ssp_cfm_req->rmt_io_caps == 0x03)))
    743         {
    744             BTIF_TRACE_EVENT3("%s: User consent needed for incoming pairing request. loc_io_caps: %d, rmt_io_caps: %d",
    745                 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
    746         }
    747         else
    748         {
    749             BTIF_TRACE_EVENT1("%s: Auto-accept JustWorks pairing", __FUNCTION__);
    750             btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
    751             return;
    752         }
    753     }
    754 
    755     cod = devclass2uint(p_ssp_cfm_req->dev_class);
    756 
    757     if ( cod == 0) {
    758         ALOGD("cod is 0, set as unclassified");
    759         cod = COD_UNCLASSIFIED;
    760     }
    761 
    762     pairing_cb.sdp_attempts = 0;
    763     HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
    764                      (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
    765                      p_ssp_cfm_req->num_val);
    766 }
    767 
    768 static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
    769 {
    770     bt_bdaddr_t bd_addr;
    771     bt_bdname_t bd_name;
    772     UINT32 cod;
    773 
    774     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
    775 
    776     /* Remote properties update */
    777     btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
    778                                   p_ssp_key_notif->dev_class, BT_DEVICE_TYPE_BREDR);
    779 
    780     bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
    781     memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
    782 
    783     bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
    784     pairing_cb.is_ssp = TRUE;
    785     cod = devclass2uint(p_ssp_key_notif->dev_class);
    786 
    787     if ( cod == 0) {
    788         ALOGD("cod is 0, set as unclassified");
    789         cod = COD_UNCLASSIFIED;
    790     }
    791 
    792     HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
    793                      cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
    794                      p_ssp_key_notif->passkey);
    795 }
    796 /*******************************************************************************
    797 **
    798 ** Function         btif_dm_auth_cmpl_evt
    799 **
    800 ** Description      Executes authentication complete event in btif context
    801 **
    802 ** Returns          void
    803 **
    804 *******************************************************************************/
    805 static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
    806 {
    807     /* Save link key, if not temporary */
    808     bt_bdaddr_t bd_addr;
    809     bt_status_t status = BT_STATUS_FAIL;
    810     bt_bond_state_t state = BT_BOND_STATE_NONE;
    811 
    812     bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
    813     if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
    814     {
    815         if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB)  || (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
    816             (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) || (!pairing_cb.is_temp))
    817         {
    818             bt_status_t ret;
    819             BTIF_TRACE_DEBUG3("%s: Storing link key. key_type=0x%x, is_temp=%d",
    820                 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.is_temp);
    821             ret = btif_storage_add_bonded_device(&bd_addr,
    822                                 p_auth_cmpl->key, p_auth_cmpl->key_type,
    823                                 pairing_cb.pin_code_len);
    824             ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
    825         }
    826         else
    827         {
    828             BTIF_TRACE_DEBUG3("%s: Temporary key. Not storing. key_type=0x%x, is_temp=%d",
    829                 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.is_temp);
    830         }
    831     }
    832     if (p_auth_cmpl->success)
    833     {
    834         status = BT_STATUS_SUCCESS;
    835         state = BT_BOND_STATE_BONDED;
    836 
    837         /* Trigger SDP on the device */
    838         pairing_cb.sdp_attempts = 1;;
    839 
    840         if(btif_dm_inquiry_in_progress)
    841             btif_dm_cancel_discovery();
    842 
    843         btif_dm_get_remote_services(&bd_addr);
    844         /* Do not call bond_state_changed_cb yet. Wait till fetch remote service is complete */
    845     }
    846     else
    847     {
    848          /*Map the HCI fail reason  to  bt status  */
    849         switch(p_auth_cmpl->fail_reason)
    850         {
    851             case HCI_ERR_PAGE_TIMEOUT:
    852             case HCI_ERR_CONNECTION_TOUT:
    853                 status =  BT_STATUS_RMT_DEV_DOWN;
    854                 break;
    855 
    856             /* map the auth failure codes, so we can retry pairing if necessary */
    857             case HCI_ERR_AUTH_FAILURE:
    858             case HCI_ERR_HOST_REJECT_SECURITY:
    859             case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
    860             case HCI_ERR_UNIT_KEY_USED:
    861             case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
    862             case HCI_ERR_INSUFFCIENT_SECURITY:
    863                 BTIF_TRACE_DEBUG1(" %s() Authentication fail ", __FUNCTION__);
    864                 if (pairing_cb.autopair_attempts  == 1)
    865                 {
    866                     BTIF_TRACE_DEBUG1("%s(): Adding device to blacklist ", __FUNCTION__);
    867 
    868                     /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class  */
    869                     if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
    870                         check_cod(&bd_addr, COD_AV_HANDSFREE) ||
    871                         check_cod(&bd_addr, COD_AV_HEADPHONES) ||
    872                         check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
    873                         check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
    874                         check_cod(&bd_addr, COD_HID_POINTING))
    875                     {
    876                         btif_storage_add_device_to_autopair_blacklist (&bd_addr);
    877                     }
    878                     pairing_cb.autopair_attempts++;
    879 
    880                     /* Create the Bond once again */
    881                     BTIF_TRACE_DEBUG1("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
    882                     btif_dm_cb_create_bond (&bd_addr);
    883                     return;
    884                 }
    885                 else
    886                 {
    887                     /* if autopair attempts are more than 1, or not attempted */
    888                     status =  BT_STATUS_AUTH_FAILURE;
    889                 }
    890                 break;
    891 
    892             default:
    893                 status =  BT_STATUS_FAIL;
    894         }
    895         bond_state_changed(status, &bd_addr, state);
    896     }
    897 }
    898 
    899 /******************************************************************************
    900 **
    901 ** Function         btif_dm_search_devices_evt
    902 **
    903 ** Description      Executes search devices callback events in btif context
    904 **
    905 ** Returns          void
    906 **
    907 ******************************************************************************/
    908 static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
    909 {
    910     tBTA_DM_SEARCH *p_search_data;
    911     BTIF_TRACE_EVENT2("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
    912 
    913     switch (event)
    914     {
    915         case BTA_DM_DISC_RES_EVT:
    916         {
    917             p_search_data = (tBTA_DM_SEARCH *)p_param;
    918             /* Remote name update */
    919             if (strlen((const char *) p_search_data->disc_res.bd_name))
    920             {
    921                 bt_property_t properties[1];
    922                 bt_bdaddr_t bdaddr;
    923                 bt_status_t status;
    924 
    925                 properties[0].type = BT_PROPERTY_BDNAME;
    926                 properties[0].val = p_search_data->disc_res.bd_name;
    927                 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
    928                 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
    929 
    930                 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
    931                 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
    932                 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
    933                                  status, &bdaddr, 1, properties);
    934             }
    935             /* TODO: Services? */
    936         }
    937         break;
    938 
    939         case BTA_DM_INQ_RES_EVT:
    940         {
    941             /* inquiry result */
    942             UINT32 cod;
    943             UINT8 *p_eir_remote_name = NULL;
    944             bt_bdname_t bdname;
    945             bt_bdaddr_t bdaddr;
    946             UINT8 remote_name_len;
    947             UINT8 *p_cached_name = NULL;
    948             tBTA_SERVICE_MASK services = 0;
    949             bdstr_t bdstr;
    950 
    951             p_search_data = (tBTA_DM_SEARCH *)p_param;
    952             bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
    953 
    954             BTIF_TRACE_DEBUG3("%s() %s device_type = 0x%x\n", __FUNCTION__, bd2str(&bdaddr, &bdstr),
    955 #if (BLE_INCLUDED == TRUE)
    956                     p_search_data->inq_res.device_type);
    957 #else
    958                     BT_DEVICE_TYPE_BREDR);
    959 #endif
    960             bdname.name[0] = 0;
    961 
    962             cod = devclass2uint (p_search_data->inq_res.dev_class);
    963 
    964             if ( cod == 0) {
    965                 ALOGD("cod is 0, set as unclassified");
    966                 cod = COD_UNCLASSIFIED;
    967             }
    968 
    969             if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
    970                 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
    971 
    972             /* Check EIR for remote name and services */
    973             if (p_search_data->inq_res.p_eir)
    974             {
    975                 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
    976                 BTIF_TRACE_DEBUG2("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
    977                 /* TODO:  Get the service list and check to see which uuids we got and send it back to the client. */
    978             }
    979 
    980 
    981             {
    982                 bt_property_t properties[5];
    983                 bt_device_type_t dev_type;
    984                 uint32_t num_properties = 0;
    985                 bt_status_t status;
    986 
    987                 memset(properties, 0, sizeof(properties));
    988                 /* BD_ADDR */
    989                 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
    990                                     BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
    991                 num_properties++;
    992                 /* BD_NAME */
    993                 /* Don't send BDNAME if it is empty */
    994                 if (bdname.name[0]) {
    995                     BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
    996                                                BT_PROPERTY_BDNAME,
    997                                                strlen((char *)bdname.name), &bdname);
    998                     num_properties++;
    999                 }
   1000 
   1001                 /* DEV_CLASS */
   1002                 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
   1003                                     BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
   1004                 num_properties++;
   1005                 /* DEV_TYPE */
   1006 #if (BLE_INCLUDED == TRUE)
   1007                 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
   1008                 dev_type = p_search_data->inq_res.device_type;
   1009 #else
   1010                 dev_type = BT_DEVICE_TYPE_BREDR;
   1011 #endif
   1012                 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
   1013                                     BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
   1014                 num_properties++;
   1015                 /* RSSI */
   1016                 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
   1017                                     BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
   1018                                     &(p_search_data->inq_res.rssi));
   1019                 num_properties++;
   1020 
   1021                 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
   1022                 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
   1023 
   1024                 /* Callback to notify upper layer of device */
   1025                 HAL_CBACK(bt_hal_cbacks, device_found_cb,
   1026                                  num_properties, properties);
   1027             }
   1028         }
   1029         break;
   1030 
   1031         case BTA_DM_INQ_CMPL_EVT:
   1032         {
   1033         }
   1034         break;
   1035         case BTA_DM_DISC_CMPL_EVT:
   1036         {
   1037             HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
   1038         }
   1039         break;
   1040         case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
   1041         {
   1042            /* if inquiry is not in progress and we get a cancel event, then
   1043             * it means we are done with inquiry, but remote_name fetches are in
   1044             * progress
   1045             *
   1046             * if inquiry  is in progress, then we don't want to act on this cancel_cmpl_evt
   1047             * but instead wait for the cancel_cmpl_evt via the Busy Level
   1048             *
   1049             */
   1050            if (btif_dm_inquiry_in_progress == FALSE)
   1051            {
   1052                HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
   1053            }
   1054         }
   1055         break;
   1056     }
   1057 }
   1058 
   1059 /*******************************************************************************
   1060 **
   1061 ** Function         btif_dm_search_services_evt
   1062 **
   1063 ** Description      Executes search services event in btif context
   1064 **
   1065 ** Returns          void
   1066 **
   1067 *******************************************************************************/
   1068 static void btif_dm_search_services_evt(UINT16 event, char *p_param)
   1069 {
   1070     tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
   1071 
   1072     BTIF_TRACE_EVENT2("%s:  event = %d", __FUNCTION__, event);
   1073     switch (event)
   1074     {
   1075         case BTA_DM_DISC_RES_EVT:
   1076         {
   1077             bt_uuid_t uuid_arr[BT_MAX_NUM_UUIDS]; /* Max 32 services */
   1078             bt_property_t prop;
   1079             uint32_t i = 0,  j = 0;
   1080             bt_bdaddr_t bd_addr;
   1081             bt_status_t ret;
   1082 
   1083             bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
   1084 
   1085             BTIF_TRACE_DEBUG3("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
   1086                     p_data->disc_res.result, p_data->disc_res.services);
   1087             if  ((p_data->disc_res.result != BTA_SUCCESS) &&
   1088                  (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
   1089                  (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
   1090             {
   1091                 BTIF_TRACE_WARNING1("%s:SDP failed after bonding re-attempting", __FUNCTION__);
   1092                 pairing_cb.sdp_attempts++;
   1093                 btif_dm_get_remote_services(&bd_addr);
   1094                 return;
   1095             }
   1096             prop.type = BT_PROPERTY_UUIDS;
   1097             prop.len = 0;
   1098             if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
   1099             {
   1100                  prop.val = p_data->disc_res.p_uuid_list;
   1101                  prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
   1102                  for (i=0; i < p_data->disc_res.num_uuids; i++)
   1103                  {
   1104                       char temp[256];
   1105                       uuid_to_string((bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i*MAX_UUID_SIZE)), temp);
   1106                       BTIF_TRACE_ERROR2("Index: %d uuid:%s", i, temp);
   1107                  }
   1108             }
   1109 
   1110             /* onUuidChanged requires getBondedDevices to be populated.
   1111             ** bond_state_changed needs to be sent prior to remote_device_property
   1112             */
   1113             if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
   1114                 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0)&&
   1115                 pairing_cb.sdp_attempts > 0)
   1116             {
   1117                  BTIF_TRACE_DEBUG1("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
   1118                                    __FUNCTION__);
   1119                  pairing_cb.sdp_attempts  = 0;
   1120                  bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
   1121             }
   1122 
   1123             /* Also write this to the NVRAM */
   1124             ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
   1125             ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
   1126             /* Send the event to the BTIF */
   1127             HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
   1128                              BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
   1129         }
   1130         break;
   1131 
   1132         case BTA_DM_DISC_CMPL_EVT:
   1133             /* fixme */
   1134         break;
   1135 
   1136         default:
   1137         {
   1138             ASSERTC(0, "unhandled search services event", event);
   1139         }
   1140         break;
   1141     }
   1142 }
   1143 
   1144 /*******************************************************************************
   1145 **
   1146 ** Function         btif_dm_remote_service_record_evt
   1147 **
   1148 ** Description      Executes search service record event in btif context
   1149 **
   1150 ** Returns          void
   1151 **
   1152 *******************************************************************************/
   1153 static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
   1154 {
   1155     tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
   1156 
   1157     BTIF_TRACE_EVENT2("%s:  event = %d", __FUNCTION__, event);
   1158     switch (event)
   1159     {
   1160         case BTA_DM_DISC_RES_EVT:
   1161         {
   1162             bt_service_record_t rec;
   1163             bt_property_t prop;
   1164             uint32_t i = 0;
   1165             bt_bdaddr_t bd_addr;
   1166 
   1167             memset(&rec, 0, sizeof(bt_service_record_t));
   1168             bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
   1169 
   1170             BTIF_TRACE_DEBUG3("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
   1171                     p_data->disc_res.result, p_data->disc_res.services);
   1172             prop.type = BT_PROPERTY_SERVICE_RECORD;
   1173             prop.val = (void*)&rec;
   1174             prop.len = sizeof(rec);
   1175 
   1176             /* disc_res.result is overloaded with SCN. Cannot check result */
   1177             p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
   1178             /* TODO: Get the UUID as well */
   1179             rec.channel = p_data->disc_res.result - 3;
   1180             /* TODO: Need to get the service name using p_raw_data */
   1181             rec.name[0] = 0;
   1182 
   1183             HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
   1184                              BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
   1185         }
   1186         break;
   1187 
   1188         default:
   1189         {
   1190            ASSERTC(0, "unhandled remote service record event", event);
   1191         }
   1192         break;
   1193     }
   1194 }
   1195 
   1196 /*******************************************************************************
   1197 **
   1198 ** Function         btif_dm_upstreams_cback
   1199 **
   1200 ** Description      Executes UPSTREAMS events in btif context
   1201 **
   1202 ** Returns          void
   1203 **
   1204 *******************************************************************************/
   1205 static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
   1206 {
   1207     tBTA_DM_SEC_EVT dm_event = (tBTA_DM_SEC_EVT)event;
   1208     tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
   1209     tBTA_SERVICE_MASK service_mask;
   1210     uint32_t i;
   1211     bt_bdaddr_t bd_addr;
   1212 
   1213     BTIF_TRACE_EVENT1("btif_dm_upstreams_cback  ev: %s", dump_dm_event(event));
   1214 
   1215     switch (event)
   1216     {
   1217         case BTA_DM_ENABLE_EVT:
   1218         {
   1219              BD_NAME bdname;
   1220              bt_status_t status;
   1221              bt_property_t prop;
   1222              prop.type = BT_PROPERTY_BDNAME;
   1223              prop.len = BD_NAME_LEN;
   1224              prop.val = (void*)bdname;
   1225 
   1226              status = btif_storage_get_adapter_property(&prop);
   1227              /* Storage does not have a name yet.
   1228              ** Use the default name and write it to the chip
   1229              */
   1230              if (status != BT_STATUS_SUCCESS)
   1231              {
   1232                  BTA_DmSetDeviceName((char *)BTM_DEF_LOCAL_NAME);
   1233                  /* Hmmm...Should we store this too??? */
   1234              }
   1235              else
   1236              {
   1237                  /* A name exists in the storage. Make this the device name */
   1238                  BTA_DmSetDeviceName((char*)prop.val);
   1239              }
   1240 
   1241              /* for each of the enabled services in the mask, trigger the profile
   1242               * enable */
   1243              service_mask = btif_get_enabled_services_mask();
   1244              for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
   1245              {
   1246                  if (service_mask &
   1247                      (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
   1248                  {
   1249                      btif_in_execute_service_request(i, TRUE);
   1250                  }
   1251              }
   1252              /* clear control blocks */
   1253              memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
   1254 
   1255              /* This function will also trigger the adapter_properties_cb
   1256              ** and bonded_devices_info_cb
   1257              */
   1258              btif_storage_load_bonded_devices();
   1259 
   1260              btif_storage_load_autopair_device_list();
   1261 
   1262              btif_enable_bluetooth_evt(p_data->enable.status, p_data->enable.bd_addr);
   1263         }
   1264         break;
   1265 
   1266         case BTA_DM_DISABLE_EVT:
   1267             /* for each of the enabled services in the mask, trigger the profile
   1268              * disable */
   1269             service_mask = btif_get_enabled_services_mask();
   1270             for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
   1271             {
   1272                 if (service_mask &
   1273                     (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
   1274                 {
   1275                     btif_in_execute_service_request(i, FALSE);
   1276                 }
   1277             }
   1278             btif_disable_bluetooth_evt();
   1279             break;
   1280 
   1281         case BTA_DM_PIN_REQ_EVT:
   1282             btif_dm_pin_req_evt(&p_data->pin_req);
   1283             break;
   1284 
   1285         case BTA_DM_AUTH_CMPL_EVT:
   1286             btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
   1287             break;
   1288 
   1289         case BTA_DM_BOND_CANCEL_CMPL_EVT:
   1290             if (pairing_cb.state == BT_BOND_STATE_BONDING)
   1291             {
   1292                 bdcpy(bd_addr.address, pairing_cb.bd_addr);
   1293                 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
   1294             }
   1295             break;
   1296 
   1297         case BTA_DM_SP_CFM_REQ_EVT:
   1298             btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
   1299             break;
   1300         case BTA_DM_SP_KEY_NOTIF_EVT:
   1301             btif_dm_ssp_key_notif_evt(&p_data->key_notif);
   1302             break;
   1303 
   1304         case BTA_DM_DEV_UNPAIRED_EVT:
   1305             bdcpy(bd_addr.address, p_data->link_down.bd_addr);
   1306 
   1307             /*special handling for HID devices */
   1308             #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
   1309             if (check_cod_hid(&bd_addr, COD_HID_MAJOR))
   1310             {
   1311                 btif_hh_remove_device(bd_addr);
   1312             }
   1313             #endif
   1314             btif_storage_remove_bonded_device(&bd_addr);
   1315             bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
   1316             break;
   1317 
   1318         case BTA_DM_BUSY_LEVEL_EVT:
   1319         {
   1320 
   1321             if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
   1322             {
   1323                 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
   1324                 {
   1325                        HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
   1326                                                 BT_DISCOVERY_STARTED);
   1327                        btif_dm_inquiry_in_progress = TRUE;
   1328                 }
   1329                 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
   1330                 {
   1331                        HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
   1332                                                 BT_DISCOVERY_STOPPED);
   1333                        btif_dm_inquiry_in_progress = FALSE;
   1334                 }
   1335                 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
   1336                 {
   1337                        btif_dm_inquiry_in_progress = FALSE;
   1338                 }
   1339             }
   1340         }break;
   1341 
   1342         case BTA_DM_LINK_UP_EVT:
   1343             bdcpy(bd_addr.address, p_data->link_up.bd_addr);
   1344             BTIF_TRACE_DEBUG0("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
   1345 
   1346             btif_update_remote_version_property(&bd_addr);
   1347 
   1348             HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
   1349                       &bd_addr, BT_ACL_STATE_CONNECTED);
   1350             break;
   1351 
   1352         case BTA_DM_LINK_DOWN_EVT:
   1353             bdcpy(bd_addr.address, p_data->link_down.bd_addr);
   1354             BTIF_TRACE_DEBUG0("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
   1355             HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
   1356                       &bd_addr, BT_ACL_STATE_DISCONNECTED);
   1357             break;
   1358 
   1359         case BTA_DM_HW_ERROR_EVT:
   1360             BTIF_TRACE_ERROR0("Received H/W Error. ");
   1361             /* Flush storage data */
   1362             btif_config_flush();
   1363             usleep(100000); /* 100milliseconds */
   1364             /* Killing the process to force a restart as part of fault tolerance */
   1365             kill(getpid(), SIGKILL);
   1366             break;
   1367 
   1368 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
   1369         case BTA_DM_BLE_KEY_EVT:
   1370             BTIF_TRACE_DEBUG1("BTA_DM_BLE_KEY_EVT key_type=0x%02x ", p_data->ble_key.key_type);
   1371 
   1372             /* If this pairing is by-product of local initiated GATT client Read or Write,
   1373             BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
   1374             have setup properly. Setup pairing_cb and notify App about Bonding state now*/
   1375             if (pairing_cb.state != BT_BOND_STATE_BONDING)
   1376             {
   1377                 BTIF_TRACE_DEBUG0("Bond state not sent to App so far.Notify the app now");
   1378                 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
   1379                                    BT_BOND_STATE_BONDING);
   1380             }
   1381             else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
   1382             {
   1383                 BTIF_TRACE_ERROR1("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
   1384                 break;
   1385             }
   1386 
   1387             switch (p_data->ble_key.key_type)
   1388             {
   1389                 case BTA_LE_KEY_PENC:
   1390                     BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_PENC");
   1391                     pairing_cb.ble.is_penc_key_rcvd = TRUE;
   1392                     memcpy(pairing_cb.ble.penc_key.ltk,p_data->ble_key.key_value.penc_key.ltk, 16);
   1393                     memcpy(pairing_cb.ble.penc_key.rand, p_data->ble_key.key_value.penc_key.rand,8);
   1394                     pairing_cb.ble.penc_key.ediv = p_data->ble_key.key_value.penc_key.ediv;
   1395                     pairing_cb.ble.penc_key.sec_level = p_data->ble_key.key_value.penc_key.sec_level;
   1396 
   1397                     for (i=0; i<16; i++)
   1398                     {
   1399                         BTIF_TRACE_DEBUG2("pairing_cb.ble.penc_key.ltk[%d]=0x%02x",i,pairing_cb.ble.penc_key.ltk[i]);
   1400                     }
   1401                     for (i=0; i<8; i++)
   1402                     {
   1403                         BTIF_TRACE_DEBUG2("pairing_cb.ble.penc_key.rand[%d]=0x%02x",i,pairing_cb.ble.penc_key.rand[i]);
   1404                     }
   1405                     BTIF_TRACE_DEBUG1("pairing_cb.ble.penc_key.ediv=0x%04x",pairing_cb.ble.penc_key.ediv);
   1406                     BTIF_TRACE_DEBUG1("pairing_cb.ble.penc_key.sec_level=0x%02x",pairing_cb.ble.penc_key.sec_level);
   1407                     BTIF_TRACE_DEBUG1("pairing_cb.ble.penc_key.key_size=0x%02x",pairing_cb.ble.penc_key.key_size);
   1408                     break;
   1409 
   1410                 case BTA_LE_KEY_PID:
   1411                     BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_PID");
   1412                     pairing_cb.ble.is_pid_key_rcvd = TRUE;
   1413                     memcpy(pairing_cb.ble.pid_key, p_data->ble_key.key_value.pid_key.irk, 16);
   1414                     for (i=0; i<16; i++)
   1415                     {
   1416                         BTIF_TRACE_DEBUG2("pairing_cb.ble.pid_key[%d]=0x%02x",i,pairing_cb.ble.pid_key[i]);
   1417                     }
   1418                     break;
   1419 
   1420                 case BTA_LE_KEY_PCSRK:
   1421                     BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_PCSRK");
   1422                     pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
   1423                     pairing_cb.ble.pcsrk_key.counter = p_data->ble_key.key_value.pcsrk_key.counter;
   1424                     pairing_cb.ble.pcsrk_key.sec_level = p_data->ble_key.key_value.pcsrk_key.sec_level;
   1425                     memcpy(pairing_cb.ble.pcsrk_key.csrk,p_data->ble_key.key_value.pcsrk_key.csrk,16);
   1426 
   1427                     for (i=0; i<16; i++)
   1428                     {
   1429                         BTIF_TRACE_DEBUG2("pairing_cb.ble.pcsrk_key.csrk[%d]=0x%02x",i,pairing_cb.ble.pcsrk_key.csrk[i]);
   1430                     }
   1431                     BTIF_TRACE_DEBUG1("pairing_cb.ble.pcsrk_key.counter=0x%08x",pairing_cb.ble.pcsrk_key.counter);
   1432                     BTIF_TRACE_DEBUG1("pairing_cb.ble.pcsrk_key.sec_level=0x%02x",pairing_cb.ble.pcsrk_key.sec_level);
   1433                     break;
   1434 
   1435                 case BTA_LE_KEY_LENC:
   1436                     BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_LENC");
   1437                     pairing_cb.ble.is_lenc_key_rcvd = TRUE;
   1438                     pairing_cb.ble.lenc_key.div = p_data->ble_key.key_value.lenc_key.div;
   1439                     pairing_cb.ble.lenc_key.key_size = p_data->ble_key.key_value.lenc_key.key_size;
   1440                     pairing_cb.ble.lenc_key.sec_level = p_data->ble_key.key_value.lenc_key.sec_level;
   1441 
   1442                     BTIF_TRACE_DEBUG1("pairing_cb.ble.lenc_key.div=0x%04x",pairing_cb.ble.lenc_key.div);
   1443                     BTIF_TRACE_DEBUG1("pairing_cb.ble.lenc_key.key_size=0x%02x",pairing_cb.ble.lenc_key.key_size);
   1444                     BTIF_TRACE_DEBUG1("pairing_cb.ble.lenc_key.sec_level=0x%02x",pairing_cb.ble.lenc_key.sec_level);
   1445                     break;
   1446 
   1447 
   1448 
   1449                 case BTA_LE_KEY_LCSRK:
   1450                     BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_LCSRK");
   1451                     pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
   1452                     pairing_cb.ble.lcsrk_key.counter = p_data->ble_key.key_value.lcsrk_key.counter;
   1453                     pairing_cb.ble.lcsrk_key.div = p_data->ble_key.key_value.lcsrk_key.div;
   1454                     pairing_cb.ble.lcsrk_key.sec_level = p_data->ble_key.key_value.lcsrk_key.sec_level;
   1455 
   1456                     BTIF_TRACE_DEBUG1("pairing_cb.ble.lcsrk_key.div=0x%04x",pairing_cb.ble.lcsrk_key.div);
   1457                     BTIF_TRACE_DEBUG1("pairing_cb.ble.lcsrk_key.counter=0x%08x",pairing_cb.ble.lcsrk_key.counter);
   1458                     BTIF_TRACE_DEBUG1("pairing_cb.ble.lcsrk_key.sec_level=0x%02x",pairing_cb.ble.lcsrk_key.sec_level);
   1459 
   1460                     break;
   1461 
   1462                 default:
   1463                     BTIF_TRACE_ERROR1("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
   1464                     break;
   1465             }
   1466 
   1467             break;
   1468         case BTA_DM_BLE_SEC_REQ_EVT:
   1469             BTIF_TRACE_DEBUG0("BTA_DM_BLE_SEC_REQ_EVT. ");
   1470             btif_dm_ble_sec_req_evt(&p_data->ble_req);
   1471             break;
   1472         case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
   1473             BTIF_TRACE_DEBUG0("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
   1474             btif_dm_ble_key_notif_evt(&p_data->key_notif);
   1475             break;
   1476         case BTA_DM_BLE_PASSKEY_REQ_EVT:
   1477             BTIF_TRACE_DEBUG0("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
   1478             btif_dm_ble_passkey_req_evt(&p_data->pin_req);
   1479             break;
   1480         case BTA_DM_BLE_OOB_REQ_EVT:
   1481             BTIF_TRACE_DEBUG0("BTA_DM_BLE_OOB_REQ_EVT. ");
   1482             break;
   1483         case BTA_DM_BLE_LOCAL_IR_EVT:
   1484             BTIF_TRACE_DEBUG0("BTA_DM_BLE_LOCAL_IR_EVT. ");
   1485             ble_local_key_cb.is_id_keys_rcvd = TRUE;
   1486             memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
   1487             memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
   1488             memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
   1489             btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
   1490                                             BTIF_DM_LE_LOCAL_KEY_IR,
   1491                                             BT_OCTET16_LEN);
   1492             btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
   1493                                             BTIF_DM_LE_LOCAL_KEY_IRK,
   1494                                             BT_OCTET16_LEN);
   1495             btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
   1496                                             BTIF_DM_LE_LOCAL_KEY_DHK,
   1497                                             BT_OCTET16_LEN);
   1498             break;
   1499         case BTA_DM_BLE_LOCAL_ER_EVT:
   1500             BTIF_TRACE_DEBUG0("BTA_DM_BLE_LOCAL_ER_EVT. ");
   1501             ble_local_key_cb.is_er_rcvd = TRUE;
   1502             memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
   1503             btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
   1504                                             BTIF_DM_LE_LOCAL_KEY_ER,
   1505                                             BT_OCTET16_LEN);
   1506             break;
   1507 
   1508         case BTA_DM_BLE_AUTH_CMPL_EVT:
   1509             BTIF_TRACE_DEBUG0("BTA_DM_BLE_KEY_EVT. ");
   1510             btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
   1511             break;
   1512 #endif
   1513 
   1514         case BTA_DM_AUTHORIZE_EVT:
   1515         case BTA_DM_SIG_STRENGTH_EVT:
   1516         case BTA_DM_SP_RMT_OOB_EVT:
   1517         case BTA_DM_SP_KEYPRESS_EVT:
   1518         case BTA_DM_ROLE_CHG_EVT:
   1519 
   1520         default:
   1521             BTIF_TRACE_WARNING1( "btif_dm_cback : unhandled event (%d)", event );
   1522             break;
   1523     }
   1524 } /* btui_security_cback() */
   1525 
   1526 
   1527 /*******************************************************************************
   1528 **
   1529 ** Function         btif_dm_generic_evt
   1530 **
   1531 ** Description      Executes non-BTA upstream events in BTIF context
   1532 **
   1533 ** Returns          void
   1534 **
   1535 *******************************************************************************/
   1536 static void btif_dm_generic_evt(UINT16 event, char* p_param)
   1537 {
   1538     BTIF_TRACE_EVENT2("%s: event=%d", __FUNCTION__, event);
   1539     switch(event)
   1540     {
   1541         case BTIF_DM_CB_DISCOVERY_STARTED:
   1542         {
   1543             HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
   1544         }
   1545         break;
   1546 
   1547         case BTIF_DM_CB_CREATE_BOND:
   1548         {
   1549             btif_dm_cb_create_bond((bt_bdaddr_t *)p_param);
   1550         }
   1551         break;
   1552 
   1553         case BTIF_DM_CB_REMOVE_BOND:
   1554         {
   1555             btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
   1556         }
   1557         break;
   1558 
   1559         case BTIF_DM_CB_HID_REMOTE_NAME:
   1560         {
   1561             btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
   1562         }
   1563         break;
   1564 
   1565         case BTIF_DM_CB_BOND_STATE_BONDING:
   1566             {
   1567                 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
   1568             }
   1569             break;
   1570         case BTIF_DM_CB_LE_TX_TEST:
   1571         case BTIF_DM_CB_LE_RX_TEST:
   1572             {
   1573                 uint8_t status;
   1574                 STREAM_TO_UINT8(status, p_param);
   1575                 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
   1576                       (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
   1577             }
   1578             break;
   1579         case BTIF_DM_CB_LE_TEST_END:
   1580             {
   1581                 uint8_t status;
   1582                 uint16_t count = 0;
   1583                 STREAM_TO_UINT8(status, p_param);
   1584                 if (status == 0)
   1585                     STREAM_TO_UINT16(count, p_param);
   1586                 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
   1587                       (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
   1588             }
   1589             break;
   1590         default:
   1591         {
   1592             BTIF_TRACE_WARNING2("%s : Unknown event 0x%x", __FUNCTION__, event);
   1593         }
   1594         break;
   1595     }
   1596 }
   1597 
   1598 /*******************************************************************************
   1599 **
   1600 ** Function         bte_dm_evt
   1601 **
   1602 ** Description      Switches context from BTE to BTIF for all DM events
   1603 **
   1604 ** Returns          void
   1605 **
   1606 *******************************************************************************/
   1607 
   1608 void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
   1609 {
   1610     bt_status_t status;
   1611 
   1612     /* switch context to btif task context (copy full union size for convenience) */
   1613     status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event, (void*)p_data, sizeof(tBTA_DM_SEC), NULL);
   1614 
   1615     /* catch any failed context transfers */
   1616     ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
   1617 }
   1618 
   1619 /*******************************************************************************
   1620 **
   1621 ** Function         bte_search_devices_evt
   1622 **
   1623 ** Description      Switches context from BTE to BTIF for DM search events
   1624 **
   1625 ** Returns          void
   1626 **
   1627 *******************************************************************************/
   1628 static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
   1629 {
   1630     UINT16 param_len = 0;
   1631 
   1632     if (p_data)
   1633         param_len += sizeof(tBTA_DM_SEARCH);
   1634     /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
   1635     switch (event)
   1636     {
   1637         case BTA_DM_INQ_RES_EVT:
   1638         {
   1639             if (p_data->inq_res.p_eir)
   1640                 param_len += HCI_EXT_INQ_RESPONSE_LEN;
   1641         }
   1642         break;
   1643 
   1644         case BTA_DM_DISC_RES_EVT:
   1645         {
   1646             if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
   1647                 param_len += p_data->disc_res.raw_data_size;
   1648         }
   1649         break;
   1650     }
   1651     BTIF_TRACE_DEBUG3("%s event=%s param_len=%d", __FUNCTION__, dump_dm_search_event(event), param_len);
   1652 
   1653     /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
   1654     if (event == BTA_DM_INQ_RES_EVT)
   1655         p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
   1656 
   1657     btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
   1658         (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
   1659 }
   1660 
   1661 /*******************************************************************************
   1662 **
   1663 ** Function         bte_dm_search_services_evt
   1664 **
   1665 ** Description      Switches context from BTE to BTIF for DM search services
   1666 **                  event
   1667 **
   1668 ** Returns          void
   1669 **
   1670 *******************************************************************************/
   1671 static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
   1672 {
   1673     UINT16 param_len = 0;
   1674    if (p_data)
   1675        param_len += sizeof(tBTA_DM_SEARCH);
   1676    switch (event)
   1677    {
   1678          case BTA_DM_DISC_RES_EVT:
   1679          {
   1680              if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
   1681                   param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
   1682              }
   1683          } break;
   1684    }
   1685    /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
   1686     * if raw_data is needed. */
   1687    btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
   1688          (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
   1689 }
   1690 
   1691 /*******************************************************************************
   1692 **
   1693 ** Function         bte_dm_remote_service_record_evt
   1694 **
   1695 ** Description      Switches context from BTE to BTIF for DM search service
   1696 **                  record event
   1697 **
   1698 ** Returns          void
   1699 **
   1700 *******************************************************************************/
   1701 static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
   1702 {
   1703    /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
   1704    btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
   1705 }
   1706 
   1707 /*****************************************************************************
   1708 **
   1709 **   btif api functions (no context switch)
   1710 **
   1711 *****************************************************************************/
   1712 
   1713 /*******************************************************************************
   1714 **
   1715 ** Function         btif_dm_start_discovery
   1716 **
   1717 ** Description      Start device discovery/inquiry
   1718 **
   1719 ** Returns          bt_status_t
   1720 **
   1721 *******************************************************************************/
   1722 bt_status_t btif_dm_start_discovery(void)
   1723 {
   1724     tBTA_DM_INQ inq_params;
   1725     tBTA_SERVICE_MASK services = 0;
   1726 
   1727     BTIF_TRACE_EVENT1("%s", __FUNCTION__);
   1728     /* TODO: Do we need to handle multiple inquiries at the same time? */
   1729 
   1730     /* Set inquiry params and call API */
   1731 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
   1732     inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
   1733 #else
   1734     inq_params.mode = BTA_DM_GENERAL_INQUIRY;
   1735 #endif
   1736     inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
   1737 
   1738     inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
   1739     inq_params.report_dup = TRUE;
   1740 
   1741     inq_params.filter_type = BTA_DM_INQ_CLR;
   1742     /* TODO: Filter device by BDA needs to be implemented here */
   1743 
   1744     /* Will be enabled to TRUE once inquiry busy level has been received */
   1745     btif_dm_inquiry_in_progress = FALSE;
   1746     /* find nearby devices */
   1747     BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
   1748 
   1749     return BT_STATUS_SUCCESS;
   1750 }
   1751 
   1752 /*******************************************************************************
   1753 **
   1754 ** Function         btif_dm_cancel_discovery
   1755 **
   1756 ** Description      Cancels search
   1757 **
   1758 ** Returns          bt_status_t
   1759 **
   1760 *******************************************************************************/
   1761 bt_status_t btif_dm_cancel_discovery(void)
   1762 {
   1763     BTIF_TRACE_EVENT1("%s", __FUNCTION__);
   1764     BTA_DmSearchCancel();
   1765     return BT_STATUS_SUCCESS;
   1766 }
   1767 
   1768 /*******************************************************************************
   1769 **
   1770 ** Function         btif_dm_create_bond
   1771 **
   1772 ** Description      Initiate bonding with the specified device
   1773 **
   1774 ** Returns          bt_status_t
   1775 **
   1776 *******************************************************************************/
   1777 bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr)
   1778 {
   1779     bdstr_t bdstr;
   1780 
   1781     BTIF_TRACE_EVENT2("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *) bd_addr, &bdstr));
   1782     if (pairing_cb.state != BT_BOND_STATE_NONE)
   1783         return BT_STATUS_BUSY;
   1784 
   1785     btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
   1786                           (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
   1787 
   1788     return BT_STATUS_SUCCESS;
   1789 }
   1790 
   1791 /*******************************************************************************
   1792 **
   1793 ** Function         btif_dm_cancel_bond
   1794 **
   1795 ** Description      Initiate bonding with the specified device
   1796 **
   1797 ** Returns          bt_status_t
   1798 **
   1799 *******************************************************************************/
   1800 
   1801 bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
   1802 {
   1803     bdstr_t bdstr;
   1804 
   1805     BTIF_TRACE_EVENT2("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *)bd_addr, &bdstr));
   1806 
   1807     /* TODO:
   1808     **  1. Restore scan modes
   1809     **  2. special handling for HID devices
   1810     */
   1811     if (pairing_cb.state == BT_BOND_STATE_BONDING)
   1812     {
   1813 
   1814 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
   1815 
   1816         if (pairing_cb.is_ssp)
   1817         {
   1818             if (pairing_cb.is_le_only)
   1819             {
   1820                 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
   1821             }
   1822             else
   1823                 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
   1824         }
   1825         else
   1826         {
   1827             if (pairing_cb.is_le_only)
   1828             {
   1829                 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
   1830             }
   1831             else
   1832             {
   1833                 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
   1834             }
   1835         /* Cancel bonding, in case it is in ACL connection setup state */
   1836         BTA_DmBondCancel ((UINT8 *)bd_addr->address);
   1837         }
   1838 
   1839 #else
   1840         if (pairing_cb.is_ssp)
   1841         {
   1842             BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
   1843         }
   1844         else
   1845         {
   1846             BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
   1847         }
   1848         /* Cancel bonding, in case it is in ACL connection setup state */
   1849         BTA_DmBondCancel ((UINT8 *)bd_addr->address);
   1850         btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
   1851 #endif
   1852     }
   1853 
   1854     return BT_STATUS_SUCCESS;
   1855 }
   1856 
   1857 /*******************************************************************************
   1858 **
   1859 ** Function         btif_dm_remove_bond
   1860 **
   1861 ** Description      Removes bonding with the specified device
   1862 **
   1863 ** Returns          bt_status_t
   1864 **
   1865 *******************************************************************************/
   1866 
   1867 bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
   1868 {
   1869     bdstr_t bdstr;
   1870 
   1871     BTIF_TRACE_EVENT2("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *)bd_addr, &bdstr));
   1872     btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
   1873                           (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
   1874 
   1875     return BT_STATUS_SUCCESS;
   1876 }
   1877 
   1878 /*******************************************************************************
   1879 **
   1880 ** Function         btif_dm_pin_reply
   1881 **
   1882 ** Description      BT legacy pairing - PIN code reply
   1883 **
   1884 ** Returns          bt_status_t
   1885 **
   1886 *******************************************************************************/
   1887 
   1888 bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
   1889                                uint8_t pin_len, bt_pin_code_t *pin_code)
   1890 {
   1891     BTIF_TRACE_EVENT2("%s: accept=%d", __FUNCTION__, accept);
   1892 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
   1893 
   1894     if (pairing_cb.is_le_only)
   1895     {
   1896         int i;
   1897         UINT32 passkey = 0;
   1898         int multi[] = {100000, 10000, 1000, 100, 10,1};
   1899         BD_ADDR remote_bd_addr;
   1900         bdcpy(remote_bd_addr, bd_addr->address);
   1901         for (i = 0; i < 6; i++)
   1902         {
   1903             passkey += (multi[i] * (pin_code->pin[i] - '0'));
   1904         }
   1905         BTIF_TRACE_DEBUG1("btif_dm_pin_reply: passkey: %d", passkey);
   1906         BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
   1907 
   1908     }
   1909     else
   1910     {
   1911         BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
   1912         if (accept)
   1913             pairing_cb.pin_code_len = pin_len;
   1914     }
   1915 #else
   1916     BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
   1917 
   1918     if (accept)
   1919         pairing_cb.pin_code_len = pin_len;
   1920 #endif
   1921     return BT_STATUS_SUCCESS;
   1922 }
   1923 
   1924 /*******************************************************************************
   1925 **
   1926 ** Function         btif_dm_ssp_reply
   1927 **
   1928 ** Description      BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
   1929 **
   1930 ** Returns          bt_status_t
   1931 **
   1932 *******************************************************************************/
   1933 
   1934 bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
   1935                                  bt_ssp_variant_t variant, uint8_t accept,
   1936                                  uint32_t passkey)
   1937 {
   1938     if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
   1939     {
   1940         /* This is not implemented in the stack.
   1941          * For devices with display, this is not needed
   1942         */
   1943         BTIF_TRACE_WARNING1("%s: Not implemented", __FUNCTION__);
   1944         return BT_STATUS_FAIL;
   1945     }
   1946     /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
   1947     BTIF_TRACE_EVENT2("%s: accept=%d", __FUNCTION__, accept);
   1948 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
   1949     if (pairing_cb.is_le_only)
   1950     {
   1951         if (accept)
   1952             BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
   1953         else
   1954             BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
   1955     }
   1956     else
   1957         BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
   1958 
   1959 #else
   1960     BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
   1961 #endif
   1962     return BT_STATUS_SUCCESS;
   1963 }
   1964 
   1965 /*******************************************************************************
   1966 **
   1967 ** Function         btif_dm_get_adapter_property
   1968 **
   1969 ** Description     Queries the BTA for the adapter property
   1970 **
   1971 ** Returns          bt_status_t
   1972 **
   1973 *******************************************************************************/
   1974 bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
   1975 {
   1976     bt_status_t status;
   1977 
   1978     BTIF_TRACE_EVENT2("%s: type=0x%x", __FUNCTION__, prop->type);
   1979     switch (prop->type)
   1980     {
   1981         case BT_PROPERTY_BDNAME:
   1982         {
   1983             bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
   1984             strcpy((char *)bd_name->name, (char *)BTM_DEF_LOCAL_NAME);
   1985             prop->len = strlen((char *)bd_name->name);
   1986         }
   1987         break;
   1988 
   1989         case BT_PROPERTY_ADAPTER_SCAN_MODE:
   1990         {
   1991             /* if the storage does not have it. Most likely app never set it. Default is NONE */
   1992             bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
   1993             *mode = BT_SCAN_MODE_NONE;
   1994             prop->len = sizeof(bt_scan_mode_t);
   1995         }
   1996         break;
   1997 
   1998         case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
   1999         {
   2000             uint32_t *tmt = (uint32_t*)prop->val;
   2001             *tmt = 120; /* default to 120s, if not found in NV */
   2002             prop->len = sizeof(uint32_t);
   2003         }
   2004         break;
   2005 
   2006         default:
   2007             prop->len = 0;
   2008             return BT_STATUS_FAIL;
   2009     }
   2010     return BT_STATUS_SUCCESS;
   2011 }
   2012 
   2013 /*******************************************************************************
   2014 **
   2015 ** Function         btif_dm_get_remote_services
   2016 **
   2017 ** Description      Start SDP to get remote services
   2018 **
   2019 ** Returns          bt_status_t
   2020 **
   2021 *******************************************************************************/
   2022 bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
   2023 {
   2024     bdstr_t bdstr;
   2025 
   2026     BTIF_TRACE_EVENT2("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
   2027 
   2028     BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
   2029                    bte_dm_search_services_evt, TRUE);
   2030 
   2031     return BT_STATUS_SUCCESS;
   2032 }
   2033 
   2034 /*******************************************************************************
   2035 **
   2036 ** Function         btif_dm_get_remote_service_record
   2037 **
   2038 ** Description      Start SDP to get remote service record
   2039 **
   2040 **
   2041 ** Returns          bt_status_t
   2042 *******************************************************************************/
   2043 bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
   2044                                                     bt_uuid_t *uuid)
   2045 {
   2046     tSDP_UUID sdp_uuid;
   2047     bdstr_t bdstr;
   2048 
   2049     BTIF_TRACE_EVENT2("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
   2050 
   2051     sdp_uuid.len = MAX_UUID_SIZE;
   2052     memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
   2053 
   2054     BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
   2055                        bte_dm_remote_service_record_evt, TRUE);
   2056 
   2057     return BT_STATUS_SUCCESS;
   2058 }
   2059 
   2060 void btif_dm_execute_service_request(UINT16 event, char *p_param)
   2061 {
   2062     BOOLEAN b_enable = FALSE;
   2063     bt_status_t status;
   2064     if (event == BTIF_DM_ENABLE_SERVICE)
   2065     {
   2066         b_enable = TRUE;
   2067     }
   2068     status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
   2069     if (status == BT_STATUS_SUCCESS)
   2070     {
   2071         bt_property_t property;
   2072         bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
   2073 
   2074         /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
   2075         BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
   2076                                     sizeof(local_uuids), local_uuids);
   2077         btif_storage_get_adapter_property(&property);
   2078         HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
   2079                           BT_STATUS_SUCCESS, 1, &property);
   2080     }
   2081     return;
   2082 }
   2083 
   2084 #if (BTM_OOB_INCLUDED == TRUE)
   2085 void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA  *p_oob_data)
   2086 {
   2087     if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
   2088         oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
   2089     {
   2090         *p_oob_data = FALSE;
   2091     }
   2092     else
   2093     {
   2094         *p_oob_data = TRUE;
   2095     }
   2096     BTIF_TRACE_DEBUG1("btif_dm_set_oob_for_io_req *p_oob_data=%d", *p_oob_data);
   2097 }
   2098 #endif /* BTM_OOB_INCLUDED */
   2099 
   2100 #ifdef BTIF_DM_OOB_TEST
   2101 void btif_dm_load_local_oob(void)
   2102 {
   2103     char prop_oob[PROPERTY_VALUE_MAX];
   2104     property_get("service.brcm.bt.oob", prop_oob, "3");
   2105     BTIF_TRACE_DEBUG1("btif_dm_load_local_oob prop_oob = %s",prop_oob);
   2106     if (prop_oob[0] != '3')
   2107     {
   2108 #if (BTM_OOB_INCLUDED == TRUE)
   2109         if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
   2110             oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
   2111         {
   2112             BTIF_TRACE_DEBUG0("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
   2113             BTA_DmLocalOob();
   2114         }
   2115 #else
   2116         BTIF_TRACE_ERROR0("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
   2117 #endif
   2118     }
   2119 }
   2120 
   2121 void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
   2122 {
   2123     FILE *fp;
   2124     char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
   2125     char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
   2126     char *path = NULL;
   2127     char prop_oob[PROPERTY_VALUE_MAX];
   2128     BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob: valid=%d", valid);
   2129     if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
   2130         oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
   2131         valid)
   2132     {
   2133         BTIF_TRACE_DEBUG0("save local OOB data in memory");
   2134         memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
   2135         memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
   2136         property_get("service.brcm.bt.oob", prop_oob, "3");
   2137         BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
   2138         if (prop_oob[0] == '1')
   2139             path = path_a;
   2140         else if (prop_oob[0] == '2')
   2141             path = path_b;
   2142         if (path)
   2143         {
   2144             fp = fopen(path, "wb+");
   2145             if (fp == NULL)
   2146             {
   2147                 BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob: failed to save local OOB data to %s", path);
   2148             }
   2149             else
   2150             {
   2151                 BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob: save local OOB data into file %s",path);
   2152                 fwrite (c , 1 , BT_OCTET16_LEN , fp );
   2153                 fwrite (r , 1 , BT_OCTET16_LEN , fp );
   2154                 fclose(fp);
   2155             }
   2156         }
   2157     }
   2158 }
   2159 BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr,  BT_OCTET16 p_c, BT_OCTET16 p_r)
   2160 {
   2161     char t[128];
   2162     FILE *fp;
   2163     char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
   2164     char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
   2165     char *path = NULL;
   2166     char prop_oob[PROPERTY_VALUE_MAX];
   2167     BOOLEAN result = FALSE;
   2168     bt_bdaddr_t bt_bd_addr;
   2169     bdcpy(oob_cb.oob_bdaddr, bd_addr);
   2170     property_get("service.brcm.bt.oob", prop_oob, "3");
   2171     BTIF_TRACE_DEBUG1("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
   2172     if (prop_oob[0] == '1')
   2173         path = path_b;
   2174     else if (prop_oob[0] == '2')
   2175         path = path_a;
   2176     if (path)
   2177     {
   2178         fp = fopen(path, "rb");
   2179         if (fp == NULL)
   2180         {
   2181             BTIF_TRACE_DEBUG1("btapp_dm_rmt_oob_reply: failed to read OOB keys from %s",path);
   2182             return FALSE;
   2183         }
   2184         else
   2185         {
   2186             BTIF_TRACE_DEBUG1("btif_dm_proc_rmt_oob: read OOB data from %s",path);
   2187             fread (p_c , 1 , BT_OCTET16_LEN , fp );
   2188             fread (p_r , 1 , BT_OCTET16_LEN , fp );
   2189             fclose(fp);
   2190         }
   2191         BTIF_TRACE_DEBUG0("----btif_dm_proc_rmt_oob: TRUE");
   2192         sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
   2193                 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
   2194                 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
   2195         BTIF_TRACE_DEBUG1("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
   2196         sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
   2197                 p_c[0], p_c[1], p_c[2],  p_c[3],  p_c[4],  p_c[5],  p_c[6],  p_c[7],
   2198                 p_c[8], p_c[9], p_c[10], p_c[11], p_c[12], p_c[13], p_c[14], p_c[15]);
   2199         BTIF_TRACE_DEBUG1("----btif_dm_proc_rmt_oob: c = %s",t);
   2200         sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
   2201                 p_r[0], p_r[1], p_r[2],  p_r[3],  p_r[4],  p_r[5],  p_r[6],  p_r[7],
   2202                 p_r[8], p_r[9], p_r[10], p_r[11], p_r[12], p_r[13], p_r[14], p_r[15]);
   2203         BTIF_TRACE_DEBUG1("----btif_dm_proc_rmt_oob: r = %s",t);
   2204         bdcpy(bt_bd_addr.address, bd_addr);
   2205         btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
   2206                               (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
   2207         result = TRUE;
   2208     }
   2209     BTIF_TRACE_DEBUG1("btif_dm_proc_rmt_oob result=%d",result);
   2210     return result;
   2211 }
   2212 #endif /*  BTIF_DM_OOB_TEST */
   2213 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
   2214 
   2215 static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
   2216 {
   2217     bt_bdaddr_t bd_addr;
   2218     bt_bdname_t bd_name;
   2219     UINT32 cod;
   2220 
   2221     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   2222 
   2223     /* Remote name update */
   2224     btif_update_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
   2225                                           NULL, BT_DEVICE_TYPE_BLE);
   2226     bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
   2227     memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
   2228 
   2229     bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
   2230     pairing_cb.is_ssp = FALSE;
   2231     cod = COD_UNCLASSIFIED;
   2232 
   2233     HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
   2234               cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
   2235               p_ssp_key_notif->passkey);
   2236 }
   2237 
   2238 /*******************************************************************************
   2239 **
   2240 ** Function         btif_dm_ble_auth_cmpl_evt
   2241 **
   2242 ** Description      Executes authentication complete event in btif context
   2243 **
   2244 ** Returns          void
   2245 **
   2246 *******************************************************************************/
   2247 static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
   2248 {
   2249     /* Save link key, if not temporary */
   2250     bt_bdaddr_t bd_addr;
   2251     bt_status_t status = BT_STATUS_FAIL;
   2252     bt_bond_state_t state = BT_BOND_STATE_NONE;
   2253 
   2254     bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
   2255     if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
   2256     {
   2257         /* store keys */
   2258     }
   2259     if (p_auth_cmpl->success)
   2260     {
   2261         status = BT_STATUS_SUCCESS;
   2262         state = BT_BOND_STATE_BONDED;
   2263 
   2264         btif_dm_save_ble_bonding_keys();
   2265         BTA_GATTC_Refresh(bd_addr.address);
   2266         btif_dm_get_remote_services(&bd_addr);
   2267     }
   2268     else
   2269     {
   2270         /*Map the HCI fail reason  to  bt status  */
   2271         switch (p_auth_cmpl->fail_reason)
   2272         {
   2273             default:
   2274                 btif_dm_remove_ble_bonding_keys();
   2275                 status =  BT_STATUS_FAIL;
   2276                 break;
   2277         }
   2278     }
   2279     bond_state_changed(status, &bd_addr, state);
   2280 }
   2281 
   2282 
   2283 
   2284 void    btif_dm_load_ble_local_keys(void)
   2285 {
   2286     bt_status_t bt_status;
   2287 
   2288     memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
   2289 
   2290     if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
   2291                                        BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
   2292     {
   2293         ble_local_key_cb.is_er_rcvd = TRUE;
   2294         BTIF_TRACE_DEBUG1("%s BLE ER key loaded",__FUNCTION__ );
   2295     }
   2296 
   2297     if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
   2298                                         BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
   2299         (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
   2300                                         BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
   2301         (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
   2302                                         BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
   2303     {
   2304         ble_local_key_cb.is_id_keys_rcvd = TRUE;
   2305         BTIF_TRACE_DEBUG1("%s BLE ID keys loaded",__FUNCTION__ );
   2306     }
   2307 
   2308 }
   2309 void    btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
   2310                                    tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
   2311 {
   2312     if (ble_local_key_cb.is_er_rcvd )
   2313     {
   2314         memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
   2315         *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
   2316     }
   2317 
   2318     if (ble_local_key_cb.is_id_keys_rcvd)
   2319     {
   2320         memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
   2321         memcpy(&p_id_keys->irk[0],  &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
   2322         memcpy(&p_id_keys->dhk[0],  &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
   2323         *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
   2324     }
   2325     BTIF_TRACE_DEBUG2("%s  *p_key_mask=0x%02x",__FUNCTION__,   *p_key_mask);
   2326 }
   2327 
   2328 void btif_dm_save_ble_bonding_keys(void)
   2329 {
   2330 
   2331     bt_bdaddr_t bd_addr;
   2332 
   2333     BTIF_TRACE_DEBUG1("%s",__FUNCTION__ );
   2334 
   2335     bdcpy(bd_addr.address, pairing_cb.bd_addr);
   2336 
   2337     if (pairing_cb.ble.is_penc_key_rcvd)
   2338     {
   2339         btif_storage_add_ble_bonding_key(&bd_addr,
   2340                                          (char *) &pairing_cb.ble.penc_key,
   2341                                          BTIF_DM_LE_KEY_PENC,
   2342                                          sizeof(btif_dm_ble_penc_keys_t));
   2343     }
   2344 
   2345     if (pairing_cb.ble.is_pid_key_rcvd)
   2346     {
   2347         btif_storage_add_ble_bonding_key(&bd_addr,
   2348                                          (char *) &pairing_cb.ble.pid_key[0],
   2349                                          BTIF_DM_LE_KEY_PID,
   2350                                          BT_OCTET16_LEN);
   2351     }
   2352 
   2353 
   2354     if (pairing_cb.ble.is_pcsrk_key_rcvd)
   2355     {
   2356         btif_storage_add_ble_bonding_key(&bd_addr,
   2357                                          (char *) &pairing_cb.ble.pcsrk_key,
   2358                                          BTIF_DM_LE_KEY_PCSRK,
   2359                                          sizeof(btif_dm_ble_pcsrk_keys_t));
   2360     }
   2361 
   2362 
   2363     if (pairing_cb.ble.is_lenc_key_rcvd)
   2364     {
   2365         btif_storage_add_ble_bonding_key(&bd_addr,
   2366                                          (char *) &pairing_cb.ble.lenc_key,
   2367                                          BTIF_DM_LE_KEY_LENC,
   2368                                          sizeof(btif_dm_ble_lenc_keys_t));
   2369     }
   2370 
   2371     if (pairing_cb.ble.is_lcsrk_key_rcvd)
   2372     {
   2373         btif_storage_add_ble_bonding_key(&bd_addr,
   2374                                          (char *) &pairing_cb.ble.lcsrk_key,
   2375                                          BTIF_DM_LE_KEY_LCSRK,
   2376                                          sizeof(btif_dm_ble_lcsrk_keys_t));
   2377     }
   2378 
   2379 }
   2380 
   2381 
   2382 void btif_dm_remove_ble_bonding_keys(void)
   2383 {
   2384     bt_bdaddr_t bd_addr;
   2385 
   2386     BTIF_TRACE_DEBUG1("%s",__FUNCTION__ );
   2387 
   2388     bdcpy(bd_addr.address, pairing_cb.bd_addr);
   2389     btif_storage_remove_ble_bonding_keys(&bd_addr);
   2390 }
   2391 
   2392 
   2393 /*******************************************************************************
   2394 **
   2395 ** Function         btif_dm_ble_sec_req_evt
   2396 **
   2397 ** Description      Eprocess security request event in btif context
   2398 **
   2399 ** Returns          void
   2400 **
   2401 *******************************************************************************/
   2402 void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
   2403 {
   2404     bt_bdaddr_t bd_addr;
   2405     bt_bdname_t bd_name;
   2406     UINT32 cod;
   2407     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   2408 
   2409     if (pairing_cb.state == BT_BOND_STATE_BONDING)
   2410     {
   2411         BTIF_TRACE_DEBUG1("%s Discard security request", __FUNCTION__);
   2412         return;
   2413     }
   2414 
   2415     /* Remote name update */
   2416     btif_update_remote_properties(p_ble_req->bd_addr,p_ble_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
   2417 
   2418     bdcpy(bd_addr.address, p_ble_req->bd_addr);
   2419     memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
   2420 
   2421     bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
   2422 
   2423     pairing_cb.is_temp = FALSE;
   2424     pairing_cb.is_le_only = TRUE;
   2425     pairing_cb.is_ssp = TRUE;
   2426 
   2427     cod = COD_UNCLASSIFIED;
   2428 
   2429     HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
   2430               BT_SSP_VARIANT_CONSENT, 0);
   2431 }
   2432 
   2433 
   2434 
   2435 /*******************************************************************************
   2436 **
   2437 ** Function         btif_dm_ble_passkey_req_evt
   2438 **
   2439 ** Description      Executes pin request event in btif context
   2440 **
   2441 ** Returns          void
   2442 **
   2443 *******************************************************************************/
   2444 static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
   2445 {
   2446     bt_bdaddr_t bd_addr;
   2447     bt_bdname_t bd_name;
   2448     UINT32 cod;
   2449 
   2450     /* Remote name update */
   2451     btif_update_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
   2452 
   2453     bdcpy(bd_addr.address, p_pin_req->bd_addr);
   2454     memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
   2455 
   2456     bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
   2457     pairing_cb.is_le_only = TRUE;
   2458 
   2459     cod = COD_UNCLASSIFIED;
   2460 
   2461     HAL_CBACK(bt_hal_cbacks, pin_request_cb,
   2462               &bd_addr, &bd_name, cod);
   2463 }
   2464 
   2465 
   2466 void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
   2467                                            tBT_DEVICE_TYPE dev_type)
   2468 {
   2469    btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
   2470 }
   2471 
   2472 static void btif_dm_ble_tx_test_cback(void *p)
   2473 {
   2474     btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
   2475                           (char *)p, 1, NULL);
   2476 }
   2477 
   2478 static void btif_dm_ble_rx_test_cback(void *p)
   2479 {
   2480     btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
   2481                           (char *)p, 1, NULL);
   2482 }
   2483 
   2484 static void btif_dm_ble_test_end_cback(void *p)
   2485 {
   2486     btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
   2487                           (char *)p, 3, NULL);
   2488 }
   2489 /*******************************************************************************
   2490 **
   2491 ** Function         btif_le_test_mode
   2492 **
   2493 ** Description     Sends a HCI BLE Test command to the Controller
   2494 **
   2495 ** Returns          BT_STATUS_SUCCESS on success
   2496 **
   2497 *******************************************************************************/
   2498 bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
   2499 {
   2500      switch (opcode) {
   2501          case HCI_BLE_TRANSMITTER_TEST:
   2502              if (len != 3) return BT_STATUS_PARM_INVALID;
   2503              BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
   2504              break;
   2505          case HCI_BLE_RECEIVER_TEST:
   2506              if (len != 1) return BT_STATUS_PARM_INVALID;
   2507              BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
   2508              break;
   2509          case HCI_BLE_TEST_END:
   2510              BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
   2511              break;
   2512          default:
   2513              BTIF_TRACE_ERROR2("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
   2514              return BT_STATUS_UNSUPPORTED;
   2515      }
   2516      return BT_STATUS_SUCCESS;
   2517 }
   2518 
   2519 #endif
   2520 
   2521 void btif_dm_on_disable()
   2522 {
   2523     /* cancel any pending pairing requests */
   2524     if (pairing_cb.state == BT_BOND_STATE_BONDING)
   2525     {
   2526         bt_bdaddr_t bd_addr;
   2527 
   2528         BTIF_TRACE_DEBUG1("%s: Cancel pending pairing request", __FUNCTION__);
   2529         bdcpy(bd_addr.address, pairing_cb.bd_addr);
   2530         btif_dm_cancel_bond(&bd_addr);
   2531     }
   2532 }
   2533