blob: 5b86ab40687764c470728a11312bf24949f7758f [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
3 * Copyright (C) 2009-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19/************************************************************************************
20 *
21 * Filename: btif_dm.c
22 *
23 * Description: Contains Device Management (DM) related functionality
24 *
25 *
26 ***********************************************************************************/
Sharvil Nanavati44802762014-12-23 23:08:58 -080027
Chris Mantonf8027002015-03-12 09:22:48 -070028#define LOG_TAG "bt_btif_dm"
Sharvil Nanavati44802762014-12-23 23:08:58 -080029
The Android Open Source Project5738f832012-12-12 16:00:35 -080030#include <stdio.h>
31#include <stdlib.h>
Ian Coolidgec7503db2015-01-24 02:01:26 -080032#include <string.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080033#include <unistd.h>
34
35#include <hardware/bluetooth.h>
36
The Android Open Source Project5738f832012-12-12 16:00:35 -080037#include <cutils/properties.h>
38#include "gki.h"
39#include "btu.h"
Sharvil Nanavati95b74f22015-03-12 15:55:21 -070040#include "btcore/include/bdaddr.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080041#include "bta_api.h"
42#include "btif_api.h"
43#include "btif_util.h"
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080044#include "btif_dm.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080045#include "btif_storage.h"
46#include "btif_hh.h"
47#include "btif_config.h"
48
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080049#include "bta_gatt_api.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080050#include "include/stack_config.h"
Chris Mantonf8027002015-03-12 09:22:48 -070051
Sharvil Nanavati44802762014-12-23 23:08:58 -080052#include "osi/include/log.h"
Andre Eisenbach31a64002014-10-14 14:29:19 -070053
54/******************************************************************************
55** Device specific workarounds
56******************************************************************************/
57
58/**
59 * The devices below have proven problematic during the pairing process, often
60 * requiring multiple retries to complete pairing. To avoid degrading the user
61 * experience for other devices, explicitely blacklist troubled devices here.
62 */
63static const UINT8 blacklist_pairing_retries[][3] = {
64 {0x9C, 0xDF, 0x03} // BMW car kits (Harman/Becker)
65};
66
67BOOLEAN blacklistPairingRetries(BD_ADDR bd_addr)
68{
69 const unsigned blacklist_size = sizeof(blacklist_pairing_retries)
70 / sizeof(blacklist_pairing_retries[0]);
71 for (unsigned i = 0; i != blacklist_size; ++i)
72 {
73 if (blacklist_pairing_retries[i][0] == bd_addr[0] &&
74 blacklist_pairing_retries[i][1] == bd_addr[1] &&
75 blacklist_pairing_retries[i][2] == bd_addr[2])
76 return TRUE;
77 }
78 return FALSE;
79}
80
The Android Open Source Project5738f832012-12-12 16:00:35 -080081/******************************************************************************
82** Constants & Macros
83******************************************************************************/
84
85#define COD_UNCLASSIFIED ((0x1F) << 8)
Priti Agheraebb1d752012-11-27 18:03:22 -080086#define COD_HID_KEYBOARD 0x0540
87#define COD_HID_POINTING 0x0580
88#define COD_HID_COMBO 0x05C0
89#define COD_HID_MAJOR 0x0500
90#define COD_AV_HEADSETS 0x0404
91#define COD_AV_HANDSFREE 0x0408
92#define COD_AV_HEADPHONES 0x0418
93#define COD_AV_PORTABLE_AUDIO 0x041C
94#define COD_AV_HIFI_AUDIO 0x0428
The Android Open Source Project5738f832012-12-12 16:00:35 -080095
96
97#define BTIF_DM_DEFAULT_INQ_MAX_RESULTS 0
98#define BTIF_DM_DEFAULT_INQ_MAX_DURATION 10
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -070099#define BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING 2
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800100
Andre Eisenbach31a64002014-10-14 14:29:19 -0700101#define NUM_TIMEOUT_RETRIES 5
102
Matthew Xie1e5109b2012-11-09 18:26:26 -0800103#define PROPERTY_PRODUCT_MODEL "ro.product.model"
Matthew Xiea30d95a2013-09-18 12:30:36 -0700104#define DEFAULT_LOCAL_NAME_MAX 31
Matthew Xie1e5109b2012-11-09 18:26:26 -0800105#if (DEFAULT_LOCAL_NAME_MAX > BTM_MAX_LOC_BD_NAME_LEN)
106 #error "default btif local name size exceeds stack supported length"
107#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800108
Matthew Xie7f3e4292013-09-30 12:44:10 -0700109#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
110#define BTIF_DM_INTERLEAVE_DURATION_BR_ONE 2
111#define BTIF_DM_INTERLEAVE_DURATION_LE_ONE 2
112#define BTIF_DM_INTERLEAVE_DURATION_BR_TWO 3
113#define BTIF_DM_INTERLEAVE_DURATION_LE_TWO 4
114#endif
115
Priti Agherac0edf9f2014-06-26 11:23:51 -0700116#define MAX_SDP_BL_ENTRIES 3
117
Andre Eisenbach89363762015-01-26 13:49:36 -0800118#define BOND_TYPE_UNKNOWN 0
119#define BOND_TYPE_PERSISTENT 1
120#define BOND_TYPE_TEMPORARY 2
121
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800122#define ENCRYPTED_BREDR 2
123#define ENCRYPTED_LE 4
124
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800125typedef struct
126{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800127 bt_bond_state_t state;
128 BD_ADDR bd_addr;
Andre Eisenbach89363762015-01-26 13:49:36 -0800129 UINT8 bond_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800130 UINT8 pin_code_len;
131 UINT8 is_ssp;
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -0700132 UINT8 auth_req;
133 UINT8 io_cap;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800134 UINT8 autopair_attempts;
Andre Eisenbach31a64002014-10-14 14:29:19 -0700135 UINT8 timeout_retries;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800136 UINT8 is_local_initiated;
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700137 UINT8 sdp_attempts;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800138#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
139 BOOLEAN is_le_only;
140 btif_dm_ble_cb_t ble;
141#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800142} btif_dm_pairing_cb_t;
143
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800144
145typedef struct
146{
147 UINT8 ir[BT_OCTET16_LEN];
148 UINT8 irk[BT_OCTET16_LEN];
149 UINT8 dhk[BT_OCTET16_LEN];
150}btif_dm_local_key_id_t;
151
152typedef struct
153{
154 BOOLEAN is_er_rcvd;
155 UINT8 er[BT_OCTET16_LEN];
156 BOOLEAN is_id_keys_rcvd;
157 btif_dm_local_key_id_t id_keys; /* ID kyes */
158
159}btif_dm_local_key_cb_t;
160
161typedef struct
162{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800163 BD_ADDR bd_addr;
164 BD_NAME bd_name;
165} btif_dm_remote_name_t;
166
167typedef struct
168{
169 BT_OCTET16 sp_c;
170 BT_OCTET16 sp_r;
171 BD_ADDR oob_bdaddr; /* peer bdaddr*/
172} btif_dm_oob_cb_t;
Satya Callojie5ba8842014-07-03 17:18:02 -0700173
174typedef struct
175{
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700176 bt_bdaddr_t bdaddr;
177 UINT8 transport; /* 0=Unknown, 1=BR/EDR, 2=LE */
178} btif_dm_create_bond_cb_t;
179
180typedef struct
181{
Satya Callojie5ba8842014-07-03 17:18:02 -0700182 uint8_t status;
183 uint8_t ctrl_state;
184 uint64_t tx_time;
185 uint64_t rx_time;
186 uint64_t idle_time;
187 uint64_t energy_used;
188} btif_activity_energy_info_cb_t;
189
Priti Agherac0edf9f2014-06-26 11:23:51 -0700190typedef struct
191{
192 unsigned int manufact_id;
193}skip_sdp_entry_t;
194
The Android Open Source Project5738f832012-12-12 16:00:35 -0800195#define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id))
196
Priti Agherac0edf9f2014-06-26 11:23:51 -0700197#define MAX_SDP_BL_ENTRIES 3
198#define UUID_HUMAN_INTERFACE_DEVICE "00001124-0000-1000-8000-00805f9b34fb"
199
200static skip_sdp_entry_t sdp_blacklist[] = {{76}}; //Apple Mouse and Keyboard
201
202
The Android Open Source Project5738f832012-12-12 16:00:35 -0800203/* This flag will be true if HCI_Inquiry is in progress */
204static BOOLEAN btif_dm_inquiry_in_progress = FALSE;
205
Priti Agherac0edf9f2014-06-26 11:23:51 -0700206
207
Matthew Xie1e5109b2012-11-09 18:26:26 -0800208/************************************************************************************
209** Static variables
210************************************************************************************/
211static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'};
212
The Android Open Source Project5738f832012-12-12 16:00:35 -0800213/******************************************************************************
214** Static functions
215******************************************************************************/
216static btif_dm_pairing_cb_t pairing_cb;
217static btif_dm_oob_cb_t oob_cb;
218static void btif_dm_generic_evt(UINT16 event, char* p_param);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700219static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800220static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name);
221static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
222 DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800223#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
224static btif_dm_local_key_cb_t ble_local_key_cb;
225static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif);
226static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl);
227static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req);
228#endif
Satya Calloji6e2d9db2014-07-08 16:18:58 -0700229
230static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
231 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
232 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status);
233
Matthew Xie1e5109b2012-11-09 18:26:26 -0800234static char* btif_get_default_local_name();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800235/******************************************************************************
236** Externs
237******************************************************************************/
238extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
239extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
240extern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
Rakesh Iyer9c8dfac2015-04-08 12:25:37 -0700241extern bt_status_t btif_av_sink_execute_service(BOOLEAN b_enable);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800242extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
Hemant Gupta10256872013-08-19 18:33:01 +0530243extern bt_status_t btif_hf_client_execute_service(BOOLEAN b_enable);
Hemant Gupta2dc99992014-04-18 12:54:08 +0530244extern bt_status_t btif_mce_execute_service(BOOLEAN b_enable);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800245extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700246extern void bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800247
248
249/******************************************************************************
250** Functions
251******************************************************************************/
252
253bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
254 BOOLEAN b_enable)
255{
256 /* Check the service_ID and invoke the profile's BT state changed API */
257 switch (service_id)
258 {
259 case BTA_HFP_SERVICE_ID:
260 case BTA_HSP_SERVICE_ID:
261 {
262 btif_hf_execute_service(b_enable);
263 }break;
Sharvil Nanavati9609cee2014-10-15 18:30:49 -0700264 case BTA_A2DP_SOURCE_SERVICE_ID:
The Android Open Source Project5738f832012-12-12 16:00:35 -0800265 {
266 btif_av_execute_service(b_enable);
267 }break;
Rakesh Iyer9c8dfac2015-04-08 12:25:37 -0700268 case BTA_A2DP_SINK_SERVICE_ID:
269 {
270 btif_av_sink_execute_service(b_enable);
271 }break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800272 case BTA_HID_SERVICE_ID:
273 {
274 btif_hh_execute_service(b_enable);
275 }break;
Hemant Gupta10256872013-08-19 18:33:01 +0530276 case BTA_HFP_HS_SERVICE_ID:
277 {
278 btif_hf_client_execute_service(b_enable);
279 }break;
Hemant Gupta2dc99992014-04-18 12:54:08 +0530280 case BTA_MAP_SERVICE_ID:
281 {
282 btif_mce_execute_service(b_enable);
283 }break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800284 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700285 BTIF_TRACE_ERROR("%s: Unknown service being enabled", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800286 return BT_STATUS_FAIL;
287 }
288 return BT_STATUS_SUCCESS;
289}
290
291/*******************************************************************************
292**
293** Function check_eir_remote_name
294**
295** Description Check if remote name is in the EIR data
296**
297** Returns TRUE if remote name found
298** Populate p_remote_name, if provided and remote name found
299**
300*******************************************************************************/
301static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
302 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
303{
304 UINT8 *p_eir_remote_name = NULL;
305 UINT8 remote_name_len = 0;
306
307 /* Check EIR for remote name and services */
308 if (p_search_data->inq_res.p_eir)
309 {
Zach Johnsona50fc882014-10-30 21:02:58 -0700310 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800311 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
312 if (!p_eir_remote_name)
313 {
Zach Johnsona50fc882014-10-30 21:02:58 -0700314 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800315 BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
316 }
317
318 if (p_eir_remote_name)
319 {
320 if (remote_name_len > BD_NAME_LEN)
321 remote_name_len = BD_NAME_LEN;
322
323 if (p_remote_name && p_remote_name_len)
324 {
325 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
326 *(p_remote_name + remote_name_len) = 0;
327 *p_remote_name_len = remote_name_len;
328 }
329
330 return TRUE;
331 }
332 }
333
334 return FALSE;
335
336}
337
338/*******************************************************************************
339**
340** Function check_cached_remote_name
341**
342** Description Check if remote name is in the NVRAM cache
343**
344** Returns TRUE if remote name found
345** Populate p_remote_name, if provided and remote name found
346**
347*******************************************************************************/
348static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
349 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
350{
351 bt_bdname_t bdname;
352 bt_bdaddr_t remote_bdaddr;
353 bt_property_t prop_name;
354
355 /* check if we already have it in our btif_storage cache */
356 bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
357 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
358 sizeof(bt_bdname_t), &bdname);
359 if (btif_storage_get_remote_device_property(
360 &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
361 {
362 if (p_remote_name && p_remote_name_len)
363 {
364 strcpy((char *)p_remote_name, (char *)bdname.name);
365 *p_remote_name_len = strlen((char *)p_remote_name);
366 }
367 return TRUE;
368 }
369
370 return FALSE;
371}
372
373BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
374{
375 uint32_t remote_cod;
376 bt_property_t prop_name;
377
378 /* check if we already have it in our btif_storage cache */
379 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
380 sizeof(uint32_t), &remote_cod);
381 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
382 {
Chris Mantonf8027002015-03-12 09:22:48 -0700383 LOG_INFO("%s remote_cod = 0x%08x cod = 0x%08x", __func__, remote_cod, cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800384 if ((remote_cod & 0x7ff) == cod)
385 return TRUE;
386 }
387
388 return FALSE;
389}
390
Priti Agheraebb1d752012-11-27 18:03:22 -0800391BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
392{
393 uint32_t remote_cod;
394 bt_property_t prop_name;
395
396 /* check if we already have it in our btif_storage cache */
397 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
398 sizeof(uint32_t), &remote_cod);
399 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
400 &prop_name) == BT_STATUS_SUCCESS)
401 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700402 BTIF_TRACE_DEBUG("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
Priti Agheraebb1d752012-11-27 18:03:22 -0800403 if ((remote_cod & 0x700) == cod)
404 return TRUE;
405 }
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700406 return FALSE;
407}
Priti Agheraebb1d752012-11-27 18:03:22 -0800408
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700409BOOLEAN check_hid_le(const bt_bdaddr_t *remote_bdaddr)
410{
411 uint32_t remote_dev_type;
412 bt_property_t prop_name;
413
414 /* check if we already have it in our btif_storage cache */
415 BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE,
416 sizeof(uint32_t), &remote_dev_type);
417 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
418 &prop_name) == BT_STATUS_SUCCESS)
419 {
420 if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE)
421 {
422 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -0700423 bdaddr_to_string(remote_bdaddr, bdstr, sizeof(bdstr));
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700424 if(btif_config_exist(bdstr, "HidAppId"))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700425 return TRUE;
426 }
427 }
Priti Agheraebb1d752012-11-27 18:03:22 -0800428 return FALSE;
429}
430
Priti Agherac0edf9f2014-06-26 11:23:51 -0700431/*****************************************************************************
432**
433** Function check_sdp_bl
434**
435** Description Checks if a given device is blacklisted to skip sdp
436**
437** Parameters skip_sdp_entry
438**
439** Returns TRUE if the device is present in blacklist, else FALSE
440**
441*******************************************************************************/
442BOOLEAN check_sdp_bl(const bt_bdaddr_t *remote_bdaddr)
443{
Priti Agherac0edf9f2014-06-26 11:23:51 -0700444 UINT16 manufacturer = 0;
445 UINT8 lmp_ver = 0;
446 UINT16 lmp_subver = 0;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700447 bt_property_t prop_name;
448 bt_remote_version_t info;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700449
450
451 if (remote_bdaddr == NULL)
452 return FALSE;
453
454/* fetch additional info about remote device used in iop query */
Sharvil Nanavatif1c764f2015-02-23 17:31:48 -0800455 BTM_ReadRemoteVersion(*(BD_ADDR*)remote_bdaddr, &lmp_ver,
Priti Agherac0edf9f2014-06-26 11:23:51 -0700456 &manufacturer, &lmp_subver);
457
458
459
460 /* if not available yet, try fetching from config database */
461 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO,
462 sizeof(bt_remote_version_t), &info);
463
464 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
465 &prop_name) != BT_STATUS_SUCCESS)
466 {
467
468 return FALSE;
469 }
470 manufacturer = info.manufacturer;
471
472 for (int i = 0; i < MAX_SDP_BL_ENTRIES; i++)
473 {
474 if (manufacturer == sdp_blacklist[i].manufact_id)
475 return TRUE;
476 }
477 return FALSE;
478}
479
480
The Android Open Source Project5738f832012-12-12 16:00:35 -0800481static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
482{
483 /* Send bonding state only once - based on outgoing/incoming we may receive duplicates */
484 if ( (pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING) )
485 return;
486
Andre Eisenbach89363762015-01-26 13:49:36 -0800487 if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800488 {
489 state = BT_BOND_STATE_NONE;
490 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700491 BTIF_TRACE_DEBUG("%s: state=%d prev_state=%d", __FUNCTION__, state, pairing_cb.state);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800492
493 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
494
495 if (state == BT_BOND_STATE_BONDING)
496 {
497 pairing_cb.state = state;
498 bdcpy(pairing_cb.bd_addr, bd_addr->address);
499 }
500 else
501 {
502 memset(&pairing_cb, 0, sizeof(pairing_cb));
503 }
504
505}
506
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800507/* store remote version in bt config to always have access
508 to it post pairing*/
509static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
510{
511 bt_property_t property;
512 UINT8 lmp_ver = 0;
513 UINT16 lmp_subver = 0;
514 UINT16 mfct_set = 0;
515 tBTM_STATUS btm_status;
516 bt_remote_version_t info;
517 bt_status_t status;
518 bdstr_t bdstr;
519
520 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
521 &mfct_set, &lmp_subver);
522
Sharvil Nanavati44802762014-12-23 23:08:58 -0800523 LOG_DEBUG("remote version info [%s]: %x, %x, %x", bdaddr_to_string(p_bd, bdstr, sizeof(bdstr)),
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800524 lmp_ver, mfct_set, lmp_subver);
525
526 if (btm_status == BTM_SUCCESS)
527 {
528 /* always update cache to ensure we have availability whenever BTM API
529 is not populated */
530 info.manufacturer = mfct_set;
531 info.sub_ver = lmp_subver;
532 info.version = lmp_ver;
533 BTIF_STORAGE_FILL_PROPERTY(&property,
534 BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
535 &info);
536 status = btif_storage_set_remote_device_property(p_bd, &property);
537 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
538 }
539}
540
The Android Open Source Project5738f832012-12-12 16:00:35 -0800541
542static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
543 DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
544{
545 int num_properties = 0;
546 bt_property_t properties[3];
547 bt_bdaddr_t bdaddr;
548 bt_status_t status;
549 UINT32 cod;
550 bt_device_type_t dev_type;
551
552 memset(properties, 0, sizeof(properties));
553 bdcpy(bdaddr.address, bd_addr);
554
555 /* remote name */
556 if (strlen((const char *) bd_name))
557 {
558 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
559 BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
560 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
561 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
562 num_properties++;
563 }
564
565 /* class of device */
566 cod = devclass2uint(dev_class);
Chris Mantonf8027002015-03-12 09:22:48 -0700567 BTIF_TRACE_DEBUG("%s cod is 0x%06x", __func__, cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800568 if ( cod == 0) {
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530569 /* Try to retrieve cod from storage */
Chris Mantonf8027002015-03-12 09:22:48 -0700570 BTIF_TRACE_DEBUG("%s cod is 0, checking cod from storage", __func__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530571 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
572 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
573 status = btif_storage_get_remote_device_property(&bdaddr, &properties[num_properties]);
Chris Mantonf8027002015-03-12 09:22:48 -0700574 BTIF_TRACE_DEBUG("%s cod retrieved from storage is 0x%06x", __func__, cod);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530575 if ( cod == 0) {
Chris Mantonf8027002015-03-12 09:22:48 -0700576 BTIF_TRACE_DEBUG("%s cod is again 0, set as unclassified", __func__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530577 cod = COD_UNCLASSIFIED;
578 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800579 }
580
581 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
582 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
583 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
584 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
585 num_properties++;
586
587 /* device type */
588 dev_type = device_type;
589 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
590 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
591 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
592 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
593 num_properties++;
594
595 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
596 status, &bdaddr, num_properties, properties);
597}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800598
599/*******************************************************************************
600**
601** Function btif_dm_cb_hid_remote_name
602**
603** Description Remote name callback for HID device. Called in btif context
604** Special handling for HID devices
605**
606** Returns void
607**
608*******************************************************************************/
609static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
610{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700611 BTIF_TRACE_DEBUG("%s: status=%d pairing_cb.state=%d", __FUNCTION__, p_remote_name->status, pairing_cb.state);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800612 if (pairing_cb.state == BT_BOND_STATE_BONDING)
613 {
614 bt_bdaddr_t remote_bd;
615
616 bdcpy(remote_bd.address, pairing_cb.bd_addr);
617
618 if (p_remote_name->status == BTM_SUCCESS)
619 {
620 bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
621 }
622 else
623 bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
624 }
625}
626
The Android Open Source Project5738f832012-12-12 16:00:35 -0800627/*******************************************************************************
628**
629** Function btif_dm_cb_create_bond
630**
631** Description Create bond initiated from the BTIF thread context
632** Special handling for HID devices
633**
634** Returns void
635**
636*******************************************************************************/
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700637static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800638{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800639 BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800640 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800641
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800642#if BLE_INCLUDED == TRUE
643 int device_type;
644 int addr_type;
645 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -0700646 bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr));
Matthew Xie64c54792014-09-16 00:55:03 -0700647 if (transport == BT_TRANSPORT_LE)
648 {
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700649 if (!btif_config_get_int((char const *)&bdstr,"DevType", &device_type))
Matthew Xie64c54792014-09-16 00:55:03 -0700650 {
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700651 btif_config_set_int(bdstr, "DevType", BT_DEVICE_TYPE_BLE);
Matthew Xie64c54792014-09-16 00:55:03 -0700652 }
653 if (btif_storage_get_remote_addr_type(bd_addr, &addr_type) != BT_STATUS_SUCCESS)
654 {
655 btif_storage_set_remote_addr_type(bd_addr, BLE_ADDR_PUBLIC);
656 }
657 }
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700658 if((btif_config_get_int((char const *)&bdstr,"DevType", &device_type) &&
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800659 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
lungtsai_lin3baff792014-08-29 13:47:47 +0800660 (device_type == BT_DEVICE_TYPE_BLE)) || (transport == BT_TRANSPORT_LE))
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800661 {
662 BTA_DmAddBleDevice(bd_addr->address, addr_type, BT_DEVICE_TYPE_BLE);
663 }
664#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800665
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800666#if BLE_INCLUDED == TRUE
667 if(is_hid && device_type != BT_DEVICE_TYPE_BLE)
668#else
669 if(is_hid)
670#endif
671 {
672 int status;
673 status = btif_hh_connect(bd_addr);
674 if(status != BT_STATUS_SUCCESS)
675 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800676 }
677 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800678 {
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700679 BTA_DmBondByTransport((UINT8 *)bd_addr->address, transport);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800680 }
681 /* Track originator of bond creation */
682 pairing_cb.is_local_initiated = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800683
684}
685
686/*******************************************************************************
687**
688** Function btif_dm_cb_remove_bond
689**
690** Description remove bond initiated from the BTIF thread context
691** Special handling for HID devices
692**
693** Returns void
694**
695*******************************************************************************/
696void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
697{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800698 /*special handling for HID devices */
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700699 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there
700 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
701#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
702 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
703#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800704 {
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700705 BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800706 }
707}
708
709/*******************************************************************************
710**
Andre Eisenbach249f6002014-06-18 12:20:37 -0700711** Function btif_dm_get_connection_state
712**
713** Description Returns whether the remote device is currently connected
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800714** and whether encryption is active for the connection
Andre Eisenbach249f6002014-06-18 12:20:37 -0700715**
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800716** Returns 0 if not connected; 1 if connected and > 1 if connection is
717** encrypted
Andre Eisenbach249f6002014-06-18 12:20:37 -0700718**
719*******************************************************************************/
720uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr)
721{
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800722 uint8_t *bda = (uint8_t*)bd_addr->address;
723 uint16_t rc = BTA_DmGetConnectionState(bda);
724
725 if (rc != 0)
726 {
727 uint8_t flags = 0;
728
729 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_BR_EDR);
730 BTIF_TRACE_DEBUG("%s: security flags (BR/EDR)=0x%02x", __FUNCTION__, flags);
731 if (flags & BTM_SEC_FLAG_ENCRYPTED)
732 rc |= ENCRYPTED_BREDR;
733
734 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_LE);
735 BTIF_TRACE_DEBUG("%s: security flags (LE)=0x%02x", __FUNCTION__, flags);
736 if (flags & BTM_SEC_FLAG_ENCRYPTED)
737 rc |= ENCRYPTED_LE;
738 }
739
740 return rc;
Andre Eisenbach249f6002014-06-18 12:20:37 -0700741}
742
743/*******************************************************************************
744**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800745** Function search_devices_copy_cb
746**
747** Description Deep copy callback for search devices event
748**
749** Returns void
750**
751*******************************************************************************/
752static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
753{
754 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
755 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
756
757 if (!p_src)
758 return;
759
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700760 BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800761 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
762 switch (event)
763 {
764 case BTA_DM_INQ_RES_EVT:
765 {
766 if (p_src_data->inq_res.p_eir)
767 {
768 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
769 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
770 }
771 }
772 break;
773
774 case BTA_DM_DISC_RES_EVT:
775 {
776 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
777 {
778 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
779 memcpy(p_dest_data->disc_res.p_raw_data,
780 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
781 }
782 }
783 break;
784 }
785}
786
787static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
788{
789 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
790 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
791
792 if (!p_src)
793 return;
794 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
795 switch (event)
796 {
797 case BTA_DM_DISC_RES_EVT:
798 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530799 if (p_src_data->disc_res.result == BTA_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800800 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530801 if (p_src_data->disc_res.num_uuids > 0)
802 {
803 p_dest_data->disc_res.p_uuid_list =
804 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
805 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
806 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
807 GKI_freebuf(p_src_data->disc_res.p_uuid_list);
808 }
809 if (p_src_data->disc_res.p_raw_data != NULL)
810 {
811 GKI_freebuf(p_src_data->disc_res.p_raw_data);
812 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800813 }
814 } break;
815 }
816}
817/******************************************************************************
818**
819** BTIF DM callback events
820**
821*****************************************************************************/
822
823/*******************************************************************************
824**
825** Function btif_dm_pin_req_evt
826**
827** Description Executes pin request event in btif context
828**
829** Returns void
830**
831*******************************************************************************/
832static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
833{
834 bt_bdaddr_t bd_addr;
835 bt_bdname_t bd_name;
836 UINT32 cod;
837 bt_pin_code_t pin_code;
Matthew Xie86f97ed2014-11-10 10:24:46 -0800838 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800839
840 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -0800841 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
842 {
843 dev_type = BT_DEVICE_TYPE_BREDR;
844 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800845 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -0800846 p_pin_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800847
848 bdcpy(bd_addr.address, p_pin_req->bd_addr);
849 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
850
851 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
852
853 cod = devclass2uint(p_pin_req->dev_class);
854
Chris Mantonf8027002015-03-12 09:22:48 -0700855 if (cod == 0) {
856 BTIF_TRACE_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800857 cod = COD_UNCLASSIFIED;
858 }
859
860 /* check for auto pair possiblity only if bond was initiated by local device */
861 if (pairing_cb.is_local_initiated)
862 {
863 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
864 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
865 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
866 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
867 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
868 check_cod(&bd_addr, COD_HID_POINTING))
869 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700870 BTIF_TRACE_DEBUG("%s()cod matches for auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800871 /* Check if this device can be auto paired */
872 if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) &&
873 (pairing_cb.autopair_attempts == 0))
874 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700875 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800876 pin_code.pin[0] = 0x30;
877 pin_code.pin[1] = 0x30;
878 pin_code.pin[2] = 0x30;
879 pin_code.pin[3] = 0x30;
880
881 pairing_cb.autopair_attempts++;
882 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
883 return;
884 }
885 }
886 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
887 check_cod(&bd_addr, COD_HID_COMBO))
888 {
889 if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) &&
890 (pairing_cb.autopair_attempts == 0))
891 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700892 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800893 pin_code.pin[0] = 0x30;
894 pin_code.pin[1] = 0x30;
895 pin_code.pin[2] = 0x30;
896 pin_code.pin[3] = 0x30;
897
898 pairing_cb.autopair_attempts++;
899 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
900 return;
901 }
902 }
903 }
904 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
905 &bd_addr, &bd_name, cod);
906}
907
908/*******************************************************************************
909**
910** Function btif_dm_ssp_cfm_req_evt
911**
912** Description Executes SSP confirm request event in btif context
913**
914** Returns void
915**
916*******************************************************************************/
917static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
918{
919 bt_bdaddr_t bd_addr;
920 bt_bdname_t bd_name;
921 UINT32 cod;
922 BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
Matthew Xie86f97ed2014-11-10 10:24:46 -0800923 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800924
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700925 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800926
927 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -0800928 if (!btif_get_device_type(p_ssp_cfm_req->bd_addr, &dev_type))
929 {
930 dev_type = BT_DEVICE_TYPE_BREDR;
931 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800932 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -0800933 p_ssp_cfm_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800934
935 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
936 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
937
938 /* Set the pairing_cb based on the local & remote authentication requirements */
939 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
940
941 /* if just_works and bonding bit is not set treat this as temporary */
942 if (p_ssp_cfm_req->just_works && !(p_ssp_cfm_req->loc_auth_req & BTM_AUTH_BONDS) &&
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800943 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
944 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
Andre Eisenbach89363762015-01-26 13:49:36 -0800945 pairing_cb.bond_type = BOND_TYPE_TEMPORARY;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800946 else
Andre Eisenbach89363762015-01-26 13:49:36 -0800947 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800948
949 pairing_cb.is_ssp = TRUE;
950
951 /* If JustWorks auto-accept */
952 if (p_ssp_cfm_req->just_works)
953 {
954 /* Pairing consent for JustWorks needed if:
955 * 1. Incoming pairing is detected AND
956 * 2. local IO capabilities are DisplayYesNo AND
957 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
958 */
959 if ((is_incoming) && ((p_ssp_cfm_req->loc_io_caps == 0x01) &&
960 (p_ssp_cfm_req->rmt_io_caps == 0x00 || p_ssp_cfm_req->rmt_io_caps == 0x03)))
961 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700962 BTIF_TRACE_EVENT("%s: User consent needed for incoming pairing request. loc_io_caps: %d, rmt_io_caps: %d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800963 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
964 }
965 else
966 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700967 BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800968 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
969 return;
970 }
971 }
972
973 cod = devclass2uint(p_ssp_cfm_req->dev_class);
974
Chris Mantonf8027002015-03-12 09:22:48 -0700975 if (cod == 0) {
976 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800977 cod = COD_UNCLASSIFIED;
978 }
979
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700980 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800981 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
982 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
983 p_ssp_cfm_req->num_val);
984}
985
986static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
987{
988 bt_bdaddr_t bd_addr;
989 bt_bdname_t bd_name;
990 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -0800991 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800992
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700993 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800994
995 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -0800996 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
997 {
998 dev_type = BT_DEVICE_TYPE_BREDR;
999 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001000 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -08001001 p_ssp_key_notif->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001002
1003 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
1004 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
1005
1006 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1007 pairing_cb.is_ssp = TRUE;
1008 cod = devclass2uint(p_ssp_key_notif->dev_class);
1009
Chris Mantonf8027002015-03-12 09:22:48 -07001010 if (cod == 0) {
1011 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001012 cod = COD_UNCLASSIFIED;
1013 }
1014
1015 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
1016 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
1017 p_ssp_key_notif->passkey);
1018}
1019/*******************************************************************************
1020**
1021** Function btif_dm_auth_cmpl_evt
1022**
1023** Description Executes authentication complete event in btif context
1024**
1025** Returns void
1026**
1027*******************************************************************************/
1028static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
1029{
1030 /* Save link key, if not temporary */
1031 bt_bdaddr_t bd_addr;
1032 bt_status_t status = BT_STATUS_FAIL;
1033 bt_bond_state_t state = BT_BOND_STATE_NONE;
Priti Agherac0edf9f2014-06-26 11:23:51 -07001034 BOOLEAN skip_sdp = FALSE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001035
1036 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
1037 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
1038 {
1039 if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) || (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
Andre Eisenbach89363762015-01-26 13:49:36 -08001040 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) || pairing_cb.bond_type == BOND_TYPE_PERSISTENT)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001041 {
1042 bt_status_t ret;
Andre Eisenbach89363762015-01-26 13:49:36 -08001043 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, bond_type=%d",
1044 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001045 ret = btif_storage_add_bonded_device(&bd_addr,
1046 p_auth_cmpl->key, p_auth_cmpl->key_type,
1047 pairing_cb.pin_code_len);
1048 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
1049 }
1050 else
1051 {
Andre Eisenbach89363762015-01-26 13:49:36 -08001052 BTIF_TRACE_DEBUG("%s: Temporary key. Not storing. key_type=0x%x, bond_type=%d",
1053 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1054 if(pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
Hemant Guptab820aec2013-12-24 19:59:57 +05301055 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001056 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
Hemant Guptab820aec2013-12-24 19:59:57 +05301057 __FUNCTION__);
1058 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1059 return;
1060 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001061 }
1062 }
Priti Agherac0edf9f2014-06-26 11:23:51 -07001063
1064 // Skip SDP for certain HID Devices
The Android Open Source Project5738f832012-12-12 16:00:35 -08001065 if (p_auth_cmpl->success)
1066 {
Andre Eisenbach31a64002014-10-14 14:29:19 -07001067 pairing_cb.timeout_retries = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001068 status = BT_STATUS_SUCCESS;
1069 state = BT_BOND_STATE_BONDED;
Priti Agherac0edf9f2014-06-26 11:23:51 -07001070 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001071
Priti Agherac0edf9f2014-06-26 11:23:51 -07001072 if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr, COD_HID_MAJOR))
1073 {
Sharvil Nanavati44802762014-12-23 23:08:58 -08001074 LOG_WARN("%s:skip SDP", __FUNCTION__);
Priti Agherac0edf9f2014-06-26 11:23:51 -07001075 skip_sdp = TRUE;
1076 }
1077 if(!pairing_cb.is_local_initiated && skip_sdp)
1078 {
1079 bond_state_changed(status, &bd_addr, state);
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -08001080
Sharvil Nanavati44802762014-12-23 23:08:58 -08001081 LOG_WARN("%s: Incoming HID Connection",__FUNCTION__);
Priti Agherac0edf9f2014-06-26 11:23:51 -07001082 bt_property_t prop;
1083 bt_bdaddr_t bd_addr;
1084 bt_uuid_t uuid;
1085 char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE;
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -08001086
Priti Agherac0edf9f2014-06-26 11:23:51 -07001087 string_to_uuid(uuid_str, &uuid);
1088
1089 prop.type = BT_PROPERTY_UUIDS;
1090 prop.val = uuid.uu;
1091 prop.len = MAX_UUID_SIZE;
1092
1093 /* Send the event to the BTIF */
1094 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1095 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1096 }
1097 else
1098 {
1099 /* Trigger SDP on the device */
1100 pairing_cb.sdp_attempts = 1;;
1101
1102 if(btif_dm_inquiry_in_progress)
1103 btif_dm_cancel_discovery();
1104
1105 btif_dm_get_remote_services(&bd_addr);
1106 }
1107 /* Do not call bond_state_changed_cb yet. Wait till fetch remote service is complete */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001108 }
1109 else
1110 {
1111 /*Map the HCI fail reason to bt status */
1112 switch(p_auth_cmpl->fail_reason)
1113 {
1114 case HCI_ERR_PAGE_TIMEOUT:
Andre Eisenbach31a64002014-10-14 14:29:19 -07001115 if (blacklistPairingRetries(bd_addr.address) && pairing_cb.timeout_retries)
1116 {
1117 BTIF_TRACE_WARNING("%s() - Pairing timeout; retrying (%d) ...", __FUNCTION__, pairing_cb.timeout_retries);
1118 --pairing_cb.timeout_retries;
1119 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
1120 return;
1121 }
1122 /* Fall-through */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001123 case HCI_ERR_CONNECTION_TOUT:
1124 status = BT_STATUS_RMT_DEV_DOWN;
1125 break;
1126
Hemant Guptaaef7a672013-07-31 19:00:12 +05301127 case HCI_ERR_PAIRING_NOT_ALLOWED:
1128 status = BT_STATUS_AUTH_REJECTED;
1129 break;
1130
Hemant Guptab4801442014-01-07 18:11:15 +05301131 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
1132 status = BT_STATUS_AUTH_FAILURE;
1133 break;
1134
The Android Open Source Project5738f832012-12-12 16:00:35 -08001135 /* map the auth failure codes, so we can retry pairing if necessary */
1136 case HCI_ERR_AUTH_FAILURE:
Hemant Gupta59a88ec2014-03-19 19:01:35 +05301137 case HCI_ERR_KEY_MISSING:
Zhihai Xua7ea8092013-11-27 14:10:53 +05301138 btif_storage_remove_bonded_device(&bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001139 case HCI_ERR_HOST_REJECT_SECURITY:
1140 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
1141 case HCI_ERR_UNIT_KEY_USED:
1142 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
1143 case HCI_ERR_INSUFFCIENT_SECURITY:
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301144 case HCI_ERR_PEER_USER:
1145 case HCI_ERR_UNSPECIFIED:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001146 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d",
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301147 __FUNCTION__, p_auth_cmpl->fail_reason);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001148 if (pairing_cb.autopair_attempts == 1)
1149 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001150 BTIF_TRACE_DEBUG("%s(): Adding device to blacklist ", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001151
1152 /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class */
1153 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
1154 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
1155 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
1156 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
1157 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
1158 check_cod(&bd_addr, COD_HID_POINTING))
1159 {
1160 btif_storage_add_device_to_autopair_blacklist (&bd_addr);
1161 }
1162 pairing_cb.autopair_attempts++;
1163
1164 /* Create the Bond once again */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001165 BTIF_TRACE_DEBUG("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07001166 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001167 return;
1168 }
1169 else
1170 {
1171 /* if autopair attempts are more than 1, or not attempted */
1172 status = BT_STATUS_AUTH_FAILURE;
1173 }
1174 break;
1175
1176 default:
1177 status = BT_STATUS_FAIL;
1178 }
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301179 /* Special Handling for HID Devices */
1180 if (check_cod(&bd_addr, COD_HID_POINTING)) {
1181 /* Remove Device as bonded in nvram as authentication failed */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001182 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __FUNCTION__);
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301183 btif_storage_remove_bonded_device(&bd_addr);
1184 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001185 bond_state_changed(status, &bd_addr, state);
1186 }
1187}
1188
1189/******************************************************************************
1190**
1191** Function btif_dm_search_devices_evt
1192**
1193** Description Executes search devices callback events in btif context
1194**
1195** Returns void
1196**
1197******************************************************************************/
1198static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
1199{
1200 tBTA_DM_SEARCH *p_search_data;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001201 BTIF_TRACE_EVENT("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001202
1203 switch (event)
1204 {
1205 case BTA_DM_DISC_RES_EVT:
1206 {
1207 p_search_data = (tBTA_DM_SEARCH *)p_param;
1208 /* Remote name update */
1209 if (strlen((const char *) p_search_data->disc_res.bd_name))
1210 {
1211 bt_property_t properties[1];
1212 bt_bdaddr_t bdaddr;
1213 bt_status_t status;
1214
1215 properties[0].type = BT_PROPERTY_BDNAME;
1216 properties[0].val = p_search_data->disc_res.bd_name;
1217 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
1218 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
1219
1220 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
1221 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
1222 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1223 status, &bdaddr, 1, properties);
1224 }
1225 /* TODO: Services? */
1226 }
1227 break;
1228
1229 case BTA_DM_INQ_RES_EVT:
1230 {
1231 /* inquiry result */
1232 UINT32 cod;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001233 bt_bdname_t bdname;
1234 bt_bdaddr_t bdaddr;
1235 UINT8 remote_name_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001236 tBTA_SERVICE_MASK services = 0;
1237 bdstr_t bdstr;
1238
1239 p_search_data = (tBTA_DM_SEARCH *)p_param;
1240 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
1241
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07001242 BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __FUNCTION__, bdaddr_to_string(&bdaddr, bdstr, sizeof(bdstr)),
The Android Open Source Project5738f832012-12-12 16:00:35 -08001243#if (BLE_INCLUDED == TRUE)
1244 p_search_data->inq_res.device_type);
1245#else
1246 BT_DEVICE_TYPE_BREDR);
1247#endif
1248 bdname.name[0] = 0;
1249
1250 cod = devclass2uint (p_search_data->inq_res.dev_class);
1251
Chris Mantonf8027002015-03-12 09:22:48 -07001252 if (cod == 0) {
1253 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001254 cod = COD_UNCLASSIFIED;
1255 }
1256
1257 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1258 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1259
1260 /* Check EIR for remote name and services */
1261 if (p_search_data->inq_res.p_eir)
1262 {
1263 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001264 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001265 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */
1266 }
1267
1268
1269 {
1270 bt_property_t properties[5];
1271 bt_device_type_t dev_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001272 UINT8 addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001273 uint32_t num_properties = 0;
1274 bt_status_t status;
1275
1276 memset(properties, 0, sizeof(properties));
1277 /* BD_ADDR */
1278 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1279 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1280 num_properties++;
1281 /* BD_NAME */
1282 /* Don't send BDNAME if it is empty */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001283 if (bdname.name[0])
1284 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001285 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1286 BT_PROPERTY_BDNAME,
1287 strlen((char *)bdname.name), &bdname);
1288 num_properties++;
1289 }
1290
1291 /* DEV_CLASS */
1292 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1293 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1294 num_properties++;
1295 /* DEV_TYPE */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001296#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001297 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1298 dev_type = p_search_data->inq_res.device_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001299 addr_type = p_search_data->inq_res.ble_addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001300#else
1301 dev_type = BT_DEVICE_TYPE_BREDR;
1302#endif
1303 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1304 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1305 num_properties++;
1306 /* RSSI */
1307 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1308 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1309 &(p_search_data->inq_res.rssi));
1310 num_properties++;
1311
1312 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1313 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001314#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1315 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001316 if (( dev_type == BT_DEVICE_TYPE_DUMO)&&
1317 (p_search_data->inq_res.flag & BTA_BLE_DMT_CONTROLLER_SPT) &&
1318 (p_search_data->inq_res.flag & BTA_BLE_DMT_HOST_SPT))
1319 {
1320 btif_storage_set_dmt_support_type (&bdaddr, TRUE);
1321 }
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001322 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1323#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001324 /* Callback to notify upper layer of device */
1325 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1326 num_properties, properties);
1327 }
1328 }
1329 break;
1330
1331 case BTA_DM_INQ_CMPL_EVT:
1332 {
Satya Calloji6e2d9db2014-07-08 16:18:58 -07001333#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1334 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1335 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1336 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1337 bte_scan_filt_param_cfg_evt, 0);
1338#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001339 }
1340 break;
1341 case BTA_DM_DISC_CMPL_EVT:
1342 {
1343 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1344 }
1345 break;
1346 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1347 {
1348 /* if inquiry is not in progress and we get a cancel event, then
1349 * it means we are done with inquiry, but remote_name fetches are in
1350 * progress
1351 *
1352 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt
1353 * but instead wait for the cancel_cmpl_evt via the Busy Level
1354 *
1355 */
1356 if (btif_dm_inquiry_in_progress == FALSE)
1357 {
Nitin Arora7b85efa2014-09-26 14:05:24 -07001358#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1359 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1360 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1361 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1362 bte_scan_filt_param_cfg_evt, 0);
1363#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001364 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1365 }
1366 }
1367 break;
1368 }
1369}
1370
1371/*******************************************************************************
1372**
1373** Function btif_dm_search_services_evt
1374**
1375** Description Executes search services event in btif context
1376**
1377** Returns void
1378**
1379*******************************************************************************/
1380static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1381{
1382 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1383
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001384 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001385 switch (event)
1386 {
1387 case BTA_DM_DISC_RES_EVT:
1388 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001389 bt_property_t prop;
Bernhard Rosenkränzer104e3f22014-11-12 21:53:08 +01001390 uint32_t i = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001391 bt_bdaddr_t bd_addr;
1392 bt_status_t ret;
1393
1394 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1395
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001396 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001397 p_data->disc_res.result, p_data->disc_res.services);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001398 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1399 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1400 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1401 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001402 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001403 pairing_cb.sdp_attempts++;
1404 btif_dm_get_remote_services(&bd_addr);
1405 return;
1406 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001407 prop.type = BT_PROPERTY_UUIDS;
1408 prop.len = 0;
1409 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1410 {
1411 prop.val = p_data->disc_res.p_uuid_list;
1412 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1413 for (i=0; i < p_data->disc_res.num_uuids; i++)
1414 {
1415 char temp[256];
Chris Manton8ff3fea2015-01-07 13:59:14 -08001416 uuid_to_string_legacy((bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i*MAX_UUID_SIZE)), temp);
Chris Mantonf8027002015-03-12 09:22:48 -07001417 LOG_INFO("%s index:%d uuid:%s", __func__, i, temp);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001418 }
1419 }
1420
1421 /* onUuidChanged requires getBondedDevices to be populated.
1422 ** bond_state_changed needs to be sent prior to remote_device_property
1423 */
1424 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1425 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0)&&
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001426 pairing_cb.sdp_attempts > 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001427 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001428 BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001429 __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001430 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001431 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1432 }
1433
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001434 if(p_data->disc_res.num_uuids != 0)
1435 {
1436 /* Also write this to the NVRAM */
1437 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1438 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1439 /* Send the event to the BTIF */
1440 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1441 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1442 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001443 }
1444 break;
1445
1446 case BTA_DM_DISC_CMPL_EVT:
1447 /* fixme */
1448 break;
1449
Matthew Xie607e3b72013-08-15 19:30:48 -07001450#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001451 case BTA_DM_DISC_BLE_RES_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001452 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__,
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001453 p_data->disc_ble_res.service.uu.uuid16);
1454 bt_uuid_t uuid;
1455 int i = 0;
1456 int j = 15;
1457 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1458 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001459 BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001460 bt_property_t prop;
1461 bt_bdaddr_t bd_addr;
1462 char temp[256];
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001463 bt_status_t ret;
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001464
1465 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1466
1467 while(i < j )
1468 {
1469 unsigned char c = uuid.uu[j];
1470 uuid.uu[j] = uuid.uu[i];
1471 uuid.uu[i] = c;
1472 i++;
1473 j--;
1474 }
1475
Chris Manton8ff3fea2015-01-07 13:59:14 -08001476 uuid_to_string_legacy(&uuid, temp);
Chris Mantonf8027002015-03-12 09:22:48 -07001477 LOG_INFO("%s uuid:%s", __func__, temp);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001478
1479 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1480 prop.type = BT_PROPERTY_UUIDS;
1481 prop.val = uuid.uu;
1482 prop.len = MAX_UUID_SIZE;
1483
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001484 /* Also write this to the NVRAM */
1485 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1486 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1487
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001488 /* Send the event to the BTIF */
1489 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1490 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1491
1492 }
1493 break;
Matthew Xie607e3b72013-08-15 19:30:48 -07001494#endif /* BLE_INCLUDED */
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001495
The Android Open Source Project5738f832012-12-12 16:00:35 -08001496 default:
1497 {
1498 ASSERTC(0, "unhandled search services event", event);
1499 }
1500 break;
1501 }
1502}
1503
1504/*******************************************************************************
1505**
1506** Function btif_dm_remote_service_record_evt
1507**
1508** Description Executes search service record event in btif context
1509**
1510** Returns void
1511**
1512*******************************************************************************/
1513static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1514{
1515 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1516
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001517 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001518 switch (event)
1519 {
1520 case BTA_DM_DISC_RES_EVT:
1521 {
1522 bt_service_record_t rec;
1523 bt_property_t prop;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001524 bt_bdaddr_t bd_addr;
1525
1526 memset(&rec, 0, sizeof(bt_service_record_t));
1527 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1528
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001529 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001530 p_data->disc_res.result, p_data->disc_res.services);
1531 prop.type = BT_PROPERTY_SERVICE_RECORD;
1532 prop.val = (void*)&rec;
1533 prop.len = sizeof(rec);
1534
1535 /* disc_res.result is overloaded with SCN. Cannot check result */
1536 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1537 /* TODO: Get the UUID as well */
1538 rec.channel = p_data->disc_res.result - 3;
1539 /* TODO: Need to get the service name using p_raw_data */
1540 rec.name[0] = 0;
1541
1542 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1543 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1544 }
1545 break;
1546
1547 default:
1548 {
1549 ASSERTC(0, "unhandled remote service record event", event);
1550 }
1551 break;
1552 }
1553}
1554
1555/*******************************************************************************
1556**
1557** Function btif_dm_upstreams_cback
1558**
1559** Description Executes UPSTREAMS events in btif context
1560**
1561** Returns void
1562**
1563*******************************************************************************/
1564static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1565{
The Android Open Source Project5738f832012-12-12 16:00:35 -08001566 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1567 tBTA_SERVICE_MASK service_mask;
1568 uint32_t i;
1569 bt_bdaddr_t bd_addr;
1570
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001571 BTIF_TRACE_EVENT("btif_dm_upstreams_cback ev: %s", dump_dm_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001572
1573 switch (event)
1574 {
1575 case BTA_DM_ENABLE_EVT:
1576 {
1577 BD_NAME bdname;
1578 bt_status_t status;
1579 bt_property_t prop;
1580 prop.type = BT_PROPERTY_BDNAME;
1581 prop.len = BD_NAME_LEN;
1582 prop.val = (void*)bdname;
1583
1584 status = btif_storage_get_adapter_property(&prop);
Matthew Xie1e5109b2012-11-09 18:26:26 -08001585 if (status == BT_STATUS_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001586 {
1587 /* A name exists in the storage. Make this the device name */
1588 BTA_DmSetDeviceName((char*)prop.val);
1589 }
Matthew Xie1e5109b2012-11-09 18:26:26 -08001590 else
1591 {
1592 /* Storage does not have a name yet.
1593 * Use the default name and write it to the chip
1594 */
1595 BTA_DmSetDeviceName(btif_get_default_local_name());
1596 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001597
Andre Eisenbacha015a832014-09-11 14:09:40 -07001598#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1599 /* Enable local privacy */
Andre Eisenbach3e0dc732014-10-24 09:55:34 -07001600 BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED);
Andre Eisenbacha015a832014-09-11 14:09:40 -07001601#endif
1602
The Android Open Source Project5738f832012-12-12 16:00:35 -08001603 /* for each of the enabled services in the mask, trigger the profile
1604 * enable */
1605 service_mask = btif_get_enabled_services_mask();
1606 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1607 {
1608 if (service_mask &
1609 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1610 {
1611 btif_in_execute_service_request(i, TRUE);
1612 }
1613 }
1614 /* clear control blocks */
1615 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1616
1617 /* This function will also trigger the adapter_properties_cb
1618 ** and bonded_devices_info_cb
1619 */
1620 btif_storage_load_bonded_devices();
1621
1622 btif_storage_load_autopair_device_list();
1623
Zach Johnson39110ec2014-10-06 13:15:00 -07001624 btif_enable_bluetooth_evt(p_data->enable.status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001625 }
1626 break;
1627
1628 case BTA_DM_DISABLE_EVT:
1629 /* for each of the enabled services in the mask, trigger the profile
1630 * disable */
1631 service_mask = btif_get_enabled_services_mask();
1632 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1633 {
1634 if (service_mask &
1635 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1636 {
1637 btif_in_execute_service_request(i, FALSE);
1638 }
1639 }
1640 btif_disable_bluetooth_evt();
1641 break;
1642
1643 case BTA_DM_PIN_REQ_EVT:
1644 btif_dm_pin_req_evt(&p_data->pin_req);
1645 break;
1646
1647 case BTA_DM_AUTH_CMPL_EVT:
1648 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1649 break;
1650
1651 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1652 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1653 {
1654 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1655 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1656 }
1657 break;
1658
1659 case BTA_DM_SP_CFM_REQ_EVT:
1660 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1661 break;
1662 case BTA_DM_SP_KEY_NOTIF_EVT:
1663 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1664 break;
1665
1666 case BTA_DM_DEV_UNPAIRED_EVT:
1667 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1668
1669 /*special handling for HID devices */
1670 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -07001671 btif_hh_remove_device(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001672 #endif
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001673 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1674 btif_storage_remove_ble_bonding_keys(&bd_addr);
1675 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001676 btif_storage_remove_bonded_device(&bd_addr);
1677 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1678 break;
1679
1680 case BTA_DM_BUSY_LEVEL_EVT:
1681 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001682
1683 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001684 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001685 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001686 {
1687 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1688 BT_DISCOVERY_STARTED);
1689 btif_dm_inquiry_in_progress = TRUE;
1690 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001691 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001692 {
1693 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1694 BT_DISCOVERY_STOPPED);
1695 btif_dm_inquiry_in_progress = FALSE;
1696 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001697 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001698 {
1699 btif_dm_inquiry_in_progress = FALSE;
1700 }
1701 }
1702 }break;
1703
1704 case BTA_DM_LINK_UP_EVT:
1705 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001706 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001707
1708 btif_update_remote_version_property(&bd_addr);
1709
The Android Open Source Project5738f832012-12-12 16:00:35 -08001710 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1711 &bd_addr, BT_ACL_STATE_CONNECTED);
1712 break;
1713
1714 case BTA_DM_LINK_DOWN_EVT:
1715 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001716 BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001717 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1718 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1719 break;
1720
1721 case BTA_DM_HW_ERROR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001722 BTIF_TRACE_ERROR("Received H/W Error. ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001723 /* Flush storage data */
1724 btif_config_flush();
1725 usleep(100000); /* 100milliseconds */
1726 /* Killing the process to force a restart as part of fault tolerance */
1727 kill(getpid(), SIGKILL);
1728 break;
1729
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001730#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1731 case BTA_DM_BLE_KEY_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001732 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT key_type=0x%02x ", p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001733
1734 /* If this pairing is by-product of local initiated GATT client Read or Write,
1735 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1736 have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1737 if (pairing_cb.state != BT_BOND_STATE_BONDING)
1738 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001739 BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001740 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1741 BT_BOND_STATE_BONDING);
1742 }
1743 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1744 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001745 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001746 break;
1747 }
1748
1749 switch (p_data->ble_key.key_type)
1750 {
1751 case BTA_LE_KEY_PENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001752 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001753 pairing_cb.ble.is_penc_key_rcvd = TRUE;
1754 memcpy(pairing_cb.ble.penc_key.ltk,p_data->ble_key.key_value.penc_key.ltk, 16);
1755 memcpy(pairing_cb.ble.penc_key.rand, p_data->ble_key.key_value.penc_key.rand,8);
1756 pairing_cb.ble.penc_key.ediv = p_data->ble_key.key_value.penc_key.ediv;
1757 pairing_cb.ble.penc_key.sec_level = p_data->ble_key.key_value.penc_key.sec_level;
1758
1759 for (i=0; i<16; i++)
1760 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001761 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.ltk[%d]=0x%02x",i,pairing_cb.ble.penc_key.ltk[i]);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001762 }
1763 for (i=0; i<8; i++)
1764 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001765 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.rand[%d]=0x%02x",i,pairing_cb.ble.penc_key.rand[i]);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001766 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001767 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.ediv=0x%04x",pairing_cb.ble.penc_key.ediv);
1768 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.sec_level=0x%02x",pairing_cb.ble.penc_key.sec_level);
1769 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.key_size=0x%02x",pairing_cb.ble.penc_key.key_size);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001770 break;
1771
1772 case BTA_LE_KEY_PID:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001773 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001774 pairing_cb.ble.is_pid_key_rcvd = TRUE;
Andre Eisenbach5e808462014-10-21 12:37:53 -07001775 pairing_cb.ble.pid_key.addr_type = p_data->ble_key.key_value.pid_key.addr_type;
1776 memcpy(pairing_cb.ble.pid_key.irk, p_data->ble_key.key_value.pid_key.irk, 16);
1777 memcpy(pairing_cb.ble.pid_key.static_addr,
1778 p_data->ble_key.key_value.pid_key.static_addr,BD_ADDR_LEN);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001779 for (i=0; i<16; i++)
1780 {
Andre Eisenbach5e808462014-10-21 12:37:53 -07001781 BTIF_TRACE_DEBUG("pairing_cb.ble.pid_key.irk[%d]=0x%02x"
1782 ,i,pairing_cb.ble.pid_key.irk[i]);
1783 }
1784 for (i=0; i<BD_ADDR_LEN; i++)
1785 {
1786 BTIF_TRACE_DEBUG("piaring_cb.ble.pid_address[%d] = %x"
1787 ,i, pairing_cb.ble.pid_key.static_addr[i]);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001788 }
1789 break;
1790
1791 case BTA_LE_KEY_PCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001792 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001793 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1794 pairing_cb.ble.pcsrk_key.counter = p_data->ble_key.key_value.pcsrk_key.counter;
1795 pairing_cb.ble.pcsrk_key.sec_level = p_data->ble_key.key_value.pcsrk_key.sec_level;
1796 memcpy(pairing_cb.ble.pcsrk_key.csrk,p_data->ble_key.key_value.pcsrk_key.csrk,16);
1797
1798 for (i=0; i<16; i++)
1799 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001800 BTIF_TRACE_DEBUG("pairing_cb.ble.pcsrk_key.csrk[%d]=0x%02x",i,pairing_cb.ble.pcsrk_key.csrk[i]);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001801 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001802 BTIF_TRACE_DEBUG("pairing_cb.ble.pcsrk_key.counter=0x%08x",pairing_cb.ble.pcsrk_key.counter);
1803 BTIF_TRACE_DEBUG("pairing_cb.ble.pcsrk_key.sec_level=0x%02x",pairing_cb.ble.pcsrk_key.sec_level);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001804 break;
1805
1806 case BTA_LE_KEY_LENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001807 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001808 pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1809 pairing_cb.ble.lenc_key.div = p_data->ble_key.key_value.lenc_key.div;
1810 pairing_cb.ble.lenc_key.key_size = p_data->ble_key.key_value.lenc_key.key_size;
1811 pairing_cb.ble.lenc_key.sec_level = p_data->ble_key.key_value.lenc_key.sec_level;
1812
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001813 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.div=0x%04x",pairing_cb.ble.lenc_key.div);
1814 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.key_size=0x%02x",pairing_cb.ble.lenc_key.key_size);
1815 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.sec_level=0x%02x",pairing_cb.ble.lenc_key.sec_level);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001816 break;
1817
1818
1819
1820 case BTA_LE_KEY_LCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001821 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001822 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1823 pairing_cb.ble.lcsrk_key.counter = p_data->ble_key.key_value.lcsrk_key.counter;
1824 pairing_cb.ble.lcsrk_key.div = p_data->ble_key.key_value.lcsrk_key.div;
1825 pairing_cb.ble.lcsrk_key.sec_level = p_data->ble_key.key_value.lcsrk_key.sec_level;
1826
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001827 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.div=0x%04x",pairing_cb.ble.lcsrk_key.div);
1828 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.counter=0x%08x",pairing_cb.ble.lcsrk_key.counter);
1829 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.sec_level=0x%02x",pairing_cb.ble.lcsrk_key.sec_level);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001830
1831 break;
1832
1833 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001834 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001835 break;
1836 }
1837
1838 break;
1839 case BTA_DM_BLE_SEC_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001840 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001841 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1842 break;
1843 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001844 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001845 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1846 break;
1847 case BTA_DM_BLE_PASSKEY_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001848 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001849 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1850 break;
1851 case BTA_DM_BLE_OOB_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001852 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001853 break;
1854 case BTA_DM_BLE_LOCAL_IR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001855 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001856 ble_local_key_cb.is_id_keys_rcvd = TRUE;
1857 memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1858 memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1859 memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1860 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1861 BTIF_DM_LE_LOCAL_KEY_IR,
1862 BT_OCTET16_LEN);
1863 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1864 BTIF_DM_LE_LOCAL_KEY_IRK,
1865 BT_OCTET16_LEN);
1866 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1867 BTIF_DM_LE_LOCAL_KEY_DHK,
1868 BT_OCTET16_LEN);
1869 break;
1870 case BTA_DM_BLE_LOCAL_ER_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001871 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001872 ble_local_key_cb.is_er_rcvd = TRUE;
1873 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1874 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1875 BTIF_DM_LE_LOCAL_KEY_ER,
1876 BT_OCTET16_LEN);
1877 break;
1878
1879 case BTA_DM_BLE_AUTH_CMPL_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001880 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001881 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1882 break;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001883
1884 case BTA_DM_LE_FEATURES_READ:
1885 {
1886 tBTM_BLE_VSC_CB cmn_vsc_cb;
1887 bt_local_le_features_t local_le_features;
1888 char buf[512];
1889 bt_property_t prop;
1890 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1891 prop.val = (void*)buf;
1892 prop.len = sizeof(buf);
1893
1894 /* LE features are not stored in storage. Should be retrived from stack */
1895 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1896 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1897
1898 prop.len = sizeof (bt_local_le_features_t);
1899 if (cmn_vsc_cb.filter_support == 1)
1900 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1901 else
1902 local_le_features.max_adv_filter_supported = 0;
1903 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1904 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1905 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
Satya Callojid773c2c2014-07-29 22:08:55 -07001906 local_le_features.scan_result_storage_size_hibyte =
1907 (cmn_vsc_cb.tot_scan_results_strg >> 8) & (0xFF);
1908 local_le_features.scan_result_storage_size_lobyte =
1909 (cmn_vsc_cb.tot_scan_results_strg) & (0xFF);
Satya Callojiefaddcb2014-07-28 23:22:05 -07001910 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001911 memcpy(prop.val, &local_le_features, prop.len);
1912 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
1913 break;
1914 }
Satya Callojie5ba8842014-07-03 17:18:02 -07001915
1916 case BTA_DM_ENER_INFO_READ:
1917 {
1918 btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param;
1919 bt_activity_energy_info energy_info;
1920 energy_info.status = p_ener_data->status;
1921 energy_info.ctrl_state = p_ener_data->ctrl_state;
1922 energy_info.rx_time = p_ener_data->rx_time;
1923 energy_info.tx_time = p_ener_data->tx_time;
1924 energy_info.idle_time = p_ener_data->idle_time;
1925 energy_info.energy_used = p_ener_data->energy_used;
1926 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info);
1927 break;
1928 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001929#endif
1930
The Android Open Source Project5738f832012-12-12 16:00:35 -08001931 case BTA_DM_AUTHORIZE_EVT:
1932 case BTA_DM_SIG_STRENGTH_EVT:
1933 case BTA_DM_SP_RMT_OOB_EVT:
1934 case BTA_DM_SP_KEYPRESS_EVT:
1935 case BTA_DM_ROLE_CHG_EVT:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001936
The Android Open Source Project5738f832012-12-12 16:00:35 -08001937 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001938 BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001939 break;
1940 }
1941} /* btui_security_cback() */
1942
1943
1944/*******************************************************************************
1945**
1946** Function btif_dm_generic_evt
1947**
1948** Description Executes non-BTA upstream events in BTIF context
1949**
1950** Returns void
1951**
1952*******************************************************************************/
1953static void btif_dm_generic_evt(UINT16 event, char* p_param)
1954{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001955 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001956 switch(event)
1957 {
1958 case BTIF_DM_CB_DISCOVERY_STARTED:
1959 {
1960 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
1961 }
1962 break;
1963
1964 case BTIF_DM_CB_CREATE_BOND:
1965 {
Andre Eisenbach31a64002014-10-14 14:29:19 -07001966 pairing_cb.timeout_retries = NUM_TIMEOUT_RETRIES;
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07001967 btif_dm_create_bond_cb_t *create_bond_cb = (btif_dm_create_bond_cb_t*)p_param;
1968 btif_dm_cb_create_bond(&create_bond_cb->bdaddr, create_bond_cb->transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001969 }
1970 break;
1971
1972 case BTIF_DM_CB_REMOVE_BOND:
1973 {
1974 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
1975 }
1976 break;
1977
1978 case BTIF_DM_CB_HID_REMOTE_NAME:
1979 {
1980 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
1981 }
1982 break;
1983
1984 case BTIF_DM_CB_BOND_STATE_BONDING:
1985 {
1986 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
1987 }
1988 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001989 case BTIF_DM_CB_LE_TX_TEST:
1990 case BTIF_DM_CB_LE_RX_TEST:
1991 {
1992 uint8_t status;
1993 STREAM_TO_UINT8(status, p_param);
1994 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1995 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
1996 }
1997 break;
1998 case BTIF_DM_CB_LE_TEST_END:
1999 {
2000 uint8_t status;
2001 uint16_t count = 0;
2002 STREAM_TO_UINT8(status, p_param);
2003 if (status == 0)
2004 STREAM_TO_UINT16(count, p_param);
2005 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2006 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
2007 }
2008 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002009 default:
2010 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002011 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002012 }
2013 break;
2014 }
2015}
2016
2017/*******************************************************************************
2018**
2019** Function bte_dm_evt
2020**
2021** Description Switches context from BTE to BTIF for all DM events
2022**
2023** Returns void
2024**
2025*******************************************************************************/
2026
2027void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
2028{
2029 bt_status_t status;
2030
2031 /* switch context to btif task context (copy full union size for convenience) */
2032 status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event, (void*)p_data, sizeof(tBTA_DM_SEC), NULL);
2033
2034 /* catch any failed context transfers */
2035 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
2036}
2037
2038/*******************************************************************************
2039**
2040** Function bte_search_devices_evt
2041**
2042** Description Switches context from BTE to BTIF for DM search events
2043**
2044** Returns void
2045**
2046*******************************************************************************/
2047static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2048{
2049 UINT16 param_len = 0;
2050
2051 if (p_data)
2052 param_len += sizeof(tBTA_DM_SEARCH);
2053 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
2054 switch (event)
2055 {
2056 case BTA_DM_INQ_RES_EVT:
2057 {
2058 if (p_data->inq_res.p_eir)
2059 param_len += HCI_EXT_INQ_RESPONSE_LEN;
2060 }
2061 break;
2062
2063 case BTA_DM_DISC_RES_EVT:
2064 {
2065 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
2066 param_len += p_data->disc_res.raw_data_size;
2067 }
2068 break;
2069 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002070 BTIF_TRACE_DEBUG("%s event=%s param_len=%d", __FUNCTION__, dump_dm_search_event(event), param_len);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002071
2072 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
2073 if (event == BTA_DM_INQ_RES_EVT)
2074 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
2075
2076 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
2077 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
2078}
2079
2080/*******************************************************************************
2081**
2082** Function bte_dm_search_services_evt
2083**
2084** Description Switches context from BTE to BTIF for DM search services
2085** event
2086**
2087** Returns void
2088**
2089*******************************************************************************/
2090static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2091{
2092 UINT16 param_len = 0;
2093 if (p_data)
2094 param_len += sizeof(tBTA_DM_SEARCH);
2095 switch (event)
2096 {
2097 case BTA_DM_DISC_RES_EVT:
2098 {
2099 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
2100 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
2101 }
2102 } break;
2103 }
2104 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
2105 * if raw_data is needed. */
2106 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
2107 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
2108}
2109
2110/*******************************************************************************
2111**
2112** Function bte_dm_remote_service_record_evt
2113**
2114** Description Switches context from BTE to BTIF for DM search service
2115** record event
2116**
2117** Returns void
2118**
2119*******************************************************************************/
2120static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2121{
2122 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
2123 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
2124}
2125
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002126#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07002127/*******************************************************************************
2128**
2129** Function bta_energy_info_cb
2130**
2131** Description Switches context from BTE to BTIF for DM energy info event
2132**
2133** Returns void
2134**
2135*******************************************************************************/
2136static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time,
2137 tBTA_DM_BLE_IDLE_TIME_MS idle_time,
2138 tBTA_DM_BLE_ENERGY_USED energy_used,
2139 tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status)
2140{
2141 BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld",
2142 status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
2143
2144 btif_activity_energy_info_cb_t btif_cb;
2145 btif_cb.status = status;
2146 btif_cb.ctrl_state = ctrl_state;
2147 btif_cb.tx_time = (uint64_t) tx_time;
2148 btif_cb.rx_time = (uint64_t) rx_time;
2149 btif_cb.idle_time =(uint64_t) idle_time;
2150 btif_cb.energy_used =(uint64_t) energy_used;
2151 btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
2152 (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL);
2153}
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002154#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07002155
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002156/*******************************************************************************
2157**
2158** Function bte_scan_filt_param_cfg_evt
2159**
2160** Description Scan filter param config event
2161**
2162** Returns void
2163**
2164*******************************************************************************/
2165static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
2166 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
2167 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
2168{
2169 /* This event occurs on calling BTA_DmBleCfgFilterCondition internally,
2170 ** and that is why there is no HAL callback
2171 */
2172 if(BTA_SUCCESS != status)
2173 {
2174 BTIF_TRACE_ERROR("%s, %d", __FUNCTION__, status);
2175 }
2176 else
2177 {
2178 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2179 }
2180}
2181
The Android Open Source Project5738f832012-12-12 16:00:35 -08002182/*****************************************************************************
2183**
2184** btif api functions (no context switch)
2185**
2186*****************************************************************************/
2187
2188/*******************************************************************************
2189**
2190** Function btif_dm_start_discovery
2191**
2192** Description Start device discovery/inquiry
2193**
2194** Returns bt_status_t
2195**
2196*******************************************************************************/
2197bt_status_t btif_dm_start_discovery(void)
2198{
2199 tBTA_DM_INQ inq_params;
2200 tBTA_SERVICE_MASK services = 0;
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002201 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002202
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002203 BTIF_TRACE_EVENT("%s", __FUNCTION__);
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002204
2205#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2206 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
2207 /* Cleanup anything remaining on index 0 */
2208 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
2209 bte_scan_filt_param_cfg_evt, 0);
2210
2211 /* Add an allow-all filter on index 0*/
2212 adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE;
2213 adv_filt_param.feat_seln = ALLOW_ALL_FILTER;
2214 adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;
2215 adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;
2216 adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE;
2217 adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE;
2218 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL,
2219 bte_scan_filt_param_cfg_evt, 0);
2220
The Android Open Source Project5738f832012-12-12 16:00:35 -08002221 /* TODO: Do we need to handle multiple inquiries at the same time? */
2222
2223 /* Set inquiry params and call API */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002224 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
Matthew Xie7f3e4292013-09-30 12:44:10 -07002225#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
2226 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
2227 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
2228 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
2229 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
2230#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002231#else
2232 inq_params.mode = BTA_DM_GENERAL_INQUIRY;
2233#endif
2234 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
2235
2236 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
2237 inq_params.report_dup = TRUE;
2238
2239 inq_params.filter_type = BTA_DM_INQ_CLR;
2240 /* TODO: Filter device by BDA needs to be implemented here */
2241
2242 /* Will be enabled to TRUE once inquiry busy level has been received */
2243 btif_dm_inquiry_in_progress = FALSE;
2244 /* find nearby devices */
2245 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
2246
2247 return BT_STATUS_SUCCESS;
2248}
2249
2250/*******************************************************************************
2251**
2252** Function btif_dm_cancel_discovery
2253**
2254** Description Cancels search
2255**
2256** Returns bt_status_t
2257**
2258*******************************************************************************/
2259bt_status_t btif_dm_cancel_discovery(void)
2260{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002261 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002262 BTA_DmSearchCancel();
2263 return BT_STATUS_SUCCESS;
2264}
2265
2266/*******************************************************************************
2267**
2268** Function btif_dm_create_bond
2269**
2270** Description Initiate bonding with the specified device
2271**
2272** Returns bt_status_t
2273**
2274*******************************************************************************/
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002275bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr, int transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002276{
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002277 btif_dm_create_bond_cb_t create_bond_cb;
2278 create_bond_cb.transport = transport;
2279 bdcpy(create_bond_cb.bdaddr.address, bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002280
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002281 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002282 BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)), transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002283 if (pairing_cb.state != BT_BOND_STATE_NONE)
2284 return BT_STATUS_BUSY;
2285
2286 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002287 (char *)&create_bond_cb, sizeof(btif_dm_create_bond_cb_t), NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002288
2289 return BT_STATUS_SUCCESS;
2290}
2291
2292/*******************************************************************************
2293**
2294** Function btif_dm_cancel_bond
2295**
2296** Description Initiate bonding with the specified device
2297**
2298** Returns bt_status_t
2299**
2300*******************************************************************************/
2301
2302bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
2303{
2304 bdstr_t bdstr;
2305
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002306 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002307
2308 /* TODO:
2309 ** 1. Restore scan modes
2310 ** 2. special handling for HID devices
2311 */
2312 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2313 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002314
2315#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2316
2317 if (pairing_cb.is_ssp)
2318 {
2319 if (pairing_cb.is_le_only)
2320 {
2321 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2322 }
2323 else
Hemant Guptae1468692013-11-14 16:21:29 +05302324 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002325 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
Hemant Guptae1468692013-11-14 16:21:29 +05302326 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2327 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2328 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002329 }
2330 else
2331 {
2332 if (pairing_cb.is_le_only)
2333 {
2334 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2335 }
2336 else
2337 {
2338 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2339 }
2340 /* Cancel bonding, in case it is in ACL connection setup state */
2341 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2342 }
2343
2344#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002345 if (pairing_cb.is_ssp)
2346 {
2347 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2348 }
2349 else
2350 {
2351 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2352 }
2353 /* Cancel bonding, in case it is in ACL connection setup state */
2354 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2355 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002356#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002357 }
2358
2359 return BT_STATUS_SUCCESS;
2360}
2361
2362/*******************************************************************************
2363**
Kim Schulza9eb25c2013-09-30 10:55:52 +02002364** Function btif_dm_hh_open_failed
2365**
2366** Description informs the upper layers if the HH have failed during bonding
2367**
2368** Returns none
2369**
2370*******************************************************************************/
2371
2372void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
2373{
2374 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2375 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2376 {
2377 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2378 }
2379}
2380
2381/*******************************************************************************
2382**
The Android Open Source Project5738f832012-12-12 16:00:35 -08002383** Function btif_dm_remove_bond
2384**
2385** Description Removes bonding with the specified device
2386**
2387** Returns bt_status_t
2388**
2389*******************************************************************************/
2390
2391bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
2392{
2393 bdstr_t bdstr;
2394
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002395 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002396 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2397 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2398
2399 return BT_STATUS_SUCCESS;
2400}
2401
2402/*******************************************************************************
2403**
2404** Function btif_dm_pin_reply
2405**
2406** Description BT legacy pairing - PIN code reply
2407**
2408** Returns bt_status_t
2409**
2410*******************************************************************************/
2411
2412bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2413 uint8_t pin_len, bt_pin_code_t *pin_code)
2414{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002415 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Hemant Gupta831423e2014-01-08 12:42:13 +05302416 if (pin_code == NULL)
2417 return BT_STATUS_FAIL;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002418#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002419
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002420 if (pairing_cb.is_le_only)
2421 {
2422 int i;
2423 UINT32 passkey = 0;
2424 int multi[] = {100000, 10000, 1000, 100, 10,1};
2425 BD_ADDR remote_bd_addr;
2426 bdcpy(remote_bd_addr, bd_addr->address);
2427 for (i = 0; i < 6; i++)
2428 {
2429 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2430 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002431 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002432 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2433
2434 }
2435 else
2436 {
2437 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2438 if (accept)
2439 pairing_cb.pin_code_len = pin_len;
2440 }
2441#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002442 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2443
2444 if (accept)
2445 pairing_cb.pin_code_len = pin_len;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002446#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002447 return BT_STATUS_SUCCESS;
2448}
2449
2450/*******************************************************************************
2451**
2452** Function btif_dm_ssp_reply
2453**
2454** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2455**
2456** Returns bt_status_t
2457**
2458*******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002459bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2460 bt_ssp_variant_t variant, uint8_t accept,
2461 uint32_t passkey)
2462{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08002463 UNUSED(passkey);
2464
The Android Open Source Project5738f832012-12-12 16:00:35 -08002465 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2466 {
2467 /* This is not implemented in the stack.
2468 * For devices with display, this is not needed
2469 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002470 BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002471 return BT_STATUS_FAIL;
2472 }
2473 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002474 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002475#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2476 if (pairing_cb.is_le_only)
2477 {
2478 if (accept)
2479 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2480 else
2481 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2482 }
2483 else
2484 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002485
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002486#else
2487 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2488#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002489 return BT_STATUS_SUCCESS;
2490}
2491
2492/*******************************************************************************
2493**
2494** Function btif_dm_get_adapter_property
2495**
2496** Description Queries the BTA for the adapter property
2497**
2498** Returns bt_status_t
2499**
2500*******************************************************************************/
2501bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2502{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002503 BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002504 switch (prop->type)
2505 {
2506 case BT_PROPERTY_BDNAME:
2507 {
2508 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
VenkatRaghavan VijayaRaghavan1d8e6b82015-02-05 22:20:39 -08002509 strncpy((char *)bd_name->name,btif_get_default_local_name(),
2510 sizeof(bd_name->name) - 1);
2511 bd_name->name[sizeof(bd_name->name) - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002512 prop->len = strlen((char *)bd_name->name);
2513 }
2514 break;
2515
2516 case BT_PROPERTY_ADAPTER_SCAN_MODE:
2517 {
2518 /* if the storage does not have it. Most likely app never set it. Default is NONE */
2519 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2520 *mode = BT_SCAN_MODE_NONE;
2521 prop->len = sizeof(bt_scan_mode_t);
2522 }
2523 break;
2524
2525 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2526 {
2527 uint32_t *tmt = (uint32_t*)prop->val;
2528 *tmt = 120; /* default to 120s, if not found in NV */
2529 prop->len = sizeof(uint32_t);
2530 }
2531 break;
2532
2533 default:
2534 prop->len = 0;
2535 return BT_STATUS_FAIL;
2536 }
2537 return BT_STATUS_SUCCESS;
2538}
2539
2540/*******************************************************************************
2541**
2542** Function btif_dm_get_remote_services
2543**
2544** Description Start SDP to get remote services
2545**
2546** Returns bt_status_t
2547**
2548*******************************************************************************/
2549bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2550{
2551 bdstr_t bdstr;
2552
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002553 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr)));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002554
2555 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2556 bte_dm_search_services_evt, TRUE);
2557
2558 return BT_STATUS_SUCCESS;
2559}
2560
2561/*******************************************************************************
2562**
2563** Function btif_dm_get_remote_service_record
2564**
2565** Description Start SDP to get remote service record
2566**
2567**
2568** Returns bt_status_t
2569*******************************************************************************/
2570bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2571 bt_uuid_t *uuid)
2572{
2573 tSDP_UUID sdp_uuid;
2574 bdstr_t bdstr;
2575
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002576 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr)));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002577
2578 sdp_uuid.len = MAX_UUID_SIZE;
2579 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2580
2581 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2582 bte_dm_remote_service_record_evt, TRUE);
2583
2584 return BT_STATUS_SUCCESS;
2585}
2586
2587void btif_dm_execute_service_request(UINT16 event, char *p_param)
2588{
2589 BOOLEAN b_enable = FALSE;
2590 bt_status_t status;
2591 if (event == BTIF_DM_ENABLE_SERVICE)
2592 {
2593 b_enable = TRUE;
2594 }
2595 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2596 if (status == BT_STATUS_SUCCESS)
2597 {
2598 bt_property_t property;
2599 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2600
2601 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2602 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2603 sizeof(local_uuids), local_uuids);
2604 btif_storage_get_adapter_property(&property);
2605 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2606 BT_STATUS_SUCCESS, 1, &property);
2607 }
2608 return;
2609}
2610
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002611void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2612 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2613{
2614 UINT8 yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2615 /* if local initiated:
2616 ** 1. set DD + MITM
2617 ** if remote initiated:
2618 ** 1. Copy over the auth_req from peer's io_rsp
2619 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2620 ** as a fallback set MITM+GB if peer had MITM set
2621 */
2622 UNUSED (bd_addr);
2623 UNUSED (p_io_cap);
2624 UNUSED (p_oob_data);
2625
2626
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002627 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002628 if(pairing_cb.is_local_initiated)
2629 {
2630 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2631 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2632 }
2633 else if (!is_orig)
2634 {
2635 /* peer initiated paring. They probably know what they want.
2636 ** Copy the mitm from peer device.
2637 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002638 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002639 __FUNCTION__, pairing_cb.auth_req);
2640 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2641
2642 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2643 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2644 *p_auth_req |= BTA_AUTH_SP_YES;
2645 }
2646 else if (yes_no_bit)
2647 {
2648 /* set the general bonding bit for stored device */
2649 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2650 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002651 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002652}
2653
2654void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2655 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2656{
2657 UNUSED (bd_addr);
2658 UNUSED (oob_data);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002659
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002660 if(auth_req & BTA_AUTH_BONDS)
2661 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002662 BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002663 pairing_cb.auth_req = auth_req;
2664 pairing_cb.io_cap = io_cap;
2665 }
2666}
2667
The Android Open Source Project5738f832012-12-12 16:00:35 -08002668#if (BTM_OOB_INCLUDED == TRUE)
2669void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data)
2670{
2671 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2672 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2673 {
2674 *p_oob_data = FALSE;
2675 }
2676 else
2677 {
2678 *p_oob_data = TRUE;
2679 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002680 BTIF_TRACE_DEBUG("btif_dm_set_oob_for_io_req *p_oob_data=%d", *p_oob_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002681}
2682#endif /* BTM_OOB_INCLUDED */
2683
2684#ifdef BTIF_DM_OOB_TEST
2685void btif_dm_load_local_oob(void)
2686{
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002687 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002688 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002689 BTIF_TRACE_DEBUG("btif_dm_load_local_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002690 if (prop_oob[0] != '3')
2691 {
2692#if (BTM_OOB_INCLUDED == TRUE)
2693 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2694 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2695 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002696 BTIF_TRACE_DEBUG("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002697 BTA_DmLocalOob();
2698 }
2699#else
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002700 BTIF_TRACE_ERROR("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002701#endif
2702 }
2703}
2704
2705void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2706{
2707 FILE *fp;
2708 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2709 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2710 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002711 char prop_oob[PROPERTY_VALUE_MAX];
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002712 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: valid=%d", valid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002713 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2714 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2715 valid)
2716 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002717 BTIF_TRACE_DEBUG("save local OOB data in memory");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002718 memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2719 memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2720 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002721 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002722 if (prop_oob[0] == '1')
2723 path = path_a;
2724 else if (prop_oob[0] == '2')
2725 path = path_b;
2726 if (path)
2727 {
2728 fp = fopen(path, "wb+");
2729 if (fp == NULL)
2730 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002731 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: failed to save local OOB data to %s", path);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002732 }
2733 else
2734 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002735 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: save local OOB data into file %s",path);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002736 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2737 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2738 fclose(fp);
2739 }
2740 }
2741 }
2742}
2743BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2744{
2745 char t[128];
2746 FILE *fp;
2747 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2748 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2749 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002750 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002751 BOOLEAN result = FALSE;
2752 bt_bdaddr_t bt_bd_addr;
2753 bdcpy(oob_cb.oob_bdaddr, bd_addr);
2754 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002755 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002756 if (prop_oob[0] == '1')
2757 path = path_b;
2758 else if (prop_oob[0] == '2')
2759 path = path_a;
2760 if (path)
2761 {
2762 fp = fopen(path, "rb");
2763 if (fp == NULL)
2764 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002765 BTIF_TRACE_DEBUG("btapp_dm_rmt_oob_reply: failed to read OOB keys from %s",path);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002766 return FALSE;
2767 }
2768 else
2769 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002770 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob: read OOB data from %s",path);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002771 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2772 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2773 fclose(fp);
2774 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002775 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: TRUE");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002776 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2777 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2778 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002779 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002780 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2781 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
2782 p_c[8], p_c[9], p_c[10], p_c[11], p_c[12], p_c[13], p_c[14], p_c[15]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002783 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: c = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002784 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2785 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
2786 p_r[8], p_r[9], p_r[10], p_r[11], p_r[12], p_r[13], p_r[14], p_r[15]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002787 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: r = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002788 bdcpy(bt_bd_addr.address, bd_addr);
2789 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2790 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2791 result = TRUE;
2792 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002793 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob result=%d",result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002794 return result;
2795}
2796#endif /* BTIF_DM_OOB_TEST */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002797#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2798
2799static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2800{
2801 bt_bdaddr_t bd_addr;
2802 bt_bdname_t bd_name;
2803 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08002804 int dev_type;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002805
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002806 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002807
2808 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08002809 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
2810 {
2811 dev_type = BT_DEVICE_TYPE_BLE;
2812 }
2813 btif_dm_update_ble_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2814 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002815 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2816 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2817
2818 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2819 pairing_cb.is_ssp = FALSE;
2820 cod = COD_UNCLASSIFIED;
2821
2822 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2823 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2824 p_ssp_key_notif->passkey);
2825}
2826
2827/*******************************************************************************
2828**
2829** Function btif_dm_ble_auth_cmpl_evt
2830**
2831** Description Executes authentication complete event in btif context
2832**
2833** Returns void
2834**
2835*******************************************************************************/
2836static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2837{
2838 /* Save link key, if not temporary */
2839 bt_bdaddr_t bd_addr;
2840 bt_status_t status = BT_STATUS_FAIL;
2841 bt_bond_state_t state = BT_BOND_STATE_NONE;
2842
2843 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2844 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2845 {
2846 /* store keys */
2847 }
2848 if (p_auth_cmpl->success)
2849 {
2850 status = BT_STATUS_SUCCESS;
2851 state = BT_BOND_STATE_BONDED;
2852
2853 btif_dm_save_ble_bonding_keys();
2854 BTA_GATTC_Refresh(bd_addr.address);
2855 btif_dm_get_remote_services(&bd_addr);
2856 }
2857 else
2858 {
2859 /*Map the HCI fail reason to bt status */
2860 switch (p_auth_cmpl->fail_reason)
2861 {
Priti Aghera156c52b2014-07-09 14:58:19 -07002862 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
2863 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
2864 btif_dm_remove_ble_bonding_keys();
2865 status = BT_STATUS_AUTH_FAILURE;
2866 break;
2867 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
2868 status = BT_STATUS_AUTH_REJECTED;
2869 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002870 default:
Andre Eisenbachca22ac42013-02-13 17:02:11 +09002871 btif_dm_remove_ble_bonding_keys();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002872 status = BT_STATUS_FAIL;
2873 break;
2874 }
2875 }
2876 bond_state_changed(status, &bd_addr, state);
2877}
2878
2879
2880
2881void btif_dm_load_ble_local_keys(void)
2882{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002883 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2884
2885 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2886 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2887 {
2888 ble_local_key_cb.is_er_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002889 BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002890 }
2891
2892 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
2893 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
2894 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
2895 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
2896 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
2897 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
2898 {
2899 ble_local_key_cb.is_id_keys_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002900 BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002901 }
2902
2903}
2904void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
2905 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
2906{
2907 if (ble_local_key_cb.is_er_rcvd )
2908 {
2909 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2910 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2911 }
2912
2913 if (ble_local_key_cb.is_id_keys_rcvd)
2914 {
2915 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
2916 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
2917 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
2918 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
2919 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002920 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002921}
2922
2923void btif_dm_save_ble_bonding_keys(void)
2924{
2925
2926 bt_bdaddr_t bd_addr;
2927
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002928 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002929
2930 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2931
2932 if (pairing_cb.ble.is_penc_key_rcvd)
2933 {
2934 btif_storage_add_ble_bonding_key(&bd_addr,
2935 (char *) &pairing_cb.ble.penc_key,
2936 BTIF_DM_LE_KEY_PENC,
2937 sizeof(btif_dm_ble_penc_keys_t));
2938 }
2939
2940 if (pairing_cb.ble.is_pid_key_rcvd)
2941 {
2942 btif_storage_add_ble_bonding_key(&bd_addr,
Andre Eisenbach5e808462014-10-21 12:37:53 -07002943 (char *) &pairing_cb.ble.pid_key,
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002944 BTIF_DM_LE_KEY_PID,
Andre Eisenbach5e808462014-10-21 12:37:53 -07002945 sizeof(btif_dm_ble_pid_keys_t));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002946 }
2947
2948
2949 if (pairing_cb.ble.is_pcsrk_key_rcvd)
2950 {
2951 btif_storage_add_ble_bonding_key(&bd_addr,
2952 (char *) &pairing_cb.ble.pcsrk_key,
2953 BTIF_DM_LE_KEY_PCSRK,
2954 sizeof(btif_dm_ble_pcsrk_keys_t));
2955 }
2956
2957
2958 if (pairing_cb.ble.is_lenc_key_rcvd)
2959 {
2960 btif_storage_add_ble_bonding_key(&bd_addr,
2961 (char *) &pairing_cb.ble.lenc_key,
2962 BTIF_DM_LE_KEY_LENC,
2963 sizeof(btif_dm_ble_lenc_keys_t));
2964 }
2965
2966 if (pairing_cb.ble.is_lcsrk_key_rcvd)
2967 {
2968 btif_storage_add_ble_bonding_key(&bd_addr,
2969 (char *) &pairing_cb.ble.lcsrk_key,
2970 BTIF_DM_LE_KEY_LCSRK,
2971 sizeof(btif_dm_ble_lcsrk_keys_t));
2972 }
2973
2974}
2975
2976
2977void btif_dm_remove_ble_bonding_keys(void)
2978{
2979 bt_bdaddr_t bd_addr;
2980
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002981 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002982
2983 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2984 btif_storage_remove_ble_bonding_keys(&bd_addr);
2985}
2986
2987
2988/*******************************************************************************
2989**
2990** Function btif_dm_ble_sec_req_evt
2991**
2992** Description Eprocess security request event in btif context
2993**
2994** Returns void
2995**
2996*******************************************************************************/
2997void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
2998{
2999 bt_bdaddr_t bd_addr;
3000 bt_bdname_t bd_name;
3001 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08003002 int dev_type;
3003
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003004 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003005
3006 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3007 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003008 BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003009 return;
3010 }
3011
3012 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08003013 if (!btif_get_device_type(p_ble_req->bd_addr, &dev_type))
3014 {
3015 dev_type = BT_DEVICE_TYPE_BLE;
3016 }
3017 btif_dm_update_ble_remote_properties(p_ble_req->bd_addr, p_ble_req->bd_name,
3018 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003019
3020 bdcpy(bd_addr.address, p_ble_req->bd_addr);
3021 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
3022
3023 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3024
Andre Eisenbach89363762015-01-26 13:49:36 -08003025 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003026 pairing_cb.is_le_only = TRUE;
3027 pairing_cb.is_ssp = TRUE;
3028
3029 cod = COD_UNCLASSIFIED;
3030
3031 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
3032 BT_SSP_VARIANT_CONSENT, 0);
3033}
3034
3035
3036
3037/*******************************************************************************
3038**
3039** Function btif_dm_ble_passkey_req_evt
3040**
3041** Description Executes pin request event in btif context
3042**
3043** Returns void
3044**
3045*******************************************************************************/
3046static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
3047{
3048 bt_bdaddr_t bd_addr;
3049 bt_bdname_t bd_name;
3050 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08003051 int dev_type;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003052
3053 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08003054 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
3055 {
3056 dev_type = BT_DEVICE_TYPE_BLE;
3057 }
3058 btif_dm_update_ble_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,
3059 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003060
3061 bdcpy(bd_addr.address, p_pin_req->bd_addr);
3062 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
3063
3064 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3065 pairing_cb.is_le_only = TRUE;
3066
3067 cod = COD_UNCLASSIFIED;
3068
3069 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
3070 &bd_addr, &bd_name, cod);
3071}
3072
3073
3074void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
3075 tBT_DEVICE_TYPE dev_type)
3076{
3077 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
3078}
3079
3080static void btif_dm_ble_tx_test_cback(void *p)
3081{
3082 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
3083 (char *)p, 1, NULL);
3084}
3085
3086static void btif_dm_ble_rx_test_cback(void *p)
3087{
3088 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
3089 (char *)p, 1, NULL);
3090}
3091
3092static void btif_dm_ble_test_end_cback(void *p)
3093{
3094 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
3095 (char *)p, 3, NULL);
3096}
3097/*******************************************************************************
3098**
3099** Function btif_le_test_mode
3100**
3101** Description Sends a HCI BLE Test command to the Controller
3102**
3103** Returns BT_STATUS_SUCCESS on success
3104**
3105*******************************************************************************/
3106bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
3107{
3108 switch (opcode) {
3109 case HCI_BLE_TRANSMITTER_TEST:
3110 if (len != 3) return BT_STATUS_PARM_INVALID;
3111 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
3112 break;
3113 case HCI_BLE_RECEIVER_TEST:
3114 if (len != 1) return BT_STATUS_PARM_INVALID;
3115 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
3116 break;
3117 case HCI_BLE_TEST_END:
3118 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
3119 break;
3120 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003121 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003122 return BT_STATUS_UNSUPPORTED;
3123 }
3124 return BT_STATUS_SUCCESS;
3125}
3126
3127#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08003128
3129void btif_dm_on_disable()
3130{
3131 /* cancel any pending pairing requests */
3132 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3133 {
3134 bt_bdaddr_t bd_addr;
3135
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003136 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003137 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3138 btif_dm_cancel_bond(&bd_addr);
3139 }
3140}
Matthew Xie1e5109b2012-11-09 18:26:26 -08003141
Satya Callojie5ba8842014-07-03 17:18:02 -07003142/*******************************************************************************
3143**
3144** Function btif_dm_read_energy_info
3145**
3146** Description Reads the energy info from controller
3147**
3148** Returns void
3149**
3150*******************************************************************************/
3151void btif_dm_read_energy_info()
3152{
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003153#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07003154 BTA_DmBleGetEnergyInfo(bta_energy_info_cb);
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003155#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07003156}
3157
Matthew Xie1e5109b2012-11-09 18:26:26 -08003158static char* btif_get_default_local_name() {
3159 if (btif_default_local_name[0] == '\0')
3160 {
3161 int max_len = sizeof(btif_default_local_name) - 1;
3162 if (BTM_DEF_LOCAL_NAME[0] != '\0')
3163 {
3164 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
3165 }
3166 else
3167 {
3168 char prop_model[PROPERTY_VALUE_MAX];
3169 property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
3170 strncpy(btif_default_local_name, prop_model, max_len);
3171 }
3172 btif_default_local_name[max_len] = '\0';
3173 }
3174 return btif_default_local_name;
3175}