blob: 7a7ec01a36d3446a898c2e46d191fb698cee192c [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
Scott James Remnant933926c2015-04-02 15:22:14 -070030#include <signal.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080031#include <stdio.h>
32#include <stdlib.h>
Scott James Remnant933926c2015-04-02 15:22:14 -070033#include <string.h>
34#include <sys/types.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080035#include <unistd.h>
36
37#include <hardware/bluetooth.h>
38
The Android Open Source Project5738f832012-12-12 16:00:35 -080039#include <cutils/properties.h>
40#include "gki.h"
41#include "btu.h"
Sharvil Nanavati95b74f22015-03-12 15:55:21 -070042#include "btcore/include/bdaddr.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080043#include "bta_api.h"
44#include "btif_api.h"
45#include "btif_util.h"
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080046#include "btif_dm.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080047#include "btif_storage.h"
48#include "btif_hh.h"
49#include "btif_config.h"
Kim Schulz8372aa52015-03-25 10:39:40 +010050#include "btif_sdp.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080051
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080052#include "bta_gatt_api.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080053#include "include/stack_config.h"
Chris Mantonf8027002015-03-12 09:22:48 -070054
Sharvil Nanavati44802762014-12-23 23:08:58 -080055#include "osi/include/log.h"
Andre Eisenbach31a64002014-10-14 14:29:19 -070056
57/******************************************************************************
58** Device specific workarounds
59******************************************************************************/
60
61/**
62 * The devices below have proven problematic during the pairing process, often
63 * requiring multiple retries to complete pairing. To avoid degrading the user
64 * experience for other devices, explicitely blacklist troubled devices here.
65 */
66static const UINT8 blacklist_pairing_retries[][3] = {
67 {0x9C, 0xDF, 0x03} // BMW car kits (Harman/Becker)
68};
69
70BOOLEAN blacklistPairingRetries(BD_ADDR bd_addr)
71{
72 const unsigned blacklist_size = sizeof(blacklist_pairing_retries)
73 / sizeof(blacklist_pairing_retries[0]);
74 for (unsigned i = 0; i != blacklist_size; ++i)
75 {
76 if (blacklist_pairing_retries[i][0] == bd_addr[0] &&
77 blacklist_pairing_retries[i][1] == bd_addr[1] &&
78 blacklist_pairing_retries[i][2] == bd_addr[2])
79 return TRUE;
80 }
81 return FALSE;
82}
83
The Android Open Source Project5738f832012-12-12 16:00:35 -080084/******************************************************************************
85** Constants & Macros
86******************************************************************************/
87
88#define COD_UNCLASSIFIED ((0x1F) << 8)
Priti Agheraebb1d752012-11-27 18:03:22 -080089#define COD_HID_KEYBOARD 0x0540
90#define COD_HID_POINTING 0x0580
91#define COD_HID_COMBO 0x05C0
92#define COD_HID_MAJOR 0x0500
93#define COD_AV_HEADSETS 0x0404
94#define COD_AV_HANDSFREE 0x0408
95#define COD_AV_HEADPHONES 0x0418
96#define COD_AV_PORTABLE_AUDIO 0x041C
97#define COD_AV_HIFI_AUDIO 0x0428
The Android Open Source Project5738f832012-12-12 16:00:35 -080098
99
100#define BTIF_DM_DEFAULT_INQ_MAX_RESULTS 0
101#define BTIF_DM_DEFAULT_INQ_MAX_DURATION 10
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700102#define BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING 2
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800103
Andre Eisenbach31a64002014-10-14 14:29:19 -0700104#define NUM_TIMEOUT_RETRIES 5
105
Matthew Xie1e5109b2012-11-09 18:26:26 -0800106#define PROPERTY_PRODUCT_MODEL "ro.product.model"
Matthew Xiea30d95a2013-09-18 12:30:36 -0700107#define DEFAULT_LOCAL_NAME_MAX 31
Matthew Xie1e5109b2012-11-09 18:26:26 -0800108#if (DEFAULT_LOCAL_NAME_MAX > BTM_MAX_LOC_BD_NAME_LEN)
109 #error "default btif local name size exceeds stack supported length"
110#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800111
Matthew Xie7f3e4292013-09-30 12:44:10 -0700112#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
113#define BTIF_DM_INTERLEAVE_DURATION_BR_ONE 2
114#define BTIF_DM_INTERLEAVE_DURATION_LE_ONE 2
115#define BTIF_DM_INTERLEAVE_DURATION_BR_TWO 3
116#define BTIF_DM_INTERLEAVE_DURATION_LE_TWO 4
117#endif
118
Priti Agherac0edf9f2014-06-26 11:23:51 -0700119#define MAX_SDP_BL_ENTRIES 3
120
Andre Eisenbach89363762015-01-26 13:49:36 -0800121#define BOND_TYPE_UNKNOWN 0
122#define BOND_TYPE_PERSISTENT 1
123#define BOND_TYPE_TEMPORARY 2
124
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800125#define ENCRYPTED_BREDR 2
126#define ENCRYPTED_LE 4
127
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800128typedef struct
129{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800130 bt_bond_state_t state;
131 BD_ADDR bd_addr;
Andre Eisenbach89363762015-01-26 13:49:36 -0800132 UINT8 bond_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800133 UINT8 pin_code_len;
134 UINT8 is_ssp;
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -0700135 UINT8 auth_req;
136 UINT8 io_cap;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800137 UINT8 autopair_attempts;
Andre Eisenbach31a64002014-10-14 14:29:19 -0700138 UINT8 timeout_retries;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800139 UINT8 is_local_initiated;
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700140 UINT8 sdp_attempts;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800141#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
142 BOOLEAN is_le_only;
143 btif_dm_ble_cb_t ble;
144#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800145} btif_dm_pairing_cb_t;
146
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800147
148typedef struct
149{
150 UINT8 ir[BT_OCTET16_LEN];
151 UINT8 irk[BT_OCTET16_LEN];
152 UINT8 dhk[BT_OCTET16_LEN];
153}btif_dm_local_key_id_t;
154
155typedef struct
156{
157 BOOLEAN is_er_rcvd;
158 UINT8 er[BT_OCTET16_LEN];
159 BOOLEAN is_id_keys_rcvd;
160 btif_dm_local_key_id_t id_keys; /* ID kyes */
161
162}btif_dm_local_key_cb_t;
163
164typedef struct
165{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800166 BD_ADDR bd_addr;
167 BD_NAME bd_name;
168} btif_dm_remote_name_t;
169
170typedef struct
171{
172 BT_OCTET16 sp_c;
173 BT_OCTET16 sp_r;
174 BD_ADDR oob_bdaddr; /* peer bdaddr*/
175} btif_dm_oob_cb_t;
Satya Callojie5ba8842014-07-03 17:18:02 -0700176
177typedef struct
178{
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700179 bt_bdaddr_t bdaddr;
180 UINT8 transport; /* 0=Unknown, 1=BR/EDR, 2=LE */
181} btif_dm_create_bond_cb_t;
182
183typedef struct
184{
Satya Callojie5ba8842014-07-03 17:18:02 -0700185 uint8_t status;
186 uint8_t ctrl_state;
187 uint64_t tx_time;
188 uint64_t rx_time;
189 uint64_t idle_time;
190 uint64_t energy_used;
191} btif_activity_energy_info_cb_t;
192
Priti Agherac0edf9f2014-06-26 11:23:51 -0700193typedef struct
194{
195 unsigned int manufact_id;
196}skip_sdp_entry_t;
197
The Android Open Source Project5738f832012-12-12 16:00:35 -0800198#define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id))
199
Priti Agherac0edf9f2014-06-26 11:23:51 -0700200#define MAX_SDP_BL_ENTRIES 3
201#define UUID_HUMAN_INTERFACE_DEVICE "00001124-0000-1000-8000-00805f9b34fb"
202
203static skip_sdp_entry_t sdp_blacklist[] = {{76}}; //Apple Mouse and Keyboard
204
205
The Android Open Source Project5738f832012-12-12 16:00:35 -0800206/* This flag will be true if HCI_Inquiry is in progress */
207static BOOLEAN btif_dm_inquiry_in_progress = FALSE;
208
Priti Agherac0edf9f2014-06-26 11:23:51 -0700209
210
Matthew Xie1e5109b2012-11-09 18:26:26 -0800211/************************************************************************************
212** Static variables
213************************************************************************************/
214static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'};
215
The Android Open Source Project5738f832012-12-12 16:00:35 -0800216/******************************************************************************
217** Static functions
218******************************************************************************/
219static btif_dm_pairing_cb_t pairing_cb;
220static btif_dm_oob_cb_t oob_cb;
221static void btif_dm_generic_evt(UINT16 event, char* p_param);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700222static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800223static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name);
224static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
225 DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800226#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
227static btif_dm_local_key_cb_t ble_local_key_cb;
228static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif);
229static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl);
230static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req);
231#endif
Satya Calloji6e2d9db2014-07-08 16:18:58 -0700232
233static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
234 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
235 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status);
236
Matthew Xie1e5109b2012-11-09 18:26:26 -0800237static char* btif_get_default_local_name();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800238/******************************************************************************
239** Externs
240******************************************************************************/
241extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
242extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
243extern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
244extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
Hemant Gupta10256872013-08-19 18:33:01 +0530245extern bt_status_t btif_hf_client_execute_service(BOOLEAN b_enable);
Kim Schulz8372aa52015-03-25 10:39:40 +0100246extern bt_status_t btif_sdp_execute_service(BOOLEAN b_enable);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800247extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700248extern 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 -0800249
250
251/******************************************************************************
252** Functions
253******************************************************************************/
254
255bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
256 BOOLEAN b_enable)
257{
Kim Schulz8372aa52015-03-25 10:39:40 +0100258 BTIF_TRACE_DEBUG("%s service_id: %d", __FUNCTION__, service_id);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800259 /* Check the service_ID and invoke the profile's BT state changed API */
260 switch (service_id)
261 {
262 case BTA_HFP_SERVICE_ID:
263 case BTA_HSP_SERVICE_ID:
264 {
265 btif_hf_execute_service(b_enable);
266 }break;
Sharvil Nanavati9609cee2014-10-15 18:30:49 -0700267 case BTA_A2DP_SOURCE_SERVICE_ID:
The Android Open Source Project5738f832012-12-12 16:00:35 -0800268 {
269 btif_av_execute_service(b_enable);
270 }break;
271 case BTA_HID_SERVICE_ID:
272 {
273 btif_hh_execute_service(b_enable);
274 }break;
Hemant Gupta10256872013-08-19 18:33:01 +0530275 case BTA_HFP_HS_SERVICE_ID:
276 {
277 btif_hf_client_execute_service(b_enable);
278 }break;
Kim Schulz8372aa52015-03-25 10:39:40 +0100279 case BTA_SDP_SERVICE_ID:
Hemant Gupta2dc99992014-04-18 12:54:08 +0530280 {
Kim Schulz8372aa52015-03-25 10:39:40 +0100281 btif_sdp_execute_service(b_enable);
Hemant Gupta2dc99992014-04-18 12:54:08 +0530282 }break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800283 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700284 BTIF_TRACE_ERROR("%s: Unknown service being enabled", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800285 return BT_STATUS_FAIL;
286 }
287 return BT_STATUS_SUCCESS;
288}
289
290/*******************************************************************************
291**
292** Function check_eir_remote_name
293**
294** Description Check if remote name is in the EIR data
295**
296** Returns TRUE if remote name found
297** Populate p_remote_name, if provided and remote name found
298**
299*******************************************************************************/
300static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
301 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
302{
303 UINT8 *p_eir_remote_name = NULL;
304 UINT8 remote_name_len = 0;
305
306 /* Check EIR for remote name and services */
307 if (p_search_data->inq_res.p_eir)
308 {
Zach Johnsona50fc882014-10-30 21:02:58 -0700309 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800310 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
311 if (!p_eir_remote_name)
312 {
Zach Johnsona50fc882014-10-30 21:02:58 -0700313 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800314 BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
315 }
316
317 if (p_eir_remote_name)
318 {
319 if (remote_name_len > BD_NAME_LEN)
320 remote_name_len = BD_NAME_LEN;
321
322 if (p_remote_name && p_remote_name_len)
323 {
324 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
325 *(p_remote_name + remote_name_len) = 0;
326 *p_remote_name_len = remote_name_len;
327 }
328
329 return TRUE;
330 }
331 }
332
333 return FALSE;
334
335}
336
337/*******************************************************************************
338**
339** Function check_cached_remote_name
340**
341** Description Check if remote name is in the NVRAM cache
342**
343** Returns TRUE if remote name found
344** Populate p_remote_name, if provided and remote name found
345**
346*******************************************************************************/
347static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
348 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
349{
350 bt_bdname_t bdname;
351 bt_bdaddr_t remote_bdaddr;
352 bt_property_t prop_name;
353
354 /* check if we already have it in our btif_storage cache */
355 bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
356 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
357 sizeof(bt_bdname_t), &bdname);
358 if (btif_storage_get_remote_device_property(
359 &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
360 {
361 if (p_remote_name && p_remote_name_len)
362 {
363 strcpy((char *)p_remote_name, (char *)bdname.name);
364 *p_remote_name_len = strlen((char *)p_remote_name);
365 }
366 return TRUE;
367 }
368
369 return FALSE;
370}
371
372BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
373{
374 uint32_t remote_cod;
375 bt_property_t prop_name;
376
377 /* check if we already have it in our btif_storage cache */
378 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
379 sizeof(uint32_t), &remote_cod);
380 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
381 {
Chris Mantonf8027002015-03-12 09:22:48 -0700382 LOG_INFO("%s remote_cod = 0x%08x cod = 0x%08x", __func__, remote_cod, cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800383 if ((remote_cod & 0x7ff) == cod)
384 return TRUE;
385 }
386
387 return FALSE;
388}
389
Priti Agheraebb1d752012-11-27 18:03:22 -0800390BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
391{
392 uint32_t remote_cod;
393 bt_property_t prop_name;
394
395 /* check if we already have it in our btif_storage cache */
396 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
397 sizeof(uint32_t), &remote_cod);
398 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
399 &prop_name) == BT_STATUS_SUCCESS)
400 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700401 BTIF_TRACE_DEBUG("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
Priti Agheraebb1d752012-11-27 18:03:22 -0800402 if ((remote_cod & 0x700) == cod)
403 return TRUE;
404 }
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700405 return FALSE;
406}
Priti Agheraebb1d752012-11-27 18:03:22 -0800407
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700408BOOLEAN check_hid_le(const bt_bdaddr_t *remote_bdaddr)
409{
410 uint32_t remote_dev_type;
411 bt_property_t prop_name;
412
413 /* check if we already have it in our btif_storage cache */
414 BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE,
415 sizeof(uint32_t), &remote_dev_type);
416 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
417 &prop_name) == BT_STATUS_SUCCESS)
418 {
419 if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE)
420 {
421 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -0700422 bdaddr_to_string(remote_bdaddr, bdstr, sizeof(bdstr));
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700423 if(btif_config_exist(bdstr, "HidAppId"))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700424 return TRUE;
425 }
426 }
Priti Agheraebb1d752012-11-27 18:03:22 -0800427 return FALSE;
428}
429
Priti Agherac0edf9f2014-06-26 11:23:51 -0700430/*****************************************************************************
431**
432** Function check_sdp_bl
433**
434** Description Checks if a given device is blacklisted to skip sdp
435**
436** Parameters skip_sdp_entry
437**
438** Returns TRUE if the device is present in blacklist, else FALSE
439**
440*******************************************************************************/
441BOOLEAN check_sdp_bl(const bt_bdaddr_t *remote_bdaddr)
442{
443 UINT8 i = 0;
444 UINT16 manufacturer = 0;
445 UINT8 lmp_ver = 0;
446 UINT16 lmp_subver = 0;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700447 bt_property_t prop_name;
448 bt_remote_version_t info;
449 bt_status_t status;
450
451
452 if (remote_bdaddr == NULL)
453 return FALSE;
454
455/* fetch additional info about remote device used in iop query */
Sharvil Nanavatif1c764f2015-02-23 17:31:48 -0800456 BTM_ReadRemoteVersion(*(BD_ADDR*)remote_bdaddr, &lmp_ver,
Priti Agherac0edf9f2014-06-26 11:23:51 -0700457 &manufacturer, &lmp_subver);
458
459
460
461 /* if not available yet, try fetching from config database */
462 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO,
463 sizeof(bt_remote_version_t), &info);
464
465 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
466 &prop_name) != BT_STATUS_SUCCESS)
467 {
468
469 return FALSE;
470 }
471 manufacturer = info.manufacturer;
472
473 for (int i = 0; i < MAX_SDP_BL_ENTRIES; i++)
474 {
475 if (manufacturer == sdp_blacklist[i].manufact_id)
476 return TRUE;
477 }
478 return FALSE;
479}
480
481
The Android Open Source Project5738f832012-12-12 16:00:35 -0800482static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
483{
484 /* Send bonding state only once - based on outgoing/incoming we may receive duplicates */
485 if ( (pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING) )
486 return;
487
Andre Eisenbach89363762015-01-26 13:49:36 -0800488 if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800489 {
490 state = BT_BOND_STATE_NONE;
491 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700492 BTIF_TRACE_DEBUG("%s: state=%d prev_state=%d", __FUNCTION__, state, pairing_cb.state);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800493
494 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
495
496 if (state == BT_BOND_STATE_BONDING)
497 {
498 pairing_cb.state = state;
499 bdcpy(pairing_cb.bd_addr, bd_addr->address);
500 }
501 else
502 {
503 memset(&pairing_cb, 0, sizeof(pairing_cb));
504 }
505
506}
507
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800508/* store remote version in bt config to always have access
509 to it post pairing*/
510static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
511{
512 bt_property_t property;
513 UINT8 lmp_ver = 0;
514 UINT16 lmp_subver = 0;
515 UINT16 mfct_set = 0;
516 tBTM_STATUS btm_status;
517 bt_remote_version_t info;
518 bt_status_t status;
519 bdstr_t bdstr;
520
521 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
522 &mfct_set, &lmp_subver);
523
Sharvil Nanavati44802762014-12-23 23:08:58 -0800524 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 -0800525 lmp_ver, mfct_set, lmp_subver);
526
527 if (btm_status == BTM_SUCCESS)
528 {
529 /* always update cache to ensure we have availability whenever BTM API
530 is not populated */
531 info.manufacturer = mfct_set;
532 info.sub_ver = lmp_subver;
533 info.version = lmp_ver;
534 BTIF_STORAGE_FILL_PROPERTY(&property,
535 BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
536 &info);
537 status = btif_storage_set_remote_device_property(p_bd, &property);
538 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
539 }
540}
541
The Android Open Source Project5738f832012-12-12 16:00:35 -0800542
543static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
544 DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
545{
546 int num_properties = 0;
547 bt_property_t properties[3];
548 bt_bdaddr_t bdaddr;
549 bt_status_t status;
550 UINT32 cod;
551 bt_device_type_t dev_type;
552
553 memset(properties, 0, sizeof(properties));
554 bdcpy(bdaddr.address, bd_addr);
555
556 /* remote name */
557 if (strlen((const char *) bd_name))
558 {
559 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
560 BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
561 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
562 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
563 num_properties++;
564 }
565
566 /* class of device */
567 cod = devclass2uint(dev_class);
Chris Mantonf8027002015-03-12 09:22:48 -0700568 BTIF_TRACE_DEBUG("%s cod is 0x%06x", __func__, cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800569 if ( cod == 0) {
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530570 /* Try to retrieve cod from storage */
Chris Mantonf8027002015-03-12 09:22:48 -0700571 BTIF_TRACE_DEBUG("%s cod is 0, checking cod from storage", __func__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530572 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
573 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
574 status = btif_storage_get_remote_device_property(&bdaddr, &properties[num_properties]);
Chris Mantonf8027002015-03-12 09:22:48 -0700575 BTIF_TRACE_DEBUG("%s cod retrieved from storage is 0x%06x", __func__, cod);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530576 if ( cod == 0) {
Chris Mantonf8027002015-03-12 09:22:48 -0700577 BTIF_TRACE_DEBUG("%s cod is again 0, set as unclassified", __func__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530578 cod = COD_UNCLASSIFIED;
579 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800580 }
581
582 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
583 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
584 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
585 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
586 num_properties++;
587
588 /* device type */
589 dev_type = device_type;
590 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
591 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
592 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
593 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
594 num_properties++;
595
596 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
597 status, &bdaddr, num_properties, properties);
598}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800599
600/*******************************************************************************
601**
602** Function btif_dm_cb_hid_remote_name
603**
604** Description Remote name callback for HID device. Called in btif context
605** Special handling for HID devices
606**
607** Returns void
608**
609*******************************************************************************/
610static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
611{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700612 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 -0800613 if (pairing_cb.state == BT_BOND_STATE_BONDING)
614 {
615 bt_bdaddr_t remote_bd;
616
617 bdcpy(remote_bd.address, pairing_cb.bd_addr);
618
619 if (p_remote_name->status == BTM_SUCCESS)
620 {
621 bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
622 }
623 else
624 bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
625 }
626}
627
The Android Open Source Project5738f832012-12-12 16:00:35 -0800628/*******************************************************************************
629**
630** Function btif_dm_cb_create_bond
631**
632** Description Create bond initiated from the BTIF thread context
633** Special handling for HID devices
634**
635** Returns void
636**
637*******************************************************************************/
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700638static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800639{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800640 BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800641 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800642
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800643#if BLE_INCLUDED == TRUE
644 int device_type;
645 int addr_type;
646 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -0700647 bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr));
Matthew Xie64c54792014-09-16 00:55:03 -0700648 if (transport == BT_TRANSPORT_LE)
649 {
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700650 if (!btif_config_get_int((char const *)&bdstr,"DevType", &device_type))
Matthew Xie64c54792014-09-16 00:55:03 -0700651 {
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700652 btif_config_set_int(bdstr, "DevType", BT_DEVICE_TYPE_BLE);
Matthew Xie64c54792014-09-16 00:55:03 -0700653 }
654 if (btif_storage_get_remote_addr_type(bd_addr, &addr_type) != BT_STATUS_SUCCESS)
655 {
656 btif_storage_set_remote_addr_type(bd_addr, BLE_ADDR_PUBLIC);
657 }
658 }
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700659 if((btif_config_get_int((char const *)&bdstr,"DevType", &device_type) &&
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800660 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
lungtsai_lin3baff792014-08-29 13:47:47 +0800661 (device_type == BT_DEVICE_TYPE_BLE)) || (transport == BT_TRANSPORT_LE))
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800662 {
663 BTA_DmAddBleDevice(bd_addr->address, addr_type, BT_DEVICE_TYPE_BLE);
664 }
665#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800666
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800667#if BLE_INCLUDED == TRUE
668 if(is_hid && device_type != BT_DEVICE_TYPE_BLE)
669#else
670 if(is_hid)
671#endif
672 {
673 int status;
674 status = btif_hh_connect(bd_addr);
675 if(status != BT_STATUS_SUCCESS)
676 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800677 }
678 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800679 {
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700680 BTA_DmBondByTransport((UINT8 *)bd_addr->address, transport);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800681 }
682 /* Track originator of bond creation */
683 pairing_cb.is_local_initiated = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800684
685}
686
687/*******************************************************************************
688**
689** Function btif_dm_cb_remove_bond
690**
691** Description remove bond initiated from the BTIF thread context
692** Special handling for HID devices
693**
694** Returns void
695**
696*******************************************************************************/
697void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
698{
699 bdstr_t bdstr;
700 /*special handling for HID devices */
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700701 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there
702 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
703#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
704 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
705#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800706 {
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700707 BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800708 }
709}
710
711/*******************************************************************************
712**
Andre Eisenbach249f6002014-06-18 12:20:37 -0700713** Function btif_dm_get_connection_state
714**
715** Description Returns whether the remote device is currently connected
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800716** and whether encryption is active for the connection
Andre Eisenbach249f6002014-06-18 12:20:37 -0700717**
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800718** Returns 0 if not connected; 1 if connected and > 1 if connection is
719** encrypted
Andre Eisenbach249f6002014-06-18 12:20:37 -0700720**
721*******************************************************************************/
722uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr)
723{
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800724 uint8_t *bda = (uint8_t*)bd_addr->address;
725 uint16_t rc = BTA_DmGetConnectionState(bda);
726
727 if (rc != 0)
728 {
729 uint8_t flags = 0;
730
731 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_BR_EDR);
732 BTIF_TRACE_DEBUG("%s: security flags (BR/EDR)=0x%02x", __FUNCTION__, flags);
733 if (flags & BTM_SEC_FLAG_ENCRYPTED)
734 rc |= ENCRYPTED_BREDR;
735
736 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_LE);
737 BTIF_TRACE_DEBUG("%s: security flags (LE)=0x%02x", __FUNCTION__, flags);
738 if (flags & BTM_SEC_FLAG_ENCRYPTED)
739 rc |= ENCRYPTED_LE;
740 }
741
742 return rc;
Andre Eisenbach249f6002014-06-18 12:20:37 -0700743}
744
745/*******************************************************************************
746**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800747** Function search_devices_copy_cb
748**
749** Description Deep copy callback for search devices event
750**
751** Returns void
752**
753*******************************************************************************/
754static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
755{
756 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
757 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
758
759 if (!p_src)
760 return;
761
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700762 BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800763 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
764 switch (event)
765 {
766 case BTA_DM_INQ_RES_EVT:
767 {
768 if (p_src_data->inq_res.p_eir)
769 {
770 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
771 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
772 }
773 }
774 break;
775
776 case BTA_DM_DISC_RES_EVT:
777 {
778 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
779 {
780 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
781 memcpy(p_dest_data->disc_res.p_raw_data,
782 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
783 }
784 }
785 break;
786 }
787}
788
789static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
790{
791 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
792 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
793
794 if (!p_src)
795 return;
796 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
797 switch (event)
798 {
799 case BTA_DM_DISC_RES_EVT:
800 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530801 if (p_src_data->disc_res.result == BTA_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800802 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530803 if (p_src_data->disc_res.num_uuids > 0)
804 {
805 p_dest_data->disc_res.p_uuid_list =
806 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
807 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
808 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
809 GKI_freebuf(p_src_data->disc_res.p_uuid_list);
810 }
811 if (p_src_data->disc_res.p_raw_data != NULL)
812 {
813 GKI_freebuf(p_src_data->disc_res.p_raw_data);
814 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800815 }
816 } break;
817 }
818}
819/******************************************************************************
820**
821** BTIF DM callback events
822**
823*****************************************************************************/
824
825/*******************************************************************************
826**
827** Function btif_dm_pin_req_evt
828**
829** Description Executes pin request event in btif context
830**
831** Returns void
832**
833*******************************************************************************/
834static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
835{
836 bt_bdaddr_t bd_addr;
837 bt_bdname_t bd_name;
838 UINT32 cod;
839 bt_pin_code_t pin_code;
Matthew Xie86f97ed2014-11-10 10:24:46 -0800840 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800841
842 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -0800843 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
844 {
845 dev_type = BT_DEVICE_TYPE_BREDR;
846 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800847 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -0800848 p_pin_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800849
850 bdcpy(bd_addr.address, p_pin_req->bd_addr);
851 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
852
853 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
854
855 cod = devclass2uint(p_pin_req->dev_class);
856
Chris Mantonf8027002015-03-12 09:22:48 -0700857 if (cod == 0) {
858 BTIF_TRACE_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800859 cod = COD_UNCLASSIFIED;
860 }
861
862 /* check for auto pair possiblity only if bond was initiated by local device */
863 if (pairing_cb.is_local_initiated)
864 {
865 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
866 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
867 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
868 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
869 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
870 check_cod(&bd_addr, COD_HID_POINTING))
871 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700872 BTIF_TRACE_DEBUG("%s()cod matches for auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800873 /* Check if this device can be auto paired */
874 if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) &&
875 (pairing_cb.autopair_attempts == 0))
876 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700877 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800878 pin_code.pin[0] = 0x30;
879 pin_code.pin[1] = 0x30;
880 pin_code.pin[2] = 0x30;
881 pin_code.pin[3] = 0x30;
882
883 pairing_cb.autopair_attempts++;
884 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
885 return;
886 }
887 }
888 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
889 check_cod(&bd_addr, COD_HID_COMBO))
890 {
891 if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) &&
892 (pairing_cb.autopair_attempts == 0))
893 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700894 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800895 pin_code.pin[0] = 0x30;
896 pin_code.pin[1] = 0x30;
897 pin_code.pin[2] = 0x30;
898 pin_code.pin[3] = 0x30;
899
900 pairing_cb.autopair_attempts++;
901 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
902 return;
903 }
904 }
905 }
906 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
907 &bd_addr, &bd_name, cod);
908}
909
910/*******************************************************************************
911**
912** Function btif_dm_ssp_cfm_req_evt
913**
914** Description Executes SSP confirm request event in btif context
915**
916** Returns void
917**
918*******************************************************************************/
919static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
920{
921 bt_bdaddr_t bd_addr;
922 bt_bdname_t bd_name;
923 UINT32 cod;
924 BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
Matthew Xie86f97ed2014-11-10 10:24:46 -0800925 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800926
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700927 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800928
929 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -0800930 if (!btif_get_device_type(p_ssp_cfm_req->bd_addr, &dev_type))
931 {
932 dev_type = BT_DEVICE_TYPE_BREDR;
933 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800934 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -0800935 p_ssp_cfm_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800936
937 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
938 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
939
940 /* Set the pairing_cb based on the local & remote authentication requirements */
941 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
942
943 /* if just_works and bonding bit is not set treat this as temporary */
944 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 -0800945 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
946 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
Andre Eisenbach89363762015-01-26 13:49:36 -0800947 pairing_cb.bond_type = BOND_TYPE_TEMPORARY;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800948 else
Andre Eisenbach89363762015-01-26 13:49:36 -0800949 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800950
951 pairing_cb.is_ssp = TRUE;
952
953 /* If JustWorks auto-accept */
954 if (p_ssp_cfm_req->just_works)
955 {
956 /* Pairing consent for JustWorks needed if:
957 * 1. Incoming pairing is detected AND
958 * 2. local IO capabilities are DisplayYesNo AND
959 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
960 */
961 if ((is_incoming) && ((p_ssp_cfm_req->loc_io_caps == 0x01) &&
962 (p_ssp_cfm_req->rmt_io_caps == 0x00 || p_ssp_cfm_req->rmt_io_caps == 0x03)))
963 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700964 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 -0800965 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
966 }
967 else
968 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700969 BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800970 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
971 return;
972 }
973 }
974
975 cod = devclass2uint(p_ssp_cfm_req->dev_class);
976
Chris Mantonf8027002015-03-12 09:22:48 -0700977 if (cod == 0) {
978 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800979 cod = COD_UNCLASSIFIED;
980 }
981
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700982 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800983 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
984 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
985 p_ssp_cfm_req->num_val);
986}
987
988static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
989{
990 bt_bdaddr_t bd_addr;
991 bt_bdname_t bd_name;
992 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -0800993 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800994
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700995 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800996
997 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -0800998 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
999 {
1000 dev_type = BT_DEVICE_TYPE_BREDR;
1001 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001002 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -08001003 p_ssp_key_notif->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001004
1005 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
1006 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
1007
1008 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1009 pairing_cb.is_ssp = TRUE;
1010 cod = devclass2uint(p_ssp_key_notif->dev_class);
1011
Chris Mantonf8027002015-03-12 09:22:48 -07001012 if (cod == 0) {
1013 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001014 cod = COD_UNCLASSIFIED;
1015 }
1016
1017 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
1018 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
1019 p_ssp_key_notif->passkey);
1020}
1021/*******************************************************************************
1022**
1023** Function btif_dm_auth_cmpl_evt
1024**
1025** Description Executes authentication complete event in btif context
1026**
1027** Returns void
1028**
1029*******************************************************************************/
1030static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
1031{
1032 /* Save link key, if not temporary */
1033 bt_bdaddr_t bd_addr;
1034 bt_status_t status = BT_STATUS_FAIL;
1035 bt_bond_state_t state = BT_BOND_STATE_NONE;
Priti Agherac0edf9f2014-06-26 11:23:51 -07001036 BOOLEAN skip_sdp = FALSE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001037
1038 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
1039 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
1040 {
1041 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 -08001042 (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 -08001043 {
1044 bt_status_t ret;
Andre Eisenbach89363762015-01-26 13:49:36 -08001045 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, bond_type=%d",
1046 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001047 ret = btif_storage_add_bonded_device(&bd_addr,
1048 p_auth_cmpl->key, p_auth_cmpl->key_type,
1049 pairing_cb.pin_code_len);
1050 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
1051 }
1052 else
1053 {
Andre Eisenbach89363762015-01-26 13:49:36 -08001054 BTIF_TRACE_DEBUG("%s: Temporary key. Not storing. key_type=0x%x, bond_type=%d",
1055 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1056 if(pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
Hemant Guptab820aec2013-12-24 19:59:57 +05301057 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001058 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
Hemant Guptab820aec2013-12-24 19:59:57 +05301059 __FUNCTION__);
1060 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1061 return;
1062 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001063 }
1064 }
Priti Agherac0edf9f2014-06-26 11:23:51 -07001065
1066 // Skip SDP for certain HID Devices
The Android Open Source Project5738f832012-12-12 16:00:35 -08001067 if (p_auth_cmpl->success)
1068 {
Andre Eisenbach31a64002014-10-14 14:29:19 -07001069 pairing_cb.timeout_retries = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001070 status = BT_STATUS_SUCCESS;
1071 state = BT_BOND_STATE_BONDED;
Priti Agherac0edf9f2014-06-26 11:23:51 -07001072 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001073
Priti Agherac0edf9f2014-06-26 11:23:51 -07001074 if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr, COD_HID_MAJOR))
1075 {
Sharvil Nanavati44802762014-12-23 23:08:58 -08001076 LOG_WARN("%s:skip SDP", __FUNCTION__);
Priti Agherac0edf9f2014-06-26 11:23:51 -07001077 skip_sdp = TRUE;
1078 }
1079 if(!pairing_cb.is_local_initiated && skip_sdp)
1080 {
1081 bond_state_changed(status, &bd_addr, state);
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -08001082
Sharvil Nanavati44802762014-12-23 23:08:58 -08001083 LOG_WARN("%s: Incoming HID Connection",__FUNCTION__);
Priti Agherac0edf9f2014-06-26 11:23:51 -07001084 bt_property_t prop;
1085 bt_bdaddr_t bd_addr;
1086 bt_uuid_t uuid;
1087 char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE;
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -08001088
Priti Agherac0edf9f2014-06-26 11:23:51 -07001089 string_to_uuid(uuid_str, &uuid);
1090
1091 prop.type = BT_PROPERTY_UUIDS;
1092 prop.val = uuid.uu;
1093 prop.len = MAX_UUID_SIZE;
1094
1095 /* Send the event to the BTIF */
1096 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1097 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1098 }
1099 else
1100 {
1101 /* Trigger SDP on the device */
1102 pairing_cb.sdp_attempts = 1;;
1103
1104 if(btif_dm_inquiry_in_progress)
1105 btif_dm_cancel_discovery();
1106
1107 btif_dm_get_remote_services(&bd_addr);
1108 }
1109 /* 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 -08001110 }
1111 else
1112 {
1113 /*Map the HCI fail reason to bt status */
1114 switch(p_auth_cmpl->fail_reason)
1115 {
1116 case HCI_ERR_PAGE_TIMEOUT:
Andre Eisenbach31a64002014-10-14 14:29:19 -07001117 if (blacklistPairingRetries(bd_addr.address) && pairing_cb.timeout_retries)
1118 {
1119 BTIF_TRACE_WARNING("%s() - Pairing timeout; retrying (%d) ...", __FUNCTION__, pairing_cb.timeout_retries);
1120 --pairing_cb.timeout_retries;
1121 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
1122 return;
1123 }
1124 /* Fall-through */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001125 case HCI_ERR_CONNECTION_TOUT:
1126 status = BT_STATUS_RMT_DEV_DOWN;
1127 break;
1128
Hemant Guptaaef7a672013-07-31 19:00:12 +05301129 case HCI_ERR_PAIRING_NOT_ALLOWED:
1130 status = BT_STATUS_AUTH_REJECTED;
1131 break;
1132
Hemant Guptab4801442014-01-07 18:11:15 +05301133 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
1134 status = BT_STATUS_AUTH_FAILURE;
1135 break;
1136
The Android Open Source Project5738f832012-12-12 16:00:35 -08001137 /* map the auth failure codes, so we can retry pairing if necessary */
1138 case HCI_ERR_AUTH_FAILURE:
Hemant Gupta59a88ec2014-03-19 19:01:35 +05301139 case HCI_ERR_KEY_MISSING:
Zhihai Xua7ea8092013-11-27 14:10:53 +05301140 btif_storage_remove_bonded_device(&bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001141 case HCI_ERR_HOST_REJECT_SECURITY:
1142 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
1143 case HCI_ERR_UNIT_KEY_USED:
1144 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
1145 case HCI_ERR_INSUFFCIENT_SECURITY:
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301146 case HCI_ERR_PEER_USER:
1147 case HCI_ERR_UNSPECIFIED:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001148 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d",
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301149 __FUNCTION__, p_auth_cmpl->fail_reason);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001150 if (pairing_cb.autopair_attempts == 1)
1151 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001152 BTIF_TRACE_DEBUG("%s(): Adding device to blacklist ", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001153
1154 /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class */
1155 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
1156 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
1157 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
1158 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
1159 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
1160 check_cod(&bd_addr, COD_HID_POINTING))
1161 {
1162 btif_storage_add_device_to_autopair_blacklist (&bd_addr);
1163 }
1164 pairing_cb.autopair_attempts++;
1165
1166 /* Create the Bond once again */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001167 BTIF_TRACE_DEBUG("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07001168 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001169 return;
1170 }
1171 else
1172 {
1173 /* if autopair attempts are more than 1, or not attempted */
1174 status = BT_STATUS_AUTH_FAILURE;
1175 }
1176 break;
1177
1178 default:
1179 status = BT_STATUS_FAIL;
1180 }
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301181 /* Special Handling for HID Devices */
1182 if (check_cod(&bd_addr, COD_HID_POINTING)) {
1183 /* Remove Device as bonded in nvram as authentication failed */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001184 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __FUNCTION__);
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301185 btif_storage_remove_bonded_device(&bd_addr);
1186 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001187 bond_state_changed(status, &bd_addr, state);
1188 }
1189}
1190
1191/******************************************************************************
1192**
1193** Function btif_dm_search_devices_evt
1194**
1195** Description Executes search devices callback events in btif context
1196**
1197** Returns void
1198**
1199******************************************************************************/
1200static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
1201{
1202 tBTA_DM_SEARCH *p_search_data;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001203 BTIF_TRACE_EVENT("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001204
1205 switch (event)
1206 {
1207 case BTA_DM_DISC_RES_EVT:
1208 {
1209 p_search_data = (tBTA_DM_SEARCH *)p_param;
1210 /* Remote name update */
1211 if (strlen((const char *) p_search_data->disc_res.bd_name))
1212 {
1213 bt_property_t properties[1];
1214 bt_bdaddr_t bdaddr;
1215 bt_status_t status;
1216
1217 properties[0].type = BT_PROPERTY_BDNAME;
1218 properties[0].val = p_search_data->disc_res.bd_name;
1219 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
1220 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
1221
1222 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
1223 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
1224 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1225 status, &bdaddr, 1, properties);
1226 }
1227 /* TODO: Services? */
1228 }
1229 break;
1230
1231 case BTA_DM_INQ_RES_EVT:
1232 {
1233 /* inquiry result */
1234 UINT32 cod;
1235 UINT8 *p_eir_remote_name = NULL;
1236 bt_bdname_t bdname;
1237 bt_bdaddr_t bdaddr;
1238 UINT8 remote_name_len;
1239 UINT8 *p_cached_name = NULL;
1240 tBTA_SERVICE_MASK services = 0;
1241 bdstr_t bdstr;
1242
1243 p_search_data = (tBTA_DM_SEARCH *)p_param;
1244 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
1245
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07001246 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 -08001247#if (BLE_INCLUDED == TRUE)
1248 p_search_data->inq_res.device_type);
1249#else
1250 BT_DEVICE_TYPE_BREDR);
1251#endif
1252 bdname.name[0] = 0;
1253
1254 cod = devclass2uint (p_search_data->inq_res.dev_class);
1255
Chris Mantonf8027002015-03-12 09:22:48 -07001256 if (cod == 0) {
1257 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001258 cod = COD_UNCLASSIFIED;
1259 }
1260
1261 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1262 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1263
1264 /* Check EIR for remote name and services */
1265 if (p_search_data->inq_res.p_eir)
1266 {
1267 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001268 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001269 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */
1270 }
1271
1272
1273 {
1274 bt_property_t properties[5];
1275 bt_device_type_t dev_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001276 UINT8 addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001277 uint32_t num_properties = 0;
1278 bt_status_t status;
1279
1280 memset(properties, 0, sizeof(properties));
1281 /* BD_ADDR */
1282 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1283 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1284 num_properties++;
1285 /* BD_NAME */
1286 /* Don't send BDNAME if it is empty */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001287 if (bdname.name[0])
1288 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001289 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1290 BT_PROPERTY_BDNAME,
1291 strlen((char *)bdname.name), &bdname);
1292 num_properties++;
1293 }
1294
1295 /* DEV_CLASS */
1296 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1297 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1298 num_properties++;
1299 /* DEV_TYPE */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001300#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001301 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1302 dev_type = p_search_data->inq_res.device_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001303 addr_type = p_search_data->inq_res.ble_addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001304#else
1305 dev_type = BT_DEVICE_TYPE_BREDR;
1306#endif
1307 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1308 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1309 num_properties++;
1310 /* RSSI */
1311 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1312 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1313 &(p_search_data->inq_res.rssi));
1314 num_properties++;
1315
1316 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1317 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001318#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1319 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001320 if (( dev_type == BT_DEVICE_TYPE_DUMO)&&
1321 (p_search_data->inq_res.flag & BTA_BLE_DMT_CONTROLLER_SPT) &&
1322 (p_search_data->inq_res.flag & BTA_BLE_DMT_HOST_SPT))
1323 {
1324 btif_storage_set_dmt_support_type (&bdaddr, TRUE);
1325 }
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001326 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1327#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001328 /* Callback to notify upper layer of device */
1329 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1330 num_properties, properties);
1331 }
1332 }
1333 break;
1334
1335 case BTA_DM_INQ_CMPL_EVT:
1336 {
Satya Calloji6e2d9db2014-07-08 16:18:58 -07001337#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1338 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1339 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1340 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1341 bte_scan_filt_param_cfg_evt, 0);
1342#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001343 }
1344 break;
1345 case BTA_DM_DISC_CMPL_EVT:
1346 {
1347 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1348 }
1349 break;
1350 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1351 {
1352 /* if inquiry is not in progress and we get a cancel event, then
1353 * it means we are done with inquiry, but remote_name fetches are in
1354 * progress
1355 *
1356 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt
1357 * but instead wait for the cancel_cmpl_evt via the Busy Level
1358 *
1359 */
1360 if (btif_dm_inquiry_in_progress == FALSE)
1361 {
Nitin Arora7b85efa2014-09-26 14:05:24 -07001362#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1363 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1364 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1365 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1366 bte_scan_filt_param_cfg_evt, 0);
1367#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001368 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1369 }
1370 }
1371 break;
1372 }
1373}
1374
1375/*******************************************************************************
1376**
1377** Function btif_dm_search_services_evt
1378**
1379** Description Executes search services event in btif context
1380**
1381** Returns void
1382**
1383*******************************************************************************/
1384static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1385{
1386 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1387
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001388 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001389 switch (event)
1390 {
1391 case BTA_DM_DISC_RES_EVT:
1392 {
1393 bt_uuid_t uuid_arr[BT_MAX_NUM_UUIDS]; /* Max 32 services */
1394 bt_property_t prop;
1395 uint32_t i = 0, j = 0;
1396 bt_bdaddr_t bd_addr;
1397 bt_status_t ret;
1398
1399 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1400
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001401 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001402 p_data->disc_res.result, p_data->disc_res.services);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001403 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1404 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1405 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1406 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001407 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001408 pairing_cb.sdp_attempts++;
1409 btif_dm_get_remote_services(&bd_addr);
1410 return;
1411 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001412 prop.type = BT_PROPERTY_UUIDS;
1413 prop.len = 0;
1414 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1415 {
1416 prop.val = p_data->disc_res.p_uuid_list;
1417 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1418 for (i=0; i < p_data->disc_res.num_uuids; i++)
1419 {
1420 char temp[256];
Chris Manton8ff3fea2015-01-07 13:59:14 -08001421 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 -07001422 LOG_INFO("%s index:%d uuid:%s", __func__, i, temp);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001423 }
1424 }
1425
1426 /* onUuidChanged requires getBondedDevices to be populated.
1427 ** bond_state_changed needs to be sent prior to remote_device_property
1428 */
1429 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1430 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0)&&
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001431 pairing_cb.sdp_attempts > 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001432 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001433 BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001434 __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001435 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001436 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1437 }
1438
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001439 if(p_data->disc_res.num_uuids != 0)
1440 {
1441 /* Also write this to the NVRAM */
1442 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1443 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1444 /* Send the event to the BTIF */
1445 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1446 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1447 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001448 }
1449 break;
1450
1451 case BTA_DM_DISC_CMPL_EVT:
1452 /* fixme */
1453 break;
1454
Matthew Xie607e3b72013-08-15 19:30:48 -07001455#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001456 case BTA_DM_DISC_BLE_RES_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001457 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__,
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001458 p_data->disc_ble_res.service.uu.uuid16);
1459 bt_uuid_t uuid;
1460 int i = 0;
1461 int j = 15;
1462 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1463 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001464 BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001465 bt_property_t prop;
1466 bt_bdaddr_t bd_addr;
1467 char temp[256];
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001468 bt_status_t ret;
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001469
1470 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1471
1472 while(i < j )
1473 {
1474 unsigned char c = uuid.uu[j];
1475 uuid.uu[j] = uuid.uu[i];
1476 uuid.uu[i] = c;
1477 i++;
1478 j--;
1479 }
1480
Chris Manton8ff3fea2015-01-07 13:59:14 -08001481 uuid_to_string_legacy(&uuid, temp);
Chris Mantonf8027002015-03-12 09:22:48 -07001482 LOG_INFO("%s uuid:%s", __func__, temp);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001483
1484 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1485 prop.type = BT_PROPERTY_UUIDS;
1486 prop.val = uuid.uu;
1487 prop.len = MAX_UUID_SIZE;
1488
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001489 /* Also write this to the NVRAM */
1490 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1491 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1492
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001493 /* Send the event to the BTIF */
1494 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1495 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1496
1497 }
1498 break;
Matthew Xie607e3b72013-08-15 19:30:48 -07001499#endif /* BLE_INCLUDED */
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001500
The Android Open Source Project5738f832012-12-12 16:00:35 -08001501 default:
1502 {
1503 ASSERTC(0, "unhandled search services event", event);
1504 }
1505 break;
1506 }
1507}
1508
1509/*******************************************************************************
1510**
1511** Function btif_dm_remote_service_record_evt
1512**
1513** Description Executes search service record event in btif context
1514**
1515** Returns void
1516**
1517*******************************************************************************/
1518static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1519{
1520 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1521
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001522 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001523 switch (event)
1524 {
1525 case BTA_DM_DISC_RES_EVT:
1526 {
1527 bt_service_record_t rec;
1528 bt_property_t prop;
1529 uint32_t i = 0;
1530 bt_bdaddr_t bd_addr;
1531
1532 memset(&rec, 0, sizeof(bt_service_record_t));
1533 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1534
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001535 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001536 p_data->disc_res.result, p_data->disc_res.services);
1537 prop.type = BT_PROPERTY_SERVICE_RECORD;
1538 prop.val = (void*)&rec;
1539 prop.len = sizeof(rec);
1540
1541 /* disc_res.result is overloaded with SCN. Cannot check result */
1542 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1543 /* TODO: Get the UUID as well */
1544 rec.channel = p_data->disc_res.result - 3;
1545 /* TODO: Need to get the service name using p_raw_data */
1546 rec.name[0] = 0;
1547
1548 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1549 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1550 }
1551 break;
1552
1553 default:
1554 {
1555 ASSERTC(0, "unhandled remote service record event", event);
1556 }
1557 break;
1558 }
1559}
1560
1561/*******************************************************************************
1562**
1563** Function btif_dm_upstreams_cback
1564**
1565** Description Executes UPSTREAMS events in btif context
1566**
1567** Returns void
1568**
1569*******************************************************************************/
1570static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1571{
1572 tBTA_DM_SEC_EVT dm_event = (tBTA_DM_SEC_EVT)event;
1573 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1574 tBTA_SERVICE_MASK service_mask;
1575 uint32_t i;
1576 bt_bdaddr_t bd_addr;
1577
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001578 BTIF_TRACE_EVENT("btif_dm_upstreams_cback ev: %s", dump_dm_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001579
1580 switch (event)
1581 {
1582 case BTA_DM_ENABLE_EVT:
1583 {
1584 BD_NAME bdname;
1585 bt_status_t status;
1586 bt_property_t prop;
1587 prop.type = BT_PROPERTY_BDNAME;
1588 prop.len = BD_NAME_LEN;
1589 prop.val = (void*)bdname;
1590
1591 status = btif_storage_get_adapter_property(&prop);
Matthew Xie1e5109b2012-11-09 18:26:26 -08001592 if (status == BT_STATUS_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001593 {
1594 /* A name exists in the storage. Make this the device name */
1595 BTA_DmSetDeviceName((char*)prop.val);
1596 }
Matthew Xie1e5109b2012-11-09 18:26:26 -08001597 else
1598 {
1599 /* Storage does not have a name yet.
1600 * Use the default name and write it to the chip
1601 */
1602 BTA_DmSetDeviceName(btif_get_default_local_name());
1603 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001604
Andre Eisenbacha015a832014-09-11 14:09:40 -07001605#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1606 /* Enable local privacy */
Andre Eisenbach3e0dc732014-10-24 09:55:34 -07001607 BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED);
Andre Eisenbacha015a832014-09-11 14:09:40 -07001608#endif
1609
The Android Open Source Project5738f832012-12-12 16:00:35 -08001610 /* for each of the enabled services in the mask, trigger the profile
1611 * enable */
1612 service_mask = btif_get_enabled_services_mask();
1613 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1614 {
1615 if (service_mask &
1616 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1617 {
1618 btif_in_execute_service_request(i, TRUE);
1619 }
1620 }
1621 /* clear control blocks */
1622 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1623
1624 /* This function will also trigger the adapter_properties_cb
1625 ** and bonded_devices_info_cb
1626 */
1627 btif_storage_load_bonded_devices();
1628
1629 btif_storage_load_autopair_device_list();
1630
Zach Johnson39110ec2014-10-06 13:15:00 -07001631 btif_enable_bluetooth_evt(p_data->enable.status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001632 }
1633 break;
1634
1635 case BTA_DM_DISABLE_EVT:
1636 /* for each of the enabled services in the mask, trigger the profile
1637 * disable */
1638 service_mask = btif_get_enabled_services_mask();
1639 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1640 {
1641 if (service_mask &
1642 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1643 {
1644 btif_in_execute_service_request(i, FALSE);
1645 }
1646 }
1647 btif_disable_bluetooth_evt();
1648 break;
1649
1650 case BTA_DM_PIN_REQ_EVT:
1651 btif_dm_pin_req_evt(&p_data->pin_req);
1652 break;
1653
1654 case BTA_DM_AUTH_CMPL_EVT:
1655 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1656 break;
1657
1658 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1659 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1660 {
1661 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1662 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1663 }
1664 break;
1665
1666 case BTA_DM_SP_CFM_REQ_EVT:
1667 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1668 break;
1669 case BTA_DM_SP_KEY_NOTIF_EVT:
1670 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1671 break;
1672
1673 case BTA_DM_DEV_UNPAIRED_EVT:
1674 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1675
1676 /*special handling for HID devices */
1677 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -07001678 btif_hh_remove_device(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001679 #endif
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001680 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1681 btif_storage_remove_ble_bonding_keys(&bd_addr);
1682 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001683 btif_storage_remove_bonded_device(&bd_addr);
1684 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1685 break;
1686
1687 case BTA_DM_BUSY_LEVEL_EVT:
1688 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001689
1690 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001691 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001692 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001693 {
1694 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1695 BT_DISCOVERY_STARTED);
1696 btif_dm_inquiry_in_progress = TRUE;
1697 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001698 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001699 {
1700 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1701 BT_DISCOVERY_STOPPED);
1702 btif_dm_inquiry_in_progress = FALSE;
1703 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001704 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001705 {
1706 btif_dm_inquiry_in_progress = FALSE;
1707 }
1708 }
1709 }break;
1710
1711 case BTA_DM_LINK_UP_EVT:
1712 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001713 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001714
1715 btif_update_remote_version_property(&bd_addr);
1716
The Android Open Source Project5738f832012-12-12 16:00:35 -08001717 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1718 &bd_addr, BT_ACL_STATE_CONNECTED);
1719 break;
1720
1721 case BTA_DM_LINK_DOWN_EVT:
1722 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001723 BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001724 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1725 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1726 break;
1727
1728 case BTA_DM_HW_ERROR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001729 BTIF_TRACE_ERROR("Received H/W Error. ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001730 /* Flush storage data */
1731 btif_config_flush();
1732 usleep(100000); /* 100milliseconds */
1733 /* Killing the process to force a restart as part of fault tolerance */
1734 kill(getpid(), SIGKILL);
1735 break;
1736
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001737#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1738 case BTA_DM_BLE_KEY_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001739 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 -08001740
1741 /* If this pairing is by-product of local initiated GATT client Read or Write,
1742 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1743 have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1744 if (pairing_cb.state != BT_BOND_STATE_BONDING)
1745 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001746 BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001747 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1748 BT_BOND_STATE_BONDING);
1749 }
1750 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1751 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001752 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001753 break;
1754 }
1755
1756 switch (p_data->ble_key.key_type)
1757 {
1758 case BTA_LE_KEY_PENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001759 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001760 pairing_cb.ble.is_penc_key_rcvd = TRUE;
1761 memcpy(pairing_cb.ble.penc_key.ltk,p_data->ble_key.key_value.penc_key.ltk, 16);
1762 memcpy(pairing_cb.ble.penc_key.rand, p_data->ble_key.key_value.penc_key.rand,8);
1763 pairing_cb.ble.penc_key.ediv = p_data->ble_key.key_value.penc_key.ediv;
1764 pairing_cb.ble.penc_key.sec_level = p_data->ble_key.key_value.penc_key.sec_level;
1765
1766 for (i=0; i<16; i++)
1767 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001768 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 -08001769 }
1770 for (i=0; i<8; i++)
1771 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001772 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 -08001773 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001774 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.ediv=0x%04x",pairing_cb.ble.penc_key.ediv);
1775 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.sec_level=0x%02x",pairing_cb.ble.penc_key.sec_level);
1776 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 -08001777 break;
1778
1779 case BTA_LE_KEY_PID:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001780 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001781 pairing_cb.ble.is_pid_key_rcvd = TRUE;
Andre Eisenbach5e808462014-10-21 12:37:53 -07001782 pairing_cb.ble.pid_key.addr_type = p_data->ble_key.key_value.pid_key.addr_type;
1783 memcpy(pairing_cb.ble.pid_key.irk, p_data->ble_key.key_value.pid_key.irk, 16);
1784 memcpy(pairing_cb.ble.pid_key.static_addr,
1785 p_data->ble_key.key_value.pid_key.static_addr,BD_ADDR_LEN);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001786 for (i=0; i<16; i++)
1787 {
Andre Eisenbach5e808462014-10-21 12:37:53 -07001788 BTIF_TRACE_DEBUG("pairing_cb.ble.pid_key.irk[%d]=0x%02x"
1789 ,i,pairing_cb.ble.pid_key.irk[i]);
1790 }
1791 for (i=0; i<BD_ADDR_LEN; i++)
1792 {
1793 BTIF_TRACE_DEBUG("piaring_cb.ble.pid_address[%d] = %x"
1794 ,i, pairing_cb.ble.pid_key.static_addr[i]);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001795 }
1796 break;
1797
1798 case BTA_LE_KEY_PCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001799 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001800 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1801 pairing_cb.ble.pcsrk_key.counter = p_data->ble_key.key_value.pcsrk_key.counter;
1802 pairing_cb.ble.pcsrk_key.sec_level = p_data->ble_key.key_value.pcsrk_key.sec_level;
1803 memcpy(pairing_cb.ble.pcsrk_key.csrk,p_data->ble_key.key_value.pcsrk_key.csrk,16);
1804
1805 for (i=0; i<16; i++)
1806 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001807 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 -08001808 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001809 BTIF_TRACE_DEBUG("pairing_cb.ble.pcsrk_key.counter=0x%08x",pairing_cb.ble.pcsrk_key.counter);
1810 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 -08001811 break;
1812
1813 case BTA_LE_KEY_LENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001814 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001815 pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1816 pairing_cb.ble.lenc_key.div = p_data->ble_key.key_value.lenc_key.div;
1817 pairing_cb.ble.lenc_key.key_size = p_data->ble_key.key_value.lenc_key.key_size;
1818 pairing_cb.ble.lenc_key.sec_level = p_data->ble_key.key_value.lenc_key.sec_level;
1819
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001820 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.div=0x%04x",pairing_cb.ble.lenc_key.div);
1821 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.key_size=0x%02x",pairing_cb.ble.lenc_key.key_size);
1822 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 -08001823 break;
1824
1825
1826
1827 case BTA_LE_KEY_LCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001828 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001829 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1830 pairing_cb.ble.lcsrk_key.counter = p_data->ble_key.key_value.lcsrk_key.counter;
1831 pairing_cb.ble.lcsrk_key.div = p_data->ble_key.key_value.lcsrk_key.div;
1832 pairing_cb.ble.lcsrk_key.sec_level = p_data->ble_key.key_value.lcsrk_key.sec_level;
1833
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001834 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.div=0x%04x",pairing_cb.ble.lcsrk_key.div);
1835 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.counter=0x%08x",pairing_cb.ble.lcsrk_key.counter);
1836 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 -08001837
1838 break;
1839
1840 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001841 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001842 break;
1843 }
1844
1845 break;
1846 case BTA_DM_BLE_SEC_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001847 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001848 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1849 break;
1850 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001851 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001852 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1853 break;
1854 case BTA_DM_BLE_PASSKEY_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001855 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001856 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1857 break;
1858 case BTA_DM_BLE_OOB_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001859 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001860 break;
1861 case BTA_DM_BLE_LOCAL_IR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001862 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001863 ble_local_key_cb.is_id_keys_rcvd = TRUE;
1864 memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1865 memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1866 memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1867 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1868 BTIF_DM_LE_LOCAL_KEY_IR,
1869 BT_OCTET16_LEN);
1870 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1871 BTIF_DM_LE_LOCAL_KEY_IRK,
1872 BT_OCTET16_LEN);
1873 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1874 BTIF_DM_LE_LOCAL_KEY_DHK,
1875 BT_OCTET16_LEN);
1876 break;
1877 case BTA_DM_BLE_LOCAL_ER_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001878 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001879 ble_local_key_cb.is_er_rcvd = TRUE;
1880 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1881 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1882 BTIF_DM_LE_LOCAL_KEY_ER,
1883 BT_OCTET16_LEN);
1884 break;
1885
1886 case BTA_DM_BLE_AUTH_CMPL_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001887 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001888 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1889 break;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001890
1891 case BTA_DM_LE_FEATURES_READ:
1892 {
1893 tBTM_BLE_VSC_CB cmn_vsc_cb;
1894 bt_local_le_features_t local_le_features;
1895 char buf[512];
1896 bt_property_t prop;
1897 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1898 prop.val = (void*)buf;
1899 prop.len = sizeof(buf);
1900
1901 /* LE features are not stored in storage. Should be retrived from stack */
1902 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1903 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1904
1905 prop.len = sizeof (bt_local_le_features_t);
1906 if (cmn_vsc_cb.filter_support == 1)
1907 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1908 else
1909 local_le_features.max_adv_filter_supported = 0;
1910 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1911 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1912 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
Satya Callojiefaddcb2014-07-28 23:22:05 -07001913 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
Satya Callojif5387072015-02-09 17:40:52 -08001914 local_le_features.scan_result_storage_size = cmn_vsc_cb.tot_scan_results_strg;
1915 local_le_features.version_supported = cmn_vsc_cb.version_supported;
1916 local_le_features.total_trackable_advertisers =
1917 cmn_vsc_cb.total_trackable_advertisers;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001918 memcpy(prop.val, &local_le_features, prop.len);
1919 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
1920 break;
1921 }
Satya Callojie5ba8842014-07-03 17:18:02 -07001922
1923 case BTA_DM_ENER_INFO_READ:
1924 {
1925 btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param;
1926 bt_activity_energy_info energy_info;
1927 energy_info.status = p_ener_data->status;
1928 energy_info.ctrl_state = p_ener_data->ctrl_state;
1929 energy_info.rx_time = p_ener_data->rx_time;
1930 energy_info.tx_time = p_ener_data->tx_time;
1931 energy_info.idle_time = p_ener_data->idle_time;
1932 energy_info.energy_used = p_ener_data->energy_used;
1933 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info);
1934 break;
1935 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001936#endif
1937
The Android Open Source Project5738f832012-12-12 16:00:35 -08001938 case BTA_DM_AUTHORIZE_EVT:
1939 case BTA_DM_SIG_STRENGTH_EVT:
1940 case BTA_DM_SP_RMT_OOB_EVT:
1941 case BTA_DM_SP_KEYPRESS_EVT:
1942 case BTA_DM_ROLE_CHG_EVT:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001943
The Android Open Source Project5738f832012-12-12 16:00:35 -08001944 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001945 BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001946 break;
1947 }
1948} /* btui_security_cback() */
1949
1950
1951/*******************************************************************************
1952**
1953** Function btif_dm_generic_evt
1954**
1955** Description Executes non-BTA upstream events in BTIF context
1956**
1957** Returns void
1958**
1959*******************************************************************************/
1960static void btif_dm_generic_evt(UINT16 event, char* p_param)
1961{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001962 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001963 switch(event)
1964 {
1965 case BTIF_DM_CB_DISCOVERY_STARTED:
1966 {
1967 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
1968 }
1969 break;
1970
1971 case BTIF_DM_CB_CREATE_BOND:
1972 {
Andre Eisenbach31a64002014-10-14 14:29:19 -07001973 pairing_cb.timeout_retries = NUM_TIMEOUT_RETRIES;
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07001974 btif_dm_create_bond_cb_t *create_bond_cb = (btif_dm_create_bond_cb_t*)p_param;
1975 btif_dm_cb_create_bond(&create_bond_cb->bdaddr, create_bond_cb->transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001976 }
1977 break;
1978
1979 case BTIF_DM_CB_REMOVE_BOND:
1980 {
1981 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
1982 }
1983 break;
1984
1985 case BTIF_DM_CB_HID_REMOTE_NAME:
1986 {
1987 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
1988 }
1989 break;
1990
1991 case BTIF_DM_CB_BOND_STATE_BONDING:
1992 {
1993 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
1994 }
1995 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001996 case BTIF_DM_CB_LE_TX_TEST:
1997 case BTIF_DM_CB_LE_RX_TEST:
1998 {
1999 uint8_t status;
2000 STREAM_TO_UINT8(status, p_param);
2001 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2002 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
2003 }
2004 break;
2005 case BTIF_DM_CB_LE_TEST_END:
2006 {
2007 uint8_t status;
2008 uint16_t count = 0;
2009 STREAM_TO_UINT8(status, p_param);
2010 if (status == 0)
2011 STREAM_TO_UINT16(count, p_param);
2012 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2013 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
2014 }
2015 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002016 default:
2017 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002018 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002019 }
2020 break;
2021 }
2022}
2023
2024/*******************************************************************************
2025**
2026** Function bte_dm_evt
2027**
2028** Description Switches context from BTE to BTIF for all DM events
2029**
2030** Returns void
2031**
2032*******************************************************************************/
2033
2034void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
2035{
2036 bt_status_t status;
2037
2038 /* switch context to btif task context (copy full union size for convenience) */
2039 status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event, (void*)p_data, sizeof(tBTA_DM_SEC), NULL);
2040
2041 /* catch any failed context transfers */
2042 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
2043}
2044
2045/*******************************************************************************
2046**
2047** Function bte_search_devices_evt
2048**
2049** Description Switches context from BTE to BTIF for DM search events
2050**
2051** Returns void
2052**
2053*******************************************************************************/
2054static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2055{
2056 UINT16 param_len = 0;
2057
2058 if (p_data)
2059 param_len += sizeof(tBTA_DM_SEARCH);
2060 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
2061 switch (event)
2062 {
2063 case BTA_DM_INQ_RES_EVT:
2064 {
2065 if (p_data->inq_res.p_eir)
2066 param_len += HCI_EXT_INQ_RESPONSE_LEN;
2067 }
2068 break;
2069
2070 case BTA_DM_DISC_RES_EVT:
2071 {
2072 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
2073 param_len += p_data->disc_res.raw_data_size;
2074 }
2075 break;
2076 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002077 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 -08002078
2079 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
2080 if (event == BTA_DM_INQ_RES_EVT)
2081 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
2082
2083 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
2084 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
2085}
2086
2087/*******************************************************************************
2088**
2089** Function bte_dm_search_services_evt
2090**
2091** Description Switches context from BTE to BTIF for DM search services
2092** event
2093**
2094** Returns void
2095**
2096*******************************************************************************/
2097static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2098{
2099 UINT16 param_len = 0;
2100 if (p_data)
2101 param_len += sizeof(tBTA_DM_SEARCH);
2102 switch (event)
2103 {
2104 case BTA_DM_DISC_RES_EVT:
2105 {
2106 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
2107 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
2108 }
2109 } break;
2110 }
2111 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
2112 * if raw_data is needed. */
2113 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
2114 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
2115}
2116
2117/*******************************************************************************
2118**
2119** Function bte_dm_remote_service_record_evt
2120**
2121** Description Switches context from BTE to BTIF for DM search service
2122** record event
2123**
2124** Returns void
2125**
2126*******************************************************************************/
2127static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2128{
2129 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
2130 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
2131}
2132
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002133#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07002134/*******************************************************************************
2135**
2136** Function bta_energy_info_cb
2137**
2138** Description Switches context from BTE to BTIF for DM energy info event
2139**
2140** Returns void
2141**
2142*******************************************************************************/
2143static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time,
2144 tBTA_DM_BLE_IDLE_TIME_MS idle_time,
2145 tBTA_DM_BLE_ENERGY_USED energy_used,
2146 tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status)
2147{
2148 BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld",
2149 status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
2150
2151 btif_activity_energy_info_cb_t btif_cb;
2152 btif_cb.status = status;
2153 btif_cb.ctrl_state = ctrl_state;
2154 btif_cb.tx_time = (uint64_t) tx_time;
2155 btif_cb.rx_time = (uint64_t) rx_time;
2156 btif_cb.idle_time =(uint64_t) idle_time;
2157 btif_cb.energy_used =(uint64_t) energy_used;
2158 btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
2159 (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL);
2160}
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002161#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07002162
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002163/*******************************************************************************
2164**
2165** Function bte_scan_filt_param_cfg_evt
2166**
2167** Description Scan filter param config event
2168**
2169** Returns void
2170**
2171*******************************************************************************/
2172static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
2173 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
2174 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
2175{
2176 /* This event occurs on calling BTA_DmBleCfgFilterCondition internally,
2177 ** and that is why there is no HAL callback
2178 */
2179 if(BTA_SUCCESS != status)
2180 {
2181 BTIF_TRACE_ERROR("%s, %d", __FUNCTION__, status);
2182 }
2183 else
2184 {
2185 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2186 }
2187}
2188
The Android Open Source Project5738f832012-12-12 16:00:35 -08002189/*****************************************************************************
2190**
2191** btif api functions (no context switch)
2192**
2193*****************************************************************************/
2194
2195/*******************************************************************************
2196**
2197** Function btif_dm_start_discovery
2198**
2199** Description Start device discovery/inquiry
2200**
2201** Returns bt_status_t
2202**
2203*******************************************************************************/
2204bt_status_t btif_dm_start_discovery(void)
2205{
2206 tBTA_DM_INQ inq_params;
2207 tBTA_SERVICE_MASK services = 0;
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002208 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002209
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002210 BTIF_TRACE_EVENT("%s", __FUNCTION__);
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002211
2212#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2213 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
2214 /* Cleanup anything remaining on index 0 */
2215 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
2216 bte_scan_filt_param_cfg_evt, 0);
2217
2218 /* Add an allow-all filter on index 0*/
2219 adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE;
2220 adv_filt_param.feat_seln = ALLOW_ALL_FILTER;
2221 adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;
2222 adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;
2223 adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE;
2224 adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE;
2225 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL,
2226 bte_scan_filt_param_cfg_evt, 0);
2227
The Android Open Source Project5738f832012-12-12 16:00:35 -08002228 /* TODO: Do we need to handle multiple inquiries at the same time? */
2229
2230 /* Set inquiry params and call API */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002231 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
Matthew Xie7f3e4292013-09-30 12:44:10 -07002232#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
2233 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
2234 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
2235 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
2236 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
2237#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002238#else
2239 inq_params.mode = BTA_DM_GENERAL_INQUIRY;
2240#endif
2241 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
2242
2243 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
2244 inq_params.report_dup = TRUE;
2245
2246 inq_params.filter_type = BTA_DM_INQ_CLR;
2247 /* TODO: Filter device by BDA needs to be implemented here */
2248
2249 /* Will be enabled to TRUE once inquiry busy level has been received */
2250 btif_dm_inquiry_in_progress = FALSE;
2251 /* find nearby devices */
2252 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
2253
2254 return BT_STATUS_SUCCESS;
2255}
2256
2257/*******************************************************************************
2258**
2259** Function btif_dm_cancel_discovery
2260**
2261** Description Cancels search
2262**
2263** Returns bt_status_t
2264**
2265*******************************************************************************/
2266bt_status_t btif_dm_cancel_discovery(void)
2267{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002268 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002269 BTA_DmSearchCancel();
2270 return BT_STATUS_SUCCESS;
2271}
2272
2273/*******************************************************************************
2274**
2275** Function btif_dm_create_bond
2276**
2277** Description Initiate bonding with the specified device
2278**
2279** Returns bt_status_t
2280**
2281*******************************************************************************/
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002282bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr, int transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002283{
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002284 btif_dm_create_bond_cb_t create_bond_cb;
2285 create_bond_cb.transport = transport;
2286 bdcpy(create_bond_cb.bdaddr.address, bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002287
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002288 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002289 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 -08002290 if (pairing_cb.state != BT_BOND_STATE_NONE)
2291 return BT_STATUS_BUSY;
2292
2293 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002294 (char *)&create_bond_cb, sizeof(btif_dm_create_bond_cb_t), NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002295
2296 return BT_STATUS_SUCCESS;
2297}
2298
2299/*******************************************************************************
2300**
2301** Function btif_dm_cancel_bond
2302**
2303** Description Initiate bonding with the specified device
2304**
2305** Returns bt_status_t
2306**
2307*******************************************************************************/
2308
2309bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
2310{
2311 bdstr_t bdstr;
2312
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002313 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 -08002314
2315 /* TODO:
2316 ** 1. Restore scan modes
2317 ** 2. special handling for HID devices
2318 */
2319 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2320 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002321
2322#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2323
2324 if (pairing_cb.is_ssp)
2325 {
2326 if (pairing_cb.is_le_only)
2327 {
2328 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2329 }
2330 else
Hemant Guptae1468692013-11-14 16:21:29 +05302331 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002332 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
Hemant Guptae1468692013-11-14 16:21:29 +05302333 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2334 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2335 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002336 }
2337 else
2338 {
2339 if (pairing_cb.is_le_only)
2340 {
2341 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2342 }
2343 else
2344 {
2345 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2346 }
2347 /* Cancel bonding, in case it is in ACL connection setup state */
2348 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2349 }
2350
2351#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002352 if (pairing_cb.is_ssp)
2353 {
2354 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2355 }
2356 else
2357 {
2358 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2359 }
2360 /* Cancel bonding, in case it is in ACL connection setup state */
2361 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2362 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002363#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002364 }
2365
2366 return BT_STATUS_SUCCESS;
2367}
2368
2369/*******************************************************************************
2370**
Kim Schulza9eb25c2013-09-30 10:55:52 +02002371** Function btif_dm_hh_open_failed
2372**
2373** Description informs the upper layers if the HH have failed during bonding
2374**
2375** Returns none
2376**
2377*******************************************************************************/
2378
2379void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
2380{
2381 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2382 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2383 {
2384 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2385 }
2386}
2387
2388/*******************************************************************************
2389**
The Android Open Source Project5738f832012-12-12 16:00:35 -08002390** Function btif_dm_remove_bond
2391**
2392** Description Removes bonding with the specified device
2393**
2394** Returns bt_status_t
2395**
2396*******************************************************************************/
2397
2398bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
2399{
2400 bdstr_t bdstr;
2401
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002402 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 -08002403 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2404 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2405
2406 return BT_STATUS_SUCCESS;
2407}
2408
2409/*******************************************************************************
2410**
2411** Function btif_dm_pin_reply
2412**
2413** Description BT legacy pairing - PIN code reply
2414**
2415** Returns bt_status_t
2416**
2417*******************************************************************************/
2418
2419bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2420 uint8_t pin_len, bt_pin_code_t *pin_code)
2421{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002422 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Hemant Gupta831423e2014-01-08 12:42:13 +05302423 if (pin_code == NULL)
2424 return BT_STATUS_FAIL;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002425#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002426
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002427 if (pairing_cb.is_le_only)
2428 {
2429 int i;
2430 UINT32 passkey = 0;
2431 int multi[] = {100000, 10000, 1000, 100, 10,1};
2432 BD_ADDR remote_bd_addr;
2433 bdcpy(remote_bd_addr, bd_addr->address);
2434 for (i = 0; i < 6; i++)
2435 {
2436 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2437 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002438 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002439 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2440
2441 }
2442 else
2443 {
2444 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2445 if (accept)
2446 pairing_cb.pin_code_len = pin_len;
2447 }
2448#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002449 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2450
2451 if (accept)
2452 pairing_cb.pin_code_len = pin_len;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002453#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002454 return BT_STATUS_SUCCESS;
2455}
2456
2457/*******************************************************************************
2458**
2459** Function btif_dm_ssp_reply
2460**
2461** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2462**
2463** Returns bt_status_t
2464**
2465*******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002466bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2467 bt_ssp_variant_t variant, uint8_t accept,
2468 uint32_t passkey)
2469{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08002470 UNUSED(passkey);
2471
The Android Open Source Project5738f832012-12-12 16:00:35 -08002472 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2473 {
2474 /* This is not implemented in the stack.
2475 * For devices with display, this is not needed
2476 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002477 BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002478 return BT_STATUS_FAIL;
2479 }
2480 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002481 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002482#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2483 if (pairing_cb.is_le_only)
2484 {
2485 if (accept)
2486 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2487 else
2488 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2489 }
2490 else
2491 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002492
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002493#else
2494 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2495#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002496 return BT_STATUS_SUCCESS;
2497}
2498
2499/*******************************************************************************
2500**
2501** Function btif_dm_get_adapter_property
2502**
2503** Description Queries the BTA for the adapter property
2504**
2505** Returns bt_status_t
2506**
2507*******************************************************************************/
2508bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2509{
2510 bt_status_t status;
2511
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002512 BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002513 switch (prop->type)
2514 {
2515 case BT_PROPERTY_BDNAME:
2516 {
2517 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
VenkatRaghavan VijayaRaghavan4540f592015-02-05 04:40:47 -08002518 strncpy((char *)bd_name->name, (char *)btif_get_default_local_name(),
VenkatRaghavan VijayaRaghavan1d8e6b82015-02-05 22:20:39 -08002519 sizeof(bd_name->name) - 1);
2520 bd_name->name[sizeof(bd_name->name) - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002521 prop->len = strlen((char *)bd_name->name);
2522 }
2523 break;
2524
2525 case BT_PROPERTY_ADAPTER_SCAN_MODE:
2526 {
2527 /* if the storage does not have it. Most likely app never set it. Default is NONE */
2528 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2529 *mode = BT_SCAN_MODE_NONE;
2530 prop->len = sizeof(bt_scan_mode_t);
2531 }
2532 break;
2533
2534 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2535 {
2536 uint32_t *tmt = (uint32_t*)prop->val;
2537 *tmt = 120; /* default to 120s, if not found in NV */
2538 prop->len = sizeof(uint32_t);
2539 }
2540 break;
2541
2542 default:
2543 prop->len = 0;
2544 return BT_STATUS_FAIL;
2545 }
2546 return BT_STATUS_SUCCESS;
2547}
2548
2549/*******************************************************************************
2550**
2551** Function btif_dm_get_remote_services
2552**
2553** Description Start SDP to get remote services
2554**
2555** Returns bt_status_t
2556**
2557*******************************************************************************/
2558bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2559{
2560 bdstr_t bdstr;
2561
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002562 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 -08002563
2564 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2565 bte_dm_search_services_evt, TRUE);
2566
2567 return BT_STATUS_SUCCESS;
2568}
2569
2570/*******************************************************************************
2571**
2572** Function btif_dm_get_remote_service_record
2573**
2574** Description Start SDP to get remote service record
2575**
2576**
2577** Returns bt_status_t
2578*******************************************************************************/
2579bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2580 bt_uuid_t *uuid)
2581{
2582 tSDP_UUID sdp_uuid;
2583 bdstr_t bdstr;
2584
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002585 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 -08002586
2587 sdp_uuid.len = MAX_UUID_SIZE;
2588 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2589
2590 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2591 bte_dm_remote_service_record_evt, TRUE);
2592
2593 return BT_STATUS_SUCCESS;
2594}
2595
2596void btif_dm_execute_service_request(UINT16 event, char *p_param)
2597{
2598 BOOLEAN b_enable = FALSE;
2599 bt_status_t status;
2600 if (event == BTIF_DM_ENABLE_SERVICE)
2601 {
2602 b_enable = TRUE;
2603 }
2604 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2605 if (status == BT_STATUS_SUCCESS)
2606 {
2607 bt_property_t property;
2608 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2609
2610 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2611 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2612 sizeof(local_uuids), local_uuids);
2613 btif_storage_get_adapter_property(&property);
2614 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2615 BT_STATUS_SUCCESS, 1, &property);
2616 }
2617 return;
2618}
2619
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002620void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2621 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2622{
2623 UINT8 yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2624 /* if local initiated:
2625 ** 1. set DD + MITM
2626 ** if remote initiated:
2627 ** 1. Copy over the auth_req from peer's io_rsp
2628 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2629 ** as a fallback set MITM+GB if peer had MITM set
2630 */
2631 UNUSED (bd_addr);
2632 UNUSED (p_io_cap);
2633 UNUSED (p_oob_data);
2634
2635
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002636 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002637 if(pairing_cb.is_local_initiated)
2638 {
2639 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2640 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2641 }
2642 else if (!is_orig)
2643 {
2644 /* peer initiated paring. They probably know what they want.
2645 ** Copy the mitm from peer device.
2646 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002647 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002648 __FUNCTION__, pairing_cb.auth_req);
2649 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2650
2651 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2652 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2653 *p_auth_req |= BTA_AUTH_SP_YES;
2654 }
2655 else if (yes_no_bit)
2656 {
2657 /* set the general bonding bit for stored device */
2658 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2659 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002660 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002661}
2662
2663void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2664 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2665{
2666 UNUSED (bd_addr);
2667 UNUSED (oob_data);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002668
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002669 if(auth_req & BTA_AUTH_BONDS)
2670 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002671 BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002672 pairing_cb.auth_req = auth_req;
2673 pairing_cb.io_cap = io_cap;
2674 }
2675}
2676
The Android Open Source Project5738f832012-12-12 16:00:35 -08002677#if (BTM_OOB_INCLUDED == TRUE)
2678void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data)
2679{
2680 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2681 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2682 {
2683 *p_oob_data = FALSE;
2684 }
2685 else
2686 {
2687 *p_oob_data = TRUE;
2688 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002689 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 -08002690}
2691#endif /* BTM_OOB_INCLUDED */
2692
2693#ifdef BTIF_DM_OOB_TEST
2694void btif_dm_load_local_oob(void)
2695{
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002696 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002697 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002698 BTIF_TRACE_DEBUG("btif_dm_load_local_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002699 if (prop_oob[0] != '3')
2700 {
2701#if (BTM_OOB_INCLUDED == TRUE)
2702 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2703 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2704 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002705 BTIF_TRACE_DEBUG("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002706 BTA_DmLocalOob();
2707 }
2708#else
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002709 BTIF_TRACE_ERROR("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002710#endif
2711 }
2712}
2713
2714void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2715{
2716 FILE *fp;
2717 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2718 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2719 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002720 char prop_oob[PROPERTY_VALUE_MAX];
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002721 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: valid=%d", valid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002722 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2723 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2724 valid)
2725 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002726 BTIF_TRACE_DEBUG("save local OOB data in memory");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002727 memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2728 memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2729 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002730 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002731 if (prop_oob[0] == '1')
2732 path = path_a;
2733 else if (prop_oob[0] == '2')
2734 path = path_b;
2735 if (path)
2736 {
2737 fp = fopen(path, "wb+");
2738 if (fp == NULL)
2739 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002740 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 -08002741 }
2742 else
2743 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002744 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 -08002745 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2746 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2747 fclose(fp);
2748 }
2749 }
2750 }
2751}
2752BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2753{
2754 char t[128];
2755 FILE *fp;
2756 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2757 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2758 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002759 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002760 BOOLEAN result = FALSE;
2761 bt_bdaddr_t bt_bd_addr;
2762 bdcpy(oob_cb.oob_bdaddr, bd_addr);
2763 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002764 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002765 if (prop_oob[0] == '1')
2766 path = path_b;
2767 else if (prop_oob[0] == '2')
2768 path = path_a;
2769 if (path)
2770 {
2771 fp = fopen(path, "rb");
2772 if (fp == NULL)
2773 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002774 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 -08002775 return FALSE;
2776 }
2777 else
2778 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002779 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob: read OOB data from %s",path);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002780 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2781 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2782 fclose(fp);
2783 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002784 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: TRUE");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002785 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2786 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2787 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002788 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002789 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2790 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
2791 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 -07002792 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: c = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002793 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2794 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
2795 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 -07002796 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: r = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002797 bdcpy(bt_bd_addr.address, bd_addr);
2798 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2799 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2800 result = TRUE;
2801 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002802 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob result=%d",result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002803 return result;
2804}
2805#endif /* BTIF_DM_OOB_TEST */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002806#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2807
2808static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2809{
2810 bt_bdaddr_t bd_addr;
2811 bt_bdname_t bd_name;
2812 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08002813 int dev_type;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002814
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002815 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002816
2817 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08002818 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
2819 {
2820 dev_type = BT_DEVICE_TYPE_BLE;
2821 }
2822 btif_dm_update_ble_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2823 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002824 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2825 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2826
2827 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2828 pairing_cb.is_ssp = FALSE;
2829 cod = COD_UNCLASSIFIED;
2830
2831 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2832 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2833 p_ssp_key_notif->passkey);
2834}
2835
2836/*******************************************************************************
2837**
2838** Function btif_dm_ble_auth_cmpl_evt
2839**
2840** Description Executes authentication complete event in btif context
2841**
2842** Returns void
2843**
2844*******************************************************************************/
2845static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2846{
2847 /* Save link key, if not temporary */
2848 bt_bdaddr_t bd_addr;
2849 bt_status_t status = BT_STATUS_FAIL;
2850 bt_bond_state_t state = BT_BOND_STATE_NONE;
2851
2852 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2853 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2854 {
2855 /* store keys */
2856 }
2857 if (p_auth_cmpl->success)
2858 {
2859 status = BT_STATUS_SUCCESS;
2860 state = BT_BOND_STATE_BONDED;
2861
2862 btif_dm_save_ble_bonding_keys();
2863 BTA_GATTC_Refresh(bd_addr.address);
2864 btif_dm_get_remote_services(&bd_addr);
2865 }
2866 else
2867 {
2868 /*Map the HCI fail reason to bt status */
2869 switch (p_auth_cmpl->fail_reason)
2870 {
Priti Aghera156c52b2014-07-09 14:58:19 -07002871 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
2872 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
2873 btif_dm_remove_ble_bonding_keys();
2874 status = BT_STATUS_AUTH_FAILURE;
2875 break;
2876 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
2877 status = BT_STATUS_AUTH_REJECTED;
2878 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002879 default:
Andre Eisenbachca22ac42013-02-13 17:02:11 +09002880 btif_dm_remove_ble_bonding_keys();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002881 status = BT_STATUS_FAIL;
2882 break;
2883 }
2884 }
2885 bond_state_changed(status, &bd_addr, state);
2886}
2887
2888
2889
2890void btif_dm_load_ble_local_keys(void)
2891{
2892 bt_status_t bt_status;
2893
2894 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2895
2896 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2897 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2898 {
2899 ble_local_key_cb.is_er_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002900 BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002901 }
2902
2903 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
2904 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
2905 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
2906 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
2907 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
2908 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
2909 {
2910 ble_local_key_cb.is_id_keys_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002911 BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002912 }
2913
2914}
2915void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
2916 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
2917{
2918 if (ble_local_key_cb.is_er_rcvd )
2919 {
2920 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2921 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2922 }
2923
2924 if (ble_local_key_cb.is_id_keys_rcvd)
2925 {
2926 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
2927 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
2928 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
2929 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
2930 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002931 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002932}
2933
2934void btif_dm_save_ble_bonding_keys(void)
2935{
2936
2937 bt_bdaddr_t bd_addr;
2938
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002939 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002940
2941 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2942
2943 if (pairing_cb.ble.is_penc_key_rcvd)
2944 {
2945 btif_storage_add_ble_bonding_key(&bd_addr,
2946 (char *) &pairing_cb.ble.penc_key,
2947 BTIF_DM_LE_KEY_PENC,
2948 sizeof(btif_dm_ble_penc_keys_t));
2949 }
2950
2951 if (pairing_cb.ble.is_pid_key_rcvd)
2952 {
2953 btif_storage_add_ble_bonding_key(&bd_addr,
Andre Eisenbach5e808462014-10-21 12:37:53 -07002954 (char *) &pairing_cb.ble.pid_key,
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002955 BTIF_DM_LE_KEY_PID,
Andre Eisenbach5e808462014-10-21 12:37:53 -07002956 sizeof(btif_dm_ble_pid_keys_t));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002957 }
2958
2959
2960 if (pairing_cb.ble.is_pcsrk_key_rcvd)
2961 {
2962 btif_storage_add_ble_bonding_key(&bd_addr,
2963 (char *) &pairing_cb.ble.pcsrk_key,
2964 BTIF_DM_LE_KEY_PCSRK,
2965 sizeof(btif_dm_ble_pcsrk_keys_t));
2966 }
2967
2968
2969 if (pairing_cb.ble.is_lenc_key_rcvd)
2970 {
2971 btif_storage_add_ble_bonding_key(&bd_addr,
2972 (char *) &pairing_cb.ble.lenc_key,
2973 BTIF_DM_LE_KEY_LENC,
2974 sizeof(btif_dm_ble_lenc_keys_t));
2975 }
2976
2977 if (pairing_cb.ble.is_lcsrk_key_rcvd)
2978 {
2979 btif_storage_add_ble_bonding_key(&bd_addr,
2980 (char *) &pairing_cb.ble.lcsrk_key,
2981 BTIF_DM_LE_KEY_LCSRK,
2982 sizeof(btif_dm_ble_lcsrk_keys_t));
2983 }
2984
2985}
2986
2987
2988void btif_dm_remove_ble_bonding_keys(void)
2989{
2990 bt_bdaddr_t bd_addr;
2991
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002992 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002993
2994 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2995 btif_storage_remove_ble_bonding_keys(&bd_addr);
2996}
2997
2998
2999/*******************************************************************************
3000**
3001** Function btif_dm_ble_sec_req_evt
3002**
3003** Description Eprocess security request event in btif context
3004**
3005** Returns void
3006**
3007*******************************************************************************/
3008void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
3009{
3010 bt_bdaddr_t bd_addr;
3011 bt_bdname_t bd_name;
3012 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08003013 int dev_type;
3014
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003015 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003016
3017 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3018 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003019 BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003020 return;
3021 }
3022
3023 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08003024 if (!btif_get_device_type(p_ble_req->bd_addr, &dev_type))
3025 {
3026 dev_type = BT_DEVICE_TYPE_BLE;
3027 }
3028 btif_dm_update_ble_remote_properties(p_ble_req->bd_addr, p_ble_req->bd_name,
3029 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003030
3031 bdcpy(bd_addr.address, p_ble_req->bd_addr);
3032 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
3033
3034 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3035
Andre Eisenbach89363762015-01-26 13:49:36 -08003036 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003037 pairing_cb.is_le_only = TRUE;
3038 pairing_cb.is_ssp = TRUE;
3039
3040 cod = COD_UNCLASSIFIED;
3041
3042 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
3043 BT_SSP_VARIANT_CONSENT, 0);
3044}
3045
3046
3047
3048/*******************************************************************************
3049**
3050** Function btif_dm_ble_passkey_req_evt
3051**
3052** Description Executes pin request event in btif context
3053**
3054** Returns void
3055**
3056*******************************************************************************/
3057static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
3058{
3059 bt_bdaddr_t bd_addr;
3060 bt_bdname_t bd_name;
3061 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08003062 int dev_type;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003063
3064 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08003065 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
3066 {
3067 dev_type = BT_DEVICE_TYPE_BLE;
3068 }
3069 btif_dm_update_ble_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,
3070 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003071
3072 bdcpy(bd_addr.address, p_pin_req->bd_addr);
3073 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
3074
3075 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3076 pairing_cb.is_le_only = TRUE;
3077
3078 cod = COD_UNCLASSIFIED;
3079
3080 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
3081 &bd_addr, &bd_name, cod);
3082}
3083
3084
3085void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
3086 tBT_DEVICE_TYPE dev_type)
3087{
3088 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
3089}
3090
3091static void btif_dm_ble_tx_test_cback(void *p)
3092{
3093 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
3094 (char *)p, 1, NULL);
3095}
3096
3097static void btif_dm_ble_rx_test_cback(void *p)
3098{
3099 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
3100 (char *)p, 1, NULL);
3101}
3102
3103static void btif_dm_ble_test_end_cback(void *p)
3104{
3105 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
3106 (char *)p, 3, NULL);
3107}
3108/*******************************************************************************
3109**
3110** Function btif_le_test_mode
3111**
3112** Description Sends a HCI BLE Test command to the Controller
3113**
3114** Returns BT_STATUS_SUCCESS on success
3115**
3116*******************************************************************************/
3117bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
3118{
3119 switch (opcode) {
3120 case HCI_BLE_TRANSMITTER_TEST:
3121 if (len != 3) return BT_STATUS_PARM_INVALID;
3122 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
3123 break;
3124 case HCI_BLE_RECEIVER_TEST:
3125 if (len != 1) return BT_STATUS_PARM_INVALID;
3126 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
3127 break;
3128 case HCI_BLE_TEST_END:
3129 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
3130 break;
3131 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003132 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003133 return BT_STATUS_UNSUPPORTED;
3134 }
3135 return BT_STATUS_SUCCESS;
3136}
3137
3138#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08003139
3140void btif_dm_on_disable()
3141{
3142 /* cancel any pending pairing requests */
3143 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3144 {
3145 bt_bdaddr_t bd_addr;
3146
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003147 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003148 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3149 btif_dm_cancel_bond(&bd_addr);
3150 }
3151}
Matthew Xie1e5109b2012-11-09 18:26:26 -08003152
Satya Callojie5ba8842014-07-03 17:18:02 -07003153/*******************************************************************************
3154**
3155** Function btif_dm_read_energy_info
3156**
3157** Description Reads the energy info from controller
3158**
3159** Returns void
3160**
3161*******************************************************************************/
3162void btif_dm_read_energy_info()
3163{
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003164#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07003165 BTA_DmBleGetEnergyInfo(bta_energy_info_cb);
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003166#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07003167}
3168
Matthew Xie1e5109b2012-11-09 18:26:26 -08003169static char* btif_get_default_local_name() {
3170 if (btif_default_local_name[0] == '\0')
3171 {
3172 int max_len = sizeof(btif_default_local_name) - 1;
3173 if (BTM_DEF_LOCAL_NAME[0] != '\0')
3174 {
3175 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
3176 }
3177 else
3178 {
3179 char prop_model[PROPERTY_VALUE_MAX];
3180 property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
3181 strncpy(btif_default_local_name, prop_model, max_len);
3182 }
3183 btif_default_local_name[max_len] = '\0';
3184 }
3185 return btif_default_local_name;
3186}