blob: 399410bf5a60f5ba0dbe33fd68de83e8962167e1 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
3 * Copyright (C) 2009-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19/************************************************************************************
20 *
21 * Filename: btif_dm.c
22 *
23 * Description: Contains Device Management (DM) related functionality
24 *
25 *
26 ***********************************************************************************/
Sharvil Nanavati44802762014-12-23 23:08:58 -080027
Chris Mantonf8027002015-03-12 09:22:48 -070028#define LOG_TAG "bt_btif_dm"
Sharvil Nanavati44802762014-12-23 23:08:58 -080029
The Android Open Source Project5738f832012-12-12 16:00:35 -080030#include <stdio.h>
31#include <stdlib.h>
32#include <unistd.h>
33
34#include <hardware/bluetooth.h>
35
The Android Open Source Project5738f832012-12-12 16:00:35 -080036#include <cutils/properties.h>
37#include "gki.h"
38#include "btu.h"
Sharvil Nanavati95b74f22015-03-12 15:55:21 -070039#include "btcore/include/bdaddr.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080040#include "bta_api.h"
41#include "btif_api.h"
42#include "btif_util.h"
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080043#include "btif_dm.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080044#include "btif_storage.h"
45#include "btif_hh.h"
46#include "btif_config.h"
47
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080048#include "bta_gatt_api.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080049#include "include/stack_config.h"
Chris Mantonf8027002015-03-12 09:22:48 -070050
Sharvil Nanavati44802762014-12-23 23:08:58 -080051#include "osi/include/log.h"
Andre Eisenbach31a64002014-10-14 14:29:19 -070052
53/******************************************************************************
54** Device specific workarounds
55******************************************************************************/
56
57/**
58 * The devices below have proven problematic during the pairing process, often
59 * requiring multiple retries to complete pairing. To avoid degrading the user
60 * experience for other devices, explicitely blacklist troubled devices here.
61 */
62static const UINT8 blacklist_pairing_retries[][3] = {
63 {0x9C, 0xDF, 0x03} // BMW car kits (Harman/Becker)
64};
65
66BOOLEAN blacklistPairingRetries(BD_ADDR bd_addr)
67{
68 const unsigned blacklist_size = sizeof(blacklist_pairing_retries)
69 / sizeof(blacklist_pairing_retries[0]);
70 for (unsigned i = 0; i != blacklist_size; ++i)
71 {
72 if (blacklist_pairing_retries[i][0] == bd_addr[0] &&
73 blacklist_pairing_retries[i][1] == bd_addr[1] &&
74 blacklist_pairing_retries[i][2] == bd_addr[2])
75 return TRUE;
76 }
77 return FALSE;
78}
79
The Android Open Source Project5738f832012-12-12 16:00:35 -080080/******************************************************************************
81** Constants & Macros
82******************************************************************************/
83
84#define COD_UNCLASSIFIED ((0x1F) << 8)
Priti Agheraebb1d752012-11-27 18:03:22 -080085#define COD_HID_KEYBOARD 0x0540
86#define COD_HID_POINTING 0x0580
87#define COD_HID_COMBO 0x05C0
88#define COD_HID_MAJOR 0x0500
89#define COD_AV_HEADSETS 0x0404
90#define COD_AV_HANDSFREE 0x0408
91#define COD_AV_HEADPHONES 0x0418
92#define COD_AV_PORTABLE_AUDIO 0x041C
93#define COD_AV_HIFI_AUDIO 0x0428
The Android Open Source Project5738f832012-12-12 16:00:35 -080094
95
96#define BTIF_DM_DEFAULT_INQ_MAX_RESULTS 0
97#define BTIF_DM_DEFAULT_INQ_MAX_DURATION 10
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -070098#define BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING 2
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080099
Andre Eisenbach31a64002014-10-14 14:29:19 -0700100#define NUM_TIMEOUT_RETRIES 5
101
Matthew Xie1e5109b2012-11-09 18:26:26 -0800102#define PROPERTY_PRODUCT_MODEL "ro.product.model"
Matthew Xiea30d95a2013-09-18 12:30:36 -0700103#define DEFAULT_LOCAL_NAME_MAX 31
Matthew Xie1e5109b2012-11-09 18:26:26 -0800104#if (DEFAULT_LOCAL_NAME_MAX > BTM_MAX_LOC_BD_NAME_LEN)
105 #error "default btif local name size exceeds stack supported length"
106#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800107
Matthew Xie7f3e4292013-09-30 12:44:10 -0700108#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
109#define BTIF_DM_INTERLEAVE_DURATION_BR_ONE 2
110#define BTIF_DM_INTERLEAVE_DURATION_LE_ONE 2
111#define BTIF_DM_INTERLEAVE_DURATION_BR_TWO 3
112#define BTIF_DM_INTERLEAVE_DURATION_LE_TWO 4
113#endif
114
Priti Agherac0edf9f2014-06-26 11:23:51 -0700115#define MAX_SDP_BL_ENTRIES 3
116
Andre Eisenbach89363762015-01-26 13:49:36 -0800117#define BOND_TYPE_UNKNOWN 0
118#define BOND_TYPE_PERSISTENT 1
119#define BOND_TYPE_TEMPORARY 2
120
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800121#define ENCRYPTED_BREDR 2
122#define ENCRYPTED_LE 4
123
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800124typedef struct
125{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800126 bt_bond_state_t state;
127 BD_ADDR bd_addr;
Andre Eisenbach89363762015-01-26 13:49:36 -0800128 UINT8 bond_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800129 UINT8 pin_code_len;
130 UINT8 is_ssp;
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -0700131 UINT8 auth_req;
132 UINT8 io_cap;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800133 UINT8 autopair_attempts;
Andre Eisenbach31a64002014-10-14 14:29:19 -0700134 UINT8 timeout_retries;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800135 UINT8 is_local_initiated;
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700136 UINT8 sdp_attempts;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800137#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
138 BOOLEAN is_le_only;
139 btif_dm_ble_cb_t ble;
140#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800141} btif_dm_pairing_cb_t;
142
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800143
144typedef struct
145{
146 UINT8 ir[BT_OCTET16_LEN];
147 UINT8 irk[BT_OCTET16_LEN];
148 UINT8 dhk[BT_OCTET16_LEN];
149}btif_dm_local_key_id_t;
150
151typedef struct
152{
153 BOOLEAN is_er_rcvd;
154 UINT8 er[BT_OCTET16_LEN];
155 BOOLEAN is_id_keys_rcvd;
156 btif_dm_local_key_id_t id_keys; /* ID kyes */
157
158}btif_dm_local_key_cb_t;
159
160typedef struct
161{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800162 BD_ADDR bd_addr;
163 BD_NAME bd_name;
164} btif_dm_remote_name_t;
165
166typedef struct
167{
168 BT_OCTET16 sp_c;
169 BT_OCTET16 sp_r;
170 BD_ADDR oob_bdaddr; /* peer bdaddr*/
171} btif_dm_oob_cb_t;
Satya Callojie5ba8842014-07-03 17:18:02 -0700172
173typedef struct
174{
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700175 bt_bdaddr_t bdaddr;
176 UINT8 transport; /* 0=Unknown, 1=BR/EDR, 2=LE */
177} btif_dm_create_bond_cb_t;
178
179typedef struct
180{
Satya Callojie5ba8842014-07-03 17:18:02 -0700181 uint8_t status;
182 uint8_t ctrl_state;
183 uint64_t tx_time;
184 uint64_t rx_time;
185 uint64_t idle_time;
186 uint64_t energy_used;
187} btif_activity_energy_info_cb_t;
188
Priti Agherac0edf9f2014-06-26 11:23:51 -0700189typedef struct
190{
191 unsigned int manufact_id;
192}skip_sdp_entry_t;
193
The Android Open Source Project5738f832012-12-12 16:00:35 -0800194#define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id))
195
Priti Agherac0edf9f2014-06-26 11:23:51 -0700196#define MAX_SDP_BL_ENTRIES 3
197#define UUID_HUMAN_INTERFACE_DEVICE "00001124-0000-1000-8000-00805f9b34fb"
198
199static skip_sdp_entry_t sdp_blacklist[] = {{76}}; //Apple Mouse and Keyboard
200
201
The Android Open Source Project5738f832012-12-12 16:00:35 -0800202/* This flag will be true if HCI_Inquiry is in progress */
203static BOOLEAN btif_dm_inquiry_in_progress = FALSE;
204
Priti Agherac0edf9f2014-06-26 11:23:51 -0700205
206
Matthew Xie1e5109b2012-11-09 18:26:26 -0800207/************************************************************************************
208** Static variables
209************************************************************************************/
210static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'};
211
The Android Open Source Project5738f832012-12-12 16:00:35 -0800212/******************************************************************************
213** Static functions
214******************************************************************************/
215static btif_dm_pairing_cb_t pairing_cb;
216static btif_dm_oob_cb_t oob_cb;
217static void btif_dm_generic_evt(UINT16 event, char* p_param);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700218static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800219static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name);
220static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
221 DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800222#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
223static btif_dm_local_key_cb_t ble_local_key_cb;
224static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif);
225static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl);
226static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req);
227#endif
Satya Calloji6e2d9db2014-07-08 16:18:58 -0700228
229static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
230 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
231 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status);
232
Matthew Xie1e5109b2012-11-09 18:26:26 -0800233static char* btif_get_default_local_name();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800234/******************************************************************************
235** Externs
236******************************************************************************/
237extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
238extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
239extern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
240extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
Hemant Gupta10256872013-08-19 18:33:01 +0530241extern bt_status_t btif_hf_client_execute_service(BOOLEAN b_enable);
Hemant Gupta2dc99992014-04-18 12:54:08 +0530242extern bt_status_t btif_mce_execute_service(BOOLEAN b_enable);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800243extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700244extern 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 -0800245
246
247/******************************************************************************
248** Functions
249******************************************************************************/
250
251bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
252 BOOLEAN b_enable)
253{
254 /* Check the service_ID and invoke the profile's BT state changed API */
255 switch (service_id)
256 {
257 case BTA_HFP_SERVICE_ID:
258 case BTA_HSP_SERVICE_ID:
259 {
260 btif_hf_execute_service(b_enable);
261 }break;
Sharvil Nanavati9609cee2014-10-15 18:30:49 -0700262 case BTA_A2DP_SOURCE_SERVICE_ID:
The Android Open Source Project5738f832012-12-12 16:00:35 -0800263 {
264 btif_av_execute_service(b_enable);
265 }break;
266 case BTA_HID_SERVICE_ID:
267 {
268 btif_hh_execute_service(b_enable);
269 }break;
Hemant Gupta10256872013-08-19 18:33:01 +0530270 case BTA_HFP_HS_SERVICE_ID:
271 {
272 btif_hf_client_execute_service(b_enable);
273 }break;
Hemant Gupta2dc99992014-04-18 12:54:08 +0530274 case BTA_MAP_SERVICE_ID:
275 {
276 btif_mce_execute_service(b_enable);
277 }break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800278 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700279 BTIF_TRACE_ERROR("%s: Unknown service being enabled", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800280 return BT_STATUS_FAIL;
281 }
282 return BT_STATUS_SUCCESS;
283}
284
285/*******************************************************************************
286**
287** Function check_eir_remote_name
288**
289** Description Check if remote name is in the EIR data
290**
291** Returns TRUE if remote name found
292** Populate p_remote_name, if provided and remote name found
293**
294*******************************************************************************/
295static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
296 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
297{
298 UINT8 *p_eir_remote_name = NULL;
299 UINT8 remote_name_len = 0;
300
301 /* Check EIR for remote name and services */
302 if (p_search_data->inq_res.p_eir)
303 {
Zach Johnsona50fc882014-10-30 21:02:58 -0700304 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800305 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
306 if (!p_eir_remote_name)
307 {
Zach Johnsona50fc882014-10-30 21:02:58 -0700308 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800309 BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
310 }
311
312 if (p_eir_remote_name)
313 {
314 if (remote_name_len > BD_NAME_LEN)
315 remote_name_len = BD_NAME_LEN;
316
317 if (p_remote_name && p_remote_name_len)
318 {
319 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
320 *(p_remote_name + remote_name_len) = 0;
321 *p_remote_name_len = remote_name_len;
322 }
323
324 return TRUE;
325 }
326 }
327
328 return FALSE;
329
330}
331
332/*******************************************************************************
333**
334** Function check_cached_remote_name
335**
336** Description Check if remote name is in the NVRAM cache
337**
338** Returns TRUE if remote name found
339** Populate p_remote_name, if provided and remote name found
340**
341*******************************************************************************/
342static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
343 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
344{
345 bt_bdname_t bdname;
346 bt_bdaddr_t remote_bdaddr;
347 bt_property_t prop_name;
348
349 /* check if we already have it in our btif_storage cache */
350 bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
351 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
352 sizeof(bt_bdname_t), &bdname);
353 if (btif_storage_get_remote_device_property(
354 &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
355 {
356 if (p_remote_name && p_remote_name_len)
357 {
358 strcpy((char *)p_remote_name, (char *)bdname.name);
359 *p_remote_name_len = strlen((char *)p_remote_name);
360 }
361 return TRUE;
362 }
363
364 return FALSE;
365}
366
367BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
368{
369 uint32_t remote_cod;
370 bt_property_t prop_name;
371
372 /* check if we already have it in our btif_storage cache */
373 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
374 sizeof(uint32_t), &remote_cod);
375 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
376 {
Chris Mantonf8027002015-03-12 09:22:48 -0700377 LOG_INFO("%s remote_cod = 0x%08x cod = 0x%08x", __func__, remote_cod, cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800378 if ((remote_cod & 0x7ff) == cod)
379 return TRUE;
380 }
381
382 return FALSE;
383}
384
Priti Agheraebb1d752012-11-27 18:03:22 -0800385BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
386{
387 uint32_t remote_cod;
388 bt_property_t prop_name;
389
390 /* check if we already have it in our btif_storage cache */
391 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
392 sizeof(uint32_t), &remote_cod);
393 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
394 &prop_name) == BT_STATUS_SUCCESS)
395 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700396 BTIF_TRACE_DEBUG("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
Priti Agheraebb1d752012-11-27 18:03:22 -0800397 if ((remote_cod & 0x700) == cod)
398 return TRUE;
399 }
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700400 return FALSE;
401}
Priti Agheraebb1d752012-11-27 18:03:22 -0800402
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700403BOOLEAN check_hid_le(const bt_bdaddr_t *remote_bdaddr)
404{
405 uint32_t remote_dev_type;
406 bt_property_t prop_name;
407
408 /* check if we already have it in our btif_storage cache */
409 BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE,
410 sizeof(uint32_t), &remote_dev_type);
411 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
412 &prop_name) == BT_STATUS_SUCCESS)
413 {
414 if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE)
415 {
416 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -0700417 bdaddr_to_string(remote_bdaddr, bdstr, sizeof(bdstr));
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700418 if(btif_config_exist(bdstr, "HidAppId"))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700419 return TRUE;
420 }
421 }
Priti Agheraebb1d752012-11-27 18:03:22 -0800422 return FALSE;
423}
424
Priti Agherac0edf9f2014-06-26 11:23:51 -0700425/*****************************************************************************
426**
427** Function check_sdp_bl
428**
429** Description Checks if a given device is blacklisted to skip sdp
430**
431** Parameters skip_sdp_entry
432**
433** Returns TRUE if the device is present in blacklist, else FALSE
434**
435*******************************************************************************/
436BOOLEAN check_sdp_bl(const bt_bdaddr_t *remote_bdaddr)
437{
438 UINT8 i = 0;
439 UINT16 manufacturer = 0;
440 UINT8 lmp_ver = 0;
441 UINT16 lmp_subver = 0;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700442 bt_property_t prop_name;
443 bt_remote_version_t info;
444 bt_status_t status;
445
446
447 if (remote_bdaddr == NULL)
448 return FALSE;
449
450/* fetch additional info about remote device used in iop query */
Sharvil Nanavatif1c764f2015-02-23 17:31:48 -0800451 BTM_ReadRemoteVersion(*(BD_ADDR*)remote_bdaddr, &lmp_ver,
Priti Agherac0edf9f2014-06-26 11:23:51 -0700452 &manufacturer, &lmp_subver);
453
454
455
456 /* if not available yet, try fetching from config database */
457 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO,
458 sizeof(bt_remote_version_t), &info);
459
460 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
461 &prop_name) != BT_STATUS_SUCCESS)
462 {
463
464 return FALSE;
465 }
466 manufacturer = info.manufacturer;
467
468 for (int i = 0; i < MAX_SDP_BL_ENTRIES; i++)
469 {
470 if (manufacturer == sdp_blacklist[i].manufact_id)
471 return TRUE;
472 }
473 return FALSE;
474}
475
476
The Android Open Source Project5738f832012-12-12 16:00:35 -0800477static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
478{
479 /* Send bonding state only once - based on outgoing/incoming we may receive duplicates */
480 if ( (pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING) )
481 return;
482
Andre Eisenbach89363762015-01-26 13:49:36 -0800483 if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800484 {
485 state = BT_BOND_STATE_NONE;
486 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700487 BTIF_TRACE_DEBUG("%s: state=%d prev_state=%d", __FUNCTION__, state, pairing_cb.state);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800488
489 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
490
491 if (state == BT_BOND_STATE_BONDING)
492 {
493 pairing_cb.state = state;
494 bdcpy(pairing_cb.bd_addr, bd_addr->address);
495 }
496 else
497 {
498 memset(&pairing_cb, 0, sizeof(pairing_cb));
499 }
500
501}
502
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800503/* store remote version in bt config to always have access
504 to it post pairing*/
505static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
506{
507 bt_property_t property;
508 UINT8 lmp_ver = 0;
509 UINT16 lmp_subver = 0;
510 UINT16 mfct_set = 0;
511 tBTM_STATUS btm_status;
512 bt_remote_version_t info;
513 bt_status_t status;
514 bdstr_t bdstr;
515
516 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
517 &mfct_set, &lmp_subver);
518
Sharvil Nanavati44802762014-12-23 23:08:58 -0800519 LOG_DEBUG("remote version info [%s]: %x, %x, %x", bdaddr_to_string(p_bd, bdstr, sizeof(bdstr)),
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800520 lmp_ver, mfct_set, lmp_subver);
521
522 if (btm_status == BTM_SUCCESS)
523 {
524 /* always update cache to ensure we have availability whenever BTM API
525 is not populated */
526 info.manufacturer = mfct_set;
527 info.sub_ver = lmp_subver;
528 info.version = lmp_ver;
529 BTIF_STORAGE_FILL_PROPERTY(&property,
530 BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
531 &info);
532 status = btif_storage_set_remote_device_property(p_bd, &property);
533 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
534 }
535}
536
The Android Open Source Project5738f832012-12-12 16:00:35 -0800537
538static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
539 DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
540{
541 int num_properties = 0;
542 bt_property_t properties[3];
543 bt_bdaddr_t bdaddr;
544 bt_status_t status;
545 UINT32 cod;
546 bt_device_type_t dev_type;
547
548 memset(properties, 0, sizeof(properties));
549 bdcpy(bdaddr.address, bd_addr);
550
551 /* remote name */
552 if (strlen((const char *) bd_name))
553 {
554 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
555 BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
556 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
557 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
558 num_properties++;
559 }
560
561 /* class of device */
562 cod = devclass2uint(dev_class);
Chris Mantonf8027002015-03-12 09:22:48 -0700563 BTIF_TRACE_DEBUG("%s cod is 0x%06x", __func__, cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800564 if ( cod == 0) {
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530565 /* Try to retrieve cod from storage */
Chris Mantonf8027002015-03-12 09:22:48 -0700566 BTIF_TRACE_DEBUG("%s cod is 0, checking cod from storage", __func__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530567 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
568 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
569 status = btif_storage_get_remote_device_property(&bdaddr, &properties[num_properties]);
Chris Mantonf8027002015-03-12 09:22:48 -0700570 BTIF_TRACE_DEBUG("%s cod retrieved from storage is 0x%06x", __func__, cod);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530571 if ( cod == 0) {
Chris Mantonf8027002015-03-12 09:22:48 -0700572 BTIF_TRACE_DEBUG("%s cod is again 0, set as unclassified", __func__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530573 cod = COD_UNCLASSIFIED;
574 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800575 }
576
577 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
578 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
579 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
580 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
581 num_properties++;
582
583 /* device type */
584 dev_type = device_type;
585 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
586 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
587 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
588 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
589 num_properties++;
590
591 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
592 status, &bdaddr, num_properties, properties);
593}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800594
595/*******************************************************************************
596**
597** Function btif_dm_cb_hid_remote_name
598**
599** Description Remote name callback for HID device. Called in btif context
600** Special handling for HID devices
601**
602** Returns void
603**
604*******************************************************************************/
605static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
606{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700607 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 -0800608 if (pairing_cb.state == BT_BOND_STATE_BONDING)
609 {
610 bt_bdaddr_t remote_bd;
611
612 bdcpy(remote_bd.address, pairing_cb.bd_addr);
613
614 if (p_remote_name->status == BTM_SUCCESS)
615 {
616 bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
617 }
618 else
619 bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
620 }
621}
622
The Android Open Source Project5738f832012-12-12 16:00:35 -0800623/*******************************************************************************
624**
625** Function btif_dm_cb_create_bond
626**
627** Description Create bond initiated from the BTIF thread context
628** Special handling for HID devices
629**
630** Returns void
631**
632*******************************************************************************/
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700633static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800634{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800635 BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800636 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800637
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800638#if BLE_INCLUDED == TRUE
639 int device_type;
640 int addr_type;
641 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -0700642 bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr));
Matthew Xie64c54792014-09-16 00:55:03 -0700643 if (transport == BT_TRANSPORT_LE)
644 {
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700645 if (!btif_config_get_int((char const *)&bdstr,"DevType", &device_type))
Matthew Xie64c54792014-09-16 00:55:03 -0700646 {
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700647 btif_config_set_int(bdstr, "DevType", BT_DEVICE_TYPE_BLE);
Matthew Xie64c54792014-09-16 00:55:03 -0700648 }
649 if (btif_storage_get_remote_addr_type(bd_addr, &addr_type) != BT_STATUS_SUCCESS)
650 {
651 btif_storage_set_remote_addr_type(bd_addr, BLE_ADDR_PUBLIC);
652 }
653 }
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700654 if((btif_config_get_int((char const *)&bdstr,"DevType", &device_type) &&
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800655 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
lungtsai_lin3baff792014-08-29 13:47:47 +0800656 (device_type == BT_DEVICE_TYPE_BLE)) || (transport == BT_TRANSPORT_LE))
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800657 {
658 BTA_DmAddBleDevice(bd_addr->address, addr_type, BT_DEVICE_TYPE_BLE);
659 }
660#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800661
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800662#if BLE_INCLUDED == TRUE
663 if(is_hid && device_type != BT_DEVICE_TYPE_BLE)
664#else
665 if(is_hid)
666#endif
667 {
668 int status;
669 status = btif_hh_connect(bd_addr);
670 if(status != BT_STATUS_SUCCESS)
671 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800672 }
673 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800674 {
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700675 BTA_DmBondByTransport((UINT8 *)bd_addr->address, transport);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800676 }
677 /* Track originator of bond creation */
678 pairing_cb.is_local_initiated = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800679
680}
681
682/*******************************************************************************
683**
684** Function btif_dm_cb_remove_bond
685**
686** Description remove bond initiated from the BTIF thread context
687** Special handling for HID devices
688**
689** Returns void
690**
691*******************************************************************************/
692void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
693{
694 bdstr_t bdstr;
695 /*special handling for HID devices */
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700696 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there
697 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
698#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
699 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
700#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800701 {
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700702 BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800703 }
704}
705
706/*******************************************************************************
707**
Andre Eisenbach249f6002014-06-18 12:20:37 -0700708** Function btif_dm_get_connection_state
709**
710** Description Returns whether the remote device is currently connected
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800711** and whether encryption is active for the connection
Andre Eisenbach249f6002014-06-18 12:20:37 -0700712**
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800713** Returns 0 if not connected; 1 if connected and > 1 if connection is
714** encrypted
Andre Eisenbach249f6002014-06-18 12:20:37 -0700715**
716*******************************************************************************/
717uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr)
718{
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800719 uint8_t *bda = (uint8_t*)bd_addr->address;
720 uint16_t rc = BTA_DmGetConnectionState(bda);
721
722 if (rc != 0)
723 {
724 uint8_t flags = 0;
725
726 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_BR_EDR);
727 BTIF_TRACE_DEBUG("%s: security flags (BR/EDR)=0x%02x", __FUNCTION__, flags);
728 if (flags & BTM_SEC_FLAG_ENCRYPTED)
729 rc |= ENCRYPTED_BREDR;
730
731 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_LE);
732 BTIF_TRACE_DEBUG("%s: security flags (LE)=0x%02x", __FUNCTION__, flags);
733 if (flags & BTM_SEC_FLAG_ENCRYPTED)
734 rc |= ENCRYPTED_LE;
735 }
736
737 return rc;
Andre Eisenbach249f6002014-06-18 12:20:37 -0700738}
739
740/*******************************************************************************
741**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800742** Function search_devices_copy_cb
743**
744** Description Deep copy callback for search devices event
745**
746** Returns void
747**
748*******************************************************************************/
749static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
750{
751 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
752 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
753
754 if (!p_src)
755 return;
756
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700757 BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800758 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
759 switch (event)
760 {
761 case BTA_DM_INQ_RES_EVT:
762 {
763 if (p_src_data->inq_res.p_eir)
764 {
765 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
766 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
767 }
768 }
769 break;
770
771 case BTA_DM_DISC_RES_EVT:
772 {
773 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
774 {
775 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
776 memcpy(p_dest_data->disc_res.p_raw_data,
777 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
778 }
779 }
780 break;
781 }
782}
783
784static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
785{
786 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
787 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
788
789 if (!p_src)
790 return;
791 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
792 switch (event)
793 {
794 case BTA_DM_DISC_RES_EVT:
795 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530796 if (p_src_data->disc_res.result == BTA_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800797 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530798 if (p_src_data->disc_res.num_uuids > 0)
799 {
800 p_dest_data->disc_res.p_uuid_list =
801 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
802 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
803 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
804 GKI_freebuf(p_src_data->disc_res.p_uuid_list);
805 }
806 if (p_src_data->disc_res.p_raw_data != NULL)
807 {
808 GKI_freebuf(p_src_data->disc_res.p_raw_data);
809 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800810 }
811 } break;
812 }
813}
814/******************************************************************************
815**
816** BTIF DM callback events
817**
818*****************************************************************************/
819
820/*******************************************************************************
821**
822** Function btif_dm_pin_req_evt
823**
824** Description Executes pin request event in btif context
825**
826** Returns void
827**
828*******************************************************************************/
829static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
830{
831 bt_bdaddr_t bd_addr;
832 bt_bdname_t bd_name;
833 UINT32 cod;
834 bt_pin_code_t pin_code;
Matthew Xie86f97ed2014-11-10 10:24:46 -0800835 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800836
837 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -0800838 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
839 {
840 dev_type = BT_DEVICE_TYPE_BREDR;
841 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800842 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -0800843 p_pin_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800844
845 bdcpy(bd_addr.address, p_pin_req->bd_addr);
846 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
847
848 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
849
850 cod = devclass2uint(p_pin_req->dev_class);
851
Chris Mantonf8027002015-03-12 09:22:48 -0700852 if (cod == 0) {
853 BTIF_TRACE_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800854 cod = COD_UNCLASSIFIED;
855 }
856
857 /* check for auto pair possiblity only if bond was initiated by local device */
858 if (pairing_cb.is_local_initiated)
859 {
860 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
861 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
862 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
863 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
864 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
865 check_cod(&bd_addr, COD_HID_POINTING))
866 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700867 BTIF_TRACE_DEBUG("%s()cod matches for auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800868 /* Check if this device can be auto paired */
869 if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) &&
870 (pairing_cb.autopair_attempts == 0))
871 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700872 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800873 pin_code.pin[0] = 0x30;
874 pin_code.pin[1] = 0x30;
875 pin_code.pin[2] = 0x30;
876 pin_code.pin[3] = 0x30;
877
878 pairing_cb.autopair_attempts++;
879 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
880 return;
881 }
882 }
883 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
884 check_cod(&bd_addr, COD_HID_COMBO))
885 {
886 if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) &&
887 (pairing_cb.autopair_attempts == 0))
888 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700889 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800890 pin_code.pin[0] = 0x30;
891 pin_code.pin[1] = 0x30;
892 pin_code.pin[2] = 0x30;
893 pin_code.pin[3] = 0x30;
894
895 pairing_cb.autopair_attempts++;
896 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
897 return;
898 }
899 }
900 }
901 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
902 &bd_addr, &bd_name, cod);
903}
904
905/*******************************************************************************
906**
907** Function btif_dm_ssp_cfm_req_evt
908**
909** Description Executes SSP confirm request event in btif context
910**
911** Returns void
912**
913*******************************************************************************/
914static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
915{
916 bt_bdaddr_t bd_addr;
917 bt_bdname_t bd_name;
918 UINT32 cod;
919 BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
Matthew Xie86f97ed2014-11-10 10:24:46 -0800920 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800921
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700922 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800923
924 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -0800925 if (!btif_get_device_type(p_ssp_cfm_req->bd_addr, &dev_type))
926 {
927 dev_type = BT_DEVICE_TYPE_BREDR;
928 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800929 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -0800930 p_ssp_cfm_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800931
932 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
933 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
934
935 /* Set the pairing_cb based on the local & remote authentication requirements */
936 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
937
938 /* if just_works and bonding bit is not set treat this as temporary */
939 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 -0800940 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
941 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
Andre Eisenbach89363762015-01-26 13:49:36 -0800942 pairing_cb.bond_type = BOND_TYPE_TEMPORARY;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800943 else
Andre Eisenbach89363762015-01-26 13:49:36 -0800944 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800945
946 pairing_cb.is_ssp = TRUE;
947
948 /* If JustWorks auto-accept */
949 if (p_ssp_cfm_req->just_works)
950 {
951 /* Pairing consent for JustWorks needed if:
952 * 1. Incoming pairing is detected AND
953 * 2. local IO capabilities are DisplayYesNo AND
954 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
955 */
956 if ((is_incoming) && ((p_ssp_cfm_req->loc_io_caps == 0x01) &&
957 (p_ssp_cfm_req->rmt_io_caps == 0x00 || p_ssp_cfm_req->rmt_io_caps == 0x03)))
958 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700959 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 -0800960 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
961 }
962 else
963 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700964 BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800965 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
966 return;
967 }
968 }
969
970 cod = devclass2uint(p_ssp_cfm_req->dev_class);
971
Chris Mantonf8027002015-03-12 09:22:48 -0700972 if (cod == 0) {
973 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800974 cod = COD_UNCLASSIFIED;
975 }
976
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700977 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800978 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
979 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
980 p_ssp_cfm_req->num_val);
981}
982
983static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
984{
985 bt_bdaddr_t bd_addr;
986 bt_bdname_t bd_name;
987 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -0800988 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800989
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700990 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800991
992 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -0800993 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
994 {
995 dev_type = BT_DEVICE_TYPE_BREDR;
996 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800997 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -0800998 p_ssp_key_notif->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800999
1000 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
1001 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
1002
1003 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1004 pairing_cb.is_ssp = TRUE;
1005 cod = devclass2uint(p_ssp_key_notif->dev_class);
1006
Chris Mantonf8027002015-03-12 09:22:48 -07001007 if (cod == 0) {
1008 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001009 cod = COD_UNCLASSIFIED;
1010 }
1011
1012 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
1013 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
1014 p_ssp_key_notif->passkey);
1015}
1016/*******************************************************************************
1017**
1018** Function btif_dm_auth_cmpl_evt
1019**
1020** Description Executes authentication complete event in btif context
1021**
1022** Returns void
1023**
1024*******************************************************************************/
1025static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
1026{
1027 /* Save link key, if not temporary */
1028 bt_bdaddr_t bd_addr;
1029 bt_status_t status = BT_STATUS_FAIL;
1030 bt_bond_state_t state = BT_BOND_STATE_NONE;
Priti Agherac0edf9f2014-06-26 11:23:51 -07001031 BOOLEAN skip_sdp = FALSE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001032
1033 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
1034 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
1035 {
1036 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 -08001037 (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 -08001038 {
1039 bt_status_t ret;
Andre Eisenbach89363762015-01-26 13:49:36 -08001040 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, bond_type=%d",
1041 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001042 ret = btif_storage_add_bonded_device(&bd_addr,
1043 p_auth_cmpl->key, p_auth_cmpl->key_type,
1044 pairing_cb.pin_code_len);
1045 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
1046 }
1047 else
1048 {
Andre Eisenbach89363762015-01-26 13:49:36 -08001049 BTIF_TRACE_DEBUG("%s: Temporary key. Not storing. key_type=0x%x, bond_type=%d",
1050 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1051 if(pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
Hemant Guptab820aec2013-12-24 19:59:57 +05301052 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001053 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
Hemant Guptab820aec2013-12-24 19:59:57 +05301054 __FUNCTION__);
1055 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1056 return;
1057 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001058 }
1059 }
Priti Agherac0edf9f2014-06-26 11:23:51 -07001060
1061 // Skip SDP for certain HID Devices
The Android Open Source Project5738f832012-12-12 16:00:35 -08001062 if (p_auth_cmpl->success)
1063 {
Andre Eisenbach31a64002014-10-14 14:29:19 -07001064 pairing_cb.timeout_retries = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001065 status = BT_STATUS_SUCCESS;
1066 state = BT_BOND_STATE_BONDED;
Priti Agherac0edf9f2014-06-26 11:23:51 -07001067 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001068
Priti Agherac0edf9f2014-06-26 11:23:51 -07001069 if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr, COD_HID_MAJOR))
1070 {
Sharvil Nanavati44802762014-12-23 23:08:58 -08001071 LOG_WARN("%s:skip SDP", __FUNCTION__);
Priti Agherac0edf9f2014-06-26 11:23:51 -07001072 skip_sdp = TRUE;
1073 }
1074 if(!pairing_cb.is_local_initiated && skip_sdp)
1075 {
1076 bond_state_changed(status, &bd_addr, state);
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -08001077
Sharvil Nanavati44802762014-12-23 23:08:58 -08001078 LOG_WARN("%s: Incoming HID Connection",__FUNCTION__);
Priti Agherac0edf9f2014-06-26 11:23:51 -07001079 bt_property_t prop;
1080 bt_bdaddr_t bd_addr;
1081 bt_uuid_t uuid;
1082 char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE;
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -08001083
Priti Agherac0edf9f2014-06-26 11:23:51 -07001084 string_to_uuid(uuid_str, &uuid);
1085
1086 prop.type = BT_PROPERTY_UUIDS;
1087 prop.val = uuid.uu;
1088 prop.len = MAX_UUID_SIZE;
1089
1090 /* Send the event to the BTIF */
1091 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1092 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1093 }
1094 else
1095 {
1096 /* Trigger SDP on the device */
1097 pairing_cb.sdp_attempts = 1;;
1098
1099 if(btif_dm_inquiry_in_progress)
1100 btif_dm_cancel_discovery();
1101
1102 btif_dm_get_remote_services(&bd_addr);
1103 }
1104 /* 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 -08001105 }
1106 else
1107 {
1108 /*Map the HCI fail reason to bt status */
1109 switch(p_auth_cmpl->fail_reason)
1110 {
1111 case HCI_ERR_PAGE_TIMEOUT:
Andre Eisenbach31a64002014-10-14 14:29:19 -07001112 if (blacklistPairingRetries(bd_addr.address) && pairing_cb.timeout_retries)
1113 {
1114 BTIF_TRACE_WARNING("%s() - Pairing timeout; retrying (%d) ...", __FUNCTION__, pairing_cb.timeout_retries);
1115 --pairing_cb.timeout_retries;
1116 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
1117 return;
1118 }
1119 /* Fall-through */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001120 case HCI_ERR_CONNECTION_TOUT:
1121 status = BT_STATUS_RMT_DEV_DOWN;
1122 break;
1123
Hemant Guptaaef7a672013-07-31 19:00:12 +05301124 case HCI_ERR_PAIRING_NOT_ALLOWED:
1125 status = BT_STATUS_AUTH_REJECTED;
1126 break;
1127
Hemant Guptab4801442014-01-07 18:11:15 +05301128 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
1129 status = BT_STATUS_AUTH_FAILURE;
1130 break;
1131
The Android Open Source Project5738f832012-12-12 16:00:35 -08001132 /* map the auth failure codes, so we can retry pairing if necessary */
1133 case HCI_ERR_AUTH_FAILURE:
Hemant Gupta59a88ec2014-03-19 19:01:35 +05301134 case HCI_ERR_KEY_MISSING:
Zhihai Xua7ea8092013-11-27 14:10:53 +05301135 btif_storage_remove_bonded_device(&bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001136 case HCI_ERR_HOST_REJECT_SECURITY:
1137 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
1138 case HCI_ERR_UNIT_KEY_USED:
1139 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
1140 case HCI_ERR_INSUFFCIENT_SECURITY:
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301141 case HCI_ERR_PEER_USER:
1142 case HCI_ERR_UNSPECIFIED:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001143 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d",
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301144 __FUNCTION__, p_auth_cmpl->fail_reason);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001145 if (pairing_cb.autopair_attempts == 1)
1146 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001147 BTIF_TRACE_DEBUG("%s(): Adding device to blacklist ", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001148
1149 /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class */
1150 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
1151 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
1152 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
1153 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
1154 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
1155 check_cod(&bd_addr, COD_HID_POINTING))
1156 {
1157 btif_storage_add_device_to_autopair_blacklist (&bd_addr);
1158 }
1159 pairing_cb.autopair_attempts++;
1160
1161 /* Create the Bond once again */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001162 BTIF_TRACE_DEBUG("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07001163 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001164 return;
1165 }
1166 else
1167 {
1168 /* if autopair attempts are more than 1, or not attempted */
1169 status = BT_STATUS_AUTH_FAILURE;
1170 }
1171 break;
1172
1173 default:
1174 status = BT_STATUS_FAIL;
1175 }
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301176 /* Special Handling for HID Devices */
1177 if (check_cod(&bd_addr, COD_HID_POINTING)) {
1178 /* Remove Device as bonded in nvram as authentication failed */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001179 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __FUNCTION__);
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301180 btif_storage_remove_bonded_device(&bd_addr);
1181 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001182 bond_state_changed(status, &bd_addr, state);
1183 }
1184}
1185
1186/******************************************************************************
1187**
1188** Function btif_dm_search_devices_evt
1189**
1190** Description Executes search devices callback events in btif context
1191**
1192** Returns void
1193**
1194******************************************************************************/
1195static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
1196{
1197 tBTA_DM_SEARCH *p_search_data;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001198 BTIF_TRACE_EVENT("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001199
1200 switch (event)
1201 {
1202 case BTA_DM_DISC_RES_EVT:
1203 {
1204 p_search_data = (tBTA_DM_SEARCH *)p_param;
1205 /* Remote name update */
1206 if (strlen((const char *) p_search_data->disc_res.bd_name))
1207 {
1208 bt_property_t properties[1];
1209 bt_bdaddr_t bdaddr;
1210 bt_status_t status;
1211
1212 properties[0].type = BT_PROPERTY_BDNAME;
1213 properties[0].val = p_search_data->disc_res.bd_name;
1214 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
1215 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
1216
1217 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
1218 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
1219 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1220 status, &bdaddr, 1, properties);
1221 }
1222 /* TODO: Services? */
1223 }
1224 break;
1225
1226 case BTA_DM_INQ_RES_EVT:
1227 {
1228 /* inquiry result */
1229 UINT32 cod;
1230 UINT8 *p_eir_remote_name = NULL;
1231 bt_bdname_t bdname;
1232 bt_bdaddr_t bdaddr;
1233 UINT8 remote_name_len;
1234 UINT8 *p_cached_name = NULL;
1235 tBTA_SERVICE_MASK services = 0;
1236 bdstr_t bdstr;
1237
1238 p_search_data = (tBTA_DM_SEARCH *)p_param;
1239 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
1240
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07001241 BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __FUNCTION__, bdaddr_to_string(&bdaddr, bdstr, sizeof(bdstr)),
The Android Open Source Project5738f832012-12-12 16:00:35 -08001242#if (BLE_INCLUDED == TRUE)
1243 p_search_data->inq_res.device_type);
1244#else
1245 BT_DEVICE_TYPE_BREDR);
1246#endif
1247 bdname.name[0] = 0;
1248
1249 cod = devclass2uint (p_search_data->inq_res.dev_class);
1250
Chris Mantonf8027002015-03-12 09:22:48 -07001251 if (cod == 0) {
1252 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001253 cod = COD_UNCLASSIFIED;
1254 }
1255
1256 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1257 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1258
1259 /* Check EIR for remote name and services */
1260 if (p_search_data->inq_res.p_eir)
1261 {
1262 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001263 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001264 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */
1265 }
1266
1267
1268 {
1269 bt_property_t properties[5];
1270 bt_device_type_t dev_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001271 UINT8 addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001272 uint32_t num_properties = 0;
1273 bt_status_t status;
1274
1275 memset(properties, 0, sizeof(properties));
1276 /* BD_ADDR */
1277 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1278 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1279 num_properties++;
1280 /* BD_NAME */
1281 /* Don't send BDNAME if it is empty */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001282 if (bdname.name[0])
1283 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001284 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1285 BT_PROPERTY_BDNAME,
1286 strlen((char *)bdname.name), &bdname);
1287 num_properties++;
1288 }
1289
1290 /* DEV_CLASS */
1291 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1292 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1293 num_properties++;
1294 /* DEV_TYPE */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001295#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001296 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1297 dev_type = p_search_data->inq_res.device_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001298 addr_type = p_search_data->inq_res.ble_addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001299#else
1300 dev_type = BT_DEVICE_TYPE_BREDR;
1301#endif
1302 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1303 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1304 num_properties++;
1305 /* RSSI */
1306 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1307 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1308 &(p_search_data->inq_res.rssi));
1309 num_properties++;
1310
1311 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1312 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001313#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1314 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001315 if (( dev_type == BT_DEVICE_TYPE_DUMO)&&
1316 (p_search_data->inq_res.flag & BTA_BLE_DMT_CONTROLLER_SPT) &&
1317 (p_search_data->inq_res.flag & BTA_BLE_DMT_HOST_SPT))
1318 {
1319 btif_storage_set_dmt_support_type (&bdaddr, TRUE);
1320 }
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001321 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1322#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001323 /* Callback to notify upper layer of device */
1324 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1325 num_properties, properties);
1326 }
1327 }
1328 break;
1329
1330 case BTA_DM_INQ_CMPL_EVT:
1331 {
Satya Calloji6e2d9db2014-07-08 16:18:58 -07001332#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1333 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1334 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1335 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1336 bte_scan_filt_param_cfg_evt, 0);
1337#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001338 }
1339 break;
1340 case BTA_DM_DISC_CMPL_EVT:
1341 {
1342 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1343 }
1344 break;
1345 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1346 {
1347 /* if inquiry is not in progress and we get a cancel event, then
1348 * it means we are done with inquiry, but remote_name fetches are in
1349 * progress
1350 *
1351 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt
1352 * but instead wait for the cancel_cmpl_evt via the Busy Level
1353 *
1354 */
1355 if (btif_dm_inquiry_in_progress == FALSE)
1356 {
1357 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1358 }
1359 }
1360 break;
1361 }
1362}
1363
1364/*******************************************************************************
1365**
1366** Function btif_dm_search_services_evt
1367**
1368** Description Executes search services event in btif context
1369**
1370** Returns void
1371**
1372*******************************************************************************/
1373static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1374{
1375 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1376
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001377 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001378 switch (event)
1379 {
1380 case BTA_DM_DISC_RES_EVT:
1381 {
1382 bt_uuid_t uuid_arr[BT_MAX_NUM_UUIDS]; /* Max 32 services */
1383 bt_property_t prop;
1384 uint32_t i = 0, j = 0;
1385 bt_bdaddr_t bd_addr;
1386 bt_status_t ret;
1387
1388 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1389
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001390 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001391 p_data->disc_res.result, p_data->disc_res.services);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001392 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1393 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1394 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1395 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001396 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001397 pairing_cb.sdp_attempts++;
1398 btif_dm_get_remote_services(&bd_addr);
1399 return;
1400 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001401 prop.type = BT_PROPERTY_UUIDS;
1402 prop.len = 0;
1403 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1404 {
1405 prop.val = p_data->disc_res.p_uuid_list;
1406 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1407 for (i=0; i < p_data->disc_res.num_uuids; i++)
1408 {
1409 char temp[256];
Chris Manton8ff3fea2015-01-07 13:59:14 -08001410 uuid_to_string_legacy((bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i*MAX_UUID_SIZE)), temp);
Chris Mantonf8027002015-03-12 09:22:48 -07001411 LOG_INFO("%s index:%d uuid:%s", __func__, i, temp);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001412 }
1413 }
1414
1415 /* onUuidChanged requires getBondedDevices to be populated.
1416 ** bond_state_changed needs to be sent prior to remote_device_property
1417 */
1418 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1419 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0)&&
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001420 pairing_cb.sdp_attempts > 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001421 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001422 BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001423 __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001424 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001425 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1426 }
1427
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001428 if(p_data->disc_res.num_uuids != 0)
1429 {
1430 /* Also write this to the NVRAM */
1431 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1432 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1433 /* Send the event to the BTIF */
1434 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1435 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1436 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001437 }
1438 break;
1439
1440 case BTA_DM_DISC_CMPL_EVT:
1441 /* fixme */
1442 break;
1443
Matthew Xie607e3b72013-08-15 19:30:48 -07001444#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001445 case BTA_DM_DISC_BLE_RES_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001446 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__,
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001447 p_data->disc_ble_res.service.uu.uuid16);
1448 bt_uuid_t uuid;
1449 int i = 0;
1450 int j = 15;
1451 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1452 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001453 BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001454 bt_property_t prop;
1455 bt_bdaddr_t bd_addr;
1456 char temp[256];
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001457 bt_status_t ret;
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001458
1459 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1460
1461 while(i < j )
1462 {
1463 unsigned char c = uuid.uu[j];
1464 uuid.uu[j] = uuid.uu[i];
1465 uuid.uu[i] = c;
1466 i++;
1467 j--;
1468 }
1469
Chris Manton8ff3fea2015-01-07 13:59:14 -08001470 uuid_to_string_legacy(&uuid, temp);
Chris Mantonf8027002015-03-12 09:22:48 -07001471 LOG_INFO("%s uuid:%s", __func__, temp);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001472
1473 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1474 prop.type = BT_PROPERTY_UUIDS;
1475 prop.val = uuid.uu;
1476 prop.len = MAX_UUID_SIZE;
1477
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001478 /* Also write this to the NVRAM */
1479 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1480 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1481
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001482 /* Send the event to the BTIF */
1483 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1484 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1485
1486 }
1487 break;
Matthew Xie607e3b72013-08-15 19:30:48 -07001488#endif /* BLE_INCLUDED */
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001489
The Android Open Source Project5738f832012-12-12 16:00:35 -08001490 default:
1491 {
1492 ASSERTC(0, "unhandled search services event", event);
1493 }
1494 break;
1495 }
1496}
1497
1498/*******************************************************************************
1499**
1500** Function btif_dm_remote_service_record_evt
1501**
1502** Description Executes search service record event in btif context
1503**
1504** Returns void
1505**
1506*******************************************************************************/
1507static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1508{
1509 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1510
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001511 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001512 switch (event)
1513 {
1514 case BTA_DM_DISC_RES_EVT:
1515 {
1516 bt_service_record_t rec;
1517 bt_property_t prop;
1518 uint32_t i = 0;
1519 bt_bdaddr_t bd_addr;
1520
1521 memset(&rec, 0, sizeof(bt_service_record_t));
1522 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1523
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001524 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001525 p_data->disc_res.result, p_data->disc_res.services);
1526 prop.type = BT_PROPERTY_SERVICE_RECORD;
1527 prop.val = (void*)&rec;
1528 prop.len = sizeof(rec);
1529
1530 /* disc_res.result is overloaded with SCN. Cannot check result */
1531 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1532 /* TODO: Get the UUID as well */
1533 rec.channel = p_data->disc_res.result - 3;
1534 /* TODO: Need to get the service name using p_raw_data */
1535 rec.name[0] = 0;
1536
1537 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1538 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1539 }
1540 break;
1541
1542 default:
1543 {
1544 ASSERTC(0, "unhandled remote service record event", event);
1545 }
1546 break;
1547 }
1548}
1549
1550/*******************************************************************************
1551**
1552** Function btif_dm_upstreams_cback
1553**
1554** Description Executes UPSTREAMS events in btif context
1555**
1556** Returns void
1557**
1558*******************************************************************************/
1559static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1560{
1561 tBTA_DM_SEC_EVT dm_event = (tBTA_DM_SEC_EVT)event;
1562 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1563 tBTA_SERVICE_MASK service_mask;
1564 uint32_t i;
1565 bt_bdaddr_t bd_addr;
1566
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001567 BTIF_TRACE_EVENT("btif_dm_upstreams_cback ev: %s", dump_dm_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001568
1569 switch (event)
1570 {
1571 case BTA_DM_ENABLE_EVT:
1572 {
1573 BD_NAME bdname;
1574 bt_status_t status;
1575 bt_property_t prop;
1576 prop.type = BT_PROPERTY_BDNAME;
1577 prop.len = BD_NAME_LEN;
1578 prop.val = (void*)bdname;
1579
1580 status = btif_storage_get_adapter_property(&prop);
Matthew Xie1e5109b2012-11-09 18:26:26 -08001581 if (status == BT_STATUS_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001582 {
1583 /* A name exists in the storage. Make this the device name */
1584 BTA_DmSetDeviceName((char*)prop.val);
1585 }
Matthew Xie1e5109b2012-11-09 18:26:26 -08001586 else
1587 {
1588 /* Storage does not have a name yet.
1589 * Use the default name and write it to the chip
1590 */
1591 BTA_DmSetDeviceName(btif_get_default_local_name());
1592 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001593
Andre Eisenbacha015a832014-09-11 14:09:40 -07001594#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1595 /* Enable local privacy */
Andre Eisenbach3e0dc732014-10-24 09:55:34 -07001596 BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED);
Andre Eisenbacha015a832014-09-11 14:09:40 -07001597#endif
1598
The Android Open Source Project5738f832012-12-12 16:00:35 -08001599 /* for each of the enabled services in the mask, trigger the profile
1600 * enable */
1601 service_mask = btif_get_enabled_services_mask();
1602 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1603 {
1604 if (service_mask &
1605 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1606 {
1607 btif_in_execute_service_request(i, TRUE);
1608 }
1609 }
1610 /* clear control blocks */
1611 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1612
1613 /* This function will also trigger the adapter_properties_cb
1614 ** and bonded_devices_info_cb
1615 */
1616 btif_storage_load_bonded_devices();
1617
1618 btif_storage_load_autopair_device_list();
1619
Zach Johnson39110ec2014-10-06 13:15:00 -07001620 btif_enable_bluetooth_evt(p_data->enable.status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001621 }
1622 break;
1623
1624 case BTA_DM_DISABLE_EVT:
1625 /* for each of the enabled services in the mask, trigger the profile
1626 * disable */
1627 service_mask = btif_get_enabled_services_mask();
1628 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1629 {
1630 if (service_mask &
1631 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1632 {
1633 btif_in_execute_service_request(i, FALSE);
1634 }
1635 }
1636 btif_disable_bluetooth_evt();
1637 break;
1638
1639 case BTA_DM_PIN_REQ_EVT:
1640 btif_dm_pin_req_evt(&p_data->pin_req);
1641 break;
1642
1643 case BTA_DM_AUTH_CMPL_EVT:
1644 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1645 break;
1646
1647 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1648 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1649 {
1650 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1651 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1652 }
1653 break;
1654
1655 case BTA_DM_SP_CFM_REQ_EVT:
1656 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1657 break;
1658 case BTA_DM_SP_KEY_NOTIF_EVT:
1659 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1660 break;
1661
1662 case BTA_DM_DEV_UNPAIRED_EVT:
1663 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1664
1665 /*special handling for HID devices */
1666 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -07001667 btif_hh_remove_device(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001668 #endif
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001669 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1670 btif_storage_remove_ble_bonding_keys(&bd_addr);
1671 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001672 btif_storage_remove_bonded_device(&bd_addr);
1673 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1674 break;
1675
1676 case BTA_DM_BUSY_LEVEL_EVT:
1677 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001678
1679 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001680 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001681 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001682 {
1683 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1684 BT_DISCOVERY_STARTED);
1685 btif_dm_inquiry_in_progress = TRUE;
1686 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001687 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001688 {
1689 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1690 BT_DISCOVERY_STOPPED);
1691 btif_dm_inquiry_in_progress = FALSE;
1692 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001693 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001694 {
1695 btif_dm_inquiry_in_progress = FALSE;
1696 }
1697 }
1698 }break;
1699
1700 case BTA_DM_LINK_UP_EVT:
1701 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001702 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001703
1704 btif_update_remote_version_property(&bd_addr);
1705
The Android Open Source Project5738f832012-12-12 16:00:35 -08001706 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1707 &bd_addr, BT_ACL_STATE_CONNECTED);
1708 break;
1709
1710 case BTA_DM_LINK_DOWN_EVT:
1711 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001712 BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001713 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1714 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1715 break;
1716
1717 case BTA_DM_HW_ERROR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001718 BTIF_TRACE_ERROR("Received H/W Error. ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001719 /* Flush storage data */
1720 btif_config_flush();
1721 usleep(100000); /* 100milliseconds */
1722 /* Killing the process to force a restart as part of fault tolerance */
1723 kill(getpid(), SIGKILL);
1724 break;
1725
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001726#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1727 case BTA_DM_BLE_KEY_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001728 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 -08001729
1730 /* If this pairing is by-product of local initiated GATT client Read or Write,
1731 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1732 have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1733 if (pairing_cb.state != BT_BOND_STATE_BONDING)
1734 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001735 BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001736 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1737 BT_BOND_STATE_BONDING);
1738 }
1739 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1740 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001741 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001742 break;
1743 }
1744
1745 switch (p_data->ble_key.key_type)
1746 {
1747 case BTA_LE_KEY_PENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001748 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001749 pairing_cb.ble.is_penc_key_rcvd = TRUE;
1750 memcpy(pairing_cb.ble.penc_key.ltk,p_data->ble_key.key_value.penc_key.ltk, 16);
1751 memcpy(pairing_cb.ble.penc_key.rand, p_data->ble_key.key_value.penc_key.rand,8);
1752 pairing_cb.ble.penc_key.ediv = p_data->ble_key.key_value.penc_key.ediv;
1753 pairing_cb.ble.penc_key.sec_level = p_data->ble_key.key_value.penc_key.sec_level;
1754
1755 for (i=0; i<16; i++)
1756 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001757 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 -08001758 }
1759 for (i=0; i<8; i++)
1760 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001761 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 -08001762 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001763 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.ediv=0x%04x",pairing_cb.ble.penc_key.ediv);
1764 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.sec_level=0x%02x",pairing_cb.ble.penc_key.sec_level);
1765 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 -08001766 break;
1767
1768 case BTA_LE_KEY_PID:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001769 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001770 pairing_cb.ble.is_pid_key_rcvd = TRUE;
Andre Eisenbach5e808462014-10-21 12:37:53 -07001771 pairing_cb.ble.pid_key.addr_type = p_data->ble_key.key_value.pid_key.addr_type;
1772 memcpy(pairing_cb.ble.pid_key.irk, p_data->ble_key.key_value.pid_key.irk, 16);
1773 memcpy(pairing_cb.ble.pid_key.static_addr,
1774 p_data->ble_key.key_value.pid_key.static_addr,BD_ADDR_LEN);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001775 for (i=0; i<16; i++)
1776 {
Andre Eisenbach5e808462014-10-21 12:37:53 -07001777 BTIF_TRACE_DEBUG("pairing_cb.ble.pid_key.irk[%d]=0x%02x"
1778 ,i,pairing_cb.ble.pid_key.irk[i]);
1779 }
1780 for (i=0; i<BD_ADDR_LEN; i++)
1781 {
1782 BTIF_TRACE_DEBUG("piaring_cb.ble.pid_address[%d] = %x"
1783 ,i, pairing_cb.ble.pid_key.static_addr[i]);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001784 }
1785 break;
1786
1787 case BTA_LE_KEY_PCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001788 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001789 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1790 pairing_cb.ble.pcsrk_key.counter = p_data->ble_key.key_value.pcsrk_key.counter;
1791 pairing_cb.ble.pcsrk_key.sec_level = p_data->ble_key.key_value.pcsrk_key.sec_level;
1792 memcpy(pairing_cb.ble.pcsrk_key.csrk,p_data->ble_key.key_value.pcsrk_key.csrk,16);
1793
1794 for (i=0; i<16; i++)
1795 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001796 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 -08001797 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001798 BTIF_TRACE_DEBUG("pairing_cb.ble.pcsrk_key.counter=0x%08x",pairing_cb.ble.pcsrk_key.counter);
1799 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 -08001800 break;
1801
1802 case BTA_LE_KEY_LENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001803 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001804 pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1805 pairing_cb.ble.lenc_key.div = p_data->ble_key.key_value.lenc_key.div;
1806 pairing_cb.ble.lenc_key.key_size = p_data->ble_key.key_value.lenc_key.key_size;
1807 pairing_cb.ble.lenc_key.sec_level = p_data->ble_key.key_value.lenc_key.sec_level;
1808
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001809 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.div=0x%04x",pairing_cb.ble.lenc_key.div);
1810 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.key_size=0x%02x",pairing_cb.ble.lenc_key.key_size);
1811 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 -08001812 break;
1813
1814
1815
1816 case BTA_LE_KEY_LCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001817 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001818 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1819 pairing_cb.ble.lcsrk_key.counter = p_data->ble_key.key_value.lcsrk_key.counter;
1820 pairing_cb.ble.lcsrk_key.div = p_data->ble_key.key_value.lcsrk_key.div;
1821 pairing_cb.ble.lcsrk_key.sec_level = p_data->ble_key.key_value.lcsrk_key.sec_level;
1822
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001823 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.div=0x%04x",pairing_cb.ble.lcsrk_key.div);
1824 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.counter=0x%08x",pairing_cb.ble.lcsrk_key.counter);
1825 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 -08001826
1827 break;
1828
1829 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001830 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001831 break;
1832 }
1833
1834 break;
1835 case BTA_DM_BLE_SEC_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001836 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001837 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1838 break;
1839 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001840 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001841 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1842 break;
1843 case BTA_DM_BLE_PASSKEY_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001844 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001845 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1846 break;
1847 case BTA_DM_BLE_OOB_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001848 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001849 break;
1850 case BTA_DM_BLE_LOCAL_IR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001851 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001852 ble_local_key_cb.is_id_keys_rcvd = TRUE;
1853 memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1854 memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1855 memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1856 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1857 BTIF_DM_LE_LOCAL_KEY_IR,
1858 BT_OCTET16_LEN);
1859 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1860 BTIF_DM_LE_LOCAL_KEY_IRK,
1861 BT_OCTET16_LEN);
1862 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1863 BTIF_DM_LE_LOCAL_KEY_DHK,
1864 BT_OCTET16_LEN);
1865 break;
1866 case BTA_DM_BLE_LOCAL_ER_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001867 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001868 ble_local_key_cb.is_er_rcvd = TRUE;
1869 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1870 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1871 BTIF_DM_LE_LOCAL_KEY_ER,
1872 BT_OCTET16_LEN);
1873 break;
1874
1875 case BTA_DM_BLE_AUTH_CMPL_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001876 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001877 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1878 break;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001879
1880 case BTA_DM_LE_FEATURES_READ:
1881 {
1882 tBTM_BLE_VSC_CB cmn_vsc_cb;
1883 bt_local_le_features_t local_le_features;
1884 char buf[512];
1885 bt_property_t prop;
1886 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1887 prop.val = (void*)buf;
1888 prop.len = sizeof(buf);
1889
1890 /* LE features are not stored in storage. Should be retrived from stack */
1891 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1892 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1893
1894 prop.len = sizeof (bt_local_le_features_t);
1895 if (cmn_vsc_cb.filter_support == 1)
1896 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1897 else
1898 local_le_features.max_adv_filter_supported = 0;
1899 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1900 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1901 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
Satya Callojid773c2c2014-07-29 22:08:55 -07001902 local_le_features.scan_result_storage_size_hibyte =
1903 (cmn_vsc_cb.tot_scan_results_strg >> 8) & (0xFF);
1904 local_le_features.scan_result_storage_size_lobyte =
1905 (cmn_vsc_cb.tot_scan_results_strg) & (0xFF);
Satya Callojiefaddcb2014-07-28 23:22:05 -07001906 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001907 memcpy(prop.val, &local_le_features, prop.len);
1908 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
1909 break;
1910 }
Satya Callojie5ba8842014-07-03 17:18:02 -07001911
1912 case BTA_DM_ENER_INFO_READ:
1913 {
1914 btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param;
1915 bt_activity_energy_info energy_info;
1916 energy_info.status = p_ener_data->status;
1917 energy_info.ctrl_state = p_ener_data->ctrl_state;
1918 energy_info.rx_time = p_ener_data->rx_time;
1919 energy_info.tx_time = p_ener_data->tx_time;
1920 energy_info.idle_time = p_ener_data->idle_time;
1921 energy_info.energy_used = p_ener_data->energy_used;
1922 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info);
1923 break;
1924 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001925#endif
1926
The Android Open Source Project5738f832012-12-12 16:00:35 -08001927 case BTA_DM_AUTHORIZE_EVT:
1928 case BTA_DM_SIG_STRENGTH_EVT:
1929 case BTA_DM_SP_RMT_OOB_EVT:
1930 case BTA_DM_SP_KEYPRESS_EVT:
1931 case BTA_DM_ROLE_CHG_EVT:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001932
The Android Open Source Project5738f832012-12-12 16:00:35 -08001933 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001934 BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001935 break;
1936 }
1937} /* btui_security_cback() */
1938
1939
1940/*******************************************************************************
1941**
1942** Function btif_dm_generic_evt
1943**
1944** Description Executes non-BTA upstream events in BTIF context
1945**
1946** Returns void
1947**
1948*******************************************************************************/
1949static void btif_dm_generic_evt(UINT16 event, char* p_param)
1950{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001951 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001952 switch(event)
1953 {
1954 case BTIF_DM_CB_DISCOVERY_STARTED:
1955 {
1956 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
1957 }
1958 break;
1959
1960 case BTIF_DM_CB_CREATE_BOND:
1961 {
Andre Eisenbach31a64002014-10-14 14:29:19 -07001962 pairing_cb.timeout_retries = NUM_TIMEOUT_RETRIES;
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07001963 btif_dm_create_bond_cb_t *create_bond_cb = (btif_dm_create_bond_cb_t*)p_param;
1964 btif_dm_cb_create_bond(&create_bond_cb->bdaddr, create_bond_cb->transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001965 }
1966 break;
1967
1968 case BTIF_DM_CB_REMOVE_BOND:
1969 {
1970 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
1971 }
1972 break;
1973
1974 case BTIF_DM_CB_HID_REMOTE_NAME:
1975 {
1976 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
1977 }
1978 break;
1979
1980 case BTIF_DM_CB_BOND_STATE_BONDING:
1981 {
1982 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
1983 }
1984 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001985 case BTIF_DM_CB_LE_TX_TEST:
1986 case BTIF_DM_CB_LE_RX_TEST:
1987 {
1988 uint8_t status;
1989 STREAM_TO_UINT8(status, p_param);
1990 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1991 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
1992 }
1993 break;
1994 case BTIF_DM_CB_LE_TEST_END:
1995 {
1996 uint8_t status;
1997 uint16_t count = 0;
1998 STREAM_TO_UINT8(status, p_param);
1999 if (status == 0)
2000 STREAM_TO_UINT16(count, p_param);
2001 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2002 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
2003 }
2004 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002005 default:
2006 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002007 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002008 }
2009 break;
2010 }
2011}
2012
2013/*******************************************************************************
2014**
2015** Function bte_dm_evt
2016**
2017** Description Switches context from BTE to BTIF for all DM events
2018**
2019** Returns void
2020**
2021*******************************************************************************/
2022
2023void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
2024{
2025 bt_status_t status;
2026
2027 /* switch context to btif task context (copy full union size for convenience) */
2028 status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event, (void*)p_data, sizeof(tBTA_DM_SEC), NULL);
2029
2030 /* catch any failed context transfers */
2031 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
2032}
2033
2034/*******************************************************************************
2035**
2036** Function bte_search_devices_evt
2037**
2038** Description Switches context from BTE to BTIF for DM search events
2039**
2040** Returns void
2041**
2042*******************************************************************************/
2043static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2044{
2045 UINT16 param_len = 0;
2046
2047 if (p_data)
2048 param_len += sizeof(tBTA_DM_SEARCH);
2049 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
2050 switch (event)
2051 {
2052 case BTA_DM_INQ_RES_EVT:
2053 {
2054 if (p_data->inq_res.p_eir)
2055 param_len += HCI_EXT_INQ_RESPONSE_LEN;
2056 }
2057 break;
2058
2059 case BTA_DM_DISC_RES_EVT:
2060 {
2061 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
2062 param_len += p_data->disc_res.raw_data_size;
2063 }
2064 break;
2065 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002066 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 -08002067
2068 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
2069 if (event == BTA_DM_INQ_RES_EVT)
2070 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
2071
2072 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
2073 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
2074}
2075
2076/*******************************************************************************
2077**
2078** Function bte_dm_search_services_evt
2079**
2080** Description Switches context from BTE to BTIF for DM search services
2081** event
2082**
2083** Returns void
2084**
2085*******************************************************************************/
2086static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2087{
2088 UINT16 param_len = 0;
2089 if (p_data)
2090 param_len += sizeof(tBTA_DM_SEARCH);
2091 switch (event)
2092 {
2093 case BTA_DM_DISC_RES_EVT:
2094 {
2095 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
2096 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
2097 }
2098 } break;
2099 }
2100 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
2101 * if raw_data is needed. */
2102 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
2103 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
2104}
2105
2106/*******************************************************************************
2107**
2108** Function bte_dm_remote_service_record_evt
2109**
2110** Description Switches context from BTE to BTIF for DM search service
2111** record event
2112**
2113** Returns void
2114**
2115*******************************************************************************/
2116static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2117{
2118 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
2119 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
2120}
2121
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002122#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07002123/*******************************************************************************
2124**
2125** Function bta_energy_info_cb
2126**
2127** Description Switches context from BTE to BTIF for DM energy info event
2128**
2129** Returns void
2130**
2131*******************************************************************************/
2132static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time,
2133 tBTA_DM_BLE_IDLE_TIME_MS idle_time,
2134 tBTA_DM_BLE_ENERGY_USED energy_used,
2135 tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status)
2136{
2137 BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld",
2138 status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
2139
2140 btif_activity_energy_info_cb_t btif_cb;
2141 btif_cb.status = status;
2142 btif_cb.ctrl_state = ctrl_state;
2143 btif_cb.tx_time = (uint64_t) tx_time;
2144 btif_cb.rx_time = (uint64_t) rx_time;
2145 btif_cb.idle_time =(uint64_t) idle_time;
2146 btif_cb.energy_used =(uint64_t) energy_used;
2147 btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
2148 (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL);
2149}
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002150#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07002151
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002152/*******************************************************************************
2153**
2154** Function bte_scan_filt_param_cfg_evt
2155**
2156** Description Scan filter param config event
2157**
2158** Returns void
2159**
2160*******************************************************************************/
2161static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
2162 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
2163 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
2164{
2165 /* This event occurs on calling BTA_DmBleCfgFilterCondition internally,
2166 ** and that is why there is no HAL callback
2167 */
2168 if(BTA_SUCCESS != status)
2169 {
2170 BTIF_TRACE_ERROR("%s, %d", __FUNCTION__, status);
2171 }
2172 else
2173 {
2174 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2175 }
2176}
2177
The Android Open Source Project5738f832012-12-12 16:00:35 -08002178/*****************************************************************************
2179**
2180** btif api functions (no context switch)
2181**
2182*****************************************************************************/
2183
2184/*******************************************************************************
2185**
2186** Function btif_dm_start_discovery
2187**
2188** Description Start device discovery/inquiry
2189**
2190** Returns bt_status_t
2191**
2192*******************************************************************************/
2193bt_status_t btif_dm_start_discovery(void)
2194{
2195 tBTA_DM_INQ inq_params;
2196 tBTA_SERVICE_MASK services = 0;
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002197 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002198
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002199 BTIF_TRACE_EVENT("%s", __FUNCTION__);
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002200
2201#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2202 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
2203 /* Cleanup anything remaining on index 0 */
2204 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
2205 bte_scan_filt_param_cfg_evt, 0);
2206
2207 /* Add an allow-all filter on index 0*/
2208 adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE;
2209 adv_filt_param.feat_seln = ALLOW_ALL_FILTER;
2210 adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;
2211 adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;
2212 adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE;
2213 adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE;
2214 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL,
2215 bte_scan_filt_param_cfg_evt, 0);
2216
The Android Open Source Project5738f832012-12-12 16:00:35 -08002217 /* TODO: Do we need to handle multiple inquiries at the same time? */
2218
2219 /* Set inquiry params and call API */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002220 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
Matthew Xie7f3e4292013-09-30 12:44:10 -07002221#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
2222 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
2223 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
2224 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
2225 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
2226#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002227#else
2228 inq_params.mode = BTA_DM_GENERAL_INQUIRY;
2229#endif
2230 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
2231
2232 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
2233 inq_params.report_dup = TRUE;
2234
2235 inq_params.filter_type = BTA_DM_INQ_CLR;
2236 /* TODO: Filter device by BDA needs to be implemented here */
2237
2238 /* Will be enabled to TRUE once inquiry busy level has been received */
2239 btif_dm_inquiry_in_progress = FALSE;
2240 /* find nearby devices */
2241 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
2242
2243 return BT_STATUS_SUCCESS;
2244}
2245
2246/*******************************************************************************
2247**
2248** Function btif_dm_cancel_discovery
2249**
2250** Description Cancels search
2251**
2252** Returns bt_status_t
2253**
2254*******************************************************************************/
2255bt_status_t btif_dm_cancel_discovery(void)
2256{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002257 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002258 BTA_DmSearchCancel();
2259 return BT_STATUS_SUCCESS;
2260}
2261
2262/*******************************************************************************
2263**
2264** Function btif_dm_create_bond
2265**
2266** Description Initiate bonding with the specified device
2267**
2268** Returns bt_status_t
2269**
2270*******************************************************************************/
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002271bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr, int transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002272{
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002273 btif_dm_create_bond_cb_t create_bond_cb;
2274 create_bond_cb.transport = transport;
2275 bdcpy(create_bond_cb.bdaddr.address, bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002276
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002277 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002278 BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)), transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002279 if (pairing_cb.state != BT_BOND_STATE_NONE)
2280 return BT_STATUS_BUSY;
2281
2282 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002283 (char *)&create_bond_cb, sizeof(btif_dm_create_bond_cb_t), NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002284
2285 return BT_STATUS_SUCCESS;
2286}
2287
2288/*******************************************************************************
2289**
2290** Function btif_dm_cancel_bond
2291**
2292** Description Initiate bonding with the specified device
2293**
2294** Returns bt_status_t
2295**
2296*******************************************************************************/
2297
2298bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
2299{
2300 bdstr_t bdstr;
2301
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002302 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002303
2304 /* TODO:
2305 ** 1. Restore scan modes
2306 ** 2. special handling for HID devices
2307 */
2308 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2309 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002310
2311#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2312
2313 if (pairing_cb.is_ssp)
2314 {
2315 if (pairing_cb.is_le_only)
2316 {
2317 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2318 }
2319 else
Hemant Guptae1468692013-11-14 16:21:29 +05302320 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002321 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
Hemant Guptae1468692013-11-14 16:21:29 +05302322 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2323 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2324 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002325 }
2326 else
2327 {
2328 if (pairing_cb.is_le_only)
2329 {
2330 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2331 }
2332 else
2333 {
2334 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2335 }
2336 /* Cancel bonding, in case it is in ACL connection setup state */
2337 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2338 }
2339
2340#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002341 if (pairing_cb.is_ssp)
2342 {
2343 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2344 }
2345 else
2346 {
2347 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2348 }
2349 /* Cancel bonding, in case it is in ACL connection setup state */
2350 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2351 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002352#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002353 }
2354
2355 return BT_STATUS_SUCCESS;
2356}
2357
2358/*******************************************************************************
2359**
Kim Schulza9eb25c2013-09-30 10:55:52 +02002360** Function btif_dm_hh_open_failed
2361**
2362** Description informs the upper layers if the HH have failed during bonding
2363**
2364** Returns none
2365**
2366*******************************************************************************/
2367
2368void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
2369{
2370 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2371 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2372 {
2373 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2374 }
2375}
2376
2377/*******************************************************************************
2378**
The Android Open Source Project5738f832012-12-12 16:00:35 -08002379** Function btif_dm_remove_bond
2380**
2381** Description Removes bonding with the specified device
2382**
2383** Returns bt_status_t
2384**
2385*******************************************************************************/
2386
2387bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
2388{
2389 bdstr_t bdstr;
2390
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002391 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002392 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2393 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2394
2395 return BT_STATUS_SUCCESS;
2396}
2397
2398/*******************************************************************************
2399**
2400** Function btif_dm_pin_reply
2401**
2402** Description BT legacy pairing - PIN code reply
2403**
2404** Returns bt_status_t
2405**
2406*******************************************************************************/
2407
2408bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2409 uint8_t pin_len, bt_pin_code_t *pin_code)
2410{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002411 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Hemant Gupta831423e2014-01-08 12:42:13 +05302412 if (pin_code == NULL)
2413 return BT_STATUS_FAIL;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002414#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002415
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002416 if (pairing_cb.is_le_only)
2417 {
2418 int i;
2419 UINT32 passkey = 0;
2420 int multi[] = {100000, 10000, 1000, 100, 10,1};
2421 BD_ADDR remote_bd_addr;
2422 bdcpy(remote_bd_addr, bd_addr->address);
2423 for (i = 0; i < 6; i++)
2424 {
2425 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2426 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002427 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002428 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2429
2430 }
2431 else
2432 {
2433 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2434 if (accept)
2435 pairing_cb.pin_code_len = pin_len;
2436 }
2437#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002438 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2439
2440 if (accept)
2441 pairing_cb.pin_code_len = pin_len;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002442#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002443 return BT_STATUS_SUCCESS;
2444}
2445
2446/*******************************************************************************
2447**
2448** Function btif_dm_ssp_reply
2449**
2450** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2451**
2452** Returns bt_status_t
2453**
2454*******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002455bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2456 bt_ssp_variant_t variant, uint8_t accept,
2457 uint32_t passkey)
2458{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08002459 UNUSED(passkey);
2460
The Android Open Source Project5738f832012-12-12 16:00:35 -08002461 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2462 {
2463 /* This is not implemented in the stack.
2464 * For devices with display, this is not needed
2465 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002466 BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002467 return BT_STATUS_FAIL;
2468 }
2469 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002470 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002471#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2472 if (pairing_cb.is_le_only)
2473 {
2474 if (accept)
2475 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2476 else
2477 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2478 }
2479 else
2480 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002481
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002482#else
2483 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2484#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002485 return BT_STATUS_SUCCESS;
2486}
2487
2488/*******************************************************************************
2489**
2490** Function btif_dm_get_adapter_property
2491**
2492** Description Queries the BTA for the adapter property
2493**
2494** Returns bt_status_t
2495**
2496*******************************************************************************/
2497bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2498{
2499 bt_status_t status;
2500
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002501 BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002502 switch (prop->type)
2503 {
2504 case BT_PROPERTY_BDNAME:
2505 {
2506 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
VenkatRaghavan VijayaRaghavan1d8e6b82015-02-05 22:20:39 -08002507 strncpy((char *)bd_name->name,btif_get_default_local_name(),
2508 sizeof(bd_name->name) - 1);
2509 bd_name->name[sizeof(bd_name->name) - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002510 prop->len = strlen((char *)bd_name->name);
2511 }
2512 break;
2513
2514 case BT_PROPERTY_ADAPTER_SCAN_MODE:
2515 {
2516 /* if the storage does not have it. Most likely app never set it. Default is NONE */
2517 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2518 *mode = BT_SCAN_MODE_NONE;
2519 prop->len = sizeof(bt_scan_mode_t);
2520 }
2521 break;
2522
2523 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2524 {
2525 uint32_t *tmt = (uint32_t*)prop->val;
2526 *tmt = 120; /* default to 120s, if not found in NV */
2527 prop->len = sizeof(uint32_t);
2528 }
2529 break;
2530
2531 default:
2532 prop->len = 0;
2533 return BT_STATUS_FAIL;
2534 }
2535 return BT_STATUS_SUCCESS;
2536}
2537
2538/*******************************************************************************
2539**
2540** Function btif_dm_get_remote_services
2541**
2542** Description Start SDP to get remote services
2543**
2544** Returns bt_status_t
2545**
2546*******************************************************************************/
2547bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2548{
2549 bdstr_t bdstr;
2550
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002551 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr)));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002552
2553 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2554 bte_dm_search_services_evt, TRUE);
2555
2556 return BT_STATUS_SUCCESS;
2557}
2558
2559/*******************************************************************************
2560**
2561** Function btif_dm_get_remote_service_record
2562**
2563** Description Start SDP to get remote service record
2564**
2565**
2566** Returns bt_status_t
2567*******************************************************************************/
2568bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2569 bt_uuid_t *uuid)
2570{
2571 tSDP_UUID sdp_uuid;
2572 bdstr_t bdstr;
2573
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002574 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr)));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002575
2576 sdp_uuid.len = MAX_UUID_SIZE;
2577 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2578
2579 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2580 bte_dm_remote_service_record_evt, TRUE);
2581
2582 return BT_STATUS_SUCCESS;
2583}
2584
2585void btif_dm_execute_service_request(UINT16 event, char *p_param)
2586{
2587 BOOLEAN b_enable = FALSE;
2588 bt_status_t status;
2589 if (event == BTIF_DM_ENABLE_SERVICE)
2590 {
2591 b_enable = TRUE;
2592 }
2593 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2594 if (status == BT_STATUS_SUCCESS)
2595 {
2596 bt_property_t property;
2597 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2598
2599 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2600 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2601 sizeof(local_uuids), local_uuids);
2602 btif_storage_get_adapter_property(&property);
2603 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2604 BT_STATUS_SUCCESS, 1, &property);
2605 }
2606 return;
2607}
2608
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002609void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2610 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2611{
2612 UINT8 yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2613 /* if local initiated:
2614 ** 1. set DD + MITM
2615 ** if remote initiated:
2616 ** 1. Copy over the auth_req from peer's io_rsp
2617 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2618 ** as a fallback set MITM+GB if peer had MITM set
2619 */
2620 UNUSED (bd_addr);
2621 UNUSED (p_io_cap);
2622 UNUSED (p_oob_data);
2623
2624
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002625 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002626 if(pairing_cb.is_local_initiated)
2627 {
2628 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2629 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2630 }
2631 else if (!is_orig)
2632 {
2633 /* peer initiated paring. They probably know what they want.
2634 ** Copy the mitm from peer device.
2635 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002636 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002637 __FUNCTION__, pairing_cb.auth_req);
2638 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2639
2640 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2641 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2642 *p_auth_req |= BTA_AUTH_SP_YES;
2643 }
2644 else if (yes_no_bit)
2645 {
2646 /* set the general bonding bit for stored device */
2647 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2648 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002649 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002650}
2651
2652void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2653 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2654{
2655 UNUSED (bd_addr);
2656 UNUSED (oob_data);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002657
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002658 if(auth_req & BTA_AUTH_BONDS)
2659 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002660 BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002661 pairing_cb.auth_req = auth_req;
2662 pairing_cb.io_cap = io_cap;
2663 }
2664}
2665
The Android Open Source Project5738f832012-12-12 16:00:35 -08002666#if (BTM_OOB_INCLUDED == TRUE)
2667void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data)
2668{
2669 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2670 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2671 {
2672 *p_oob_data = FALSE;
2673 }
2674 else
2675 {
2676 *p_oob_data = TRUE;
2677 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002678 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 -08002679}
2680#endif /* BTM_OOB_INCLUDED */
2681
2682#ifdef BTIF_DM_OOB_TEST
2683void btif_dm_load_local_oob(void)
2684{
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002685 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002686 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002687 BTIF_TRACE_DEBUG("btif_dm_load_local_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002688 if (prop_oob[0] != '3')
2689 {
2690#if (BTM_OOB_INCLUDED == TRUE)
2691 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2692 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2693 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002694 BTIF_TRACE_DEBUG("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002695 BTA_DmLocalOob();
2696 }
2697#else
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002698 BTIF_TRACE_ERROR("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002699#endif
2700 }
2701}
2702
2703void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2704{
2705 FILE *fp;
2706 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2707 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2708 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002709 char prop_oob[PROPERTY_VALUE_MAX];
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002710 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: valid=%d", valid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002711 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2712 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2713 valid)
2714 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002715 BTIF_TRACE_DEBUG("save local OOB data in memory");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002716 memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2717 memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2718 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002719 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002720 if (prop_oob[0] == '1')
2721 path = path_a;
2722 else if (prop_oob[0] == '2')
2723 path = path_b;
2724 if (path)
2725 {
2726 fp = fopen(path, "wb+");
2727 if (fp == NULL)
2728 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002729 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 -08002730 }
2731 else
2732 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002733 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 -08002734 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2735 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2736 fclose(fp);
2737 }
2738 }
2739 }
2740}
2741BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2742{
2743 char t[128];
2744 FILE *fp;
2745 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2746 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2747 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002748 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002749 BOOLEAN result = FALSE;
2750 bt_bdaddr_t bt_bd_addr;
2751 bdcpy(oob_cb.oob_bdaddr, bd_addr);
2752 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002753 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002754 if (prop_oob[0] == '1')
2755 path = path_b;
2756 else if (prop_oob[0] == '2')
2757 path = path_a;
2758 if (path)
2759 {
2760 fp = fopen(path, "rb");
2761 if (fp == NULL)
2762 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002763 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 -08002764 return FALSE;
2765 }
2766 else
2767 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002768 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob: read OOB data from %s",path);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002769 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2770 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2771 fclose(fp);
2772 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002773 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: TRUE");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002774 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2775 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2776 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002777 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: peer_bdaddr = %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_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
2780 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 -07002781 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: c = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002782 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2783 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
2784 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 -07002785 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: r = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002786 bdcpy(bt_bd_addr.address, bd_addr);
2787 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2788 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2789 result = TRUE;
2790 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002791 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob result=%d",result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002792 return result;
2793}
2794#endif /* BTIF_DM_OOB_TEST */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002795#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2796
2797static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2798{
2799 bt_bdaddr_t bd_addr;
2800 bt_bdname_t bd_name;
2801 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08002802 int dev_type;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002803
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002804 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002805
2806 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08002807 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
2808 {
2809 dev_type = BT_DEVICE_TYPE_BLE;
2810 }
2811 btif_dm_update_ble_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2812 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002813 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2814 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2815
2816 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2817 pairing_cb.is_ssp = FALSE;
2818 cod = COD_UNCLASSIFIED;
2819
2820 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2821 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2822 p_ssp_key_notif->passkey);
2823}
2824
2825/*******************************************************************************
2826**
2827** Function btif_dm_ble_auth_cmpl_evt
2828**
2829** Description Executes authentication complete event in btif context
2830**
2831** Returns void
2832**
2833*******************************************************************************/
2834static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2835{
2836 /* Save link key, if not temporary */
2837 bt_bdaddr_t bd_addr;
2838 bt_status_t status = BT_STATUS_FAIL;
2839 bt_bond_state_t state = BT_BOND_STATE_NONE;
2840
2841 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2842 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2843 {
2844 /* store keys */
2845 }
2846 if (p_auth_cmpl->success)
2847 {
2848 status = BT_STATUS_SUCCESS;
2849 state = BT_BOND_STATE_BONDED;
2850
2851 btif_dm_save_ble_bonding_keys();
2852 BTA_GATTC_Refresh(bd_addr.address);
2853 btif_dm_get_remote_services(&bd_addr);
2854 }
2855 else
2856 {
2857 /*Map the HCI fail reason to bt status */
2858 switch (p_auth_cmpl->fail_reason)
2859 {
Priti Aghera156c52b2014-07-09 14:58:19 -07002860 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
2861 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
2862 btif_dm_remove_ble_bonding_keys();
2863 status = BT_STATUS_AUTH_FAILURE;
2864 break;
2865 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
2866 status = BT_STATUS_AUTH_REJECTED;
2867 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002868 default:
Andre Eisenbachca22ac42013-02-13 17:02:11 +09002869 btif_dm_remove_ble_bonding_keys();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002870 status = BT_STATUS_FAIL;
2871 break;
2872 }
2873 }
2874 bond_state_changed(status, &bd_addr, state);
2875}
2876
2877
2878
2879void btif_dm_load_ble_local_keys(void)
2880{
2881 bt_status_t bt_status;
2882
2883 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2884
2885 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2886 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2887 {
2888 ble_local_key_cb.is_er_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002889 BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002890 }
2891
2892 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
2893 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
2894 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
2895 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
2896 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
2897 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
2898 {
2899 ble_local_key_cb.is_id_keys_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002900 BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002901 }
2902
2903}
2904void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
2905 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
2906{
2907 if (ble_local_key_cb.is_er_rcvd )
2908 {
2909 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2910 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2911 }
2912
2913 if (ble_local_key_cb.is_id_keys_rcvd)
2914 {
2915 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
2916 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
2917 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
2918 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
2919 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002920 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002921}
2922
2923void btif_dm_save_ble_bonding_keys(void)
2924{
2925
2926 bt_bdaddr_t bd_addr;
2927
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002928 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002929
2930 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2931
2932 if (pairing_cb.ble.is_penc_key_rcvd)
2933 {
2934 btif_storage_add_ble_bonding_key(&bd_addr,
2935 (char *) &pairing_cb.ble.penc_key,
2936 BTIF_DM_LE_KEY_PENC,
2937 sizeof(btif_dm_ble_penc_keys_t));
2938 }
2939
2940 if (pairing_cb.ble.is_pid_key_rcvd)
2941 {
2942 btif_storage_add_ble_bonding_key(&bd_addr,
Andre Eisenbach5e808462014-10-21 12:37:53 -07002943 (char *) &pairing_cb.ble.pid_key,
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002944 BTIF_DM_LE_KEY_PID,
Andre Eisenbach5e808462014-10-21 12:37:53 -07002945 sizeof(btif_dm_ble_pid_keys_t));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002946 }
2947
2948
2949 if (pairing_cb.ble.is_pcsrk_key_rcvd)
2950 {
2951 btif_storage_add_ble_bonding_key(&bd_addr,
2952 (char *) &pairing_cb.ble.pcsrk_key,
2953 BTIF_DM_LE_KEY_PCSRK,
2954 sizeof(btif_dm_ble_pcsrk_keys_t));
2955 }
2956
2957
2958 if (pairing_cb.ble.is_lenc_key_rcvd)
2959 {
2960 btif_storage_add_ble_bonding_key(&bd_addr,
2961 (char *) &pairing_cb.ble.lenc_key,
2962 BTIF_DM_LE_KEY_LENC,
2963 sizeof(btif_dm_ble_lenc_keys_t));
2964 }
2965
2966 if (pairing_cb.ble.is_lcsrk_key_rcvd)
2967 {
2968 btif_storage_add_ble_bonding_key(&bd_addr,
2969 (char *) &pairing_cb.ble.lcsrk_key,
2970 BTIF_DM_LE_KEY_LCSRK,
2971 sizeof(btif_dm_ble_lcsrk_keys_t));
2972 }
2973
2974}
2975
2976
2977void btif_dm_remove_ble_bonding_keys(void)
2978{
2979 bt_bdaddr_t bd_addr;
2980
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002981 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002982
2983 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2984 btif_storage_remove_ble_bonding_keys(&bd_addr);
2985}
2986
2987
2988/*******************************************************************************
2989**
2990** Function btif_dm_ble_sec_req_evt
2991**
2992** Description Eprocess security request event in btif context
2993**
2994** Returns void
2995**
2996*******************************************************************************/
2997void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
2998{
2999 bt_bdaddr_t bd_addr;
3000 bt_bdname_t bd_name;
3001 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08003002 int dev_type;
3003
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003004 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003005
3006 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3007 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003008 BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003009 return;
3010 }
3011
3012 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08003013 if (!btif_get_device_type(p_ble_req->bd_addr, &dev_type))
3014 {
3015 dev_type = BT_DEVICE_TYPE_BLE;
3016 }
3017 btif_dm_update_ble_remote_properties(p_ble_req->bd_addr, p_ble_req->bd_name,
3018 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003019
3020 bdcpy(bd_addr.address, p_ble_req->bd_addr);
3021 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
3022
3023 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3024
Andre Eisenbach89363762015-01-26 13:49:36 -08003025 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003026 pairing_cb.is_le_only = TRUE;
3027 pairing_cb.is_ssp = TRUE;
3028
3029 cod = COD_UNCLASSIFIED;
3030
3031 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
3032 BT_SSP_VARIANT_CONSENT, 0);
3033}
3034
3035
3036
3037/*******************************************************************************
3038**
3039** Function btif_dm_ble_passkey_req_evt
3040**
3041** Description Executes pin request event in btif context
3042**
3043** Returns void
3044**
3045*******************************************************************************/
3046static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
3047{
3048 bt_bdaddr_t bd_addr;
3049 bt_bdname_t bd_name;
3050 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08003051 int dev_type;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003052
3053 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08003054 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
3055 {
3056 dev_type = BT_DEVICE_TYPE_BLE;
3057 }
3058 btif_dm_update_ble_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,
3059 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003060
3061 bdcpy(bd_addr.address, p_pin_req->bd_addr);
3062 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
3063
3064 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3065 pairing_cb.is_le_only = TRUE;
3066
3067 cod = COD_UNCLASSIFIED;
3068
3069 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
3070 &bd_addr, &bd_name, cod);
3071}
3072
3073
3074void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
3075 tBT_DEVICE_TYPE dev_type)
3076{
3077 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
3078}
3079
3080static void btif_dm_ble_tx_test_cback(void *p)
3081{
3082 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
3083 (char *)p, 1, NULL);
3084}
3085
3086static void btif_dm_ble_rx_test_cback(void *p)
3087{
3088 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
3089 (char *)p, 1, NULL);
3090}
3091
3092static void btif_dm_ble_test_end_cback(void *p)
3093{
3094 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
3095 (char *)p, 3, NULL);
3096}
3097/*******************************************************************************
3098**
3099** Function btif_le_test_mode
3100**
3101** Description Sends a HCI BLE Test command to the Controller
3102**
3103** Returns BT_STATUS_SUCCESS on success
3104**
3105*******************************************************************************/
3106bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
3107{
3108 switch (opcode) {
3109 case HCI_BLE_TRANSMITTER_TEST:
3110 if (len != 3) return BT_STATUS_PARM_INVALID;
3111 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
3112 break;
3113 case HCI_BLE_RECEIVER_TEST:
3114 if (len != 1) return BT_STATUS_PARM_INVALID;
3115 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
3116 break;
3117 case HCI_BLE_TEST_END:
3118 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
3119 break;
3120 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003121 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003122 return BT_STATUS_UNSUPPORTED;
3123 }
3124 return BT_STATUS_SUCCESS;
3125}
3126
3127#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08003128
3129void btif_dm_on_disable()
3130{
3131 /* cancel any pending pairing requests */
3132 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3133 {
3134 bt_bdaddr_t bd_addr;
3135
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003136 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003137 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3138 btif_dm_cancel_bond(&bd_addr);
3139 }
3140}
Matthew Xie1e5109b2012-11-09 18:26:26 -08003141
Satya Callojie5ba8842014-07-03 17:18:02 -07003142/*******************************************************************************
3143**
3144** Function btif_dm_read_energy_info
3145**
3146** Description Reads the energy info from controller
3147**
3148** Returns void
3149**
3150*******************************************************************************/
3151void btif_dm_read_energy_info()
3152{
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003153#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07003154 BTA_DmBleGetEnergyInfo(bta_energy_info_cb);
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003155#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07003156}
3157
Matthew Xie1e5109b2012-11-09 18:26:26 -08003158static char* btif_get_default_local_name() {
3159 if (btif_default_local_name[0] == '\0')
3160 {
3161 int max_len = sizeof(btif_default_local_name) - 1;
3162 if (BTM_DEF_LOCAL_NAME[0] != '\0')
3163 {
3164 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
3165 }
3166 else
3167 {
3168 char prop_model[PROPERTY_VALUE_MAX];
3169 property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
3170 strncpy(btif_default_local_name, prop_model, max_len);
3171 }
3172 btif_default_local_name[max_len] = '\0';
3173 }
3174 return btif_default_local_name;
3175}