blob: b11b3648baa2ab3cc392e15a2c9839790117c447 [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 {
Nitin Arora7b85efa2014-09-26 14:05:24 -07001357#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1358 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1359 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1360 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1361 bte_scan_filt_param_cfg_evt, 0);
1362#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001363 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1364 }
1365 }
1366 break;
1367 }
1368}
1369
1370/*******************************************************************************
1371**
1372** Function btif_dm_search_services_evt
1373**
1374** Description Executes search services event in btif context
1375**
1376** Returns void
1377**
1378*******************************************************************************/
1379static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1380{
1381 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1382
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001383 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001384 switch (event)
1385 {
1386 case BTA_DM_DISC_RES_EVT:
1387 {
1388 bt_uuid_t uuid_arr[BT_MAX_NUM_UUIDS]; /* Max 32 services */
1389 bt_property_t prop;
1390 uint32_t i = 0, j = 0;
1391 bt_bdaddr_t bd_addr;
1392 bt_status_t ret;
1393
1394 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1395
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001396 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001397 p_data->disc_res.result, p_data->disc_res.services);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001398 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1399 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1400 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1401 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001402 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001403 pairing_cb.sdp_attempts++;
1404 btif_dm_get_remote_services(&bd_addr);
1405 return;
1406 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001407 prop.type = BT_PROPERTY_UUIDS;
1408 prop.len = 0;
1409 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1410 {
1411 prop.val = p_data->disc_res.p_uuid_list;
1412 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1413 for (i=0; i < p_data->disc_res.num_uuids; i++)
1414 {
1415 char temp[256];
Chris Manton8ff3fea2015-01-07 13:59:14 -08001416 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 -07001417 LOG_INFO("%s index:%d uuid:%s", __func__, i, temp);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001418 }
1419 }
1420
1421 /* onUuidChanged requires getBondedDevices to be populated.
1422 ** bond_state_changed needs to be sent prior to remote_device_property
1423 */
1424 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1425 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0)&&
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001426 pairing_cb.sdp_attempts > 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001427 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001428 BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001429 __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001430 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001431 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1432 }
1433
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001434 if(p_data->disc_res.num_uuids != 0)
1435 {
1436 /* Also write this to the NVRAM */
1437 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1438 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1439 /* Send the event to the BTIF */
1440 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1441 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1442 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001443 }
1444 break;
1445
1446 case BTA_DM_DISC_CMPL_EVT:
1447 /* fixme */
1448 break;
1449
Matthew Xie607e3b72013-08-15 19:30:48 -07001450#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001451 case BTA_DM_DISC_BLE_RES_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001452 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__,
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001453 p_data->disc_ble_res.service.uu.uuid16);
1454 bt_uuid_t uuid;
1455 int i = 0;
1456 int j = 15;
1457 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1458 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001459 BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001460 bt_property_t prop;
1461 bt_bdaddr_t bd_addr;
1462 char temp[256];
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001463 bt_status_t ret;
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001464
1465 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1466
1467 while(i < j )
1468 {
1469 unsigned char c = uuid.uu[j];
1470 uuid.uu[j] = uuid.uu[i];
1471 uuid.uu[i] = c;
1472 i++;
1473 j--;
1474 }
1475
Chris Manton8ff3fea2015-01-07 13:59:14 -08001476 uuid_to_string_legacy(&uuid, temp);
Chris Mantonf8027002015-03-12 09:22:48 -07001477 LOG_INFO("%s uuid:%s", __func__, temp);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001478
1479 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1480 prop.type = BT_PROPERTY_UUIDS;
1481 prop.val = uuid.uu;
1482 prop.len = MAX_UUID_SIZE;
1483
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001484 /* Also write this to the NVRAM */
1485 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1486 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1487
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001488 /* Send the event to the BTIF */
1489 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1490 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1491
1492 }
1493 break;
Matthew Xie607e3b72013-08-15 19:30:48 -07001494#endif /* BLE_INCLUDED */
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001495
The Android Open Source Project5738f832012-12-12 16:00:35 -08001496 default:
1497 {
1498 ASSERTC(0, "unhandled search services event", event);
1499 }
1500 break;
1501 }
1502}
1503
1504/*******************************************************************************
1505**
1506** Function btif_dm_remote_service_record_evt
1507**
1508** Description Executes search service record event in btif context
1509**
1510** Returns void
1511**
1512*******************************************************************************/
1513static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1514{
1515 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1516
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001517 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001518 switch (event)
1519 {
1520 case BTA_DM_DISC_RES_EVT:
1521 {
1522 bt_service_record_t rec;
1523 bt_property_t prop;
1524 uint32_t i = 0;
1525 bt_bdaddr_t bd_addr;
1526
1527 memset(&rec, 0, sizeof(bt_service_record_t));
1528 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1529
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001530 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001531 p_data->disc_res.result, p_data->disc_res.services);
1532 prop.type = BT_PROPERTY_SERVICE_RECORD;
1533 prop.val = (void*)&rec;
1534 prop.len = sizeof(rec);
1535
1536 /* disc_res.result is overloaded with SCN. Cannot check result */
1537 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1538 /* TODO: Get the UUID as well */
1539 rec.channel = p_data->disc_res.result - 3;
1540 /* TODO: Need to get the service name using p_raw_data */
1541 rec.name[0] = 0;
1542
1543 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1544 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1545 }
1546 break;
1547
1548 default:
1549 {
1550 ASSERTC(0, "unhandled remote service record event", event);
1551 }
1552 break;
1553 }
1554}
1555
1556/*******************************************************************************
1557**
1558** Function btif_dm_upstreams_cback
1559**
1560** Description Executes UPSTREAMS events in btif context
1561**
1562** Returns void
1563**
1564*******************************************************************************/
1565static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1566{
1567 tBTA_DM_SEC_EVT dm_event = (tBTA_DM_SEC_EVT)event;
1568 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1569 tBTA_SERVICE_MASK service_mask;
1570 uint32_t i;
1571 bt_bdaddr_t bd_addr;
1572
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001573 BTIF_TRACE_EVENT("btif_dm_upstreams_cback ev: %s", dump_dm_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001574
1575 switch (event)
1576 {
1577 case BTA_DM_ENABLE_EVT:
1578 {
1579 BD_NAME bdname;
1580 bt_status_t status;
1581 bt_property_t prop;
1582 prop.type = BT_PROPERTY_BDNAME;
1583 prop.len = BD_NAME_LEN;
1584 prop.val = (void*)bdname;
1585
1586 status = btif_storage_get_adapter_property(&prop);
Matthew Xie1e5109b2012-11-09 18:26:26 -08001587 if (status == BT_STATUS_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001588 {
1589 /* A name exists in the storage. Make this the device name */
1590 BTA_DmSetDeviceName((char*)prop.val);
1591 }
Matthew Xie1e5109b2012-11-09 18:26:26 -08001592 else
1593 {
1594 /* Storage does not have a name yet.
1595 * Use the default name and write it to the chip
1596 */
1597 BTA_DmSetDeviceName(btif_get_default_local_name());
1598 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001599
Andre Eisenbacha015a832014-09-11 14:09:40 -07001600#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1601 /* Enable local privacy */
Andre Eisenbach3e0dc732014-10-24 09:55:34 -07001602 BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED);
Andre Eisenbacha015a832014-09-11 14:09:40 -07001603#endif
1604
The Android Open Source Project5738f832012-12-12 16:00:35 -08001605 /* for each of the enabled services in the mask, trigger the profile
1606 * enable */
1607 service_mask = btif_get_enabled_services_mask();
1608 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1609 {
1610 if (service_mask &
1611 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1612 {
1613 btif_in_execute_service_request(i, TRUE);
1614 }
1615 }
1616 /* clear control blocks */
1617 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1618
1619 /* This function will also trigger the adapter_properties_cb
1620 ** and bonded_devices_info_cb
1621 */
1622 btif_storage_load_bonded_devices();
1623
1624 btif_storage_load_autopair_device_list();
1625
Zach Johnson39110ec2014-10-06 13:15:00 -07001626 btif_enable_bluetooth_evt(p_data->enable.status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001627 }
1628 break;
1629
1630 case BTA_DM_DISABLE_EVT:
1631 /* for each of the enabled services in the mask, trigger the profile
1632 * disable */
1633 service_mask = btif_get_enabled_services_mask();
1634 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1635 {
1636 if (service_mask &
1637 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1638 {
1639 btif_in_execute_service_request(i, FALSE);
1640 }
1641 }
1642 btif_disable_bluetooth_evt();
1643 break;
1644
1645 case BTA_DM_PIN_REQ_EVT:
1646 btif_dm_pin_req_evt(&p_data->pin_req);
1647 break;
1648
1649 case BTA_DM_AUTH_CMPL_EVT:
1650 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1651 break;
1652
1653 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1654 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1655 {
1656 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1657 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1658 }
1659 break;
1660
1661 case BTA_DM_SP_CFM_REQ_EVT:
1662 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1663 break;
1664 case BTA_DM_SP_KEY_NOTIF_EVT:
1665 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1666 break;
1667
1668 case BTA_DM_DEV_UNPAIRED_EVT:
1669 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1670
1671 /*special handling for HID devices */
1672 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -07001673 btif_hh_remove_device(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001674 #endif
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001675 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1676 btif_storage_remove_ble_bonding_keys(&bd_addr);
1677 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001678 btif_storage_remove_bonded_device(&bd_addr);
1679 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1680 break;
1681
1682 case BTA_DM_BUSY_LEVEL_EVT:
1683 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001684
1685 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001686 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001687 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001688 {
1689 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1690 BT_DISCOVERY_STARTED);
1691 btif_dm_inquiry_in_progress = TRUE;
1692 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001693 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001694 {
1695 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1696 BT_DISCOVERY_STOPPED);
1697 btif_dm_inquiry_in_progress = FALSE;
1698 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001699 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001700 {
1701 btif_dm_inquiry_in_progress = FALSE;
1702 }
1703 }
1704 }break;
1705
1706 case BTA_DM_LINK_UP_EVT:
1707 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001708 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001709
1710 btif_update_remote_version_property(&bd_addr);
1711
The Android Open Source Project5738f832012-12-12 16:00:35 -08001712 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1713 &bd_addr, BT_ACL_STATE_CONNECTED);
1714 break;
1715
1716 case BTA_DM_LINK_DOWN_EVT:
1717 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001718 BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001719 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1720 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1721 break;
1722
1723 case BTA_DM_HW_ERROR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001724 BTIF_TRACE_ERROR("Received H/W Error. ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001725 /* Flush storage data */
1726 btif_config_flush();
1727 usleep(100000); /* 100milliseconds */
1728 /* Killing the process to force a restart as part of fault tolerance */
1729 kill(getpid(), SIGKILL);
1730 break;
1731
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001732#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1733 case BTA_DM_BLE_KEY_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001734 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 -08001735
1736 /* If this pairing is by-product of local initiated GATT client Read or Write,
1737 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1738 have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1739 if (pairing_cb.state != BT_BOND_STATE_BONDING)
1740 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001741 BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001742 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1743 BT_BOND_STATE_BONDING);
1744 }
1745 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1746 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001747 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001748 break;
1749 }
1750
1751 switch (p_data->ble_key.key_type)
1752 {
1753 case BTA_LE_KEY_PENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001754 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001755 pairing_cb.ble.is_penc_key_rcvd = TRUE;
1756 memcpy(pairing_cb.ble.penc_key.ltk,p_data->ble_key.key_value.penc_key.ltk, 16);
1757 memcpy(pairing_cb.ble.penc_key.rand, p_data->ble_key.key_value.penc_key.rand,8);
1758 pairing_cb.ble.penc_key.ediv = p_data->ble_key.key_value.penc_key.ediv;
1759 pairing_cb.ble.penc_key.sec_level = p_data->ble_key.key_value.penc_key.sec_level;
1760
1761 for (i=0; i<16; i++)
1762 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001763 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 -08001764 }
1765 for (i=0; i<8; i++)
1766 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001767 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 -08001768 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001769 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.ediv=0x%04x",pairing_cb.ble.penc_key.ediv);
1770 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.sec_level=0x%02x",pairing_cb.ble.penc_key.sec_level);
1771 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 -08001772 break;
1773
1774 case BTA_LE_KEY_PID:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001775 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001776 pairing_cb.ble.is_pid_key_rcvd = TRUE;
Andre Eisenbach5e808462014-10-21 12:37:53 -07001777 pairing_cb.ble.pid_key.addr_type = p_data->ble_key.key_value.pid_key.addr_type;
1778 memcpy(pairing_cb.ble.pid_key.irk, p_data->ble_key.key_value.pid_key.irk, 16);
1779 memcpy(pairing_cb.ble.pid_key.static_addr,
1780 p_data->ble_key.key_value.pid_key.static_addr,BD_ADDR_LEN);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001781 for (i=0; i<16; i++)
1782 {
Andre Eisenbach5e808462014-10-21 12:37:53 -07001783 BTIF_TRACE_DEBUG("pairing_cb.ble.pid_key.irk[%d]=0x%02x"
1784 ,i,pairing_cb.ble.pid_key.irk[i]);
1785 }
1786 for (i=0; i<BD_ADDR_LEN; i++)
1787 {
1788 BTIF_TRACE_DEBUG("piaring_cb.ble.pid_address[%d] = %x"
1789 ,i, pairing_cb.ble.pid_key.static_addr[i]);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001790 }
1791 break;
1792
1793 case BTA_LE_KEY_PCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001794 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001795 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1796 pairing_cb.ble.pcsrk_key.counter = p_data->ble_key.key_value.pcsrk_key.counter;
1797 pairing_cb.ble.pcsrk_key.sec_level = p_data->ble_key.key_value.pcsrk_key.sec_level;
1798 memcpy(pairing_cb.ble.pcsrk_key.csrk,p_data->ble_key.key_value.pcsrk_key.csrk,16);
1799
1800 for (i=0; i<16; i++)
1801 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001802 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 -08001803 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001804 BTIF_TRACE_DEBUG("pairing_cb.ble.pcsrk_key.counter=0x%08x",pairing_cb.ble.pcsrk_key.counter);
1805 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 -08001806 break;
1807
1808 case BTA_LE_KEY_LENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001809 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001810 pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1811 pairing_cb.ble.lenc_key.div = p_data->ble_key.key_value.lenc_key.div;
1812 pairing_cb.ble.lenc_key.key_size = p_data->ble_key.key_value.lenc_key.key_size;
1813 pairing_cb.ble.lenc_key.sec_level = p_data->ble_key.key_value.lenc_key.sec_level;
1814
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001815 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.div=0x%04x",pairing_cb.ble.lenc_key.div);
1816 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.key_size=0x%02x",pairing_cb.ble.lenc_key.key_size);
1817 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 -08001818 break;
1819
1820
1821
1822 case BTA_LE_KEY_LCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001823 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001824 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1825 pairing_cb.ble.lcsrk_key.counter = p_data->ble_key.key_value.lcsrk_key.counter;
1826 pairing_cb.ble.lcsrk_key.div = p_data->ble_key.key_value.lcsrk_key.div;
1827 pairing_cb.ble.lcsrk_key.sec_level = p_data->ble_key.key_value.lcsrk_key.sec_level;
1828
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001829 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.div=0x%04x",pairing_cb.ble.lcsrk_key.div);
1830 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.counter=0x%08x",pairing_cb.ble.lcsrk_key.counter);
1831 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 -08001832
1833 break;
1834
1835 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001836 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001837 break;
1838 }
1839
1840 break;
1841 case BTA_DM_BLE_SEC_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001842 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001843 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1844 break;
1845 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001846 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001847 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1848 break;
1849 case BTA_DM_BLE_PASSKEY_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001850 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001851 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1852 break;
1853 case BTA_DM_BLE_OOB_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001854 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001855 break;
1856 case BTA_DM_BLE_LOCAL_IR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001857 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001858 ble_local_key_cb.is_id_keys_rcvd = TRUE;
1859 memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1860 memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1861 memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1862 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1863 BTIF_DM_LE_LOCAL_KEY_IR,
1864 BT_OCTET16_LEN);
1865 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1866 BTIF_DM_LE_LOCAL_KEY_IRK,
1867 BT_OCTET16_LEN);
1868 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1869 BTIF_DM_LE_LOCAL_KEY_DHK,
1870 BT_OCTET16_LEN);
1871 break;
1872 case BTA_DM_BLE_LOCAL_ER_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001873 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001874 ble_local_key_cb.is_er_rcvd = TRUE;
1875 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1876 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1877 BTIF_DM_LE_LOCAL_KEY_ER,
1878 BT_OCTET16_LEN);
1879 break;
1880
1881 case BTA_DM_BLE_AUTH_CMPL_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001882 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001883 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1884 break;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001885
1886 case BTA_DM_LE_FEATURES_READ:
1887 {
1888 tBTM_BLE_VSC_CB cmn_vsc_cb;
1889 bt_local_le_features_t local_le_features;
1890 char buf[512];
1891 bt_property_t prop;
1892 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1893 prop.val = (void*)buf;
1894 prop.len = sizeof(buf);
1895
1896 /* LE features are not stored in storage. Should be retrived from stack */
1897 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1898 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1899
1900 prop.len = sizeof (bt_local_le_features_t);
1901 if (cmn_vsc_cb.filter_support == 1)
1902 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1903 else
1904 local_le_features.max_adv_filter_supported = 0;
1905 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1906 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1907 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
Satya Callojid773c2c2014-07-29 22:08:55 -07001908 local_le_features.scan_result_storage_size_hibyte =
1909 (cmn_vsc_cb.tot_scan_results_strg >> 8) & (0xFF);
1910 local_le_features.scan_result_storage_size_lobyte =
1911 (cmn_vsc_cb.tot_scan_results_strg) & (0xFF);
Satya Callojiefaddcb2014-07-28 23:22:05 -07001912 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001913 memcpy(prop.val, &local_le_features, prop.len);
1914 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
1915 break;
1916 }
Satya Callojie5ba8842014-07-03 17:18:02 -07001917
1918 case BTA_DM_ENER_INFO_READ:
1919 {
1920 btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param;
1921 bt_activity_energy_info energy_info;
1922 energy_info.status = p_ener_data->status;
1923 energy_info.ctrl_state = p_ener_data->ctrl_state;
1924 energy_info.rx_time = p_ener_data->rx_time;
1925 energy_info.tx_time = p_ener_data->tx_time;
1926 energy_info.idle_time = p_ener_data->idle_time;
1927 energy_info.energy_used = p_ener_data->energy_used;
1928 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info);
1929 break;
1930 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001931#endif
1932
The Android Open Source Project5738f832012-12-12 16:00:35 -08001933 case BTA_DM_AUTHORIZE_EVT:
1934 case BTA_DM_SIG_STRENGTH_EVT:
1935 case BTA_DM_SP_RMT_OOB_EVT:
1936 case BTA_DM_SP_KEYPRESS_EVT:
1937 case BTA_DM_ROLE_CHG_EVT:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001938
The Android Open Source Project5738f832012-12-12 16:00:35 -08001939 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001940 BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001941 break;
1942 }
1943} /* btui_security_cback() */
1944
1945
1946/*******************************************************************************
1947**
1948** Function btif_dm_generic_evt
1949**
1950** Description Executes non-BTA upstream events in BTIF context
1951**
1952** Returns void
1953**
1954*******************************************************************************/
1955static void btif_dm_generic_evt(UINT16 event, char* p_param)
1956{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001957 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001958 switch(event)
1959 {
1960 case BTIF_DM_CB_DISCOVERY_STARTED:
1961 {
1962 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
1963 }
1964 break;
1965
1966 case BTIF_DM_CB_CREATE_BOND:
1967 {
Andre Eisenbach31a64002014-10-14 14:29:19 -07001968 pairing_cb.timeout_retries = NUM_TIMEOUT_RETRIES;
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07001969 btif_dm_create_bond_cb_t *create_bond_cb = (btif_dm_create_bond_cb_t*)p_param;
1970 btif_dm_cb_create_bond(&create_bond_cb->bdaddr, create_bond_cb->transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001971 }
1972 break;
1973
1974 case BTIF_DM_CB_REMOVE_BOND:
1975 {
1976 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
1977 }
1978 break;
1979
1980 case BTIF_DM_CB_HID_REMOTE_NAME:
1981 {
1982 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
1983 }
1984 break;
1985
1986 case BTIF_DM_CB_BOND_STATE_BONDING:
1987 {
1988 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
1989 }
1990 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001991 case BTIF_DM_CB_LE_TX_TEST:
1992 case BTIF_DM_CB_LE_RX_TEST:
1993 {
1994 uint8_t status;
1995 STREAM_TO_UINT8(status, p_param);
1996 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1997 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
1998 }
1999 break;
2000 case BTIF_DM_CB_LE_TEST_END:
2001 {
2002 uint8_t status;
2003 uint16_t count = 0;
2004 STREAM_TO_UINT8(status, p_param);
2005 if (status == 0)
2006 STREAM_TO_UINT16(count, p_param);
2007 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2008 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
2009 }
2010 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002011 default:
2012 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002013 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002014 }
2015 break;
2016 }
2017}
2018
2019/*******************************************************************************
2020**
2021** Function bte_dm_evt
2022**
2023** Description Switches context from BTE to BTIF for all DM events
2024**
2025** Returns void
2026**
2027*******************************************************************************/
2028
2029void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
2030{
2031 bt_status_t status;
2032
2033 /* switch context to btif task context (copy full union size for convenience) */
2034 status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event, (void*)p_data, sizeof(tBTA_DM_SEC), NULL);
2035
2036 /* catch any failed context transfers */
2037 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
2038}
2039
2040/*******************************************************************************
2041**
2042** Function bte_search_devices_evt
2043**
2044** Description Switches context from BTE to BTIF for DM search events
2045**
2046** Returns void
2047**
2048*******************************************************************************/
2049static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2050{
2051 UINT16 param_len = 0;
2052
2053 if (p_data)
2054 param_len += sizeof(tBTA_DM_SEARCH);
2055 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
2056 switch (event)
2057 {
2058 case BTA_DM_INQ_RES_EVT:
2059 {
2060 if (p_data->inq_res.p_eir)
2061 param_len += HCI_EXT_INQ_RESPONSE_LEN;
2062 }
2063 break;
2064
2065 case BTA_DM_DISC_RES_EVT:
2066 {
2067 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
2068 param_len += p_data->disc_res.raw_data_size;
2069 }
2070 break;
2071 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002072 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 -08002073
2074 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
2075 if (event == BTA_DM_INQ_RES_EVT)
2076 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
2077
2078 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
2079 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
2080}
2081
2082/*******************************************************************************
2083**
2084** Function bte_dm_search_services_evt
2085**
2086** Description Switches context from BTE to BTIF for DM search services
2087** event
2088**
2089** Returns void
2090**
2091*******************************************************************************/
2092static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2093{
2094 UINT16 param_len = 0;
2095 if (p_data)
2096 param_len += sizeof(tBTA_DM_SEARCH);
2097 switch (event)
2098 {
2099 case BTA_DM_DISC_RES_EVT:
2100 {
2101 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
2102 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
2103 }
2104 } break;
2105 }
2106 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
2107 * if raw_data is needed. */
2108 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
2109 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
2110}
2111
2112/*******************************************************************************
2113**
2114** Function bte_dm_remote_service_record_evt
2115**
2116** Description Switches context from BTE to BTIF for DM search service
2117** record event
2118**
2119** Returns void
2120**
2121*******************************************************************************/
2122static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2123{
2124 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
2125 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
2126}
2127
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002128#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07002129/*******************************************************************************
2130**
2131** Function bta_energy_info_cb
2132**
2133** Description Switches context from BTE to BTIF for DM energy info event
2134**
2135** Returns void
2136**
2137*******************************************************************************/
2138static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time,
2139 tBTA_DM_BLE_IDLE_TIME_MS idle_time,
2140 tBTA_DM_BLE_ENERGY_USED energy_used,
2141 tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status)
2142{
2143 BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld",
2144 status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
2145
2146 btif_activity_energy_info_cb_t btif_cb;
2147 btif_cb.status = status;
2148 btif_cb.ctrl_state = ctrl_state;
2149 btif_cb.tx_time = (uint64_t) tx_time;
2150 btif_cb.rx_time = (uint64_t) rx_time;
2151 btif_cb.idle_time =(uint64_t) idle_time;
2152 btif_cb.energy_used =(uint64_t) energy_used;
2153 btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
2154 (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL);
2155}
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002156#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07002157
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002158/*******************************************************************************
2159**
2160** Function bte_scan_filt_param_cfg_evt
2161**
2162** Description Scan filter param config event
2163**
2164** Returns void
2165**
2166*******************************************************************************/
2167static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
2168 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
2169 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
2170{
2171 /* This event occurs on calling BTA_DmBleCfgFilterCondition internally,
2172 ** and that is why there is no HAL callback
2173 */
2174 if(BTA_SUCCESS != status)
2175 {
2176 BTIF_TRACE_ERROR("%s, %d", __FUNCTION__, status);
2177 }
2178 else
2179 {
2180 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2181 }
2182}
2183
The Android Open Source Project5738f832012-12-12 16:00:35 -08002184/*****************************************************************************
2185**
2186** btif api functions (no context switch)
2187**
2188*****************************************************************************/
2189
2190/*******************************************************************************
2191**
2192** Function btif_dm_start_discovery
2193**
2194** Description Start device discovery/inquiry
2195**
2196** Returns bt_status_t
2197**
2198*******************************************************************************/
2199bt_status_t btif_dm_start_discovery(void)
2200{
2201 tBTA_DM_INQ inq_params;
2202 tBTA_SERVICE_MASK services = 0;
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002203 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002204
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002205 BTIF_TRACE_EVENT("%s", __FUNCTION__);
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002206
2207#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2208 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
2209 /* Cleanup anything remaining on index 0 */
2210 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
2211 bte_scan_filt_param_cfg_evt, 0);
2212
2213 /* Add an allow-all filter on index 0*/
2214 adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE;
2215 adv_filt_param.feat_seln = ALLOW_ALL_FILTER;
2216 adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;
2217 adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;
2218 adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE;
2219 adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE;
2220 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL,
2221 bte_scan_filt_param_cfg_evt, 0);
2222
The Android Open Source Project5738f832012-12-12 16:00:35 -08002223 /* TODO: Do we need to handle multiple inquiries at the same time? */
2224
2225 /* Set inquiry params and call API */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002226 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
Matthew Xie7f3e4292013-09-30 12:44:10 -07002227#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
2228 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
2229 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
2230 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
2231 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
2232#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002233#else
2234 inq_params.mode = BTA_DM_GENERAL_INQUIRY;
2235#endif
2236 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
2237
2238 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
2239 inq_params.report_dup = TRUE;
2240
2241 inq_params.filter_type = BTA_DM_INQ_CLR;
2242 /* TODO: Filter device by BDA needs to be implemented here */
2243
2244 /* Will be enabled to TRUE once inquiry busy level has been received */
2245 btif_dm_inquiry_in_progress = FALSE;
2246 /* find nearby devices */
2247 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
2248
2249 return BT_STATUS_SUCCESS;
2250}
2251
2252/*******************************************************************************
2253**
2254** Function btif_dm_cancel_discovery
2255**
2256** Description Cancels search
2257**
2258** Returns bt_status_t
2259**
2260*******************************************************************************/
2261bt_status_t btif_dm_cancel_discovery(void)
2262{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002263 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002264 BTA_DmSearchCancel();
2265 return BT_STATUS_SUCCESS;
2266}
2267
2268/*******************************************************************************
2269**
2270** Function btif_dm_create_bond
2271**
2272** Description Initiate bonding with the specified device
2273**
2274** Returns bt_status_t
2275**
2276*******************************************************************************/
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002277bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr, int transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002278{
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002279 btif_dm_create_bond_cb_t create_bond_cb;
2280 create_bond_cb.transport = transport;
2281 bdcpy(create_bond_cb.bdaddr.address, bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002282
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002283 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002284 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 -08002285 if (pairing_cb.state != BT_BOND_STATE_NONE)
2286 return BT_STATUS_BUSY;
2287
2288 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002289 (char *)&create_bond_cb, sizeof(btif_dm_create_bond_cb_t), NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002290
2291 return BT_STATUS_SUCCESS;
2292}
2293
2294/*******************************************************************************
2295**
2296** Function btif_dm_cancel_bond
2297**
2298** Description Initiate bonding with the specified device
2299**
2300** Returns bt_status_t
2301**
2302*******************************************************************************/
2303
2304bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
2305{
2306 bdstr_t bdstr;
2307
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002308 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 -08002309
2310 /* TODO:
2311 ** 1. Restore scan modes
2312 ** 2. special handling for HID devices
2313 */
2314 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2315 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002316
2317#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2318
2319 if (pairing_cb.is_ssp)
2320 {
2321 if (pairing_cb.is_le_only)
2322 {
2323 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2324 }
2325 else
Hemant Guptae1468692013-11-14 16:21:29 +05302326 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002327 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
Hemant Guptae1468692013-11-14 16:21:29 +05302328 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2329 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2330 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002331 }
2332 else
2333 {
2334 if (pairing_cb.is_le_only)
2335 {
2336 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2337 }
2338 else
2339 {
2340 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2341 }
2342 /* Cancel bonding, in case it is in ACL connection setup state */
2343 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2344 }
2345
2346#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002347 if (pairing_cb.is_ssp)
2348 {
2349 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2350 }
2351 else
2352 {
2353 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2354 }
2355 /* Cancel bonding, in case it is in ACL connection setup state */
2356 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2357 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002358#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002359 }
2360
2361 return BT_STATUS_SUCCESS;
2362}
2363
2364/*******************************************************************************
2365**
Kim Schulza9eb25c2013-09-30 10:55:52 +02002366** Function btif_dm_hh_open_failed
2367**
2368** Description informs the upper layers if the HH have failed during bonding
2369**
2370** Returns none
2371**
2372*******************************************************************************/
2373
2374void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
2375{
2376 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2377 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2378 {
2379 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2380 }
2381}
2382
2383/*******************************************************************************
2384**
The Android Open Source Project5738f832012-12-12 16:00:35 -08002385** Function btif_dm_remove_bond
2386**
2387** Description Removes bonding with the specified device
2388**
2389** Returns bt_status_t
2390**
2391*******************************************************************************/
2392
2393bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
2394{
2395 bdstr_t bdstr;
2396
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002397 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 -08002398 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2399 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2400
2401 return BT_STATUS_SUCCESS;
2402}
2403
2404/*******************************************************************************
2405**
2406** Function btif_dm_pin_reply
2407**
2408** Description BT legacy pairing - PIN code reply
2409**
2410** Returns bt_status_t
2411**
2412*******************************************************************************/
2413
2414bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2415 uint8_t pin_len, bt_pin_code_t *pin_code)
2416{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002417 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Hemant Gupta831423e2014-01-08 12:42:13 +05302418 if (pin_code == NULL)
2419 return BT_STATUS_FAIL;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002420#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002421
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002422 if (pairing_cb.is_le_only)
2423 {
2424 int i;
2425 UINT32 passkey = 0;
2426 int multi[] = {100000, 10000, 1000, 100, 10,1};
2427 BD_ADDR remote_bd_addr;
2428 bdcpy(remote_bd_addr, bd_addr->address);
2429 for (i = 0; i < 6; i++)
2430 {
2431 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2432 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002433 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002434 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2435
2436 }
2437 else
2438 {
2439 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2440 if (accept)
2441 pairing_cb.pin_code_len = pin_len;
2442 }
2443#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002444 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2445
2446 if (accept)
2447 pairing_cb.pin_code_len = pin_len;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002448#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002449 return BT_STATUS_SUCCESS;
2450}
2451
2452/*******************************************************************************
2453**
2454** Function btif_dm_ssp_reply
2455**
2456** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2457**
2458** Returns bt_status_t
2459**
2460*******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002461bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2462 bt_ssp_variant_t variant, uint8_t accept,
2463 uint32_t passkey)
2464{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08002465 UNUSED(passkey);
2466
The Android Open Source Project5738f832012-12-12 16:00:35 -08002467 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2468 {
2469 /* This is not implemented in the stack.
2470 * For devices with display, this is not needed
2471 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002472 BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002473 return BT_STATUS_FAIL;
2474 }
2475 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002476 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002477#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2478 if (pairing_cb.is_le_only)
2479 {
2480 if (accept)
2481 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2482 else
2483 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2484 }
2485 else
2486 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002487
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002488#else
2489 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2490#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002491 return BT_STATUS_SUCCESS;
2492}
2493
2494/*******************************************************************************
2495**
2496** Function btif_dm_get_adapter_property
2497**
2498** Description Queries the BTA for the adapter property
2499**
2500** Returns bt_status_t
2501**
2502*******************************************************************************/
2503bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2504{
2505 bt_status_t status;
2506
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002507 BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002508 switch (prop->type)
2509 {
2510 case BT_PROPERTY_BDNAME:
2511 {
2512 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
VenkatRaghavan VijayaRaghavan1d8e6b82015-02-05 22:20:39 -08002513 strncpy((char *)bd_name->name,btif_get_default_local_name(),
2514 sizeof(bd_name->name) - 1);
2515 bd_name->name[sizeof(bd_name->name) - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002516 prop->len = strlen((char *)bd_name->name);
2517 }
2518 break;
2519
2520 case BT_PROPERTY_ADAPTER_SCAN_MODE:
2521 {
2522 /* if the storage does not have it. Most likely app never set it. Default is NONE */
2523 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2524 *mode = BT_SCAN_MODE_NONE;
2525 prop->len = sizeof(bt_scan_mode_t);
2526 }
2527 break;
2528
2529 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2530 {
2531 uint32_t *tmt = (uint32_t*)prop->val;
2532 *tmt = 120; /* default to 120s, if not found in NV */
2533 prop->len = sizeof(uint32_t);
2534 }
2535 break;
2536
2537 default:
2538 prop->len = 0;
2539 return BT_STATUS_FAIL;
2540 }
2541 return BT_STATUS_SUCCESS;
2542}
2543
2544/*******************************************************************************
2545**
2546** Function btif_dm_get_remote_services
2547**
2548** Description Start SDP to get remote services
2549**
2550** Returns bt_status_t
2551**
2552*******************************************************************************/
2553bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2554{
2555 bdstr_t bdstr;
2556
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002557 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 -08002558
2559 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2560 bte_dm_search_services_evt, TRUE);
2561
2562 return BT_STATUS_SUCCESS;
2563}
2564
2565/*******************************************************************************
2566**
2567** Function btif_dm_get_remote_service_record
2568**
2569** Description Start SDP to get remote service record
2570**
2571**
2572** Returns bt_status_t
2573*******************************************************************************/
2574bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2575 bt_uuid_t *uuid)
2576{
2577 tSDP_UUID sdp_uuid;
2578 bdstr_t bdstr;
2579
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002580 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 -08002581
2582 sdp_uuid.len = MAX_UUID_SIZE;
2583 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2584
2585 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2586 bte_dm_remote_service_record_evt, TRUE);
2587
2588 return BT_STATUS_SUCCESS;
2589}
2590
2591void btif_dm_execute_service_request(UINT16 event, char *p_param)
2592{
2593 BOOLEAN b_enable = FALSE;
2594 bt_status_t status;
2595 if (event == BTIF_DM_ENABLE_SERVICE)
2596 {
2597 b_enable = TRUE;
2598 }
2599 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2600 if (status == BT_STATUS_SUCCESS)
2601 {
2602 bt_property_t property;
2603 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2604
2605 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2606 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2607 sizeof(local_uuids), local_uuids);
2608 btif_storage_get_adapter_property(&property);
2609 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2610 BT_STATUS_SUCCESS, 1, &property);
2611 }
2612 return;
2613}
2614
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002615void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2616 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2617{
2618 UINT8 yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2619 /* if local initiated:
2620 ** 1. set DD + MITM
2621 ** if remote initiated:
2622 ** 1. Copy over the auth_req from peer's io_rsp
2623 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2624 ** as a fallback set MITM+GB if peer had MITM set
2625 */
2626 UNUSED (bd_addr);
2627 UNUSED (p_io_cap);
2628 UNUSED (p_oob_data);
2629
2630
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002631 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002632 if(pairing_cb.is_local_initiated)
2633 {
2634 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2635 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2636 }
2637 else if (!is_orig)
2638 {
2639 /* peer initiated paring. They probably know what they want.
2640 ** Copy the mitm from peer device.
2641 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002642 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002643 __FUNCTION__, pairing_cb.auth_req);
2644 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2645
2646 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2647 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2648 *p_auth_req |= BTA_AUTH_SP_YES;
2649 }
2650 else if (yes_no_bit)
2651 {
2652 /* set the general bonding bit for stored device */
2653 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2654 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002655 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002656}
2657
2658void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2659 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2660{
2661 UNUSED (bd_addr);
2662 UNUSED (oob_data);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002663
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002664 if(auth_req & BTA_AUTH_BONDS)
2665 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002666 BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002667 pairing_cb.auth_req = auth_req;
2668 pairing_cb.io_cap = io_cap;
2669 }
2670}
2671
The Android Open Source Project5738f832012-12-12 16:00:35 -08002672#if (BTM_OOB_INCLUDED == TRUE)
2673void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data)
2674{
2675 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2676 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2677 {
2678 *p_oob_data = FALSE;
2679 }
2680 else
2681 {
2682 *p_oob_data = TRUE;
2683 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002684 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 -08002685}
2686#endif /* BTM_OOB_INCLUDED */
2687
2688#ifdef BTIF_DM_OOB_TEST
2689void btif_dm_load_local_oob(void)
2690{
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002691 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002692 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002693 BTIF_TRACE_DEBUG("btif_dm_load_local_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002694 if (prop_oob[0] != '3')
2695 {
2696#if (BTM_OOB_INCLUDED == TRUE)
2697 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2698 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2699 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002700 BTIF_TRACE_DEBUG("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002701 BTA_DmLocalOob();
2702 }
2703#else
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002704 BTIF_TRACE_ERROR("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002705#endif
2706 }
2707}
2708
2709void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2710{
2711 FILE *fp;
2712 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2713 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2714 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002715 char prop_oob[PROPERTY_VALUE_MAX];
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002716 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: valid=%d", valid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002717 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2718 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2719 valid)
2720 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002721 BTIF_TRACE_DEBUG("save local OOB data in memory");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002722 memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2723 memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2724 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002725 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002726 if (prop_oob[0] == '1')
2727 path = path_a;
2728 else if (prop_oob[0] == '2')
2729 path = path_b;
2730 if (path)
2731 {
2732 fp = fopen(path, "wb+");
2733 if (fp == NULL)
2734 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002735 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 -08002736 }
2737 else
2738 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002739 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 -08002740 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2741 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2742 fclose(fp);
2743 }
2744 }
2745 }
2746}
2747BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2748{
2749 char t[128];
2750 FILE *fp;
2751 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2752 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2753 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002754 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002755 BOOLEAN result = FALSE;
2756 bt_bdaddr_t bt_bd_addr;
2757 bdcpy(oob_cb.oob_bdaddr, bd_addr);
2758 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002759 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002760 if (prop_oob[0] == '1')
2761 path = path_b;
2762 else if (prop_oob[0] == '2')
2763 path = path_a;
2764 if (path)
2765 {
2766 fp = fopen(path, "rb");
2767 if (fp == NULL)
2768 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002769 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 -08002770 return FALSE;
2771 }
2772 else
2773 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002774 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob: read OOB data from %s",path);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002775 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2776 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2777 fclose(fp);
2778 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002779 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: TRUE");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002780 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2781 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2782 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002783 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002784 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2785 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
2786 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 -07002787 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: c = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002788 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2789 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
2790 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 -07002791 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: r = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002792 bdcpy(bt_bd_addr.address, bd_addr);
2793 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2794 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2795 result = TRUE;
2796 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002797 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob result=%d",result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002798 return result;
2799}
2800#endif /* BTIF_DM_OOB_TEST */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002801#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2802
2803static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2804{
2805 bt_bdaddr_t bd_addr;
2806 bt_bdname_t bd_name;
2807 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08002808 int dev_type;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002809
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002810 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002811
2812 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08002813 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
2814 {
2815 dev_type = BT_DEVICE_TYPE_BLE;
2816 }
2817 btif_dm_update_ble_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2818 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002819 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2820 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2821
2822 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2823 pairing_cb.is_ssp = FALSE;
2824 cod = COD_UNCLASSIFIED;
2825
2826 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2827 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2828 p_ssp_key_notif->passkey);
2829}
2830
2831/*******************************************************************************
2832**
2833** Function btif_dm_ble_auth_cmpl_evt
2834**
2835** Description Executes authentication complete event in btif context
2836**
2837** Returns void
2838**
2839*******************************************************************************/
2840static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2841{
2842 /* Save link key, if not temporary */
2843 bt_bdaddr_t bd_addr;
2844 bt_status_t status = BT_STATUS_FAIL;
2845 bt_bond_state_t state = BT_BOND_STATE_NONE;
2846
2847 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2848 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2849 {
2850 /* store keys */
2851 }
2852 if (p_auth_cmpl->success)
2853 {
2854 status = BT_STATUS_SUCCESS;
2855 state = BT_BOND_STATE_BONDED;
2856
2857 btif_dm_save_ble_bonding_keys();
2858 BTA_GATTC_Refresh(bd_addr.address);
2859 btif_dm_get_remote_services(&bd_addr);
2860 }
2861 else
2862 {
2863 /*Map the HCI fail reason to bt status */
2864 switch (p_auth_cmpl->fail_reason)
2865 {
Priti Aghera156c52b2014-07-09 14:58:19 -07002866 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
2867 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
2868 btif_dm_remove_ble_bonding_keys();
2869 status = BT_STATUS_AUTH_FAILURE;
2870 break;
2871 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
2872 status = BT_STATUS_AUTH_REJECTED;
2873 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002874 default:
Andre Eisenbachca22ac42013-02-13 17:02:11 +09002875 btif_dm_remove_ble_bonding_keys();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002876 status = BT_STATUS_FAIL;
2877 break;
2878 }
2879 }
2880 bond_state_changed(status, &bd_addr, state);
2881}
2882
2883
2884
2885void btif_dm_load_ble_local_keys(void)
2886{
2887 bt_status_t bt_status;
2888
2889 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2890
2891 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2892 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2893 {
2894 ble_local_key_cb.is_er_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002895 BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002896 }
2897
2898 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
2899 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
2900 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
2901 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
2902 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
2903 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
2904 {
2905 ble_local_key_cb.is_id_keys_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002906 BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002907 }
2908
2909}
2910void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
2911 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
2912{
2913 if (ble_local_key_cb.is_er_rcvd )
2914 {
2915 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2916 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2917 }
2918
2919 if (ble_local_key_cb.is_id_keys_rcvd)
2920 {
2921 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
2922 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
2923 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
2924 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
2925 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002926 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002927}
2928
2929void btif_dm_save_ble_bonding_keys(void)
2930{
2931
2932 bt_bdaddr_t bd_addr;
2933
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002934 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002935
2936 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2937
2938 if (pairing_cb.ble.is_penc_key_rcvd)
2939 {
2940 btif_storage_add_ble_bonding_key(&bd_addr,
2941 (char *) &pairing_cb.ble.penc_key,
2942 BTIF_DM_LE_KEY_PENC,
2943 sizeof(btif_dm_ble_penc_keys_t));
2944 }
2945
2946 if (pairing_cb.ble.is_pid_key_rcvd)
2947 {
2948 btif_storage_add_ble_bonding_key(&bd_addr,
Andre Eisenbach5e808462014-10-21 12:37:53 -07002949 (char *) &pairing_cb.ble.pid_key,
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002950 BTIF_DM_LE_KEY_PID,
Andre Eisenbach5e808462014-10-21 12:37:53 -07002951 sizeof(btif_dm_ble_pid_keys_t));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002952 }
2953
2954
2955 if (pairing_cb.ble.is_pcsrk_key_rcvd)
2956 {
2957 btif_storage_add_ble_bonding_key(&bd_addr,
2958 (char *) &pairing_cb.ble.pcsrk_key,
2959 BTIF_DM_LE_KEY_PCSRK,
2960 sizeof(btif_dm_ble_pcsrk_keys_t));
2961 }
2962
2963
2964 if (pairing_cb.ble.is_lenc_key_rcvd)
2965 {
2966 btif_storage_add_ble_bonding_key(&bd_addr,
2967 (char *) &pairing_cb.ble.lenc_key,
2968 BTIF_DM_LE_KEY_LENC,
2969 sizeof(btif_dm_ble_lenc_keys_t));
2970 }
2971
2972 if (pairing_cb.ble.is_lcsrk_key_rcvd)
2973 {
2974 btif_storage_add_ble_bonding_key(&bd_addr,
2975 (char *) &pairing_cb.ble.lcsrk_key,
2976 BTIF_DM_LE_KEY_LCSRK,
2977 sizeof(btif_dm_ble_lcsrk_keys_t));
2978 }
2979
2980}
2981
2982
2983void btif_dm_remove_ble_bonding_keys(void)
2984{
2985 bt_bdaddr_t bd_addr;
2986
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002987 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002988
2989 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2990 btif_storage_remove_ble_bonding_keys(&bd_addr);
2991}
2992
2993
2994/*******************************************************************************
2995**
2996** Function btif_dm_ble_sec_req_evt
2997**
2998** Description Eprocess security request event in btif context
2999**
3000** Returns void
3001**
3002*******************************************************************************/
3003void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
3004{
3005 bt_bdaddr_t bd_addr;
3006 bt_bdname_t bd_name;
3007 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08003008 int dev_type;
3009
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003010 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003011
3012 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3013 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003014 BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003015 return;
3016 }
3017
3018 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08003019 if (!btif_get_device_type(p_ble_req->bd_addr, &dev_type))
3020 {
3021 dev_type = BT_DEVICE_TYPE_BLE;
3022 }
3023 btif_dm_update_ble_remote_properties(p_ble_req->bd_addr, p_ble_req->bd_name,
3024 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003025
3026 bdcpy(bd_addr.address, p_ble_req->bd_addr);
3027 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
3028
3029 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3030
Andre Eisenbach89363762015-01-26 13:49:36 -08003031 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003032 pairing_cb.is_le_only = TRUE;
3033 pairing_cb.is_ssp = TRUE;
3034
3035 cod = COD_UNCLASSIFIED;
3036
3037 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
3038 BT_SSP_VARIANT_CONSENT, 0);
3039}
3040
3041
3042
3043/*******************************************************************************
3044**
3045** Function btif_dm_ble_passkey_req_evt
3046**
3047** Description Executes pin request event in btif context
3048**
3049** Returns void
3050**
3051*******************************************************************************/
3052static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
3053{
3054 bt_bdaddr_t bd_addr;
3055 bt_bdname_t bd_name;
3056 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08003057 int dev_type;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003058
3059 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08003060 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
3061 {
3062 dev_type = BT_DEVICE_TYPE_BLE;
3063 }
3064 btif_dm_update_ble_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,
3065 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003066
3067 bdcpy(bd_addr.address, p_pin_req->bd_addr);
3068 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
3069
3070 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3071 pairing_cb.is_le_only = TRUE;
3072
3073 cod = COD_UNCLASSIFIED;
3074
3075 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
3076 &bd_addr, &bd_name, cod);
3077}
3078
3079
3080void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
3081 tBT_DEVICE_TYPE dev_type)
3082{
3083 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
3084}
3085
3086static void btif_dm_ble_tx_test_cback(void *p)
3087{
3088 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
3089 (char *)p, 1, NULL);
3090}
3091
3092static void btif_dm_ble_rx_test_cback(void *p)
3093{
3094 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
3095 (char *)p, 1, NULL);
3096}
3097
3098static void btif_dm_ble_test_end_cback(void *p)
3099{
3100 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
3101 (char *)p, 3, NULL);
3102}
3103/*******************************************************************************
3104**
3105** Function btif_le_test_mode
3106**
3107** Description Sends a HCI BLE Test command to the Controller
3108**
3109** Returns BT_STATUS_SUCCESS on success
3110**
3111*******************************************************************************/
3112bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
3113{
3114 switch (opcode) {
3115 case HCI_BLE_TRANSMITTER_TEST:
3116 if (len != 3) return BT_STATUS_PARM_INVALID;
3117 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
3118 break;
3119 case HCI_BLE_RECEIVER_TEST:
3120 if (len != 1) return BT_STATUS_PARM_INVALID;
3121 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
3122 break;
3123 case HCI_BLE_TEST_END:
3124 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
3125 break;
3126 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003127 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003128 return BT_STATUS_UNSUPPORTED;
3129 }
3130 return BT_STATUS_SUCCESS;
3131}
3132
3133#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08003134
3135void btif_dm_on_disable()
3136{
3137 /* cancel any pending pairing requests */
3138 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3139 {
3140 bt_bdaddr_t bd_addr;
3141
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003142 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003143 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3144 btif_dm_cancel_bond(&bd_addr);
3145 }
3146}
Matthew Xie1e5109b2012-11-09 18:26:26 -08003147
Satya Callojie5ba8842014-07-03 17:18:02 -07003148/*******************************************************************************
3149**
3150** Function btif_dm_read_energy_info
3151**
3152** Description Reads the energy info from controller
3153**
3154** Returns void
3155**
3156*******************************************************************************/
3157void btif_dm_read_energy_info()
3158{
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003159#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07003160 BTA_DmBleGetEnergyInfo(bta_energy_info_cb);
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003161#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07003162}
3163
Matthew Xie1e5109b2012-11-09 18:26:26 -08003164static char* btif_get_default_local_name() {
3165 if (btif_default_local_name[0] == '\0')
3166 {
3167 int max_len = sizeof(btif_default_local_name) - 1;
3168 if (BTM_DEF_LOCAL_NAME[0] != '\0')
3169 {
3170 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
3171 }
3172 else
3173 {
3174 char prop_model[PROPERTY_VALUE_MAX];
3175 property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
3176 strncpy(btif_default_local_name, prop_model, max_len);
3177 }
3178 btif_default_local_name[max_len] = '\0';
3179 }
3180 return btif_default_local_name;
3181}