blob: c110ee8b30b0082c29585ccfabfd6e2a4c0d261b [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"
Andre Eisenbach31a64002014-10-14 14:29:19 -070047
48/******************************************************************************
49** Device specific workarounds
50******************************************************************************/
51
52/**
53 * The devices below have proven problematic during the pairing process, often
54 * requiring multiple retries to complete pairing. To avoid degrading the user
55 * experience for other devices, explicitely blacklist troubled devices here.
56 */
57static const UINT8 blacklist_pairing_retries[][3] = {
58 {0x9C, 0xDF, 0x03} // BMW car kits (Harman/Becker)
59};
60
61BOOLEAN blacklistPairingRetries(BD_ADDR bd_addr)
62{
63 const unsigned blacklist_size = sizeof(blacklist_pairing_retries)
64 / sizeof(blacklist_pairing_retries[0]);
65 for (unsigned i = 0; i != blacklist_size; ++i)
66 {
67 if (blacklist_pairing_retries[i][0] == bd_addr[0] &&
68 blacklist_pairing_retries[i][1] == bd_addr[1] &&
69 blacklist_pairing_retries[i][2] == bd_addr[2])
70 return TRUE;
71 }
72 return FALSE;
73}
74
The Android Open Source Project5738f832012-12-12 16:00:35 -080075/******************************************************************************
76** Constants & Macros
77******************************************************************************/
78
79#define COD_UNCLASSIFIED ((0x1F) << 8)
Priti Agheraebb1d752012-11-27 18:03:22 -080080#define COD_HID_KEYBOARD 0x0540
81#define COD_HID_POINTING 0x0580
82#define COD_HID_COMBO 0x05C0
83#define COD_HID_MAJOR 0x0500
84#define COD_AV_HEADSETS 0x0404
85#define COD_AV_HANDSFREE 0x0408
86#define COD_AV_HEADPHONES 0x0418
87#define COD_AV_PORTABLE_AUDIO 0x041C
88#define COD_AV_HIFI_AUDIO 0x0428
The Android Open Source Project5738f832012-12-12 16:00:35 -080089
90
91#define BTIF_DM_DEFAULT_INQ_MAX_RESULTS 0
92#define BTIF_DM_DEFAULT_INQ_MAX_DURATION 10
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -070093#define BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING 2
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080094
Andre Eisenbach31a64002014-10-14 14:29:19 -070095#define NUM_TIMEOUT_RETRIES 5
96
Matthew Xie1e5109b2012-11-09 18:26:26 -080097#define PROPERTY_PRODUCT_MODEL "ro.product.model"
Matthew Xiea30d95a2013-09-18 12:30:36 -070098#define DEFAULT_LOCAL_NAME_MAX 31
Matthew Xie1e5109b2012-11-09 18:26:26 -080099#if (DEFAULT_LOCAL_NAME_MAX > BTM_MAX_LOC_BD_NAME_LEN)
100 #error "default btif local name size exceeds stack supported length"
101#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800102
Matthew Xie7f3e4292013-09-30 12:44:10 -0700103#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
104#define BTIF_DM_INTERLEAVE_DURATION_BR_ONE 2
105#define BTIF_DM_INTERLEAVE_DURATION_LE_ONE 2
106#define BTIF_DM_INTERLEAVE_DURATION_BR_TWO 3
107#define BTIF_DM_INTERLEAVE_DURATION_LE_TWO 4
108#endif
109
Priti Agherac0edf9f2014-06-26 11:23:51 -0700110#define MAX_SDP_BL_ENTRIES 3
111
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800112typedef struct
113{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800114 bt_bond_state_t state;
115 BD_ADDR bd_addr;
116 UINT8 is_temp;
117 UINT8 pin_code_len;
118 UINT8 is_ssp;
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -0700119 UINT8 auth_req;
120 UINT8 io_cap;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800121 UINT8 autopair_attempts;
Andre Eisenbach31a64002014-10-14 14:29:19 -0700122 UINT8 timeout_retries;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800123 UINT8 is_local_initiated;
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700124 UINT8 sdp_attempts;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800125#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
126 BOOLEAN is_le_only;
127 btif_dm_ble_cb_t ble;
128#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800129} btif_dm_pairing_cb_t;
130
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800131
132typedef struct
133{
134 UINT8 ir[BT_OCTET16_LEN];
135 UINT8 irk[BT_OCTET16_LEN];
136 UINT8 dhk[BT_OCTET16_LEN];
137}btif_dm_local_key_id_t;
138
139typedef struct
140{
141 BOOLEAN is_er_rcvd;
142 UINT8 er[BT_OCTET16_LEN];
143 BOOLEAN is_id_keys_rcvd;
144 btif_dm_local_key_id_t id_keys; /* ID kyes */
145
146}btif_dm_local_key_cb_t;
147
148typedef struct
149{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800150 BD_ADDR bd_addr;
151 BD_NAME bd_name;
152} btif_dm_remote_name_t;
153
154typedef struct
155{
156 BT_OCTET16 sp_c;
157 BT_OCTET16 sp_r;
158 BD_ADDR oob_bdaddr; /* peer bdaddr*/
159} btif_dm_oob_cb_t;
Satya Callojie5ba8842014-07-03 17:18:02 -0700160
161typedef struct
162{
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700163 bt_bdaddr_t bdaddr;
164 UINT8 transport; /* 0=Unknown, 1=BR/EDR, 2=LE */
165} btif_dm_create_bond_cb_t;
166
167typedef struct
168{
Satya Callojie5ba8842014-07-03 17:18:02 -0700169 uint8_t status;
170 uint8_t ctrl_state;
171 uint64_t tx_time;
172 uint64_t rx_time;
173 uint64_t idle_time;
174 uint64_t energy_used;
175} btif_activity_energy_info_cb_t;
176
Priti Agherac0edf9f2014-06-26 11:23:51 -0700177typedef struct
178{
179 unsigned int manufact_id;
180}skip_sdp_entry_t;
181
The Android Open Source Project5738f832012-12-12 16:00:35 -0800182#define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id))
183
Priti Agherac0edf9f2014-06-26 11:23:51 -0700184#define MAX_SDP_BL_ENTRIES 3
185#define UUID_HUMAN_INTERFACE_DEVICE "00001124-0000-1000-8000-00805f9b34fb"
186
187static skip_sdp_entry_t sdp_blacklist[] = {{76}}; //Apple Mouse and Keyboard
188
189
The Android Open Source Project5738f832012-12-12 16:00:35 -0800190/* This flag will be true if HCI_Inquiry is in progress */
191static BOOLEAN btif_dm_inquiry_in_progress = FALSE;
192
Priti Agherac0edf9f2014-06-26 11:23:51 -0700193
194
Matthew Xie1e5109b2012-11-09 18:26:26 -0800195/************************************************************************************
196** Static variables
197************************************************************************************/
198static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'};
199
The Android Open Source Project5738f832012-12-12 16:00:35 -0800200/******************************************************************************
201** Static functions
202******************************************************************************/
203static btif_dm_pairing_cb_t pairing_cb;
204static btif_dm_oob_cb_t oob_cb;
205static void btif_dm_generic_evt(UINT16 event, char* p_param);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700206static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800207static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name);
208static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
209 DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800210#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
211static btif_dm_local_key_cb_t ble_local_key_cb;
212static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif);
213static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl);
214static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req);
215#endif
Satya Calloji6e2d9db2014-07-08 16:18:58 -0700216
217static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
218 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
219 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status);
220
Matthew Xie1e5109b2012-11-09 18:26:26 -0800221static char* btif_get_default_local_name();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800222/******************************************************************************
223** Externs
224******************************************************************************/
225extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
226extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
227extern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
228extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
Hemant Gupta10256872013-08-19 18:33:01 +0530229extern bt_status_t btif_hf_client_execute_service(BOOLEAN b_enable);
Hemant Gupta2dc99992014-04-18 12:54:08 +0530230extern bt_status_t btif_mce_execute_service(BOOLEAN b_enable);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800231extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700232extern 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 -0800233
234
235/******************************************************************************
236** Functions
237******************************************************************************/
238
239bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
240 BOOLEAN b_enable)
241{
242 /* Check the service_ID and invoke the profile's BT state changed API */
243 switch (service_id)
244 {
245 case BTA_HFP_SERVICE_ID:
246 case BTA_HSP_SERVICE_ID:
247 {
248 btif_hf_execute_service(b_enable);
249 }break;
250 case BTA_A2DP_SERVICE_ID:
251 {
252 btif_av_execute_service(b_enable);
253 }break;
254 case BTA_HID_SERVICE_ID:
255 {
256 btif_hh_execute_service(b_enable);
257 }break;
Hemant Gupta10256872013-08-19 18:33:01 +0530258 case BTA_HFP_HS_SERVICE_ID:
259 {
260 btif_hf_client_execute_service(b_enable);
261 }break;
Hemant Gupta2dc99992014-04-18 12:54:08 +0530262 case BTA_MAP_SERVICE_ID:
263 {
264 btif_mce_execute_service(b_enable);
265 }break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800266 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700267 BTIF_TRACE_ERROR("%s: Unknown service being enabled", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800268 return BT_STATUS_FAIL;
269 }
270 return BT_STATUS_SUCCESS;
271}
272
273/*******************************************************************************
274**
275** Function check_eir_remote_name
276**
277** Description Check if remote name is in the EIR data
278**
279** Returns TRUE if remote name found
280** Populate p_remote_name, if provided and remote name found
281**
282*******************************************************************************/
283static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
284 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
285{
286 UINT8 *p_eir_remote_name = NULL;
287 UINT8 remote_name_len = 0;
288
289 /* Check EIR for remote name and services */
290 if (p_search_data->inq_res.p_eir)
291 {
292 p_eir_remote_name = BTA_CheckEirData(p_search_data->inq_res.p_eir,
293 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
294 if (!p_eir_remote_name)
295 {
296 p_eir_remote_name = BTA_CheckEirData(p_search_data->inq_res.p_eir,
297 BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
298 }
299
300 if (p_eir_remote_name)
301 {
302 if (remote_name_len > BD_NAME_LEN)
303 remote_name_len = BD_NAME_LEN;
304
305 if (p_remote_name && p_remote_name_len)
306 {
307 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
308 *(p_remote_name + remote_name_len) = 0;
309 *p_remote_name_len = remote_name_len;
310 }
311
312 return TRUE;
313 }
314 }
315
316 return FALSE;
317
318}
319
320/*******************************************************************************
321**
322** Function check_cached_remote_name
323**
324** Description Check if remote name is in the NVRAM cache
325**
326** Returns TRUE if remote name found
327** Populate p_remote_name, if provided and remote name found
328**
329*******************************************************************************/
330static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
331 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
332{
333 bt_bdname_t bdname;
334 bt_bdaddr_t remote_bdaddr;
335 bt_property_t prop_name;
336
337 /* check if we already have it in our btif_storage cache */
338 bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
339 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
340 sizeof(bt_bdname_t), &bdname);
341 if (btif_storage_get_remote_device_property(
342 &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
343 {
344 if (p_remote_name && p_remote_name_len)
345 {
346 strcpy((char *)p_remote_name, (char *)bdname.name);
347 *p_remote_name_len = strlen((char *)p_remote_name);
348 }
349 return TRUE;
350 }
351
352 return FALSE;
353}
354
355BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
356{
357 uint32_t remote_cod;
358 bt_property_t prop_name;
359
360 /* check if we already have it in our btif_storage cache */
361 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
362 sizeof(uint32_t), &remote_cod);
363 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
364 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700365 BTIF_TRACE_ERROR("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800366 if ((remote_cod & 0x7ff) == cod)
367 return TRUE;
368 }
369
370 return FALSE;
371}
372
Priti Agheraebb1d752012-11-27 18:03:22 -0800373BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
374{
375 uint32_t remote_cod;
376 bt_property_t prop_name;
377
378 /* check if we already have it in our btif_storage cache */
379 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
380 sizeof(uint32_t), &remote_cod);
381 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
382 &prop_name) == BT_STATUS_SUCCESS)
383 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700384 BTIF_TRACE_DEBUG("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
Priti Agheraebb1d752012-11-27 18:03:22 -0800385 if ((remote_cod & 0x700) == cod)
386 return TRUE;
387 }
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700388 return FALSE;
389}
Priti Agheraebb1d752012-11-27 18:03:22 -0800390
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700391BOOLEAN check_hid_le(const bt_bdaddr_t *remote_bdaddr)
392{
393 uint32_t remote_dev_type;
394 bt_property_t prop_name;
395
396 /* check if we already have it in our btif_storage cache */
397 BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE,
398 sizeof(uint32_t), &remote_dev_type);
399 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
400 &prop_name) == BT_STATUS_SUCCESS)
401 {
402 if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE)
403 {
404 bdstr_t bdstr;
405 bd2str(remote_bdaddr, &bdstr);
406 if(btif_config_exist("Remote", bdstr, "HidAppId"))
407 return TRUE;
408 }
409 }
Priti Agheraebb1d752012-11-27 18:03:22 -0800410 return FALSE;
411}
412
Priti Agherac0edf9f2014-06-26 11:23:51 -0700413/*****************************************************************************
414**
415** Function check_sdp_bl
416**
417** Description Checks if a given device is blacklisted to skip sdp
418**
419** Parameters skip_sdp_entry
420**
421** Returns TRUE if the device is present in blacklist, else FALSE
422**
423*******************************************************************************/
424BOOLEAN check_sdp_bl(const bt_bdaddr_t *remote_bdaddr)
425{
426 UINT8 i = 0;
427 UINT16 manufacturer = 0;
428 UINT8 lmp_ver = 0;
429 UINT16 lmp_subver = 0;
430 tBTM_STATUS btm_status;
431 bt_property_t prop_name;
432 bt_remote_version_t info;
433 bt_status_t status;
434
435
436 if (remote_bdaddr == NULL)
437 return FALSE;
438
439/* fetch additional info about remote device used in iop query */
440 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)remote_bdaddr, &lmp_ver,
441 &manufacturer, &lmp_subver);
442
443
444
445 /* if not available yet, try fetching from config database */
446 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO,
447 sizeof(bt_remote_version_t), &info);
448
449 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
450 &prop_name) != BT_STATUS_SUCCESS)
451 {
452
453 return FALSE;
454 }
455 manufacturer = info.manufacturer;
456
457 for (int i = 0; i < MAX_SDP_BL_ENTRIES; i++)
458 {
459 if (manufacturer == sdp_blacklist[i].manufact_id)
460 return TRUE;
461 }
462 return FALSE;
463}
464
465
The Android Open Source Project5738f832012-12-12 16:00:35 -0800466static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
467{
468 /* Send bonding state only once - based on outgoing/incoming we may receive duplicates */
469 if ( (pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING) )
470 return;
471
472 if (pairing_cb.is_temp)
473 {
474 state = BT_BOND_STATE_NONE;
475 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700476 BTIF_TRACE_DEBUG("%s: state=%d prev_state=%d", __FUNCTION__, state, pairing_cb.state);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800477
478 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
479
480 if (state == BT_BOND_STATE_BONDING)
481 {
482 pairing_cb.state = state;
483 bdcpy(pairing_cb.bd_addr, bd_addr->address);
484 }
485 else
486 {
487 memset(&pairing_cb, 0, sizeof(pairing_cb));
488 }
489
490}
491
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800492/* store remote version in bt config to always have access
493 to it post pairing*/
494static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
495{
496 bt_property_t property;
497 UINT8 lmp_ver = 0;
498 UINT16 lmp_subver = 0;
499 UINT16 mfct_set = 0;
500 tBTM_STATUS btm_status;
501 bt_remote_version_t info;
502 bt_status_t status;
503 bdstr_t bdstr;
504
505 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
506 &mfct_set, &lmp_subver);
507
508 ALOGD("remote version info [%s]: %x, %x, %x", bd2str(p_bd, &bdstr),
509 lmp_ver, mfct_set, lmp_subver);
510
511 if (btm_status == BTM_SUCCESS)
512 {
513 /* always update cache to ensure we have availability whenever BTM API
514 is not populated */
515 info.manufacturer = mfct_set;
516 info.sub_ver = lmp_subver;
517 info.version = lmp_ver;
518 BTIF_STORAGE_FILL_PROPERTY(&property,
519 BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
520 &info);
521 status = btif_storage_set_remote_device_property(p_bd, &property);
522 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
523 }
524}
525
The Android Open Source Project5738f832012-12-12 16:00:35 -0800526
527static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
528 DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
529{
530 int num_properties = 0;
531 bt_property_t properties[3];
532 bt_bdaddr_t bdaddr;
533 bt_status_t status;
534 UINT32 cod;
535 bt_device_type_t dev_type;
536
537 memset(properties, 0, sizeof(properties));
538 bdcpy(bdaddr.address, bd_addr);
539
540 /* remote name */
541 if (strlen((const char *) bd_name))
542 {
543 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
544 BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
545 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
546 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
547 num_properties++;
548 }
549
550 /* class of device */
551 cod = devclass2uint(dev_class);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700552 BTIF_TRACE_DEBUG("%s():cod is 0x%06x", __FUNCTION__, cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800553 if ( cod == 0) {
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530554 /* Try to retrieve cod from storage */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700555 BTIF_TRACE_DEBUG("%s():cod is 0, checking cod from storage", __FUNCTION__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530556 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
557 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
558 status = btif_storage_get_remote_device_property(&bdaddr, &properties[num_properties]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700559 BTIF_TRACE_DEBUG("%s():cod retreived from storage is 0x%06x", __FUNCTION__, cod);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530560 if ( cod == 0) {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700561 BTIF_TRACE_DEBUG("%s():cod is again 0, set as unclassified", __FUNCTION__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530562 cod = COD_UNCLASSIFIED;
563 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800564 }
565
566 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
567 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
568 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
569 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
570 num_properties++;
571
572 /* device type */
573 dev_type = device_type;
574 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
575 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
576 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
577 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
578 num_properties++;
579
580 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
581 status, &bdaddr, num_properties, properties);
582}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800583
584/*******************************************************************************
585**
586** Function btif_dm_cb_hid_remote_name
587**
588** Description Remote name callback for HID device. Called in btif context
589** Special handling for HID devices
590**
591** Returns void
592**
593*******************************************************************************/
594static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
595{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700596 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 -0800597 if (pairing_cb.state == BT_BOND_STATE_BONDING)
598 {
599 bt_bdaddr_t remote_bd;
600
601 bdcpy(remote_bd.address, pairing_cb.bd_addr);
602
603 if (p_remote_name->status == BTM_SUCCESS)
604 {
605 bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
606 }
607 else
608 bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
609 }
610}
611
The Android Open Source Project5738f832012-12-12 16:00:35 -0800612/*******************************************************************************
613**
614** Function btif_dm_cb_create_bond
615**
616** Description Create bond initiated from the BTIF thread context
617** Special handling for HID devices
618**
619** Returns void
620**
621*******************************************************************************/
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700622static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800623{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800624 BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800625 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800626
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800627#if BLE_INCLUDED == TRUE
628 int device_type;
629 int addr_type;
630 bdstr_t bdstr;
631 bd2str(bd_addr, &bdstr);
Matthew Xie64c54792014-09-16 00:55:03 -0700632 if (transport == BT_TRANSPORT_LE)
633 {
634 if (!btif_config_get_int("Remote", (char const *)&bdstr,"DevType", &device_type))
635 {
636 btif_config_set_int("Remote", bdstr, "DevType", BT_DEVICE_TYPE_BLE);
637 }
638 if (btif_storage_get_remote_addr_type(bd_addr, &addr_type) != BT_STATUS_SUCCESS)
639 {
640 btif_storage_set_remote_addr_type(bd_addr, BLE_ADDR_PUBLIC);
641 }
642 }
lungtsai_lin3baff792014-08-29 13:47:47 +0800643 if((btif_config_get_int("Remote", (char const *)&bdstr,"DevType", &device_type) &&
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800644 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
lungtsai_lin3baff792014-08-29 13:47:47 +0800645 (device_type == BT_DEVICE_TYPE_BLE)) || (transport == BT_TRANSPORT_LE))
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800646 {
647 BTA_DmAddBleDevice(bd_addr->address, addr_type, BT_DEVICE_TYPE_BLE);
648 }
649#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800650
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800651#if BLE_INCLUDED == TRUE
652 if(is_hid && device_type != BT_DEVICE_TYPE_BLE)
653#else
654 if(is_hid)
655#endif
656 {
657 int status;
658 status = btif_hh_connect(bd_addr);
659 if(status != BT_STATUS_SUCCESS)
660 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800661 }
662 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800663 {
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700664 BTA_DmBondByTransport((UINT8 *)bd_addr->address, transport);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800665 }
666 /* Track originator of bond creation */
667 pairing_cb.is_local_initiated = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800668
669}
670
671/*******************************************************************************
672**
673** Function btif_dm_cb_remove_bond
674**
675** Description remove bond initiated from the BTIF thread context
676** Special handling for HID devices
677**
678** Returns void
679**
680*******************************************************************************/
681void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
682{
683 bdstr_t bdstr;
684 /*special handling for HID devices */
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700685 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there
686 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
687#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
688 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
689#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800690 {
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700691 BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800692 }
693}
694
695/*******************************************************************************
696**
Andre Eisenbach249f6002014-06-18 12:20:37 -0700697** Function btif_dm_get_connection_state
698**
699** Description Returns whether the remote device is currently connected
700**
701** Returns 0 if not connected
702**
703*******************************************************************************/
704uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr)
705{
706 return BTA_DmGetConnectionState((UINT8 *)bd_addr->address);
707}
708
709/*******************************************************************************
710**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800711** Function search_devices_copy_cb
712**
713** Description Deep copy callback for search devices event
714**
715** Returns void
716**
717*******************************************************************************/
718static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
719{
720 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
721 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
722
723 if (!p_src)
724 return;
725
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700726 BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800727 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
728 switch (event)
729 {
730 case BTA_DM_INQ_RES_EVT:
731 {
732 if (p_src_data->inq_res.p_eir)
733 {
734 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
735 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
736 }
737 }
738 break;
739
740 case BTA_DM_DISC_RES_EVT:
741 {
742 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
743 {
744 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
745 memcpy(p_dest_data->disc_res.p_raw_data,
746 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
747 }
748 }
749 break;
750 }
751}
752
753static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
754{
755 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
756 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
757
758 if (!p_src)
759 return;
760 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
761 switch (event)
762 {
763 case BTA_DM_DISC_RES_EVT:
764 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530765 if (p_src_data->disc_res.result == BTA_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800766 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530767 if (p_src_data->disc_res.num_uuids > 0)
768 {
769 p_dest_data->disc_res.p_uuid_list =
770 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
771 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
772 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
773 GKI_freebuf(p_src_data->disc_res.p_uuid_list);
774 }
775 if (p_src_data->disc_res.p_raw_data != NULL)
776 {
777 GKI_freebuf(p_src_data->disc_res.p_raw_data);
778 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800779 }
780 } break;
781 }
782}
783/******************************************************************************
784**
785** BTIF DM callback events
786**
787*****************************************************************************/
788
789/*******************************************************************************
790**
791** Function btif_dm_pin_req_evt
792**
793** Description Executes pin request event in btif context
794**
795** Returns void
796**
797*******************************************************************************/
798static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
799{
800 bt_bdaddr_t bd_addr;
801 bt_bdname_t bd_name;
802 UINT32 cod;
803 bt_pin_code_t pin_code;
804
805 /* Remote properties update */
806 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
807 p_pin_req->dev_class, BT_DEVICE_TYPE_BREDR);
808
809 bdcpy(bd_addr.address, p_pin_req->bd_addr);
810 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
811
812 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
813
814 cod = devclass2uint(p_pin_req->dev_class);
815
816 if ( cod == 0) {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700817 BTIF_TRACE_DEBUG("%s():cod is 0, set as unclassified", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800818 cod = COD_UNCLASSIFIED;
819 }
820
821 /* check for auto pair possiblity only if bond was initiated by local device */
822 if (pairing_cb.is_local_initiated)
823 {
824 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
825 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
826 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
827 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
828 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
829 check_cod(&bd_addr, COD_HID_POINTING))
830 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700831 BTIF_TRACE_DEBUG("%s()cod matches for auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800832 /* Check if this device can be auto paired */
833 if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) &&
834 (pairing_cb.autopair_attempts == 0))
835 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700836 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800837 pin_code.pin[0] = 0x30;
838 pin_code.pin[1] = 0x30;
839 pin_code.pin[2] = 0x30;
840 pin_code.pin[3] = 0x30;
841
842 pairing_cb.autopair_attempts++;
843 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
844 return;
845 }
846 }
847 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
848 check_cod(&bd_addr, COD_HID_COMBO))
849 {
850 if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) &&
851 (pairing_cb.autopair_attempts == 0))
852 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700853 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800854 pin_code.pin[0] = 0x30;
855 pin_code.pin[1] = 0x30;
856 pin_code.pin[2] = 0x30;
857 pin_code.pin[3] = 0x30;
858
859 pairing_cb.autopair_attempts++;
860 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
861 return;
862 }
863 }
864 }
865 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
866 &bd_addr, &bd_name, cod);
867}
868
869/*******************************************************************************
870**
871** Function btif_dm_ssp_cfm_req_evt
872**
873** Description Executes SSP confirm request event in btif context
874**
875** Returns void
876**
877*******************************************************************************/
878static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
879{
880 bt_bdaddr_t bd_addr;
881 bt_bdname_t bd_name;
882 UINT32 cod;
883 BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
884
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700885 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800886
887 /* Remote properties update */
888 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
889 p_ssp_cfm_req->dev_class, BT_DEVICE_TYPE_BREDR);
890
891 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
892 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
893
894 /* Set the pairing_cb based on the local & remote authentication requirements */
895 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
896
897 /* if just_works and bonding bit is not set treat this as temporary */
898 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 -0800899 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
900 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800901 pairing_cb.is_temp = TRUE;
902 else
903 pairing_cb.is_temp = FALSE;
904
905 pairing_cb.is_ssp = TRUE;
906
907 /* If JustWorks auto-accept */
908 if (p_ssp_cfm_req->just_works)
909 {
910 /* Pairing consent for JustWorks needed if:
911 * 1. Incoming pairing is detected AND
912 * 2. local IO capabilities are DisplayYesNo AND
913 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
914 */
915 if ((is_incoming) && ((p_ssp_cfm_req->loc_io_caps == 0x01) &&
916 (p_ssp_cfm_req->rmt_io_caps == 0x00 || p_ssp_cfm_req->rmt_io_caps == 0x03)))
917 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700918 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 -0800919 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
920 }
921 else
922 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700923 BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800924 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
925 return;
926 }
927 }
928
929 cod = devclass2uint(p_ssp_cfm_req->dev_class);
930
931 if ( cod == 0) {
932 ALOGD("cod is 0, set as unclassified");
933 cod = COD_UNCLASSIFIED;
934 }
935
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700936 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800937 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
938 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
939 p_ssp_cfm_req->num_val);
940}
941
942static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
943{
944 bt_bdaddr_t bd_addr;
945 bt_bdname_t bd_name;
946 UINT32 cod;
947
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700948 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800949
950 /* Remote properties update */
951 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
952 p_ssp_key_notif->dev_class, BT_DEVICE_TYPE_BREDR);
953
954 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
955 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
956
957 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
958 pairing_cb.is_ssp = TRUE;
959 cod = devclass2uint(p_ssp_key_notif->dev_class);
960
961 if ( cod == 0) {
962 ALOGD("cod is 0, set as unclassified");
963 cod = COD_UNCLASSIFIED;
964 }
965
966 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
967 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
968 p_ssp_key_notif->passkey);
969}
970/*******************************************************************************
971**
972** Function btif_dm_auth_cmpl_evt
973**
974** Description Executes authentication complete event in btif context
975**
976** Returns void
977**
978*******************************************************************************/
979static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
980{
981 /* Save link key, if not temporary */
982 bt_bdaddr_t bd_addr;
983 bt_status_t status = BT_STATUS_FAIL;
984 bt_bond_state_t state = BT_BOND_STATE_NONE;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700985 BOOLEAN skip_sdp = FALSE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800986
987 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
988 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
989 {
990 if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) || (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
991 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) || (!pairing_cb.is_temp))
992 {
993 bt_status_t ret;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700994 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, is_temp=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800995 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.is_temp);
996 ret = btif_storage_add_bonded_device(&bd_addr,
997 p_auth_cmpl->key, p_auth_cmpl->key_type,
998 pairing_cb.pin_code_len);
999 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
1000 }
1001 else
1002 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001003 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 -08001004 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.is_temp);
Hemant Guptab820aec2013-12-24 19:59:57 +05301005 if(pairing_cb.is_temp)
1006 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001007 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
Hemant Guptab820aec2013-12-24 19:59:57 +05301008 __FUNCTION__);
1009 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1010 return;
1011 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001012 }
1013 }
Priti Agherac0edf9f2014-06-26 11:23:51 -07001014
1015 // Skip SDP for certain HID Devices
The Android Open Source Project5738f832012-12-12 16:00:35 -08001016 if (p_auth_cmpl->success)
1017 {
Andre Eisenbach31a64002014-10-14 14:29:19 -07001018 pairing_cb.timeout_retries = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001019 status = BT_STATUS_SUCCESS;
1020 state = BT_BOND_STATE_BONDED;
Priti Agherac0edf9f2014-06-26 11:23:51 -07001021 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001022
Priti Agherac0edf9f2014-06-26 11:23:51 -07001023 if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr, COD_HID_MAJOR))
1024 {
1025 ALOGW("%s:skip SDP",
1026 __FUNCTION__);
1027 skip_sdp = TRUE;
1028 }
1029 if(!pairing_cb.is_local_initiated && skip_sdp)
1030 {
1031 bond_state_changed(status, &bd_addr, state);
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -08001032
Priti Agherac0edf9f2014-06-26 11:23:51 -07001033 ALOGW("%s: Incoming HID Connection",__FUNCTION__);
1034 bt_property_t prop;
1035 bt_bdaddr_t bd_addr;
1036 bt_uuid_t uuid;
1037 char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE;
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -08001038
Priti Agherac0edf9f2014-06-26 11:23:51 -07001039 string_to_uuid(uuid_str, &uuid);
1040
1041 prop.type = BT_PROPERTY_UUIDS;
1042 prop.val = uuid.uu;
1043 prop.len = MAX_UUID_SIZE;
1044
1045 /* Send the event to the BTIF */
1046 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1047 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1048 }
1049 else
1050 {
1051 /* Trigger SDP on the device */
1052 pairing_cb.sdp_attempts = 1;;
1053
1054 if(btif_dm_inquiry_in_progress)
1055 btif_dm_cancel_discovery();
1056
1057 btif_dm_get_remote_services(&bd_addr);
1058 }
1059 /* 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 -08001060 }
1061 else
1062 {
1063 /*Map the HCI fail reason to bt status */
1064 switch(p_auth_cmpl->fail_reason)
1065 {
1066 case HCI_ERR_PAGE_TIMEOUT:
Andre Eisenbach31a64002014-10-14 14:29:19 -07001067 if (blacklistPairingRetries(bd_addr.address) && pairing_cb.timeout_retries)
1068 {
1069 BTIF_TRACE_WARNING("%s() - Pairing timeout; retrying (%d) ...", __FUNCTION__, pairing_cb.timeout_retries);
1070 --pairing_cb.timeout_retries;
1071 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
1072 return;
1073 }
1074 /* Fall-through */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001075 case HCI_ERR_CONNECTION_TOUT:
1076 status = BT_STATUS_RMT_DEV_DOWN;
1077 break;
1078
Hemant Guptaaef7a672013-07-31 19:00:12 +05301079 case HCI_ERR_PAIRING_NOT_ALLOWED:
1080 status = BT_STATUS_AUTH_REJECTED;
1081 break;
1082
Hemant Guptab4801442014-01-07 18:11:15 +05301083 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
1084 status = BT_STATUS_AUTH_FAILURE;
1085 break;
1086
The Android Open Source Project5738f832012-12-12 16:00:35 -08001087 /* map the auth failure codes, so we can retry pairing if necessary */
1088 case HCI_ERR_AUTH_FAILURE:
Hemant Gupta59a88ec2014-03-19 19:01:35 +05301089 case HCI_ERR_KEY_MISSING:
Zhihai Xua7ea8092013-11-27 14:10:53 +05301090 btif_storage_remove_bonded_device(&bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001091 case HCI_ERR_HOST_REJECT_SECURITY:
1092 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
1093 case HCI_ERR_UNIT_KEY_USED:
1094 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
1095 case HCI_ERR_INSUFFCIENT_SECURITY:
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301096 case HCI_ERR_PEER_USER:
1097 case HCI_ERR_UNSPECIFIED:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001098 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d",
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301099 __FUNCTION__, p_auth_cmpl->fail_reason);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001100 if (pairing_cb.autopair_attempts == 1)
1101 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001102 BTIF_TRACE_DEBUG("%s(): Adding device to blacklist ", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001103
1104 /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class */
1105 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
1106 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
1107 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
1108 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
1109 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
1110 check_cod(&bd_addr, COD_HID_POINTING))
1111 {
1112 btif_storage_add_device_to_autopair_blacklist (&bd_addr);
1113 }
1114 pairing_cb.autopair_attempts++;
1115
1116 /* Create the Bond once again */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001117 BTIF_TRACE_DEBUG("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07001118 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001119 return;
1120 }
1121 else
1122 {
1123 /* if autopair attempts are more than 1, or not attempted */
1124 status = BT_STATUS_AUTH_FAILURE;
1125 }
1126 break;
1127
1128 default:
1129 status = BT_STATUS_FAIL;
1130 }
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301131 /* Special Handling for HID Devices */
1132 if (check_cod(&bd_addr, COD_HID_POINTING)) {
1133 /* Remove Device as bonded in nvram as authentication failed */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001134 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __FUNCTION__);
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301135 btif_storage_remove_bonded_device(&bd_addr);
1136 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001137 bond_state_changed(status, &bd_addr, state);
1138 }
1139}
1140
1141/******************************************************************************
1142**
1143** Function btif_dm_search_devices_evt
1144**
1145** Description Executes search devices callback events in btif context
1146**
1147** Returns void
1148**
1149******************************************************************************/
1150static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
1151{
1152 tBTA_DM_SEARCH *p_search_data;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001153 BTIF_TRACE_EVENT("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001154
1155 switch (event)
1156 {
1157 case BTA_DM_DISC_RES_EVT:
1158 {
1159 p_search_data = (tBTA_DM_SEARCH *)p_param;
1160 /* Remote name update */
1161 if (strlen((const char *) p_search_data->disc_res.bd_name))
1162 {
1163 bt_property_t properties[1];
1164 bt_bdaddr_t bdaddr;
1165 bt_status_t status;
1166
1167 properties[0].type = BT_PROPERTY_BDNAME;
1168 properties[0].val = p_search_data->disc_res.bd_name;
1169 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
1170 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
1171
1172 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
1173 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
1174 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1175 status, &bdaddr, 1, properties);
1176 }
1177 /* TODO: Services? */
1178 }
1179 break;
1180
1181 case BTA_DM_INQ_RES_EVT:
1182 {
1183 /* inquiry result */
1184 UINT32 cod;
1185 UINT8 *p_eir_remote_name = NULL;
1186 bt_bdname_t bdname;
1187 bt_bdaddr_t bdaddr;
1188 UINT8 remote_name_len;
1189 UINT8 *p_cached_name = NULL;
1190 tBTA_SERVICE_MASK services = 0;
1191 bdstr_t bdstr;
1192
1193 p_search_data = (tBTA_DM_SEARCH *)p_param;
1194 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
1195
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001196 BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __FUNCTION__, bd2str(&bdaddr, &bdstr),
The Android Open Source Project5738f832012-12-12 16:00:35 -08001197#if (BLE_INCLUDED == TRUE)
1198 p_search_data->inq_res.device_type);
1199#else
1200 BT_DEVICE_TYPE_BREDR);
1201#endif
1202 bdname.name[0] = 0;
1203
1204 cod = devclass2uint (p_search_data->inq_res.dev_class);
1205
1206 if ( cod == 0) {
1207 ALOGD("cod is 0, set as unclassified");
1208 cod = COD_UNCLASSIFIED;
1209 }
1210
1211 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1212 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1213
1214 /* Check EIR for remote name and services */
1215 if (p_search_data->inq_res.p_eir)
1216 {
1217 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001218 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001219 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */
1220 }
1221
1222
1223 {
1224 bt_property_t properties[5];
1225 bt_device_type_t dev_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001226 UINT8 addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001227 uint32_t num_properties = 0;
1228 bt_status_t status;
1229
1230 memset(properties, 0, sizeof(properties));
1231 /* BD_ADDR */
1232 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1233 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1234 num_properties++;
1235 /* BD_NAME */
1236 /* Don't send BDNAME if it is empty */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001237 if (bdname.name[0])
1238 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001239 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1240 BT_PROPERTY_BDNAME,
1241 strlen((char *)bdname.name), &bdname);
1242 num_properties++;
1243 }
1244
1245 /* DEV_CLASS */
1246 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1247 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1248 num_properties++;
1249 /* DEV_TYPE */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001250#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001251 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1252 dev_type = p_search_data->inq_res.device_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001253 addr_type = p_search_data->inq_res.ble_addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001254#else
1255 dev_type = BT_DEVICE_TYPE_BREDR;
1256#endif
1257 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1258 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1259 num_properties++;
1260 /* RSSI */
1261 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1262 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1263 &(p_search_data->inq_res.rssi));
1264 num_properties++;
1265
1266 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1267 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001268#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1269 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001270 if (( dev_type == BT_DEVICE_TYPE_DUMO)&&
1271 (p_search_data->inq_res.flag & BTA_BLE_DMT_CONTROLLER_SPT) &&
1272 (p_search_data->inq_res.flag & BTA_BLE_DMT_HOST_SPT))
1273 {
1274 btif_storage_set_dmt_support_type (&bdaddr, TRUE);
1275 }
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001276 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1277#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001278 /* Callback to notify upper layer of device */
1279 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1280 num_properties, properties);
1281 }
1282 }
1283 break;
1284
1285 case BTA_DM_INQ_CMPL_EVT:
1286 {
Satya Calloji6e2d9db2014-07-08 16:18:58 -07001287#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1288 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1289 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1290 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1291 bte_scan_filt_param_cfg_evt, 0);
1292#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001293 }
1294 break;
1295 case BTA_DM_DISC_CMPL_EVT:
1296 {
1297 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1298 }
1299 break;
1300 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1301 {
1302 /* if inquiry is not in progress and we get a cancel event, then
1303 * it means we are done with inquiry, but remote_name fetches are in
1304 * progress
1305 *
1306 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt
1307 * but instead wait for the cancel_cmpl_evt via the Busy Level
1308 *
1309 */
1310 if (btif_dm_inquiry_in_progress == FALSE)
1311 {
1312 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1313 }
1314 }
1315 break;
1316 }
1317}
1318
1319/*******************************************************************************
1320**
1321** Function btif_dm_search_services_evt
1322**
1323** Description Executes search services event in btif context
1324**
1325** Returns void
1326**
1327*******************************************************************************/
1328static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1329{
1330 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1331
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001332 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001333 switch (event)
1334 {
1335 case BTA_DM_DISC_RES_EVT:
1336 {
1337 bt_uuid_t uuid_arr[BT_MAX_NUM_UUIDS]; /* Max 32 services */
1338 bt_property_t prop;
1339 uint32_t i = 0, j = 0;
1340 bt_bdaddr_t bd_addr;
1341 bt_status_t ret;
1342
1343 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1344
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001345 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001346 p_data->disc_res.result, p_data->disc_res.services);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001347 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1348 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1349 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1350 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001351 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001352 pairing_cb.sdp_attempts++;
1353 btif_dm_get_remote_services(&bd_addr);
1354 return;
1355 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001356 prop.type = BT_PROPERTY_UUIDS;
1357 prop.len = 0;
1358 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1359 {
1360 prop.val = p_data->disc_res.p_uuid_list;
1361 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1362 for (i=0; i < p_data->disc_res.num_uuids; i++)
1363 {
1364 char temp[256];
1365 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 -07001366 BTIF_TRACE_ERROR("Index: %d uuid:%s", i, temp);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001367 }
1368 }
1369
1370 /* onUuidChanged requires getBondedDevices to be populated.
1371 ** bond_state_changed needs to be sent prior to remote_device_property
1372 */
1373 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1374 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0)&&
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001375 pairing_cb.sdp_attempts > 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001376 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001377 BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001378 __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001379 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001380 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1381 }
1382
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001383 if(p_data->disc_res.num_uuids != 0)
1384 {
1385 /* Also write this to the NVRAM */
1386 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1387 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1388 /* Send the event to the BTIF */
1389 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1390 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1391 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001392 }
1393 break;
1394
1395 case BTA_DM_DISC_CMPL_EVT:
1396 /* fixme */
1397 break;
1398
Matthew Xie607e3b72013-08-15 19:30:48 -07001399#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001400 case BTA_DM_DISC_BLE_RES_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001401 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__,
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001402 p_data->disc_ble_res.service.uu.uuid16);
1403 bt_uuid_t uuid;
1404 int i = 0;
1405 int j = 15;
1406 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1407 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001408 BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001409 bt_property_t prop;
1410 bt_bdaddr_t bd_addr;
1411 char temp[256];
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001412 bt_status_t ret;
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001413
1414 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1415
1416 while(i < j )
1417 {
1418 unsigned char c = uuid.uu[j];
1419 uuid.uu[j] = uuid.uu[i];
1420 uuid.uu[i] = c;
1421 i++;
1422 j--;
1423 }
1424
1425 uuid_to_string(&uuid, temp);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001426 BTIF_TRACE_ERROR(" uuid:%s", temp);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001427
1428 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1429 prop.type = BT_PROPERTY_UUIDS;
1430 prop.val = uuid.uu;
1431 prop.len = MAX_UUID_SIZE;
1432
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001433 /* Also write this to the NVRAM */
1434 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1435 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1436
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001437 /* Send the event to the BTIF */
1438 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1439 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1440
1441 }
1442 break;
Matthew Xie607e3b72013-08-15 19:30:48 -07001443#endif /* BLE_INCLUDED */
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001444
The Android Open Source Project5738f832012-12-12 16:00:35 -08001445 default:
1446 {
1447 ASSERTC(0, "unhandled search services event", event);
1448 }
1449 break;
1450 }
1451}
1452
1453/*******************************************************************************
1454**
1455** Function btif_dm_remote_service_record_evt
1456**
1457** Description Executes search service record event in btif context
1458**
1459** Returns void
1460**
1461*******************************************************************************/
1462static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1463{
1464 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1465
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001466 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001467 switch (event)
1468 {
1469 case BTA_DM_DISC_RES_EVT:
1470 {
1471 bt_service_record_t rec;
1472 bt_property_t prop;
1473 uint32_t i = 0;
1474 bt_bdaddr_t bd_addr;
1475
1476 memset(&rec, 0, sizeof(bt_service_record_t));
1477 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1478
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001479 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001480 p_data->disc_res.result, p_data->disc_res.services);
1481 prop.type = BT_PROPERTY_SERVICE_RECORD;
1482 prop.val = (void*)&rec;
1483 prop.len = sizeof(rec);
1484
1485 /* disc_res.result is overloaded with SCN. Cannot check result */
1486 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1487 /* TODO: Get the UUID as well */
1488 rec.channel = p_data->disc_res.result - 3;
1489 /* TODO: Need to get the service name using p_raw_data */
1490 rec.name[0] = 0;
1491
1492 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1493 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1494 }
1495 break;
1496
1497 default:
1498 {
1499 ASSERTC(0, "unhandled remote service record event", event);
1500 }
1501 break;
1502 }
1503}
1504
1505/*******************************************************************************
1506**
1507** Function btif_dm_upstreams_cback
1508**
1509** Description Executes UPSTREAMS events in btif context
1510**
1511** Returns void
1512**
1513*******************************************************************************/
1514static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1515{
1516 tBTA_DM_SEC_EVT dm_event = (tBTA_DM_SEC_EVT)event;
1517 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1518 tBTA_SERVICE_MASK service_mask;
1519 uint32_t i;
1520 bt_bdaddr_t bd_addr;
1521
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001522 BTIF_TRACE_EVENT("btif_dm_upstreams_cback ev: %s", dump_dm_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001523
1524 switch (event)
1525 {
1526 case BTA_DM_ENABLE_EVT:
1527 {
1528 BD_NAME bdname;
1529 bt_status_t status;
1530 bt_property_t prop;
1531 prop.type = BT_PROPERTY_BDNAME;
1532 prop.len = BD_NAME_LEN;
1533 prop.val = (void*)bdname;
1534
1535 status = btif_storage_get_adapter_property(&prop);
Matthew Xie1e5109b2012-11-09 18:26:26 -08001536 if (status == BT_STATUS_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001537 {
1538 /* A name exists in the storage. Make this the device name */
1539 BTA_DmSetDeviceName((char*)prop.val);
1540 }
Matthew Xie1e5109b2012-11-09 18:26:26 -08001541 else
1542 {
1543 /* Storage does not have a name yet.
1544 * Use the default name and write it to the chip
1545 */
1546 BTA_DmSetDeviceName(btif_get_default_local_name());
1547 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001548
Andre Eisenbacha015a832014-09-11 14:09:40 -07001549#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1550 /* Enable local privacy */
1551 BTA_DmBleConfigLocalPrivacy(TRUE);
1552#endif
1553
The Android Open Source Project5738f832012-12-12 16:00:35 -08001554 /* for each of the enabled services in the mask, trigger the profile
1555 * enable */
1556 service_mask = btif_get_enabled_services_mask();
1557 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1558 {
1559 if (service_mask &
1560 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1561 {
1562 btif_in_execute_service_request(i, TRUE);
1563 }
1564 }
1565 /* clear control blocks */
1566 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1567
1568 /* This function will also trigger the adapter_properties_cb
1569 ** and bonded_devices_info_cb
1570 */
1571 btif_storage_load_bonded_devices();
1572
1573 btif_storage_load_autopair_device_list();
1574
1575 btif_enable_bluetooth_evt(p_data->enable.status, p_data->enable.bd_addr);
1576 }
1577 break;
1578
1579 case BTA_DM_DISABLE_EVT:
1580 /* for each of the enabled services in the mask, trigger the profile
1581 * disable */
1582 service_mask = btif_get_enabled_services_mask();
1583 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1584 {
1585 if (service_mask &
1586 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1587 {
1588 btif_in_execute_service_request(i, FALSE);
1589 }
1590 }
1591 btif_disable_bluetooth_evt();
1592 break;
1593
1594 case BTA_DM_PIN_REQ_EVT:
1595 btif_dm_pin_req_evt(&p_data->pin_req);
1596 break;
1597
1598 case BTA_DM_AUTH_CMPL_EVT:
1599 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1600 break;
1601
1602 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1603 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1604 {
1605 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1606 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1607 }
1608 break;
1609
1610 case BTA_DM_SP_CFM_REQ_EVT:
1611 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1612 break;
1613 case BTA_DM_SP_KEY_NOTIF_EVT:
1614 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1615 break;
1616
1617 case BTA_DM_DEV_UNPAIRED_EVT:
1618 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1619
1620 /*special handling for HID devices */
1621 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -07001622 btif_hh_remove_device(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001623 #endif
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001624 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1625 btif_storage_remove_ble_bonding_keys(&bd_addr);
1626 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001627 btif_storage_remove_bonded_device(&bd_addr);
1628 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1629 break;
1630
1631 case BTA_DM_BUSY_LEVEL_EVT:
1632 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001633
1634 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001635 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001636 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001637 {
1638 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1639 BT_DISCOVERY_STARTED);
1640 btif_dm_inquiry_in_progress = TRUE;
1641 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001642 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001643 {
1644 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1645 BT_DISCOVERY_STOPPED);
1646 btif_dm_inquiry_in_progress = FALSE;
1647 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001648 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001649 {
1650 btif_dm_inquiry_in_progress = FALSE;
1651 }
1652 }
1653 }break;
1654
1655 case BTA_DM_LINK_UP_EVT:
1656 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001657 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001658
1659 btif_update_remote_version_property(&bd_addr);
1660
The Android Open Source Project5738f832012-12-12 16:00:35 -08001661 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1662 &bd_addr, BT_ACL_STATE_CONNECTED);
1663 break;
1664
1665 case BTA_DM_LINK_DOWN_EVT:
1666 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001667 BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001668 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1669 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1670 break;
1671
1672 case BTA_DM_HW_ERROR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001673 BTIF_TRACE_ERROR("Received H/W Error. ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001674 /* Flush storage data */
1675 btif_config_flush();
1676 usleep(100000); /* 100milliseconds */
1677 /* Killing the process to force a restart as part of fault tolerance */
1678 kill(getpid(), SIGKILL);
1679 break;
1680
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001681#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1682 case BTA_DM_BLE_KEY_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001683 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 -08001684
1685 /* If this pairing is by-product of local initiated GATT client Read or Write,
1686 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1687 have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1688 if (pairing_cb.state != BT_BOND_STATE_BONDING)
1689 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001690 BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001691 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1692 BT_BOND_STATE_BONDING);
1693 }
1694 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1695 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001696 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001697 break;
1698 }
1699
1700 switch (p_data->ble_key.key_type)
1701 {
1702 case BTA_LE_KEY_PENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001703 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001704 pairing_cb.ble.is_penc_key_rcvd = TRUE;
1705 memcpy(pairing_cb.ble.penc_key.ltk,p_data->ble_key.key_value.penc_key.ltk, 16);
1706 memcpy(pairing_cb.ble.penc_key.rand, p_data->ble_key.key_value.penc_key.rand,8);
1707 pairing_cb.ble.penc_key.ediv = p_data->ble_key.key_value.penc_key.ediv;
1708 pairing_cb.ble.penc_key.sec_level = p_data->ble_key.key_value.penc_key.sec_level;
1709
1710 for (i=0; i<16; i++)
1711 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001712 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 -08001713 }
1714 for (i=0; i<8; i++)
1715 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001716 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 -08001717 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001718 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.ediv=0x%04x",pairing_cb.ble.penc_key.ediv);
1719 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.sec_level=0x%02x",pairing_cb.ble.penc_key.sec_level);
1720 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 -08001721 break;
1722
1723 case BTA_LE_KEY_PID:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001724 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001725 pairing_cb.ble.is_pid_key_rcvd = TRUE;
1726 memcpy(pairing_cb.ble.pid_key, p_data->ble_key.key_value.pid_key.irk, 16);
1727 for (i=0; i<16; i++)
1728 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001729 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 -08001730 }
1731 break;
1732
1733 case BTA_LE_KEY_PCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001734 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001735 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1736 pairing_cb.ble.pcsrk_key.counter = p_data->ble_key.key_value.pcsrk_key.counter;
1737 pairing_cb.ble.pcsrk_key.sec_level = p_data->ble_key.key_value.pcsrk_key.sec_level;
1738 memcpy(pairing_cb.ble.pcsrk_key.csrk,p_data->ble_key.key_value.pcsrk_key.csrk,16);
1739
1740 for (i=0; i<16; i++)
1741 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001742 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 -08001743 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001744 BTIF_TRACE_DEBUG("pairing_cb.ble.pcsrk_key.counter=0x%08x",pairing_cb.ble.pcsrk_key.counter);
1745 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 -08001746 break;
1747
1748 case BTA_LE_KEY_LENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001749 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001750 pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1751 pairing_cb.ble.lenc_key.div = p_data->ble_key.key_value.lenc_key.div;
1752 pairing_cb.ble.lenc_key.key_size = p_data->ble_key.key_value.lenc_key.key_size;
1753 pairing_cb.ble.lenc_key.sec_level = p_data->ble_key.key_value.lenc_key.sec_level;
1754
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001755 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.div=0x%04x",pairing_cb.ble.lenc_key.div);
1756 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.key_size=0x%02x",pairing_cb.ble.lenc_key.key_size);
1757 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 -08001758 break;
1759
1760
1761
1762 case BTA_LE_KEY_LCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001763 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001764 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1765 pairing_cb.ble.lcsrk_key.counter = p_data->ble_key.key_value.lcsrk_key.counter;
1766 pairing_cb.ble.lcsrk_key.div = p_data->ble_key.key_value.lcsrk_key.div;
1767 pairing_cb.ble.lcsrk_key.sec_level = p_data->ble_key.key_value.lcsrk_key.sec_level;
1768
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001769 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.div=0x%04x",pairing_cb.ble.lcsrk_key.div);
1770 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.counter=0x%08x",pairing_cb.ble.lcsrk_key.counter);
1771 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 -08001772
1773 break;
1774
1775 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001776 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001777 break;
1778 }
1779
1780 break;
1781 case BTA_DM_BLE_SEC_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001782 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001783 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1784 break;
1785 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001786 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001787 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1788 break;
1789 case BTA_DM_BLE_PASSKEY_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001790 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001791 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1792 break;
1793 case BTA_DM_BLE_OOB_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001794 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001795 break;
1796 case BTA_DM_BLE_LOCAL_IR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001797 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001798 ble_local_key_cb.is_id_keys_rcvd = TRUE;
1799 memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1800 memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1801 memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1802 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1803 BTIF_DM_LE_LOCAL_KEY_IR,
1804 BT_OCTET16_LEN);
1805 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1806 BTIF_DM_LE_LOCAL_KEY_IRK,
1807 BT_OCTET16_LEN);
1808 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1809 BTIF_DM_LE_LOCAL_KEY_DHK,
1810 BT_OCTET16_LEN);
1811 break;
1812 case BTA_DM_BLE_LOCAL_ER_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001813 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001814 ble_local_key_cb.is_er_rcvd = TRUE;
1815 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1816 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1817 BTIF_DM_LE_LOCAL_KEY_ER,
1818 BT_OCTET16_LEN);
1819 break;
1820
1821 case BTA_DM_BLE_AUTH_CMPL_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001822 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001823 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1824 break;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001825
1826 case BTA_DM_LE_FEATURES_READ:
1827 {
1828 tBTM_BLE_VSC_CB cmn_vsc_cb;
1829 bt_local_le_features_t local_le_features;
1830 char buf[512];
1831 bt_property_t prop;
1832 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1833 prop.val = (void*)buf;
1834 prop.len = sizeof(buf);
1835
1836 /* LE features are not stored in storage. Should be retrived from stack */
1837 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1838 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1839
1840 prop.len = sizeof (bt_local_le_features_t);
1841 if (cmn_vsc_cb.filter_support == 1)
1842 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1843 else
1844 local_le_features.max_adv_filter_supported = 0;
1845 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1846 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1847 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
Satya Callojid773c2c2014-07-29 22:08:55 -07001848 local_le_features.scan_result_storage_size_hibyte =
1849 (cmn_vsc_cb.tot_scan_results_strg >> 8) & (0xFF);
1850 local_le_features.scan_result_storage_size_lobyte =
1851 (cmn_vsc_cb.tot_scan_results_strg) & (0xFF);
Satya Callojiefaddcb2014-07-28 23:22:05 -07001852 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001853 memcpy(prop.val, &local_le_features, prop.len);
1854 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
1855 break;
1856 }
Satya Callojie5ba8842014-07-03 17:18:02 -07001857
1858 case BTA_DM_ENER_INFO_READ:
1859 {
1860 btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param;
1861 bt_activity_energy_info energy_info;
1862 energy_info.status = p_ener_data->status;
1863 energy_info.ctrl_state = p_ener_data->ctrl_state;
1864 energy_info.rx_time = p_ener_data->rx_time;
1865 energy_info.tx_time = p_ener_data->tx_time;
1866 energy_info.idle_time = p_ener_data->idle_time;
1867 energy_info.energy_used = p_ener_data->energy_used;
1868 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info);
1869 break;
1870 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001871#endif
1872
The Android Open Source Project5738f832012-12-12 16:00:35 -08001873 case BTA_DM_AUTHORIZE_EVT:
1874 case BTA_DM_SIG_STRENGTH_EVT:
1875 case BTA_DM_SP_RMT_OOB_EVT:
1876 case BTA_DM_SP_KEYPRESS_EVT:
1877 case BTA_DM_ROLE_CHG_EVT:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001878
The Android Open Source Project5738f832012-12-12 16:00:35 -08001879 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001880 BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001881 break;
1882 }
1883} /* btui_security_cback() */
1884
1885
1886/*******************************************************************************
1887**
1888** Function btif_dm_generic_evt
1889**
1890** Description Executes non-BTA upstream events in BTIF context
1891**
1892** Returns void
1893**
1894*******************************************************************************/
1895static void btif_dm_generic_evt(UINT16 event, char* p_param)
1896{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001897 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001898 switch(event)
1899 {
1900 case BTIF_DM_CB_DISCOVERY_STARTED:
1901 {
1902 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
1903 }
1904 break;
1905
1906 case BTIF_DM_CB_CREATE_BOND:
1907 {
Andre Eisenbach31a64002014-10-14 14:29:19 -07001908 pairing_cb.timeout_retries = NUM_TIMEOUT_RETRIES;
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07001909 btif_dm_create_bond_cb_t *create_bond_cb = (btif_dm_create_bond_cb_t*)p_param;
1910 btif_dm_cb_create_bond(&create_bond_cb->bdaddr, create_bond_cb->transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001911 }
1912 break;
1913
1914 case BTIF_DM_CB_REMOVE_BOND:
1915 {
1916 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
1917 }
1918 break;
1919
1920 case BTIF_DM_CB_HID_REMOTE_NAME:
1921 {
1922 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
1923 }
1924 break;
1925
1926 case BTIF_DM_CB_BOND_STATE_BONDING:
1927 {
1928 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
1929 }
1930 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001931 case BTIF_DM_CB_LE_TX_TEST:
1932 case BTIF_DM_CB_LE_RX_TEST:
1933 {
1934 uint8_t status;
1935 STREAM_TO_UINT8(status, p_param);
1936 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1937 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
1938 }
1939 break;
1940 case BTIF_DM_CB_LE_TEST_END:
1941 {
1942 uint8_t status;
1943 uint16_t count = 0;
1944 STREAM_TO_UINT8(status, p_param);
1945 if (status == 0)
1946 STREAM_TO_UINT16(count, p_param);
1947 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1948 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
1949 }
1950 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001951 default:
1952 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001953 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001954 }
1955 break;
1956 }
1957}
1958
1959/*******************************************************************************
1960**
1961** Function bte_dm_evt
1962**
1963** Description Switches context from BTE to BTIF for all DM events
1964**
1965** Returns void
1966**
1967*******************************************************************************/
1968
1969void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
1970{
1971 bt_status_t status;
1972
1973 /* switch context to btif task context (copy full union size for convenience) */
1974 status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event, (void*)p_data, sizeof(tBTA_DM_SEC), NULL);
1975
1976 /* catch any failed context transfers */
1977 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
1978}
1979
1980/*******************************************************************************
1981**
1982** Function bte_search_devices_evt
1983**
1984** Description Switches context from BTE to BTIF for DM search events
1985**
1986** Returns void
1987**
1988*******************************************************************************/
1989static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1990{
1991 UINT16 param_len = 0;
1992
1993 if (p_data)
1994 param_len += sizeof(tBTA_DM_SEARCH);
1995 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
1996 switch (event)
1997 {
1998 case BTA_DM_INQ_RES_EVT:
1999 {
2000 if (p_data->inq_res.p_eir)
2001 param_len += HCI_EXT_INQ_RESPONSE_LEN;
2002 }
2003 break;
2004
2005 case BTA_DM_DISC_RES_EVT:
2006 {
2007 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
2008 param_len += p_data->disc_res.raw_data_size;
2009 }
2010 break;
2011 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002012 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 -08002013
2014 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
2015 if (event == BTA_DM_INQ_RES_EVT)
2016 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
2017
2018 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
2019 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
2020}
2021
2022/*******************************************************************************
2023**
2024** Function bte_dm_search_services_evt
2025**
2026** Description Switches context from BTE to BTIF for DM search services
2027** event
2028**
2029** Returns void
2030**
2031*******************************************************************************/
2032static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2033{
2034 UINT16 param_len = 0;
2035 if (p_data)
2036 param_len += sizeof(tBTA_DM_SEARCH);
2037 switch (event)
2038 {
2039 case BTA_DM_DISC_RES_EVT:
2040 {
2041 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
2042 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
2043 }
2044 } break;
2045 }
2046 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
2047 * if raw_data is needed. */
2048 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
2049 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
2050}
2051
2052/*******************************************************************************
2053**
2054** Function bte_dm_remote_service_record_evt
2055**
2056** Description Switches context from BTE to BTIF for DM search service
2057** record event
2058**
2059** Returns void
2060**
2061*******************************************************************************/
2062static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2063{
2064 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
2065 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
2066}
2067
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002068#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07002069/*******************************************************************************
2070**
2071** Function bta_energy_info_cb
2072**
2073** Description Switches context from BTE to BTIF for DM energy info event
2074**
2075** Returns void
2076**
2077*******************************************************************************/
2078static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time,
2079 tBTA_DM_BLE_IDLE_TIME_MS idle_time,
2080 tBTA_DM_BLE_ENERGY_USED energy_used,
2081 tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status)
2082{
2083 BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld",
2084 status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
2085
2086 btif_activity_energy_info_cb_t btif_cb;
2087 btif_cb.status = status;
2088 btif_cb.ctrl_state = ctrl_state;
2089 btif_cb.tx_time = (uint64_t) tx_time;
2090 btif_cb.rx_time = (uint64_t) rx_time;
2091 btif_cb.idle_time =(uint64_t) idle_time;
2092 btif_cb.energy_used =(uint64_t) energy_used;
2093 btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
2094 (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL);
2095}
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002096#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07002097
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002098/*******************************************************************************
2099**
2100** Function bte_scan_filt_param_cfg_evt
2101**
2102** Description Scan filter param config event
2103**
2104** Returns void
2105**
2106*******************************************************************************/
2107static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
2108 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
2109 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
2110{
2111 /* This event occurs on calling BTA_DmBleCfgFilterCondition internally,
2112 ** and that is why there is no HAL callback
2113 */
2114 if(BTA_SUCCESS != status)
2115 {
2116 BTIF_TRACE_ERROR("%s, %d", __FUNCTION__, status);
2117 }
2118 else
2119 {
2120 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2121 }
2122}
2123
The Android Open Source Project5738f832012-12-12 16:00:35 -08002124/*****************************************************************************
2125**
2126** btif api functions (no context switch)
2127**
2128*****************************************************************************/
2129
2130/*******************************************************************************
2131**
2132** Function btif_dm_start_discovery
2133**
2134** Description Start device discovery/inquiry
2135**
2136** Returns bt_status_t
2137**
2138*******************************************************************************/
2139bt_status_t btif_dm_start_discovery(void)
2140{
2141 tBTA_DM_INQ inq_params;
2142 tBTA_SERVICE_MASK services = 0;
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002143 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002144
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002145 BTIF_TRACE_EVENT("%s", __FUNCTION__);
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002146
2147#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2148 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
2149 /* Cleanup anything remaining on index 0 */
2150 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
2151 bte_scan_filt_param_cfg_evt, 0);
2152
2153 /* Add an allow-all filter on index 0*/
2154 adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE;
2155 adv_filt_param.feat_seln = ALLOW_ALL_FILTER;
2156 adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;
2157 adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;
2158 adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE;
2159 adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE;
2160 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL,
2161 bte_scan_filt_param_cfg_evt, 0);
2162
The Android Open Source Project5738f832012-12-12 16:00:35 -08002163 /* TODO: Do we need to handle multiple inquiries at the same time? */
2164
2165 /* Set inquiry params and call API */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002166 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
Matthew Xie7f3e4292013-09-30 12:44:10 -07002167#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
2168 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
2169 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
2170 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
2171 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
2172#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002173#else
2174 inq_params.mode = BTA_DM_GENERAL_INQUIRY;
2175#endif
2176 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
2177
2178 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
2179 inq_params.report_dup = TRUE;
2180
2181 inq_params.filter_type = BTA_DM_INQ_CLR;
2182 /* TODO: Filter device by BDA needs to be implemented here */
2183
2184 /* Will be enabled to TRUE once inquiry busy level has been received */
2185 btif_dm_inquiry_in_progress = FALSE;
2186 /* find nearby devices */
2187 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
2188
2189 return BT_STATUS_SUCCESS;
2190}
2191
2192/*******************************************************************************
2193**
2194** Function btif_dm_cancel_discovery
2195**
2196** Description Cancels search
2197**
2198** Returns bt_status_t
2199**
2200*******************************************************************************/
2201bt_status_t btif_dm_cancel_discovery(void)
2202{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002203 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002204 BTA_DmSearchCancel();
2205 return BT_STATUS_SUCCESS;
2206}
2207
2208/*******************************************************************************
2209**
2210** Function btif_dm_create_bond
2211**
2212** Description Initiate bonding with the specified device
2213**
2214** Returns bt_status_t
2215**
2216*******************************************************************************/
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002217bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr, int transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002218{
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002219 btif_dm_create_bond_cb_t create_bond_cb;
2220 create_bond_cb.transport = transport;
2221 bdcpy(create_bond_cb.bdaddr.address, bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002222
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002223 bdstr_t bdstr;
2224 BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __FUNCTION__,
2225 bd2str((bt_bdaddr_t *) bd_addr, &bdstr), transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002226 if (pairing_cb.state != BT_BOND_STATE_NONE)
2227 return BT_STATUS_BUSY;
2228
2229 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002230 (char *)&create_bond_cb, sizeof(btif_dm_create_bond_cb_t), NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002231
2232 return BT_STATUS_SUCCESS;
2233}
2234
2235/*******************************************************************************
2236**
2237** Function btif_dm_cancel_bond
2238**
2239** Description Initiate bonding with the specified device
2240**
2241** Returns bt_status_t
2242**
2243*******************************************************************************/
2244
2245bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
2246{
2247 bdstr_t bdstr;
2248
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002249 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 -08002250
2251 /* TODO:
2252 ** 1. Restore scan modes
2253 ** 2. special handling for HID devices
2254 */
2255 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2256 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002257
2258#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2259
2260 if (pairing_cb.is_ssp)
2261 {
2262 if (pairing_cb.is_le_only)
2263 {
2264 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2265 }
2266 else
Hemant Guptae1468692013-11-14 16:21:29 +05302267 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002268 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
Hemant Guptae1468692013-11-14 16:21:29 +05302269 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2270 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2271 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002272 }
2273 else
2274 {
2275 if (pairing_cb.is_le_only)
2276 {
2277 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2278 }
2279 else
2280 {
2281 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2282 }
2283 /* Cancel bonding, in case it is in ACL connection setup state */
2284 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2285 }
2286
2287#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002288 if (pairing_cb.is_ssp)
2289 {
2290 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2291 }
2292 else
2293 {
2294 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2295 }
2296 /* Cancel bonding, in case it is in ACL connection setup state */
2297 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2298 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002299#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002300 }
2301
2302 return BT_STATUS_SUCCESS;
2303}
2304
2305/*******************************************************************************
2306**
Kim Schulza9eb25c2013-09-30 10:55:52 +02002307** Function btif_dm_hh_open_failed
2308**
2309** Description informs the upper layers if the HH have failed during bonding
2310**
2311** Returns none
2312**
2313*******************************************************************************/
2314
2315void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
2316{
2317 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2318 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2319 {
2320 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2321 }
2322}
2323
2324/*******************************************************************************
2325**
The Android Open Source Project5738f832012-12-12 16:00:35 -08002326** Function btif_dm_remove_bond
2327**
2328** Description Removes bonding with the specified device
2329**
2330** Returns bt_status_t
2331**
2332*******************************************************************************/
2333
2334bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
2335{
2336 bdstr_t bdstr;
2337
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002338 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 -08002339 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2340 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2341
2342 return BT_STATUS_SUCCESS;
2343}
2344
2345/*******************************************************************************
2346**
2347** Function btif_dm_pin_reply
2348**
2349** Description BT legacy pairing - PIN code reply
2350**
2351** Returns bt_status_t
2352**
2353*******************************************************************************/
2354
2355bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2356 uint8_t pin_len, bt_pin_code_t *pin_code)
2357{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002358 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Hemant Gupta831423e2014-01-08 12:42:13 +05302359 if (pin_code == NULL)
2360 return BT_STATUS_FAIL;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002361#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002362
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002363 if (pairing_cb.is_le_only)
2364 {
2365 int i;
2366 UINT32 passkey = 0;
2367 int multi[] = {100000, 10000, 1000, 100, 10,1};
2368 BD_ADDR remote_bd_addr;
2369 bdcpy(remote_bd_addr, bd_addr->address);
2370 for (i = 0; i < 6; i++)
2371 {
2372 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2373 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002374 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002375 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2376
2377 }
2378 else
2379 {
2380 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2381 if (accept)
2382 pairing_cb.pin_code_len = pin_len;
2383 }
2384#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002385 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2386
2387 if (accept)
2388 pairing_cb.pin_code_len = pin_len;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002389#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002390 return BT_STATUS_SUCCESS;
2391}
2392
2393/*******************************************************************************
2394**
2395** Function btif_dm_ssp_reply
2396**
2397** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2398**
2399** Returns bt_status_t
2400**
2401*******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002402bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2403 bt_ssp_variant_t variant, uint8_t accept,
2404 uint32_t passkey)
2405{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08002406 UNUSED(passkey);
2407
The Android Open Source Project5738f832012-12-12 16:00:35 -08002408 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2409 {
2410 /* This is not implemented in the stack.
2411 * For devices with display, this is not needed
2412 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002413 BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002414 return BT_STATUS_FAIL;
2415 }
2416 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002417 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002418#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2419 if (pairing_cb.is_le_only)
2420 {
2421 if (accept)
2422 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2423 else
2424 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2425 }
2426 else
2427 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002428
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002429#else
2430 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2431#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002432 return BT_STATUS_SUCCESS;
2433}
2434
2435/*******************************************************************************
2436**
2437** Function btif_dm_get_adapter_property
2438**
2439** Description Queries the BTA for the adapter property
2440**
2441** Returns bt_status_t
2442**
2443*******************************************************************************/
2444bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2445{
2446 bt_status_t status;
2447
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002448 BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002449 switch (prop->type)
2450 {
2451 case BT_PROPERTY_BDNAME:
2452 {
2453 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
Matthew Xie1e5109b2012-11-09 18:26:26 -08002454 strcpy((char *)bd_name->name, btif_get_default_local_name());
The Android Open Source Project5738f832012-12-12 16:00:35 -08002455 prop->len = strlen((char *)bd_name->name);
2456 }
2457 break;
2458
2459 case BT_PROPERTY_ADAPTER_SCAN_MODE:
2460 {
2461 /* if the storage does not have it. Most likely app never set it. Default is NONE */
2462 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2463 *mode = BT_SCAN_MODE_NONE;
2464 prop->len = sizeof(bt_scan_mode_t);
2465 }
2466 break;
2467
2468 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2469 {
2470 uint32_t *tmt = (uint32_t*)prop->val;
2471 *tmt = 120; /* default to 120s, if not found in NV */
2472 prop->len = sizeof(uint32_t);
2473 }
2474 break;
2475
2476 default:
2477 prop->len = 0;
2478 return BT_STATUS_FAIL;
2479 }
2480 return BT_STATUS_SUCCESS;
2481}
2482
2483/*******************************************************************************
2484**
2485** Function btif_dm_get_remote_services
2486**
2487** Description Start SDP to get remote services
2488**
2489** Returns bt_status_t
2490**
2491*******************************************************************************/
2492bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2493{
2494 bdstr_t bdstr;
2495
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002496 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002497
2498 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2499 bte_dm_search_services_evt, TRUE);
2500
2501 return BT_STATUS_SUCCESS;
2502}
2503
2504/*******************************************************************************
2505**
2506** Function btif_dm_get_remote_service_record
2507**
2508** Description Start SDP to get remote service record
2509**
2510**
2511** Returns bt_status_t
2512*******************************************************************************/
2513bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2514 bt_uuid_t *uuid)
2515{
2516 tSDP_UUID sdp_uuid;
2517 bdstr_t bdstr;
2518
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002519 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002520
2521 sdp_uuid.len = MAX_UUID_SIZE;
2522 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2523
2524 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2525 bte_dm_remote_service_record_evt, TRUE);
2526
2527 return BT_STATUS_SUCCESS;
2528}
2529
2530void btif_dm_execute_service_request(UINT16 event, char *p_param)
2531{
2532 BOOLEAN b_enable = FALSE;
2533 bt_status_t status;
2534 if (event == BTIF_DM_ENABLE_SERVICE)
2535 {
2536 b_enable = TRUE;
2537 }
2538 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2539 if (status == BT_STATUS_SUCCESS)
2540 {
2541 bt_property_t property;
2542 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2543
2544 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2545 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2546 sizeof(local_uuids), local_uuids);
2547 btif_storage_get_adapter_property(&property);
2548 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2549 BT_STATUS_SUCCESS, 1, &property);
2550 }
2551 return;
2552}
2553
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002554void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2555 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2556{
2557 UINT8 yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2558 /* if local initiated:
2559 ** 1. set DD + MITM
2560 ** if remote initiated:
2561 ** 1. Copy over the auth_req from peer's io_rsp
2562 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2563 ** as a fallback set MITM+GB if peer had MITM set
2564 */
2565 UNUSED (bd_addr);
2566 UNUSED (p_io_cap);
2567 UNUSED (p_oob_data);
2568
2569
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002570 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002571 if(pairing_cb.is_local_initiated)
2572 {
2573 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2574 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2575 }
2576 else if (!is_orig)
2577 {
2578 /* peer initiated paring. They probably know what they want.
2579 ** Copy the mitm from peer device.
2580 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002581 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002582 __FUNCTION__, pairing_cb.auth_req);
2583 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2584
2585 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2586 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2587 *p_auth_req |= BTA_AUTH_SP_YES;
2588 }
2589 else if (yes_no_bit)
2590 {
2591 /* set the general bonding bit for stored device */
2592 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2593 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002594 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002595}
2596
2597void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2598 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2599{
2600 UNUSED (bd_addr);
2601 UNUSED (oob_data);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002602
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002603 if(auth_req & BTA_AUTH_BONDS)
2604 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002605 BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002606 pairing_cb.auth_req = auth_req;
2607 pairing_cb.io_cap = io_cap;
2608 }
2609}
2610
The Android Open Source Project5738f832012-12-12 16:00:35 -08002611#if (BTM_OOB_INCLUDED == TRUE)
2612void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data)
2613{
2614 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2615 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2616 {
2617 *p_oob_data = FALSE;
2618 }
2619 else
2620 {
2621 *p_oob_data = TRUE;
2622 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002623 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 -08002624}
2625#endif /* BTM_OOB_INCLUDED */
2626
2627#ifdef BTIF_DM_OOB_TEST
2628void btif_dm_load_local_oob(void)
2629{
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002630 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002631 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002632 BTIF_TRACE_DEBUG("btif_dm_load_local_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002633 if (prop_oob[0] != '3')
2634 {
2635#if (BTM_OOB_INCLUDED == TRUE)
2636 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2637 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2638 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002639 BTIF_TRACE_DEBUG("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002640 BTA_DmLocalOob();
2641 }
2642#else
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002643 BTIF_TRACE_ERROR("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002644#endif
2645 }
2646}
2647
2648void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2649{
2650 FILE *fp;
2651 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2652 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2653 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002654 char prop_oob[PROPERTY_VALUE_MAX];
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002655 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: valid=%d", valid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002656 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2657 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2658 valid)
2659 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002660 BTIF_TRACE_DEBUG("save local OOB data in memory");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002661 memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2662 memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2663 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002664 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002665 if (prop_oob[0] == '1')
2666 path = path_a;
2667 else if (prop_oob[0] == '2')
2668 path = path_b;
2669 if (path)
2670 {
2671 fp = fopen(path, "wb+");
2672 if (fp == NULL)
2673 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002674 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 -08002675 }
2676 else
2677 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002678 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 -08002679 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2680 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2681 fclose(fp);
2682 }
2683 }
2684 }
2685}
2686BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2687{
2688 char t[128];
2689 FILE *fp;
2690 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2691 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2692 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002693 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002694 BOOLEAN result = FALSE;
2695 bt_bdaddr_t bt_bd_addr;
2696 bdcpy(oob_cb.oob_bdaddr, bd_addr);
2697 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002698 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002699 if (prop_oob[0] == '1')
2700 path = path_b;
2701 else if (prop_oob[0] == '2')
2702 path = path_a;
2703 if (path)
2704 {
2705 fp = fopen(path, "rb");
2706 if (fp == NULL)
2707 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002708 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 -08002709 return FALSE;
2710 }
2711 else
2712 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002713 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob: read OOB data from %s",path);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002714 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2715 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2716 fclose(fp);
2717 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002718 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: TRUE");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002719 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2720 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2721 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002722 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002723 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2724 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
2725 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 -07002726 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: c = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002727 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2728 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
2729 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 -07002730 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: r = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002731 bdcpy(bt_bd_addr.address, bd_addr);
2732 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2733 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2734 result = TRUE;
2735 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002736 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob result=%d",result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002737 return result;
2738}
2739#endif /* BTIF_DM_OOB_TEST */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002740#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2741
2742static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2743{
2744 bt_bdaddr_t bd_addr;
2745 bt_bdname_t bd_name;
2746 UINT32 cod;
2747
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002748 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002749
2750 /* Remote name update */
2751 btif_update_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2752 NULL, BT_DEVICE_TYPE_BLE);
2753 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2754 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2755
2756 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2757 pairing_cb.is_ssp = FALSE;
2758 cod = COD_UNCLASSIFIED;
2759
2760 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2761 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2762 p_ssp_key_notif->passkey);
2763}
2764
2765/*******************************************************************************
2766**
2767** Function btif_dm_ble_auth_cmpl_evt
2768**
2769** Description Executes authentication complete event in btif context
2770**
2771** Returns void
2772**
2773*******************************************************************************/
2774static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2775{
2776 /* Save link key, if not temporary */
2777 bt_bdaddr_t bd_addr;
2778 bt_status_t status = BT_STATUS_FAIL;
2779 bt_bond_state_t state = BT_BOND_STATE_NONE;
2780
2781 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2782 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2783 {
2784 /* store keys */
2785 }
2786 if (p_auth_cmpl->success)
2787 {
2788 status = BT_STATUS_SUCCESS;
2789 state = BT_BOND_STATE_BONDED;
2790
2791 btif_dm_save_ble_bonding_keys();
2792 BTA_GATTC_Refresh(bd_addr.address);
2793 btif_dm_get_remote_services(&bd_addr);
2794 }
2795 else
2796 {
2797 /*Map the HCI fail reason to bt status */
2798 switch (p_auth_cmpl->fail_reason)
2799 {
Priti Aghera156c52b2014-07-09 14:58:19 -07002800 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
2801 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
2802 btif_dm_remove_ble_bonding_keys();
2803 status = BT_STATUS_AUTH_FAILURE;
2804 break;
2805 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
2806 status = BT_STATUS_AUTH_REJECTED;
2807 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002808 default:
Andre Eisenbachca22ac42013-02-13 17:02:11 +09002809 btif_dm_remove_ble_bonding_keys();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002810 status = BT_STATUS_FAIL;
2811 break;
2812 }
2813 }
2814 bond_state_changed(status, &bd_addr, state);
2815}
2816
2817
2818
2819void btif_dm_load_ble_local_keys(void)
2820{
2821 bt_status_t bt_status;
2822
2823 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2824
2825 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2826 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2827 {
2828 ble_local_key_cb.is_er_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002829 BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002830 }
2831
2832 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
2833 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
2834 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
2835 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
2836 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
2837 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
2838 {
2839 ble_local_key_cb.is_id_keys_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002840 BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002841 }
2842
2843}
2844void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
2845 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
2846{
2847 if (ble_local_key_cb.is_er_rcvd )
2848 {
2849 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2850 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2851 }
2852
2853 if (ble_local_key_cb.is_id_keys_rcvd)
2854 {
2855 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
2856 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
2857 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
2858 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
2859 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002860 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002861}
2862
2863void btif_dm_save_ble_bonding_keys(void)
2864{
2865
2866 bt_bdaddr_t bd_addr;
2867
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002868 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002869
2870 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2871
2872 if (pairing_cb.ble.is_penc_key_rcvd)
2873 {
2874 btif_storage_add_ble_bonding_key(&bd_addr,
2875 (char *) &pairing_cb.ble.penc_key,
2876 BTIF_DM_LE_KEY_PENC,
2877 sizeof(btif_dm_ble_penc_keys_t));
2878 }
2879
2880 if (pairing_cb.ble.is_pid_key_rcvd)
2881 {
2882 btif_storage_add_ble_bonding_key(&bd_addr,
2883 (char *) &pairing_cb.ble.pid_key[0],
2884 BTIF_DM_LE_KEY_PID,
2885 BT_OCTET16_LEN);
2886 }
2887
2888
2889 if (pairing_cb.ble.is_pcsrk_key_rcvd)
2890 {
2891 btif_storage_add_ble_bonding_key(&bd_addr,
2892 (char *) &pairing_cb.ble.pcsrk_key,
2893 BTIF_DM_LE_KEY_PCSRK,
2894 sizeof(btif_dm_ble_pcsrk_keys_t));
2895 }
2896
2897
2898 if (pairing_cb.ble.is_lenc_key_rcvd)
2899 {
2900 btif_storage_add_ble_bonding_key(&bd_addr,
2901 (char *) &pairing_cb.ble.lenc_key,
2902 BTIF_DM_LE_KEY_LENC,
2903 sizeof(btif_dm_ble_lenc_keys_t));
2904 }
2905
2906 if (pairing_cb.ble.is_lcsrk_key_rcvd)
2907 {
2908 btif_storage_add_ble_bonding_key(&bd_addr,
2909 (char *) &pairing_cb.ble.lcsrk_key,
2910 BTIF_DM_LE_KEY_LCSRK,
2911 sizeof(btif_dm_ble_lcsrk_keys_t));
2912 }
2913
2914}
2915
2916
2917void btif_dm_remove_ble_bonding_keys(void)
2918{
2919 bt_bdaddr_t bd_addr;
2920
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002921 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002922
2923 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2924 btif_storage_remove_ble_bonding_keys(&bd_addr);
2925}
2926
2927
2928/*******************************************************************************
2929**
2930** Function btif_dm_ble_sec_req_evt
2931**
2932** Description Eprocess security request event in btif context
2933**
2934** Returns void
2935**
2936*******************************************************************************/
2937void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
2938{
2939 bt_bdaddr_t bd_addr;
2940 bt_bdname_t bd_name;
2941 UINT32 cod;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002942 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002943
2944 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2945 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002946 BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002947 return;
2948 }
2949
2950 /* Remote name update */
2951 btif_update_remote_properties(p_ble_req->bd_addr,p_ble_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
2952
2953 bdcpy(bd_addr.address, p_ble_req->bd_addr);
2954 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
2955
2956 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2957
2958 pairing_cb.is_temp = FALSE;
2959 pairing_cb.is_le_only = TRUE;
2960 pairing_cb.is_ssp = TRUE;
2961
2962 cod = COD_UNCLASSIFIED;
2963
2964 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
2965 BT_SSP_VARIANT_CONSENT, 0);
2966}
2967
2968
2969
2970/*******************************************************************************
2971**
2972** Function btif_dm_ble_passkey_req_evt
2973**
2974** Description Executes pin request event in btif context
2975**
2976** Returns void
2977**
2978*******************************************************************************/
2979static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
2980{
2981 bt_bdaddr_t bd_addr;
2982 bt_bdname_t bd_name;
2983 UINT32 cod;
2984
2985 /* Remote name update */
2986 btif_update_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
2987
2988 bdcpy(bd_addr.address, p_pin_req->bd_addr);
2989 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
2990
2991 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2992 pairing_cb.is_le_only = TRUE;
2993
2994 cod = COD_UNCLASSIFIED;
2995
2996 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
2997 &bd_addr, &bd_name, cod);
2998}
2999
3000
3001void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
3002 tBT_DEVICE_TYPE dev_type)
3003{
3004 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
3005}
3006
3007static void btif_dm_ble_tx_test_cback(void *p)
3008{
3009 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
3010 (char *)p, 1, NULL);
3011}
3012
3013static void btif_dm_ble_rx_test_cback(void *p)
3014{
3015 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
3016 (char *)p, 1, NULL);
3017}
3018
3019static void btif_dm_ble_test_end_cback(void *p)
3020{
3021 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
3022 (char *)p, 3, NULL);
3023}
3024/*******************************************************************************
3025**
3026** Function btif_le_test_mode
3027**
3028** Description Sends a HCI BLE Test command to the Controller
3029**
3030** Returns BT_STATUS_SUCCESS on success
3031**
3032*******************************************************************************/
3033bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
3034{
3035 switch (opcode) {
3036 case HCI_BLE_TRANSMITTER_TEST:
3037 if (len != 3) return BT_STATUS_PARM_INVALID;
3038 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
3039 break;
3040 case HCI_BLE_RECEIVER_TEST:
3041 if (len != 1) return BT_STATUS_PARM_INVALID;
3042 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
3043 break;
3044 case HCI_BLE_TEST_END:
3045 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
3046 break;
3047 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003048 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003049 return BT_STATUS_UNSUPPORTED;
3050 }
3051 return BT_STATUS_SUCCESS;
3052}
3053
3054#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08003055
3056void btif_dm_on_disable()
3057{
3058 /* cancel any pending pairing requests */
3059 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3060 {
3061 bt_bdaddr_t bd_addr;
3062
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003063 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003064 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3065 btif_dm_cancel_bond(&bd_addr);
3066 }
3067}
Matthew Xie1e5109b2012-11-09 18:26:26 -08003068
Satya Callojie5ba8842014-07-03 17:18:02 -07003069/*******************************************************************************
3070**
3071** Function btif_dm_read_energy_info
3072**
3073** Description Reads the energy info from controller
3074**
3075** Returns void
3076**
3077*******************************************************************************/
3078void btif_dm_read_energy_info()
3079{
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003080#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07003081 BTA_DmBleGetEnergyInfo(bta_energy_info_cb);
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003082#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07003083}
3084
Matthew Xie1e5109b2012-11-09 18:26:26 -08003085static char* btif_get_default_local_name() {
3086 if (btif_default_local_name[0] == '\0')
3087 {
3088 int max_len = sizeof(btif_default_local_name) - 1;
3089 if (BTM_DEF_LOCAL_NAME[0] != '\0')
3090 {
3091 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
3092 }
3093 else
3094 {
3095 char prop_model[PROPERTY_VALUE_MAX];
3096 property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
3097 strncpy(btif_default_local_name, prop_model, max_len);
3098 }
3099 btif_default_local_name[max_len] = '\0';
3100 }
3101 return btif_default_local_name;
3102}