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