blob: d85867fb27f46b6f8843b423e95da4ebe74dd2c2 [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 ***********************************************************************************/
27#include <stdio.h>
28#include <stdlib.h>
29#include <unistd.h>
30
31#include <hardware/bluetooth.h>
32
33#include <utils/Log.h>
34#include <cutils/properties.h>
35#include "gki.h"
36#include "btu.h"
37#include "bd.h"
38#include "bta_api.h"
39#include "btif_api.h"
40#include "btif_util.h"
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080041#include "btif_dm.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080042#include "btif_storage.h"
43#include "btif_hh.h"
44#include "btif_config.h"
45
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080046#include "bta_gatt_api.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080047/******************************************************************************
48** Constants & Macros
49******************************************************************************/
50
51#define COD_UNCLASSIFIED ((0x1F) << 8)
Priti Agheraebb1d752012-11-27 18:03:22 -080052#define COD_HID_KEYBOARD 0x0540
53#define COD_HID_POINTING 0x0580
54#define COD_HID_COMBO 0x05C0
55#define COD_HID_MAJOR 0x0500
56#define COD_AV_HEADSETS 0x0404
57#define COD_AV_HANDSFREE 0x0408
58#define COD_AV_HEADPHONES 0x0418
59#define COD_AV_PORTABLE_AUDIO 0x041C
60#define COD_AV_HIFI_AUDIO 0x0428
The Android Open Source Project5738f832012-12-12 16:00:35 -080061
62
63#define BTIF_DM_DEFAULT_INQ_MAX_RESULTS 0
64#define BTIF_DM_DEFAULT_INQ_MAX_DURATION 10
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -070065#define BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING 2
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080066
Matthew Xie1e5109b2012-11-09 18:26:26 -080067#define PROPERTY_PRODUCT_MODEL "ro.product.model"
Matthew Xiea30d95a2013-09-18 12:30:36 -070068#define DEFAULT_LOCAL_NAME_MAX 31
Matthew Xie1e5109b2012-11-09 18:26:26 -080069#if (DEFAULT_LOCAL_NAME_MAX > BTM_MAX_LOC_BD_NAME_LEN)
70 #error "default btif local name size exceeds stack supported length"
71#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080072
Matthew Xie7f3e4292013-09-30 12:44:10 -070073#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
74#define BTIF_DM_INTERLEAVE_DURATION_BR_ONE 2
75#define BTIF_DM_INTERLEAVE_DURATION_LE_ONE 2
76#define BTIF_DM_INTERLEAVE_DURATION_BR_TWO 3
77#define BTIF_DM_INTERLEAVE_DURATION_LE_TWO 4
78#endif
79
Priti Agherac0edf9f2014-06-26 11:23:51 -070080#define MAX_SDP_BL_ENTRIES 3
81
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080082typedef struct
83{
The Android Open Source Project5738f832012-12-12 16:00:35 -080084 bt_bond_state_t state;
85 BD_ADDR bd_addr;
86 UINT8 is_temp;
87 UINT8 pin_code_len;
88 UINT8 is_ssp;
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -070089 UINT8 auth_req;
90 UINT8 io_cap;
The Android Open Source Project5738f832012-12-12 16:00:35 -080091 UINT8 autopair_attempts;
92 UINT8 is_local_initiated;
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -070093 UINT8 sdp_attempts;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080094#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
95 BOOLEAN is_le_only;
96 btif_dm_ble_cb_t ble;
97#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -080098} btif_dm_pairing_cb_t;
99
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800100
101typedef struct
102{
103 UINT8 ir[BT_OCTET16_LEN];
104 UINT8 irk[BT_OCTET16_LEN];
105 UINT8 dhk[BT_OCTET16_LEN];
106}btif_dm_local_key_id_t;
107
108typedef struct
109{
110 BOOLEAN is_er_rcvd;
111 UINT8 er[BT_OCTET16_LEN];
112 BOOLEAN is_id_keys_rcvd;
113 btif_dm_local_key_id_t id_keys; /* ID kyes */
114
115}btif_dm_local_key_cb_t;
116
117typedef struct
118{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800119 BD_ADDR bd_addr;
120 BD_NAME bd_name;
121} btif_dm_remote_name_t;
122
123typedef struct
124{
125 BT_OCTET16 sp_c;
126 BT_OCTET16 sp_r;
127 BD_ADDR oob_bdaddr; /* peer bdaddr*/
128} btif_dm_oob_cb_t;
Satya Callojie5ba8842014-07-03 17:18:02 -0700129
130typedef struct
131{
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700132 bt_bdaddr_t bdaddr;
133 UINT8 transport; /* 0=Unknown, 1=BR/EDR, 2=LE */
134} btif_dm_create_bond_cb_t;
135
136typedef struct
137{
Satya Callojie5ba8842014-07-03 17:18:02 -0700138 uint8_t status;
139 uint8_t ctrl_state;
140 uint64_t tx_time;
141 uint64_t rx_time;
142 uint64_t idle_time;
143 uint64_t energy_used;
144} btif_activity_energy_info_cb_t;
145
Priti Agherac0edf9f2014-06-26 11:23:51 -0700146typedef struct
147{
148 unsigned int manufact_id;
149}skip_sdp_entry_t;
150
The Android Open Source Project5738f832012-12-12 16:00:35 -0800151#define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id))
152
Priti Agherac0edf9f2014-06-26 11:23:51 -0700153#define MAX_SDP_BL_ENTRIES 3
154#define UUID_HUMAN_INTERFACE_DEVICE "00001124-0000-1000-8000-00805f9b34fb"
155
156static skip_sdp_entry_t sdp_blacklist[] = {{76}}; //Apple Mouse and Keyboard
157
158
The Android Open Source Project5738f832012-12-12 16:00:35 -0800159/* This flag will be true if HCI_Inquiry is in progress */
160static BOOLEAN btif_dm_inquiry_in_progress = FALSE;
161
Priti Agherac0edf9f2014-06-26 11:23:51 -0700162
163
Matthew Xie1e5109b2012-11-09 18:26:26 -0800164/************************************************************************************
165** Static variables
166************************************************************************************/
167static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'};
168
The Android Open Source Project5738f832012-12-12 16:00:35 -0800169/******************************************************************************
170** Static functions
171******************************************************************************/
172static btif_dm_pairing_cb_t pairing_cb;
173static btif_dm_oob_cb_t oob_cb;
174static void btif_dm_generic_evt(UINT16 event, char* p_param);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700175static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800176static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name);
177static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
178 DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800179#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
180static btif_dm_local_key_cb_t ble_local_key_cb;
181static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif);
182static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl);
183static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req);
184#endif
Satya Calloji6e2d9db2014-07-08 16:18:58 -0700185
186static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
187 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
188 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status);
189
Matthew Xie1e5109b2012-11-09 18:26:26 -0800190static char* btif_get_default_local_name();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800191/******************************************************************************
192** Externs
193******************************************************************************/
194extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
195extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
196extern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
197extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
Hemant Gupta10256872013-08-19 18:33:01 +0530198extern bt_status_t btif_hf_client_execute_service(BOOLEAN b_enable);
Hemant Gupta2dc99992014-04-18 12:54:08 +0530199extern bt_status_t btif_mce_execute_service(BOOLEAN b_enable);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800200extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700201extern 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 -0800202
203
204/******************************************************************************
205** Functions
206******************************************************************************/
207
208bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
209 BOOLEAN b_enable)
210{
211 /* Check the service_ID and invoke the profile's BT state changed API */
212 switch (service_id)
213 {
214 case BTA_HFP_SERVICE_ID:
215 case BTA_HSP_SERVICE_ID:
216 {
217 btif_hf_execute_service(b_enable);
218 }break;
219 case BTA_A2DP_SERVICE_ID:
220 {
221 btif_av_execute_service(b_enable);
222 }break;
223 case BTA_HID_SERVICE_ID:
224 {
225 btif_hh_execute_service(b_enable);
226 }break;
Hemant Gupta10256872013-08-19 18:33:01 +0530227 case BTA_HFP_HS_SERVICE_ID:
228 {
229 btif_hf_client_execute_service(b_enable);
230 }break;
Hemant Gupta2dc99992014-04-18 12:54:08 +0530231 case BTA_MAP_SERVICE_ID:
232 {
233 btif_mce_execute_service(b_enable);
234 }break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800235 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700236 BTIF_TRACE_ERROR("%s: Unknown service being enabled", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800237 return BT_STATUS_FAIL;
238 }
239 return BT_STATUS_SUCCESS;
240}
241
242/*******************************************************************************
243**
244** Function check_eir_remote_name
245**
246** Description Check if remote name is in the EIR data
247**
248** Returns TRUE if remote name found
249** Populate p_remote_name, if provided and remote name found
250**
251*******************************************************************************/
252static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
253 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
254{
255 UINT8 *p_eir_remote_name = NULL;
256 UINT8 remote_name_len = 0;
257
258 /* Check EIR for remote name and services */
259 if (p_search_data->inq_res.p_eir)
260 {
261 p_eir_remote_name = BTA_CheckEirData(p_search_data->inq_res.p_eir,
262 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
263 if (!p_eir_remote_name)
264 {
265 p_eir_remote_name = BTA_CheckEirData(p_search_data->inq_res.p_eir,
266 BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
267 }
268
269 if (p_eir_remote_name)
270 {
271 if (remote_name_len > BD_NAME_LEN)
272 remote_name_len = BD_NAME_LEN;
273
274 if (p_remote_name && p_remote_name_len)
275 {
276 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
277 *(p_remote_name + remote_name_len) = 0;
278 *p_remote_name_len = remote_name_len;
279 }
280
281 return TRUE;
282 }
283 }
284
285 return FALSE;
286
287}
288
289/*******************************************************************************
290**
291** Function check_cached_remote_name
292**
293** Description Check if remote name is in the NVRAM cache
294**
295** Returns TRUE if remote name found
296** Populate p_remote_name, if provided and remote name found
297**
298*******************************************************************************/
299static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
300 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
301{
302 bt_bdname_t bdname;
303 bt_bdaddr_t remote_bdaddr;
304 bt_property_t prop_name;
305
306 /* check if we already have it in our btif_storage cache */
307 bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
308 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
309 sizeof(bt_bdname_t), &bdname);
310 if (btif_storage_get_remote_device_property(
311 &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
312 {
313 if (p_remote_name && p_remote_name_len)
314 {
315 strcpy((char *)p_remote_name, (char *)bdname.name);
316 *p_remote_name_len = strlen((char *)p_remote_name);
317 }
318 return TRUE;
319 }
320
321 return FALSE;
322}
323
324BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
325{
326 uint32_t remote_cod;
327 bt_property_t prop_name;
328
329 /* check if we already have it in our btif_storage cache */
330 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
331 sizeof(uint32_t), &remote_cod);
332 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
333 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700334 BTIF_TRACE_ERROR("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800335 if ((remote_cod & 0x7ff) == cod)
336 return TRUE;
337 }
338
339 return FALSE;
340}
341
Priti Agheraebb1d752012-11-27 18:03:22 -0800342BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
343{
344 uint32_t remote_cod;
345 bt_property_t prop_name;
346
347 /* check if we already have it in our btif_storage cache */
348 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
349 sizeof(uint32_t), &remote_cod);
350 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
351 &prop_name) == BT_STATUS_SUCCESS)
352 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700353 BTIF_TRACE_DEBUG("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
Priti Agheraebb1d752012-11-27 18:03:22 -0800354 if ((remote_cod & 0x700) == cod)
355 return TRUE;
356 }
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700357 return FALSE;
358}
Priti Agheraebb1d752012-11-27 18:03:22 -0800359
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700360BOOLEAN check_hid_le(const bt_bdaddr_t *remote_bdaddr)
361{
362 uint32_t remote_dev_type;
363 bt_property_t prop_name;
364
365 /* check if we already have it in our btif_storage cache */
366 BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE,
367 sizeof(uint32_t), &remote_dev_type);
368 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
369 &prop_name) == BT_STATUS_SUCCESS)
370 {
371 if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE)
372 {
373 bdstr_t bdstr;
374 bd2str(remote_bdaddr, &bdstr);
375 if(btif_config_exist("Remote", bdstr, "HidAppId"))
376 return TRUE;
377 }
378 }
Priti Agheraebb1d752012-11-27 18:03:22 -0800379 return FALSE;
380}
381
Priti Agherac0edf9f2014-06-26 11:23:51 -0700382/*****************************************************************************
383**
384** Function check_sdp_bl
385**
386** Description Checks if a given device is blacklisted to skip sdp
387**
388** Parameters skip_sdp_entry
389**
390** Returns TRUE if the device is present in blacklist, else FALSE
391**
392*******************************************************************************/
393BOOLEAN check_sdp_bl(const bt_bdaddr_t *remote_bdaddr)
394{
395 UINT8 i = 0;
396 UINT16 manufacturer = 0;
397 UINT8 lmp_ver = 0;
398 UINT16 lmp_subver = 0;
399 tBTM_STATUS btm_status;
400 bt_property_t prop_name;
401 bt_remote_version_t info;
402 bt_status_t status;
403
404
405 if (remote_bdaddr == NULL)
406 return FALSE;
407
408/* fetch additional info about remote device used in iop query */
409 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)remote_bdaddr, &lmp_ver,
410 &manufacturer, &lmp_subver);
411
412
413
414 /* if not available yet, try fetching from config database */
415 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO,
416 sizeof(bt_remote_version_t), &info);
417
418 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
419 &prop_name) != BT_STATUS_SUCCESS)
420 {
421
422 return FALSE;
423 }
424 manufacturer = info.manufacturer;
425
426 for (int i = 0; i < MAX_SDP_BL_ENTRIES; i++)
427 {
428 if (manufacturer == sdp_blacklist[i].manufact_id)
429 return TRUE;
430 }
431 return FALSE;
432}
433
434
The Android Open Source Project5738f832012-12-12 16:00:35 -0800435static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
436{
437 /* Send bonding state only once - based on outgoing/incoming we may receive duplicates */
438 if ( (pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING) )
439 return;
440
441 if (pairing_cb.is_temp)
442 {
443 state = BT_BOND_STATE_NONE;
444 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700445 BTIF_TRACE_DEBUG("%s: state=%d prev_state=%d", __FUNCTION__, state, pairing_cb.state);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800446
447 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
448
449 if (state == BT_BOND_STATE_BONDING)
450 {
451 pairing_cb.state = state;
452 bdcpy(pairing_cb.bd_addr, bd_addr->address);
453 }
454 else
455 {
456 memset(&pairing_cb, 0, sizeof(pairing_cb));
457 }
458
459}
460
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800461/* store remote version in bt config to always have access
462 to it post pairing*/
463static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
464{
465 bt_property_t property;
466 UINT8 lmp_ver = 0;
467 UINT16 lmp_subver = 0;
468 UINT16 mfct_set = 0;
469 tBTM_STATUS btm_status;
470 bt_remote_version_t info;
471 bt_status_t status;
472 bdstr_t bdstr;
473
474 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
475 &mfct_set, &lmp_subver);
476
477 ALOGD("remote version info [%s]: %x, %x, %x", bd2str(p_bd, &bdstr),
478 lmp_ver, mfct_set, lmp_subver);
479
480 if (btm_status == BTM_SUCCESS)
481 {
482 /* always update cache to ensure we have availability whenever BTM API
483 is not populated */
484 info.manufacturer = mfct_set;
485 info.sub_ver = lmp_subver;
486 info.version = lmp_ver;
487 BTIF_STORAGE_FILL_PROPERTY(&property,
488 BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
489 &info);
490 status = btif_storage_set_remote_device_property(p_bd, &property);
491 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
492 }
493}
494
The Android Open Source Project5738f832012-12-12 16:00:35 -0800495
496static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
497 DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
498{
499 int num_properties = 0;
500 bt_property_t properties[3];
501 bt_bdaddr_t bdaddr;
502 bt_status_t status;
503 UINT32 cod;
504 bt_device_type_t dev_type;
505
506 memset(properties, 0, sizeof(properties));
507 bdcpy(bdaddr.address, bd_addr);
508
509 /* remote name */
510 if (strlen((const char *) bd_name))
511 {
512 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
513 BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
514 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
515 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
516 num_properties++;
517 }
518
519 /* class of device */
520 cod = devclass2uint(dev_class);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700521 BTIF_TRACE_DEBUG("%s():cod is 0x%06x", __FUNCTION__, cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800522 if ( cod == 0) {
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530523 /* Try to retrieve cod from storage */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700524 BTIF_TRACE_DEBUG("%s():cod is 0, checking cod from storage", __FUNCTION__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530525 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
526 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
527 status = btif_storage_get_remote_device_property(&bdaddr, &properties[num_properties]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700528 BTIF_TRACE_DEBUG("%s():cod retreived from storage is 0x%06x", __FUNCTION__, cod);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530529 if ( cod == 0) {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700530 BTIF_TRACE_DEBUG("%s():cod is again 0, set as unclassified", __FUNCTION__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530531 cod = COD_UNCLASSIFIED;
532 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800533 }
534
535 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
536 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
537 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
538 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
539 num_properties++;
540
541 /* device type */
542 dev_type = device_type;
543 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
544 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
545 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
546 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
547 num_properties++;
548
549 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
550 status, &bdaddr, num_properties, properties);
551}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800552
553/*******************************************************************************
554**
555** Function btif_dm_cb_hid_remote_name
556**
557** Description Remote name callback for HID device. Called in btif context
558** Special handling for HID devices
559**
560** Returns void
561**
562*******************************************************************************/
563static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
564{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700565 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 -0800566 if (pairing_cb.state == BT_BOND_STATE_BONDING)
567 {
568 bt_bdaddr_t remote_bd;
569
570 bdcpy(remote_bd.address, pairing_cb.bd_addr);
571
572 if (p_remote_name->status == BTM_SUCCESS)
573 {
574 bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
575 }
576 else
577 bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
578 }
579}
580
The Android Open Source Project5738f832012-12-12 16:00:35 -0800581/*******************************************************************************
582**
583** Function btif_dm_cb_create_bond
584**
585** Description Create bond initiated from the BTIF thread context
586** Special handling for HID devices
587**
588** Returns void
589**
590*******************************************************************************/
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700591static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800592{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800593 BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800594 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800595
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800596#if BLE_INCLUDED == TRUE
597 int device_type;
598 int addr_type;
599 bdstr_t bdstr;
600 bd2str(bd_addr, &bdstr);
lungtsai_lin3baff792014-08-29 13:47:47 +0800601 if((btif_config_get_int("Remote", (char const *)&bdstr,"DevType", &device_type) &&
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800602 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
lungtsai_lin3baff792014-08-29 13:47:47 +0800603 (device_type == BT_DEVICE_TYPE_BLE)) || (transport == BT_TRANSPORT_LE))
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800604 {
605 BTA_DmAddBleDevice(bd_addr->address, addr_type, BT_DEVICE_TYPE_BLE);
606 }
607#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800608
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800609#if BLE_INCLUDED == TRUE
610 if(is_hid && device_type != BT_DEVICE_TYPE_BLE)
611#else
612 if(is_hid)
613#endif
614 {
615 int status;
616 status = btif_hh_connect(bd_addr);
617 if(status != BT_STATUS_SUCCESS)
618 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800619 }
620 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800621 {
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700622 BTA_DmBondByTransport((UINT8 *)bd_addr->address, transport);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800623 }
624 /* Track originator of bond creation */
625 pairing_cb.is_local_initiated = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800626
627}
628
629/*******************************************************************************
630**
631** Function btif_dm_cb_remove_bond
632**
633** Description remove bond initiated from the BTIF thread context
634** Special handling for HID devices
635**
636** Returns void
637**
638*******************************************************************************/
639void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
640{
641 bdstr_t bdstr;
642 /*special handling for HID devices */
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700643 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there
644 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
645#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
646 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
647#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800648 {
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700649 BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800650 }
651}
652
653/*******************************************************************************
654**
Andre Eisenbach249f6002014-06-18 12:20:37 -0700655** Function btif_dm_get_connection_state
656**
657** Description Returns whether the remote device is currently connected
658**
659** Returns 0 if not connected
660**
661*******************************************************************************/
662uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr)
663{
664 return BTA_DmGetConnectionState((UINT8 *)bd_addr->address);
665}
666
667/*******************************************************************************
668**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800669** Function search_devices_copy_cb
670**
671** Description Deep copy callback for search devices event
672**
673** Returns void
674**
675*******************************************************************************/
676static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
677{
678 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
679 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
680
681 if (!p_src)
682 return;
683
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700684 BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800685 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
686 switch (event)
687 {
688 case BTA_DM_INQ_RES_EVT:
689 {
690 if (p_src_data->inq_res.p_eir)
691 {
692 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
693 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
694 }
695 }
696 break;
697
698 case BTA_DM_DISC_RES_EVT:
699 {
700 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
701 {
702 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
703 memcpy(p_dest_data->disc_res.p_raw_data,
704 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
705 }
706 }
707 break;
708 }
709}
710
711static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
712{
713 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
714 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
715
716 if (!p_src)
717 return;
718 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
719 switch (event)
720 {
721 case BTA_DM_DISC_RES_EVT:
722 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530723 if (p_src_data->disc_res.result == BTA_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800724 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530725 if (p_src_data->disc_res.num_uuids > 0)
726 {
727 p_dest_data->disc_res.p_uuid_list =
728 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
729 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
730 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
731 GKI_freebuf(p_src_data->disc_res.p_uuid_list);
732 }
733 if (p_src_data->disc_res.p_raw_data != NULL)
734 {
735 GKI_freebuf(p_src_data->disc_res.p_raw_data);
736 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800737 }
738 } break;
739 }
740}
741/******************************************************************************
742**
743** BTIF DM callback events
744**
745*****************************************************************************/
746
747/*******************************************************************************
748**
749** Function btif_dm_pin_req_evt
750**
751** Description Executes pin request event in btif context
752**
753** Returns void
754**
755*******************************************************************************/
756static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
757{
758 bt_bdaddr_t bd_addr;
759 bt_bdname_t bd_name;
760 UINT32 cod;
761 bt_pin_code_t pin_code;
762
763 /* Remote properties update */
764 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
765 p_pin_req->dev_class, BT_DEVICE_TYPE_BREDR);
766
767 bdcpy(bd_addr.address, p_pin_req->bd_addr);
768 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
769
770 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
771
772 cod = devclass2uint(p_pin_req->dev_class);
773
774 if ( cod == 0) {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700775 BTIF_TRACE_DEBUG("%s():cod is 0, set as unclassified", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800776 cod = COD_UNCLASSIFIED;
777 }
778
779 /* check for auto pair possiblity only if bond was initiated by local device */
780 if (pairing_cb.is_local_initiated)
781 {
782 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
783 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
784 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
785 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
786 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
787 check_cod(&bd_addr, COD_HID_POINTING))
788 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700789 BTIF_TRACE_DEBUG("%s()cod matches for auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800790 /* Check if this device can be auto paired */
791 if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) &&
792 (pairing_cb.autopair_attempts == 0))
793 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700794 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800795 pin_code.pin[0] = 0x30;
796 pin_code.pin[1] = 0x30;
797 pin_code.pin[2] = 0x30;
798 pin_code.pin[3] = 0x30;
799
800 pairing_cb.autopair_attempts++;
801 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
802 return;
803 }
804 }
805 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
806 check_cod(&bd_addr, COD_HID_COMBO))
807 {
808 if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) &&
809 (pairing_cb.autopair_attempts == 0))
810 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700811 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800812 pin_code.pin[0] = 0x30;
813 pin_code.pin[1] = 0x30;
814 pin_code.pin[2] = 0x30;
815 pin_code.pin[3] = 0x30;
816
817 pairing_cb.autopair_attempts++;
818 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
819 return;
820 }
821 }
822 }
823 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
824 &bd_addr, &bd_name, cod);
825}
826
827/*******************************************************************************
828**
829** Function btif_dm_ssp_cfm_req_evt
830**
831** Description Executes SSP confirm request event in btif context
832**
833** Returns void
834**
835*******************************************************************************/
836static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
837{
838 bt_bdaddr_t bd_addr;
839 bt_bdname_t bd_name;
840 UINT32 cod;
841 BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
842
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700843 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800844
845 /* Remote properties update */
846 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
847 p_ssp_cfm_req->dev_class, BT_DEVICE_TYPE_BREDR);
848
849 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
850 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
851
852 /* Set the pairing_cb based on the local & remote authentication requirements */
853 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
854
855 /* if just_works and bonding bit is not set treat this as temporary */
856 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 -0800857 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
858 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800859 pairing_cb.is_temp = TRUE;
860 else
861 pairing_cb.is_temp = FALSE;
862
863 pairing_cb.is_ssp = TRUE;
864
865 /* If JustWorks auto-accept */
866 if (p_ssp_cfm_req->just_works)
867 {
868 /* Pairing consent for JustWorks needed if:
869 * 1. Incoming pairing is detected AND
870 * 2. local IO capabilities are DisplayYesNo AND
871 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
872 */
873 if ((is_incoming) && ((p_ssp_cfm_req->loc_io_caps == 0x01) &&
874 (p_ssp_cfm_req->rmt_io_caps == 0x00 || p_ssp_cfm_req->rmt_io_caps == 0x03)))
875 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700876 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 -0800877 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
878 }
879 else
880 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700881 BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800882 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
883 return;
884 }
885 }
886
887 cod = devclass2uint(p_ssp_cfm_req->dev_class);
888
889 if ( cod == 0) {
890 ALOGD("cod is 0, set as unclassified");
891 cod = COD_UNCLASSIFIED;
892 }
893
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700894 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800895 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
896 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
897 p_ssp_cfm_req->num_val);
898}
899
900static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
901{
902 bt_bdaddr_t bd_addr;
903 bt_bdname_t bd_name;
904 UINT32 cod;
905
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700906 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800907
908 /* Remote properties update */
909 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
910 p_ssp_key_notif->dev_class, BT_DEVICE_TYPE_BREDR);
911
912 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
913 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
914
915 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
916 pairing_cb.is_ssp = TRUE;
917 cod = devclass2uint(p_ssp_key_notif->dev_class);
918
919 if ( cod == 0) {
920 ALOGD("cod is 0, set as unclassified");
921 cod = COD_UNCLASSIFIED;
922 }
923
924 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
925 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
926 p_ssp_key_notif->passkey);
927}
928/*******************************************************************************
929**
930** Function btif_dm_auth_cmpl_evt
931**
932** Description Executes authentication complete event in btif context
933**
934** Returns void
935**
936*******************************************************************************/
937static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
938{
939 /* Save link key, if not temporary */
940 bt_bdaddr_t bd_addr;
941 bt_status_t status = BT_STATUS_FAIL;
942 bt_bond_state_t state = BT_BOND_STATE_NONE;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700943 BOOLEAN skip_sdp = FALSE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800944
945 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
946 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
947 {
948 if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) || (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
949 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) || (!pairing_cb.is_temp))
950 {
951 bt_status_t ret;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700952 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, is_temp=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800953 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.is_temp);
954 ret = btif_storage_add_bonded_device(&bd_addr,
955 p_auth_cmpl->key, p_auth_cmpl->key_type,
956 pairing_cb.pin_code_len);
957 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
958 }
959 else
960 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700961 BTIF_TRACE_DEBUG("%s: Temporary key. Not storing. key_type=0x%x, is_temp=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800962 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.is_temp);
Hemant Guptab820aec2013-12-24 19:59:57 +0530963 if(pairing_cb.is_temp)
964 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700965 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
Hemant Guptab820aec2013-12-24 19:59:57 +0530966 __FUNCTION__);
967 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
968 return;
969 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800970 }
971 }
Priti Agherac0edf9f2014-06-26 11:23:51 -0700972
973 // Skip SDP for certain HID Devices
The Android Open Source Project5738f832012-12-12 16:00:35 -0800974 if (p_auth_cmpl->success)
975 {
976 status = BT_STATUS_SUCCESS;
977 state = BT_BOND_STATE_BONDED;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700978 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800979
Priti Agherac0edf9f2014-06-26 11:23:51 -0700980 if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr, COD_HID_MAJOR))
981 {
982 ALOGW("%s:skip SDP",
983 __FUNCTION__);
984 skip_sdp = TRUE;
985 }
986 if(!pairing_cb.is_local_initiated && skip_sdp)
987 {
988 bond_state_changed(status, &bd_addr, state);
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -0800989
Priti Agherac0edf9f2014-06-26 11:23:51 -0700990 ALOGW("%s: Incoming HID Connection",__FUNCTION__);
991 bt_property_t prop;
992 bt_bdaddr_t bd_addr;
993 bt_uuid_t uuid;
994 char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE;
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -0800995
Priti Agherac0edf9f2014-06-26 11:23:51 -0700996 string_to_uuid(uuid_str, &uuid);
997
998 prop.type = BT_PROPERTY_UUIDS;
999 prop.val = uuid.uu;
1000 prop.len = MAX_UUID_SIZE;
1001
1002 /* Send the event to the BTIF */
1003 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1004 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1005 }
1006 else
1007 {
1008 /* Trigger SDP on the device */
1009 pairing_cb.sdp_attempts = 1;;
1010
1011 if(btif_dm_inquiry_in_progress)
1012 btif_dm_cancel_discovery();
1013
1014 btif_dm_get_remote_services(&bd_addr);
1015 }
1016 /* 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 -08001017 }
1018 else
1019 {
1020 /*Map the HCI fail reason to bt status */
1021 switch(p_auth_cmpl->fail_reason)
1022 {
1023 case HCI_ERR_PAGE_TIMEOUT:
1024 case HCI_ERR_CONNECTION_TOUT:
1025 status = BT_STATUS_RMT_DEV_DOWN;
1026 break;
1027
Hemant Guptaaef7a672013-07-31 19:00:12 +05301028 case HCI_ERR_PAIRING_NOT_ALLOWED:
1029 status = BT_STATUS_AUTH_REJECTED;
1030 break;
1031
Hemant Guptab4801442014-01-07 18:11:15 +05301032 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
1033 status = BT_STATUS_AUTH_FAILURE;
1034 break;
1035
The Android Open Source Project5738f832012-12-12 16:00:35 -08001036 /* map the auth failure codes, so we can retry pairing if necessary */
1037 case HCI_ERR_AUTH_FAILURE:
Hemant Gupta59a88ec2014-03-19 19:01:35 +05301038 case HCI_ERR_KEY_MISSING:
Zhihai Xua7ea8092013-11-27 14:10:53 +05301039 btif_storage_remove_bonded_device(&bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001040 case HCI_ERR_HOST_REJECT_SECURITY:
1041 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
1042 case HCI_ERR_UNIT_KEY_USED:
1043 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
1044 case HCI_ERR_INSUFFCIENT_SECURITY:
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301045 case HCI_ERR_PEER_USER:
1046 case HCI_ERR_UNSPECIFIED:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001047 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d",
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301048 __FUNCTION__, p_auth_cmpl->fail_reason);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001049 if (pairing_cb.autopair_attempts == 1)
1050 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001051 BTIF_TRACE_DEBUG("%s(): Adding device to blacklist ", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001052
1053 /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class */
1054 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
1055 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
1056 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
1057 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
1058 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
1059 check_cod(&bd_addr, COD_HID_POINTING))
1060 {
1061 btif_storage_add_device_to_autopair_blacklist (&bd_addr);
1062 }
1063 pairing_cb.autopair_attempts++;
1064
1065 /* Create the Bond once again */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001066 BTIF_TRACE_DEBUG("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07001067 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001068 return;
1069 }
1070 else
1071 {
1072 /* if autopair attempts are more than 1, or not attempted */
1073 status = BT_STATUS_AUTH_FAILURE;
1074 }
1075 break;
1076
1077 default:
1078 status = BT_STATUS_FAIL;
1079 }
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301080 /* Special Handling for HID Devices */
1081 if (check_cod(&bd_addr, COD_HID_POINTING)) {
1082 /* Remove Device as bonded in nvram as authentication failed */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001083 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __FUNCTION__);
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301084 btif_storage_remove_bonded_device(&bd_addr);
1085 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001086 bond_state_changed(status, &bd_addr, state);
1087 }
1088}
1089
1090/******************************************************************************
1091**
1092** Function btif_dm_search_devices_evt
1093**
1094** Description Executes search devices callback events in btif context
1095**
1096** Returns void
1097**
1098******************************************************************************/
1099static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
1100{
1101 tBTA_DM_SEARCH *p_search_data;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001102 BTIF_TRACE_EVENT("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001103
1104 switch (event)
1105 {
1106 case BTA_DM_DISC_RES_EVT:
1107 {
1108 p_search_data = (tBTA_DM_SEARCH *)p_param;
1109 /* Remote name update */
1110 if (strlen((const char *) p_search_data->disc_res.bd_name))
1111 {
1112 bt_property_t properties[1];
1113 bt_bdaddr_t bdaddr;
1114 bt_status_t status;
1115
1116 properties[0].type = BT_PROPERTY_BDNAME;
1117 properties[0].val = p_search_data->disc_res.bd_name;
1118 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
1119 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
1120
1121 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
1122 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
1123 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1124 status, &bdaddr, 1, properties);
1125 }
1126 /* TODO: Services? */
1127 }
1128 break;
1129
1130 case BTA_DM_INQ_RES_EVT:
1131 {
1132 /* inquiry result */
1133 UINT32 cod;
1134 UINT8 *p_eir_remote_name = NULL;
1135 bt_bdname_t bdname;
1136 bt_bdaddr_t bdaddr;
1137 UINT8 remote_name_len;
1138 UINT8 *p_cached_name = NULL;
1139 tBTA_SERVICE_MASK services = 0;
1140 bdstr_t bdstr;
1141
1142 p_search_data = (tBTA_DM_SEARCH *)p_param;
1143 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
1144
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001145 BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __FUNCTION__, bd2str(&bdaddr, &bdstr),
The Android Open Source Project5738f832012-12-12 16:00:35 -08001146#if (BLE_INCLUDED == TRUE)
1147 p_search_data->inq_res.device_type);
1148#else
1149 BT_DEVICE_TYPE_BREDR);
1150#endif
1151 bdname.name[0] = 0;
1152
1153 cod = devclass2uint (p_search_data->inq_res.dev_class);
1154
1155 if ( cod == 0) {
1156 ALOGD("cod is 0, set as unclassified");
1157 cod = COD_UNCLASSIFIED;
1158 }
1159
1160 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1161 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1162
1163 /* Check EIR for remote name and services */
1164 if (p_search_data->inq_res.p_eir)
1165 {
1166 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001167 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001168 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */
1169 }
1170
1171
1172 {
1173 bt_property_t properties[5];
1174 bt_device_type_t dev_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001175 UINT8 addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001176 uint32_t num_properties = 0;
1177 bt_status_t status;
1178
1179 memset(properties, 0, sizeof(properties));
1180 /* BD_ADDR */
1181 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1182 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1183 num_properties++;
1184 /* BD_NAME */
1185 /* Don't send BDNAME if it is empty */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001186 if (bdname.name[0])
1187 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001188 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1189 BT_PROPERTY_BDNAME,
1190 strlen((char *)bdname.name), &bdname);
1191 num_properties++;
1192 }
1193
1194 /* DEV_CLASS */
1195 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1196 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1197 num_properties++;
1198 /* DEV_TYPE */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001199#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001200 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1201 dev_type = p_search_data->inq_res.device_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001202 addr_type = p_search_data->inq_res.ble_addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001203#else
1204 dev_type = BT_DEVICE_TYPE_BREDR;
1205#endif
1206 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1207 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1208 num_properties++;
1209 /* RSSI */
1210 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1211 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1212 &(p_search_data->inq_res.rssi));
1213 num_properties++;
1214
1215 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1216 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001217#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1218 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001219 if (( dev_type == BT_DEVICE_TYPE_DUMO)&&
1220 (p_search_data->inq_res.flag & BTA_BLE_DMT_CONTROLLER_SPT) &&
1221 (p_search_data->inq_res.flag & BTA_BLE_DMT_HOST_SPT))
1222 {
1223 btif_storage_set_dmt_support_type (&bdaddr, TRUE);
1224 }
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001225 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1226#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001227 /* Callback to notify upper layer of device */
1228 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1229 num_properties, properties);
1230 }
1231 }
1232 break;
1233
1234 case BTA_DM_INQ_CMPL_EVT:
1235 {
Satya Calloji6e2d9db2014-07-08 16:18:58 -07001236#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1237 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1238 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1239 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1240 bte_scan_filt_param_cfg_evt, 0);
1241#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001242 }
1243 break;
1244 case BTA_DM_DISC_CMPL_EVT:
1245 {
1246 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1247 }
1248 break;
1249 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1250 {
1251 /* if inquiry is not in progress and we get a cancel event, then
1252 * it means we are done with inquiry, but remote_name fetches are in
1253 * progress
1254 *
1255 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt
1256 * but instead wait for the cancel_cmpl_evt via the Busy Level
1257 *
1258 */
1259 if (btif_dm_inquiry_in_progress == FALSE)
1260 {
1261 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1262 }
1263 }
1264 break;
1265 }
1266}
1267
1268/*******************************************************************************
1269**
1270** Function btif_dm_search_services_evt
1271**
1272** Description Executes search services event in btif context
1273**
1274** Returns void
1275**
1276*******************************************************************************/
1277static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1278{
1279 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1280
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001281 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001282 switch (event)
1283 {
1284 case BTA_DM_DISC_RES_EVT:
1285 {
1286 bt_uuid_t uuid_arr[BT_MAX_NUM_UUIDS]; /* Max 32 services */
1287 bt_property_t prop;
1288 uint32_t i = 0, j = 0;
1289 bt_bdaddr_t bd_addr;
1290 bt_status_t ret;
1291
1292 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1293
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001294 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001295 p_data->disc_res.result, p_data->disc_res.services);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001296 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1297 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1298 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1299 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001300 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001301 pairing_cb.sdp_attempts++;
1302 btif_dm_get_remote_services(&bd_addr);
1303 return;
1304 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001305 prop.type = BT_PROPERTY_UUIDS;
1306 prop.len = 0;
1307 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1308 {
1309 prop.val = p_data->disc_res.p_uuid_list;
1310 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1311 for (i=0; i < p_data->disc_res.num_uuids; i++)
1312 {
1313 char temp[256];
1314 uuid_to_string((bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i*MAX_UUID_SIZE)), temp);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001315 BTIF_TRACE_ERROR("Index: %d uuid:%s", i, temp);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001316 }
1317 }
1318
1319 /* onUuidChanged requires getBondedDevices to be populated.
1320 ** bond_state_changed needs to be sent prior to remote_device_property
1321 */
1322 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1323 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0)&&
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001324 pairing_cb.sdp_attempts > 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001325 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001326 BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001327 __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001328 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001329 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1330 }
1331
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001332 if(p_data->disc_res.num_uuids != 0)
1333 {
1334 /* Also write this to the NVRAM */
1335 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1336 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1337 /* Send the event to the BTIF */
1338 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1339 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1340 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001341 }
1342 break;
1343
1344 case BTA_DM_DISC_CMPL_EVT:
1345 /* fixme */
1346 break;
1347
Matthew Xie607e3b72013-08-15 19:30:48 -07001348#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001349 case BTA_DM_DISC_BLE_RES_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001350 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__,
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001351 p_data->disc_ble_res.service.uu.uuid16);
1352 bt_uuid_t uuid;
1353 int i = 0;
1354 int j = 15;
1355 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1356 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001357 BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001358 bt_property_t prop;
1359 bt_bdaddr_t bd_addr;
1360 char temp[256];
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001361 bt_status_t ret;
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001362
1363 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1364
1365 while(i < j )
1366 {
1367 unsigned char c = uuid.uu[j];
1368 uuid.uu[j] = uuid.uu[i];
1369 uuid.uu[i] = c;
1370 i++;
1371 j--;
1372 }
1373
1374 uuid_to_string(&uuid, temp);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001375 BTIF_TRACE_ERROR(" uuid:%s", temp);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001376
1377 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1378 prop.type = BT_PROPERTY_UUIDS;
1379 prop.val = uuid.uu;
1380 prop.len = MAX_UUID_SIZE;
1381
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001382 /* Also write this to the NVRAM */
1383 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1384 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1385
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001386 /* Send the event to the BTIF */
1387 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1388 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1389
1390 }
1391 break;
Matthew Xie607e3b72013-08-15 19:30:48 -07001392#endif /* BLE_INCLUDED */
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001393
The Android Open Source Project5738f832012-12-12 16:00:35 -08001394 default:
1395 {
1396 ASSERTC(0, "unhandled search services event", event);
1397 }
1398 break;
1399 }
1400}
1401
1402/*******************************************************************************
1403**
1404** Function btif_dm_remote_service_record_evt
1405**
1406** Description Executes search service record event in btif context
1407**
1408** Returns void
1409**
1410*******************************************************************************/
1411static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1412{
1413 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1414
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001415 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001416 switch (event)
1417 {
1418 case BTA_DM_DISC_RES_EVT:
1419 {
1420 bt_service_record_t rec;
1421 bt_property_t prop;
1422 uint32_t i = 0;
1423 bt_bdaddr_t bd_addr;
1424
1425 memset(&rec, 0, sizeof(bt_service_record_t));
1426 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1427
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001428 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001429 p_data->disc_res.result, p_data->disc_res.services);
1430 prop.type = BT_PROPERTY_SERVICE_RECORD;
1431 prop.val = (void*)&rec;
1432 prop.len = sizeof(rec);
1433
1434 /* disc_res.result is overloaded with SCN. Cannot check result */
1435 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1436 /* TODO: Get the UUID as well */
1437 rec.channel = p_data->disc_res.result - 3;
1438 /* TODO: Need to get the service name using p_raw_data */
1439 rec.name[0] = 0;
1440
1441 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1442 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1443 }
1444 break;
1445
1446 default:
1447 {
1448 ASSERTC(0, "unhandled remote service record event", event);
1449 }
1450 break;
1451 }
1452}
1453
1454/*******************************************************************************
1455**
1456** Function btif_dm_upstreams_cback
1457**
1458** Description Executes UPSTREAMS events in btif context
1459**
1460** Returns void
1461**
1462*******************************************************************************/
1463static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1464{
1465 tBTA_DM_SEC_EVT dm_event = (tBTA_DM_SEC_EVT)event;
1466 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1467 tBTA_SERVICE_MASK service_mask;
1468 uint32_t i;
1469 bt_bdaddr_t bd_addr;
1470
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001471 BTIF_TRACE_EVENT("btif_dm_upstreams_cback ev: %s", dump_dm_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001472
1473 switch (event)
1474 {
1475 case BTA_DM_ENABLE_EVT:
1476 {
1477 BD_NAME bdname;
1478 bt_status_t status;
1479 bt_property_t prop;
1480 prop.type = BT_PROPERTY_BDNAME;
1481 prop.len = BD_NAME_LEN;
1482 prop.val = (void*)bdname;
1483
1484 status = btif_storage_get_adapter_property(&prop);
Matthew Xie1e5109b2012-11-09 18:26:26 -08001485 if (status == BT_STATUS_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001486 {
1487 /* A name exists in the storage. Make this the device name */
1488 BTA_DmSetDeviceName((char*)prop.val);
1489 }
Matthew Xie1e5109b2012-11-09 18:26:26 -08001490 else
1491 {
1492 /* Storage does not have a name yet.
1493 * Use the default name and write it to the chip
1494 */
1495 BTA_DmSetDeviceName(btif_get_default_local_name());
1496 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001497
1498 /* for each of the enabled services in the mask, trigger the profile
1499 * enable */
1500 service_mask = btif_get_enabled_services_mask();
1501 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1502 {
1503 if (service_mask &
1504 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1505 {
1506 btif_in_execute_service_request(i, TRUE);
1507 }
1508 }
1509 /* clear control blocks */
1510 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1511
1512 /* This function will also trigger the adapter_properties_cb
1513 ** and bonded_devices_info_cb
1514 */
1515 btif_storage_load_bonded_devices();
1516
1517 btif_storage_load_autopair_device_list();
1518
1519 btif_enable_bluetooth_evt(p_data->enable.status, p_data->enable.bd_addr);
Satya Calloji0943c102014-05-12 09:13:02 -07001520
Wei Wangbf0e4b22014-05-19 23:23:35 -07001521 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Calloji0943c102014-05-12 09:13:02 -07001522 /* Enable local privacy */
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001523 /*TODO Should this call be exposed to JAVA...? */
Satya Calloji0943c102014-05-12 09:13:02 -07001524 BTA_DmBleConfigLocalPrivacy(TRUE);
Wei Wangbf0e4b22014-05-19 23:23:35 -07001525 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001526 }
1527 break;
1528
1529 case BTA_DM_DISABLE_EVT:
1530 /* for each of the enabled services in the mask, trigger the profile
1531 * disable */
1532 service_mask = btif_get_enabled_services_mask();
1533 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1534 {
1535 if (service_mask &
1536 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1537 {
1538 btif_in_execute_service_request(i, FALSE);
1539 }
1540 }
1541 btif_disable_bluetooth_evt();
1542 break;
1543
1544 case BTA_DM_PIN_REQ_EVT:
1545 btif_dm_pin_req_evt(&p_data->pin_req);
1546 break;
1547
1548 case BTA_DM_AUTH_CMPL_EVT:
1549 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1550 break;
1551
1552 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1553 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1554 {
1555 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1556 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1557 }
1558 break;
1559
1560 case BTA_DM_SP_CFM_REQ_EVT:
1561 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1562 break;
1563 case BTA_DM_SP_KEY_NOTIF_EVT:
1564 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1565 break;
1566
1567 case BTA_DM_DEV_UNPAIRED_EVT:
1568 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1569
1570 /*special handling for HID devices */
1571 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -07001572 btif_hh_remove_device(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001573 #endif
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001574 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1575 btif_storage_remove_ble_bonding_keys(&bd_addr);
1576 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001577 btif_storage_remove_bonded_device(&bd_addr);
1578 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1579 break;
1580
1581 case BTA_DM_BUSY_LEVEL_EVT:
1582 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001583
1584 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001585 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001586 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001587 {
1588 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1589 BT_DISCOVERY_STARTED);
1590 btif_dm_inquiry_in_progress = TRUE;
1591 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001592 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001593 {
1594 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1595 BT_DISCOVERY_STOPPED);
1596 btif_dm_inquiry_in_progress = FALSE;
1597 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001598 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001599 {
1600 btif_dm_inquiry_in_progress = FALSE;
1601 }
1602 }
1603 }break;
1604
1605 case BTA_DM_LINK_UP_EVT:
1606 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001607 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001608
1609 btif_update_remote_version_property(&bd_addr);
1610
The Android Open Source Project5738f832012-12-12 16:00:35 -08001611 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1612 &bd_addr, BT_ACL_STATE_CONNECTED);
1613 break;
1614
1615 case BTA_DM_LINK_DOWN_EVT:
1616 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001617 BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001618 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1619 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1620 break;
1621
1622 case BTA_DM_HW_ERROR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001623 BTIF_TRACE_ERROR("Received H/W Error. ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001624 /* Flush storage data */
1625 btif_config_flush();
1626 usleep(100000); /* 100milliseconds */
1627 /* Killing the process to force a restart as part of fault tolerance */
1628 kill(getpid(), SIGKILL);
1629 break;
1630
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001631#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1632 case BTA_DM_BLE_KEY_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001633 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 -08001634
1635 /* If this pairing is by-product of local initiated GATT client Read or Write,
1636 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1637 have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1638 if (pairing_cb.state != BT_BOND_STATE_BONDING)
1639 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001640 BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001641 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1642 BT_BOND_STATE_BONDING);
1643 }
1644 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1645 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001646 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001647 break;
1648 }
1649
1650 switch (p_data->ble_key.key_type)
1651 {
1652 case BTA_LE_KEY_PENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001653 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001654 pairing_cb.ble.is_penc_key_rcvd = TRUE;
1655 memcpy(pairing_cb.ble.penc_key.ltk,p_data->ble_key.key_value.penc_key.ltk, 16);
1656 memcpy(pairing_cb.ble.penc_key.rand, p_data->ble_key.key_value.penc_key.rand,8);
1657 pairing_cb.ble.penc_key.ediv = p_data->ble_key.key_value.penc_key.ediv;
1658 pairing_cb.ble.penc_key.sec_level = p_data->ble_key.key_value.penc_key.sec_level;
1659
1660 for (i=0; i<16; i++)
1661 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001662 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 -08001663 }
1664 for (i=0; i<8; i++)
1665 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001666 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 -08001667 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001668 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.ediv=0x%04x",pairing_cb.ble.penc_key.ediv);
1669 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.sec_level=0x%02x",pairing_cb.ble.penc_key.sec_level);
1670 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 -08001671 break;
1672
1673 case BTA_LE_KEY_PID:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001674 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001675 pairing_cb.ble.is_pid_key_rcvd = TRUE;
1676 memcpy(pairing_cb.ble.pid_key, p_data->ble_key.key_value.pid_key.irk, 16);
1677 for (i=0; i<16; i++)
1678 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001679 BTIF_TRACE_DEBUG("pairing_cb.ble.pid_key[%d]=0x%02x",i,pairing_cb.ble.pid_key[i]);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001680 }
1681 break;
1682
1683 case BTA_LE_KEY_PCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001684 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001685 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1686 pairing_cb.ble.pcsrk_key.counter = p_data->ble_key.key_value.pcsrk_key.counter;
1687 pairing_cb.ble.pcsrk_key.sec_level = p_data->ble_key.key_value.pcsrk_key.sec_level;
1688 memcpy(pairing_cb.ble.pcsrk_key.csrk,p_data->ble_key.key_value.pcsrk_key.csrk,16);
1689
1690 for (i=0; i<16; i++)
1691 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001692 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 -08001693 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001694 BTIF_TRACE_DEBUG("pairing_cb.ble.pcsrk_key.counter=0x%08x",pairing_cb.ble.pcsrk_key.counter);
1695 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 -08001696 break;
1697
1698 case BTA_LE_KEY_LENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001699 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001700 pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1701 pairing_cb.ble.lenc_key.div = p_data->ble_key.key_value.lenc_key.div;
1702 pairing_cb.ble.lenc_key.key_size = p_data->ble_key.key_value.lenc_key.key_size;
1703 pairing_cb.ble.lenc_key.sec_level = p_data->ble_key.key_value.lenc_key.sec_level;
1704
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001705 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.div=0x%04x",pairing_cb.ble.lenc_key.div);
1706 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.key_size=0x%02x",pairing_cb.ble.lenc_key.key_size);
1707 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 -08001708 break;
1709
1710
1711
1712 case BTA_LE_KEY_LCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001713 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001714 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1715 pairing_cb.ble.lcsrk_key.counter = p_data->ble_key.key_value.lcsrk_key.counter;
1716 pairing_cb.ble.lcsrk_key.div = p_data->ble_key.key_value.lcsrk_key.div;
1717 pairing_cb.ble.lcsrk_key.sec_level = p_data->ble_key.key_value.lcsrk_key.sec_level;
1718
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001719 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.div=0x%04x",pairing_cb.ble.lcsrk_key.div);
1720 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.counter=0x%08x",pairing_cb.ble.lcsrk_key.counter);
1721 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 -08001722
1723 break;
1724
1725 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001726 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001727 break;
1728 }
1729
1730 break;
1731 case BTA_DM_BLE_SEC_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001732 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001733 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1734 break;
1735 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001736 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001737 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1738 break;
1739 case BTA_DM_BLE_PASSKEY_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001740 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001741 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1742 break;
1743 case BTA_DM_BLE_OOB_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001744 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001745 break;
1746 case BTA_DM_BLE_LOCAL_IR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001747 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001748 ble_local_key_cb.is_id_keys_rcvd = TRUE;
1749 memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1750 memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1751 memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1752 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1753 BTIF_DM_LE_LOCAL_KEY_IR,
1754 BT_OCTET16_LEN);
1755 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1756 BTIF_DM_LE_LOCAL_KEY_IRK,
1757 BT_OCTET16_LEN);
1758 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1759 BTIF_DM_LE_LOCAL_KEY_DHK,
1760 BT_OCTET16_LEN);
1761 break;
1762 case BTA_DM_BLE_LOCAL_ER_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001763 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001764 ble_local_key_cb.is_er_rcvd = TRUE;
1765 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1766 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1767 BTIF_DM_LE_LOCAL_KEY_ER,
1768 BT_OCTET16_LEN);
1769 break;
1770
1771 case BTA_DM_BLE_AUTH_CMPL_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001772 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001773 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1774 break;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001775
1776 case BTA_DM_LE_FEATURES_READ:
1777 {
1778 tBTM_BLE_VSC_CB cmn_vsc_cb;
1779 bt_local_le_features_t local_le_features;
1780 char buf[512];
1781 bt_property_t prop;
1782 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1783 prop.val = (void*)buf;
1784 prop.len = sizeof(buf);
1785
1786 /* LE features are not stored in storage. Should be retrived from stack */
1787 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1788 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1789
1790 prop.len = sizeof (bt_local_le_features_t);
1791 if (cmn_vsc_cb.filter_support == 1)
1792 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1793 else
1794 local_le_features.max_adv_filter_supported = 0;
1795 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1796 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1797 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
Satya Callojid773c2c2014-07-29 22:08:55 -07001798 local_le_features.scan_result_storage_size_hibyte =
1799 (cmn_vsc_cb.tot_scan_results_strg >> 8) & (0xFF);
1800 local_le_features.scan_result_storage_size_lobyte =
1801 (cmn_vsc_cb.tot_scan_results_strg) & (0xFF);
Satya Callojiefaddcb2014-07-28 23:22:05 -07001802 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001803 memcpy(prop.val, &local_le_features, prop.len);
1804 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
1805 break;
1806 }
Satya Callojie5ba8842014-07-03 17:18:02 -07001807
1808 case BTA_DM_ENER_INFO_READ:
1809 {
1810 btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param;
1811 bt_activity_energy_info energy_info;
1812 energy_info.status = p_ener_data->status;
1813 energy_info.ctrl_state = p_ener_data->ctrl_state;
1814 energy_info.rx_time = p_ener_data->rx_time;
1815 energy_info.tx_time = p_ener_data->tx_time;
1816 energy_info.idle_time = p_ener_data->idle_time;
1817 energy_info.energy_used = p_ener_data->energy_used;
1818 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info);
1819 break;
1820 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001821#endif
1822
The Android Open Source Project5738f832012-12-12 16:00:35 -08001823 case BTA_DM_AUTHORIZE_EVT:
1824 case BTA_DM_SIG_STRENGTH_EVT:
1825 case BTA_DM_SP_RMT_OOB_EVT:
1826 case BTA_DM_SP_KEYPRESS_EVT:
1827 case BTA_DM_ROLE_CHG_EVT:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001828
The Android Open Source Project5738f832012-12-12 16:00:35 -08001829 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001830 BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001831 break;
1832 }
1833} /* btui_security_cback() */
1834
1835
1836/*******************************************************************************
1837**
1838** Function btif_dm_generic_evt
1839**
1840** Description Executes non-BTA upstream events in BTIF context
1841**
1842** Returns void
1843**
1844*******************************************************************************/
1845static void btif_dm_generic_evt(UINT16 event, char* p_param)
1846{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001847 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001848 switch(event)
1849 {
1850 case BTIF_DM_CB_DISCOVERY_STARTED:
1851 {
1852 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
1853 }
1854 break;
1855
1856 case BTIF_DM_CB_CREATE_BOND:
1857 {
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07001858 btif_dm_create_bond_cb_t *create_bond_cb = (btif_dm_create_bond_cb_t*)p_param;
1859 btif_dm_cb_create_bond(&create_bond_cb->bdaddr, create_bond_cb->transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001860 }
1861 break;
1862
1863 case BTIF_DM_CB_REMOVE_BOND:
1864 {
1865 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
1866 }
1867 break;
1868
1869 case BTIF_DM_CB_HID_REMOTE_NAME:
1870 {
1871 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
1872 }
1873 break;
1874
1875 case BTIF_DM_CB_BOND_STATE_BONDING:
1876 {
1877 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
1878 }
1879 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001880 case BTIF_DM_CB_LE_TX_TEST:
1881 case BTIF_DM_CB_LE_RX_TEST:
1882 {
1883 uint8_t status;
1884 STREAM_TO_UINT8(status, p_param);
1885 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1886 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
1887 }
1888 break;
1889 case BTIF_DM_CB_LE_TEST_END:
1890 {
1891 uint8_t status;
1892 uint16_t count = 0;
1893 STREAM_TO_UINT8(status, p_param);
1894 if (status == 0)
1895 STREAM_TO_UINT16(count, p_param);
1896 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1897 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
1898 }
1899 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001900 default:
1901 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001902 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001903 }
1904 break;
1905 }
1906}
1907
1908/*******************************************************************************
1909**
1910** Function bte_dm_evt
1911**
1912** Description Switches context from BTE to BTIF for all DM events
1913**
1914** Returns void
1915**
1916*******************************************************************************/
1917
1918void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
1919{
1920 bt_status_t status;
1921
1922 /* switch context to btif task context (copy full union size for convenience) */
1923 status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event, (void*)p_data, sizeof(tBTA_DM_SEC), NULL);
1924
1925 /* catch any failed context transfers */
1926 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
1927}
1928
1929/*******************************************************************************
1930**
1931** Function bte_search_devices_evt
1932**
1933** Description Switches context from BTE to BTIF for DM search events
1934**
1935** Returns void
1936**
1937*******************************************************************************/
1938static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1939{
1940 UINT16 param_len = 0;
1941
1942 if (p_data)
1943 param_len += sizeof(tBTA_DM_SEARCH);
1944 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
1945 switch (event)
1946 {
1947 case BTA_DM_INQ_RES_EVT:
1948 {
1949 if (p_data->inq_res.p_eir)
1950 param_len += HCI_EXT_INQ_RESPONSE_LEN;
1951 }
1952 break;
1953
1954 case BTA_DM_DISC_RES_EVT:
1955 {
1956 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
1957 param_len += p_data->disc_res.raw_data_size;
1958 }
1959 break;
1960 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001961 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 -08001962
1963 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
1964 if (event == BTA_DM_INQ_RES_EVT)
1965 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
1966
1967 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
1968 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
1969}
1970
1971/*******************************************************************************
1972**
1973** Function bte_dm_search_services_evt
1974**
1975** Description Switches context from BTE to BTIF for DM search services
1976** event
1977**
1978** Returns void
1979**
1980*******************************************************************************/
1981static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1982{
1983 UINT16 param_len = 0;
1984 if (p_data)
1985 param_len += sizeof(tBTA_DM_SEARCH);
1986 switch (event)
1987 {
1988 case BTA_DM_DISC_RES_EVT:
1989 {
1990 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
1991 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
1992 }
1993 } break;
1994 }
1995 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
1996 * if raw_data is needed. */
1997 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
1998 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
1999}
2000
2001/*******************************************************************************
2002**
2003** Function bte_dm_remote_service_record_evt
2004**
2005** Description Switches context from BTE to BTIF for DM search service
2006** record event
2007**
2008** Returns void
2009**
2010*******************************************************************************/
2011static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2012{
2013 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
2014 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
2015}
2016
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002017#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07002018/*******************************************************************************
2019**
2020** Function bta_energy_info_cb
2021**
2022** Description Switches context from BTE to BTIF for DM energy info event
2023**
2024** Returns void
2025**
2026*******************************************************************************/
2027static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time,
2028 tBTA_DM_BLE_IDLE_TIME_MS idle_time,
2029 tBTA_DM_BLE_ENERGY_USED energy_used,
2030 tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status)
2031{
2032 BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld",
2033 status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
2034
2035 btif_activity_energy_info_cb_t btif_cb;
2036 btif_cb.status = status;
2037 btif_cb.ctrl_state = ctrl_state;
2038 btif_cb.tx_time = (uint64_t) tx_time;
2039 btif_cb.rx_time = (uint64_t) rx_time;
2040 btif_cb.idle_time =(uint64_t) idle_time;
2041 btif_cb.energy_used =(uint64_t) energy_used;
2042 btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
2043 (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL);
2044}
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002045#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07002046
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002047/*******************************************************************************
2048**
2049** Function bte_scan_filt_param_cfg_evt
2050**
2051** Description Scan filter param config event
2052**
2053** Returns void
2054**
2055*******************************************************************************/
2056static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
2057 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
2058 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
2059{
2060 /* This event occurs on calling BTA_DmBleCfgFilterCondition internally,
2061 ** and that is why there is no HAL callback
2062 */
2063 if(BTA_SUCCESS != status)
2064 {
2065 BTIF_TRACE_ERROR("%s, %d", __FUNCTION__, status);
2066 }
2067 else
2068 {
2069 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2070 }
2071}
2072
The Android Open Source Project5738f832012-12-12 16:00:35 -08002073/*****************************************************************************
2074**
2075** btif api functions (no context switch)
2076**
2077*****************************************************************************/
2078
2079/*******************************************************************************
2080**
2081** Function btif_dm_start_discovery
2082**
2083** Description Start device discovery/inquiry
2084**
2085** Returns bt_status_t
2086**
2087*******************************************************************************/
2088bt_status_t btif_dm_start_discovery(void)
2089{
2090 tBTA_DM_INQ inq_params;
2091 tBTA_SERVICE_MASK services = 0;
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002092 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002093
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002094 BTIF_TRACE_EVENT("%s", __FUNCTION__);
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002095
2096#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2097 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
2098 /* Cleanup anything remaining on index 0 */
2099 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
2100 bte_scan_filt_param_cfg_evt, 0);
2101
2102 /* Add an allow-all filter on index 0*/
2103 adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE;
2104 adv_filt_param.feat_seln = ALLOW_ALL_FILTER;
2105 adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;
2106 adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;
2107 adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE;
2108 adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE;
2109 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL,
2110 bte_scan_filt_param_cfg_evt, 0);
2111
The Android Open Source Project5738f832012-12-12 16:00:35 -08002112 /* TODO: Do we need to handle multiple inquiries at the same time? */
2113
2114 /* Set inquiry params and call API */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002115 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
Matthew Xie7f3e4292013-09-30 12:44:10 -07002116#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
2117 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
2118 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
2119 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
2120 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
2121#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002122#else
2123 inq_params.mode = BTA_DM_GENERAL_INQUIRY;
2124#endif
2125 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
2126
2127 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
2128 inq_params.report_dup = TRUE;
2129
2130 inq_params.filter_type = BTA_DM_INQ_CLR;
2131 /* TODO: Filter device by BDA needs to be implemented here */
2132
2133 /* Will be enabled to TRUE once inquiry busy level has been received */
2134 btif_dm_inquiry_in_progress = FALSE;
2135 /* find nearby devices */
2136 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
2137
2138 return BT_STATUS_SUCCESS;
2139}
2140
2141/*******************************************************************************
2142**
2143** Function btif_dm_cancel_discovery
2144**
2145** Description Cancels search
2146**
2147** Returns bt_status_t
2148**
2149*******************************************************************************/
2150bt_status_t btif_dm_cancel_discovery(void)
2151{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002152 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002153 BTA_DmSearchCancel();
2154 return BT_STATUS_SUCCESS;
2155}
2156
2157/*******************************************************************************
2158**
2159** Function btif_dm_create_bond
2160**
2161** Description Initiate bonding with the specified device
2162**
2163** Returns bt_status_t
2164**
2165*******************************************************************************/
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002166bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr, int transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002167{
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002168 btif_dm_create_bond_cb_t create_bond_cb;
2169 create_bond_cb.transport = transport;
2170 bdcpy(create_bond_cb.bdaddr.address, bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002171
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002172 bdstr_t bdstr;
2173 BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __FUNCTION__,
2174 bd2str((bt_bdaddr_t *) bd_addr, &bdstr), transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002175 if (pairing_cb.state != BT_BOND_STATE_NONE)
2176 return BT_STATUS_BUSY;
2177
2178 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002179 (char *)&create_bond_cb, sizeof(btif_dm_create_bond_cb_t), NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002180
2181 return BT_STATUS_SUCCESS;
2182}
2183
2184/*******************************************************************************
2185**
2186** Function btif_dm_cancel_bond
2187**
2188** Description Initiate bonding with the specified device
2189**
2190** Returns bt_status_t
2191**
2192*******************************************************************************/
2193
2194bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
2195{
2196 bdstr_t bdstr;
2197
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002198 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *)bd_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002199
2200 /* TODO:
2201 ** 1. Restore scan modes
2202 ** 2. special handling for HID devices
2203 */
2204 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2205 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002206
2207#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2208
2209 if (pairing_cb.is_ssp)
2210 {
2211 if (pairing_cb.is_le_only)
2212 {
2213 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2214 }
2215 else
Hemant Guptae1468692013-11-14 16:21:29 +05302216 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002217 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
Hemant Guptae1468692013-11-14 16:21:29 +05302218 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2219 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2220 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002221 }
2222 else
2223 {
2224 if (pairing_cb.is_le_only)
2225 {
2226 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2227 }
2228 else
2229 {
2230 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2231 }
2232 /* Cancel bonding, in case it is in ACL connection setup state */
2233 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2234 }
2235
2236#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002237 if (pairing_cb.is_ssp)
2238 {
2239 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2240 }
2241 else
2242 {
2243 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2244 }
2245 /* Cancel bonding, in case it is in ACL connection setup state */
2246 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2247 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002248#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002249 }
2250
2251 return BT_STATUS_SUCCESS;
2252}
2253
2254/*******************************************************************************
2255**
Kim Schulza9eb25c2013-09-30 10:55:52 +02002256** Function btif_dm_hh_open_failed
2257**
2258** Description informs the upper layers if the HH have failed during bonding
2259**
2260** Returns none
2261**
2262*******************************************************************************/
2263
2264void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
2265{
2266 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2267 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2268 {
2269 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2270 }
2271}
2272
2273/*******************************************************************************
2274**
The Android Open Source Project5738f832012-12-12 16:00:35 -08002275** Function btif_dm_remove_bond
2276**
2277** Description Removes bonding with the specified device
2278**
2279** Returns bt_status_t
2280**
2281*******************************************************************************/
2282
2283bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
2284{
2285 bdstr_t bdstr;
2286
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002287 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *)bd_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002288 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2289 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2290
2291 return BT_STATUS_SUCCESS;
2292}
2293
2294/*******************************************************************************
2295**
2296** Function btif_dm_pin_reply
2297**
2298** Description BT legacy pairing - PIN code reply
2299**
2300** Returns bt_status_t
2301**
2302*******************************************************************************/
2303
2304bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2305 uint8_t pin_len, bt_pin_code_t *pin_code)
2306{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002307 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Hemant Gupta831423e2014-01-08 12:42:13 +05302308 if (pin_code == NULL)
2309 return BT_STATUS_FAIL;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002310#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002311
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002312 if (pairing_cb.is_le_only)
2313 {
2314 int i;
2315 UINT32 passkey = 0;
2316 int multi[] = {100000, 10000, 1000, 100, 10,1};
2317 BD_ADDR remote_bd_addr;
2318 bdcpy(remote_bd_addr, bd_addr->address);
2319 for (i = 0; i < 6; i++)
2320 {
2321 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2322 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002323 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002324 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2325
2326 }
2327 else
2328 {
2329 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2330 if (accept)
2331 pairing_cb.pin_code_len = pin_len;
2332 }
2333#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002334 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2335
2336 if (accept)
2337 pairing_cb.pin_code_len = pin_len;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002338#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002339 return BT_STATUS_SUCCESS;
2340}
2341
2342/*******************************************************************************
2343**
2344** Function btif_dm_ssp_reply
2345**
2346** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2347**
2348** Returns bt_status_t
2349**
2350*******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002351bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2352 bt_ssp_variant_t variant, uint8_t accept,
2353 uint32_t passkey)
2354{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08002355 UNUSED(passkey);
2356
The Android Open Source Project5738f832012-12-12 16:00:35 -08002357 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2358 {
2359 /* This is not implemented in the stack.
2360 * For devices with display, this is not needed
2361 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002362 BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002363 return BT_STATUS_FAIL;
2364 }
2365 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002366 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002367#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2368 if (pairing_cb.is_le_only)
2369 {
2370 if (accept)
2371 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2372 else
2373 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2374 }
2375 else
2376 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002377
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002378#else
2379 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2380#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002381 return BT_STATUS_SUCCESS;
2382}
2383
2384/*******************************************************************************
2385**
2386** Function btif_dm_get_adapter_property
2387**
2388** Description Queries the BTA for the adapter property
2389**
2390** Returns bt_status_t
2391**
2392*******************************************************************************/
2393bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2394{
2395 bt_status_t status;
2396
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002397 BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002398 switch (prop->type)
2399 {
2400 case BT_PROPERTY_BDNAME:
2401 {
2402 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
Matthew Xie1e5109b2012-11-09 18:26:26 -08002403 strcpy((char *)bd_name->name, btif_get_default_local_name());
The Android Open Source Project5738f832012-12-12 16:00:35 -08002404 prop->len = strlen((char *)bd_name->name);
2405 }
2406 break;
2407
2408 case BT_PROPERTY_ADAPTER_SCAN_MODE:
2409 {
2410 /* if the storage does not have it. Most likely app never set it. Default is NONE */
2411 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2412 *mode = BT_SCAN_MODE_NONE;
2413 prop->len = sizeof(bt_scan_mode_t);
2414 }
2415 break;
2416
2417 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2418 {
2419 uint32_t *tmt = (uint32_t*)prop->val;
2420 *tmt = 120; /* default to 120s, if not found in NV */
2421 prop->len = sizeof(uint32_t);
2422 }
2423 break;
2424
2425 default:
2426 prop->len = 0;
2427 return BT_STATUS_FAIL;
2428 }
2429 return BT_STATUS_SUCCESS;
2430}
2431
2432/*******************************************************************************
2433**
2434** Function btif_dm_get_remote_services
2435**
2436** Description Start SDP to get remote services
2437**
2438** Returns bt_status_t
2439**
2440*******************************************************************************/
2441bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2442{
2443 bdstr_t bdstr;
2444
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002445 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002446
2447 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2448 bte_dm_search_services_evt, TRUE);
2449
2450 return BT_STATUS_SUCCESS;
2451}
2452
2453/*******************************************************************************
2454**
2455** Function btif_dm_get_remote_service_record
2456**
2457** Description Start SDP to get remote service record
2458**
2459**
2460** Returns bt_status_t
2461*******************************************************************************/
2462bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2463 bt_uuid_t *uuid)
2464{
2465 tSDP_UUID sdp_uuid;
2466 bdstr_t bdstr;
2467
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002468 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002469
2470 sdp_uuid.len = MAX_UUID_SIZE;
2471 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2472
2473 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2474 bte_dm_remote_service_record_evt, TRUE);
2475
2476 return BT_STATUS_SUCCESS;
2477}
2478
2479void btif_dm_execute_service_request(UINT16 event, char *p_param)
2480{
2481 BOOLEAN b_enable = FALSE;
2482 bt_status_t status;
2483 if (event == BTIF_DM_ENABLE_SERVICE)
2484 {
2485 b_enable = TRUE;
2486 }
2487 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2488 if (status == BT_STATUS_SUCCESS)
2489 {
2490 bt_property_t property;
2491 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2492
2493 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2494 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2495 sizeof(local_uuids), local_uuids);
2496 btif_storage_get_adapter_property(&property);
2497 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2498 BT_STATUS_SUCCESS, 1, &property);
2499 }
2500 return;
2501}
2502
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002503void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2504 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2505{
2506 UINT8 yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2507 /* if local initiated:
2508 ** 1. set DD + MITM
2509 ** if remote initiated:
2510 ** 1. Copy over the auth_req from peer's io_rsp
2511 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2512 ** as a fallback set MITM+GB if peer had MITM set
2513 */
2514 UNUSED (bd_addr);
2515 UNUSED (p_io_cap);
2516 UNUSED (p_oob_data);
2517
2518
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002519 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002520 if(pairing_cb.is_local_initiated)
2521 {
2522 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2523 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2524 }
2525 else if (!is_orig)
2526 {
2527 /* peer initiated paring. They probably know what they want.
2528 ** Copy the mitm from peer device.
2529 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002530 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002531 __FUNCTION__, pairing_cb.auth_req);
2532 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2533
2534 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2535 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2536 *p_auth_req |= BTA_AUTH_SP_YES;
2537 }
2538 else if (yes_no_bit)
2539 {
2540 /* set the general bonding bit for stored device */
2541 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2542 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002543 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002544}
2545
2546void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2547 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2548{
2549 UNUSED (bd_addr);
2550 UNUSED (oob_data);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002551
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002552 if(auth_req & BTA_AUTH_BONDS)
2553 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002554 BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002555 pairing_cb.auth_req = auth_req;
2556 pairing_cb.io_cap = io_cap;
2557 }
2558}
2559
The Android Open Source Project5738f832012-12-12 16:00:35 -08002560#if (BTM_OOB_INCLUDED == TRUE)
2561void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data)
2562{
2563 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2564 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2565 {
2566 *p_oob_data = FALSE;
2567 }
2568 else
2569 {
2570 *p_oob_data = TRUE;
2571 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002572 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 -08002573}
2574#endif /* BTM_OOB_INCLUDED */
2575
2576#ifdef BTIF_DM_OOB_TEST
2577void btif_dm_load_local_oob(void)
2578{
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002579 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002580 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002581 BTIF_TRACE_DEBUG("btif_dm_load_local_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002582 if (prop_oob[0] != '3')
2583 {
2584#if (BTM_OOB_INCLUDED == TRUE)
2585 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2586 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2587 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002588 BTIF_TRACE_DEBUG("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002589 BTA_DmLocalOob();
2590 }
2591#else
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002592 BTIF_TRACE_ERROR("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002593#endif
2594 }
2595}
2596
2597void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2598{
2599 FILE *fp;
2600 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2601 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2602 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002603 char prop_oob[PROPERTY_VALUE_MAX];
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002604 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: valid=%d", valid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002605 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2606 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2607 valid)
2608 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002609 BTIF_TRACE_DEBUG("save local OOB data in memory");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002610 memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2611 memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2612 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002613 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002614 if (prop_oob[0] == '1')
2615 path = path_a;
2616 else if (prop_oob[0] == '2')
2617 path = path_b;
2618 if (path)
2619 {
2620 fp = fopen(path, "wb+");
2621 if (fp == NULL)
2622 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002623 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 -08002624 }
2625 else
2626 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002627 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 -08002628 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2629 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2630 fclose(fp);
2631 }
2632 }
2633 }
2634}
2635BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2636{
2637 char t[128];
2638 FILE *fp;
2639 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2640 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2641 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002642 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002643 BOOLEAN result = FALSE;
2644 bt_bdaddr_t bt_bd_addr;
2645 bdcpy(oob_cb.oob_bdaddr, bd_addr);
2646 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002647 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002648 if (prop_oob[0] == '1')
2649 path = path_b;
2650 else if (prop_oob[0] == '2')
2651 path = path_a;
2652 if (path)
2653 {
2654 fp = fopen(path, "rb");
2655 if (fp == NULL)
2656 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002657 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 -08002658 return FALSE;
2659 }
2660 else
2661 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002662 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob: read OOB data from %s",path);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002663 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2664 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2665 fclose(fp);
2666 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002667 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: TRUE");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002668 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2669 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2670 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002671 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002672 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2673 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
2674 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 -07002675 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: c = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002676 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2677 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
2678 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 -07002679 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: r = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002680 bdcpy(bt_bd_addr.address, bd_addr);
2681 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2682 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2683 result = TRUE;
2684 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002685 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob result=%d",result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002686 return result;
2687}
2688#endif /* BTIF_DM_OOB_TEST */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002689#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2690
2691static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2692{
2693 bt_bdaddr_t bd_addr;
2694 bt_bdname_t bd_name;
2695 UINT32 cod;
2696
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002697 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002698
2699 /* Remote name update */
2700 btif_update_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2701 NULL, BT_DEVICE_TYPE_BLE);
2702 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2703 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2704
2705 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2706 pairing_cb.is_ssp = FALSE;
2707 cod = COD_UNCLASSIFIED;
2708
2709 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2710 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2711 p_ssp_key_notif->passkey);
2712}
2713
2714/*******************************************************************************
2715**
2716** Function btif_dm_ble_auth_cmpl_evt
2717**
2718** Description Executes authentication complete event in btif context
2719**
2720** Returns void
2721**
2722*******************************************************************************/
2723static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2724{
2725 /* Save link key, if not temporary */
2726 bt_bdaddr_t bd_addr;
2727 bt_status_t status = BT_STATUS_FAIL;
2728 bt_bond_state_t state = BT_BOND_STATE_NONE;
2729
2730 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2731 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2732 {
2733 /* store keys */
2734 }
2735 if (p_auth_cmpl->success)
2736 {
2737 status = BT_STATUS_SUCCESS;
2738 state = BT_BOND_STATE_BONDED;
2739
2740 btif_dm_save_ble_bonding_keys();
2741 BTA_GATTC_Refresh(bd_addr.address);
2742 btif_dm_get_remote_services(&bd_addr);
2743 }
2744 else
2745 {
2746 /*Map the HCI fail reason to bt status */
2747 switch (p_auth_cmpl->fail_reason)
2748 {
Priti Aghera156c52b2014-07-09 14:58:19 -07002749 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
2750 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
2751 btif_dm_remove_ble_bonding_keys();
2752 status = BT_STATUS_AUTH_FAILURE;
2753 break;
2754 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
2755 status = BT_STATUS_AUTH_REJECTED;
2756 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002757 default:
Andre Eisenbachca22ac42013-02-13 17:02:11 +09002758 btif_dm_remove_ble_bonding_keys();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002759 status = BT_STATUS_FAIL;
2760 break;
2761 }
2762 }
2763 bond_state_changed(status, &bd_addr, state);
2764}
2765
2766
2767
2768void btif_dm_load_ble_local_keys(void)
2769{
2770 bt_status_t bt_status;
2771
2772 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2773
2774 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2775 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2776 {
2777 ble_local_key_cb.is_er_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002778 BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002779 }
2780
2781 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
2782 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
2783 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
2784 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
2785 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
2786 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
2787 {
2788 ble_local_key_cb.is_id_keys_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002789 BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002790 }
2791
2792}
2793void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
2794 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
2795{
2796 if (ble_local_key_cb.is_er_rcvd )
2797 {
2798 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2799 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2800 }
2801
2802 if (ble_local_key_cb.is_id_keys_rcvd)
2803 {
2804 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
2805 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
2806 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
2807 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
2808 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002809 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002810}
2811
2812void btif_dm_save_ble_bonding_keys(void)
2813{
2814
2815 bt_bdaddr_t bd_addr;
2816
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002817 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002818
2819 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2820
2821 if (pairing_cb.ble.is_penc_key_rcvd)
2822 {
2823 btif_storage_add_ble_bonding_key(&bd_addr,
2824 (char *) &pairing_cb.ble.penc_key,
2825 BTIF_DM_LE_KEY_PENC,
2826 sizeof(btif_dm_ble_penc_keys_t));
2827 }
2828
2829 if (pairing_cb.ble.is_pid_key_rcvd)
2830 {
2831 btif_storage_add_ble_bonding_key(&bd_addr,
2832 (char *) &pairing_cb.ble.pid_key[0],
2833 BTIF_DM_LE_KEY_PID,
2834 BT_OCTET16_LEN);
2835 }
2836
2837
2838 if (pairing_cb.ble.is_pcsrk_key_rcvd)
2839 {
2840 btif_storage_add_ble_bonding_key(&bd_addr,
2841 (char *) &pairing_cb.ble.pcsrk_key,
2842 BTIF_DM_LE_KEY_PCSRK,
2843 sizeof(btif_dm_ble_pcsrk_keys_t));
2844 }
2845
2846
2847 if (pairing_cb.ble.is_lenc_key_rcvd)
2848 {
2849 btif_storage_add_ble_bonding_key(&bd_addr,
2850 (char *) &pairing_cb.ble.lenc_key,
2851 BTIF_DM_LE_KEY_LENC,
2852 sizeof(btif_dm_ble_lenc_keys_t));
2853 }
2854
2855 if (pairing_cb.ble.is_lcsrk_key_rcvd)
2856 {
2857 btif_storage_add_ble_bonding_key(&bd_addr,
2858 (char *) &pairing_cb.ble.lcsrk_key,
2859 BTIF_DM_LE_KEY_LCSRK,
2860 sizeof(btif_dm_ble_lcsrk_keys_t));
2861 }
2862
2863}
2864
2865
2866void btif_dm_remove_ble_bonding_keys(void)
2867{
2868 bt_bdaddr_t bd_addr;
2869
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002870 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002871
2872 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2873 btif_storage_remove_ble_bonding_keys(&bd_addr);
2874}
2875
2876
2877/*******************************************************************************
2878**
2879** Function btif_dm_ble_sec_req_evt
2880**
2881** Description Eprocess security request event in btif context
2882**
2883** Returns void
2884**
2885*******************************************************************************/
2886void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
2887{
2888 bt_bdaddr_t bd_addr;
2889 bt_bdname_t bd_name;
2890 UINT32 cod;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002891 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002892
2893 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2894 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002895 BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002896 return;
2897 }
2898
2899 /* Remote name update */
2900 btif_update_remote_properties(p_ble_req->bd_addr,p_ble_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
2901
2902 bdcpy(bd_addr.address, p_ble_req->bd_addr);
2903 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
2904
2905 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2906
2907 pairing_cb.is_temp = FALSE;
2908 pairing_cb.is_le_only = TRUE;
2909 pairing_cb.is_ssp = TRUE;
2910
2911 cod = COD_UNCLASSIFIED;
2912
2913 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
2914 BT_SSP_VARIANT_CONSENT, 0);
2915}
2916
2917
2918
2919/*******************************************************************************
2920**
2921** Function btif_dm_ble_passkey_req_evt
2922**
2923** Description Executes pin request event in btif context
2924**
2925** Returns void
2926**
2927*******************************************************************************/
2928static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
2929{
2930 bt_bdaddr_t bd_addr;
2931 bt_bdname_t bd_name;
2932 UINT32 cod;
2933
2934 /* Remote name update */
2935 btif_update_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
2936
2937 bdcpy(bd_addr.address, p_pin_req->bd_addr);
2938 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
2939
2940 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2941 pairing_cb.is_le_only = TRUE;
2942
2943 cod = COD_UNCLASSIFIED;
2944
2945 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
2946 &bd_addr, &bd_name, cod);
2947}
2948
2949
2950void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
2951 tBT_DEVICE_TYPE dev_type)
2952{
2953 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
2954}
2955
2956static void btif_dm_ble_tx_test_cback(void *p)
2957{
2958 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
2959 (char *)p, 1, NULL);
2960}
2961
2962static void btif_dm_ble_rx_test_cback(void *p)
2963{
2964 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
2965 (char *)p, 1, NULL);
2966}
2967
2968static void btif_dm_ble_test_end_cback(void *p)
2969{
2970 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
2971 (char *)p, 3, NULL);
2972}
2973/*******************************************************************************
2974**
2975** Function btif_le_test_mode
2976**
2977** Description Sends a HCI BLE Test command to the Controller
2978**
2979** Returns BT_STATUS_SUCCESS on success
2980**
2981*******************************************************************************/
2982bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
2983{
2984 switch (opcode) {
2985 case HCI_BLE_TRANSMITTER_TEST:
2986 if (len != 3) return BT_STATUS_PARM_INVALID;
2987 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
2988 break;
2989 case HCI_BLE_RECEIVER_TEST:
2990 if (len != 1) return BT_STATUS_PARM_INVALID;
2991 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
2992 break;
2993 case HCI_BLE_TEST_END:
2994 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
2995 break;
2996 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002997 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002998 return BT_STATUS_UNSUPPORTED;
2999 }
3000 return BT_STATUS_SUCCESS;
3001}
3002
3003#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08003004
3005void btif_dm_on_disable()
3006{
3007 /* cancel any pending pairing requests */
3008 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3009 {
3010 bt_bdaddr_t bd_addr;
3011
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003012 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003013 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3014 btif_dm_cancel_bond(&bd_addr);
3015 }
3016}
Matthew Xie1e5109b2012-11-09 18:26:26 -08003017
Satya Callojie5ba8842014-07-03 17:18:02 -07003018/*******************************************************************************
3019**
3020** Function btif_dm_read_energy_info
3021**
3022** Description Reads the energy info from controller
3023**
3024** Returns void
3025**
3026*******************************************************************************/
3027void btif_dm_read_energy_info()
3028{
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003029#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07003030 BTA_DmBleGetEnergyInfo(bta_energy_info_cb);
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003031#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07003032}
3033
Matthew Xie1e5109b2012-11-09 18:26:26 -08003034static char* btif_get_default_local_name() {
3035 if (btif_default_local_name[0] == '\0')
3036 {
3037 int max_len = sizeof(btif_default_local_name) - 1;
3038 if (BTM_DEF_LOCAL_NAME[0] != '\0')
3039 {
3040 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
3041 }
3042 else
3043 {
3044 char prop_model[PROPERTY_VALUE_MAX];
3045 property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
3046 strncpy(btif_default_local_name, prop_model, max_len);
3047 }
3048 btif_default_local_name[max_len] = '\0';
3049 }
3050 return btif_default_local_name;
3051}