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