btif_dm.c revision 4c03040fa975cc23ea27b183908b1008bb3231f9
117a6149219387862cea517538cb6af084d71e595sewardj/******************************************************************************
28618009823fe08e4f745be95eaf1ca204fc56709sewardj *
38618009823fe08e4f745be95eaf1ca204fc56709sewardj *  Copyright (C) 2009-2012 Broadcom Corporation
48618009823fe08e4f745be95eaf1ca204fc56709sewardj *
5fb27ca7c4f793dea4e95e01c5e1a9b7b0a4288basewardj *  Licensed under the Apache License, Version 2.0 (the "License");
68618009823fe08e4f745be95eaf1ca204fc56709sewardj *  you may not use this file except in compliance with the License.
717a6149219387862cea517538cb6af084d71e595sewardj *  You may obtain a copy of the License at:
817a6149219387862cea517538cb6af084d71e595sewardj *
917a6149219387862cea517538cb6af084d71e595sewardj *  http://www.apache.org/licenses/LICENSE-2.0
1017a6149219387862cea517538cb6af084d71e595sewardj *
1117a6149219387862cea517538cb6af084d71e595sewardj *  Unless required by applicable law or agreed to in writing, software
1217a6149219387862cea517538cb6af084d71e595sewardj *  distributed under the License is distributed on an "AS IS" BASIS,
1317a6149219387862cea517538cb6af084d71e595sewardj *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1417a6149219387862cea517538cb6af084d71e595sewardj *  See the License for the specific language governing permissions and
158618009823fe08e4f745be95eaf1ca204fc56709sewardj *  limitations under the License.
168618009823fe08e4f745be95eaf1ca204fc56709sewardj *
178618009823fe08e4f745be95eaf1ca204fc56709sewardj ******************************************************************************/
188618009823fe08e4f745be95eaf1ca204fc56709sewardj
198618009823fe08e4f745be95eaf1ca204fc56709sewardj/************************************************************************************
20483398ece74f045030f01f323e3c13cfe5e43222njn *
21483398ece74f045030f01f323e3c13cfe5e43222njn *  Filename:      btif_dm.c
22483398ece74f045030f01f323e3c13cfe5e43222njn *
23483398ece74f045030f01f323e3c13cfe5e43222njn *  Description:   Contains Device Management (DM) related functionality
24483398ece74f045030f01f323e3c13cfe5e43222njn *
25483398ece74f045030f01f323e3c13cfe5e43222njn *
268618009823fe08e4f745be95eaf1ca204fc56709sewardj ***********************************************************************************/
27a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom#include <stdio.h>
28a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom#include <stdlib.h>
29a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom#include <unistd.h>
30a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom
318618009823fe08e4f745be95eaf1ca204fc56709sewardj#include <hardware/bluetooth.h>
328618009823fe08e4f745be95eaf1ca204fc56709sewardj
338618009823fe08e4f745be95eaf1ca204fc56709sewardj#include <utils/Log.h>
348618009823fe08e4f745be95eaf1ca204fc56709sewardj#include <cutils/properties.h>
358618009823fe08e4f745be95eaf1ca204fc56709sewardj#include "gki.h"
368618009823fe08e4f745be95eaf1ca204fc56709sewardj#include "btu.h"
378618009823fe08e4f745be95eaf1ca204fc56709sewardj#include "bd.h"
3817a6149219387862cea517538cb6af084d71e595sewardj#include "bta_api.h"
3917a6149219387862cea517538cb6af084d71e595sewardj#include "btif_api.h"
4017a6149219387862cea517538cb6af084d71e595sewardj#include "btif_util.h"
4117a6149219387862cea517538cb6af084d71e595sewardj#include "btif_dm.h"
4217a6149219387862cea517538cb6af084d71e595sewardj#include "btif_storage.h"
4317a6149219387862cea517538cb6af084d71e595sewardj#include "btif_hh.h"
4417a6149219387862cea517538cb6af084d71e595sewardj#include "btif_config.h"
4517a6149219387862cea517538cb6af084d71e595sewardj
4617a6149219387862cea517538cb6af084d71e595sewardj#include "bta_gatt_api.h"
4717a6149219387862cea517538cb6af084d71e595sewardj/******************************************************************************
4817a6149219387862cea517538cb6af084d71e595sewardj**  Constants & Macros
4917a6149219387862cea517538cb6af084d71e595sewardj******************************************************************************/
5017a6149219387862cea517538cb6af084d71e595sewardj
5117a6149219387862cea517538cb6af084d71e595sewardj#define COD_UNCLASSIFIED ((0x1F) << 8)
5217a6149219387862cea517538cb6af084d71e595sewardj#define COD_HID_KEYBOARD                    0x0540
5317a6149219387862cea517538cb6af084d71e595sewardj#define COD_HID_POINTING                    0x0580
5417a6149219387862cea517538cb6af084d71e595sewardj#define COD_HID_COMBO                       0x05C0
5517a6149219387862cea517538cb6af084d71e595sewardj#define COD_HID_MAJOR                       0x0500
5617a6149219387862cea517538cb6af084d71e595sewardj#define COD_AV_HEADSETS                     0x0404
5717a6149219387862cea517538cb6af084d71e595sewardj#define COD_AV_HANDSFREE                    0x0408
5817a6149219387862cea517538cb6af084d71e595sewardj#define COD_AV_HEADPHONES                   0x0418
5917a6149219387862cea517538cb6af084d71e595sewardj#define COD_AV_PORTABLE_AUDIO               0x041C
6017a6149219387862cea517538cb6af084d71e595sewardj#define COD_AV_HIFI_AUDIO                   0x0428
6117a6149219387862cea517538cb6af084d71e595sewardj
6217a6149219387862cea517538cb6af084d71e595sewardj
6317a6149219387862cea517538cb6af084d71e595sewardj#define BTIF_DM_DEFAULT_INQ_MAX_RESULTS     0
6417a6149219387862cea517538cb6af084d71e595sewardj#define BTIF_DM_DEFAULT_INQ_MAX_DURATION    10
6517a6149219387862cea517538cb6af084d71e595sewardj#define BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING 2
6617a6149219387862cea517538cb6af084d71e595sewardj
6717a6149219387862cea517538cb6af084d71e595sewardj
6817a6149219387862cea517538cb6af084d71e595sewardjtypedef struct
6917a6149219387862cea517538cb6af084d71e595sewardj{
708618009823fe08e4f745be95eaf1ca204fc56709sewardj    bt_bond_state_t state;
718618009823fe08e4f745be95eaf1ca204fc56709sewardj    BD_ADDR bd_addr;
728618009823fe08e4f745be95eaf1ca204fc56709sewardj    UINT8   is_temp;
7317a6149219387862cea517538cb6af084d71e595sewardj    UINT8   pin_code_len;
7417a6149219387862cea517538cb6af084d71e595sewardj    UINT8   is_ssp;
7517a6149219387862cea517538cb6af084d71e595sewardj    UINT8   autopair_attempts;
7617a6149219387862cea517538cb6af084d71e595sewardj    UINT8   is_local_initiated;
7717a6149219387862cea517538cb6af084d71e595sewardj    UINT8   sdp_attempts;
7817a6149219387862cea517538cb6af084d71e595sewardj#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
7917a6149219387862cea517538cb6af084d71e595sewardj    BOOLEAN          is_le_only;
8017a6149219387862cea517538cb6af084d71e595sewardj    btif_dm_ble_cb_t ble;
8117a6149219387862cea517538cb6af084d71e595sewardj#endif
8217a6149219387862cea517538cb6af084d71e595sewardj} btif_dm_pairing_cb_t;
8317a6149219387862cea517538cb6af084d71e595sewardj
8417a6149219387862cea517538cb6af084d71e595sewardj
8517a6149219387862cea517538cb6af084d71e595sewardjtypedef struct
8617a6149219387862cea517538cb6af084d71e595sewardj{
8717a6149219387862cea517538cb6af084d71e595sewardj    UINT8       ir[BT_OCTET16_LEN];
8817a6149219387862cea517538cb6af084d71e595sewardj    UINT8       irk[BT_OCTET16_LEN];
8917a6149219387862cea517538cb6af084d71e595sewardj    UINT8       dhk[BT_OCTET16_LEN];
9017a6149219387862cea517538cb6af084d71e595sewardj}btif_dm_local_key_id_t;
9117a6149219387862cea517538cb6af084d71e595sewardj
9217a6149219387862cea517538cb6af084d71e595sewardjtypedef struct
9317a6149219387862cea517538cb6af084d71e595sewardj{
9417a6149219387862cea517538cb6af084d71e595sewardj    BOOLEAN                 is_er_rcvd;
9517a6149219387862cea517538cb6af084d71e595sewardj    UINT8                   er[BT_OCTET16_LEN];
9617a6149219387862cea517538cb6af084d71e595sewardj    BOOLEAN                 is_id_keys_rcvd;
9717a6149219387862cea517538cb6af084d71e595sewardj    btif_dm_local_key_id_t  id_keys;  /* ID kyes */
9817a6149219387862cea517538cb6af084d71e595sewardj
9917a6149219387862cea517538cb6af084d71e595sewardj}btif_dm_local_key_cb_t;
10017a6149219387862cea517538cb6af084d71e595sewardj
10117a6149219387862cea517538cb6af084d71e595sewardjtypedef struct
10217a6149219387862cea517538cb6af084d71e595sewardj{
10317a6149219387862cea517538cb6af084d71e595sewardj    BD_ADDR bd_addr;
10417a6149219387862cea517538cb6af084d71e595sewardj    BD_NAME bd_name;
10517a6149219387862cea517538cb6af084d71e595sewardj} btif_dm_remote_name_t;
10617a6149219387862cea517538cb6af084d71e595sewardj
10717a6149219387862cea517538cb6af084d71e595sewardjtypedef struct
10817a6149219387862cea517538cb6af084d71e595sewardj{
10917a6149219387862cea517538cb6af084d71e595sewardj    BT_OCTET16 sp_c;
11017a6149219387862cea517538cb6af084d71e595sewardj    BT_OCTET16 sp_r;
11117a6149219387862cea517538cb6af084d71e595sewardj    BD_ADDR  oob_bdaddr;  /* peer bdaddr*/
11217a6149219387862cea517538cb6af084d71e595sewardj} btif_dm_oob_cb_t;
1138618009823fe08e4f745be95eaf1ca204fc56709sewardj#define BTA_SERVICE_ID_TO_SERVICE_MASK(id)       (1 << (id))
1148618009823fe08e4f745be95eaf1ca204fc56709sewardj
1158618009823fe08e4f745be95eaf1ca204fc56709sewardj/* This flag will be true if HCI_Inquiry is in progress */
1168618009823fe08e4f745be95eaf1ca204fc56709sewardjstatic BOOLEAN btif_dm_inquiry_in_progress = FALSE;
1178618009823fe08e4f745be95eaf1ca204fc56709sewardj
1188618009823fe08e4f745be95eaf1ca204fc56709sewardj/******************************************************************************
1198618009823fe08e4f745be95eaf1ca204fc56709sewardj**  Static functions
1208618009823fe08e4f745be95eaf1ca204fc56709sewardj******************************************************************************/
1218618009823fe08e4f745be95eaf1ca204fc56709sewardjstatic btif_dm_pairing_cb_t pairing_cb;
12217a6149219387862cea517538cb6af084d71e595sewardjstatic btif_dm_oob_cb_t     oob_cb;
12317a6149219387862cea517538cb6af084d71e595sewardjstatic void btif_dm_generic_evt(UINT16 event, char* p_param);
12417a6149219387862cea517538cb6af084d71e595sewardjstatic void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr);
12517a6149219387862cea517538cb6af084d71e595sewardjstatic void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name);
12617a6149219387862cea517538cb6af084d71e595sewardjstatic void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
12717a6149219387862cea517538cb6af084d71e595sewardj                                          DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type);
12817a6149219387862cea517538cb6af084d71e595sewardj#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
12917a6149219387862cea517538cb6af084d71e595sewardjstatic btif_dm_local_key_cb_t ble_local_key_cb;
13017a6149219387862cea517538cb6af084d71e595sewardjstatic void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif);
13117a6149219387862cea517538cb6af084d71e595sewardjstatic void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl);
13217a6149219387862cea517538cb6af084d71e595sewardjstatic void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req);
13317a6149219387862cea517538cb6af084d71e595sewardj#endif
13417a6149219387862cea517538cb6af084d71e595sewardj/******************************************************************************
13517a6149219387862cea517538cb6af084d71e595sewardj**  Externs
13617a6149219387862cea517538cb6af084d71e595sewardj******************************************************************************/
13717a6149219387862cea517538cb6af084d71e595sewardjextern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
13817a6149219387862cea517538cb6af084d71e595sewardjextern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
13917a6149219387862cea517538cb6af084d71e595sewardjextern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
14017a6149219387862cea517538cb6af084d71e595sewardjextern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
14117a6149219387862cea517538cb6af084d71e595sewardjextern int btif_hh_connect(bt_bdaddr_t *bd_addr);
14217a6149219387862cea517538cb6af084d71e595sewardj
14317a6149219387862cea517538cb6af084d71e595sewardj
14417a6149219387862cea517538cb6af084d71e595sewardj/******************************************************************************
14517a6149219387862cea517538cb6af084d71e595sewardj**  Functions
14617a6149219387862cea517538cb6af084d71e595sewardj******************************************************************************/
14717a6149219387862cea517538cb6af084d71e595sewardj
14817a6149219387862cea517538cb6af084d71e595sewardjbt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
14917a6149219387862cea517538cb6af084d71e595sewardj                                                BOOLEAN b_enable)
15017a6149219387862cea517538cb6af084d71e595sewardj{
15117a6149219387862cea517538cb6af084d71e595sewardj    /* Check the service_ID and invoke the profile's BT state changed API */
15217a6149219387862cea517538cb6af084d71e595sewardj    switch (service_id)
15317a6149219387862cea517538cb6af084d71e595sewardj    {
1548618009823fe08e4f745be95eaf1ca204fc56709sewardj         case BTA_HFP_SERVICE_ID:
1558618009823fe08e4f745be95eaf1ca204fc56709sewardj         case BTA_HSP_SERVICE_ID:
1568618009823fe08e4f745be95eaf1ca204fc56709sewardj         {
1578618009823fe08e4f745be95eaf1ca204fc56709sewardj              btif_hf_execute_service(b_enable);
1588618009823fe08e4f745be95eaf1ca204fc56709sewardj         }break;
1598618009823fe08e4f745be95eaf1ca204fc56709sewardj         case BTA_A2DP_SERVICE_ID:
1608618009823fe08e4f745be95eaf1ca204fc56709sewardj         {
1618618009823fe08e4f745be95eaf1ca204fc56709sewardj              btif_av_execute_service(b_enable);
1628618009823fe08e4f745be95eaf1ca204fc56709sewardj         }break;
16317a6149219387862cea517538cb6af084d71e595sewardj         case BTA_HID_SERVICE_ID:
16417a6149219387862cea517538cb6af084d71e595sewardj         {
16517a6149219387862cea517538cb6af084d71e595sewardj              btif_hh_execute_service(b_enable);
16617a6149219387862cea517538cb6af084d71e595sewardj         }break;
16717a6149219387862cea517538cb6af084d71e595sewardj
16817a6149219387862cea517538cb6af084d71e595sewardj         default:
16917a6149219387862cea517538cb6af084d71e595sewardj              BTIF_TRACE_ERROR1("%s: Unknown service being enabled", __FUNCTION__);
17017a6149219387862cea517538cb6af084d71e595sewardj              return BT_STATUS_FAIL;
17117a6149219387862cea517538cb6af084d71e595sewardj    }
17217a6149219387862cea517538cb6af084d71e595sewardj    return BT_STATUS_SUCCESS;
17317a6149219387862cea517538cb6af084d71e595sewardj}
17417a6149219387862cea517538cb6af084d71e595sewardj
17517a6149219387862cea517538cb6af084d71e595sewardj/*******************************************************************************
17617a6149219387862cea517538cb6af084d71e595sewardj**
17717a6149219387862cea517538cb6af084d71e595sewardj** Function         check_eir_remote_name
17817a6149219387862cea517538cb6af084d71e595sewardj**
17917a6149219387862cea517538cb6af084d71e595sewardj** Description      Check if remote name is in the EIR data
18017a6149219387862cea517538cb6af084d71e595sewardj**
18117a6149219387862cea517538cb6af084d71e595sewardj** Returns          TRUE if remote name found
18217a6149219387862cea517538cb6af084d71e595sewardj**                  Populate p_remote_name, if provided and remote name found
18317a6149219387862cea517538cb6af084d71e595sewardj**
18417a6149219387862cea517538cb6af084d71e595sewardj*******************************************************************************/
18517a6149219387862cea517538cb6af084d71e595sewardjstatic BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
18617a6149219387862cea517538cb6af084d71e595sewardj                            UINT8 *p_remote_name, UINT8 *p_remote_name_len)
18717a6149219387862cea517538cb6af084d71e595sewardj{
18817a6149219387862cea517538cb6af084d71e595sewardj    UINT8 *p_eir_remote_name = NULL;
18917a6149219387862cea517538cb6af084d71e595sewardj    UINT8 remote_name_len = 0;
19017a6149219387862cea517538cb6af084d71e595sewardj
19117a6149219387862cea517538cb6af084d71e595sewardj    /* Check EIR for remote name and services */
19217a6149219387862cea517538cb6af084d71e595sewardj    if (p_search_data->inq_res.p_eir)
19317a6149219387862cea517538cb6af084d71e595sewardj    {
19417a6149219387862cea517538cb6af084d71e595sewardj        p_eir_remote_name = BTA_CheckEirData(p_search_data->inq_res.p_eir,
1958618009823fe08e4f745be95eaf1ca204fc56709sewardj                BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
1968618009823fe08e4f745be95eaf1ca204fc56709sewardj        if (!p_eir_remote_name)
1978618009823fe08e4f745be95eaf1ca204fc56709sewardj        {
1988618009823fe08e4f745be95eaf1ca204fc56709sewardj            p_eir_remote_name = BTA_CheckEirData(p_search_data->inq_res.p_eir,
1998618009823fe08e4f745be95eaf1ca204fc56709sewardj                    BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
2008618009823fe08e4f745be95eaf1ca204fc56709sewardj        }
2018618009823fe08e4f745be95eaf1ca204fc56709sewardj
2028618009823fe08e4f745be95eaf1ca204fc56709sewardj        if (p_eir_remote_name)
2038618009823fe08e4f745be95eaf1ca204fc56709sewardj        {
20417a6149219387862cea517538cb6af084d71e595sewardj            if (remote_name_len > BD_NAME_LEN)
20517a6149219387862cea517538cb6af084d71e595sewardj                remote_name_len = BD_NAME_LEN;
20617a6149219387862cea517538cb6af084d71e595sewardj
20717a6149219387862cea517538cb6af084d71e595sewardj            if (p_remote_name && p_remote_name_len)
20817a6149219387862cea517538cb6af084d71e595sewardj            {
20917a6149219387862cea517538cb6af084d71e595sewardj                memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
21017a6149219387862cea517538cb6af084d71e595sewardj                *(p_remote_name + remote_name_len) = 0;
21117a6149219387862cea517538cb6af084d71e595sewardj                *p_remote_name_len = remote_name_len;
21217a6149219387862cea517538cb6af084d71e595sewardj            }
21317a6149219387862cea517538cb6af084d71e595sewardj
21417a6149219387862cea517538cb6af084d71e595sewardj            return TRUE;
21517a6149219387862cea517538cb6af084d71e595sewardj        }
21617a6149219387862cea517538cb6af084d71e595sewardj    }
21717a6149219387862cea517538cb6af084d71e595sewardj
21817a6149219387862cea517538cb6af084d71e595sewardj    return FALSE;
21917a6149219387862cea517538cb6af084d71e595sewardj
22017a6149219387862cea517538cb6af084d71e595sewardj}
22117a6149219387862cea517538cb6af084d71e595sewardj
22217a6149219387862cea517538cb6af084d71e595sewardj/*******************************************************************************
22317a6149219387862cea517538cb6af084d71e595sewardj**
22417a6149219387862cea517538cb6af084d71e595sewardj** Function         check_cached_remote_name
22517a6149219387862cea517538cb6af084d71e595sewardj**
22617a6149219387862cea517538cb6af084d71e595sewardj** Description      Check if remote name is in the NVRAM cache
22717a6149219387862cea517538cb6af084d71e595sewardj**
22817a6149219387862cea517538cb6af084d71e595sewardj** Returns          TRUE if remote name found
22917a6149219387862cea517538cb6af084d71e595sewardj**                  Populate p_remote_name, if provided and remote name found
23017a6149219387862cea517538cb6af084d71e595sewardj**
23117a6149219387862cea517538cb6af084d71e595sewardj*******************************************************************************/
23217a6149219387862cea517538cb6af084d71e595sewardjstatic BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
23317a6149219387862cea517538cb6af084d71e595sewardj                                UINT8 *p_remote_name, UINT8 *p_remote_name_len)
23417a6149219387862cea517538cb6af084d71e595sewardj{
23517a6149219387862cea517538cb6af084d71e595sewardj    bt_bdname_t bdname;
23617a6149219387862cea517538cb6af084d71e595sewardj    bt_bdaddr_t remote_bdaddr;
23717a6149219387862cea517538cb6af084d71e595sewardj    bt_property_t prop_name;
23817a6149219387862cea517538cb6af084d71e595sewardj
23917a6149219387862cea517538cb6af084d71e595sewardj    /* check if we already have it in our btif_storage cache */
24017a6149219387862cea517538cb6af084d71e595sewardj    bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
24117a6149219387862cea517538cb6af084d71e595sewardj    BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
24217a6149219387862cea517538cb6af084d71e595sewardj                               sizeof(bt_bdname_t), &bdname);
24317a6149219387862cea517538cb6af084d71e595sewardj    if (btif_storage_get_remote_device_property(
2448618009823fe08e4f745be95eaf1ca204fc56709sewardj        &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
2458618009823fe08e4f745be95eaf1ca204fc56709sewardj    {
2468618009823fe08e4f745be95eaf1ca204fc56709sewardj        if (p_remote_name && p_remote_name_len)
24717a6149219387862cea517538cb6af084d71e595sewardj        {
24817a6149219387862cea517538cb6af084d71e595sewardj            strcpy((char *)p_remote_name, (char *)bdname.name);
24917a6149219387862cea517538cb6af084d71e595sewardj            *p_remote_name_len = strlen((char *)p_remote_name);
25017a6149219387862cea517538cb6af084d71e595sewardj        }
25117a6149219387862cea517538cb6af084d71e595sewardj        return TRUE;
25217a6149219387862cea517538cb6af084d71e595sewardj    }
25317a6149219387862cea517538cb6af084d71e595sewardj
25417a6149219387862cea517538cb6af084d71e595sewardj    return FALSE;
25517a6149219387862cea517538cb6af084d71e595sewardj}
25617a6149219387862cea517538cb6af084d71e595sewardj
25717a6149219387862cea517538cb6af084d71e595sewardjBOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
25817a6149219387862cea517538cb6af084d71e595sewardj{
25917a6149219387862cea517538cb6af084d71e595sewardj    uint32_t    remote_cod;
26017a6149219387862cea517538cb6af084d71e595sewardj    bt_property_t prop_name;
26117a6149219387862cea517538cb6af084d71e595sewardj
26217a6149219387862cea517538cb6af084d71e595sewardj    /* check if we already have it in our btif_storage cache */
26317a6149219387862cea517538cb6af084d71e595sewardj    BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
26417a6149219387862cea517538cb6af084d71e595sewardj                               sizeof(uint32_t), &remote_cod);
26517a6149219387862cea517538cb6af084d71e595sewardj    if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
26617a6149219387862cea517538cb6af084d71e595sewardj    {
26717a6149219387862cea517538cb6af084d71e595sewardj        if ((remote_cod & 0x7ff) == cod)
26817a6149219387862cea517538cb6af084d71e595sewardj            return TRUE;
26917a6149219387862cea517538cb6af084d71e595sewardj    }
27017a6149219387862cea517538cb6af084d71e595sewardj
27117a6149219387862cea517538cb6af084d71e595sewardj    return FALSE;
27217a6149219387862cea517538cb6af084d71e595sewardj}
27317a6149219387862cea517538cb6af084d71e595sewardj
27417a6149219387862cea517538cb6af084d71e595sewardjBOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
27517a6149219387862cea517538cb6af084d71e595sewardj{
27617a6149219387862cea517538cb6af084d71e595sewardj    uint32_t    remote_cod;
27717a6149219387862cea517538cb6af084d71e595sewardj    bt_property_t prop_name;
27817a6149219387862cea517538cb6af084d71e595sewardj
27917a6149219387862cea517538cb6af084d71e595sewardj    /* check if we already have it in our btif_storage cache */
28017a6149219387862cea517538cb6af084d71e595sewardj    BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
28117a6149219387862cea517538cb6af084d71e595sewardj                               sizeof(uint32_t), &remote_cod);
28217a6149219387862cea517538cb6af084d71e595sewardj    if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
28317a6149219387862cea517538cb6af084d71e595sewardj                                &prop_name) == BT_STATUS_SUCCESS)
28417a6149219387862cea517538cb6af084d71e595sewardj    {
28517a6149219387862cea517538cb6af084d71e595sewardj        if ((remote_cod & 0x700) == cod)
28617a6149219387862cea517538cb6af084d71e595sewardj            return TRUE;
2878618009823fe08e4f745be95eaf1ca204fc56709sewardj    }
2888618009823fe08e4f745be95eaf1ca204fc56709sewardj
2898618009823fe08e4f745be95eaf1ca204fc56709sewardj    return FALSE;
2908618009823fe08e4f745be95eaf1ca204fc56709sewardj}
2918618009823fe08e4f745be95eaf1ca204fc56709sewardj
2928618009823fe08e4f745be95eaf1ca204fc56709sewardjstatic void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
2938618009823fe08e4f745be95eaf1ca204fc56709sewardj{
2948618009823fe08e4f745be95eaf1ca204fc56709sewardj    /* Send bonding state only once - based on outgoing/incoming we may receive duplicates */
2958618009823fe08e4f745be95eaf1ca204fc56709sewardj    if ( (pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING) )
29617a6149219387862cea517538cb6af084d71e595sewardj        return;
29717a6149219387862cea517538cb6af084d71e595sewardj
29817a6149219387862cea517538cb6af084d71e595sewardj    if (pairing_cb.is_temp)
29917a6149219387862cea517538cb6af084d71e595sewardj    {
30017a6149219387862cea517538cb6af084d71e595sewardj       state = BT_BOND_STATE_NONE;
30117a6149219387862cea517538cb6af084d71e595sewardj    }
30217a6149219387862cea517538cb6af084d71e595sewardj    BTIF_TRACE_DEBUG3("%s: state=%d prev_state=%d", __FUNCTION__, state, pairing_cb.state);
30317a6149219387862cea517538cb6af084d71e595sewardj
30417a6149219387862cea517538cb6af084d71e595sewardj    HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
30517a6149219387862cea517538cb6af084d71e595sewardj
30617a6149219387862cea517538cb6af084d71e595sewardj    if (state == BT_BOND_STATE_BONDING)
30717a6149219387862cea517538cb6af084d71e595sewardj    {
30817a6149219387862cea517538cb6af084d71e595sewardj        pairing_cb.state = state;
30917a6149219387862cea517538cb6af084d71e595sewardj        bdcpy(pairing_cb.bd_addr, bd_addr->address);
31017a6149219387862cea517538cb6af084d71e595sewardj    }
31117a6149219387862cea517538cb6af084d71e595sewardj    else
31217a6149219387862cea517538cb6af084d71e595sewardj    {
31317a6149219387862cea517538cb6af084d71e595sewardj        memset(&pairing_cb, 0, sizeof(pairing_cb));
31417a6149219387862cea517538cb6af084d71e595sewardj    }
31517a6149219387862cea517538cb6af084d71e595sewardj
31617a6149219387862cea517538cb6af084d71e595sewardj}
31717a6149219387862cea517538cb6af084d71e595sewardj
31817a6149219387862cea517538cb6af084d71e595sewardj/* store remote version in bt config to always have access
31917a6149219387862cea517538cb6af084d71e595sewardj   to it post pairing*/
32017a6149219387862cea517538cb6af084d71e595sewardjstatic void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
32117a6149219387862cea517538cb6af084d71e595sewardj{
32217a6149219387862cea517538cb6af084d71e595sewardj    bt_property_t property;
32317a6149219387862cea517538cb6af084d71e595sewardj    UINT8 lmp_ver = 0;
32417a6149219387862cea517538cb6af084d71e595sewardj    UINT16 lmp_subver = 0;
32517a6149219387862cea517538cb6af084d71e595sewardj    UINT16 mfct_set = 0;
32617a6149219387862cea517538cb6af084d71e595sewardj    tBTM_STATUS btm_status;
32717a6149219387862cea517538cb6af084d71e595sewardj    bt_remote_version_t info;
32817a6149219387862cea517538cb6af084d71e595sewardj    bt_status_t status;
32917a6149219387862cea517538cb6af084d71e595sewardj    bdstr_t bdstr;
33017a6149219387862cea517538cb6af084d71e595sewardj
33117a6149219387862cea517538cb6af084d71e595sewardj    btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
33217a6149219387862cea517538cb6af084d71e595sewardj                          &mfct_set, &lmp_subver);
33317a6149219387862cea517538cb6af084d71e595sewardj
33417a6149219387862cea517538cb6af084d71e595sewardj    ALOGD("remote version info [%s]: %x, %x, %x", bd2str(p_bd, &bdstr),
33517a6149219387862cea517538cb6af084d71e595sewardj               lmp_ver, mfct_set, lmp_subver);
3368618009823fe08e4f745be95eaf1ca204fc56709sewardj
3378618009823fe08e4f745be95eaf1ca204fc56709sewardj    if (btm_status == BTM_SUCCESS)
3388618009823fe08e4f745be95eaf1ca204fc56709sewardj    {
3398618009823fe08e4f745be95eaf1ca204fc56709sewardj        /* always update cache to ensure we have availability whenever BTM API
3408618009823fe08e4f745be95eaf1ca204fc56709sewardj           is not populated */
3418618009823fe08e4f745be95eaf1ca204fc56709sewardj        info.manufacturer = mfct_set;
3428618009823fe08e4f745be95eaf1ca204fc56709sewardj        info.sub_ver = lmp_subver;
3438618009823fe08e4f745be95eaf1ca204fc56709sewardj        info.version = lmp_ver;
344acdcf542a5f8cafbbfb7c0552fe9a40c457a9387njn        BTIF_STORAGE_FILL_PROPERTY(&property,
3458618009823fe08e4f745be95eaf1ca204fc56709sewardj                            BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
3468618009823fe08e4f745be95eaf1ca204fc56709sewardj                            &info);
3478618009823fe08e4f745be95eaf1ca204fc56709sewardj        status = btif_storage_set_remote_device_property(p_bd, &property);
348a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom        ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
349a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom    }
350a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom}
351a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom
352a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom
353a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tomstatic void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
354a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom                                          DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
355a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom{
356a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom    int num_properties = 0;
357a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom    bt_property_t properties[3];
358a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom    bt_bdaddr_t bdaddr;
359a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom    bt_status_t status;
360a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom    UINT32 cod;
361a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom    bt_device_type_t dev_type;
362a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom
363a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom    memset(properties, 0, sizeof(properties));
364a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom    bdcpy(bdaddr.address, bd_addr);
365a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom
366a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom    /* remote name */
367a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom    if (strlen((const char *) bd_name))
368a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom    {
369a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom        BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
370a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom                            BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
371a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom        status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
3728618009823fe08e4f745be95eaf1ca204fc56709sewardj        ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
3738618009823fe08e4f745be95eaf1ca204fc56709sewardj        num_properties++;
374a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom    }
375a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom
376a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom    /* class of device */
377a5aa1b6d4d740deaa8e69bf84ecfe1fffbc9bc49tom    cod = devclass2uint(dev_class);
3788618009823fe08e4f745be95eaf1ca204fc56709sewardj    if ( cod == 0) {
37901e755d138cca2202221f595ebbe0e9c0f6226c9njn        BTIF_TRACE_DEBUG1("%s():cod is 0, set as unclassified", __FUNCTION__);
3808618009823fe08e4f745be95eaf1ca204fc56709sewardj        cod = COD_UNCLASSIFIED;
3818618009823fe08e4f745be95eaf1ca204fc56709sewardj    }
3828618009823fe08e4f745be95eaf1ca204fc56709sewardj
3838618009823fe08e4f745be95eaf1ca204fc56709sewardj    BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
3848618009823fe08e4f745be95eaf1ca204fc56709sewardj                        BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
3858df80b22d8f0ed19fb2d7ac7267b8015b202f661njn    status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
3868618009823fe08e4f745be95eaf1ca204fc56709sewardj    ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
3878618009823fe08e4f745be95eaf1ca204fc56709sewardj    num_properties++;
3888618009823fe08e4f745be95eaf1ca204fc56709sewardj
38917a6149219387862cea517538cb6af084d71e595sewardj    /* device type */
39017a6149219387862cea517538cb6af084d71e595sewardj    dev_type = device_type;
39117a6149219387862cea517538cb6af084d71e595sewardj    BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
39217a6149219387862cea517538cb6af084d71e595sewardj                        BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
39317a6149219387862cea517538cb6af084d71e595sewardj    status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
39417a6149219387862cea517538cb6af084d71e595sewardj    ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
39517a6149219387862cea517538cb6af084d71e595sewardj    num_properties++;
39617a6149219387862cea517538cb6af084d71e595sewardj
39717a6149219387862cea517538cb6af084d71e595sewardj    HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
39817a6149219387862cea517538cb6af084d71e595sewardj                     status, &bdaddr, num_properties, properties);
39917a6149219387862cea517538cb6af084d71e595sewardj}
40017a6149219387862cea517538cb6af084d71e595sewardj/*******************************************************************************
40117a6149219387862cea517538cb6af084d71e595sewardj**
40217a6149219387862cea517538cb6af084d71e595sewardj** Function         hid_remote_name_cback
40317a6149219387862cea517538cb6af084d71e595sewardj**
40417a6149219387862cea517538cb6af084d71e595sewardj** Description      Remote name callback for HID device. Called in stack context
40517a6149219387862cea517538cb6af084d71e595sewardj**                  Special handling for HID devices
40617a6149219387862cea517538cb6af084d71e595sewardj**
40717a6149219387862cea517538cb6af084d71e595sewardj** Returns          void
40817a6149219387862cea517538cb6af084d71e595sewardj**
40917a6149219387862cea517538cb6af084d71e595sewardj*******************************************************************************/
41017a6149219387862cea517538cb6af084d71e595sewardjstatic void hid_remote_name_cback(void *p_param)
41117a6149219387862cea517538cb6af084d71e595sewardj{
41217a6149219387862cea517538cb6af084d71e595sewardj    BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
41317a6149219387862cea517538cb6af084d71e595sewardj
41417a6149219387862cea517538cb6af084d71e595sewardj    btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_HID_REMOTE_NAME,
41517a6149219387862cea517538cb6af084d71e595sewardj        (char *)p_param, sizeof(tBTM_REMOTE_DEV_NAME), NULL);
41617a6149219387862cea517538cb6af084d71e595sewardj}
41717a6149219387862cea517538cb6af084d71e595sewardj
41817a6149219387862cea517538cb6af084d71e595sewardj/*******************************************************************************
41917a6149219387862cea517538cb6af084d71e595sewardj**
42017a6149219387862cea517538cb6af084d71e595sewardj** Function         btif_dm_cb_hid_remote_name
42117a6149219387862cea517538cb6af084d71e595sewardj**
42217a6149219387862cea517538cb6af084d71e595sewardj** Description      Remote name callback for HID device. Called in btif context
42317a6149219387862cea517538cb6af084d71e595sewardj**                  Special handling for HID devices
42417a6149219387862cea517538cb6af084d71e595sewardj**
42517a6149219387862cea517538cb6af084d71e595sewardj** Returns          void
42617a6149219387862cea517538cb6af084d71e595sewardj**
42717a6149219387862cea517538cb6af084d71e595sewardj*******************************************************************************/
42817a6149219387862cea517538cb6af084d71e595sewardjstatic void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
4298618009823fe08e4f745be95eaf1ca204fc56709sewardj{
4308618009823fe08e4f745be95eaf1ca204fc56709sewardj    BTIF_TRACE_DEBUG3("%s: status=%d pairing_cb.state=%d", __FUNCTION__, p_remote_name->status, pairing_cb.state);
4318618009823fe08e4f745be95eaf1ca204fc56709sewardj    if (pairing_cb.state == BT_BOND_STATE_BONDING)
4328618009823fe08e4f745be95eaf1ca204fc56709sewardj    {
4338618009823fe08e4f745be95eaf1ca204fc56709sewardj        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/*******************************************************************************
447**
448** Function         btif_dm_cb_create_bond
449**
450** Description      Create bond initiated from the BTIF thread context
451**                  Special handling for HID devices
452**
453** Returns          void
454**
455*******************************************************************************/
456static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr)
457{
458    BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
459
460
461    bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
462
463    if (is_hid){
464
465            int status;
466            status = btif_hh_connect(bd_addr);
467            if(status != BT_STATUS_SUCCESS)
468                bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
469    }
470    else
471    {
472#if BLE_INCLUDED == TRUE
473        int device_type;
474        int addr_type;
475        bdstr_t bdstr;
476        bd2str(bd_addr, &bdstr);
477        if(btif_config_get_int("Remote", (char const *)&bdstr,"DevType", &device_type) &&
478           (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
479           (device_type == BT_DEVICE_TYPE_BLE))
480        {
481            BTA_DmAddBleDevice(bd_addr->address, addr_type, BT_DEVICE_TYPE_BLE);
482        }
483#endif
484        BTA_DmBond ((UINT8 *)bd_addr->address);
485    }
486    /*  Track  originator of bond creation  */
487    pairing_cb.is_local_initiated = TRUE;
488
489}
490
491/*******************************************************************************
492**
493** Function         btif_dm_cb_remove_bond
494**
495** Description      remove bond initiated from the BTIF thread context
496**                  Special handling for HID devices
497**
498** Returns          void
499**
500*******************************************************************************/
501void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
502{
503     bdstr_t bdstr;
504     /*special handling for HID devices */
505     /*  VUP needs to be sent if its a HID Device. The HID HOST module will check if there
506     is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
507#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
508    if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
509#endif
510    {
511         BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
512    }
513}
514
515/*******************************************************************************
516**
517** Function         search_devices_copy_cb
518**
519** Description      Deep copy callback for search devices event
520**
521** Returns          void
522**
523*******************************************************************************/
524static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
525{
526    tBTA_DM_SEARCH *p_dest_data =  (tBTA_DM_SEARCH *) p_dest;
527    tBTA_DM_SEARCH *p_src_data =  (tBTA_DM_SEARCH *) p_src;
528
529    if (!p_src)
530        return;
531
532    BTIF_TRACE_DEBUG2("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
533    memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
534    switch (event)
535    {
536        case BTA_DM_INQ_RES_EVT:
537        {
538            if (p_src_data->inq_res.p_eir)
539            {
540                p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
541                memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
542            }
543        }
544        break;
545
546        case BTA_DM_DISC_RES_EVT:
547        {
548            if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
549            {
550                p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
551                memcpy(p_dest_data->disc_res.p_raw_data,
552                    p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
553            }
554        }
555        break;
556    }
557}
558
559static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
560{
561    tBTA_DM_SEARCH *p_dest_data =  (tBTA_DM_SEARCH *) p_dest;
562    tBTA_DM_SEARCH *p_src_data =  (tBTA_DM_SEARCH *) p_src;
563
564    if (!p_src)
565        return;
566    memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
567    switch (event)
568    {
569         case BTA_DM_DISC_RES_EVT:
570         {
571              if (p_src_data->disc_res.result == BTA_SUCCESS)
572              {
573                  if (p_src_data->disc_res.num_uuids > 0)
574                  {
575                       p_dest_data->disc_res.p_uuid_list =
576                                                        (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
577                       memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
578                              p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
579                       GKI_freebuf(p_src_data->disc_res.p_uuid_list);
580                  }
581                  if (p_src_data->disc_res.p_raw_data != NULL)
582                  {
583                      GKI_freebuf(p_src_data->disc_res.p_raw_data);
584                  }
585              }
586         } break;
587    }
588}
589/******************************************************************************
590**
591**  BTIF DM callback events
592**
593*****************************************************************************/
594
595/*******************************************************************************
596**
597** Function         btif_dm_pin_req_evt
598**
599** Description      Executes pin request event in btif context
600**
601** Returns          void
602**
603*******************************************************************************/
604static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
605{
606    bt_bdaddr_t bd_addr;
607    bt_bdname_t bd_name;
608    UINT32 cod;
609    bt_pin_code_t pin_code;
610
611    /* Remote properties update */
612    btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
613                                  p_pin_req->dev_class, BT_DEVICE_TYPE_BREDR);
614
615    bdcpy(bd_addr.address, p_pin_req->bd_addr);
616    memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
617
618    bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
619
620    cod = devclass2uint(p_pin_req->dev_class);
621
622    if ( cod == 0) {
623        BTIF_TRACE_DEBUG1("%s():cod is 0, set as unclassified", __FUNCTION__);
624        cod = COD_UNCLASSIFIED;
625    }
626
627    /* check for auto pair possiblity only if bond was initiated by local device */
628    if (pairing_cb.is_local_initiated)
629    {
630        if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
631            check_cod(&bd_addr, COD_AV_HANDSFREE) ||
632            check_cod(&bd_addr, COD_AV_HEADPHONES) ||
633            check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
634            check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
635            check_cod(&bd_addr, COD_HID_POINTING))
636        {
637            BTIF_TRACE_DEBUG1("%s()cod matches for auto pair", __FUNCTION__);
638            /*  Check if this device can be auto paired  */
639            if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) &&
640                (pairing_cb.autopair_attempts == 0))
641            {
642                BTIF_TRACE_DEBUG1("%s() Attempting auto pair", __FUNCTION__);
643                pin_code.pin[0] = 0x30;
644                pin_code.pin[1] = 0x30;
645                pin_code.pin[2] = 0x30;
646                pin_code.pin[3] = 0x30;
647
648                pairing_cb.autopair_attempts++;
649                BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
650                return;
651            }
652        }
653        else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
654                 check_cod(&bd_addr, COD_HID_COMBO))
655        {
656            if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) &&
657               (pairing_cb.autopair_attempts == 0))
658            {
659                BTIF_TRACE_DEBUG1("%s() Attempting auto pair", __FUNCTION__);
660                pin_code.pin[0] = 0x30;
661                pin_code.pin[1] = 0x30;
662                pin_code.pin[2] = 0x30;
663                pin_code.pin[3] = 0x30;
664
665                pairing_cb.autopair_attempts++;
666                BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
667                return;
668            }
669        }
670    }
671    HAL_CBACK(bt_hal_cbacks, pin_request_cb,
672                     &bd_addr, &bd_name, cod);
673}
674
675/*******************************************************************************
676**
677** Function         btif_dm_ssp_cfm_req_evt
678**
679** Description      Executes SSP confirm request event in btif context
680**
681** Returns          void
682**
683*******************************************************************************/
684static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
685{
686    bt_bdaddr_t bd_addr;
687    bt_bdname_t bd_name;
688    UINT32 cod;
689    BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
690
691    BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
692
693    /* Remote properties update */
694    btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
695                                  p_ssp_cfm_req->dev_class, BT_DEVICE_TYPE_BREDR);
696
697    bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
698    memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
699
700    /* Set the pairing_cb based on the local & remote authentication requirements */
701    bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
702
703    /* if just_works and bonding bit is not set treat this as temporary */
704    if (p_ssp_cfm_req->just_works && !(p_ssp_cfm_req->loc_auth_req & BTM_AUTH_BONDS) &&
705        !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
706        !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
707        pairing_cb.is_temp = TRUE;
708    else
709        pairing_cb.is_temp = FALSE;
710
711    pairing_cb.is_ssp = TRUE;
712
713    /* If JustWorks auto-accept */
714    if (p_ssp_cfm_req->just_works)
715    {
716        /* Pairing consent for JustWorks needed if:
717         * 1. Incoming pairing is detected AND
718         * 2. local IO capabilities are DisplayYesNo AND
719         * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
720         */
721        if ((is_incoming) && ((p_ssp_cfm_req->loc_io_caps == 0x01) &&
722                (p_ssp_cfm_req->rmt_io_caps == 0x00 || p_ssp_cfm_req->rmt_io_caps == 0x03)))
723        {
724            BTIF_TRACE_EVENT3("%s: User consent needed for incoming pairing request. loc_io_caps: %d, rmt_io_caps: %d",
725                __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
726        }
727        else
728        {
729            BTIF_TRACE_EVENT1("%s: Auto-accept JustWorks pairing", __FUNCTION__);
730            btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
731            return;
732        }
733    }
734
735    cod = devclass2uint(p_ssp_cfm_req->dev_class);
736
737    if ( cod == 0) {
738        ALOGD("cod is 0, set as unclassified");
739        cod = COD_UNCLASSIFIED;
740    }
741
742    pairing_cb.sdp_attempts = 0;
743    HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
744                     (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
745                     p_ssp_cfm_req->num_val);
746}
747
748static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
749{
750    bt_bdaddr_t bd_addr;
751    bt_bdname_t bd_name;
752    UINT32 cod;
753
754    BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
755
756    /* Remote properties update */
757    btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
758                                  p_ssp_key_notif->dev_class, BT_DEVICE_TYPE_BREDR);
759
760    bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
761    memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
762
763    bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
764    pairing_cb.is_ssp = TRUE;
765    cod = devclass2uint(p_ssp_key_notif->dev_class);
766
767    if ( cod == 0) {
768        ALOGD("cod is 0, set as unclassified");
769        cod = COD_UNCLASSIFIED;
770    }
771
772    HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
773                     cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
774                     p_ssp_key_notif->passkey);
775}
776/*******************************************************************************
777**
778** Function         btif_dm_auth_cmpl_evt
779**
780** Description      Executes authentication complete event in btif context
781**
782** Returns          void
783**
784*******************************************************************************/
785static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
786{
787    /* Save link key, if not temporary */
788    bt_bdaddr_t bd_addr;
789    bt_status_t status = BT_STATUS_FAIL;
790    bt_bond_state_t state = BT_BOND_STATE_NONE;
791
792    bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
793    if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
794    {
795        if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB)  || (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
796            (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) || (!pairing_cb.is_temp))
797        {
798            bt_status_t ret;
799            BTIF_TRACE_DEBUG3("%s: Storing link key. key_type=0x%x, is_temp=%d",
800                __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.is_temp);
801            ret = btif_storage_add_bonded_device(&bd_addr,
802                                p_auth_cmpl->key, p_auth_cmpl->key_type,
803                                pairing_cb.pin_code_len);
804            ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
805        }
806        else
807        {
808            BTIF_TRACE_DEBUG3("%s: Temporary key. Not storing. key_type=0x%x, is_temp=%d",
809                __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.is_temp);
810        }
811    }
812    if (p_auth_cmpl->success)
813    {
814        status = BT_STATUS_SUCCESS;
815        state = BT_BOND_STATE_BONDED;
816
817        /* Trigger SDP on the device */
818        pairing_cb.sdp_attempts = 1;;
819
820        if(btif_dm_inquiry_in_progress)
821            btif_dm_cancel_discovery();
822
823        btif_dm_get_remote_services(&bd_addr);
824        /* Do not call bond_state_changed_cb yet. Wait till fetch remote service is complete */
825    }
826    else
827    {
828         /*Map the HCI fail reason  to  bt status  */
829        switch(p_auth_cmpl->fail_reason)
830        {
831            case HCI_ERR_PAGE_TIMEOUT:
832            case HCI_ERR_CONNECTION_TOUT:
833                status =  BT_STATUS_RMT_DEV_DOWN;
834                break;
835
836            /* map the auth failure codes, so we can retry pairing if necessary */
837            case HCI_ERR_AUTH_FAILURE:
838            case HCI_ERR_HOST_REJECT_SECURITY:
839            case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
840            case HCI_ERR_UNIT_KEY_USED:
841            case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
842            case HCI_ERR_INSUFFCIENT_SECURITY:
843                BTIF_TRACE_DEBUG1(" %s() Authentication fail ", __FUNCTION__);
844                if (pairing_cb.autopair_attempts  == 1)
845                {
846                    BTIF_TRACE_DEBUG1("%s(): Adding device to blacklist ", __FUNCTION__);
847
848                    /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class  */
849                    if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
850                        check_cod(&bd_addr, COD_AV_HANDSFREE) ||
851                        check_cod(&bd_addr, COD_AV_HEADPHONES) ||
852                        check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
853                        check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
854                        check_cod(&bd_addr, COD_HID_POINTING))
855                    {
856                        btif_storage_add_device_to_autopair_blacklist (&bd_addr);
857                    }
858                    pairing_cb.autopair_attempts++;
859
860                    /* Create the Bond once again */
861                    BTIF_TRACE_DEBUG1("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
862                    btif_dm_cb_create_bond (&bd_addr);
863                    return;
864                }
865                else
866                {
867                    /* if autopair attempts are more than 1, or not attempted */
868                    status =  BT_STATUS_AUTH_FAILURE;
869                }
870                break;
871
872            default:
873                status =  BT_STATUS_FAIL;
874        }
875        bond_state_changed(status, &bd_addr, state);
876    }
877}
878
879/******************************************************************************
880**
881** Function         btif_dm_search_devices_evt
882**
883** Description      Executes search devices callback events in btif context
884**
885** Returns          void
886**
887******************************************************************************/
888static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
889{
890    tBTA_DM_SEARCH *p_search_data;
891    BTIF_TRACE_EVENT2("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
892
893    switch (event)
894    {
895        case BTA_DM_DISC_RES_EVT:
896        {
897            p_search_data = (tBTA_DM_SEARCH *)p_param;
898            /* Remote name update */
899            if (strlen((const char *) p_search_data->disc_res.bd_name))
900            {
901                bt_property_t properties[1];
902                bt_bdaddr_t bdaddr;
903                bt_status_t status;
904
905                properties[0].type = BT_PROPERTY_BDNAME;
906                properties[0].val = p_search_data->disc_res.bd_name;
907                properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
908                bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
909
910                status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
911                ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
912                HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
913                                 status, &bdaddr, 1, properties);
914            }
915            /* TODO: Services? */
916        }
917        break;
918
919        case BTA_DM_INQ_RES_EVT:
920        {
921            /* inquiry result */
922            UINT32 cod;
923            UINT8 *p_eir_remote_name = NULL;
924            bt_bdname_t bdname;
925            bt_bdaddr_t bdaddr;
926            UINT8 remote_name_len;
927            UINT8 *p_cached_name = NULL;
928            tBTA_SERVICE_MASK services = 0;
929            bdstr_t bdstr;
930
931            p_search_data = (tBTA_DM_SEARCH *)p_param;
932            bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
933
934            BTIF_TRACE_DEBUG3("%s() %s device_type = 0x%x\n", __FUNCTION__, bd2str(&bdaddr, &bdstr),
935#if (BLE_INCLUDED == TRUE)
936                    p_search_data->inq_res.device_type);
937#else
938                    BT_DEVICE_TYPE_BREDR);
939#endif
940            bdname.name[0] = 0;
941
942            cod = devclass2uint (p_search_data->inq_res.dev_class);
943
944            if ( cod == 0) {
945                ALOGD("cod is 0, set as unclassified");
946                cod = COD_UNCLASSIFIED;
947            }
948
949            if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
950                check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
951
952            /* Check EIR for remote name and services */
953            if (p_search_data->inq_res.p_eir)
954            {
955                BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
956                BTIF_TRACE_DEBUG2("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
957                /* TODO:  Get the service list and check to see which uuids we got and send it back to the client. */
958            }
959
960
961            {
962                bt_property_t properties[5];
963                bt_device_type_t dev_type;
964                uint32_t num_properties = 0;
965                bt_status_t status;
966
967                memset(properties, 0, sizeof(properties));
968                /* BD_ADDR */
969                BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
970                                    BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
971                num_properties++;
972                /* BD_NAME */
973                /* Don't send BDNAME if it is empty */
974                if (bdname.name[0]) {
975                    BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
976                                               BT_PROPERTY_BDNAME,
977                                               strlen((char *)bdname.name), &bdname);
978                    num_properties++;
979                }
980
981                /* DEV_CLASS */
982                BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
983                                    BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
984                num_properties++;
985                /* DEV_TYPE */
986#if (BLE_INCLUDED == TRUE)
987                /* FixMe: Assumption is that bluetooth.h and BTE enums match */
988                dev_type = p_search_data->inq_res.device_type;
989#else
990                dev_type = BT_DEVICE_TYPE_BREDR;
991#endif
992                BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
993                                    BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
994                num_properties++;
995                /* RSSI */
996                BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
997                                    BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
998                                    &(p_search_data->inq_res.rssi));
999                num_properties++;
1000
1001                status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1002                ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
1003
1004                /* Callback to notify upper layer of device */
1005                HAL_CBACK(bt_hal_cbacks, device_found_cb,
1006                                 num_properties, properties);
1007            }
1008        }
1009        break;
1010
1011        case BTA_DM_INQ_CMPL_EVT:
1012        {
1013        }
1014        break;
1015        case BTA_DM_DISC_CMPL_EVT:
1016        {
1017            HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1018        }
1019        break;
1020        case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1021        {
1022           /* if inquiry is not in progress and we get a cancel event, then
1023            * it means we are done with inquiry, but remote_name fetches are in
1024            * progress
1025            *
1026            * if inquiry  is in progress, then we don't want to act on this cancel_cmpl_evt
1027            * but instead wait for the cancel_cmpl_evt via the Busy Level
1028            *
1029            */
1030           if (btif_dm_inquiry_in_progress == FALSE)
1031           {
1032               HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1033           }
1034        }
1035        break;
1036    }
1037}
1038
1039/*******************************************************************************
1040**
1041** Function         btif_dm_search_services_evt
1042**
1043** Description      Executes search services event in btif context
1044**
1045** Returns          void
1046**
1047*******************************************************************************/
1048static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1049{
1050    tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1051
1052    BTIF_TRACE_EVENT2("%s:  event = %d", __FUNCTION__, event);
1053    switch (event)
1054    {
1055        case BTA_DM_DISC_RES_EVT:
1056        {
1057            bt_uuid_t uuid_arr[BT_MAX_NUM_UUIDS]; /* Max 32 services */
1058            bt_property_t prop;
1059            uint32_t i = 0,  j = 0;
1060            bt_bdaddr_t bd_addr;
1061            bt_status_t ret;
1062
1063            bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1064
1065            BTIF_TRACE_DEBUG3("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
1066                    p_data->disc_res.result, p_data->disc_res.services);
1067            if  ((p_data->disc_res.result != BTA_SUCCESS) &&
1068                 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1069                 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1070            {
1071                BTIF_TRACE_WARNING1("%s:SDP failed after bonding re-attempting", __FUNCTION__);
1072                pairing_cb.sdp_attempts++;
1073                btif_dm_get_remote_services(&bd_addr);
1074                return;
1075            }
1076            prop.type = BT_PROPERTY_UUIDS;
1077            prop.len = 0;
1078            if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1079            {
1080                 prop.val = p_data->disc_res.p_uuid_list;
1081                 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1082                 for (i=0; i < p_data->disc_res.num_uuids; i++)
1083                 {
1084                      char temp[256];
1085                      uuid_to_string((bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i*MAX_UUID_SIZE)), temp);
1086                      BTIF_TRACE_ERROR2("Index: %d uuid:%s", i, temp);
1087                 }
1088            }
1089
1090            /* onUuidChanged requires getBondedDevices to be populated.
1091            ** bond_state_changed needs to be sent prior to remote_device_property
1092            */
1093            if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1094                (bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0)&&
1095                pairing_cb.sdp_attempts > 0)
1096            {
1097                 BTIF_TRACE_DEBUG1("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
1098                                   __FUNCTION__);
1099                 pairing_cb.sdp_attempts  = 0;
1100                 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1101            }
1102
1103            /* Also write this to the NVRAM */
1104            ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1105            ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1106            /* Send the event to the BTIF */
1107            HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1108                             BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1109        }
1110        break;
1111
1112        case BTA_DM_DISC_CMPL_EVT:
1113            /* fixme */
1114        break;
1115
1116        default:
1117        {
1118            ASSERTC(0, "unhandled search services event", event);
1119        }
1120        break;
1121    }
1122}
1123
1124/*******************************************************************************
1125**
1126** Function         btif_dm_remote_service_record_evt
1127**
1128** Description      Executes search service record event in btif context
1129**
1130** Returns          void
1131**
1132*******************************************************************************/
1133static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1134{
1135    tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1136
1137    BTIF_TRACE_EVENT2("%s:  event = %d", __FUNCTION__, event);
1138    switch (event)
1139    {
1140        case BTA_DM_DISC_RES_EVT:
1141        {
1142            bt_service_record_t rec;
1143            bt_property_t prop;
1144            uint32_t i = 0;
1145            bt_bdaddr_t bd_addr;
1146
1147            memset(&rec, 0, sizeof(bt_service_record_t));
1148            bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1149
1150            BTIF_TRACE_DEBUG3("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
1151                    p_data->disc_res.result, p_data->disc_res.services);
1152            prop.type = BT_PROPERTY_SERVICE_RECORD;
1153            prop.val = (void*)&rec;
1154            prop.len = sizeof(rec);
1155
1156            /* disc_res.result is overloaded with SCN. Cannot check result */
1157            p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1158            /* TODO: Get the UUID as well */
1159            rec.channel = p_data->disc_res.result - 3;
1160            /* TODO: Need to get the service name using p_raw_data */
1161            rec.name[0] = 0;
1162
1163            HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1164                             BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1165        }
1166        break;
1167
1168        default:
1169        {
1170           ASSERTC(0, "unhandled remote service record event", event);
1171        }
1172        break;
1173    }
1174}
1175
1176/*******************************************************************************
1177**
1178** Function         btif_dm_upstreams_cback
1179**
1180** Description      Executes UPSTREAMS events in btif context
1181**
1182** Returns          void
1183**
1184*******************************************************************************/
1185static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1186{
1187    tBTA_DM_SEC_EVT dm_event = (tBTA_DM_SEC_EVT)event;
1188    tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1189    tBTA_SERVICE_MASK service_mask;
1190    uint32_t i;
1191    bt_bdaddr_t bd_addr;
1192
1193    BTIF_TRACE_EVENT1("btif_dm_upstreams_cback  ev: %s", dump_dm_event(event));
1194
1195    switch (event)
1196    {
1197        case BTA_DM_ENABLE_EVT:
1198        {
1199             BD_NAME bdname;
1200             bt_status_t status;
1201             bt_property_t prop;
1202             prop.type = BT_PROPERTY_BDNAME;
1203             prop.len = BD_NAME_LEN;
1204             prop.val = (void*)bdname;
1205
1206             status = btif_storage_get_adapter_property(&prop);
1207             /* Storage does not have a name yet.
1208             ** Use the default name and write it to the chip
1209             */
1210             if (status != BT_STATUS_SUCCESS)
1211             {
1212                 BTA_DmSetDeviceName((char *)BTM_DEF_LOCAL_NAME);
1213                 /* Hmmm...Should we store this too??? */
1214             }
1215             else
1216             {
1217                 /* A name exists in the storage. Make this the device name */
1218                 BTA_DmSetDeviceName((char*)prop.val);
1219             }
1220
1221             /* for each of the enabled services in the mask, trigger the profile
1222              * enable */
1223             service_mask = btif_get_enabled_services_mask();
1224             for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1225             {
1226                 if (service_mask &
1227                     (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1228                 {
1229                     btif_in_execute_service_request(i, TRUE);
1230                 }
1231             }
1232             /* clear control blocks */
1233             memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1234
1235             /* This function will also trigger the adapter_properties_cb
1236             ** and bonded_devices_info_cb
1237             */
1238             btif_storage_load_bonded_devices();
1239
1240             btif_storage_load_autopair_device_list();
1241
1242             btif_enable_bluetooth_evt(p_data->enable.status, p_data->enable.bd_addr);
1243        }
1244        break;
1245
1246        case BTA_DM_DISABLE_EVT:
1247            /* for each of the enabled services in the mask, trigger the profile
1248             * disable */
1249            service_mask = btif_get_enabled_services_mask();
1250            for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1251            {
1252                if (service_mask &
1253                    (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1254                {
1255                    btif_in_execute_service_request(i, FALSE);
1256                }
1257            }
1258            btif_disable_bluetooth_evt();
1259            break;
1260
1261        case BTA_DM_PIN_REQ_EVT:
1262            btif_dm_pin_req_evt(&p_data->pin_req);
1263            break;
1264
1265        case BTA_DM_AUTH_CMPL_EVT:
1266            btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1267            break;
1268
1269        case BTA_DM_BOND_CANCEL_CMPL_EVT:
1270            if (pairing_cb.state == BT_BOND_STATE_BONDING)
1271            {
1272                bdcpy(bd_addr.address, pairing_cb.bd_addr);
1273                bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1274            }
1275            break;
1276
1277        case BTA_DM_SP_CFM_REQ_EVT:
1278            btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1279            break;
1280        case BTA_DM_SP_KEY_NOTIF_EVT:
1281            btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1282            break;
1283
1284        case BTA_DM_DEV_UNPAIRED_EVT:
1285            bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1286
1287            /*special handling for HID devices */
1288            #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
1289            btif_hh_remove_device(bd_addr);
1290            #endif
1291            btif_storage_remove_bonded_device(&bd_addr);
1292            bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1293            break;
1294
1295        case BTA_DM_BUSY_LEVEL_EVT:
1296        {
1297
1298            if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
1299            {
1300                if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
1301                {
1302                       HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1303                                                BT_DISCOVERY_STARTED);
1304                       btif_dm_inquiry_in_progress = TRUE;
1305                }
1306                else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
1307                {
1308                       HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1309                                                BT_DISCOVERY_STOPPED);
1310                       btif_dm_inquiry_in_progress = FALSE;
1311                }
1312                else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
1313                {
1314                       btif_dm_inquiry_in_progress = FALSE;
1315                }
1316            }
1317        }break;
1318
1319        case BTA_DM_LINK_UP_EVT:
1320            bdcpy(bd_addr.address, p_data->link_up.bd_addr);
1321            BTIF_TRACE_DEBUG0("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
1322
1323            btif_update_remote_version_property(&bd_addr);
1324
1325            HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1326                      &bd_addr, BT_ACL_STATE_CONNECTED);
1327            break;
1328
1329        case BTA_DM_LINK_DOWN_EVT:
1330            bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1331            BTIF_TRACE_DEBUG0("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
1332            HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1333                      &bd_addr, BT_ACL_STATE_DISCONNECTED);
1334            break;
1335
1336        case BTA_DM_HW_ERROR_EVT:
1337            BTIF_TRACE_ERROR0("Received H/W Error. ");
1338            /* Flush storage data */
1339            btif_config_flush();
1340            usleep(100000); /* 100milliseconds */
1341            /* Killing the process to force a restart as part of fault tolerance */
1342            kill(getpid(), SIGKILL);
1343            break;
1344
1345#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1346        case BTA_DM_BLE_KEY_EVT:
1347            BTIF_TRACE_DEBUG1("BTA_DM_BLE_KEY_EVT key_type=0x%02x ", p_data->ble_key.key_type);
1348
1349            /* If this pairing is by-product of local initiated GATT client Read or Write,
1350            BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1351            have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1352            if (pairing_cb.state != BT_BOND_STATE_BONDING)
1353            {
1354                BTIF_TRACE_DEBUG0("Bond state not sent to App so far.Notify the app now");
1355                bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1356                                   BT_BOND_STATE_BONDING);
1357            }
1358            else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1359            {
1360                BTIF_TRACE_ERROR1("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
1361                break;
1362            }
1363
1364            switch (p_data->ble_key.key_type)
1365            {
1366                case BTA_LE_KEY_PENC:
1367                    BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_PENC");
1368                    pairing_cb.ble.is_penc_key_rcvd = TRUE;
1369                    memcpy(pairing_cb.ble.penc_key.ltk,p_data->ble_key.key_value.penc_key.ltk, 16);
1370                    memcpy(pairing_cb.ble.penc_key.rand, p_data->ble_key.key_value.penc_key.rand,8);
1371                    pairing_cb.ble.penc_key.ediv = p_data->ble_key.key_value.penc_key.ediv;
1372                    pairing_cb.ble.penc_key.sec_level = p_data->ble_key.key_value.penc_key.sec_level;
1373
1374                    for (i=0; i<16; i++)
1375                    {
1376                        BTIF_TRACE_DEBUG2("pairing_cb.ble.penc_key.ltk[%d]=0x%02x",i,pairing_cb.ble.penc_key.ltk[i]);
1377                    }
1378                    for (i=0; i<8; i++)
1379                    {
1380                        BTIF_TRACE_DEBUG2("pairing_cb.ble.penc_key.rand[%d]=0x%02x",i,pairing_cb.ble.penc_key.rand[i]);
1381                    }
1382                    BTIF_TRACE_DEBUG1("pairing_cb.ble.penc_key.ediv=0x%04x",pairing_cb.ble.penc_key.ediv);
1383                    BTIF_TRACE_DEBUG1("pairing_cb.ble.penc_key.sec_level=0x%02x",pairing_cb.ble.penc_key.sec_level);
1384                    BTIF_TRACE_DEBUG1("pairing_cb.ble.penc_key.key_size=0x%02x",pairing_cb.ble.penc_key.key_size);
1385                    break;
1386
1387                case BTA_LE_KEY_PID:
1388                    BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_PID");
1389                    pairing_cb.ble.is_pid_key_rcvd = TRUE;
1390                    memcpy(pairing_cb.ble.pid_key, p_data->ble_key.key_value.pid_key.irk, 16);
1391                    for (i=0; i<16; i++)
1392                    {
1393                        BTIF_TRACE_DEBUG2("pairing_cb.ble.pid_key[%d]=0x%02x",i,pairing_cb.ble.pid_key[i]);
1394                    }
1395                    break;
1396
1397                case BTA_LE_KEY_PCSRK:
1398                    BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_PCSRK");
1399                    pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1400                    pairing_cb.ble.pcsrk_key.counter = p_data->ble_key.key_value.pcsrk_key.counter;
1401                    pairing_cb.ble.pcsrk_key.sec_level = p_data->ble_key.key_value.pcsrk_key.sec_level;
1402                    memcpy(pairing_cb.ble.pcsrk_key.csrk,p_data->ble_key.key_value.pcsrk_key.csrk,16);
1403
1404                    for (i=0; i<16; i++)
1405                    {
1406                        BTIF_TRACE_DEBUG2("pairing_cb.ble.pcsrk_key.csrk[%d]=0x%02x",i,pairing_cb.ble.pcsrk_key.csrk[i]);
1407                    }
1408                    BTIF_TRACE_DEBUG1("pairing_cb.ble.pcsrk_key.counter=0x%08x",pairing_cb.ble.pcsrk_key.counter);
1409                    BTIF_TRACE_DEBUG1("pairing_cb.ble.pcsrk_key.sec_level=0x%02x",pairing_cb.ble.pcsrk_key.sec_level);
1410                    break;
1411
1412                case BTA_LE_KEY_LENC:
1413                    BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_LENC");
1414                    pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1415                    pairing_cb.ble.lenc_key.div = p_data->ble_key.key_value.lenc_key.div;
1416                    pairing_cb.ble.lenc_key.key_size = p_data->ble_key.key_value.lenc_key.key_size;
1417                    pairing_cb.ble.lenc_key.sec_level = p_data->ble_key.key_value.lenc_key.sec_level;
1418
1419                    BTIF_TRACE_DEBUG1("pairing_cb.ble.lenc_key.div=0x%04x",pairing_cb.ble.lenc_key.div);
1420                    BTIF_TRACE_DEBUG1("pairing_cb.ble.lenc_key.key_size=0x%02x",pairing_cb.ble.lenc_key.key_size);
1421                    BTIF_TRACE_DEBUG1("pairing_cb.ble.lenc_key.sec_level=0x%02x",pairing_cb.ble.lenc_key.sec_level);
1422                    break;
1423
1424
1425
1426                case BTA_LE_KEY_LCSRK:
1427                    BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_LCSRK");
1428                    pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1429                    pairing_cb.ble.lcsrk_key.counter = p_data->ble_key.key_value.lcsrk_key.counter;
1430                    pairing_cb.ble.lcsrk_key.div = p_data->ble_key.key_value.lcsrk_key.div;
1431                    pairing_cb.ble.lcsrk_key.sec_level = p_data->ble_key.key_value.lcsrk_key.sec_level;
1432
1433                    BTIF_TRACE_DEBUG1("pairing_cb.ble.lcsrk_key.div=0x%04x",pairing_cb.ble.lcsrk_key.div);
1434                    BTIF_TRACE_DEBUG1("pairing_cb.ble.lcsrk_key.counter=0x%08x",pairing_cb.ble.lcsrk_key.counter);
1435                    BTIF_TRACE_DEBUG1("pairing_cb.ble.lcsrk_key.sec_level=0x%02x",pairing_cb.ble.lcsrk_key.sec_level);
1436
1437                    break;
1438
1439                default:
1440                    BTIF_TRACE_ERROR1("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
1441                    break;
1442            }
1443
1444            break;
1445        case BTA_DM_BLE_SEC_REQ_EVT:
1446            BTIF_TRACE_DEBUG0("BTA_DM_BLE_SEC_REQ_EVT. ");
1447            btif_dm_ble_sec_req_evt(&p_data->ble_req);
1448            break;
1449        case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
1450            BTIF_TRACE_DEBUG0("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
1451            btif_dm_ble_key_notif_evt(&p_data->key_notif);
1452            break;
1453        case BTA_DM_BLE_PASSKEY_REQ_EVT:
1454            BTIF_TRACE_DEBUG0("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
1455            btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1456            break;
1457        case BTA_DM_BLE_OOB_REQ_EVT:
1458            BTIF_TRACE_DEBUG0("BTA_DM_BLE_OOB_REQ_EVT. ");
1459            break;
1460        case BTA_DM_BLE_LOCAL_IR_EVT:
1461            BTIF_TRACE_DEBUG0("BTA_DM_BLE_LOCAL_IR_EVT. ");
1462            ble_local_key_cb.is_id_keys_rcvd = TRUE;
1463            memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1464            memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1465            memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1466            btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1467                                            BTIF_DM_LE_LOCAL_KEY_IR,
1468                                            BT_OCTET16_LEN);
1469            btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1470                                            BTIF_DM_LE_LOCAL_KEY_IRK,
1471                                            BT_OCTET16_LEN);
1472            btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1473                                            BTIF_DM_LE_LOCAL_KEY_DHK,
1474                                            BT_OCTET16_LEN);
1475            break;
1476        case BTA_DM_BLE_LOCAL_ER_EVT:
1477            BTIF_TRACE_DEBUG0("BTA_DM_BLE_LOCAL_ER_EVT. ");
1478            ble_local_key_cb.is_er_rcvd = TRUE;
1479            memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1480            btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1481                                            BTIF_DM_LE_LOCAL_KEY_ER,
1482                                            BT_OCTET16_LEN);
1483            break;
1484
1485        case BTA_DM_BLE_AUTH_CMPL_EVT:
1486            BTIF_TRACE_DEBUG0("BTA_DM_BLE_KEY_EVT. ");
1487            btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1488            break;
1489#endif
1490
1491        case BTA_DM_AUTHORIZE_EVT:
1492        case BTA_DM_SIG_STRENGTH_EVT:
1493        case BTA_DM_SP_RMT_OOB_EVT:
1494        case BTA_DM_SP_KEYPRESS_EVT:
1495        case BTA_DM_ROLE_CHG_EVT:
1496
1497        default:
1498            BTIF_TRACE_WARNING1( "btif_dm_cback : unhandled event (%d)", event );
1499            break;
1500    }
1501} /* btui_security_cback() */
1502
1503
1504/*******************************************************************************
1505**
1506** Function         btif_dm_generic_evt
1507**
1508** Description      Executes non-BTA upstream events in BTIF context
1509**
1510** Returns          void
1511**
1512*******************************************************************************/
1513static void btif_dm_generic_evt(UINT16 event, char* p_param)
1514{
1515    BTIF_TRACE_EVENT2("%s: event=%d", __FUNCTION__, event);
1516    switch(event)
1517    {
1518        case BTIF_DM_CB_DISCOVERY_STARTED:
1519        {
1520            HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
1521        }
1522        break;
1523
1524        case BTIF_DM_CB_CREATE_BOND:
1525        {
1526            btif_dm_cb_create_bond((bt_bdaddr_t *)p_param);
1527        }
1528        break;
1529
1530        case BTIF_DM_CB_REMOVE_BOND:
1531        {
1532            btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
1533        }
1534        break;
1535
1536        case BTIF_DM_CB_HID_REMOTE_NAME:
1537        {
1538            btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
1539        }
1540        break;
1541
1542        case BTIF_DM_CB_BOND_STATE_BONDING:
1543            {
1544                bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
1545            }
1546            break;
1547        case BTIF_DM_CB_LE_TX_TEST:
1548        case BTIF_DM_CB_LE_RX_TEST:
1549            {
1550                uint8_t status;
1551                STREAM_TO_UINT8(status, p_param);
1552                HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1553                      (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
1554            }
1555            break;
1556        case BTIF_DM_CB_LE_TEST_END:
1557            {
1558                uint8_t status;
1559                uint16_t count = 0;
1560                STREAM_TO_UINT8(status, p_param);
1561                if (status == 0)
1562                    STREAM_TO_UINT16(count, p_param);
1563                HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1564                      (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
1565            }
1566            break;
1567        default:
1568        {
1569            BTIF_TRACE_WARNING2("%s : Unknown event 0x%x", __FUNCTION__, event);
1570        }
1571        break;
1572    }
1573}
1574
1575/*******************************************************************************
1576**
1577** Function         bte_dm_evt
1578**
1579** Description      Switches context from BTE to BTIF for all DM events
1580**
1581** Returns          void
1582**
1583*******************************************************************************/
1584
1585void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
1586{
1587    bt_status_t status;
1588
1589    /* switch context to btif task context (copy full union size for convenience) */
1590    status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event, (void*)p_data, sizeof(tBTA_DM_SEC), NULL);
1591
1592    /* catch any failed context transfers */
1593    ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
1594}
1595
1596/*******************************************************************************
1597**
1598** Function         bte_search_devices_evt
1599**
1600** Description      Switches context from BTE to BTIF for DM search events
1601**
1602** Returns          void
1603**
1604*******************************************************************************/
1605static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1606{
1607    UINT16 param_len = 0;
1608
1609    if (p_data)
1610        param_len += sizeof(tBTA_DM_SEARCH);
1611    /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
1612    switch (event)
1613    {
1614        case BTA_DM_INQ_RES_EVT:
1615        {
1616            if (p_data->inq_res.p_eir)
1617                param_len += HCI_EXT_INQ_RESPONSE_LEN;
1618        }
1619        break;
1620
1621        case BTA_DM_DISC_RES_EVT:
1622        {
1623            if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
1624                param_len += p_data->disc_res.raw_data_size;
1625        }
1626        break;
1627    }
1628    BTIF_TRACE_DEBUG3("%s event=%s param_len=%d", __FUNCTION__, dump_dm_search_event(event), param_len);
1629
1630    /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
1631    if (event == BTA_DM_INQ_RES_EVT)
1632        p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
1633
1634    btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
1635        (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
1636}
1637
1638/*******************************************************************************
1639**
1640** Function         bte_dm_search_services_evt
1641**
1642** Description      Switches context from BTE to BTIF for DM search services
1643**                  event
1644**
1645** Returns          void
1646**
1647*******************************************************************************/
1648static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1649{
1650    UINT16 param_len = 0;
1651   if (p_data)
1652       param_len += sizeof(tBTA_DM_SEARCH);
1653   switch (event)
1654   {
1655         case BTA_DM_DISC_RES_EVT:
1656         {
1657             if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
1658                  param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
1659             }
1660         } break;
1661   }
1662   /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
1663    * if raw_data is needed. */
1664   btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
1665         (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
1666}
1667
1668/*******************************************************************************
1669**
1670** Function         bte_dm_remote_service_record_evt
1671**
1672** Description      Switches context from BTE to BTIF for DM search service
1673**                  record event
1674**
1675** Returns          void
1676**
1677*******************************************************************************/
1678static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1679{
1680   /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
1681   btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
1682}
1683
1684/*****************************************************************************
1685**
1686**   btif api functions (no context switch)
1687**
1688*****************************************************************************/
1689
1690/*******************************************************************************
1691**
1692** Function         btif_dm_start_discovery
1693**
1694** Description      Start device discovery/inquiry
1695**
1696** Returns          bt_status_t
1697**
1698*******************************************************************************/
1699bt_status_t btif_dm_start_discovery(void)
1700{
1701    tBTA_DM_INQ inq_params;
1702    tBTA_SERVICE_MASK services = 0;
1703
1704    BTIF_TRACE_EVENT1("%s", __FUNCTION__);
1705    /* TODO: Do we need to handle multiple inquiries at the same time? */
1706
1707    /* Set inquiry params and call API */
1708#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1709    inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
1710#else
1711    inq_params.mode = BTA_DM_GENERAL_INQUIRY;
1712#endif
1713    inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
1714
1715    inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
1716    inq_params.report_dup = TRUE;
1717
1718    inq_params.filter_type = BTA_DM_INQ_CLR;
1719    /* TODO: Filter device by BDA needs to be implemented here */
1720
1721    /* Will be enabled to TRUE once inquiry busy level has been received */
1722    btif_dm_inquiry_in_progress = FALSE;
1723    /* find nearby devices */
1724    BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
1725
1726    return BT_STATUS_SUCCESS;
1727}
1728
1729/*******************************************************************************
1730**
1731** Function         btif_dm_cancel_discovery
1732**
1733** Description      Cancels search
1734**
1735** Returns          bt_status_t
1736**
1737*******************************************************************************/
1738bt_status_t btif_dm_cancel_discovery(void)
1739{
1740    BTIF_TRACE_EVENT1("%s", __FUNCTION__);
1741    BTA_DmSearchCancel();
1742    return BT_STATUS_SUCCESS;
1743}
1744
1745/*******************************************************************************
1746**
1747** Function         btif_dm_create_bond
1748**
1749** Description      Initiate bonding with the specified device
1750**
1751** Returns          bt_status_t
1752**
1753*******************************************************************************/
1754bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr)
1755{
1756    bdstr_t bdstr;
1757
1758    BTIF_TRACE_EVENT2("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *) bd_addr, &bdstr));
1759    if (pairing_cb.state != BT_BOND_STATE_NONE)
1760        return BT_STATUS_BUSY;
1761
1762    btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
1763                          (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
1764
1765    return BT_STATUS_SUCCESS;
1766}
1767
1768/*******************************************************************************
1769**
1770** Function         btif_dm_cancel_bond
1771**
1772** Description      Initiate bonding with the specified device
1773**
1774** Returns          bt_status_t
1775**
1776*******************************************************************************/
1777
1778bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
1779{
1780    bdstr_t bdstr;
1781
1782    BTIF_TRACE_EVENT2("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *)bd_addr, &bdstr));
1783
1784    /* TODO:
1785    **  1. Restore scan modes
1786    **  2. special handling for HID devices
1787    */
1788    if (pairing_cb.state == BT_BOND_STATE_BONDING)
1789    {
1790
1791#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1792
1793        if (pairing_cb.is_ssp)
1794        {
1795            if (pairing_cb.is_le_only)
1796            {
1797                BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
1798            }
1799            else
1800                BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
1801        }
1802        else
1803        {
1804            if (pairing_cb.is_le_only)
1805            {
1806                BTA_DmBondCancel ((UINT8 *)bd_addr->address);
1807            }
1808            else
1809            {
1810                BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
1811            }
1812        /* Cancel bonding, in case it is in ACL connection setup state */
1813        BTA_DmBondCancel ((UINT8 *)bd_addr->address);
1814        }
1815
1816#else
1817        if (pairing_cb.is_ssp)
1818        {
1819            BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
1820        }
1821        else
1822        {
1823            BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
1824        }
1825        /* Cancel bonding, in case it is in ACL connection setup state */
1826        BTA_DmBondCancel ((UINT8 *)bd_addr->address);
1827        btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
1828#endif
1829    }
1830
1831    return BT_STATUS_SUCCESS;
1832}
1833
1834/*******************************************************************************
1835**
1836** Function         btif_dm_remove_bond
1837**
1838** Description      Removes bonding with the specified device
1839**
1840** Returns          bt_status_t
1841**
1842*******************************************************************************/
1843
1844bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
1845{
1846    bdstr_t bdstr;
1847
1848    BTIF_TRACE_EVENT2("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *)bd_addr, &bdstr));
1849    btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
1850                          (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
1851
1852    return BT_STATUS_SUCCESS;
1853}
1854
1855/*******************************************************************************
1856**
1857** Function         btif_dm_pin_reply
1858**
1859** Description      BT legacy pairing - PIN code reply
1860**
1861** Returns          bt_status_t
1862**
1863*******************************************************************************/
1864
1865bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
1866                               uint8_t pin_len, bt_pin_code_t *pin_code)
1867{
1868    BTIF_TRACE_EVENT2("%s: accept=%d", __FUNCTION__, accept);
1869#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1870
1871    if (pairing_cb.is_le_only)
1872    {
1873        int i;
1874        UINT32 passkey = 0;
1875        int multi[] = {100000, 10000, 1000, 100, 10,1};
1876        BD_ADDR remote_bd_addr;
1877        bdcpy(remote_bd_addr, bd_addr->address);
1878        for (i = 0; i < 6; i++)
1879        {
1880            passkey += (multi[i] * (pin_code->pin[i] - '0'));
1881        }
1882        BTIF_TRACE_DEBUG1("btif_dm_pin_reply: passkey: %d", passkey);
1883        BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
1884
1885    }
1886    else
1887    {
1888        BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
1889        if (accept)
1890            pairing_cb.pin_code_len = pin_len;
1891    }
1892#else
1893    BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
1894
1895    if (accept)
1896        pairing_cb.pin_code_len = pin_len;
1897#endif
1898    return BT_STATUS_SUCCESS;
1899}
1900
1901/*******************************************************************************
1902**
1903** Function         btif_dm_ssp_reply
1904**
1905** Description      BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
1906**
1907** Returns          bt_status_t
1908**
1909*******************************************************************************/
1910
1911bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
1912                                 bt_ssp_variant_t variant, uint8_t accept,
1913                                 uint32_t passkey)
1914{
1915    if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
1916    {
1917        /* This is not implemented in the stack.
1918         * For devices with display, this is not needed
1919        */
1920        BTIF_TRACE_WARNING1("%s: Not implemented", __FUNCTION__);
1921        return BT_STATUS_FAIL;
1922    }
1923    /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
1924    BTIF_TRACE_EVENT2("%s: accept=%d", __FUNCTION__, accept);
1925#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1926    if (pairing_cb.is_le_only)
1927    {
1928        if (accept)
1929            BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
1930        else
1931            BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
1932    }
1933    else
1934        BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
1935
1936#else
1937    BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
1938#endif
1939    return BT_STATUS_SUCCESS;
1940}
1941
1942/*******************************************************************************
1943**
1944** Function         btif_dm_get_adapter_property
1945**
1946** Description     Queries the BTA for the adapter property
1947**
1948** Returns          bt_status_t
1949**
1950*******************************************************************************/
1951bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
1952{
1953    bt_status_t status;
1954
1955    BTIF_TRACE_EVENT2("%s: type=0x%x", __FUNCTION__, prop->type);
1956    switch (prop->type)
1957    {
1958        case BT_PROPERTY_BDNAME:
1959        {
1960            bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
1961            strcpy((char *)bd_name->name, (char *)BTM_DEF_LOCAL_NAME);
1962            prop->len = strlen((char *)bd_name->name);
1963        }
1964        break;
1965
1966        case BT_PROPERTY_ADAPTER_SCAN_MODE:
1967        {
1968            /* if the storage does not have it. Most likely app never set it. Default is NONE */
1969            bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
1970            *mode = BT_SCAN_MODE_NONE;
1971            prop->len = sizeof(bt_scan_mode_t);
1972        }
1973        break;
1974
1975        case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
1976        {
1977            uint32_t *tmt = (uint32_t*)prop->val;
1978            *tmt = 120; /* default to 120s, if not found in NV */
1979            prop->len = sizeof(uint32_t);
1980        }
1981        break;
1982
1983        default:
1984            prop->len = 0;
1985            return BT_STATUS_FAIL;
1986    }
1987    return BT_STATUS_SUCCESS;
1988}
1989
1990/*******************************************************************************
1991**
1992** Function         btif_dm_get_remote_services
1993**
1994** Description      Start SDP to get remote services
1995**
1996** Returns          bt_status_t
1997**
1998*******************************************************************************/
1999bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2000{
2001    bdstr_t bdstr;
2002
2003    BTIF_TRACE_EVENT2("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
2004
2005    BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2006                   bte_dm_search_services_evt, TRUE);
2007
2008    return BT_STATUS_SUCCESS;
2009}
2010
2011/*******************************************************************************
2012**
2013** Function         btif_dm_get_remote_service_record
2014**
2015** Description      Start SDP to get remote service record
2016**
2017**
2018** Returns          bt_status_t
2019*******************************************************************************/
2020bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2021                                                    bt_uuid_t *uuid)
2022{
2023    tSDP_UUID sdp_uuid;
2024    bdstr_t bdstr;
2025
2026    BTIF_TRACE_EVENT2("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
2027
2028    sdp_uuid.len = MAX_UUID_SIZE;
2029    memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2030
2031    BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2032                       bte_dm_remote_service_record_evt, TRUE);
2033
2034    return BT_STATUS_SUCCESS;
2035}
2036
2037void btif_dm_execute_service_request(UINT16 event, char *p_param)
2038{
2039    BOOLEAN b_enable = FALSE;
2040    bt_status_t status;
2041    if (event == BTIF_DM_ENABLE_SERVICE)
2042    {
2043        b_enable = TRUE;
2044    }
2045    status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2046    if (status == BT_STATUS_SUCCESS)
2047    {
2048        bt_property_t property;
2049        bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2050
2051        /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2052        BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2053                                    sizeof(local_uuids), local_uuids);
2054        btif_storage_get_adapter_property(&property);
2055        HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2056                          BT_STATUS_SUCCESS, 1, &property);
2057    }
2058    return;
2059}
2060
2061#if (BTM_OOB_INCLUDED == TRUE)
2062void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA  *p_oob_data)
2063{
2064    if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2065        oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2066    {
2067        *p_oob_data = FALSE;
2068    }
2069    else
2070    {
2071        *p_oob_data = TRUE;
2072    }
2073    BTIF_TRACE_DEBUG1("btif_dm_set_oob_for_io_req *p_oob_data=%d", *p_oob_data);
2074}
2075#endif /* BTM_OOB_INCLUDED */
2076
2077#ifdef BTIF_DM_OOB_TEST
2078void btif_dm_load_local_oob(void)
2079{
2080    char prop_oob[PROPERTY_VALUE_MAX];
2081    property_get("service.brcm.bt.oob", prop_oob, "3");
2082    BTIF_TRACE_DEBUG1("btif_dm_load_local_oob prop_oob = %s",prop_oob);
2083    if (prop_oob[0] != '3')
2084    {
2085#if (BTM_OOB_INCLUDED == TRUE)
2086        if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2087            oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2088        {
2089            BTIF_TRACE_DEBUG0("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
2090            BTA_DmLocalOob();
2091        }
2092#else
2093        BTIF_TRACE_ERROR0("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
2094#endif
2095    }
2096}
2097
2098void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2099{
2100    FILE *fp;
2101    char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2102    char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2103    char *path = NULL;
2104    char prop_oob[PROPERTY_VALUE_MAX];
2105    BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob: valid=%d", valid);
2106    if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2107        oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2108        valid)
2109    {
2110        BTIF_TRACE_DEBUG0("save local OOB data in memory");
2111        memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2112        memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2113        property_get("service.brcm.bt.oob", prop_oob, "3");
2114        BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
2115        if (prop_oob[0] == '1')
2116            path = path_a;
2117        else if (prop_oob[0] == '2')
2118            path = path_b;
2119        if (path)
2120        {
2121            fp = fopen(path, "wb+");
2122            if (fp == NULL)
2123            {
2124                BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob: failed to save local OOB data to %s", path);
2125            }
2126            else
2127            {
2128                BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob: save local OOB data into file %s",path);
2129                fwrite (c , 1 , BT_OCTET16_LEN , fp );
2130                fwrite (r , 1 , BT_OCTET16_LEN , fp );
2131                fclose(fp);
2132            }
2133        }
2134    }
2135}
2136BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr,  BT_OCTET16 p_c, BT_OCTET16 p_r)
2137{
2138    char t[128];
2139    FILE *fp;
2140    char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2141    char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2142    char *path = NULL;
2143    char prop_oob[PROPERTY_VALUE_MAX];
2144    BOOLEAN result = FALSE;
2145    bt_bdaddr_t bt_bd_addr;
2146    bdcpy(oob_cb.oob_bdaddr, bd_addr);
2147    property_get("service.brcm.bt.oob", prop_oob, "3");
2148    BTIF_TRACE_DEBUG1("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
2149    if (prop_oob[0] == '1')
2150        path = path_b;
2151    else if (prop_oob[0] == '2')
2152        path = path_a;
2153    if (path)
2154    {
2155        fp = fopen(path, "rb");
2156        if (fp == NULL)
2157        {
2158            BTIF_TRACE_DEBUG1("btapp_dm_rmt_oob_reply: failed to read OOB keys from %s",path);
2159            return FALSE;
2160        }
2161        else
2162        {
2163            BTIF_TRACE_DEBUG1("btif_dm_proc_rmt_oob: read OOB data from %s",path);
2164            fread (p_c , 1 , BT_OCTET16_LEN , fp );
2165            fread (p_r , 1 , BT_OCTET16_LEN , fp );
2166            fclose(fp);
2167        }
2168        BTIF_TRACE_DEBUG0("----btif_dm_proc_rmt_oob: TRUE");
2169        sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2170                oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2171                oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
2172        BTIF_TRACE_DEBUG1("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
2173        sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2174                p_c[0], p_c[1], p_c[2],  p_c[3],  p_c[4],  p_c[5],  p_c[6],  p_c[7],
2175                p_c[8], p_c[9], p_c[10], p_c[11], p_c[12], p_c[13], p_c[14], p_c[15]);
2176        BTIF_TRACE_DEBUG1("----btif_dm_proc_rmt_oob: c = %s",t);
2177        sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2178                p_r[0], p_r[1], p_r[2],  p_r[3],  p_r[4],  p_r[5],  p_r[6],  p_r[7],
2179                p_r[8], p_r[9], p_r[10], p_r[11], p_r[12], p_r[13], p_r[14], p_r[15]);
2180        BTIF_TRACE_DEBUG1("----btif_dm_proc_rmt_oob: r = %s",t);
2181        bdcpy(bt_bd_addr.address, bd_addr);
2182        btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2183                              (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2184        result = TRUE;
2185    }
2186    BTIF_TRACE_DEBUG1("btif_dm_proc_rmt_oob result=%d",result);
2187    return result;
2188}
2189#endif /*  BTIF_DM_OOB_TEST */
2190#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2191
2192static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2193{
2194    bt_bdaddr_t bd_addr;
2195    bt_bdname_t bd_name;
2196    UINT32 cod;
2197
2198    BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
2199
2200    /* Remote name update */
2201    btif_update_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2202                                          NULL, BT_DEVICE_TYPE_BLE);
2203    bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2204    memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2205
2206    bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2207    pairing_cb.is_ssp = FALSE;
2208    cod = COD_UNCLASSIFIED;
2209
2210    HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2211              cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2212              p_ssp_key_notif->passkey);
2213}
2214
2215/*******************************************************************************
2216**
2217** Function         btif_dm_ble_auth_cmpl_evt
2218**
2219** Description      Executes authentication complete event in btif context
2220**
2221** Returns          void
2222**
2223*******************************************************************************/
2224static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2225{
2226    /* Save link key, if not temporary */
2227    bt_bdaddr_t bd_addr;
2228    bt_status_t status = BT_STATUS_FAIL;
2229    bt_bond_state_t state = BT_BOND_STATE_NONE;
2230
2231    bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2232    if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2233    {
2234        /* store keys */
2235    }
2236    if (p_auth_cmpl->success)
2237    {
2238        status = BT_STATUS_SUCCESS;
2239        state = BT_BOND_STATE_BONDED;
2240
2241        btif_dm_save_ble_bonding_keys();
2242        BTA_GATTC_Refresh(bd_addr.address);
2243        btif_dm_get_remote_services(&bd_addr);
2244    }
2245    else
2246    {
2247        /*Map the HCI fail reason  to  bt status  */
2248        switch (p_auth_cmpl->fail_reason)
2249        {
2250            default:
2251                btif_dm_remove_ble_bonding_keys();
2252                status =  BT_STATUS_FAIL;
2253                break;
2254        }
2255    }
2256    bond_state_changed(status, &bd_addr, state);
2257}
2258
2259
2260
2261void    btif_dm_load_ble_local_keys(void)
2262{
2263    bt_status_t bt_status;
2264
2265    memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2266
2267    if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2268                                       BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2269    {
2270        ble_local_key_cb.is_er_rcvd = TRUE;
2271        BTIF_TRACE_DEBUG1("%s BLE ER key loaded",__FUNCTION__ );
2272    }
2273
2274    if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
2275                                        BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
2276        (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
2277                                        BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
2278        (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
2279                                        BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
2280    {
2281        ble_local_key_cb.is_id_keys_rcvd = TRUE;
2282        BTIF_TRACE_DEBUG1("%s BLE ID keys loaded",__FUNCTION__ );
2283    }
2284
2285}
2286void    btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
2287                                   tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
2288{
2289    if (ble_local_key_cb.is_er_rcvd )
2290    {
2291        memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2292        *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2293    }
2294
2295    if (ble_local_key_cb.is_id_keys_rcvd)
2296    {
2297        memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
2298        memcpy(&p_id_keys->irk[0],  &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
2299        memcpy(&p_id_keys->dhk[0],  &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
2300        *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
2301    }
2302    BTIF_TRACE_DEBUG2("%s  *p_key_mask=0x%02x",__FUNCTION__,   *p_key_mask);
2303}
2304
2305void btif_dm_save_ble_bonding_keys(void)
2306{
2307
2308    bt_bdaddr_t bd_addr;
2309
2310    BTIF_TRACE_DEBUG1("%s",__FUNCTION__ );
2311
2312    bdcpy(bd_addr.address, pairing_cb.bd_addr);
2313
2314    if (pairing_cb.ble.is_penc_key_rcvd)
2315    {
2316        btif_storage_add_ble_bonding_key(&bd_addr,
2317                                         (char *) &pairing_cb.ble.penc_key,
2318                                         BTIF_DM_LE_KEY_PENC,
2319                                         sizeof(btif_dm_ble_penc_keys_t));
2320    }
2321
2322    if (pairing_cb.ble.is_pid_key_rcvd)
2323    {
2324        btif_storage_add_ble_bonding_key(&bd_addr,
2325                                         (char *) &pairing_cb.ble.pid_key[0],
2326                                         BTIF_DM_LE_KEY_PID,
2327                                         BT_OCTET16_LEN);
2328    }
2329
2330
2331    if (pairing_cb.ble.is_pcsrk_key_rcvd)
2332    {
2333        btif_storage_add_ble_bonding_key(&bd_addr,
2334                                         (char *) &pairing_cb.ble.pcsrk_key,
2335                                         BTIF_DM_LE_KEY_PCSRK,
2336                                         sizeof(btif_dm_ble_pcsrk_keys_t));
2337    }
2338
2339
2340    if (pairing_cb.ble.is_lenc_key_rcvd)
2341    {
2342        btif_storage_add_ble_bonding_key(&bd_addr,
2343                                         (char *) &pairing_cb.ble.lenc_key,
2344                                         BTIF_DM_LE_KEY_LENC,
2345                                         sizeof(btif_dm_ble_lenc_keys_t));
2346    }
2347
2348    if (pairing_cb.ble.is_lcsrk_key_rcvd)
2349    {
2350        btif_storage_add_ble_bonding_key(&bd_addr,
2351                                         (char *) &pairing_cb.ble.lcsrk_key,
2352                                         BTIF_DM_LE_KEY_LCSRK,
2353                                         sizeof(btif_dm_ble_lcsrk_keys_t));
2354    }
2355
2356}
2357
2358
2359void btif_dm_remove_ble_bonding_keys(void)
2360{
2361    bt_bdaddr_t bd_addr;
2362
2363    BTIF_TRACE_DEBUG1("%s",__FUNCTION__ );
2364
2365    bdcpy(bd_addr.address, pairing_cb.bd_addr);
2366    btif_storage_remove_ble_bonding_keys(&bd_addr);
2367}
2368
2369
2370/*******************************************************************************
2371**
2372** Function         btif_dm_ble_sec_req_evt
2373**
2374** Description      Eprocess security request event in btif context
2375**
2376** Returns          void
2377**
2378*******************************************************************************/
2379void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
2380{
2381    bt_bdaddr_t bd_addr;
2382    bt_bdname_t bd_name;
2383    UINT32 cod;
2384    BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
2385
2386    if (pairing_cb.state == BT_BOND_STATE_BONDING)
2387    {
2388        BTIF_TRACE_DEBUG1("%s Discard security request", __FUNCTION__);
2389        return;
2390    }
2391
2392    /* Remote name update */
2393    btif_update_remote_properties(p_ble_req->bd_addr,p_ble_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
2394
2395    bdcpy(bd_addr.address, p_ble_req->bd_addr);
2396    memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
2397
2398    bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2399
2400    pairing_cb.is_temp = FALSE;
2401    pairing_cb.is_le_only = TRUE;
2402    pairing_cb.is_ssp = TRUE;
2403
2404    cod = COD_UNCLASSIFIED;
2405
2406    HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
2407              BT_SSP_VARIANT_CONSENT, 0);
2408}
2409
2410
2411
2412/*******************************************************************************
2413**
2414** Function         btif_dm_ble_passkey_req_evt
2415**
2416** Description      Executes pin request event in btif context
2417**
2418** Returns          void
2419**
2420*******************************************************************************/
2421static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
2422{
2423    bt_bdaddr_t bd_addr;
2424    bt_bdname_t bd_name;
2425    UINT32 cod;
2426
2427    /* Remote name update */
2428    btif_update_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
2429
2430    bdcpy(bd_addr.address, p_pin_req->bd_addr);
2431    memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
2432
2433    bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2434    pairing_cb.is_le_only = TRUE;
2435
2436    cod = COD_UNCLASSIFIED;
2437
2438    HAL_CBACK(bt_hal_cbacks, pin_request_cb,
2439              &bd_addr, &bd_name, cod);
2440}
2441
2442
2443void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
2444                                           tBT_DEVICE_TYPE dev_type)
2445{
2446   btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
2447}
2448
2449static void btif_dm_ble_tx_test_cback(void *p)
2450{
2451    btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
2452                          (char *)p, 1, NULL);
2453}
2454
2455static void btif_dm_ble_rx_test_cback(void *p)
2456{
2457    btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
2458                          (char *)p, 1, NULL);
2459}
2460
2461static void btif_dm_ble_test_end_cback(void *p)
2462{
2463    btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
2464                          (char *)p, 3, NULL);
2465}
2466/*******************************************************************************
2467**
2468** Function         btif_le_test_mode
2469**
2470** Description     Sends a HCI BLE Test command to the Controller
2471**
2472** Returns          BT_STATUS_SUCCESS on success
2473**
2474*******************************************************************************/
2475bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
2476{
2477     switch (opcode) {
2478         case HCI_BLE_TRANSMITTER_TEST:
2479             if (len != 3) return BT_STATUS_PARM_INVALID;
2480             BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
2481             break;
2482         case HCI_BLE_RECEIVER_TEST:
2483             if (len != 1) return BT_STATUS_PARM_INVALID;
2484             BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
2485             break;
2486         case HCI_BLE_TEST_END:
2487             BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
2488             break;
2489         default:
2490             BTIF_TRACE_ERROR2("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
2491             return BT_STATUS_UNSUPPORTED;
2492     }
2493     return BT_STATUS_SUCCESS;
2494}
2495
2496#endif
2497
2498void btif_dm_on_disable()
2499{
2500    /* cancel any pending pairing requests */
2501    if (pairing_cb.state == BT_BOND_STATE_BONDING)
2502    {
2503        bt_bdaddr_t bd_addr;
2504
2505        BTIF_TRACE_DEBUG1("%s: Cancel pending pairing request", __FUNCTION__);
2506        bdcpy(bd_addr.address, pairing_cb.bd_addr);
2507        btif_dm_cancel_bond(&bd_addr);
2508    }
2509}
2510