blob: 3e047d95ae656f8a2ad5e3fb3a7fe25d85ca2c1b [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
Andre Eisenbach89363762015-01-26 13:49:36 -0800112#define BOND_TYPE_UNKNOWN 0
113#define BOND_TYPE_PERSISTENT 1
114#define BOND_TYPE_TEMPORARY 2
115
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800116#define ENCRYPTED_BREDR 2
117#define ENCRYPTED_LE 4
118
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800119typedef struct
120{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800121 bt_bond_state_t state;
122 BD_ADDR bd_addr;
Andre Eisenbach89363762015-01-26 13:49:36 -0800123 UINT8 bond_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800124 UINT8 pin_code_len;
125 UINT8 is_ssp;
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -0700126 UINT8 auth_req;
127 UINT8 io_cap;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800128 UINT8 autopair_attempts;
Andre Eisenbach31a64002014-10-14 14:29:19 -0700129 UINT8 timeout_retries;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800130 UINT8 is_local_initiated;
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700131 UINT8 sdp_attempts;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800132#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
133 BOOLEAN is_le_only;
134 btif_dm_ble_cb_t ble;
135#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800136} btif_dm_pairing_cb_t;
137
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800138
139typedef struct
140{
141 UINT8 ir[BT_OCTET16_LEN];
142 UINT8 irk[BT_OCTET16_LEN];
143 UINT8 dhk[BT_OCTET16_LEN];
144}btif_dm_local_key_id_t;
145
146typedef struct
147{
148 BOOLEAN is_er_rcvd;
149 UINT8 er[BT_OCTET16_LEN];
150 BOOLEAN is_id_keys_rcvd;
151 btif_dm_local_key_id_t id_keys; /* ID kyes */
152
153}btif_dm_local_key_cb_t;
154
155typedef struct
156{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800157 BD_ADDR bd_addr;
158 BD_NAME bd_name;
159} btif_dm_remote_name_t;
160
161typedef struct
162{
163 BT_OCTET16 sp_c;
164 BT_OCTET16 sp_r;
165 BD_ADDR oob_bdaddr; /* peer bdaddr*/
166} btif_dm_oob_cb_t;
Satya Callojie5ba8842014-07-03 17:18:02 -0700167
168typedef struct
169{
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700170 bt_bdaddr_t bdaddr;
171 UINT8 transport; /* 0=Unknown, 1=BR/EDR, 2=LE */
172} btif_dm_create_bond_cb_t;
173
174typedef struct
175{
Satya Callojie5ba8842014-07-03 17:18:02 -0700176 uint8_t status;
177 uint8_t ctrl_state;
178 uint64_t tx_time;
179 uint64_t rx_time;
180 uint64_t idle_time;
181 uint64_t energy_used;
182} btif_activity_energy_info_cb_t;
183
Priti Agherac0edf9f2014-06-26 11:23:51 -0700184typedef struct
185{
186 unsigned int manufact_id;
187}skip_sdp_entry_t;
188
The Android Open Source Project5738f832012-12-12 16:00:35 -0800189#define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id))
190
Priti Agherac0edf9f2014-06-26 11:23:51 -0700191#define MAX_SDP_BL_ENTRIES 3
192#define UUID_HUMAN_INTERFACE_DEVICE "00001124-0000-1000-8000-00805f9b34fb"
193
194static skip_sdp_entry_t sdp_blacklist[] = {{76}}; //Apple Mouse and Keyboard
195
196
The Android Open Source Project5738f832012-12-12 16:00:35 -0800197/* This flag will be true if HCI_Inquiry is in progress */
198static BOOLEAN btif_dm_inquiry_in_progress = FALSE;
199
Priti Agherac0edf9f2014-06-26 11:23:51 -0700200
201
Matthew Xie1e5109b2012-11-09 18:26:26 -0800202/************************************************************************************
203** Static variables
204************************************************************************************/
205static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'};
206
The Android Open Source Project5738f832012-12-12 16:00:35 -0800207/******************************************************************************
208** Static functions
209******************************************************************************/
210static btif_dm_pairing_cb_t pairing_cb;
211static btif_dm_oob_cb_t oob_cb;
212static void btif_dm_generic_evt(UINT16 event, char* p_param);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700213static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800214static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name);
215static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
216 DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800217#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
218static btif_dm_local_key_cb_t ble_local_key_cb;
219static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif);
220static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl);
221static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req);
222#endif
Satya Calloji6e2d9db2014-07-08 16:18:58 -0700223
224static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
225 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
226 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status);
227
Matthew Xie1e5109b2012-11-09 18:26:26 -0800228static char* btif_get_default_local_name();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800229/******************************************************************************
230** Externs
231******************************************************************************/
232extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
233extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
234extern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
235extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
Hemant Gupta10256872013-08-19 18:33:01 +0530236extern bt_status_t btif_hf_client_execute_service(BOOLEAN b_enable);
Hemant Gupta2dc99992014-04-18 12:54:08 +0530237extern bt_status_t btif_mce_execute_service(BOOLEAN b_enable);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800238extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700239extern 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 -0800240
241
242/******************************************************************************
243** Functions
244******************************************************************************/
245
246bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
247 BOOLEAN b_enable)
248{
249 /* Check the service_ID and invoke the profile's BT state changed API */
250 switch (service_id)
251 {
252 case BTA_HFP_SERVICE_ID:
253 case BTA_HSP_SERVICE_ID:
254 {
255 btif_hf_execute_service(b_enable);
256 }break;
257 case BTA_A2DP_SERVICE_ID:
258 {
259 btif_av_execute_service(b_enable);
260 }break;
261 case BTA_HID_SERVICE_ID:
262 {
263 btif_hh_execute_service(b_enable);
264 }break;
Hemant Gupta10256872013-08-19 18:33:01 +0530265 case BTA_HFP_HS_SERVICE_ID:
266 {
267 btif_hf_client_execute_service(b_enable);
268 }break;
Hemant Gupta2dc99992014-04-18 12:54:08 +0530269 case BTA_MAP_SERVICE_ID:
270 {
271 btif_mce_execute_service(b_enable);
272 }break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800273 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700274 BTIF_TRACE_ERROR("%s: Unknown service being enabled", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800275 return BT_STATUS_FAIL;
276 }
277 return BT_STATUS_SUCCESS;
278}
279
280/*******************************************************************************
281**
282** Function check_eir_remote_name
283**
284** Description Check if remote name is in the EIR data
285**
286** Returns TRUE if remote name found
287** Populate p_remote_name, if provided and remote name found
288**
289*******************************************************************************/
290static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
291 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
292{
293 UINT8 *p_eir_remote_name = NULL;
294 UINT8 remote_name_len = 0;
295
296 /* Check EIR for remote name and services */
297 if (p_search_data->inq_res.p_eir)
298 {
299 p_eir_remote_name = BTA_CheckEirData(p_search_data->inq_res.p_eir,
300 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
301 if (!p_eir_remote_name)
302 {
303 p_eir_remote_name = BTA_CheckEirData(p_search_data->inq_res.p_eir,
304 BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
305 }
306
307 if (p_eir_remote_name)
308 {
309 if (remote_name_len > BD_NAME_LEN)
310 remote_name_len = BD_NAME_LEN;
311
312 if (p_remote_name && p_remote_name_len)
313 {
314 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
315 *(p_remote_name + remote_name_len) = 0;
316 *p_remote_name_len = remote_name_len;
317 }
318
319 return TRUE;
320 }
321 }
322
323 return FALSE;
324
325}
326
327/*******************************************************************************
328**
329** Function check_cached_remote_name
330**
331** Description Check if remote name is in the NVRAM cache
332**
333** Returns TRUE if remote name found
334** Populate p_remote_name, if provided and remote name found
335**
336*******************************************************************************/
337static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
338 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
339{
340 bt_bdname_t bdname;
341 bt_bdaddr_t remote_bdaddr;
342 bt_property_t prop_name;
343
344 /* check if we already have it in our btif_storage cache */
345 bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
346 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
347 sizeof(bt_bdname_t), &bdname);
348 if (btif_storage_get_remote_device_property(
349 &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
350 {
351 if (p_remote_name && p_remote_name_len)
352 {
353 strcpy((char *)p_remote_name, (char *)bdname.name);
354 *p_remote_name_len = strlen((char *)p_remote_name);
355 }
356 return TRUE;
357 }
358
359 return FALSE;
360}
361
362BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
363{
364 uint32_t remote_cod;
365 bt_property_t prop_name;
366
367 /* check if we already have it in our btif_storage cache */
368 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
369 sizeof(uint32_t), &remote_cod);
370 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
371 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700372 BTIF_TRACE_ERROR("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800373 if ((remote_cod & 0x7ff) == cod)
374 return TRUE;
375 }
376
377 return FALSE;
378}
379
Priti Agheraebb1d752012-11-27 18:03:22 -0800380BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
381{
382 uint32_t remote_cod;
383 bt_property_t prop_name;
384
385 /* check if we already have it in our btif_storage cache */
386 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
387 sizeof(uint32_t), &remote_cod);
388 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
389 &prop_name) == BT_STATUS_SUCCESS)
390 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700391 BTIF_TRACE_DEBUG("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
Priti Agheraebb1d752012-11-27 18:03:22 -0800392 if ((remote_cod & 0x700) == cod)
393 return TRUE;
394 }
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700395 return FALSE;
396}
Priti Agheraebb1d752012-11-27 18:03:22 -0800397
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700398BOOLEAN check_hid_le(const bt_bdaddr_t *remote_bdaddr)
399{
400 uint32_t remote_dev_type;
401 bt_property_t prop_name;
402
403 /* check if we already have it in our btif_storage cache */
404 BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE,
405 sizeof(uint32_t), &remote_dev_type);
406 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
407 &prop_name) == BT_STATUS_SUCCESS)
408 {
409 if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE)
410 {
411 bdstr_t bdstr;
412 bd2str(remote_bdaddr, &bdstr);
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700413 if(btif_config_exist(bdstr, "HidAppId"))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700414 return TRUE;
415 }
416 }
Priti Agheraebb1d752012-11-27 18:03:22 -0800417 return FALSE;
418}
419
Priti Agherac0edf9f2014-06-26 11:23:51 -0700420/*****************************************************************************
421**
422** Function check_sdp_bl
423**
424** Description Checks if a given device is blacklisted to skip sdp
425**
426** Parameters skip_sdp_entry
427**
428** Returns TRUE if the device is present in blacklist, else FALSE
429**
430*******************************************************************************/
431BOOLEAN check_sdp_bl(const bt_bdaddr_t *remote_bdaddr)
432{
433 UINT8 i = 0;
434 UINT16 manufacturer = 0;
435 UINT8 lmp_ver = 0;
436 UINT16 lmp_subver = 0;
437 tBTM_STATUS btm_status;
438 bt_property_t prop_name;
439 bt_remote_version_t info;
440 bt_status_t status;
441
442
443 if (remote_bdaddr == NULL)
444 return FALSE;
445
446/* fetch additional info about remote device used in iop query */
447 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)remote_bdaddr, &lmp_ver,
448 &manufacturer, &lmp_subver);
449
450
451
452 /* if not available yet, try fetching from config database */
453 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO,
454 sizeof(bt_remote_version_t), &info);
455
456 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
457 &prop_name) != BT_STATUS_SUCCESS)
458 {
459
460 return FALSE;
461 }
462 manufacturer = info.manufacturer;
463
464 for (int i = 0; i < MAX_SDP_BL_ENTRIES; i++)
465 {
466 if (manufacturer == sdp_blacklist[i].manufact_id)
467 return TRUE;
468 }
469 return FALSE;
470}
471
472
The Android Open Source Project5738f832012-12-12 16:00:35 -0800473static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
474{
475 /* Send bonding state only once - based on outgoing/incoming we may receive duplicates */
476 if ( (pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING) )
477 return;
478
Andre Eisenbach89363762015-01-26 13:49:36 -0800479 if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800480 {
481 state = BT_BOND_STATE_NONE;
482 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700483 BTIF_TRACE_DEBUG("%s: state=%d prev_state=%d", __FUNCTION__, state, pairing_cb.state);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800484
485 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
486
487 if (state == BT_BOND_STATE_BONDING)
488 {
489 pairing_cb.state = state;
490 bdcpy(pairing_cb.bd_addr, bd_addr->address);
491 }
492 else
493 {
494 memset(&pairing_cb, 0, sizeof(pairing_cb));
495 }
496
497}
498
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800499/* store remote version in bt config to always have access
500 to it post pairing*/
501static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
502{
503 bt_property_t property;
504 UINT8 lmp_ver = 0;
505 UINT16 lmp_subver = 0;
506 UINT16 mfct_set = 0;
507 tBTM_STATUS btm_status;
508 bt_remote_version_t info;
509 bt_status_t status;
510 bdstr_t bdstr;
511
512 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
513 &mfct_set, &lmp_subver);
514
515 ALOGD("remote version info [%s]: %x, %x, %x", bd2str(p_bd, &bdstr),
516 lmp_ver, mfct_set, lmp_subver);
517
518 if (btm_status == BTM_SUCCESS)
519 {
520 /* always update cache to ensure we have availability whenever BTM API
521 is not populated */
522 info.manufacturer = mfct_set;
523 info.sub_ver = lmp_subver;
524 info.version = lmp_ver;
525 BTIF_STORAGE_FILL_PROPERTY(&property,
526 BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
527 &info);
528 status = btif_storage_set_remote_device_property(p_bd, &property);
529 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
530 }
531}
532
The Android Open Source Project5738f832012-12-12 16:00:35 -0800533
534static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
535 DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
536{
537 int num_properties = 0;
538 bt_property_t properties[3];
539 bt_bdaddr_t bdaddr;
540 bt_status_t status;
541 UINT32 cod;
542 bt_device_type_t dev_type;
543
544 memset(properties, 0, sizeof(properties));
545 bdcpy(bdaddr.address, bd_addr);
546
547 /* remote name */
548 if (strlen((const char *) bd_name))
549 {
550 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
551 BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
552 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
553 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
554 num_properties++;
555 }
556
557 /* class of device */
558 cod = devclass2uint(dev_class);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700559 BTIF_TRACE_DEBUG("%s():cod is 0x%06x", __FUNCTION__, cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800560 if ( cod == 0) {
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530561 /* Try to retrieve cod from storage */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700562 BTIF_TRACE_DEBUG("%s():cod is 0, checking cod from storage", __FUNCTION__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530563 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
564 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
565 status = btif_storage_get_remote_device_property(&bdaddr, &properties[num_properties]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700566 BTIF_TRACE_DEBUG("%s():cod retreived from storage is 0x%06x", __FUNCTION__, cod);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530567 if ( cod == 0) {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700568 BTIF_TRACE_DEBUG("%s():cod is again 0, set as unclassified", __FUNCTION__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530569 cod = COD_UNCLASSIFIED;
570 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800571 }
572
573 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
574 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
575 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
576 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
577 num_properties++;
578
579 /* device type */
580 dev_type = device_type;
581 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
582 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
583 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
584 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
585 num_properties++;
586
587 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
588 status, &bdaddr, num_properties, properties);
589}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800590
591/*******************************************************************************
592**
593** Function btif_dm_cb_hid_remote_name
594**
595** Description Remote name callback for HID device. Called in btif context
596** Special handling for HID devices
597**
598** Returns void
599**
600*******************************************************************************/
601static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
602{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700603 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 -0800604 if (pairing_cb.state == BT_BOND_STATE_BONDING)
605 {
606 bt_bdaddr_t remote_bd;
607
608 bdcpy(remote_bd.address, pairing_cb.bd_addr);
609
610 if (p_remote_name->status == BTM_SUCCESS)
611 {
612 bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
613 }
614 else
615 bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
616 }
617}
618
The Android Open Source Project5738f832012-12-12 16:00:35 -0800619/*******************************************************************************
620**
621** Function btif_dm_cb_create_bond
622**
623** Description Create bond initiated from the BTIF thread context
624** Special handling for HID devices
625**
626** Returns void
627**
628*******************************************************************************/
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700629static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800630{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800631 BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800632 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800633
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800634#if BLE_INCLUDED == TRUE
635 int device_type;
636 int addr_type;
637 bdstr_t bdstr;
638 bd2str(bd_addr, &bdstr);
Matthew Xie64c54792014-09-16 00:55:03 -0700639 if (transport == BT_TRANSPORT_LE)
640 {
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700641 if (!btif_config_get_int((char const *)&bdstr,"DevType", &device_type))
Matthew Xie64c54792014-09-16 00:55:03 -0700642 {
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700643 btif_config_set_int(bdstr, "DevType", BT_DEVICE_TYPE_BLE);
Matthew Xie64c54792014-09-16 00:55:03 -0700644 }
645 if (btif_storage_get_remote_addr_type(bd_addr, &addr_type) != BT_STATUS_SUCCESS)
646 {
647 btif_storage_set_remote_addr_type(bd_addr, BLE_ADDR_PUBLIC);
648 }
649 }
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700650 if((btif_config_get_int((char const *)&bdstr,"DevType", &device_type) &&
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800651 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
lungtsai_lin3baff792014-08-29 13:47:47 +0800652 (device_type == BT_DEVICE_TYPE_BLE)) || (transport == BT_TRANSPORT_LE))
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800653 {
654 BTA_DmAddBleDevice(bd_addr->address, addr_type, BT_DEVICE_TYPE_BLE);
655 }
656#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800657
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800658#if BLE_INCLUDED == TRUE
659 if(is_hid && device_type != BT_DEVICE_TYPE_BLE)
660#else
661 if(is_hid)
662#endif
663 {
664 int status;
665 status = btif_hh_connect(bd_addr);
666 if(status != BT_STATUS_SUCCESS)
667 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800668 }
669 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800670 {
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700671 BTA_DmBondByTransport((UINT8 *)bd_addr->address, transport);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800672 }
673 /* Track originator of bond creation */
674 pairing_cb.is_local_initiated = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800675
676}
677
678/*******************************************************************************
679**
680** Function btif_dm_cb_remove_bond
681**
682** Description remove bond initiated from the BTIF thread context
683** Special handling for HID devices
684**
685** Returns void
686**
687*******************************************************************************/
688void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
689{
690 bdstr_t bdstr;
691 /*special handling for HID devices */
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700692 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there
693 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
694#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
695 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
696#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800697 {
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700698 BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800699 }
700}
701
702/*******************************************************************************
703**
Andre Eisenbach249f6002014-06-18 12:20:37 -0700704** Function btif_dm_get_connection_state
705**
706** Description Returns whether the remote device is currently connected
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800707** and whether encryption is active for the connection
Andre Eisenbach249f6002014-06-18 12:20:37 -0700708**
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800709** Returns 0 if not connected; 1 if connected and > 1 if connection is
710** encrypted
Andre Eisenbach249f6002014-06-18 12:20:37 -0700711**
712*******************************************************************************/
713uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr)
714{
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800715 uint8_t *bda = (uint8_t*)bd_addr->address;
716 uint16_t rc = BTA_DmGetConnectionState(bda);
717
718 if (rc != 0)
719 {
720 uint8_t flags = 0;
721
722 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_BR_EDR);
723 BTIF_TRACE_DEBUG("%s: security flags (BR/EDR)=0x%02x", __FUNCTION__, flags);
724 if (flags & BTM_SEC_FLAG_ENCRYPTED)
725 rc |= ENCRYPTED_BREDR;
726
727 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_LE);
728 BTIF_TRACE_DEBUG("%s: security flags (LE)=0x%02x", __FUNCTION__, flags);
729 if (flags & BTM_SEC_FLAG_ENCRYPTED)
730 rc |= ENCRYPTED_LE;
731 }
732
733 return rc;
Andre Eisenbach249f6002014-06-18 12:20:37 -0700734}
735
736/*******************************************************************************
737**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800738** Function search_devices_copy_cb
739**
740** Description Deep copy callback for search devices event
741**
742** Returns void
743**
744*******************************************************************************/
745static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
746{
747 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
748 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
749
750 if (!p_src)
751 return;
752
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700753 BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800754 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
755 switch (event)
756 {
757 case BTA_DM_INQ_RES_EVT:
758 {
759 if (p_src_data->inq_res.p_eir)
760 {
761 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
762 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
763 }
764 }
765 break;
766
767 case BTA_DM_DISC_RES_EVT:
768 {
769 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
770 {
771 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
772 memcpy(p_dest_data->disc_res.p_raw_data,
773 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
774 }
775 }
776 break;
777 }
778}
779
780static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
781{
782 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
783 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
784
785 if (!p_src)
786 return;
787 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
788 switch (event)
789 {
790 case BTA_DM_DISC_RES_EVT:
791 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530792 if (p_src_data->disc_res.result == BTA_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800793 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530794 if (p_src_data->disc_res.num_uuids > 0)
795 {
796 p_dest_data->disc_res.p_uuid_list =
797 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
798 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
799 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
800 GKI_freebuf(p_src_data->disc_res.p_uuid_list);
801 }
802 if (p_src_data->disc_res.p_raw_data != NULL)
803 {
804 GKI_freebuf(p_src_data->disc_res.p_raw_data);
805 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800806 }
807 } break;
808 }
809}
810/******************************************************************************
811**
812** BTIF DM callback events
813**
814*****************************************************************************/
815
816/*******************************************************************************
817**
818** Function btif_dm_pin_req_evt
819**
820** Description Executes pin request event in btif context
821**
822** Returns void
823**
824*******************************************************************************/
825static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
826{
827 bt_bdaddr_t bd_addr;
828 bt_bdname_t bd_name;
829 UINT32 cod;
830 bt_pin_code_t pin_code;
Matthew Xie86f97ed2014-11-10 10:24:46 -0800831 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800832
833 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -0800834 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
835 {
836 dev_type = BT_DEVICE_TYPE_BREDR;
837 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800838 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -0800839 p_pin_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800840
841 bdcpy(bd_addr.address, p_pin_req->bd_addr);
842 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
843
844 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
845
846 cod = devclass2uint(p_pin_req->dev_class);
847
848 if ( cod == 0) {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700849 BTIF_TRACE_DEBUG("%s():cod is 0, set as unclassified", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800850 cod = COD_UNCLASSIFIED;
851 }
852
853 /* check for auto pair possiblity only if bond was initiated by local device */
854 if (pairing_cb.is_local_initiated)
855 {
856 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
857 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
858 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
859 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
860 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
861 check_cod(&bd_addr, COD_HID_POINTING))
862 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700863 BTIF_TRACE_DEBUG("%s()cod matches for auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800864 /* Check if this device can be auto paired */
865 if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) &&
866 (pairing_cb.autopair_attempts == 0))
867 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700868 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800869 pin_code.pin[0] = 0x30;
870 pin_code.pin[1] = 0x30;
871 pin_code.pin[2] = 0x30;
872 pin_code.pin[3] = 0x30;
873
874 pairing_cb.autopair_attempts++;
875 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
876 return;
877 }
878 }
879 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
880 check_cod(&bd_addr, COD_HID_COMBO))
881 {
882 if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) &&
883 (pairing_cb.autopair_attempts == 0))
884 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700885 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800886 pin_code.pin[0] = 0x30;
887 pin_code.pin[1] = 0x30;
888 pin_code.pin[2] = 0x30;
889 pin_code.pin[3] = 0x30;
890
891 pairing_cb.autopair_attempts++;
892 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
893 return;
894 }
895 }
896 }
897 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
898 &bd_addr, &bd_name, cod);
899}
900
901/*******************************************************************************
902**
903** Function btif_dm_ssp_cfm_req_evt
904**
905** Description Executes SSP confirm request event in btif context
906**
907** Returns void
908**
909*******************************************************************************/
910static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
911{
912 bt_bdaddr_t bd_addr;
913 bt_bdname_t bd_name;
914 UINT32 cod;
915 BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
Matthew Xie86f97ed2014-11-10 10:24:46 -0800916 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800917
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700918 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800919
920 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -0800921 if (!btif_get_device_type(p_ssp_cfm_req->bd_addr, &dev_type))
922 {
923 dev_type = BT_DEVICE_TYPE_BREDR;
924 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800925 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -0800926 p_ssp_cfm_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800927
928 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
929 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
930
931 /* Set the pairing_cb based on the local & remote authentication requirements */
932 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
933
934 /* if just_works and bonding bit is not set treat this as temporary */
935 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 -0800936 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
937 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
Andre Eisenbach89363762015-01-26 13:49:36 -0800938 pairing_cb.bond_type = BOND_TYPE_TEMPORARY;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800939 else
Andre Eisenbach89363762015-01-26 13:49:36 -0800940 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800941
942 pairing_cb.is_ssp = TRUE;
943
944 /* If JustWorks auto-accept */
945 if (p_ssp_cfm_req->just_works)
946 {
947 /* Pairing consent for JustWorks needed if:
948 * 1. Incoming pairing is detected AND
949 * 2. local IO capabilities are DisplayYesNo AND
950 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
951 */
952 if ((is_incoming) && ((p_ssp_cfm_req->loc_io_caps == 0x01) &&
953 (p_ssp_cfm_req->rmt_io_caps == 0x00 || p_ssp_cfm_req->rmt_io_caps == 0x03)))
954 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700955 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 -0800956 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
957 }
958 else
959 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700960 BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800961 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
962 return;
963 }
964 }
965
966 cod = devclass2uint(p_ssp_cfm_req->dev_class);
967
968 if ( cod == 0) {
969 ALOGD("cod is 0, set as unclassified");
970 cod = COD_UNCLASSIFIED;
971 }
972
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700973 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800974 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
975 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
976 p_ssp_cfm_req->num_val);
977}
978
979static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
980{
981 bt_bdaddr_t bd_addr;
982 bt_bdname_t bd_name;
983 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -0800984 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800985
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700986 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800987
988 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -0800989 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
990 {
991 dev_type = BT_DEVICE_TYPE_BREDR;
992 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800993 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -0800994 p_ssp_key_notif->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800995
996 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
997 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
998
999 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1000 pairing_cb.is_ssp = TRUE;
1001 cod = devclass2uint(p_ssp_key_notif->dev_class);
1002
1003 if ( cod == 0) {
1004 ALOGD("cod is 0, set as unclassified");
1005 cod = COD_UNCLASSIFIED;
1006 }
1007
1008 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
1009 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
1010 p_ssp_key_notif->passkey);
1011}
1012/*******************************************************************************
1013**
1014** Function btif_dm_auth_cmpl_evt
1015**
1016** Description Executes authentication complete event in btif context
1017**
1018** Returns void
1019**
1020*******************************************************************************/
1021static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
1022{
1023 /* Save link key, if not temporary */
1024 bt_bdaddr_t bd_addr;
1025 bt_status_t status = BT_STATUS_FAIL;
1026 bt_bond_state_t state = BT_BOND_STATE_NONE;
Priti Agherac0edf9f2014-06-26 11:23:51 -07001027 BOOLEAN skip_sdp = FALSE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001028
1029 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
1030 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
1031 {
1032 if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) || (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
Andre Eisenbach89363762015-01-26 13:49:36 -08001033 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) || pairing_cb.bond_type == BOND_TYPE_PERSISTENT)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001034 {
1035 bt_status_t ret;
Andre Eisenbach89363762015-01-26 13:49:36 -08001036 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, bond_type=%d",
1037 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001038 ret = btif_storage_add_bonded_device(&bd_addr,
1039 p_auth_cmpl->key, p_auth_cmpl->key_type,
1040 pairing_cb.pin_code_len);
1041 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
1042 }
1043 else
1044 {
Andre Eisenbach89363762015-01-26 13:49:36 -08001045 BTIF_TRACE_DEBUG("%s: Temporary key. Not storing. key_type=0x%x, bond_type=%d",
1046 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1047 if(pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
Hemant Guptab820aec2013-12-24 19:59:57 +05301048 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001049 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
Hemant Guptab820aec2013-12-24 19:59:57 +05301050 __FUNCTION__);
1051 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1052 return;
1053 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001054 }
1055 }
Priti Agherac0edf9f2014-06-26 11:23:51 -07001056
1057 // Skip SDP for certain HID Devices
The Android Open Source Project5738f832012-12-12 16:00:35 -08001058 if (p_auth_cmpl->success)
1059 {
Andre Eisenbach31a64002014-10-14 14:29:19 -07001060 pairing_cb.timeout_retries = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001061 status = BT_STATUS_SUCCESS;
1062 state = BT_BOND_STATE_BONDED;
Priti Agherac0edf9f2014-06-26 11:23:51 -07001063 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001064
Priti Agherac0edf9f2014-06-26 11:23:51 -07001065 if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr, COD_HID_MAJOR))
1066 {
1067 ALOGW("%s:skip SDP",
1068 __FUNCTION__);
1069 skip_sdp = TRUE;
1070 }
1071 if(!pairing_cb.is_local_initiated && skip_sdp)
1072 {
1073 bond_state_changed(status, &bd_addr, state);
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -08001074
Priti Agherac0edf9f2014-06-26 11:23:51 -07001075 ALOGW("%s: Incoming HID Connection",__FUNCTION__);
1076 bt_property_t prop;
1077 bt_bdaddr_t bd_addr;
1078 bt_uuid_t uuid;
1079 char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE;
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -08001080
Priti Agherac0edf9f2014-06-26 11:23:51 -07001081 string_to_uuid(uuid_str, &uuid);
1082
1083 prop.type = BT_PROPERTY_UUIDS;
1084 prop.val = uuid.uu;
1085 prop.len = MAX_UUID_SIZE;
1086
1087 /* Send the event to the BTIF */
1088 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1089 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1090 }
1091 else
1092 {
1093 /* Trigger SDP on the device */
1094 pairing_cb.sdp_attempts = 1;;
1095
1096 if(btif_dm_inquiry_in_progress)
1097 btif_dm_cancel_discovery();
1098
1099 btif_dm_get_remote_services(&bd_addr);
1100 }
1101 /* 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 -08001102 }
1103 else
1104 {
1105 /*Map the HCI fail reason to bt status */
1106 switch(p_auth_cmpl->fail_reason)
1107 {
1108 case HCI_ERR_PAGE_TIMEOUT:
Andre Eisenbach31a64002014-10-14 14:29:19 -07001109 if (blacklistPairingRetries(bd_addr.address) && pairing_cb.timeout_retries)
1110 {
1111 BTIF_TRACE_WARNING("%s() - Pairing timeout; retrying (%d) ...", __FUNCTION__, pairing_cb.timeout_retries);
1112 --pairing_cb.timeout_retries;
1113 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
1114 return;
1115 }
1116 /* Fall-through */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001117 case HCI_ERR_CONNECTION_TOUT:
1118 status = BT_STATUS_RMT_DEV_DOWN;
1119 break;
1120
Hemant Guptaaef7a672013-07-31 19:00:12 +05301121 case HCI_ERR_PAIRING_NOT_ALLOWED:
1122 status = BT_STATUS_AUTH_REJECTED;
1123 break;
1124
Hemant Guptab4801442014-01-07 18:11:15 +05301125 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
1126 status = BT_STATUS_AUTH_FAILURE;
1127 break;
1128
The Android Open Source Project5738f832012-12-12 16:00:35 -08001129 /* map the auth failure codes, so we can retry pairing if necessary */
1130 case HCI_ERR_AUTH_FAILURE:
Hemant Gupta59a88ec2014-03-19 19:01:35 +05301131 case HCI_ERR_KEY_MISSING:
Zhihai Xua7ea8092013-11-27 14:10:53 +05301132 btif_storage_remove_bonded_device(&bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001133 case HCI_ERR_HOST_REJECT_SECURITY:
1134 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
1135 case HCI_ERR_UNIT_KEY_USED:
1136 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
1137 case HCI_ERR_INSUFFCIENT_SECURITY:
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301138 case HCI_ERR_PEER_USER:
1139 case HCI_ERR_UNSPECIFIED:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001140 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d",
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301141 __FUNCTION__, p_auth_cmpl->fail_reason);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001142 if (pairing_cb.autopair_attempts == 1)
1143 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001144 BTIF_TRACE_DEBUG("%s(): Adding device to blacklist ", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001145
1146 /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class */
1147 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
1148 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
1149 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
1150 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
1151 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
1152 check_cod(&bd_addr, COD_HID_POINTING))
1153 {
1154 btif_storage_add_device_to_autopair_blacklist (&bd_addr);
1155 }
1156 pairing_cb.autopair_attempts++;
1157
1158 /* Create the Bond once again */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001159 BTIF_TRACE_DEBUG("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07001160 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001161 return;
1162 }
1163 else
1164 {
1165 /* if autopair attempts are more than 1, or not attempted */
1166 status = BT_STATUS_AUTH_FAILURE;
1167 }
1168 break;
1169
1170 default:
1171 status = BT_STATUS_FAIL;
1172 }
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301173 /* Special Handling for HID Devices */
1174 if (check_cod(&bd_addr, COD_HID_POINTING)) {
1175 /* Remove Device as bonded in nvram as authentication failed */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001176 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __FUNCTION__);
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301177 btif_storage_remove_bonded_device(&bd_addr);
1178 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001179 bond_state_changed(status, &bd_addr, state);
1180 }
1181}
1182
1183/******************************************************************************
1184**
1185** Function btif_dm_search_devices_evt
1186**
1187** Description Executes search devices callback events in btif context
1188**
1189** Returns void
1190**
1191******************************************************************************/
1192static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
1193{
1194 tBTA_DM_SEARCH *p_search_data;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001195 BTIF_TRACE_EVENT("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001196
1197 switch (event)
1198 {
1199 case BTA_DM_DISC_RES_EVT:
1200 {
1201 p_search_data = (tBTA_DM_SEARCH *)p_param;
1202 /* Remote name update */
1203 if (strlen((const char *) p_search_data->disc_res.bd_name))
1204 {
1205 bt_property_t properties[1];
1206 bt_bdaddr_t bdaddr;
1207 bt_status_t status;
1208
1209 properties[0].type = BT_PROPERTY_BDNAME;
1210 properties[0].val = p_search_data->disc_res.bd_name;
1211 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
1212 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
1213
1214 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
1215 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
1216 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1217 status, &bdaddr, 1, properties);
1218 }
1219 /* TODO: Services? */
1220 }
1221 break;
1222
1223 case BTA_DM_INQ_RES_EVT:
1224 {
1225 /* inquiry result */
1226 UINT32 cod;
1227 UINT8 *p_eir_remote_name = NULL;
1228 bt_bdname_t bdname;
1229 bt_bdaddr_t bdaddr;
1230 UINT8 remote_name_len;
1231 UINT8 *p_cached_name = NULL;
1232 tBTA_SERVICE_MASK services = 0;
1233 bdstr_t bdstr;
1234
1235 p_search_data = (tBTA_DM_SEARCH *)p_param;
1236 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
1237
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001238 BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __FUNCTION__, bd2str(&bdaddr, &bdstr),
The Android Open Source Project5738f832012-12-12 16:00:35 -08001239#if (BLE_INCLUDED == TRUE)
1240 p_search_data->inq_res.device_type);
1241#else
1242 BT_DEVICE_TYPE_BREDR);
1243#endif
1244 bdname.name[0] = 0;
1245
1246 cod = devclass2uint (p_search_data->inq_res.dev_class);
1247
1248 if ( cod == 0) {
1249 ALOGD("cod is 0, set as unclassified");
1250 cod = COD_UNCLASSIFIED;
1251 }
1252
1253 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1254 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1255
1256 /* Check EIR for remote name and services */
1257 if (p_search_data->inq_res.p_eir)
1258 {
1259 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001260 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001261 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */
1262 }
1263
1264
1265 {
1266 bt_property_t properties[5];
1267 bt_device_type_t dev_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001268 UINT8 addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001269 uint32_t num_properties = 0;
1270 bt_status_t status;
1271
1272 memset(properties, 0, sizeof(properties));
1273 /* BD_ADDR */
1274 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1275 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1276 num_properties++;
1277 /* BD_NAME */
1278 /* Don't send BDNAME if it is empty */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001279 if (bdname.name[0])
1280 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001281 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1282 BT_PROPERTY_BDNAME,
1283 strlen((char *)bdname.name), &bdname);
1284 num_properties++;
1285 }
1286
1287 /* DEV_CLASS */
1288 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1289 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1290 num_properties++;
1291 /* DEV_TYPE */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001292#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001293 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1294 dev_type = p_search_data->inq_res.device_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001295 addr_type = p_search_data->inq_res.ble_addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001296#else
1297 dev_type = BT_DEVICE_TYPE_BREDR;
1298#endif
1299 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1300 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1301 num_properties++;
1302 /* RSSI */
1303 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1304 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1305 &(p_search_data->inq_res.rssi));
1306 num_properties++;
1307
1308 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1309 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001310#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1311 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001312 if (( dev_type == BT_DEVICE_TYPE_DUMO)&&
1313 (p_search_data->inq_res.flag & BTA_BLE_DMT_CONTROLLER_SPT) &&
1314 (p_search_data->inq_res.flag & BTA_BLE_DMT_HOST_SPT))
1315 {
1316 btif_storage_set_dmt_support_type (&bdaddr, TRUE);
1317 }
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001318 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1319#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001320 /* Callback to notify upper layer of device */
1321 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1322 num_properties, properties);
1323 }
1324 }
1325 break;
1326
1327 case BTA_DM_INQ_CMPL_EVT:
1328 {
Satya Calloji6e2d9db2014-07-08 16:18:58 -07001329#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1330 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1331 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1332 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1333 bte_scan_filt_param_cfg_evt, 0);
1334#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001335 }
1336 break;
1337 case BTA_DM_DISC_CMPL_EVT:
1338 {
1339 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1340 }
1341 break;
1342 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1343 {
1344 /* if inquiry is not in progress and we get a cancel event, then
1345 * it means we are done with inquiry, but remote_name fetches are in
1346 * progress
1347 *
1348 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt
1349 * but instead wait for the cancel_cmpl_evt via the Busy Level
1350 *
1351 */
1352 if (btif_dm_inquiry_in_progress == FALSE)
1353 {
1354 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1355 }
1356 }
1357 break;
1358 }
1359}
1360
1361/*******************************************************************************
1362**
1363** Function btif_dm_search_services_evt
1364**
1365** Description Executes search services event in btif context
1366**
1367** Returns void
1368**
1369*******************************************************************************/
1370static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1371{
1372 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1373
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001374 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001375 switch (event)
1376 {
1377 case BTA_DM_DISC_RES_EVT:
1378 {
1379 bt_uuid_t uuid_arr[BT_MAX_NUM_UUIDS]; /* Max 32 services */
1380 bt_property_t prop;
1381 uint32_t i = 0, j = 0;
1382 bt_bdaddr_t bd_addr;
1383 bt_status_t ret;
1384
1385 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1386
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001387 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001388 p_data->disc_res.result, p_data->disc_res.services);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001389 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1390 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1391 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1392 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001393 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001394 pairing_cb.sdp_attempts++;
1395 btif_dm_get_remote_services(&bd_addr);
1396 return;
1397 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001398 prop.type = BT_PROPERTY_UUIDS;
1399 prop.len = 0;
1400 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1401 {
1402 prop.val = p_data->disc_res.p_uuid_list;
1403 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1404 for (i=0; i < p_data->disc_res.num_uuids; i++)
1405 {
1406 char temp[256];
1407 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 -07001408 BTIF_TRACE_ERROR("Index: %d uuid:%s", i, temp);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001409 }
1410 }
1411
1412 /* onUuidChanged requires getBondedDevices to be populated.
1413 ** bond_state_changed needs to be sent prior to remote_device_property
1414 */
1415 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1416 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0)&&
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001417 pairing_cb.sdp_attempts > 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001418 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001419 BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001420 __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001421 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001422 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1423 }
1424
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001425 if(p_data->disc_res.num_uuids != 0)
1426 {
1427 /* Also write this to the NVRAM */
1428 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1429 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1430 /* Send the event to the BTIF */
1431 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1432 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1433 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001434 }
1435 break;
1436
1437 case BTA_DM_DISC_CMPL_EVT:
1438 /* fixme */
1439 break;
1440
Matthew Xie607e3b72013-08-15 19:30:48 -07001441#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001442 case BTA_DM_DISC_BLE_RES_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001443 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__,
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001444 p_data->disc_ble_res.service.uu.uuid16);
1445 bt_uuid_t uuid;
1446 int i = 0;
1447 int j = 15;
1448 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1449 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001450 BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001451 bt_property_t prop;
1452 bt_bdaddr_t bd_addr;
1453 char temp[256];
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001454 bt_status_t ret;
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001455
1456 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1457
1458 while(i < j )
1459 {
1460 unsigned char c = uuid.uu[j];
1461 uuid.uu[j] = uuid.uu[i];
1462 uuid.uu[i] = c;
1463 i++;
1464 j--;
1465 }
1466
1467 uuid_to_string(&uuid, temp);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001468 BTIF_TRACE_ERROR(" uuid:%s", temp);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001469
1470 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1471 prop.type = BT_PROPERTY_UUIDS;
1472 prop.val = uuid.uu;
1473 prop.len = MAX_UUID_SIZE;
1474
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001475 /* Also write this to the NVRAM */
1476 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1477 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1478
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001479 /* Send the event to the BTIF */
1480 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1481 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1482
1483 }
1484 break;
Matthew Xie607e3b72013-08-15 19:30:48 -07001485#endif /* BLE_INCLUDED */
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001486
The Android Open Source Project5738f832012-12-12 16:00:35 -08001487 default:
1488 {
1489 ASSERTC(0, "unhandled search services event", event);
1490 }
1491 break;
1492 }
1493}
1494
1495/*******************************************************************************
1496**
1497** Function btif_dm_remote_service_record_evt
1498**
1499** Description Executes search service record event in btif context
1500**
1501** Returns void
1502**
1503*******************************************************************************/
1504static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1505{
1506 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1507
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001508 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001509 switch (event)
1510 {
1511 case BTA_DM_DISC_RES_EVT:
1512 {
1513 bt_service_record_t rec;
1514 bt_property_t prop;
1515 uint32_t i = 0;
1516 bt_bdaddr_t bd_addr;
1517
1518 memset(&rec, 0, sizeof(bt_service_record_t));
1519 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1520
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001521 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001522 p_data->disc_res.result, p_data->disc_res.services);
1523 prop.type = BT_PROPERTY_SERVICE_RECORD;
1524 prop.val = (void*)&rec;
1525 prop.len = sizeof(rec);
1526
1527 /* disc_res.result is overloaded with SCN. Cannot check result */
1528 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1529 /* TODO: Get the UUID as well */
1530 rec.channel = p_data->disc_res.result - 3;
1531 /* TODO: Need to get the service name using p_raw_data */
1532 rec.name[0] = 0;
1533
1534 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1535 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1536 }
1537 break;
1538
1539 default:
1540 {
1541 ASSERTC(0, "unhandled remote service record event", event);
1542 }
1543 break;
1544 }
1545}
1546
1547/*******************************************************************************
1548**
1549** Function btif_dm_upstreams_cback
1550**
1551** Description Executes UPSTREAMS events in btif context
1552**
1553** Returns void
1554**
1555*******************************************************************************/
1556static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1557{
1558 tBTA_DM_SEC_EVT dm_event = (tBTA_DM_SEC_EVT)event;
1559 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1560 tBTA_SERVICE_MASK service_mask;
1561 uint32_t i;
1562 bt_bdaddr_t bd_addr;
1563
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001564 BTIF_TRACE_EVENT("btif_dm_upstreams_cback ev: %s", dump_dm_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001565
1566 switch (event)
1567 {
1568 case BTA_DM_ENABLE_EVT:
1569 {
1570 BD_NAME bdname;
1571 bt_status_t status;
1572 bt_property_t prop;
1573 prop.type = BT_PROPERTY_BDNAME;
1574 prop.len = BD_NAME_LEN;
1575 prop.val = (void*)bdname;
1576
1577 status = btif_storage_get_adapter_property(&prop);
Matthew Xie1e5109b2012-11-09 18:26:26 -08001578 if (status == BT_STATUS_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001579 {
1580 /* A name exists in the storage. Make this the device name */
1581 BTA_DmSetDeviceName((char*)prop.val);
1582 }
Matthew Xie1e5109b2012-11-09 18:26:26 -08001583 else
1584 {
1585 /* Storage does not have a name yet.
1586 * Use the default name and write it to the chip
1587 */
1588 BTA_DmSetDeviceName(btif_get_default_local_name());
1589 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001590
Andre Eisenbacha015a832014-09-11 14:09:40 -07001591#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1592 /* Enable local privacy */
Andre Eisenbach3e0dc732014-10-24 09:55:34 -07001593 BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED);
Andre Eisenbacha015a832014-09-11 14:09:40 -07001594#endif
1595
The Android Open Source Project5738f832012-12-12 16:00:35 -08001596 /* for each of the enabled services in the mask, trigger the profile
1597 * enable */
1598 service_mask = btif_get_enabled_services_mask();
1599 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1600 {
1601 if (service_mask &
1602 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1603 {
1604 btif_in_execute_service_request(i, TRUE);
1605 }
1606 }
1607 /* clear control blocks */
1608 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1609
1610 /* This function will also trigger the adapter_properties_cb
1611 ** and bonded_devices_info_cb
1612 */
1613 btif_storage_load_bonded_devices();
1614
1615 btif_storage_load_autopair_device_list();
1616
1617 btif_enable_bluetooth_evt(p_data->enable.status, p_data->enable.bd_addr);
1618 }
1619 break;
1620
1621 case BTA_DM_DISABLE_EVT:
1622 /* for each of the enabled services in the mask, trigger the profile
1623 * disable */
1624 service_mask = btif_get_enabled_services_mask();
1625 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1626 {
1627 if (service_mask &
1628 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1629 {
1630 btif_in_execute_service_request(i, FALSE);
1631 }
1632 }
1633 btif_disable_bluetooth_evt();
1634 break;
1635
1636 case BTA_DM_PIN_REQ_EVT:
1637 btif_dm_pin_req_evt(&p_data->pin_req);
1638 break;
1639
1640 case BTA_DM_AUTH_CMPL_EVT:
1641 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1642 break;
1643
1644 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1645 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1646 {
1647 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1648 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1649 }
1650 break;
1651
1652 case BTA_DM_SP_CFM_REQ_EVT:
1653 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1654 break;
1655 case BTA_DM_SP_KEY_NOTIF_EVT:
1656 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1657 break;
1658
1659 case BTA_DM_DEV_UNPAIRED_EVT:
1660 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1661
1662 /*special handling for HID devices */
1663 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -07001664 btif_hh_remove_device(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001665 #endif
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001666 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1667 btif_storage_remove_ble_bonding_keys(&bd_addr);
1668 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001669 btif_storage_remove_bonded_device(&bd_addr);
1670 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1671 break;
1672
1673 case BTA_DM_BUSY_LEVEL_EVT:
1674 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001675
1676 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001677 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001678 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001679 {
1680 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1681 BT_DISCOVERY_STARTED);
1682 btif_dm_inquiry_in_progress = TRUE;
1683 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001684 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001685 {
1686 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1687 BT_DISCOVERY_STOPPED);
1688 btif_dm_inquiry_in_progress = FALSE;
1689 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001690 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001691 {
1692 btif_dm_inquiry_in_progress = FALSE;
1693 }
1694 }
1695 }break;
1696
1697 case BTA_DM_LINK_UP_EVT:
1698 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001699 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001700
1701 btif_update_remote_version_property(&bd_addr);
1702
The Android Open Source Project5738f832012-12-12 16:00:35 -08001703 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1704 &bd_addr, BT_ACL_STATE_CONNECTED);
1705 break;
1706
1707 case BTA_DM_LINK_DOWN_EVT:
1708 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001709 BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001710 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1711 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1712 break;
1713
1714 case BTA_DM_HW_ERROR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001715 BTIF_TRACE_ERROR("Received H/W Error. ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001716 /* Flush storage data */
1717 btif_config_flush();
1718 usleep(100000); /* 100milliseconds */
1719 /* Killing the process to force a restart as part of fault tolerance */
1720 kill(getpid(), SIGKILL);
1721 break;
1722
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001723#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1724 case BTA_DM_BLE_KEY_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001725 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 -08001726
1727 /* If this pairing is by-product of local initiated GATT client Read or Write,
1728 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1729 have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1730 if (pairing_cb.state != BT_BOND_STATE_BONDING)
1731 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001732 BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001733 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1734 BT_BOND_STATE_BONDING);
1735 }
1736 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1737 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001738 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001739 break;
1740 }
1741
1742 switch (p_data->ble_key.key_type)
1743 {
1744 case BTA_LE_KEY_PENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001745 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001746 pairing_cb.ble.is_penc_key_rcvd = TRUE;
1747 memcpy(pairing_cb.ble.penc_key.ltk,p_data->ble_key.key_value.penc_key.ltk, 16);
1748 memcpy(pairing_cb.ble.penc_key.rand, p_data->ble_key.key_value.penc_key.rand,8);
1749 pairing_cb.ble.penc_key.ediv = p_data->ble_key.key_value.penc_key.ediv;
1750 pairing_cb.ble.penc_key.sec_level = p_data->ble_key.key_value.penc_key.sec_level;
1751
1752 for (i=0; i<16; i++)
1753 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001754 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 -08001755 }
1756 for (i=0; i<8; i++)
1757 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001758 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 -08001759 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001760 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.ediv=0x%04x",pairing_cb.ble.penc_key.ediv);
1761 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.sec_level=0x%02x",pairing_cb.ble.penc_key.sec_level);
1762 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 -08001763 break;
1764
1765 case BTA_LE_KEY_PID:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001766 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001767 pairing_cb.ble.is_pid_key_rcvd = TRUE;
Andre Eisenbach5e808462014-10-21 12:37:53 -07001768 pairing_cb.ble.pid_key.addr_type = p_data->ble_key.key_value.pid_key.addr_type;
1769 memcpy(pairing_cb.ble.pid_key.irk, p_data->ble_key.key_value.pid_key.irk, 16);
1770 memcpy(pairing_cb.ble.pid_key.static_addr,
1771 p_data->ble_key.key_value.pid_key.static_addr,BD_ADDR_LEN);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001772 for (i=0; i<16; i++)
1773 {
Andre Eisenbach5e808462014-10-21 12:37:53 -07001774 BTIF_TRACE_DEBUG("pairing_cb.ble.pid_key.irk[%d]=0x%02x"
1775 ,i,pairing_cb.ble.pid_key.irk[i]);
1776 }
1777 for (i=0; i<BD_ADDR_LEN; i++)
1778 {
1779 BTIF_TRACE_DEBUG("piaring_cb.ble.pid_address[%d] = %x"
1780 ,i, pairing_cb.ble.pid_key.static_addr[i]);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001781 }
1782 break;
1783
1784 case BTA_LE_KEY_PCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001785 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001786 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1787 pairing_cb.ble.pcsrk_key.counter = p_data->ble_key.key_value.pcsrk_key.counter;
1788 pairing_cb.ble.pcsrk_key.sec_level = p_data->ble_key.key_value.pcsrk_key.sec_level;
1789 memcpy(pairing_cb.ble.pcsrk_key.csrk,p_data->ble_key.key_value.pcsrk_key.csrk,16);
1790
1791 for (i=0; i<16; i++)
1792 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001793 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 -08001794 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001795 BTIF_TRACE_DEBUG("pairing_cb.ble.pcsrk_key.counter=0x%08x",pairing_cb.ble.pcsrk_key.counter);
1796 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 -08001797 break;
1798
1799 case BTA_LE_KEY_LENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001800 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001801 pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1802 pairing_cb.ble.lenc_key.div = p_data->ble_key.key_value.lenc_key.div;
1803 pairing_cb.ble.lenc_key.key_size = p_data->ble_key.key_value.lenc_key.key_size;
1804 pairing_cb.ble.lenc_key.sec_level = p_data->ble_key.key_value.lenc_key.sec_level;
1805
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001806 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.div=0x%04x",pairing_cb.ble.lenc_key.div);
1807 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.key_size=0x%02x",pairing_cb.ble.lenc_key.key_size);
1808 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 -08001809 break;
1810
1811
1812
1813 case BTA_LE_KEY_LCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001814 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001815 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1816 pairing_cb.ble.lcsrk_key.counter = p_data->ble_key.key_value.lcsrk_key.counter;
1817 pairing_cb.ble.lcsrk_key.div = p_data->ble_key.key_value.lcsrk_key.div;
1818 pairing_cb.ble.lcsrk_key.sec_level = p_data->ble_key.key_value.lcsrk_key.sec_level;
1819
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001820 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.div=0x%04x",pairing_cb.ble.lcsrk_key.div);
1821 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.counter=0x%08x",pairing_cb.ble.lcsrk_key.counter);
1822 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 -08001823
1824 break;
1825
1826 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001827 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001828 break;
1829 }
1830
1831 break;
1832 case BTA_DM_BLE_SEC_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001833 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001834 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1835 break;
1836 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001837 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001838 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1839 break;
1840 case BTA_DM_BLE_PASSKEY_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001841 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001842 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1843 break;
1844 case BTA_DM_BLE_OOB_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001845 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001846 break;
1847 case BTA_DM_BLE_LOCAL_IR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001848 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001849 ble_local_key_cb.is_id_keys_rcvd = TRUE;
1850 memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1851 memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1852 memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1853 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1854 BTIF_DM_LE_LOCAL_KEY_IR,
1855 BT_OCTET16_LEN);
1856 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1857 BTIF_DM_LE_LOCAL_KEY_IRK,
1858 BT_OCTET16_LEN);
1859 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1860 BTIF_DM_LE_LOCAL_KEY_DHK,
1861 BT_OCTET16_LEN);
1862 break;
1863 case BTA_DM_BLE_LOCAL_ER_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001864 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001865 ble_local_key_cb.is_er_rcvd = TRUE;
1866 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1867 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1868 BTIF_DM_LE_LOCAL_KEY_ER,
1869 BT_OCTET16_LEN);
1870 break;
1871
1872 case BTA_DM_BLE_AUTH_CMPL_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001873 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001874 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1875 break;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001876
1877 case BTA_DM_LE_FEATURES_READ:
1878 {
1879 tBTM_BLE_VSC_CB cmn_vsc_cb;
1880 bt_local_le_features_t local_le_features;
1881 char buf[512];
1882 bt_property_t prop;
1883 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1884 prop.val = (void*)buf;
1885 prop.len = sizeof(buf);
1886
1887 /* LE features are not stored in storage. Should be retrived from stack */
1888 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1889 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1890
1891 prop.len = sizeof (bt_local_le_features_t);
1892 if (cmn_vsc_cb.filter_support == 1)
1893 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1894 else
1895 local_le_features.max_adv_filter_supported = 0;
1896 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1897 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1898 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
Satya Callojid773c2c2014-07-29 22:08:55 -07001899 local_le_features.scan_result_storage_size_hibyte =
1900 (cmn_vsc_cb.tot_scan_results_strg >> 8) & (0xFF);
1901 local_le_features.scan_result_storage_size_lobyte =
1902 (cmn_vsc_cb.tot_scan_results_strg) & (0xFF);
Satya Callojiefaddcb2014-07-28 23:22:05 -07001903 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001904 memcpy(prop.val, &local_le_features, prop.len);
1905 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
1906 break;
1907 }
Satya Callojie5ba8842014-07-03 17:18:02 -07001908
1909 case BTA_DM_ENER_INFO_READ:
1910 {
1911 btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param;
1912 bt_activity_energy_info energy_info;
1913 energy_info.status = p_ener_data->status;
1914 energy_info.ctrl_state = p_ener_data->ctrl_state;
1915 energy_info.rx_time = p_ener_data->rx_time;
1916 energy_info.tx_time = p_ener_data->tx_time;
1917 energy_info.idle_time = p_ener_data->idle_time;
1918 energy_info.energy_used = p_ener_data->energy_used;
1919 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info);
1920 break;
1921 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001922#endif
1923
The Android Open Source Project5738f832012-12-12 16:00:35 -08001924 case BTA_DM_AUTHORIZE_EVT:
1925 case BTA_DM_SIG_STRENGTH_EVT:
1926 case BTA_DM_SP_RMT_OOB_EVT:
1927 case BTA_DM_SP_KEYPRESS_EVT:
1928 case BTA_DM_ROLE_CHG_EVT:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001929
The Android Open Source Project5738f832012-12-12 16:00:35 -08001930 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001931 BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001932 break;
1933 }
1934} /* btui_security_cback() */
1935
1936
1937/*******************************************************************************
1938**
1939** Function btif_dm_generic_evt
1940**
1941** Description Executes non-BTA upstream events in BTIF context
1942**
1943** Returns void
1944**
1945*******************************************************************************/
1946static void btif_dm_generic_evt(UINT16 event, char* p_param)
1947{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001948 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001949 switch(event)
1950 {
1951 case BTIF_DM_CB_DISCOVERY_STARTED:
1952 {
1953 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
1954 }
1955 break;
1956
1957 case BTIF_DM_CB_CREATE_BOND:
1958 {
Andre Eisenbach31a64002014-10-14 14:29:19 -07001959 pairing_cb.timeout_retries = NUM_TIMEOUT_RETRIES;
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07001960 btif_dm_create_bond_cb_t *create_bond_cb = (btif_dm_create_bond_cb_t*)p_param;
1961 btif_dm_cb_create_bond(&create_bond_cb->bdaddr, create_bond_cb->transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001962 }
1963 break;
1964
1965 case BTIF_DM_CB_REMOVE_BOND:
1966 {
1967 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
1968 }
1969 break;
1970
1971 case BTIF_DM_CB_HID_REMOTE_NAME:
1972 {
1973 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
1974 }
1975 break;
1976
1977 case BTIF_DM_CB_BOND_STATE_BONDING:
1978 {
1979 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
1980 }
1981 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001982 case BTIF_DM_CB_LE_TX_TEST:
1983 case BTIF_DM_CB_LE_RX_TEST:
1984 {
1985 uint8_t status;
1986 STREAM_TO_UINT8(status, p_param);
1987 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1988 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
1989 }
1990 break;
1991 case BTIF_DM_CB_LE_TEST_END:
1992 {
1993 uint8_t status;
1994 uint16_t count = 0;
1995 STREAM_TO_UINT8(status, p_param);
1996 if (status == 0)
1997 STREAM_TO_UINT16(count, p_param);
1998 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1999 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
2000 }
2001 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002002 default:
2003 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002004 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002005 }
2006 break;
2007 }
2008}
2009
2010/*******************************************************************************
2011**
2012** Function bte_dm_evt
2013**
2014** Description Switches context from BTE to BTIF for all DM events
2015**
2016** Returns void
2017**
2018*******************************************************************************/
2019
2020void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
2021{
2022 bt_status_t status;
2023
2024 /* switch context to btif task context (copy full union size for convenience) */
2025 status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event, (void*)p_data, sizeof(tBTA_DM_SEC), NULL);
2026
2027 /* catch any failed context transfers */
2028 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
2029}
2030
2031/*******************************************************************************
2032**
2033** Function bte_search_devices_evt
2034**
2035** Description Switches context from BTE to BTIF for DM search events
2036**
2037** Returns void
2038**
2039*******************************************************************************/
2040static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2041{
2042 UINT16 param_len = 0;
2043
2044 if (p_data)
2045 param_len += sizeof(tBTA_DM_SEARCH);
2046 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
2047 switch (event)
2048 {
2049 case BTA_DM_INQ_RES_EVT:
2050 {
2051 if (p_data->inq_res.p_eir)
2052 param_len += HCI_EXT_INQ_RESPONSE_LEN;
2053 }
2054 break;
2055
2056 case BTA_DM_DISC_RES_EVT:
2057 {
2058 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
2059 param_len += p_data->disc_res.raw_data_size;
2060 }
2061 break;
2062 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002063 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 -08002064
2065 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
2066 if (event == BTA_DM_INQ_RES_EVT)
2067 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
2068
2069 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
2070 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
2071}
2072
2073/*******************************************************************************
2074**
2075** Function bte_dm_search_services_evt
2076**
2077** Description Switches context from BTE to BTIF for DM search services
2078** event
2079**
2080** Returns void
2081**
2082*******************************************************************************/
2083static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2084{
2085 UINT16 param_len = 0;
2086 if (p_data)
2087 param_len += sizeof(tBTA_DM_SEARCH);
2088 switch (event)
2089 {
2090 case BTA_DM_DISC_RES_EVT:
2091 {
2092 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
2093 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
2094 }
2095 } break;
2096 }
2097 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
2098 * if raw_data is needed. */
2099 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
2100 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
2101}
2102
2103/*******************************************************************************
2104**
2105** Function bte_dm_remote_service_record_evt
2106**
2107** Description Switches context from BTE to BTIF for DM search service
2108** record event
2109**
2110** Returns void
2111**
2112*******************************************************************************/
2113static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2114{
2115 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
2116 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
2117}
2118
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002119#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07002120/*******************************************************************************
2121**
2122** Function bta_energy_info_cb
2123**
2124** Description Switches context from BTE to BTIF for DM energy info event
2125**
2126** Returns void
2127**
2128*******************************************************************************/
2129static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time,
2130 tBTA_DM_BLE_IDLE_TIME_MS idle_time,
2131 tBTA_DM_BLE_ENERGY_USED energy_used,
2132 tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status)
2133{
2134 BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld",
2135 status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
2136
2137 btif_activity_energy_info_cb_t btif_cb;
2138 btif_cb.status = status;
2139 btif_cb.ctrl_state = ctrl_state;
2140 btif_cb.tx_time = (uint64_t) tx_time;
2141 btif_cb.rx_time = (uint64_t) rx_time;
2142 btif_cb.idle_time =(uint64_t) idle_time;
2143 btif_cb.energy_used =(uint64_t) energy_used;
2144 btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
2145 (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL);
2146}
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002147#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07002148
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002149/*******************************************************************************
2150**
2151** Function bte_scan_filt_param_cfg_evt
2152**
2153** Description Scan filter param config event
2154**
2155** Returns void
2156**
2157*******************************************************************************/
2158static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
2159 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
2160 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
2161{
2162 /* This event occurs on calling BTA_DmBleCfgFilterCondition internally,
2163 ** and that is why there is no HAL callback
2164 */
2165 if(BTA_SUCCESS != status)
2166 {
2167 BTIF_TRACE_ERROR("%s, %d", __FUNCTION__, status);
2168 }
2169 else
2170 {
2171 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2172 }
2173}
2174
The Android Open Source Project5738f832012-12-12 16:00:35 -08002175/*****************************************************************************
2176**
2177** btif api functions (no context switch)
2178**
2179*****************************************************************************/
2180
2181/*******************************************************************************
2182**
2183** Function btif_dm_start_discovery
2184**
2185** Description Start device discovery/inquiry
2186**
2187** Returns bt_status_t
2188**
2189*******************************************************************************/
2190bt_status_t btif_dm_start_discovery(void)
2191{
2192 tBTA_DM_INQ inq_params;
2193 tBTA_SERVICE_MASK services = 0;
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002194 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002195
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002196 BTIF_TRACE_EVENT("%s", __FUNCTION__);
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002197
2198#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2199 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
2200 /* Cleanup anything remaining on index 0 */
2201 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
2202 bte_scan_filt_param_cfg_evt, 0);
2203
2204 /* Add an allow-all filter on index 0*/
2205 adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE;
2206 adv_filt_param.feat_seln = ALLOW_ALL_FILTER;
2207 adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;
2208 adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;
2209 adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE;
2210 adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE;
2211 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL,
2212 bte_scan_filt_param_cfg_evt, 0);
2213
The Android Open Source Project5738f832012-12-12 16:00:35 -08002214 /* TODO: Do we need to handle multiple inquiries at the same time? */
2215
2216 /* Set inquiry params and call API */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002217 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
Matthew Xie7f3e4292013-09-30 12:44:10 -07002218#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
2219 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
2220 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
2221 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
2222 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
2223#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002224#else
2225 inq_params.mode = BTA_DM_GENERAL_INQUIRY;
2226#endif
2227 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
2228
2229 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
2230 inq_params.report_dup = TRUE;
2231
2232 inq_params.filter_type = BTA_DM_INQ_CLR;
2233 /* TODO: Filter device by BDA needs to be implemented here */
2234
2235 /* Will be enabled to TRUE once inquiry busy level has been received */
2236 btif_dm_inquiry_in_progress = FALSE;
2237 /* find nearby devices */
2238 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
2239
2240 return BT_STATUS_SUCCESS;
2241}
2242
2243/*******************************************************************************
2244**
2245** Function btif_dm_cancel_discovery
2246**
2247** Description Cancels search
2248**
2249** Returns bt_status_t
2250**
2251*******************************************************************************/
2252bt_status_t btif_dm_cancel_discovery(void)
2253{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002254 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002255 BTA_DmSearchCancel();
2256 return BT_STATUS_SUCCESS;
2257}
2258
2259/*******************************************************************************
2260**
2261** Function btif_dm_create_bond
2262**
2263** Description Initiate bonding with the specified device
2264**
2265** Returns bt_status_t
2266**
2267*******************************************************************************/
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002268bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr, int transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002269{
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002270 btif_dm_create_bond_cb_t create_bond_cb;
2271 create_bond_cb.transport = transport;
2272 bdcpy(create_bond_cb.bdaddr.address, bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002273
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002274 bdstr_t bdstr;
2275 BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __FUNCTION__,
2276 bd2str((bt_bdaddr_t *) bd_addr, &bdstr), transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002277 if (pairing_cb.state != BT_BOND_STATE_NONE)
2278 return BT_STATUS_BUSY;
2279
2280 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002281 (char *)&create_bond_cb, sizeof(btif_dm_create_bond_cb_t), NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002282
2283 return BT_STATUS_SUCCESS;
2284}
2285
2286/*******************************************************************************
2287**
2288** Function btif_dm_cancel_bond
2289**
2290** Description Initiate bonding with the specified device
2291**
2292** Returns bt_status_t
2293**
2294*******************************************************************************/
2295
2296bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
2297{
2298 bdstr_t bdstr;
2299
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002300 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 -08002301
2302 /* TODO:
2303 ** 1. Restore scan modes
2304 ** 2. special handling for HID devices
2305 */
2306 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2307 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002308
2309#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2310
2311 if (pairing_cb.is_ssp)
2312 {
2313 if (pairing_cb.is_le_only)
2314 {
2315 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2316 }
2317 else
Hemant Guptae1468692013-11-14 16:21:29 +05302318 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002319 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
Hemant Guptae1468692013-11-14 16:21:29 +05302320 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2321 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2322 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002323 }
2324 else
2325 {
2326 if (pairing_cb.is_le_only)
2327 {
2328 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2329 }
2330 else
2331 {
2332 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2333 }
2334 /* Cancel bonding, in case it is in ACL connection setup state */
2335 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2336 }
2337
2338#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002339 if (pairing_cb.is_ssp)
2340 {
2341 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2342 }
2343 else
2344 {
2345 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2346 }
2347 /* Cancel bonding, in case it is in ACL connection setup state */
2348 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2349 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002350#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002351 }
2352
2353 return BT_STATUS_SUCCESS;
2354}
2355
2356/*******************************************************************************
2357**
Kim Schulza9eb25c2013-09-30 10:55:52 +02002358** Function btif_dm_hh_open_failed
2359**
2360** Description informs the upper layers if the HH have failed during bonding
2361**
2362** Returns none
2363**
2364*******************************************************************************/
2365
2366void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
2367{
2368 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2369 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2370 {
2371 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2372 }
2373}
2374
2375/*******************************************************************************
2376**
The Android Open Source Project5738f832012-12-12 16:00:35 -08002377** Function btif_dm_remove_bond
2378**
2379** Description Removes bonding with the specified device
2380**
2381** Returns bt_status_t
2382**
2383*******************************************************************************/
2384
2385bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
2386{
2387 bdstr_t bdstr;
2388
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002389 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 -08002390 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2391 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2392
2393 return BT_STATUS_SUCCESS;
2394}
2395
2396/*******************************************************************************
2397**
2398** Function btif_dm_pin_reply
2399**
2400** Description BT legacy pairing - PIN code reply
2401**
2402** Returns bt_status_t
2403**
2404*******************************************************************************/
2405
2406bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2407 uint8_t pin_len, bt_pin_code_t *pin_code)
2408{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002409 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Hemant Gupta831423e2014-01-08 12:42:13 +05302410 if (pin_code == NULL)
2411 return BT_STATUS_FAIL;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002412#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002413
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002414 if (pairing_cb.is_le_only)
2415 {
2416 int i;
2417 UINT32 passkey = 0;
2418 int multi[] = {100000, 10000, 1000, 100, 10,1};
2419 BD_ADDR remote_bd_addr;
2420 bdcpy(remote_bd_addr, bd_addr->address);
2421 for (i = 0; i < 6; i++)
2422 {
2423 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2424 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002425 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002426 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2427
2428 }
2429 else
2430 {
2431 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2432 if (accept)
2433 pairing_cb.pin_code_len = pin_len;
2434 }
2435#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002436 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2437
2438 if (accept)
2439 pairing_cb.pin_code_len = pin_len;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002440#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002441 return BT_STATUS_SUCCESS;
2442}
2443
2444/*******************************************************************************
2445**
2446** Function btif_dm_ssp_reply
2447**
2448** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2449**
2450** Returns bt_status_t
2451**
2452*******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002453bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2454 bt_ssp_variant_t variant, uint8_t accept,
2455 uint32_t passkey)
2456{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08002457 UNUSED(passkey);
2458
The Android Open Source Project5738f832012-12-12 16:00:35 -08002459 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2460 {
2461 /* This is not implemented in the stack.
2462 * For devices with display, this is not needed
2463 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002464 BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002465 return BT_STATUS_FAIL;
2466 }
2467 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002468 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002469#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2470 if (pairing_cb.is_le_only)
2471 {
2472 if (accept)
2473 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2474 else
2475 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2476 }
2477 else
2478 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002479
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002480#else
2481 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2482#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002483 return BT_STATUS_SUCCESS;
2484}
2485
2486/*******************************************************************************
2487**
2488** Function btif_dm_get_adapter_property
2489**
2490** Description Queries the BTA for the adapter property
2491**
2492** Returns bt_status_t
2493**
2494*******************************************************************************/
2495bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2496{
2497 bt_status_t status;
2498
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002499 BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002500 switch (prop->type)
2501 {
2502 case BT_PROPERTY_BDNAME:
2503 {
2504 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
Matthew Xie1e5109b2012-11-09 18:26:26 -08002505 strcpy((char *)bd_name->name, btif_get_default_local_name());
The Android Open Source Project5738f832012-12-12 16:00:35 -08002506 prop->len = strlen((char *)bd_name->name);
2507 }
2508 break;
2509
2510 case BT_PROPERTY_ADAPTER_SCAN_MODE:
2511 {
2512 /* if the storage does not have it. Most likely app never set it. Default is NONE */
2513 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2514 *mode = BT_SCAN_MODE_NONE;
2515 prop->len = sizeof(bt_scan_mode_t);
2516 }
2517 break;
2518
2519 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2520 {
2521 uint32_t *tmt = (uint32_t*)prop->val;
2522 *tmt = 120; /* default to 120s, if not found in NV */
2523 prop->len = sizeof(uint32_t);
2524 }
2525 break;
2526
2527 default:
2528 prop->len = 0;
2529 return BT_STATUS_FAIL;
2530 }
2531 return BT_STATUS_SUCCESS;
2532}
2533
2534/*******************************************************************************
2535**
2536** Function btif_dm_get_remote_services
2537**
2538** Description Start SDP to get remote services
2539**
2540** Returns bt_status_t
2541**
2542*******************************************************************************/
2543bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2544{
2545 bdstr_t bdstr;
2546
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002547 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002548
2549 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2550 bte_dm_search_services_evt, TRUE);
2551
2552 return BT_STATUS_SUCCESS;
2553}
2554
2555/*******************************************************************************
2556**
2557** Function btif_dm_get_remote_service_record
2558**
2559** Description Start SDP to get remote service record
2560**
2561**
2562** Returns bt_status_t
2563*******************************************************************************/
2564bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2565 bt_uuid_t *uuid)
2566{
2567 tSDP_UUID sdp_uuid;
2568 bdstr_t bdstr;
2569
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002570 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002571
2572 sdp_uuid.len = MAX_UUID_SIZE;
2573 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2574
2575 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2576 bte_dm_remote_service_record_evt, TRUE);
2577
2578 return BT_STATUS_SUCCESS;
2579}
2580
2581void btif_dm_execute_service_request(UINT16 event, char *p_param)
2582{
2583 BOOLEAN b_enable = FALSE;
2584 bt_status_t status;
2585 if (event == BTIF_DM_ENABLE_SERVICE)
2586 {
2587 b_enable = TRUE;
2588 }
2589 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2590 if (status == BT_STATUS_SUCCESS)
2591 {
2592 bt_property_t property;
2593 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2594
2595 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2596 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2597 sizeof(local_uuids), local_uuids);
2598 btif_storage_get_adapter_property(&property);
2599 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2600 BT_STATUS_SUCCESS, 1, &property);
2601 }
2602 return;
2603}
2604
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002605void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2606 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2607{
2608 UINT8 yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2609 /* if local initiated:
2610 ** 1. set DD + MITM
2611 ** if remote initiated:
2612 ** 1. Copy over the auth_req from peer's io_rsp
2613 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2614 ** as a fallback set MITM+GB if peer had MITM set
2615 */
2616 UNUSED (bd_addr);
2617 UNUSED (p_io_cap);
2618 UNUSED (p_oob_data);
2619
2620
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002621 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002622 if(pairing_cb.is_local_initiated)
2623 {
2624 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2625 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2626 }
2627 else if (!is_orig)
2628 {
2629 /* peer initiated paring. They probably know what they want.
2630 ** Copy the mitm from peer device.
2631 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002632 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002633 __FUNCTION__, pairing_cb.auth_req);
2634 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2635
2636 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2637 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2638 *p_auth_req |= BTA_AUTH_SP_YES;
2639 }
2640 else if (yes_no_bit)
2641 {
2642 /* set the general bonding bit for stored device */
2643 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2644 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002645 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002646}
2647
2648void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2649 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2650{
2651 UNUSED (bd_addr);
2652 UNUSED (oob_data);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002653
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002654 if(auth_req & BTA_AUTH_BONDS)
2655 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002656 BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002657 pairing_cb.auth_req = auth_req;
2658 pairing_cb.io_cap = io_cap;
2659 }
2660}
2661
The Android Open Source Project5738f832012-12-12 16:00:35 -08002662#if (BTM_OOB_INCLUDED == TRUE)
2663void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data)
2664{
2665 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2666 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2667 {
2668 *p_oob_data = FALSE;
2669 }
2670 else
2671 {
2672 *p_oob_data = TRUE;
2673 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002674 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 -08002675}
2676#endif /* BTM_OOB_INCLUDED */
2677
2678#ifdef BTIF_DM_OOB_TEST
2679void btif_dm_load_local_oob(void)
2680{
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002681 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002682 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002683 BTIF_TRACE_DEBUG("btif_dm_load_local_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002684 if (prop_oob[0] != '3')
2685 {
2686#if (BTM_OOB_INCLUDED == TRUE)
2687 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2688 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2689 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002690 BTIF_TRACE_DEBUG("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002691 BTA_DmLocalOob();
2692 }
2693#else
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002694 BTIF_TRACE_ERROR("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002695#endif
2696 }
2697}
2698
2699void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2700{
2701 FILE *fp;
2702 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2703 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2704 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002705 char prop_oob[PROPERTY_VALUE_MAX];
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002706 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: valid=%d", valid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002707 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2708 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2709 valid)
2710 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002711 BTIF_TRACE_DEBUG("save local OOB data in memory");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002712 memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2713 memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2714 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002715 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002716 if (prop_oob[0] == '1')
2717 path = path_a;
2718 else if (prop_oob[0] == '2')
2719 path = path_b;
2720 if (path)
2721 {
2722 fp = fopen(path, "wb+");
2723 if (fp == NULL)
2724 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002725 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 -08002726 }
2727 else
2728 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002729 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 -08002730 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2731 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2732 fclose(fp);
2733 }
2734 }
2735 }
2736}
2737BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2738{
2739 char t[128];
2740 FILE *fp;
2741 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2742 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2743 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002744 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002745 BOOLEAN result = FALSE;
2746 bt_bdaddr_t bt_bd_addr;
2747 bdcpy(oob_cb.oob_bdaddr, bd_addr);
2748 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002749 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002750 if (prop_oob[0] == '1')
2751 path = path_b;
2752 else if (prop_oob[0] == '2')
2753 path = path_a;
2754 if (path)
2755 {
2756 fp = fopen(path, "rb");
2757 if (fp == NULL)
2758 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002759 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 -08002760 return FALSE;
2761 }
2762 else
2763 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002764 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob: read OOB data from %s",path);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002765 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2766 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2767 fclose(fp);
2768 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002769 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: TRUE");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002770 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2771 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2772 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002773 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002774 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2775 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
2776 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 -07002777 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: c = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002778 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2779 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
2780 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 -07002781 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: r = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002782 bdcpy(bt_bd_addr.address, bd_addr);
2783 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2784 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2785 result = TRUE;
2786 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002787 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob result=%d",result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002788 return result;
2789}
2790#endif /* BTIF_DM_OOB_TEST */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002791#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2792
2793static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2794{
2795 bt_bdaddr_t bd_addr;
2796 bt_bdname_t bd_name;
2797 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08002798 int dev_type;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002799
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002800 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002801
2802 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08002803 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
2804 {
2805 dev_type = BT_DEVICE_TYPE_BLE;
2806 }
2807 btif_dm_update_ble_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2808 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002809 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2810 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2811
2812 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2813 pairing_cb.is_ssp = FALSE;
2814 cod = COD_UNCLASSIFIED;
2815
2816 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2817 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2818 p_ssp_key_notif->passkey);
2819}
2820
2821/*******************************************************************************
2822**
2823** Function btif_dm_ble_auth_cmpl_evt
2824**
2825** Description Executes authentication complete event in btif context
2826**
2827** Returns void
2828**
2829*******************************************************************************/
2830static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2831{
2832 /* Save link key, if not temporary */
2833 bt_bdaddr_t bd_addr;
2834 bt_status_t status = BT_STATUS_FAIL;
2835 bt_bond_state_t state = BT_BOND_STATE_NONE;
2836
2837 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2838 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2839 {
2840 /* store keys */
2841 }
2842 if (p_auth_cmpl->success)
2843 {
2844 status = BT_STATUS_SUCCESS;
2845 state = BT_BOND_STATE_BONDED;
2846
2847 btif_dm_save_ble_bonding_keys();
2848 BTA_GATTC_Refresh(bd_addr.address);
2849 btif_dm_get_remote_services(&bd_addr);
2850 }
2851 else
2852 {
2853 /*Map the HCI fail reason to bt status */
2854 switch (p_auth_cmpl->fail_reason)
2855 {
Priti Aghera156c52b2014-07-09 14:58:19 -07002856 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
2857 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
2858 btif_dm_remove_ble_bonding_keys();
2859 status = BT_STATUS_AUTH_FAILURE;
2860 break;
2861 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
2862 status = BT_STATUS_AUTH_REJECTED;
2863 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002864 default:
Andre Eisenbachca22ac42013-02-13 17:02:11 +09002865 btif_dm_remove_ble_bonding_keys();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002866 status = BT_STATUS_FAIL;
2867 break;
2868 }
2869 }
2870 bond_state_changed(status, &bd_addr, state);
2871}
2872
2873
2874
2875void btif_dm_load_ble_local_keys(void)
2876{
2877 bt_status_t bt_status;
2878
2879 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2880
2881 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2882 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2883 {
2884 ble_local_key_cb.is_er_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002885 BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002886 }
2887
2888 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
2889 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
2890 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
2891 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
2892 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
2893 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
2894 {
2895 ble_local_key_cb.is_id_keys_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002896 BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002897 }
2898
2899}
2900void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
2901 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
2902{
2903 if (ble_local_key_cb.is_er_rcvd )
2904 {
2905 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2906 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2907 }
2908
2909 if (ble_local_key_cb.is_id_keys_rcvd)
2910 {
2911 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
2912 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
2913 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
2914 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
2915 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002916 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002917}
2918
2919void btif_dm_save_ble_bonding_keys(void)
2920{
2921
2922 bt_bdaddr_t bd_addr;
2923
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002924 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002925
2926 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2927
2928 if (pairing_cb.ble.is_penc_key_rcvd)
2929 {
2930 btif_storage_add_ble_bonding_key(&bd_addr,
2931 (char *) &pairing_cb.ble.penc_key,
2932 BTIF_DM_LE_KEY_PENC,
2933 sizeof(btif_dm_ble_penc_keys_t));
2934 }
2935
2936 if (pairing_cb.ble.is_pid_key_rcvd)
2937 {
2938 btif_storage_add_ble_bonding_key(&bd_addr,
Andre Eisenbach5e808462014-10-21 12:37:53 -07002939 (char *) &pairing_cb.ble.pid_key,
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002940 BTIF_DM_LE_KEY_PID,
Andre Eisenbach5e808462014-10-21 12:37:53 -07002941 sizeof(btif_dm_ble_pid_keys_t));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002942 }
2943
2944
2945 if (pairing_cb.ble.is_pcsrk_key_rcvd)
2946 {
2947 btif_storage_add_ble_bonding_key(&bd_addr,
2948 (char *) &pairing_cb.ble.pcsrk_key,
2949 BTIF_DM_LE_KEY_PCSRK,
2950 sizeof(btif_dm_ble_pcsrk_keys_t));
2951 }
2952
2953
2954 if (pairing_cb.ble.is_lenc_key_rcvd)
2955 {
2956 btif_storage_add_ble_bonding_key(&bd_addr,
2957 (char *) &pairing_cb.ble.lenc_key,
2958 BTIF_DM_LE_KEY_LENC,
2959 sizeof(btif_dm_ble_lenc_keys_t));
2960 }
2961
2962 if (pairing_cb.ble.is_lcsrk_key_rcvd)
2963 {
2964 btif_storage_add_ble_bonding_key(&bd_addr,
2965 (char *) &pairing_cb.ble.lcsrk_key,
2966 BTIF_DM_LE_KEY_LCSRK,
2967 sizeof(btif_dm_ble_lcsrk_keys_t));
2968 }
2969
2970}
2971
2972
2973void btif_dm_remove_ble_bonding_keys(void)
2974{
2975 bt_bdaddr_t bd_addr;
2976
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002977 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002978
2979 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2980 btif_storage_remove_ble_bonding_keys(&bd_addr);
2981}
2982
2983
2984/*******************************************************************************
2985**
2986** Function btif_dm_ble_sec_req_evt
2987**
2988** Description Eprocess security request event in btif context
2989**
2990** Returns void
2991**
2992*******************************************************************************/
2993void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
2994{
2995 bt_bdaddr_t bd_addr;
2996 bt_bdname_t bd_name;
2997 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08002998 int dev_type;
2999
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003000 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003001
3002 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3003 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003004 BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003005 return;
3006 }
3007
3008 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08003009 if (!btif_get_device_type(p_ble_req->bd_addr, &dev_type))
3010 {
3011 dev_type = BT_DEVICE_TYPE_BLE;
3012 }
3013 btif_dm_update_ble_remote_properties(p_ble_req->bd_addr, p_ble_req->bd_name,
3014 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003015
3016 bdcpy(bd_addr.address, p_ble_req->bd_addr);
3017 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
3018
3019 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3020
Andre Eisenbach89363762015-01-26 13:49:36 -08003021 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003022 pairing_cb.is_le_only = TRUE;
3023 pairing_cb.is_ssp = TRUE;
3024
3025 cod = COD_UNCLASSIFIED;
3026
3027 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
3028 BT_SSP_VARIANT_CONSENT, 0);
3029}
3030
3031
3032
3033/*******************************************************************************
3034**
3035** Function btif_dm_ble_passkey_req_evt
3036**
3037** Description Executes pin request event in btif context
3038**
3039** Returns void
3040**
3041*******************************************************************************/
3042static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
3043{
3044 bt_bdaddr_t bd_addr;
3045 bt_bdname_t bd_name;
3046 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08003047 int dev_type;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003048
3049 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08003050 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
3051 {
3052 dev_type = BT_DEVICE_TYPE_BLE;
3053 }
3054 btif_dm_update_ble_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,
3055 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003056
3057 bdcpy(bd_addr.address, p_pin_req->bd_addr);
3058 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
3059
3060 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3061 pairing_cb.is_le_only = TRUE;
3062
3063 cod = COD_UNCLASSIFIED;
3064
3065 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
3066 &bd_addr, &bd_name, cod);
3067}
3068
3069
3070void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
3071 tBT_DEVICE_TYPE dev_type)
3072{
3073 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
3074}
3075
3076static void btif_dm_ble_tx_test_cback(void *p)
3077{
3078 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
3079 (char *)p, 1, NULL);
3080}
3081
3082static void btif_dm_ble_rx_test_cback(void *p)
3083{
3084 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
3085 (char *)p, 1, NULL);
3086}
3087
3088static void btif_dm_ble_test_end_cback(void *p)
3089{
3090 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
3091 (char *)p, 3, NULL);
3092}
3093/*******************************************************************************
3094**
3095** Function btif_le_test_mode
3096**
3097** Description Sends a HCI BLE Test command to the Controller
3098**
3099** Returns BT_STATUS_SUCCESS on success
3100**
3101*******************************************************************************/
3102bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
3103{
3104 switch (opcode) {
3105 case HCI_BLE_TRANSMITTER_TEST:
3106 if (len != 3) return BT_STATUS_PARM_INVALID;
3107 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
3108 break;
3109 case HCI_BLE_RECEIVER_TEST:
3110 if (len != 1) return BT_STATUS_PARM_INVALID;
3111 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
3112 break;
3113 case HCI_BLE_TEST_END:
3114 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
3115 break;
3116 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003117 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003118 return BT_STATUS_UNSUPPORTED;
3119 }
3120 return BT_STATUS_SUCCESS;
3121}
3122
3123#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08003124
3125void btif_dm_on_disable()
3126{
3127 /* cancel any pending pairing requests */
3128 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3129 {
3130 bt_bdaddr_t bd_addr;
3131
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003132 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003133 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3134 btif_dm_cancel_bond(&bd_addr);
3135 }
3136}
Matthew Xie1e5109b2012-11-09 18:26:26 -08003137
Satya Callojie5ba8842014-07-03 17:18:02 -07003138/*******************************************************************************
3139**
3140** Function btif_dm_read_energy_info
3141**
3142** Description Reads the energy info from controller
3143**
3144** Returns void
3145**
3146*******************************************************************************/
3147void btif_dm_read_energy_info()
3148{
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003149#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07003150 BTA_DmBleGetEnergyInfo(bta_energy_info_cb);
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003151#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07003152}
3153
Matthew Xie1e5109b2012-11-09 18:26:26 -08003154static char* btif_get_default_local_name() {
3155 if (btif_default_local_name[0] == '\0')
3156 {
3157 int max_len = sizeof(btif_default_local_name) - 1;
3158 if (BTM_DEF_LOCAL_NAME[0] != '\0')
3159 {
3160 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
3161 }
3162 else
3163 {
3164 char prop_model[PROPERTY_VALUE_MAX];
3165 property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
3166 strncpy(btif_default_local_name, prop_model, max_len);
3167 }
3168 btif_default_local_name[max_len] = '\0';
3169 }
3170 return btif_default_local_name;
3171}