blob: 4bca265a55323f02172d9127d14bd3ae46be3a1d [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);
Matthew Xie64c54792014-09-16 00:55:03 -0700601 if (transport == BT_TRANSPORT_LE)
602 {
603 if (!btif_config_get_int("Remote", (char const *)&bdstr,"DevType", &device_type))
604 {
605 btif_config_set_int("Remote", bdstr, "DevType", BT_DEVICE_TYPE_BLE);
606 }
607 if (btif_storage_get_remote_addr_type(bd_addr, &addr_type) != BT_STATUS_SUCCESS)
608 {
609 btif_storage_set_remote_addr_type(bd_addr, BLE_ADDR_PUBLIC);
610 }
611 }
lungtsai_lin3baff792014-08-29 13:47:47 +0800612 if((btif_config_get_int("Remote", (char const *)&bdstr,"DevType", &device_type) &&
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800613 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
lungtsai_lin3baff792014-08-29 13:47:47 +0800614 (device_type == BT_DEVICE_TYPE_BLE)) || (transport == BT_TRANSPORT_LE))
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800615 {
616 BTA_DmAddBleDevice(bd_addr->address, addr_type, BT_DEVICE_TYPE_BLE);
617 }
618#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800619
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800620#if BLE_INCLUDED == TRUE
621 if(is_hid && device_type != BT_DEVICE_TYPE_BLE)
622#else
623 if(is_hid)
624#endif
625 {
626 int status;
627 status = btif_hh_connect(bd_addr);
628 if(status != BT_STATUS_SUCCESS)
629 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800630 }
631 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800632 {
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700633 BTA_DmBondByTransport((UINT8 *)bd_addr->address, transport);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800634 }
635 /* Track originator of bond creation */
636 pairing_cb.is_local_initiated = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800637
638}
639
640/*******************************************************************************
641**
642** Function btif_dm_cb_remove_bond
643**
644** Description remove bond initiated from the BTIF thread context
645** Special handling for HID devices
646**
647** Returns void
648**
649*******************************************************************************/
650void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
651{
652 bdstr_t bdstr;
653 /*special handling for HID devices */
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700654 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there
655 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
656#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
657 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
658#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800659 {
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700660 BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800661 }
662}
663
664/*******************************************************************************
665**
Andre Eisenbach249f6002014-06-18 12:20:37 -0700666** Function btif_dm_get_connection_state
667**
668** Description Returns whether the remote device is currently connected
669**
670** Returns 0 if not connected
671**
672*******************************************************************************/
673uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr)
674{
675 return BTA_DmGetConnectionState((UINT8 *)bd_addr->address);
676}
677
678/*******************************************************************************
679**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800680** Function search_devices_copy_cb
681**
682** Description Deep copy callback for search devices event
683**
684** Returns void
685**
686*******************************************************************************/
687static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
688{
689 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
690 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
691
692 if (!p_src)
693 return;
694
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700695 BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800696 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
697 switch (event)
698 {
699 case BTA_DM_INQ_RES_EVT:
700 {
701 if (p_src_data->inq_res.p_eir)
702 {
703 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
704 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
705 }
706 }
707 break;
708
709 case BTA_DM_DISC_RES_EVT:
710 {
711 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
712 {
713 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
714 memcpy(p_dest_data->disc_res.p_raw_data,
715 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
716 }
717 }
718 break;
719 }
720}
721
722static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
723{
724 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
725 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
726
727 if (!p_src)
728 return;
729 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
730 switch (event)
731 {
732 case BTA_DM_DISC_RES_EVT:
733 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530734 if (p_src_data->disc_res.result == BTA_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800735 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530736 if (p_src_data->disc_res.num_uuids > 0)
737 {
738 p_dest_data->disc_res.p_uuid_list =
739 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
740 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
741 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
742 GKI_freebuf(p_src_data->disc_res.p_uuid_list);
743 }
744 if (p_src_data->disc_res.p_raw_data != NULL)
745 {
746 GKI_freebuf(p_src_data->disc_res.p_raw_data);
747 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800748 }
749 } break;
750 }
751}
752/******************************************************************************
753**
754** BTIF DM callback events
755**
756*****************************************************************************/
757
758/*******************************************************************************
759**
760** Function btif_dm_pin_req_evt
761**
762** Description Executes pin request event in btif context
763**
764** Returns void
765**
766*******************************************************************************/
767static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
768{
769 bt_bdaddr_t bd_addr;
770 bt_bdname_t bd_name;
771 UINT32 cod;
772 bt_pin_code_t pin_code;
773
774 /* Remote properties update */
775 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
776 p_pin_req->dev_class, BT_DEVICE_TYPE_BREDR);
777
778 bdcpy(bd_addr.address, p_pin_req->bd_addr);
779 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
780
781 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
782
783 cod = devclass2uint(p_pin_req->dev_class);
784
785 if ( cod == 0) {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700786 BTIF_TRACE_DEBUG("%s():cod is 0, set as unclassified", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800787 cod = COD_UNCLASSIFIED;
788 }
789
790 /* check for auto pair possiblity only if bond was initiated by local device */
791 if (pairing_cb.is_local_initiated)
792 {
793 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
794 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
795 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
796 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
797 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
798 check_cod(&bd_addr, COD_HID_POINTING))
799 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700800 BTIF_TRACE_DEBUG("%s()cod matches for auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800801 /* Check if this device can be auto paired */
802 if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) &&
803 (pairing_cb.autopair_attempts == 0))
804 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700805 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800806 pin_code.pin[0] = 0x30;
807 pin_code.pin[1] = 0x30;
808 pin_code.pin[2] = 0x30;
809 pin_code.pin[3] = 0x30;
810
811 pairing_cb.autopair_attempts++;
812 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
813 return;
814 }
815 }
816 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
817 check_cod(&bd_addr, COD_HID_COMBO))
818 {
819 if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) &&
820 (pairing_cb.autopair_attempts == 0))
821 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700822 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800823 pin_code.pin[0] = 0x30;
824 pin_code.pin[1] = 0x30;
825 pin_code.pin[2] = 0x30;
826 pin_code.pin[3] = 0x30;
827
828 pairing_cb.autopair_attempts++;
829 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
830 return;
831 }
832 }
833 }
834 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
835 &bd_addr, &bd_name, cod);
836}
837
838/*******************************************************************************
839**
840** Function btif_dm_ssp_cfm_req_evt
841**
842** Description Executes SSP confirm request event in btif context
843**
844** Returns void
845**
846*******************************************************************************/
847static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
848{
849 bt_bdaddr_t bd_addr;
850 bt_bdname_t bd_name;
851 UINT32 cod;
852 BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
853
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700854 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800855
856 /* Remote properties update */
857 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
858 p_ssp_cfm_req->dev_class, BT_DEVICE_TYPE_BREDR);
859
860 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
861 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
862
863 /* Set the pairing_cb based on the local & remote authentication requirements */
864 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
865
866 /* if just_works and bonding bit is not set treat this as temporary */
867 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 -0800868 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
869 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800870 pairing_cb.is_temp = TRUE;
871 else
872 pairing_cb.is_temp = FALSE;
873
874 pairing_cb.is_ssp = TRUE;
875
876 /* If JustWorks auto-accept */
877 if (p_ssp_cfm_req->just_works)
878 {
879 /* Pairing consent for JustWorks needed if:
880 * 1. Incoming pairing is detected AND
881 * 2. local IO capabilities are DisplayYesNo AND
882 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
883 */
884 if ((is_incoming) && ((p_ssp_cfm_req->loc_io_caps == 0x01) &&
885 (p_ssp_cfm_req->rmt_io_caps == 0x00 || p_ssp_cfm_req->rmt_io_caps == 0x03)))
886 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700887 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 -0800888 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
889 }
890 else
891 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700892 BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800893 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
894 return;
895 }
896 }
897
898 cod = devclass2uint(p_ssp_cfm_req->dev_class);
899
900 if ( cod == 0) {
901 ALOGD("cod is 0, set as unclassified");
902 cod = COD_UNCLASSIFIED;
903 }
904
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700905 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800906 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
907 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
908 p_ssp_cfm_req->num_val);
909}
910
911static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
912{
913 bt_bdaddr_t bd_addr;
914 bt_bdname_t bd_name;
915 UINT32 cod;
916
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700917 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800918
919 /* Remote properties update */
920 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
921 p_ssp_key_notif->dev_class, BT_DEVICE_TYPE_BREDR);
922
923 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
924 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
925
926 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
927 pairing_cb.is_ssp = TRUE;
928 cod = devclass2uint(p_ssp_key_notif->dev_class);
929
930 if ( cod == 0) {
931 ALOGD("cod is 0, set as unclassified");
932 cod = COD_UNCLASSIFIED;
933 }
934
935 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
936 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
937 p_ssp_key_notif->passkey);
938}
939/*******************************************************************************
940**
941** Function btif_dm_auth_cmpl_evt
942**
943** Description Executes authentication complete event in btif context
944**
945** Returns void
946**
947*******************************************************************************/
948static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
949{
950 /* Save link key, if not temporary */
951 bt_bdaddr_t bd_addr;
952 bt_status_t status = BT_STATUS_FAIL;
953 bt_bond_state_t state = BT_BOND_STATE_NONE;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700954 BOOLEAN skip_sdp = FALSE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800955
956 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
957 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
958 {
959 if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) || (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
960 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) || (!pairing_cb.is_temp))
961 {
962 bt_status_t ret;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700963 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, is_temp=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800964 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.is_temp);
965 ret = btif_storage_add_bonded_device(&bd_addr,
966 p_auth_cmpl->key, p_auth_cmpl->key_type,
967 pairing_cb.pin_code_len);
968 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
969 }
970 else
971 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700972 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 -0800973 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.is_temp);
Hemant Guptab820aec2013-12-24 19:59:57 +0530974 if(pairing_cb.is_temp)
975 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700976 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
Hemant Guptab820aec2013-12-24 19:59:57 +0530977 __FUNCTION__);
978 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
979 return;
980 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800981 }
982 }
Priti Agherac0edf9f2014-06-26 11:23:51 -0700983
984 // Skip SDP for certain HID Devices
The Android Open Source Project5738f832012-12-12 16:00:35 -0800985 if (p_auth_cmpl->success)
986 {
987 status = BT_STATUS_SUCCESS;
988 state = BT_BOND_STATE_BONDED;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700989 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800990
Priti Agherac0edf9f2014-06-26 11:23:51 -0700991 if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr, COD_HID_MAJOR))
992 {
993 ALOGW("%s:skip SDP",
994 __FUNCTION__);
995 skip_sdp = TRUE;
996 }
997 if(!pairing_cb.is_local_initiated && skip_sdp)
998 {
999 bond_state_changed(status, &bd_addr, state);
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -08001000
Priti Agherac0edf9f2014-06-26 11:23:51 -07001001 ALOGW("%s: Incoming HID Connection",__FUNCTION__);
1002 bt_property_t prop;
1003 bt_bdaddr_t bd_addr;
1004 bt_uuid_t uuid;
1005 char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE;
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -08001006
Priti Agherac0edf9f2014-06-26 11:23:51 -07001007 string_to_uuid(uuid_str, &uuid);
1008
1009 prop.type = BT_PROPERTY_UUIDS;
1010 prop.val = uuid.uu;
1011 prop.len = MAX_UUID_SIZE;
1012
1013 /* Send the event to the BTIF */
1014 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1015 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1016 }
1017 else
1018 {
1019 /* Trigger SDP on the device */
1020 pairing_cb.sdp_attempts = 1;;
1021
1022 if(btif_dm_inquiry_in_progress)
1023 btif_dm_cancel_discovery();
1024
1025 btif_dm_get_remote_services(&bd_addr);
1026 }
1027 /* 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 -08001028 }
1029 else
1030 {
1031 /*Map the HCI fail reason to bt status */
1032 switch(p_auth_cmpl->fail_reason)
1033 {
1034 case HCI_ERR_PAGE_TIMEOUT:
1035 case HCI_ERR_CONNECTION_TOUT:
1036 status = BT_STATUS_RMT_DEV_DOWN;
1037 break;
1038
Hemant Guptaaef7a672013-07-31 19:00:12 +05301039 case HCI_ERR_PAIRING_NOT_ALLOWED:
1040 status = BT_STATUS_AUTH_REJECTED;
1041 break;
1042
Hemant Guptab4801442014-01-07 18:11:15 +05301043 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
1044 status = BT_STATUS_AUTH_FAILURE;
1045 break;
1046
The Android Open Source Project5738f832012-12-12 16:00:35 -08001047 /* map the auth failure codes, so we can retry pairing if necessary */
1048 case HCI_ERR_AUTH_FAILURE:
Hemant Gupta59a88ec2014-03-19 19:01:35 +05301049 case HCI_ERR_KEY_MISSING:
Zhihai Xua7ea8092013-11-27 14:10:53 +05301050 btif_storage_remove_bonded_device(&bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001051 case HCI_ERR_HOST_REJECT_SECURITY:
1052 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
1053 case HCI_ERR_UNIT_KEY_USED:
1054 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
1055 case HCI_ERR_INSUFFCIENT_SECURITY:
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301056 case HCI_ERR_PEER_USER:
1057 case HCI_ERR_UNSPECIFIED:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001058 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d",
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301059 __FUNCTION__, p_auth_cmpl->fail_reason);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001060 if (pairing_cb.autopair_attempts == 1)
1061 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001062 BTIF_TRACE_DEBUG("%s(): Adding device to blacklist ", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001063
1064 /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class */
1065 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
1066 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
1067 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
1068 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
1069 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
1070 check_cod(&bd_addr, COD_HID_POINTING))
1071 {
1072 btif_storage_add_device_to_autopair_blacklist (&bd_addr);
1073 }
1074 pairing_cb.autopair_attempts++;
1075
1076 /* Create the Bond once again */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001077 BTIF_TRACE_DEBUG("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07001078 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001079 return;
1080 }
1081 else
1082 {
1083 /* if autopair attempts are more than 1, or not attempted */
1084 status = BT_STATUS_AUTH_FAILURE;
1085 }
1086 break;
1087
1088 default:
1089 status = BT_STATUS_FAIL;
1090 }
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301091 /* Special Handling for HID Devices */
1092 if (check_cod(&bd_addr, COD_HID_POINTING)) {
1093 /* Remove Device as bonded in nvram as authentication failed */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001094 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __FUNCTION__);
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301095 btif_storage_remove_bonded_device(&bd_addr);
1096 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001097 bond_state_changed(status, &bd_addr, state);
1098 }
1099}
1100
1101/******************************************************************************
1102**
1103** Function btif_dm_search_devices_evt
1104**
1105** Description Executes search devices callback events in btif context
1106**
1107** Returns void
1108**
1109******************************************************************************/
1110static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
1111{
1112 tBTA_DM_SEARCH *p_search_data;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001113 BTIF_TRACE_EVENT("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001114
1115 switch (event)
1116 {
1117 case BTA_DM_DISC_RES_EVT:
1118 {
1119 p_search_data = (tBTA_DM_SEARCH *)p_param;
1120 /* Remote name update */
1121 if (strlen((const char *) p_search_data->disc_res.bd_name))
1122 {
1123 bt_property_t properties[1];
1124 bt_bdaddr_t bdaddr;
1125 bt_status_t status;
1126
1127 properties[0].type = BT_PROPERTY_BDNAME;
1128 properties[0].val = p_search_data->disc_res.bd_name;
1129 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
1130 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
1131
1132 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
1133 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
1134 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1135 status, &bdaddr, 1, properties);
1136 }
1137 /* TODO: Services? */
1138 }
1139 break;
1140
1141 case BTA_DM_INQ_RES_EVT:
1142 {
1143 /* inquiry result */
1144 UINT32 cod;
1145 UINT8 *p_eir_remote_name = NULL;
1146 bt_bdname_t bdname;
1147 bt_bdaddr_t bdaddr;
1148 UINT8 remote_name_len;
1149 UINT8 *p_cached_name = NULL;
1150 tBTA_SERVICE_MASK services = 0;
1151 bdstr_t bdstr;
1152
1153 p_search_data = (tBTA_DM_SEARCH *)p_param;
1154 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
1155
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001156 BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __FUNCTION__, bd2str(&bdaddr, &bdstr),
The Android Open Source Project5738f832012-12-12 16:00:35 -08001157#if (BLE_INCLUDED == TRUE)
1158 p_search_data->inq_res.device_type);
1159#else
1160 BT_DEVICE_TYPE_BREDR);
1161#endif
1162 bdname.name[0] = 0;
1163
1164 cod = devclass2uint (p_search_data->inq_res.dev_class);
1165
1166 if ( cod == 0) {
1167 ALOGD("cod is 0, set as unclassified");
1168 cod = COD_UNCLASSIFIED;
1169 }
1170
1171 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1172 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1173
1174 /* Check EIR for remote name and services */
1175 if (p_search_data->inq_res.p_eir)
1176 {
1177 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001178 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001179 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */
1180 }
1181
1182
1183 {
1184 bt_property_t properties[5];
1185 bt_device_type_t dev_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001186 UINT8 addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001187 uint32_t num_properties = 0;
1188 bt_status_t status;
1189
1190 memset(properties, 0, sizeof(properties));
1191 /* BD_ADDR */
1192 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1193 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1194 num_properties++;
1195 /* BD_NAME */
1196 /* Don't send BDNAME if it is empty */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001197 if (bdname.name[0])
1198 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001199 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1200 BT_PROPERTY_BDNAME,
1201 strlen((char *)bdname.name), &bdname);
1202 num_properties++;
1203 }
1204
1205 /* DEV_CLASS */
1206 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1207 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1208 num_properties++;
1209 /* DEV_TYPE */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001210#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001211 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1212 dev_type = p_search_data->inq_res.device_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001213 addr_type = p_search_data->inq_res.ble_addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001214#else
1215 dev_type = BT_DEVICE_TYPE_BREDR;
1216#endif
1217 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1218 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1219 num_properties++;
1220 /* RSSI */
1221 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1222 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1223 &(p_search_data->inq_res.rssi));
1224 num_properties++;
1225
1226 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1227 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001228#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1229 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001230 if (( dev_type == BT_DEVICE_TYPE_DUMO)&&
1231 (p_search_data->inq_res.flag & BTA_BLE_DMT_CONTROLLER_SPT) &&
1232 (p_search_data->inq_res.flag & BTA_BLE_DMT_HOST_SPT))
1233 {
1234 btif_storage_set_dmt_support_type (&bdaddr, TRUE);
1235 }
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001236 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1237#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001238 /* Callback to notify upper layer of device */
1239 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1240 num_properties, properties);
1241 }
1242 }
1243 break;
1244
1245 case BTA_DM_INQ_CMPL_EVT:
1246 {
Satya Calloji6e2d9db2014-07-08 16:18:58 -07001247#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1248 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1249 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1250 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1251 bte_scan_filt_param_cfg_evt, 0);
1252#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001253 }
1254 break;
1255 case BTA_DM_DISC_CMPL_EVT:
1256 {
1257 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1258 }
1259 break;
1260 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1261 {
1262 /* if inquiry is not in progress and we get a cancel event, then
1263 * it means we are done with inquiry, but remote_name fetches are in
1264 * progress
1265 *
1266 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt
1267 * but instead wait for the cancel_cmpl_evt via the Busy Level
1268 *
1269 */
1270 if (btif_dm_inquiry_in_progress == FALSE)
1271 {
1272 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1273 }
1274 }
1275 break;
1276 }
1277}
1278
1279/*******************************************************************************
1280**
1281** Function btif_dm_search_services_evt
1282**
1283** Description Executes search services event in btif context
1284**
1285** Returns void
1286**
1287*******************************************************************************/
1288static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1289{
1290 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1291
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001292 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001293 switch (event)
1294 {
1295 case BTA_DM_DISC_RES_EVT:
1296 {
1297 bt_uuid_t uuid_arr[BT_MAX_NUM_UUIDS]; /* Max 32 services */
1298 bt_property_t prop;
1299 uint32_t i = 0, j = 0;
1300 bt_bdaddr_t bd_addr;
1301 bt_status_t ret;
1302
1303 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1304
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001305 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001306 p_data->disc_res.result, p_data->disc_res.services);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001307 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1308 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1309 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1310 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001311 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001312 pairing_cb.sdp_attempts++;
1313 btif_dm_get_remote_services(&bd_addr);
1314 return;
1315 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001316 prop.type = BT_PROPERTY_UUIDS;
1317 prop.len = 0;
1318 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1319 {
1320 prop.val = p_data->disc_res.p_uuid_list;
1321 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1322 for (i=0; i < p_data->disc_res.num_uuids; i++)
1323 {
1324 char temp[256];
1325 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 -07001326 BTIF_TRACE_ERROR("Index: %d uuid:%s", i, temp);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001327 }
1328 }
1329
1330 /* onUuidChanged requires getBondedDevices to be populated.
1331 ** bond_state_changed needs to be sent prior to remote_device_property
1332 */
1333 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1334 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0)&&
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001335 pairing_cb.sdp_attempts > 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001336 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001337 BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001338 __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001339 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001340 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1341 }
1342
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001343 if(p_data->disc_res.num_uuids != 0)
1344 {
1345 /* Also write this to the NVRAM */
1346 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1347 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1348 /* Send the event to the BTIF */
1349 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1350 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1351 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001352 }
1353 break;
1354
1355 case BTA_DM_DISC_CMPL_EVT:
1356 /* fixme */
1357 break;
1358
Matthew Xie607e3b72013-08-15 19:30:48 -07001359#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001360 case BTA_DM_DISC_BLE_RES_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001361 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__,
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001362 p_data->disc_ble_res.service.uu.uuid16);
1363 bt_uuid_t uuid;
1364 int i = 0;
1365 int j = 15;
1366 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1367 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001368 BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001369 bt_property_t prop;
1370 bt_bdaddr_t bd_addr;
1371 char temp[256];
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001372 bt_status_t ret;
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001373
1374 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1375
1376 while(i < j )
1377 {
1378 unsigned char c = uuid.uu[j];
1379 uuid.uu[j] = uuid.uu[i];
1380 uuid.uu[i] = c;
1381 i++;
1382 j--;
1383 }
1384
1385 uuid_to_string(&uuid, temp);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001386 BTIF_TRACE_ERROR(" uuid:%s", temp);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001387
1388 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1389 prop.type = BT_PROPERTY_UUIDS;
1390 prop.val = uuid.uu;
1391 prop.len = MAX_UUID_SIZE;
1392
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001393 /* Also write this to the NVRAM */
1394 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1395 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1396
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001397 /* Send the event to the BTIF */
1398 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1399 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1400
1401 }
1402 break;
Matthew Xie607e3b72013-08-15 19:30:48 -07001403#endif /* BLE_INCLUDED */
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001404
The Android Open Source Project5738f832012-12-12 16:00:35 -08001405 default:
1406 {
1407 ASSERTC(0, "unhandled search services event", event);
1408 }
1409 break;
1410 }
1411}
1412
1413/*******************************************************************************
1414**
1415** Function btif_dm_remote_service_record_evt
1416**
1417** Description Executes search service record event in btif context
1418**
1419** Returns void
1420**
1421*******************************************************************************/
1422static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1423{
1424 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1425
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001426 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001427 switch (event)
1428 {
1429 case BTA_DM_DISC_RES_EVT:
1430 {
1431 bt_service_record_t rec;
1432 bt_property_t prop;
1433 uint32_t i = 0;
1434 bt_bdaddr_t bd_addr;
1435
1436 memset(&rec, 0, sizeof(bt_service_record_t));
1437 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1438
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001439 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001440 p_data->disc_res.result, p_data->disc_res.services);
1441 prop.type = BT_PROPERTY_SERVICE_RECORD;
1442 prop.val = (void*)&rec;
1443 prop.len = sizeof(rec);
1444
1445 /* disc_res.result is overloaded with SCN. Cannot check result */
1446 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1447 /* TODO: Get the UUID as well */
1448 rec.channel = p_data->disc_res.result - 3;
1449 /* TODO: Need to get the service name using p_raw_data */
1450 rec.name[0] = 0;
1451
1452 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1453 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1454 }
1455 break;
1456
1457 default:
1458 {
1459 ASSERTC(0, "unhandled remote service record event", event);
1460 }
1461 break;
1462 }
1463}
1464
1465/*******************************************************************************
1466**
1467** Function btif_dm_upstreams_cback
1468**
1469** Description Executes UPSTREAMS events in btif context
1470**
1471** Returns void
1472**
1473*******************************************************************************/
1474static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1475{
1476 tBTA_DM_SEC_EVT dm_event = (tBTA_DM_SEC_EVT)event;
1477 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1478 tBTA_SERVICE_MASK service_mask;
1479 uint32_t i;
1480 bt_bdaddr_t bd_addr;
1481
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001482 BTIF_TRACE_EVENT("btif_dm_upstreams_cback ev: %s", dump_dm_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001483
1484 switch (event)
1485 {
1486 case BTA_DM_ENABLE_EVT:
1487 {
1488 BD_NAME bdname;
1489 bt_status_t status;
1490 bt_property_t prop;
1491 prop.type = BT_PROPERTY_BDNAME;
1492 prop.len = BD_NAME_LEN;
1493 prop.val = (void*)bdname;
1494
1495 status = btif_storage_get_adapter_property(&prop);
Matthew Xie1e5109b2012-11-09 18:26:26 -08001496 if (status == BT_STATUS_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001497 {
1498 /* A name exists in the storage. Make this the device name */
1499 BTA_DmSetDeviceName((char*)prop.val);
1500 }
Matthew Xie1e5109b2012-11-09 18:26:26 -08001501 else
1502 {
1503 /* Storage does not have a name yet.
1504 * Use the default name and write it to the chip
1505 */
1506 BTA_DmSetDeviceName(btif_get_default_local_name());
1507 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001508
Andre Eisenbacha015a832014-09-11 14:09:40 -07001509#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1510 /* Enable local privacy */
1511 BTA_DmBleConfigLocalPrivacy(TRUE);
1512#endif
1513
The Android Open Source Project5738f832012-12-12 16:00:35 -08001514 /* for each of the enabled services in the mask, trigger the profile
1515 * enable */
1516 service_mask = btif_get_enabled_services_mask();
1517 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1518 {
1519 if (service_mask &
1520 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1521 {
1522 btif_in_execute_service_request(i, TRUE);
1523 }
1524 }
1525 /* clear control blocks */
1526 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1527
1528 /* This function will also trigger the adapter_properties_cb
1529 ** and bonded_devices_info_cb
1530 */
1531 btif_storage_load_bonded_devices();
1532
1533 btif_storage_load_autopair_device_list();
1534
1535 btif_enable_bluetooth_evt(p_data->enable.status, p_data->enable.bd_addr);
1536 }
1537 break;
1538
1539 case BTA_DM_DISABLE_EVT:
1540 /* for each of the enabled services in the mask, trigger the profile
1541 * disable */
1542 service_mask = btif_get_enabled_services_mask();
1543 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1544 {
1545 if (service_mask &
1546 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1547 {
1548 btif_in_execute_service_request(i, FALSE);
1549 }
1550 }
1551 btif_disable_bluetooth_evt();
1552 break;
1553
1554 case BTA_DM_PIN_REQ_EVT:
1555 btif_dm_pin_req_evt(&p_data->pin_req);
1556 break;
1557
1558 case BTA_DM_AUTH_CMPL_EVT:
1559 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1560 break;
1561
1562 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1563 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1564 {
1565 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1566 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1567 }
1568 break;
1569
1570 case BTA_DM_SP_CFM_REQ_EVT:
1571 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1572 break;
1573 case BTA_DM_SP_KEY_NOTIF_EVT:
1574 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1575 break;
1576
1577 case BTA_DM_DEV_UNPAIRED_EVT:
1578 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1579
1580 /*special handling for HID devices */
1581 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -07001582 btif_hh_remove_device(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001583 #endif
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001584 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1585 btif_storage_remove_ble_bonding_keys(&bd_addr);
1586 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001587 btif_storage_remove_bonded_device(&bd_addr);
1588 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1589 break;
1590
1591 case BTA_DM_BUSY_LEVEL_EVT:
1592 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001593
1594 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001595 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001596 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001597 {
1598 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1599 BT_DISCOVERY_STARTED);
1600 btif_dm_inquiry_in_progress = TRUE;
1601 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001602 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001603 {
1604 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1605 BT_DISCOVERY_STOPPED);
1606 btif_dm_inquiry_in_progress = FALSE;
1607 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001608 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001609 {
1610 btif_dm_inquiry_in_progress = FALSE;
1611 }
1612 }
1613 }break;
1614
1615 case BTA_DM_LINK_UP_EVT:
1616 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001617 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001618
1619 btif_update_remote_version_property(&bd_addr);
1620
The Android Open Source Project5738f832012-12-12 16:00:35 -08001621 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1622 &bd_addr, BT_ACL_STATE_CONNECTED);
1623 break;
1624
1625 case BTA_DM_LINK_DOWN_EVT:
1626 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001627 BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001628 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1629 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1630 break;
1631
1632 case BTA_DM_HW_ERROR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001633 BTIF_TRACE_ERROR("Received H/W Error. ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001634 /* Flush storage data */
1635 btif_config_flush();
1636 usleep(100000); /* 100milliseconds */
1637 /* Killing the process to force a restart as part of fault tolerance */
1638 kill(getpid(), SIGKILL);
1639 break;
1640
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001641#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1642 case BTA_DM_BLE_KEY_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001643 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 -08001644
1645 /* If this pairing is by-product of local initiated GATT client Read or Write,
1646 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1647 have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1648 if (pairing_cb.state != BT_BOND_STATE_BONDING)
1649 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001650 BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001651 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1652 BT_BOND_STATE_BONDING);
1653 }
1654 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1655 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001656 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001657 break;
1658 }
1659
1660 switch (p_data->ble_key.key_type)
1661 {
1662 case BTA_LE_KEY_PENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001663 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001664 pairing_cb.ble.is_penc_key_rcvd = TRUE;
1665 memcpy(pairing_cb.ble.penc_key.ltk,p_data->ble_key.key_value.penc_key.ltk, 16);
1666 memcpy(pairing_cb.ble.penc_key.rand, p_data->ble_key.key_value.penc_key.rand,8);
1667 pairing_cb.ble.penc_key.ediv = p_data->ble_key.key_value.penc_key.ediv;
1668 pairing_cb.ble.penc_key.sec_level = p_data->ble_key.key_value.penc_key.sec_level;
1669
1670 for (i=0; i<16; i++)
1671 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001672 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 -08001673 }
1674 for (i=0; i<8; i++)
1675 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001676 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 -08001677 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001678 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.ediv=0x%04x",pairing_cb.ble.penc_key.ediv);
1679 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.sec_level=0x%02x",pairing_cb.ble.penc_key.sec_level);
1680 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 -08001681 break;
1682
1683 case BTA_LE_KEY_PID:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001684 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001685 pairing_cb.ble.is_pid_key_rcvd = TRUE;
1686 memcpy(pairing_cb.ble.pid_key, p_data->ble_key.key_value.pid_key.irk, 16);
1687 for (i=0; i<16; i++)
1688 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001689 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 -08001690 }
1691 break;
1692
1693 case BTA_LE_KEY_PCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001694 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001695 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1696 pairing_cb.ble.pcsrk_key.counter = p_data->ble_key.key_value.pcsrk_key.counter;
1697 pairing_cb.ble.pcsrk_key.sec_level = p_data->ble_key.key_value.pcsrk_key.sec_level;
1698 memcpy(pairing_cb.ble.pcsrk_key.csrk,p_data->ble_key.key_value.pcsrk_key.csrk,16);
1699
1700 for (i=0; i<16; i++)
1701 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001702 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 -08001703 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001704 BTIF_TRACE_DEBUG("pairing_cb.ble.pcsrk_key.counter=0x%08x",pairing_cb.ble.pcsrk_key.counter);
1705 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 -08001706 break;
1707
1708 case BTA_LE_KEY_LENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001709 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001710 pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1711 pairing_cb.ble.lenc_key.div = p_data->ble_key.key_value.lenc_key.div;
1712 pairing_cb.ble.lenc_key.key_size = p_data->ble_key.key_value.lenc_key.key_size;
1713 pairing_cb.ble.lenc_key.sec_level = p_data->ble_key.key_value.lenc_key.sec_level;
1714
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001715 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.div=0x%04x",pairing_cb.ble.lenc_key.div);
1716 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.key_size=0x%02x",pairing_cb.ble.lenc_key.key_size);
1717 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 -08001718 break;
1719
1720
1721
1722 case BTA_LE_KEY_LCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001723 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001724 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1725 pairing_cb.ble.lcsrk_key.counter = p_data->ble_key.key_value.lcsrk_key.counter;
1726 pairing_cb.ble.lcsrk_key.div = p_data->ble_key.key_value.lcsrk_key.div;
1727 pairing_cb.ble.lcsrk_key.sec_level = p_data->ble_key.key_value.lcsrk_key.sec_level;
1728
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001729 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.div=0x%04x",pairing_cb.ble.lcsrk_key.div);
1730 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.counter=0x%08x",pairing_cb.ble.lcsrk_key.counter);
1731 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 -08001732
1733 break;
1734
1735 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001736 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001737 break;
1738 }
1739
1740 break;
1741 case BTA_DM_BLE_SEC_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001742 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001743 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1744 break;
1745 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001746 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001747 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1748 break;
1749 case BTA_DM_BLE_PASSKEY_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001750 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001751 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1752 break;
1753 case BTA_DM_BLE_OOB_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001754 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001755 break;
1756 case BTA_DM_BLE_LOCAL_IR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001757 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001758 ble_local_key_cb.is_id_keys_rcvd = TRUE;
1759 memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1760 memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1761 memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1762 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1763 BTIF_DM_LE_LOCAL_KEY_IR,
1764 BT_OCTET16_LEN);
1765 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1766 BTIF_DM_LE_LOCAL_KEY_IRK,
1767 BT_OCTET16_LEN);
1768 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1769 BTIF_DM_LE_LOCAL_KEY_DHK,
1770 BT_OCTET16_LEN);
1771 break;
1772 case BTA_DM_BLE_LOCAL_ER_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001773 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001774 ble_local_key_cb.is_er_rcvd = TRUE;
1775 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1776 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1777 BTIF_DM_LE_LOCAL_KEY_ER,
1778 BT_OCTET16_LEN);
1779 break;
1780
1781 case BTA_DM_BLE_AUTH_CMPL_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001782 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001783 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1784 break;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001785
1786 case BTA_DM_LE_FEATURES_READ:
1787 {
1788 tBTM_BLE_VSC_CB cmn_vsc_cb;
1789 bt_local_le_features_t local_le_features;
1790 char buf[512];
1791 bt_property_t prop;
1792 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1793 prop.val = (void*)buf;
1794 prop.len = sizeof(buf);
1795
1796 /* LE features are not stored in storage. Should be retrived from stack */
1797 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1798 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1799
1800 prop.len = sizeof (bt_local_le_features_t);
1801 if (cmn_vsc_cb.filter_support == 1)
1802 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1803 else
1804 local_le_features.max_adv_filter_supported = 0;
1805 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1806 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1807 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
Satya Callojid773c2c2014-07-29 22:08:55 -07001808 local_le_features.scan_result_storage_size_hibyte =
1809 (cmn_vsc_cb.tot_scan_results_strg >> 8) & (0xFF);
1810 local_le_features.scan_result_storage_size_lobyte =
1811 (cmn_vsc_cb.tot_scan_results_strg) & (0xFF);
Satya Callojiefaddcb2014-07-28 23:22:05 -07001812 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001813 memcpy(prop.val, &local_le_features, prop.len);
1814 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
1815 break;
1816 }
Satya Callojie5ba8842014-07-03 17:18:02 -07001817
1818 case BTA_DM_ENER_INFO_READ:
1819 {
1820 btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param;
1821 bt_activity_energy_info energy_info;
1822 energy_info.status = p_ener_data->status;
1823 energy_info.ctrl_state = p_ener_data->ctrl_state;
1824 energy_info.rx_time = p_ener_data->rx_time;
1825 energy_info.tx_time = p_ener_data->tx_time;
1826 energy_info.idle_time = p_ener_data->idle_time;
1827 energy_info.energy_used = p_ener_data->energy_used;
1828 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info);
1829 break;
1830 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001831#endif
1832
The Android Open Source Project5738f832012-12-12 16:00:35 -08001833 case BTA_DM_AUTHORIZE_EVT:
1834 case BTA_DM_SIG_STRENGTH_EVT:
1835 case BTA_DM_SP_RMT_OOB_EVT:
1836 case BTA_DM_SP_KEYPRESS_EVT:
1837 case BTA_DM_ROLE_CHG_EVT:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001838
The Android Open Source Project5738f832012-12-12 16:00:35 -08001839 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001840 BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001841 break;
1842 }
1843} /* btui_security_cback() */
1844
1845
1846/*******************************************************************************
1847**
1848** Function btif_dm_generic_evt
1849**
1850** Description Executes non-BTA upstream events in BTIF context
1851**
1852** Returns void
1853**
1854*******************************************************************************/
1855static void btif_dm_generic_evt(UINT16 event, char* p_param)
1856{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001857 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001858 switch(event)
1859 {
1860 case BTIF_DM_CB_DISCOVERY_STARTED:
1861 {
1862 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
1863 }
1864 break;
1865
1866 case BTIF_DM_CB_CREATE_BOND:
1867 {
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07001868 btif_dm_create_bond_cb_t *create_bond_cb = (btif_dm_create_bond_cb_t*)p_param;
1869 btif_dm_cb_create_bond(&create_bond_cb->bdaddr, create_bond_cb->transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001870 }
1871 break;
1872
1873 case BTIF_DM_CB_REMOVE_BOND:
1874 {
1875 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
1876 }
1877 break;
1878
1879 case BTIF_DM_CB_HID_REMOTE_NAME:
1880 {
1881 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
1882 }
1883 break;
1884
1885 case BTIF_DM_CB_BOND_STATE_BONDING:
1886 {
1887 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
1888 }
1889 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001890 case BTIF_DM_CB_LE_TX_TEST:
1891 case BTIF_DM_CB_LE_RX_TEST:
1892 {
1893 uint8_t status;
1894 STREAM_TO_UINT8(status, p_param);
1895 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1896 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
1897 }
1898 break;
1899 case BTIF_DM_CB_LE_TEST_END:
1900 {
1901 uint8_t status;
1902 uint16_t count = 0;
1903 STREAM_TO_UINT8(status, p_param);
1904 if (status == 0)
1905 STREAM_TO_UINT16(count, p_param);
1906 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1907 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
1908 }
1909 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001910 default:
1911 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001912 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001913 }
1914 break;
1915 }
1916}
1917
1918/*******************************************************************************
1919**
1920** Function bte_dm_evt
1921**
1922** Description Switches context from BTE to BTIF for all DM events
1923**
1924** Returns void
1925**
1926*******************************************************************************/
1927
1928void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
1929{
1930 bt_status_t status;
1931
1932 /* switch context to btif task context (copy full union size for convenience) */
1933 status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event, (void*)p_data, sizeof(tBTA_DM_SEC), NULL);
1934
1935 /* catch any failed context transfers */
1936 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
1937}
1938
1939/*******************************************************************************
1940**
1941** Function bte_search_devices_evt
1942**
1943** Description Switches context from BTE to BTIF for DM search events
1944**
1945** Returns void
1946**
1947*******************************************************************************/
1948static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1949{
1950 UINT16 param_len = 0;
1951
1952 if (p_data)
1953 param_len += sizeof(tBTA_DM_SEARCH);
1954 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
1955 switch (event)
1956 {
1957 case BTA_DM_INQ_RES_EVT:
1958 {
1959 if (p_data->inq_res.p_eir)
1960 param_len += HCI_EXT_INQ_RESPONSE_LEN;
1961 }
1962 break;
1963
1964 case BTA_DM_DISC_RES_EVT:
1965 {
1966 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
1967 param_len += p_data->disc_res.raw_data_size;
1968 }
1969 break;
1970 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001971 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 -08001972
1973 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
1974 if (event == BTA_DM_INQ_RES_EVT)
1975 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
1976
1977 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
1978 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
1979}
1980
1981/*******************************************************************************
1982**
1983** Function bte_dm_search_services_evt
1984**
1985** Description Switches context from BTE to BTIF for DM search services
1986** event
1987**
1988** Returns void
1989**
1990*******************************************************************************/
1991static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1992{
1993 UINT16 param_len = 0;
1994 if (p_data)
1995 param_len += sizeof(tBTA_DM_SEARCH);
1996 switch (event)
1997 {
1998 case BTA_DM_DISC_RES_EVT:
1999 {
2000 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
2001 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
2002 }
2003 } break;
2004 }
2005 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
2006 * if raw_data is needed. */
2007 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
2008 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
2009}
2010
2011/*******************************************************************************
2012**
2013** Function bte_dm_remote_service_record_evt
2014**
2015** Description Switches context from BTE to BTIF for DM search service
2016** record event
2017**
2018** Returns void
2019**
2020*******************************************************************************/
2021static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2022{
2023 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
2024 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
2025}
2026
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002027#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07002028/*******************************************************************************
2029**
2030** Function bta_energy_info_cb
2031**
2032** Description Switches context from BTE to BTIF for DM energy info event
2033**
2034** Returns void
2035**
2036*******************************************************************************/
2037static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time,
2038 tBTA_DM_BLE_IDLE_TIME_MS idle_time,
2039 tBTA_DM_BLE_ENERGY_USED energy_used,
2040 tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status)
2041{
2042 BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld",
2043 status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
2044
2045 btif_activity_energy_info_cb_t btif_cb;
2046 btif_cb.status = status;
2047 btif_cb.ctrl_state = ctrl_state;
2048 btif_cb.tx_time = (uint64_t) tx_time;
2049 btif_cb.rx_time = (uint64_t) rx_time;
2050 btif_cb.idle_time =(uint64_t) idle_time;
2051 btif_cb.energy_used =(uint64_t) energy_used;
2052 btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
2053 (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL);
2054}
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002055#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07002056
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002057/*******************************************************************************
2058**
2059** Function bte_scan_filt_param_cfg_evt
2060**
2061** Description Scan filter param config event
2062**
2063** Returns void
2064**
2065*******************************************************************************/
2066static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
2067 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
2068 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
2069{
2070 /* This event occurs on calling BTA_DmBleCfgFilterCondition internally,
2071 ** and that is why there is no HAL callback
2072 */
2073 if(BTA_SUCCESS != status)
2074 {
2075 BTIF_TRACE_ERROR("%s, %d", __FUNCTION__, status);
2076 }
2077 else
2078 {
2079 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2080 }
2081}
2082
The Android Open Source Project5738f832012-12-12 16:00:35 -08002083/*****************************************************************************
2084**
2085** btif api functions (no context switch)
2086**
2087*****************************************************************************/
2088
2089/*******************************************************************************
2090**
2091** Function btif_dm_start_discovery
2092**
2093** Description Start device discovery/inquiry
2094**
2095** Returns bt_status_t
2096**
2097*******************************************************************************/
2098bt_status_t btif_dm_start_discovery(void)
2099{
2100 tBTA_DM_INQ inq_params;
2101 tBTA_SERVICE_MASK services = 0;
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002102 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002103
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002104 BTIF_TRACE_EVENT("%s", __FUNCTION__);
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002105
2106#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2107 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
2108 /* Cleanup anything remaining on index 0 */
2109 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
2110 bte_scan_filt_param_cfg_evt, 0);
2111
2112 /* Add an allow-all filter on index 0*/
2113 adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE;
2114 adv_filt_param.feat_seln = ALLOW_ALL_FILTER;
2115 adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;
2116 adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;
2117 adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE;
2118 adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE;
2119 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL,
2120 bte_scan_filt_param_cfg_evt, 0);
2121
The Android Open Source Project5738f832012-12-12 16:00:35 -08002122 /* TODO: Do we need to handle multiple inquiries at the same time? */
2123
2124 /* Set inquiry params and call API */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002125 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
Matthew Xie7f3e4292013-09-30 12:44:10 -07002126#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
2127 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
2128 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
2129 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
2130 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
2131#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002132#else
2133 inq_params.mode = BTA_DM_GENERAL_INQUIRY;
2134#endif
2135 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
2136
2137 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
2138 inq_params.report_dup = TRUE;
2139
2140 inq_params.filter_type = BTA_DM_INQ_CLR;
2141 /* TODO: Filter device by BDA needs to be implemented here */
2142
2143 /* Will be enabled to TRUE once inquiry busy level has been received */
2144 btif_dm_inquiry_in_progress = FALSE;
2145 /* find nearby devices */
2146 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
2147
2148 return BT_STATUS_SUCCESS;
2149}
2150
2151/*******************************************************************************
2152**
2153** Function btif_dm_cancel_discovery
2154**
2155** Description Cancels search
2156**
2157** Returns bt_status_t
2158**
2159*******************************************************************************/
2160bt_status_t btif_dm_cancel_discovery(void)
2161{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002162 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002163 BTA_DmSearchCancel();
2164 return BT_STATUS_SUCCESS;
2165}
2166
2167/*******************************************************************************
2168**
2169** Function btif_dm_create_bond
2170**
2171** Description Initiate bonding with the specified device
2172**
2173** Returns bt_status_t
2174**
2175*******************************************************************************/
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002176bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr, int transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002177{
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002178 btif_dm_create_bond_cb_t create_bond_cb;
2179 create_bond_cb.transport = transport;
2180 bdcpy(create_bond_cb.bdaddr.address, bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002181
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002182 bdstr_t bdstr;
2183 BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __FUNCTION__,
2184 bd2str((bt_bdaddr_t *) bd_addr, &bdstr), transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002185 if (pairing_cb.state != BT_BOND_STATE_NONE)
2186 return BT_STATUS_BUSY;
2187
2188 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002189 (char *)&create_bond_cb, sizeof(btif_dm_create_bond_cb_t), NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002190
2191 return BT_STATUS_SUCCESS;
2192}
2193
2194/*******************************************************************************
2195**
2196** Function btif_dm_cancel_bond
2197**
2198** Description Initiate bonding with the specified device
2199**
2200** Returns bt_status_t
2201**
2202*******************************************************************************/
2203
2204bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
2205{
2206 bdstr_t bdstr;
2207
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002208 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 -08002209
2210 /* TODO:
2211 ** 1. Restore scan modes
2212 ** 2. special handling for HID devices
2213 */
2214 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2215 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002216
2217#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2218
2219 if (pairing_cb.is_ssp)
2220 {
2221 if (pairing_cb.is_le_only)
2222 {
2223 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2224 }
2225 else
Hemant Guptae1468692013-11-14 16:21:29 +05302226 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002227 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
Hemant Guptae1468692013-11-14 16:21:29 +05302228 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2229 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2230 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002231 }
2232 else
2233 {
2234 if (pairing_cb.is_le_only)
2235 {
2236 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2237 }
2238 else
2239 {
2240 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2241 }
2242 /* Cancel bonding, in case it is in ACL connection setup state */
2243 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2244 }
2245
2246#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002247 if (pairing_cb.is_ssp)
2248 {
2249 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2250 }
2251 else
2252 {
2253 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2254 }
2255 /* Cancel bonding, in case it is in ACL connection setup state */
2256 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2257 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002258#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002259 }
2260
2261 return BT_STATUS_SUCCESS;
2262}
2263
2264/*******************************************************************************
2265**
Kim Schulza9eb25c2013-09-30 10:55:52 +02002266** Function btif_dm_hh_open_failed
2267**
2268** Description informs the upper layers if the HH have failed during bonding
2269**
2270** Returns none
2271**
2272*******************************************************************************/
2273
2274void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
2275{
2276 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2277 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2278 {
2279 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2280 }
2281}
2282
2283/*******************************************************************************
2284**
The Android Open Source Project5738f832012-12-12 16:00:35 -08002285** Function btif_dm_remove_bond
2286**
2287** Description Removes bonding with the specified device
2288**
2289** Returns bt_status_t
2290**
2291*******************************************************************************/
2292
2293bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
2294{
2295 bdstr_t bdstr;
2296
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002297 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 -08002298 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2299 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2300
2301 return BT_STATUS_SUCCESS;
2302}
2303
2304/*******************************************************************************
2305**
2306** Function btif_dm_pin_reply
2307**
2308** Description BT legacy pairing - PIN code reply
2309**
2310** Returns bt_status_t
2311**
2312*******************************************************************************/
2313
2314bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2315 uint8_t pin_len, bt_pin_code_t *pin_code)
2316{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002317 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Hemant Gupta831423e2014-01-08 12:42:13 +05302318 if (pin_code == NULL)
2319 return BT_STATUS_FAIL;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002320#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002321
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002322 if (pairing_cb.is_le_only)
2323 {
2324 int i;
2325 UINT32 passkey = 0;
2326 int multi[] = {100000, 10000, 1000, 100, 10,1};
2327 BD_ADDR remote_bd_addr;
2328 bdcpy(remote_bd_addr, bd_addr->address);
2329 for (i = 0; i < 6; i++)
2330 {
2331 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2332 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002333 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002334 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2335
2336 }
2337 else
2338 {
2339 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2340 if (accept)
2341 pairing_cb.pin_code_len = pin_len;
2342 }
2343#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002344 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2345
2346 if (accept)
2347 pairing_cb.pin_code_len = pin_len;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002348#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002349 return BT_STATUS_SUCCESS;
2350}
2351
2352/*******************************************************************************
2353**
2354** Function btif_dm_ssp_reply
2355**
2356** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2357**
2358** Returns bt_status_t
2359**
2360*******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002361bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2362 bt_ssp_variant_t variant, uint8_t accept,
2363 uint32_t passkey)
2364{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08002365 UNUSED(passkey);
2366
The Android Open Source Project5738f832012-12-12 16:00:35 -08002367 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2368 {
2369 /* This is not implemented in the stack.
2370 * For devices with display, this is not needed
2371 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002372 BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002373 return BT_STATUS_FAIL;
2374 }
2375 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002376 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002377#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2378 if (pairing_cb.is_le_only)
2379 {
2380 if (accept)
2381 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2382 else
2383 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2384 }
2385 else
2386 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002387
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002388#else
2389 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2390#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002391 return BT_STATUS_SUCCESS;
2392}
2393
2394/*******************************************************************************
2395**
2396** Function btif_dm_get_adapter_property
2397**
2398** Description Queries the BTA for the adapter property
2399**
2400** Returns bt_status_t
2401**
2402*******************************************************************************/
2403bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2404{
2405 bt_status_t status;
2406
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002407 BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002408 switch (prop->type)
2409 {
2410 case BT_PROPERTY_BDNAME:
2411 {
2412 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
Matthew Xie1e5109b2012-11-09 18:26:26 -08002413 strcpy((char *)bd_name->name, btif_get_default_local_name());
The Android Open Source Project5738f832012-12-12 16:00:35 -08002414 prop->len = strlen((char *)bd_name->name);
2415 }
2416 break;
2417
2418 case BT_PROPERTY_ADAPTER_SCAN_MODE:
2419 {
2420 /* if the storage does not have it. Most likely app never set it. Default is NONE */
2421 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2422 *mode = BT_SCAN_MODE_NONE;
2423 prop->len = sizeof(bt_scan_mode_t);
2424 }
2425 break;
2426
2427 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2428 {
2429 uint32_t *tmt = (uint32_t*)prop->val;
2430 *tmt = 120; /* default to 120s, if not found in NV */
2431 prop->len = sizeof(uint32_t);
2432 }
2433 break;
2434
2435 default:
2436 prop->len = 0;
2437 return BT_STATUS_FAIL;
2438 }
2439 return BT_STATUS_SUCCESS;
2440}
2441
2442/*******************************************************************************
2443**
2444** Function btif_dm_get_remote_services
2445**
2446** Description Start SDP to get remote services
2447**
2448** Returns bt_status_t
2449**
2450*******************************************************************************/
2451bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2452{
2453 bdstr_t bdstr;
2454
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002455 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002456
2457 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2458 bte_dm_search_services_evt, TRUE);
2459
2460 return BT_STATUS_SUCCESS;
2461}
2462
2463/*******************************************************************************
2464**
2465** Function btif_dm_get_remote_service_record
2466**
2467** Description Start SDP to get remote service record
2468**
2469**
2470** Returns bt_status_t
2471*******************************************************************************/
2472bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2473 bt_uuid_t *uuid)
2474{
2475 tSDP_UUID sdp_uuid;
2476 bdstr_t bdstr;
2477
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002478 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002479
2480 sdp_uuid.len = MAX_UUID_SIZE;
2481 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2482
2483 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2484 bte_dm_remote_service_record_evt, TRUE);
2485
2486 return BT_STATUS_SUCCESS;
2487}
2488
2489void btif_dm_execute_service_request(UINT16 event, char *p_param)
2490{
2491 BOOLEAN b_enable = FALSE;
2492 bt_status_t status;
2493 if (event == BTIF_DM_ENABLE_SERVICE)
2494 {
2495 b_enable = TRUE;
2496 }
2497 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2498 if (status == BT_STATUS_SUCCESS)
2499 {
2500 bt_property_t property;
2501 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2502
2503 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2504 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2505 sizeof(local_uuids), local_uuids);
2506 btif_storage_get_adapter_property(&property);
2507 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2508 BT_STATUS_SUCCESS, 1, &property);
2509 }
2510 return;
2511}
2512
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002513void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2514 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2515{
2516 UINT8 yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2517 /* if local initiated:
2518 ** 1. set DD + MITM
2519 ** if remote initiated:
2520 ** 1. Copy over the auth_req from peer's io_rsp
2521 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2522 ** as a fallback set MITM+GB if peer had MITM set
2523 */
2524 UNUSED (bd_addr);
2525 UNUSED (p_io_cap);
2526 UNUSED (p_oob_data);
2527
2528
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002529 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002530 if(pairing_cb.is_local_initiated)
2531 {
2532 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2533 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2534 }
2535 else if (!is_orig)
2536 {
2537 /* peer initiated paring. They probably know what they want.
2538 ** Copy the mitm from peer device.
2539 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002540 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002541 __FUNCTION__, pairing_cb.auth_req);
2542 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2543
2544 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2545 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2546 *p_auth_req |= BTA_AUTH_SP_YES;
2547 }
2548 else if (yes_no_bit)
2549 {
2550 /* set the general bonding bit for stored device */
2551 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2552 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002553 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002554}
2555
2556void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2557 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2558{
2559 UNUSED (bd_addr);
2560 UNUSED (oob_data);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002561
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002562 if(auth_req & BTA_AUTH_BONDS)
2563 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002564 BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002565 pairing_cb.auth_req = auth_req;
2566 pairing_cb.io_cap = io_cap;
2567 }
2568}
2569
The Android Open Source Project5738f832012-12-12 16:00:35 -08002570#if (BTM_OOB_INCLUDED == TRUE)
2571void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data)
2572{
2573 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2574 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2575 {
2576 *p_oob_data = FALSE;
2577 }
2578 else
2579 {
2580 *p_oob_data = TRUE;
2581 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002582 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 -08002583}
2584#endif /* BTM_OOB_INCLUDED */
2585
2586#ifdef BTIF_DM_OOB_TEST
2587void btif_dm_load_local_oob(void)
2588{
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002589 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002590 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002591 BTIF_TRACE_DEBUG("btif_dm_load_local_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002592 if (prop_oob[0] != '3')
2593 {
2594#if (BTM_OOB_INCLUDED == TRUE)
2595 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2596 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2597 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002598 BTIF_TRACE_DEBUG("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002599 BTA_DmLocalOob();
2600 }
2601#else
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002602 BTIF_TRACE_ERROR("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002603#endif
2604 }
2605}
2606
2607void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2608{
2609 FILE *fp;
2610 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2611 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2612 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002613 char prop_oob[PROPERTY_VALUE_MAX];
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002614 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: valid=%d", valid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002615 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2616 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2617 valid)
2618 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002619 BTIF_TRACE_DEBUG("save local OOB data in memory");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002620 memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2621 memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2622 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002623 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002624 if (prop_oob[0] == '1')
2625 path = path_a;
2626 else if (prop_oob[0] == '2')
2627 path = path_b;
2628 if (path)
2629 {
2630 fp = fopen(path, "wb+");
2631 if (fp == NULL)
2632 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002633 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 -08002634 }
2635 else
2636 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002637 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 -08002638 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2639 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2640 fclose(fp);
2641 }
2642 }
2643 }
2644}
2645BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2646{
2647 char t[128];
2648 FILE *fp;
2649 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2650 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2651 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002652 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002653 BOOLEAN result = FALSE;
2654 bt_bdaddr_t bt_bd_addr;
2655 bdcpy(oob_cb.oob_bdaddr, bd_addr);
2656 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002657 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002658 if (prop_oob[0] == '1')
2659 path = path_b;
2660 else if (prop_oob[0] == '2')
2661 path = path_a;
2662 if (path)
2663 {
2664 fp = fopen(path, "rb");
2665 if (fp == NULL)
2666 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002667 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 -08002668 return FALSE;
2669 }
2670 else
2671 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002672 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob: read OOB data from %s",path);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002673 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2674 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2675 fclose(fp);
2676 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002677 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: TRUE");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002678 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2679 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2680 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002681 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002682 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2683 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
2684 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 -07002685 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: c = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002686 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2687 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
2688 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 -07002689 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: r = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002690 bdcpy(bt_bd_addr.address, bd_addr);
2691 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2692 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2693 result = TRUE;
2694 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002695 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob result=%d",result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002696 return result;
2697}
2698#endif /* BTIF_DM_OOB_TEST */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002699#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2700
2701static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2702{
2703 bt_bdaddr_t bd_addr;
2704 bt_bdname_t bd_name;
2705 UINT32 cod;
2706
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002707 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002708
2709 /* Remote name update */
2710 btif_update_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2711 NULL, BT_DEVICE_TYPE_BLE);
2712 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2713 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2714
2715 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2716 pairing_cb.is_ssp = FALSE;
2717 cod = COD_UNCLASSIFIED;
2718
2719 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2720 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2721 p_ssp_key_notif->passkey);
2722}
2723
2724/*******************************************************************************
2725**
2726** Function btif_dm_ble_auth_cmpl_evt
2727**
2728** Description Executes authentication complete event in btif context
2729**
2730** Returns void
2731**
2732*******************************************************************************/
2733static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2734{
2735 /* Save link key, if not temporary */
2736 bt_bdaddr_t bd_addr;
2737 bt_status_t status = BT_STATUS_FAIL;
2738 bt_bond_state_t state = BT_BOND_STATE_NONE;
2739
2740 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2741 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2742 {
2743 /* store keys */
2744 }
2745 if (p_auth_cmpl->success)
2746 {
2747 status = BT_STATUS_SUCCESS;
2748 state = BT_BOND_STATE_BONDED;
2749
2750 btif_dm_save_ble_bonding_keys();
2751 BTA_GATTC_Refresh(bd_addr.address);
2752 btif_dm_get_remote_services(&bd_addr);
2753 }
2754 else
2755 {
2756 /*Map the HCI fail reason to bt status */
2757 switch (p_auth_cmpl->fail_reason)
2758 {
Priti Aghera156c52b2014-07-09 14:58:19 -07002759 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
2760 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
2761 btif_dm_remove_ble_bonding_keys();
2762 status = BT_STATUS_AUTH_FAILURE;
2763 break;
2764 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
2765 status = BT_STATUS_AUTH_REJECTED;
2766 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002767 default:
Andre Eisenbachca22ac42013-02-13 17:02:11 +09002768 btif_dm_remove_ble_bonding_keys();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002769 status = BT_STATUS_FAIL;
2770 break;
2771 }
2772 }
2773 bond_state_changed(status, &bd_addr, state);
2774}
2775
2776
2777
2778void btif_dm_load_ble_local_keys(void)
2779{
2780 bt_status_t bt_status;
2781
2782 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2783
2784 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2785 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2786 {
2787 ble_local_key_cb.is_er_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002788 BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002789 }
2790
2791 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
2792 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
2793 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
2794 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
2795 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
2796 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
2797 {
2798 ble_local_key_cb.is_id_keys_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002799 BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002800 }
2801
2802}
2803void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
2804 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
2805{
2806 if (ble_local_key_cb.is_er_rcvd )
2807 {
2808 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2809 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2810 }
2811
2812 if (ble_local_key_cb.is_id_keys_rcvd)
2813 {
2814 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
2815 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
2816 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
2817 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
2818 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002819 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002820}
2821
2822void btif_dm_save_ble_bonding_keys(void)
2823{
2824
2825 bt_bdaddr_t bd_addr;
2826
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002827 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002828
2829 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2830
2831 if (pairing_cb.ble.is_penc_key_rcvd)
2832 {
2833 btif_storage_add_ble_bonding_key(&bd_addr,
2834 (char *) &pairing_cb.ble.penc_key,
2835 BTIF_DM_LE_KEY_PENC,
2836 sizeof(btif_dm_ble_penc_keys_t));
2837 }
2838
2839 if (pairing_cb.ble.is_pid_key_rcvd)
2840 {
2841 btif_storage_add_ble_bonding_key(&bd_addr,
2842 (char *) &pairing_cb.ble.pid_key[0],
2843 BTIF_DM_LE_KEY_PID,
2844 BT_OCTET16_LEN);
2845 }
2846
2847
2848 if (pairing_cb.ble.is_pcsrk_key_rcvd)
2849 {
2850 btif_storage_add_ble_bonding_key(&bd_addr,
2851 (char *) &pairing_cb.ble.pcsrk_key,
2852 BTIF_DM_LE_KEY_PCSRK,
2853 sizeof(btif_dm_ble_pcsrk_keys_t));
2854 }
2855
2856
2857 if (pairing_cb.ble.is_lenc_key_rcvd)
2858 {
2859 btif_storage_add_ble_bonding_key(&bd_addr,
2860 (char *) &pairing_cb.ble.lenc_key,
2861 BTIF_DM_LE_KEY_LENC,
2862 sizeof(btif_dm_ble_lenc_keys_t));
2863 }
2864
2865 if (pairing_cb.ble.is_lcsrk_key_rcvd)
2866 {
2867 btif_storage_add_ble_bonding_key(&bd_addr,
2868 (char *) &pairing_cb.ble.lcsrk_key,
2869 BTIF_DM_LE_KEY_LCSRK,
2870 sizeof(btif_dm_ble_lcsrk_keys_t));
2871 }
2872
2873}
2874
2875
2876void btif_dm_remove_ble_bonding_keys(void)
2877{
2878 bt_bdaddr_t bd_addr;
2879
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002880 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002881
2882 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2883 btif_storage_remove_ble_bonding_keys(&bd_addr);
2884}
2885
2886
2887/*******************************************************************************
2888**
2889** Function btif_dm_ble_sec_req_evt
2890**
2891** Description Eprocess security request event in btif context
2892**
2893** Returns void
2894**
2895*******************************************************************************/
2896void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
2897{
2898 bt_bdaddr_t bd_addr;
2899 bt_bdname_t bd_name;
2900 UINT32 cod;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002901 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002902
2903 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2904 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002905 BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002906 return;
2907 }
2908
2909 /* Remote name update */
2910 btif_update_remote_properties(p_ble_req->bd_addr,p_ble_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
2911
2912 bdcpy(bd_addr.address, p_ble_req->bd_addr);
2913 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
2914
2915 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2916
2917 pairing_cb.is_temp = FALSE;
2918 pairing_cb.is_le_only = TRUE;
2919 pairing_cb.is_ssp = TRUE;
2920
2921 cod = COD_UNCLASSIFIED;
2922
2923 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
2924 BT_SSP_VARIANT_CONSENT, 0);
2925}
2926
2927
2928
2929/*******************************************************************************
2930**
2931** Function btif_dm_ble_passkey_req_evt
2932**
2933** Description Executes pin request event in btif context
2934**
2935** Returns void
2936**
2937*******************************************************************************/
2938static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
2939{
2940 bt_bdaddr_t bd_addr;
2941 bt_bdname_t bd_name;
2942 UINT32 cod;
2943
2944 /* Remote name update */
2945 btif_update_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
2946
2947 bdcpy(bd_addr.address, p_pin_req->bd_addr);
2948 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
2949
2950 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2951 pairing_cb.is_le_only = TRUE;
2952
2953 cod = COD_UNCLASSIFIED;
2954
2955 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
2956 &bd_addr, &bd_name, cod);
2957}
2958
2959
2960void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
2961 tBT_DEVICE_TYPE dev_type)
2962{
2963 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
2964}
2965
2966static void btif_dm_ble_tx_test_cback(void *p)
2967{
2968 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
2969 (char *)p, 1, NULL);
2970}
2971
2972static void btif_dm_ble_rx_test_cback(void *p)
2973{
2974 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
2975 (char *)p, 1, NULL);
2976}
2977
2978static void btif_dm_ble_test_end_cback(void *p)
2979{
2980 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
2981 (char *)p, 3, NULL);
2982}
2983/*******************************************************************************
2984**
2985** Function btif_le_test_mode
2986**
2987** Description Sends a HCI BLE Test command to the Controller
2988**
2989** Returns BT_STATUS_SUCCESS on success
2990**
2991*******************************************************************************/
2992bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
2993{
2994 switch (opcode) {
2995 case HCI_BLE_TRANSMITTER_TEST:
2996 if (len != 3) return BT_STATUS_PARM_INVALID;
2997 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
2998 break;
2999 case HCI_BLE_RECEIVER_TEST:
3000 if (len != 1) return BT_STATUS_PARM_INVALID;
3001 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
3002 break;
3003 case HCI_BLE_TEST_END:
3004 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
3005 break;
3006 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003007 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003008 return BT_STATUS_UNSUPPORTED;
3009 }
3010 return BT_STATUS_SUCCESS;
3011}
3012
3013#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08003014
3015void btif_dm_on_disable()
3016{
3017 /* cancel any pending pairing requests */
3018 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3019 {
3020 bt_bdaddr_t bd_addr;
3021
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003022 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003023 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3024 btif_dm_cancel_bond(&bd_addr);
3025 }
3026}
Matthew Xie1e5109b2012-11-09 18:26:26 -08003027
Satya Callojie5ba8842014-07-03 17:18:02 -07003028/*******************************************************************************
3029**
3030** Function btif_dm_read_energy_info
3031**
3032** Description Reads the energy info from controller
3033**
3034** Returns void
3035**
3036*******************************************************************************/
3037void btif_dm_read_energy_info()
3038{
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003039#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07003040 BTA_DmBleGetEnergyInfo(bta_energy_info_cb);
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003041#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07003042}
3043
Matthew Xie1e5109b2012-11-09 18:26:26 -08003044static char* btif_get_default_local_name() {
3045 if (btif_default_local_name[0] == '\0')
3046 {
3047 int max_len = sizeof(btif_default_local_name) - 1;
3048 if (BTM_DEF_LOCAL_NAME[0] != '\0')
3049 {
3050 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
3051 }
3052 else
3053 {
3054 char prop_model[PROPERTY_VALUE_MAX];
3055 property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
3056 strncpy(btif_default_local_name, prop_model, max_len);
3057 }
3058 btif_default_local_name[max_len] = '\0';
3059 }
3060 return btif_default_local_name;
3061}