blob: a317a946d2ae3fbbf2c3fda6f817c42eee44f669 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
3 * Copyright (C) 2009-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19/************************************************************************************
20 *
21 * Filename: btif_dm.c
22 *
23 * Description: Contains Device Management (DM) related functionality
24 *
25 *
26 ***********************************************************************************/
Sharvil Nanavati44802762014-12-23 23:08:58 -080027
Chris Mantonf8027002015-03-12 09:22:48 -070028#define LOG_TAG "bt_btif_dm"
Sharvil Nanavati44802762014-12-23 23:08:58 -080029
The Android Open Source Project5738f832012-12-12 16:00:35 -080030#include <stdio.h>
31#include <stdlib.h>
Ian Coolidgec7503db2015-01-24 02:01:26 -080032#include <string.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080033#include <unistd.h>
34
35#include <hardware/bluetooth.h>
36
The Android Open Source Project5738f832012-12-12 16:00:35 -080037#include <cutils/properties.h>
38#include "gki.h"
39#include "btu.h"
Sharvil Nanavati95b74f22015-03-12 15:55:21 -070040#include "btcore/include/bdaddr.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080041#include "bta_api.h"
42#include "btif_api.h"
43#include "btif_util.h"
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080044#include "btif_dm.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080045#include "btif_storage.h"
46#include "btif_hh.h"
47#include "btif_config.h"
48
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080049#include "bta_gatt_api.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080050#include "include/stack_config.h"
Chris Mantonf8027002015-03-12 09:22:48 -070051
Sharvil Nanavati44802762014-12-23 23:08:58 -080052#include "osi/include/log.h"
Andre Eisenbach31a64002014-10-14 14:29:19 -070053
54/******************************************************************************
55** Device specific workarounds
56******************************************************************************/
57
58/**
59 * The devices below have proven problematic during the pairing process, often
60 * requiring multiple retries to complete pairing. To avoid degrading the user
61 * experience for other devices, explicitely blacklist troubled devices here.
62 */
63static const UINT8 blacklist_pairing_retries[][3] = {
64 {0x9C, 0xDF, 0x03} // BMW car kits (Harman/Becker)
65};
66
67BOOLEAN blacklistPairingRetries(BD_ADDR bd_addr)
68{
69 const unsigned blacklist_size = sizeof(blacklist_pairing_retries)
70 / sizeof(blacklist_pairing_retries[0]);
71 for (unsigned i = 0; i != blacklist_size; ++i)
72 {
73 if (blacklist_pairing_retries[i][0] == bd_addr[0] &&
74 blacklist_pairing_retries[i][1] == bd_addr[1] &&
75 blacklist_pairing_retries[i][2] == bd_addr[2])
76 return TRUE;
77 }
78 return FALSE;
79}
80
The Android Open Source Project5738f832012-12-12 16:00:35 -080081/******************************************************************************
82** Constants & Macros
83******************************************************************************/
84
85#define COD_UNCLASSIFIED ((0x1F) << 8)
Priti Agheraebb1d752012-11-27 18:03:22 -080086#define COD_HID_KEYBOARD 0x0540
87#define COD_HID_POINTING 0x0580
88#define COD_HID_COMBO 0x05C0
89#define COD_HID_MAJOR 0x0500
90#define COD_AV_HEADSETS 0x0404
91#define COD_AV_HANDSFREE 0x0408
92#define COD_AV_HEADPHONES 0x0418
93#define COD_AV_PORTABLE_AUDIO 0x041C
94#define COD_AV_HIFI_AUDIO 0x0428
The Android Open Source Project5738f832012-12-12 16:00:35 -080095
96
97#define BTIF_DM_DEFAULT_INQ_MAX_RESULTS 0
98#define BTIF_DM_DEFAULT_INQ_MAX_DURATION 10
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -070099#define BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING 2
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800100
Andre Eisenbach31a64002014-10-14 14:29:19 -0700101#define NUM_TIMEOUT_RETRIES 5
102
Matthew Xie1e5109b2012-11-09 18:26:26 -0800103#define PROPERTY_PRODUCT_MODEL "ro.product.model"
Matthew Xiea30d95a2013-09-18 12:30:36 -0700104#define DEFAULT_LOCAL_NAME_MAX 31
Matthew Xie1e5109b2012-11-09 18:26:26 -0800105#if (DEFAULT_LOCAL_NAME_MAX > BTM_MAX_LOC_BD_NAME_LEN)
106 #error "default btif local name size exceeds stack supported length"
107#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800108
Matthew Xie7f3e4292013-09-30 12:44:10 -0700109#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
110#define BTIF_DM_INTERLEAVE_DURATION_BR_ONE 2
111#define BTIF_DM_INTERLEAVE_DURATION_LE_ONE 2
112#define BTIF_DM_INTERLEAVE_DURATION_BR_TWO 3
113#define BTIF_DM_INTERLEAVE_DURATION_LE_TWO 4
114#endif
115
Priti Agherac0edf9f2014-06-26 11:23:51 -0700116#define MAX_SDP_BL_ENTRIES 3
117
Andre Eisenbach89363762015-01-26 13:49:36 -0800118#define BOND_TYPE_UNKNOWN 0
119#define BOND_TYPE_PERSISTENT 1
120#define BOND_TYPE_TEMPORARY 2
121
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800122#define ENCRYPTED_BREDR 2
123#define ENCRYPTED_LE 4
124
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800125typedef struct
126{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800127 bt_bond_state_t state;
128 BD_ADDR bd_addr;
Andre Eisenbach89363762015-01-26 13:49:36 -0800129 UINT8 bond_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800130 UINT8 pin_code_len;
131 UINT8 is_ssp;
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -0700132 UINT8 auth_req;
133 UINT8 io_cap;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800134 UINT8 autopair_attempts;
Andre Eisenbach31a64002014-10-14 14:29:19 -0700135 UINT8 timeout_retries;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800136 UINT8 is_local_initiated;
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700137 UINT8 sdp_attempts;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800138#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
139 BOOLEAN is_le_only;
140 btif_dm_ble_cb_t ble;
141#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800142} btif_dm_pairing_cb_t;
143
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800144
145typedef struct
146{
147 UINT8 ir[BT_OCTET16_LEN];
148 UINT8 irk[BT_OCTET16_LEN];
149 UINT8 dhk[BT_OCTET16_LEN];
150}btif_dm_local_key_id_t;
151
152typedef struct
153{
154 BOOLEAN is_er_rcvd;
155 UINT8 er[BT_OCTET16_LEN];
156 BOOLEAN is_id_keys_rcvd;
157 btif_dm_local_key_id_t id_keys; /* ID kyes */
158
159}btif_dm_local_key_cb_t;
160
161typedef struct
162{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800163 BD_ADDR bd_addr;
164 BD_NAME bd_name;
165} btif_dm_remote_name_t;
166
167typedef struct
168{
169 BT_OCTET16 sp_c;
170 BT_OCTET16 sp_r;
171 BD_ADDR oob_bdaddr; /* peer bdaddr*/
172} btif_dm_oob_cb_t;
Satya Callojie5ba8842014-07-03 17:18:02 -0700173
174typedef struct
175{
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700176 bt_bdaddr_t bdaddr;
177 UINT8 transport; /* 0=Unknown, 1=BR/EDR, 2=LE */
178} btif_dm_create_bond_cb_t;
179
180typedef struct
181{
Satya Callojie5ba8842014-07-03 17:18:02 -0700182 uint8_t status;
183 uint8_t ctrl_state;
184 uint64_t tx_time;
185 uint64_t rx_time;
186 uint64_t idle_time;
187 uint64_t energy_used;
188} btif_activity_energy_info_cb_t;
189
Priti Agherac0edf9f2014-06-26 11:23:51 -0700190typedef struct
191{
192 unsigned int manufact_id;
193}skip_sdp_entry_t;
194
The Android Open Source Project5738f832012-12-12 16:00:35 -0800195#define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id))
196
Priti Agherac0edf9f2014-06-26 11:23:51 -0700197#define MAX_SDP_BL_ENTRIES 3
198#define UUID_HUMAN_INTERFACE_DEVICE "00001124-0000-1000-8000-00805f9b34fb"
199
200static skip_sdp_entry_t sdp_blacklist[] = {{76}}; //Apple Mouse and Keyboard
201
202
The Android Open Source Project5738f832012-12-12 16:00:35 -0800203/* This flag will be true if HCI_Inquiry is in progress */
204static BOOLEAN btif_dm_inquiry_in_progress = FALSE;
205
Priti Agherac0edf9f2014-06-26 11:23:51 -0700206
207
Matthew Xie1e5109b2012-11-09 18:26:26 -0800208/************************************************************************************
209** Static variables
210************************************************************************************/
211static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'};
212
The Android Open Source Project5738f832012-12-12 16:00:35 -0800213/******************************************************************************
214** Static functions
215******************************************************************************/
216static btif_dm_pairing_cb_t pairing_cb;
217static btif_dm_oob_cb_t oob_cb;
218static void btif_dm_generic_evt(UINT16 event, char* p_param);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700219static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800220static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name);
221static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
222 DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800223#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
224static btif_dm_local_key_cb_t ble_local_key_cb;
225static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif);
226static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl);
227static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req);
228#endif
Satya Calloji6e2d9db2014-07-08 16:18:58 -0700229
230static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
231 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
232 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status);
233
Matthew Xie1e5109b2012-11-09 18:26:26 -0800234static char* btif_get_default_local_name();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800235/******************************************************************************
236** Externs
237******************************************************************************/
238extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
239extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
240extern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
241extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
Hemant Gupta10256872013-08-19 18:33:01 +0530242extern bt_status_t btif_hf_client_execute_service(BOOLEAN b_enable);
Hemant Gupta2dc99992014-04-18 12:54:08 +0530243extern bt_status_t btif_mce_execute_service(BOOLEAN b_enable);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800244extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700245extern 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 -0800246
247
248/******************************************************************************
249** Functions
250******************************************************************************/
251
252bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
253 BOOLEAN b_enable)
254{
255 /* Check the service_ID and invoke the profile's BT state changed API */
256 switch (service_id)
257 {
258 case BTA_HFP_SERVICE_ID:
259 case BTA_HSP_SERVICE_ID:
260 {
261 btif_hf_execute_service(b_enable);
262 }break;
Sharvil Nanavati9609cee2014-10-15 18:30:49 -0700263 case BTA_A2DP_SOURCE_SERVICE_ID:
The Android Open Source Project5738f832012-12-12 16:00:35 -0800264 {
265 btif_av_execute_service(b_enable);
266 }break;
267 case BTA_HID_SERVICE_ID:
268 {
269 btif_hh_execute_service(b_enable);
270 }break;
Hemant Gupta10256872013-08-19 18:33:01 +0530271 case BTA_HFP_HS_SERVICE_ID:
272 {
273 btif_hf_client_execute_service(b_enable);
274 }break;
Hemant Gupta2dc99992014-04-18 12:54:08 +0530275 case BTA_MAP_SERVICE_ID:
276 {
277 btif_mce_execute_service(b_enable);
278 }break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800279 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700280 BTIF_TRACE_ERROR("%s: Unknown service being enabled", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800281 return BT_STATUS_FAIL;
282 }
283 return BT_STATUS_SUCCESS;
284}
285
286/*******************************************************************************
287**
288** Function check_eir_remote_name
289**
290** Description Check if remote name is in the EIR data
291**
292** Returns TRUE if remote name found
293** Populate p_remote_name, if provided and remote name found
294**
295*******************************************************************************/
296static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
297 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
298{
299 UINT8 *p_eir_remote_name = NULL;
300 UINT8 remote_name_len = 0;
301
302 /* Check EIR for remote name and services */
303 if (p_search_data->inq_res.p_eir)
304 {
Zach Johnsona50fc882014-10-30 21:02:58 -0700305 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800306 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
307 if (!p_eir_remote_name)
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_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
311 }
312
313 if (p_eir_remote_name)
314 {
315 if (remote_name_len > BD_NAME_LEN)
316 remote_name_len = BD_NAME_LEN;
317
318 if (p_remote_name && p_remote_name_len)
319 {
320 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
321 *(p_remote_name + remote_name_len) = 0;
322 *p_remote_name_len = remote_name_len;
323 }
324
325 return TRUE;
326 }
327 }
328
329 return FALSE;
330
331}
332
333/*******************************************************************************
334**
335** Function check_cached_remote_name
336**
337** Description Check if remote name is in the NVRAM cache
338**
339** Returns TRUE if remote name found
340** Populate p_remote_name, if provided and remote name found
341**
342*******************************************************************************/
343static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
344 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
345{
346 bt_bdname_t bdname;
347 bt_bdaddr_t remote_bdaddr;
348 bt_property_t prop_name;
349
350 /* check if we already have it in our btif_storage cache */
351 bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
352 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
353 sizeof(bt_bdname_t), &bdname);
354 if (btif_storage_get_remote_device_property(
355 &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
356 {
357 if (p_remote_name && p_remote_name_len)
358 {
359 strcpy((char *)p_remote_name, (char *)bdname.name);
360 *p_remote_name_len = strlen((char *)p_remote_name);
361 }
362 return TRUE;
363 }
364
365 return FALSE;
366}
367
368BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
369{
370 uint32_t remote_cod;
371 bt_property_t prop_name;
372
373 /* check if we already have it in our btif_storage cache */
374 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
375 sizeof(uint32_t), &remote_cod);
376 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
377 {
Chris Mantonf8027002015-03-12 09:22:48 -0700378 LOG_INFO("%s remote_cod = 0x%08x cod = 0x%08x", __func__, remote_cod, cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800379 if ((remote_cod & 0x7ff) == cod)
380 return TRUE;
381 }
382
383 return FALSE;
384}
385
Priti Agheraebb1d752012-11-27 18:03:22 -0800386BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
387{
388 uint32_t remote_cod;
389 bt_property_t prop_name;
390
391 /* check if we already have it in our btif_storage cache */
392 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
393 sizeof(uint32_t), &remote_cod);
394 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
395 &prop_name) == BT_STATUS_SUCCESS)
396 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700397 BTIF_TRACE_DEBUG("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
Priti Agheraebb1d752012-11-27 18:03:22 -0800398 if ((remote_cod & 0x700) == cod)
399 return TRUE;
400 }
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700401 return FALSE;
402}
Priti Agheraebb1d752012-11-27 18:03:22 -0800403
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700404BOOLEAN check_hid_le(const bt_bdaddr_t *remote_bdaddr)
405{
406 uint32_t remote_dev_type;
407 bt_property_t prop_name;
408
409 /* check if we already have it in our btif_storage cache */
410 BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE,
411 sizeof(uint32_t), &remote_dev_type);
412 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
413 &prop_name) == BT_STATUS_SUCCESS)
414 {
415 if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE)
416 {
417 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -0700418 bdaddr_to_string(remote_bdaddr, bdstr, sizeof(bdstr));
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700419 if(btif_config_exist(bdstr, "HidAppId"))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700420 return TRUE;
421 }
422 }
Priti Agheraebb1d752012-11-27 18:03:22 -0800423 return FALSE;
424}
425
Priti Agherac0edf9f2014-06-26 11:23:51 -0700426/*****************************************************************************
427**
428** Function check_sdp_bl
429**
430** Description Checks if a given device is blacklisted to skip sdp
431**
432** Parameters skip_sdp_entry
433**
434** Returns TRUE if the device is present in blacklist, else FALSE
435**
436*******************************************************************************/
437BOOLEAN check_sdp_bl(const bt_bdaddr_t *remote_bdaddr)
438{
Priti Agherac0edf9f2014-06-26 11:23:51 -0700439 UINT16 manufacturer = 0;
440 UINT8 lmp_ver = 0;
441 UINT16 lmp_subver = 0;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700442 bt_property_t prop_name;
443 bt_remote_version_t info;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700444
445
446 if (remote_bdaddr == NULL)
447 return FALSE;
448
449/* fetch additional info about remote device used in iop query */
Sharvil Nanavatif1c764f2015-02-23 17:31:48 -0800450 BTM_ReadRemoteVersion(*(BD_ADDR*)remote_bdaddr, &lmp_ver,
Priti Agherac0edf9f2014-06-26 11:23:51 -0700451 &manufacturer, &lmp_subver);
452
453
454
455 /* if not available yet, try fetching from config database */
456 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO,
457 sizeof(bt_remote_version_t), &info);
458
459 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
460 &prop_name) != BT_STATUS_SUCCESS)
461 {
462
463 return FALSE;
464 }
465 manufacturer = info.manufacturer;
466
467 for (int i = 0; i < MAX_SDP_BL_ENTRIES; i++)
468 {
469 if (manufacturer == sdp_blacklist[i].manufact_id)
470 return TRUE;
471 }
472 return FALSE;
473}
474
475
The Android Open Source Project5738f832012-12-12 16:00:35 -0800476static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
477{
478 /* Send bonding state only once - based on outgoing/incoming we may receive duplicates */
479 if ( (pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING) )
480 return;
481
Andre Eisenbach89363762015-01-26 13:49:36 -0800482 if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800483 {
484 state = BT_BOND_STATE_NONE;
485 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700486 BTIF_TRACE_DEBUG("%s: state=%d prev_state=%d", __FUNCTION__, state, pairing_cb.state);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800487
488 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
489
490 if (state == BT_BOND_STATE_BONDING)
491 {
492 pairing_cb.state = state;
493 bdcpy(pairing_cb.bd_addr, bd_addr->address);
494 }
495 else
496 {
497 memset(&pairing_cb, 0, sizeof(pairing_cb));
498 }
499
500}
501
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800502/* store remote version in bt config to always have access
503 to it post pairing*/
504static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
505{
506 bt_property_t property;
507 UINT8 lmp_ver = 0;
508 UINT16 lmp_subver = 0;
509 UINT16 mfct_set = 0;
510 tBTM_STATUS btm_status;
511 bt_remote_version_t info;
512 bt_status_t status;
513 bdstr_t bdstr;
514
515 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
516 &mfct_set, &lmp_subver);
517
Sharvil Nanavati44802762014-12-23 23:08:58 -0800518 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 -0800519 lmp_ver, mfct_set, lmp_subver);
520
521 if (btm_status == BTM_SUCCESS)
522 {
523 /* always update cache to ensure we have availability whenever BTM API
524 is not populated */
525 info.manufacturer = mfct_set;
526 info.sub_ver = lmp_subver;
527 info.version = lmp_ver;
528 BTIF_STORAGE_FILL_PROPERTY(&property,
529 BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
530 &info);
531 status = btif_storage_set_remote_device_property(p_bd, &property);
532 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
533 }
534}
535
The Android Open Source Project5738f832012-12-12 16:00:35 -0800536
537static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
538 DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
539{
540 int num_properties = 0;
541 bt_property_t properties[3];
542 bt_bdaddr_t bdaddr;
543 bt_status_t status;
544 UINT32 cod;
545 bt_device_type_t dev_type;
546
547 memset(properties, 0, sizeof(properties));
548 bdcpy(bdaddr.address, bd_addr);
549
550 /* remote name */
551 if (strlen((const char *) bd_name))
552 {
553 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
554 BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
555 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
556 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
557 num_properties++;
558 }
559
560 /* class of device */
561 cod = devclass2uint(dev_class);
Chris Mantonf8027002015-03-12 09:22:48 -0700562 BTIF_TRACE_DEBUG("%s cod is 0x%06x", __func__, cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800563 if ( cod == 0) {
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530564 /* Try to retrieve cod from storage */
Chris Mantonf8027002015-03-12 09:22:48 -0700565 BTIF_TRACE_DEBUG("%s cod is 0, checking cod from storage", __func__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530566 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
567 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
568 status = btif_storage_get_remote_device_property(&bdaddr, &properties[num_properties]);
Chris Mantonf8027002015-03-12 09:22:48 -0700569 BTIF_TRACE_DEBUG("%s cod retrieved from storage is 0x%06x", __func__, cod);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530570 if ( cod == 0) {
Chris Mantonf8027002015-03-12 09:22:48 -0700571 BTIF_TRACE_DEBUG("%s cod is again 0, set as unclassified", __func__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530572 cod = COD_UNCLASSIFIED;
573 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800574 }
575
576 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
577 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
578 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
579 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
580 num_properties++;
581
582 /* device type */
583 dev_type = device_type;
584 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
585 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
586 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
587 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
588 num_properties++;
589
590 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
591 status, &bdaddr, num_properties, properties);
592}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800593
594/*******************************************************************************
595**
596** Function btif_dm_cb_hid_remote_name
597**
598** Description Remote name callback for HID device. Called in btif context
599** Special handling for HID devices
600**
601** Returns void
602**
603*******************************************************************************/
604static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
605{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700606 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 -0800607 if (pairing_cb.state == BT_BOND_STATE_BONDING)
608 {
609 bt_bdaddr_t remote_bd;
610
611 bdcpy(remote_bd.address, pairing_cb.bd_addr);
612
613 if (p_remote_name->status == BTM_SUCCESS)
614 {
615 bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
616 }
617 else
618 bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
619 }
620}
621
The Android Open Source Project5738f832012-12-12 16:00:35 -0800622/*******************************************************************************
623**
624** Function btif_dm_cb_create_bond
625**
626** Description Create bond initiated from the BTIF thread context
627** Special handling for HID devices
628**
629** Returns void
630**
631*******************************************************************************/
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700632static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800633{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800634 BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800635 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800636
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800637#if BLE_INCLUDED == TRUE
638 int device_type;
639 int addr_type;
640 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -0700641 bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr));
Matthew Xie64c54792014-09-16 00:55:03 -0700642 if (transport == BT_TRANSPORT_LE)
643 {
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700644 if (!btif_config_get_int((char const *)&bdstr,"DevType", &device_type))
Matthew Xie64c54792014-09-16 00:55:03 -0700645 {
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700646 btif_config_set_int(bdstr, "DevType", BT_DEVICE_TYPE_BLE);
Matthew Xie64c54792014-09-16 00:55:03 -0700647 }
648 if (btif_storage_get_remote_addr_type(bd_addr, &addr_type) != BT_STATUS_SUCCESS)
649 {
650 btif_storage_set_remote_addr_type(bd_addr, BLE_ADDR_PUBLIC);
651 }
652 }
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700653 if((btif_config_get_int((char const *)&bdstr,"DevType", &device_type) &&
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800654 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
lungtsai_lin3baff792014-08-29 13:47:47 +0800655 (device_type == BT_DEVICE_TYPE_BLE)) || (transport == BT_TRANSPORT_LE))
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800656 {
657 BTA_DmAddBleDevice(bd_addr->address, addr_type, BT_DEVICE_TYPE_BLE);
658 }
659#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800660
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800661#if BLE_INCLUDED == TRUE
662 if(is_hid && device_type != BT_DEVICE_TYPE_BLE)
663#else
664 if(is_hid)
665#endif
666 {
667 int status;
668 status = btif_hh_connect(bd_addr);
669 if(status != BT_STATUS_SUCCESS)
670 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800671 }
672 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800673 {
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700674 BTA_DmBondByTransport((UINT8 *)bd_addr->address, transport);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800675 }
676 /* Track originator of bond creation */
677 pairing_cb.is_local_initiated = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800678
679}
680
681/*******************************************************************************
682**
683** Function btif_dm_cb_remove_bond
684**
685** Description remove bond initiated from the BTIF thread context
686** Special handling for HID devices
687**
688** Returns void
689**
690*******************************************************************************/
691void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
692{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800693 /*special handling for HID devices */
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700694 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there
695 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
696#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
697 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
698#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800699 {
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700700 BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800701 }
702}
703
704/*******************************************************************************
705**
Andre Eisenbach249f6002014-06-18 12:20:37 -0700706** Function btif_dm_get_connection_state
707**
708** Description Returns whether the remote device is currently connected
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800709** and whether encryption is active for the connection
Andre Eisenbach249f6002014-06-18 12:20:37 -0700710**
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800711** Returns 0 if not connected; 1 if connected and > 1 if connection is
712** encrypted
Andre Eisenbach249f6002014-06-18 12:20:37 -0700713**
714*******************************************************************************/
715uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr)
716{
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800717 uint8_t *bda = (uint8_t*)bd_addr->address;
718 uint16_t rc = BTA_DmGetConnectionState(bda);
719
720 if (rc != 0)
721 {
722 uint8_t flags = 0;
723
724 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_BR_EDR);
725 BTIF_TRACE_DEBUG("%s: security flags (BR/EDR)=0x%02x", __FUNCTION__, flags);
726 if (flags & BTM_SEC_FLAG_ENCRYPTED)
727 rc |= ENCRYPTED_BREDR;
728
729 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_LE);
730 BTIF_TRACE_DEBUG("%s: security flags (LE)=0x%02x", __FUNCTION__, flags);
731 if (flags & BTM_SEC_FLAG_ENCRYPTED)
732 rc |= ENCRYPTED_LE;
733 }
734
735 return rc;
Andre Eisenbach249f6002014-06-18 12:20:37 -0700736}
737
738/*******************************************************************************
739**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800740** Function search_devices_copy_cb
741**
742** Description Deep copy callback for search devices event
743**
744** Returns void
745**
746*******************************************************************************/
747static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
748{
749 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
750 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
751
752 if (!p_src)
753 return;
754
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700755 BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800756 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
757 switch (event)
758 {
759 case BTA_DM_INQ_RES_EVT:
760 {
761 if (p_src_data->inq_res.p_eir)
762 {
763 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
764 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
765 }
766 }
767 break;
768
769 case BTA_DM_DISC_RES_EVT:
770 {
771 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
772 {
773 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
774 memcpy(p_dest_data->disc_res.p_raw_data,
775 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
776 }
777 }
778 break;
779 }
780}
781
782static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
783{
784 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
785 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
786
787 if (!p_src)
788 return;
789 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
790 switch (event)
791 {
792 case BTA_DM_DISC_RES_EVT:
793 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530794 if (p_src_data->disc_res.result == BTA_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800795 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530796 if (p_src_data->disc_res.num_uuids > 0)
797 {
798 p_dest_data->disc_res.p_uuid_list =
799 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
800 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
801 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
802 GKI_freebuf(p_src_data->disc_res.p_uuid_list);
803 }
804 if (p_src_data->disc_res.p_raw_data != NULL)
805 {
806 GKI_freebuf(p_src_data->disc_res.p_raw_data);
807 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800808 }
809 } break;
810 }
811}
812/******************************************************************************
813**
814** BTIF DM callback events
815**
816*****************************************************************************/
817
818/*******************************************************************************
819**
820** Function btif_dm_pin_req_evt
821**
822** Description Executes pin request event in btif context
823**
824** Returns void
825**
826*******************************************************************************/
827static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
828{
829 bt_bdaddr_t bd_addr;
830 bt_bdname_t bd_name;
831 UINT32 cod;
832 bt_pin_code_t pin_code;
Matthew Xie86f97ed2014-11-10 10:24:46 -0800833 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800834
835 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -0800836 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
837 {
838 dev_type = BT_DEVICE_TYPE_BREDR;
839 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800840 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -0800841 p_pin_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800842
843 bdcpy(bd_addr.address, p_pin_req->bd_addr);
844 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
845
846 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
847
848 cod = devclass2uint(p_pin_req->dev_class);
849
Chris Mantonf8027002015-03-12 09:22:48 -0700850 if (cod == 0) {
851 BTIF_TRACE_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800852 cod = COD_UNCLASSIFIED;
853 }
854
855 /* check for auto pair possiblity only if bond was initiated by local device */
856 if (pairing_cb.is_local_initiated)
857 {
858 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
859 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
860 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
861 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
862 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
863 check_cod(&bd_addr, COD_HID_POINTING))
864 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700865 BTIF_TRACE_DEBUG("%s()cod matches for auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800866 /* Check if this device can be auto paired */
867 if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) &&
868 (pairing_cb.autopair_attempts == 0))
869 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700870 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800871 pin_code.pin[0] = 0x30;
872 pin_code.pin[1] = 0x30;
873 pin_code.pin[2] = 0x30;
874 pin_code.pin[3] = 0x30;
875
876 pairing_cb.autopair_attempts++;
877 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
878 return;
879 }
880 }
881 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
882 check_cod(&bd_addr, COD_HID_COMBO))
883 {
884 if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) &&
885 (pairing_cb.autopair_attempts == 0))
886 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700887 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800888 pin_code.pin[0] = 0x30;
889 pin_code.pin[1] = 0x30;
890 pin_code.pin[2] = 0x30;
891 pin_code.pin[3] = 0x30;
892
893 pairing_cb.autopair_attempts++;
894 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
895 return;
896 }
897 }
898 }
899 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
900 &bd_addr, &bd_name, cod);
901}
902
903/*******************************************************************************
904**
905** Function btif_dm_ssp_cfm_req_evt
906**
907** Description Executes SSP confirm request event in btif context
908**
909** Returns void
910**
911*******************************************************************************/
912static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
913{
914 bt_bdaddr_t bd_addr;
915 bt_bdname_t bd_name;
916 UINT32 cod;
917 BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
Matthew Xie86f97ed2014-11-10 10:24:46 -0800918 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800919
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700920 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800921
922 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -0800923 if (!btif_get_device_type(p_ssp_cfm_req->bd_addr, &dev_type))
924 {
925 dev_type = BT_DEVICE_TYPE_BREDR;
926 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800927 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -0800928 p_ssp_cfm_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800929
930 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
931 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
932
933 /* Set the pairing_cb based on the local & remote authentication requirements */
934 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
935
936 /* if just_works and bonding bit is not set treat this as temporary */
937 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 -0800938 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
939 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
Andre Eisenbach89363762015-01-26 13:49:36 -0800940 pairing_cb.bond_type = BOND_TYPE_TEMPORARY;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800941 else
Andre Eisenbach89363762015-01-26 13:49:36 -0800942 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800943
944 pairing_cb.is_ssp = TRUE;
945
946 /* If JustWorks auto-accept */
947 if (p_ssp_cfm_req->just_works)
948 {
949 /* Pairing consent for JustWorks needed if:
950 * 1. Incoming pairing is detected AND
951 * 2. local IO capabilities are DisplayYesNo AND
952 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
953 */
954 if ((is_incoming) && ((p_ssp_cfm_req->loc_io_caps == 0x01) &&
955 (p_ssp_cfm_req->rmt_io_caps == 0x00 || p_ssp_cfm_req->rmt_io_caps == 0x03)))
956 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700957 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 -0800958 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
959 }
960 else
961 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700962 BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800963 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
964 return;
965 }
966 }
967
968 cod = devclass2uint(p_ssp_cfm_req->dev_class);
969
Chris Mantonf8027002015-03-12 09:22:48 -0700970 if (cod == 0) {
971 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800972 cod = COD_UNCLASSIFIED;
973 }
974
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700975 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800976 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
977 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
978 p_ssp_cfm_req->num_val);
979}
980
981static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
982{
983 bt_bdaddr_t bd_addr;
984 bt_bdname_t bd_name;
985 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -0800986 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800987
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700988 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800989
990 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -0800991 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
992 {
993 dev_type = BT_DEVICE_TYPE_BREDR;
994 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800995 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -0800996 p_ssp_key_notif->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800997
998 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
999 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
1000
1001 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1002 pairing_cb.is_ssp = TRUE;
1003 cod = devclass2uint(p_ssp_key_notif->dev_class);
1004
Chris Mantonf8027002015-03-12 09:22:48 -07001005 if (cod == 0) {
1006 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001007 cod = COD_UNCLASSIFIED;
1008 }
1009
1010 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
1011 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
1012 p_ssp_key_notif->passkey);
1013}
1014/*******************************************************************************
1015**
1016** Function btif_dm_auth_cmpl_evt
1017**
1018** Description Executes authentication complete event in btif context
1019**
1020** Returns void
1021**
1022*******************************************************************************/
1023static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
1024{
1025 /* Save link key, if not temporary */
1026 bt_bdaddr_t bd_addr;
1027 bt_status_t status = BT_STATUS_FAIL;
1028 bt_bond_state_t state = BT_BOND_STATE_NONE;
Priti Agherac0edf9f2014-06-26 11:23:51 -07001029 BOOLEAN skip_sdp = FALSE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001030
1031 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
1032 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
1033 {
1034 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 -08001035 (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 -08001036 {
1037 bt_status_t ret;
Andre Eisenbach89363762015-01-26 13:49:36 -08001038 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, bond_type=%d",
1039 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001040 ret = btif_storage_add_bonded_device(&bd_addr,
1041 p_auth_cmpl->key, p_auth_cmpl->key_type,
1042 pairing_cb.pin_code_len);
1043 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
1044 }
1045 else
1046 {
Andre Eisenbach89363762015-01-26 13:49:36 -08001047 BTIF_TRACE_DEBUG("%s: Temporary key. Not storing. key_type=0x%x, bond_type=%d",
1048 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1049 if(pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
Hemant Guptab820aec2013-12-24 19:59:57 +05301050 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001051 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
Hemant Guptab820aec2013-12-24 19:59:57 +05301052 __FUNCTION__);
1053 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1054 return;
1055 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001056 }
1057 }
Priti Agherac0edf9f2014-06-26 11:23:51 -07001058
1059 // Skip SDP for certain HID Devices
The Android Open Source Project5738f832012-12-12 16:00:35 -08001060 if (p_auth_cmpl->success)
1061 {
Andre Eisenbach31a64002014-10-14 14:29:19 -07001062 pairing_cb.timeout_retries = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001063 status = BT_STATUS_SUCCESS;
1064 state = BT_BOND_STATE_BONDED;
Priti Agherac0edf9f2014-06-26 11:23:51 -07001065 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001066
Priti Agherac0edf9f2014-06-26 11:23:51 -07001067 if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr, COD_HID_MAJOR))
1068 {
Sharvil Nanavati44802762014-12-23 23:08:58 -08001069 LOG_WARN("%s:skip SDP", __FUNCTION__);
Priti Agherac0edf9f2014-06-26 11:23:51 -07001070 skip_sdp = TRUE;
1071 }
1072 if(!pairing_cb.is_local_initiated && skip_sdp)
1073 {
1074 bond_state_changed(status, &bd_addr, state);
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -08001075
Sharvil Nanavati44802762014-12-23 23:08:58 -08001076 LOG_WARN("%s: Incoming HID Connection",__FUNCTION__);
Priti Agherac0edf9f2014-06-26 11:23:51 -07001077 bt_property_t prop;
1078 bt_bdaddr_t bd_addr;
1079 bt_uuid_t uuid;
1080 char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE;
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -08001081
Priti Agherac0edf9f2014-06-26 11:23:51 -07001082 string_to_uuid(uuid_str, &uuid);
1083
1084 prop.type = BT_PROPERTY_UUIDS;
1085 prop.val = uuid.uu;
1086 prop.len = MAX_UUID_SIZE;
1087
1088 /* Send the event to the BTIF */
1089 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1090 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1091 }
1092 else
1093 {
1094 /* Trigger SDP on the device */
1095 pairing_cb.sdp_attempts = 1;;
1096
1097 if(btif_dm_inquiry_in_progress)
1098 btif_dm_cancel_discovery();
1099
1100 btif_dm_get_remote_services(&bd_addr);
1101 }
1102 /* 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 -08001103 }
1104 else
1105 {
1106 /*Map the HCI fail reason to bt status */
1107 switch(p_auth_cmpl->fail_reason)
1108 {
1109 case HCI_ERR_PAGE_TIMEOUT:
Andre Eisenbach31a64002014-10-14 14:29:19 -07001110 if (blacklistPairingRetries(bd_addr.address) && pairing_cb.timeout_retries)
1111 {
1112 BTIF_TRACE_WARNING("%s() - Pairing timeout; retrying (%d) ...", __FUNCTION__, pairing_cb.timeout_retries);
1113 --pairing_cb.timeout_retries;
1114 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
1115 return;
1116 }
1117 /* Fall-through */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001118 case HCI_ERR_CONNECTION_TOUT:
1119 status = BT_STATUS_RMT_DEV_DOWN;
1120 break;
1121
Hemant Guptaaef7a672013-07-31 19:00:12 +05301122 case HCI_ERR_PAIRING_NOT_ALLOWED:
1123 status = BT_STATUS_AUTH_REJECTED;
1124 break;
1125
Hemant Guptab4801442014-01-07 18:11:15 +05301126 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
1127 status = BT_STATUS_AUTH_FAILURE;
1128 break;
1129
The Android Open Source Project5738f832012-12-12 16:00:35 -08001130 /* map the auth failure codes, so we can retry pairing if necessary */
1131 case HCI_ERR_AUTH_FAILURE:
Hemant Gupta59a88ec2014-03-19 19:01:35 +05301132 case HCI_ERR_KEY_MISSING:
Zhihai Xua7ea8092013-11-27 14:10:53 +05301133 btif_storage_remove_bonded_device(&bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001134 case HCI_ERR_HOST_REJECT_SECURITY:
1135 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
1136 case HCI_ERR_UNIT_KEY_USED:
1137 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
1138 case HCI_ERR_INSUFFCIENT_SECURITY:
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301139 case HCI_ERR_PEER_USER:
1140 case HCI_ERR_UNSPECIFIED:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001141 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d",
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301142 __FUNCTION__, p_auth_cmpl->fail_reason);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001143 if (pairing_cb.autopair_attempts == 1)
1144 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001145 BTIF_TRACE_DEBUG("%s(): Adding device to blacklist ", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001146
1147 /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class */
1148 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
1149 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
1150 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
1151 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
1152 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
1153 check_cod(&bd_addr, COD_HID_POINTING))
1154 {
1155 btif_storage_add_device_to_autopair_blacklist (&bd_addr);
1156 }
1157 pairing_cb.autopair_attempts++;
1158
1159 /* Create the Bond once again */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001160 BTIF_TRACE_DEBUG("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07001161 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001162 return;
1163 }
1164 else
1165 {
1166 /* if autopair attempts are more than 1, or not attempted */
1167 status = BT_STATUS_AUTH_FAILURE;
1168 }
1169 break;
1170
1171 default:
1172 status = BT_STATUS_FAIL;
1173 }
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301174 /* Special Handling for HID Devices */
1175 if (check_cod(&bd_addr, COD_HID_POINTING)) {
1176 /* Remove Device as bonded in nvram as authentication failed */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001177 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __FUNCTION__);
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301178 btif_storage_remove_bonded_device(&bd_addr);
1179 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001180 bond_state_changed(status, &bd_addr, state);
1181 }
1182}
1183
1184/******************************************************************************
1185**
1186** Function btif_dm_search_devices_evt
1187**
1188** Description Executes search devices callback events in btif context
1189**
1190** Returns void
1191**
1192******************************************************************************/
1193static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
1194{
1195 tBTA_DM_SEARCH *p_search_data;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001196 BTIF_TRACE_EVENT("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001197
1198 switch (event)
1199 {
1200 case BTA_DM_DISC_RES_EVT:
1201 {
1202 p_search_data = (tBTA_DM_SEARCH *)p_param;
1203 /* Remote name update */
1204 if (strlen((const char *) p_search_data->disc_res.bd_name))
1205 {
1206 bt_property_t properties[1];
1207 bt_bdaddr_t bdaddr;
1208 bt_status_t status;
1209
1210 properties[0].type = BT_PROPERTY_BDNAME;
1211 properties[0].val = p_search_data->disc_res.bd_name;
1212 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
1213 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
1214
1215 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
1216 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
1217 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1218 status, &bdaddr, 1, properties);
1219 }
1220 /* TODO: Services? */
1221 }
1222 break;
1223
1224 case BTA_DM_INQ_RES_EVT:
1225 {
1226 /* inquiry result */
1227 UINT32 cod;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001228 bt_bdname_t bdname;
1229 bt_bdaddr_t bdaddr;
1230 UINT8 remote_name_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001231 tBTA_SERVICE_MASK services = 0;
1232 bdstr_t bdstr;
1233
1234 p_search_data = (tBTA_DM_SEARCH *)p_param;
1235 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
1236
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07001237 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 -08001238#if (BLE_INCLUDED == TRUE)
1239 p_search_data->inq_res.device_type);
1240#else
1241 BT_DEVICE_TYPE_BREDR);
1242#endif
1243 bdname.name[0] = 0;
1244
1245 cod = devclass2uint (p_search_data->inq_res.dev_class);
1246
Chris Mantonf8027002015-03-12 09:22:48 -07001247 if (cod == 0) {
1248 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001249 cod = COD_UNCLASSIFIED;
1250 }
1251
1252 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1253 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1254
1255 /* Check EIR for remote name and services */
1256 if (p_search_data->inq_res.p_eir)
1257 {
1258 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001259 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001260 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */
1261 }
1262
1263
1264 {
1265 bt_property_t properties[5];
1266 bt_device_type_t dev_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001267 UINT8 addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001268 uint32_t num_properties = 0;
1269 bt_status_t status;
1270
1271 memset(properties, 0, sizeof(properties));
1272 /* BD_ADDR */
1273 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1274 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1275 num_properties++;
1276 /* BD_NAME */
1277 /* Don't send BDNAME if it is empty */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001278 if (bdname.name[0])
1279 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001280 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1281 BT_PROPERTY_BDNAME,
1282 strlen((char *)bdname.name), &bdname);
1283 num_properties++;
1284 }
1285
1286 /* DEV_CLASS */
1287 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1288 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1289 num_properties++;
1290 /* DEV_TYPE */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001291#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001292 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1293 dev_type = p_search_data->inq_res.device_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001294 addr_type = p_search_data->inq_res.ble_addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001295#else
1296 dev_type = BT_DEVICE_TYPE_BREDR;
1297#endif
1298 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1299 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1300 num_properties++;
1301 /* RSSI */
1302 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1303 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1304 &(p_search_data->inq_res.rssi));
1305 num_properties++;
1306
1307 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1308 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001309#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1310 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001311 if (( dev_type == BT_DEVICE_TYPE_DUMO)&&
1312 (p_search_data->inq_res.flag & BTA_BLE_DMT_CONTROLLER_SPT) &&
1313 (p_search_data->inq_res.flag & BTA_BLE_DMT_HOST_SPT))
1314 {
1315 btif_storage_set_dmt_support_type (&bdaddr, TRUE);
1316 }
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001317 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1318#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001319 /* Callback to notify upper layer of device */
1320 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1321 num_properties, properties);
1322 }
1323 }
1324 break;
1325
1326 case BTA_DM_INQ_CMPL_EVT:
1327 {
Satya Calloji6e2d9db2014-07-08 16:18:58 -07001328#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1329 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1330 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1331 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1332 bte_scan_filt_param_cfg_evt, 0);
1333#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001334 }
1335 break;
1336 case BTA_DM_DISC_CMPL_EVT:
1337 {
1338 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1339 }
1340 break;
1341 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1342 {
1343 /* if inquiry is not in progress and we get a cancel event, then
1344 * it means we are done with inquiry, but remote_name fetches are in
1345 * progress
1346 *
1347 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt
1348 * but instead wait for the cancel_cmpl_evt via the Busy Level
1349 *
1350 */
1351 if (btif_dm_inquiry_in_progress == FALSE)
1352 {
Nitin Arora7b85efa2014-09-26 14:05:24 -07001353#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1354 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1355 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1356 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1357 bte_scan_filt_param_cfg_evt, 0);
1358#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001359 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1360 }
1361 }
1362 break;
1363 }
1364}
1365
1366/*******************************************************************************
1367**
1368** Function btif_dm_search_services_evt
1369**
1370** Description Executes search services event in btif context
1371**
1372** Returns void
1373**
1374*******************************************************************************/
1375static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1376{
1377 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1378
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001379 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001380 switch (event)
1381 {
1382 case BTA_DM_DISC_RES_EVT:
1383 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001384 bt_property_t prop;
Bernhard Rosenkränzer104e3f22014-11-12 21:53:08 +01001385 uint32_t i = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001386 bt_bdaddr_t bd_addr;
1387 bt_status_t ret;
1388
1389 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1390
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001391 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001392 p_data->disc_res.result, p_data->disc_res.services);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001393 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1394 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1395 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1396 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001397 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001398 pairing_cb.sdp_attempts++;
1399 btif_dm_get_remote_services(&bd_addr);
1400 return;
1401 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001402 prop.type = BT_PROPERTY_UUIDS;
1403 prop.len = 0;
1404 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1405 {
1406 prop.val = p_data->disc_res.p_uuid_list;
1407 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1408 for (i=0; i < p_data->disc_res.num_uuids; i++)
1409 {
1410 char temp[256];
Chris Manton8ff3fea2015-01-07 13:59:14 -08001411 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 -07001412 LOG_INFO("%s index:%d uuid:%s", __func__, i, temp);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001413 }
1414 }
1415
1416 /* onUuidChanged requires getBondedDevices to be populated.
1417 ** bond_state_changed needs to be sent prior to remote_device_property
1418 */
1419 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1420 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0)&&
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001421 pairing_cb.sdp_attempts > 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001422 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001423 BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001424 __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001425 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001426 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1427 }
1428
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001429 if(p_data->disc_res.num_uuids != 0)
1430 {
1431 /* Also write this to the NVRAM */
1432 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1433 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1434 /* Send the event to the BTIF */
1435 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1436 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1437 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001438 }
1439 break;
1440
1441 case BTA_DM_DISC_CMPL_EVT:
1442 /* fixme */
1443 break;
1444
Matthew Xie607e3b72013-08-15 19:30:48 -07001445#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001446 case BTA_DM_DISC_BLE_RES_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001447 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__,
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001448 p_data->disc_ble_res.service.uu.uuid16);
1449 bt_uuid_t uuid;
1450 int i = 0;
1451 int j = 15;
1452 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1453 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001454 BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001455 bt_property_t prop;
1456 bt_bdaddr_t bd_addr;
1457 char temp[256];
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001458 bt_status_t ret;
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001459
1460 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1461
1462 while(i < j )
1463 {
1464 unsigned char c = uuid.uu[j];
1465 uuid.uu[j] = uuid.uu[i];
1466 uuid.uu[i] = c;
1467 i++;
1468 j--;
1469 }
1470
Chris Manton8ff3fea2015-01-07 13:59:14 -08001471 uuid_to_string_legacy(&uuid, temp);
Chris Mantonf8027002015-03-12 09:22:48 -07001472 LOG_INFO("%s uuid:%s", __func__, temp);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001473
1474 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1475 prop.type = BT_PROPERTY_UUIDS;
1476 prop.val = uuid.uu;
1477 prop.len = MAX_UUID_SIZE;
1478
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001479 /* Also write this to the NVRAM */
1480 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1481 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1482
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001483 /* Send the event to the BTIF */
1484 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1485 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1486
1487 }
1488 break;
Matthew Xie607e3b72013-08-15 19:30:48 -07001489#endif /* BLE_INCLUDED */
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001490
The Android Open Source Project5738f832012-12-12 16:00:35 -08001491 default:
1492 {
1493 ASSERTC(0, "unhandled search services event", event);
1494 }
1495 break;
1496 }
1497}
1498
1499/*******************************************************************************
1500**
1501** Function btif_dm_remote_service_record_evt
1502**
1503** Description Executes search service record event in btif context
1504**
1505** Returns void
1506**
1507*******************************************************************************/
1508static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1509{
1510 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1511
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001512 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001513 switch (event)
1514 {
1515 case BTA_DM_DISC_RES_EVT:
1516 {
1517 bt_service_record_t rec;
1518 bt_property_t prop;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001519 bt_bdaddr_t bd_addr;
1520
1521 memset(&rec, 0, sizeof(bt_service_record_t));
1522 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1523
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001524 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001525 p_data->disc_res.result, p_data->disc_res.services);
1526 prop.type = BT_PROPERTY_SERVICE_RECORD;
1527 prop.val = (void*)&rec;
1528 prop.len = sizeof(rec);
1529
1530 /* disc_res.result is overloaded with SCN. Cannot check result */
1531 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1532 /* TODO: Get the UUID as well */
1533 rec.channel = p_data->disc_res.result - 3;
1534 /* TODO: Need to get the service name using p_raw_data */
1535 rec.name[0] = 0;
1536
1537 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1538 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1539 }
1540 break;
1541
1542 default:
1543 {
1544 ASSERTC(0, "unhandled remote service record event", event);
1545 }
1546 break;
1547 }
1548}
1549
1550/*******************************************************************************
1551**
1552** Function btif_dm_upstreams_cback
1553**
1554** Description Executes UPSTREAMS events in btif context
1555**
1556** Returns void
1557**
1558*******************************************************************************/
1559static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1560{
The Android Open Source Project5738f832012-12-12 16:00:35 -08001561 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1562 tBTA_SERVICE_MASK service_mask;
1563 uint32_t i;
1564 bt_bdaddr_t bd_addr;
1565
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001566 BTIF_TRACE_EVENT("btif_dm_upstreams_cback ev: %s", dump_dm_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001567
1568 switch (event)
1569 {
1570 case BTA_DM_ENABLE_EVT:
1571 {
1572 BD_NAME bdname;
1573 bt_status_t status;
1574 bt_property_t prop;
1575 prop.type = BT_PROPERTY_BDNAME;
1576 prop.len = BD_NAME_LEN;
1577 prop.val = (void*)bdname;
1578
1579 status = btif_storage_get_adapter_property(&prop);
Matthew Xie1e5109b2012-11-09 18:26:26 -08001580 if (status == BT_STATUS_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001581 {
1582 /* A name exists in the storage. Make this the device name */
1583 BTA_DmSetDeviceName((char*)prop.val);
1584 }
Matthew Xie1e5109b2012-11-09 18:26:26 -08001585 else
1586 {
1587 /* Storage does not have a name yet.
1588 * Use the default name and write it to the chip
1589 */
1590 BTA_DmSetDeviceName(btif_get_default_local_name());
1591 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001592
Andre Eisenbacha015a832014-09-11 14:09:40 -07001593#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1594 /* Enable local privacy */
Andre Eisenbach3e0dc732014-10-24 09:55:34 -07001595 BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED);
Andre Eisenbacha015a832014-09-11 14:09:40 -07001596#endif
1597
The Android Open Source Project5738f832012-12-12 16:00:35 -08001598 /* for each of the enabled services in the mask, trigger the profile
1599 * enable */
1600 service_mask = btif_get_enabled_services_mask();
1601 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1602 {
1603 if (service_mask &
1604 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1605 {
1606 btif_in_execute_service_request(i, TRUE);
1607 }
1608 }
1609 /* clear control blocks */
1610 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1611
1612 /* This function will also trigger the adapter_properties_cb
1613 ** and bonded_devices_info_cb
1614 */
1615 btif_storage_load_bonded_devices();
1616
1617 btif_storage_load_autopair_device_list();
1618
Zach Johnson39110ec2014-10-06 13:15:00 -07001619 btif_enable_bluetooth_evt(p_data->enable.status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001620 }
1621 break;
1622
1623 case BTA_DM_DISABLE_EVT:
1624 /* for each of the enabled services in the mask, trigger the profile
1625 * disable */
1626 service_mask = btif_get_enabled_services_mask();
1627 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1628 {
1629 if (service_mask &
1630 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1631 {
1632 btif_in_execute_service_request(i, FALSE);
1633 }
1634 }
1635 btif_disable_bluetooth_evt();
1636 break;
1637
1638 case BTA_DM_PIN_REQ_EVT:
1639 btif_dm_pin_req_evt(&p_data->pin_req);
1640 break;
1641
1642 case BTA_DM_AUTH_CMPL_EVT:
1643 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1644 break;
1645
1646 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1647 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1648 {
1649 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1650 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1651 }
1652 break;
1653
1654 case BTA_DM_SP_CFM_REQ_EVT:
1655 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1656 break;
1657 case BTA_DM_SP_KEY_NOTIF_EVT:
1658 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1659 break;
1660
1661 case BTA_DM_DEV_UNPAIRED_EVT:
1662 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1663
1664 /*special handling for HID devices */
1665 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -07001666 btif_hh_remove_device(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001667 #endif
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001668 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1669 btif_storage_remove_ble_bonding_keys(&bd_addr);
1670 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001671 btif_storage_remove_bonded_device(&bd_addr);
1672 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1673 break;
1674
1675 case BTA_DM_BUSY_LEVEL_EVT:
1676 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001677
1678 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001679 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001680 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001681 {
1682 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1683 BT_DISCOVERY_STARTED);
1684 btif_dm_inquiry_in_progress = TRUE;
1685 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001686 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001687 {
1688 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1689 BT_DISCOVERY_STOPPED);
1690 btif_dm_inquiry_in_progress = FALSE;
1691 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001692 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001693 {
1694 btif_dm_inquiry_in_progress = FALSE;
1695 }
1696 }
1697 }break;
1698
1699 case BTA_DM_LINK_UP_EVT:
1700 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001701 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001702
1703 btif_update_remote_version_property(&bd_addr);
1704
The Android Open Source Project5738f832012-12-12 16:00:35 -08001705 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1706 &bd_addr, BT_ACL_STATE_CONNECTED);
1707 break;
1708
1709 case BTA_DM_LINK_DOWN_EVT:
1710 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001711 BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001712 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1713 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1714 break;
1715
1716 case BTA_DM_HW_ERROR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001717 BTIF_TRACE_ERROR("Received H/W Error. ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001718 /* Flush storage data */
1719 btif_config_flush();
1720 usleep(100000); /* 100milliseconds */
1721 /* Killing the process to force a restart as part of fault tolerance */
1722 kill(getpid(), SIGKILL);
1723 break;
1724
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001725#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1726 case BTA_DM_BLE_KEY_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001727 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 -08001728
1729 /* If this pairing is by-product of local initiated GATT client Read or Write,
1730 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1731 have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1732 if (pairing_cb.state != BT_BOND_STATE_BONDING)
1733 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001734 BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001735 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1736 BT_BOND_STATE_BONDING);
1737 }
1738 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1739 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001740 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001741 break;
1742 }
1743
1744 switch (p_data->ble_key.key_type)
1745 {
1746 case BTA_LE_KEY_PENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001747 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001748 pairing_cb.ble.is_penc_key_rcvd = TRUE;
1749 memcpy(pairing_cb.ble.penc_key.ltk,p_data->ble_key.key_value.penc_key.ltk, 16);
1750 memcpy(pairing_cb.ble.penc_key.rand, p_data->ble_key.key_value.penc_key.rand,8);
1751 pairing_cb.ble.penc_key.ediv = p_data->ble_key.key_value.penc_key.ediv;
1752 pairing_cb.ble.penc_key.sec_level = p_data->ble_key.key_value.penc_key.sec_level;
1753
1754 for (i=0; i<16; i++)
1755 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001756 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 -08001757 }
1758 for (i=0; i<8; i++)
1759 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001760 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 -08001761 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001762 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.ediv=0x%04x",pairing_cb.ble.penc_key.ediv);
1763 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.sec_level=0x%02x",pairing_cb.ble.penc_key.sec_level);
1764 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 -08001765 break;
1766
1767 case BTA_LE_KEY_PID:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001768 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001769 pairing_cb.ble.is_pid_key_rcvd = TRUE;
Andre Eisenbach5e808462014-10-21 12:37:53 -07001770 pairing_cb.ble.pid_key.addr_type = p_data->ble_key.key_value.pid_key.addr_type;
1771 memcpy(pairing_cb.ble.pid_key.irk, p_data->ble_key.key_value.pid_key.irk, 16);
1772 memcpy(pairing_cb.ble.pid_key.static_addr,
1773 p_data->ble_key.key_value.pid_key.static_addr,BD_ADDR_LEN);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001774 for (i=0; i<16; i++)
1775 {
Andre Eisenbach5e808462014-10-21 12:37:53 -07001776 BTIF_TRACE_DEBUG("pairing_cb.ble.pid_key.irk[%d]=0x%02x"
1777 ,i,pairing_cb.ble.pid_key.irk[i]);
1778 }
1779 for (i=0; i<BD_ADDR_LEN; i++)
1780 {
1781 BTIF_TRACE_DEBUG("piaring_cb.ble.pid_address[%d] = %x"
1782 ,i, pairing_cb.ble.pid_key.static_addr[i]);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001783 }
1784 break;
1785
1786 case BTA_LE_KEY_PCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001787 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001788 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1789 pairing_cb.ble.pcsrk_key.counter = p_data->ble_key.key_value.pcsrk_key.counter;
1790 pairing_cb.ble.pcsrk_key.sec_level = p_data->ble_key.key_value.pcsrk_key.sec_level;
1791 memcpy(pairing_cb.ble.pcsrk_key.csrk,p_data->ble_key.key_value.pcsrk_key.csrk,16);
1792
1793 for (i=0; i<16; i++)
1794 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001795 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 -08001796 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001797 BTIF_TRACE_DEBUG("pairing_cb.ble.pcsrk_key.counter=0x%08x",pairing_cb.ble.pcsrk_key.counter);
1798 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 -08001799 break;
1800
1801 case BTA_LE_KEY_LENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001802 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001803 pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1804 pairing_cb.ble.lenc_key.div = p_data->ble_key.key_value.lenc_key.div;
1805 pairing_cb.ble.lenc_key.key_size = p_data->ble_key.key_value.lenc_key.key_size;
1806 pairing_cb.ble.lenc_key.sec_level = p_data->ble_key.key_value.lenc_key.sec_level;
1807
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001808 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.div=0x%04x",pairing_cb.ble.lenc_key.div);
1809 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.key_size=0x%02x",pairing_cb.ble.lenc_key.key_size);
1810 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 -08001811 break;
1812
1813
1814
1815 case BTA_LE_KEY_LCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001816 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001817 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1818 pairing_cb.ble.lcsrk_key.counter = p_data->ble_key.key_value.lcsrk_key.counter;
1819 pairing_cb.ble.lcsrk_key.div = p_data->ble_key.key_value.lcsrk_key.div;
1820 pairing_cb.ble.lcsrk_key.sec_level = p_data->ble_key.key_value.lcsrk_key.sec_level;
1821
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001822 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.div=0x%04x",pairing_cb.ble.lcsrk_key.div);
1823 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.counter=0x%08x",pairing_cb.ble.lcsrk_key.counter);
1824 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 -08001825
1826 break;
1827
1828 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001829 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001830 break;
1831 }
1832
1833 break;
1834 case BTA_DM_BLE_SEC_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001835 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001836 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1837 break;
1838 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001839 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001840 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1841 break;
1842 case BTA_DM_BLE_PASSKEY_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001843 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001844 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1845 break;
1846 case BTA_DM_BLE_OOB_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001847 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001848 break;
1849 case BTA_DM_BLE_LOCAL_IR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001850 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001851 ble_local_key_cb.is_id_keys_rcvd = TRUE;
1852 memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1853 memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1854 memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1855 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1856 BTIF_DM_LE_LOCAL_KEY_IR,
1857 BT_OCTET16_LEN);
1858 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1859 BTIF_DM_LE_LOCAL_KEY_IRK,
1860 BT_OCTET16_LEN);
1861 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1862 BTIF_DM_LE_LOCAL_KEY_DHK,
1863 BT_OCTET16_LEN);
1864 break;
1865 case BTA_DM_BLE_LOCAL_ER_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001866 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001867 ble_local_key_cb.is_er_rcvd = TRUE;
1868 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1869 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1870 BTIF_DM_LE_LOCAL_KEY_ER,
1871 BT_OCTET16_LEN);
1872 break;
1873
1874 case BTA_DM_BLE_AUTH_CMPL_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001875 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001876 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1877 break;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001878
1879 case BTA_DM_LE_FEATURES_READ:
1880 {
1881 tBTM_BLE_VSC_CB cmn_vsc_cb;
1882 bt_local_le_features_t local_le_features;
1883 char buf[512];
1884 bt_property_t prop;
1885 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1886 prop.val = (void*)buf;
1887 prop.len = sizeof(buf);
1888
1889 /* LE features are not stored in storage. Should be retrived from stack */
1890 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1891 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1892
1893 prop.len = sizeof (bt_local_le_features_t);
1894 if (cmn_vsc_cb.filter_support == 1)
1895 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1896 else
1897 local_le_features.max_adv_filter_supported = 0;
1898 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1899 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1900 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
Satya Callojid773c2c2014-07-29 22:08:55 -07001901 local_le_features.scan_result_storage_size_hibyte =
1902 (cmn_vsc_cb.tot_scan_results_strg >> 8) & (0xFF);
1903 local_le_features.scan_result_storage_size_lobyte =
1904 (cmn_vsc_cb.tot_scan_results_strg) & (0xFF);
Satya Callojiefaddcb2014-07-28 23:22:05 -07001905 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001906 memcpy(prop.val, &local_le_features, prop.len);
1907 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
1908 break;
1909 }
Satya Callojie5ba8842014-07-03 17:18:02 -07001910
1911 case BTA_DM_ENER_INFO_READ:
1912 {
1913 btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param;
1914 bt_activity_energy_info energy_info;
1915 energy_info.status = p_ener_data->status;
1916 energy_info.ctrl_state = p_ener_data->ctrl_state;
1917 energy_info.rx_time = p_ener_data->rx_time;
1918 energy_info.tx_time = p_ener_data->tx_time;
1919 energy_info.idle_time = p_ener_data->idle_time;
1920 energy_info.energy_used = p_ener_data->energy_used;
1921 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info);
1922 break;
1923 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001924#endif
1925
The Android Open Source Project5738f832012-12-12 16:00:35 -08001926 case BTA_DM_AUTHORIZE_EVT:
1927 case BTA_DM_SIG_STRENGTH_EVT:
1928 case BTA_DM_SP_RMT_OOB_EVT:
1929 case BTA_DM_SP_KEYPRESS_EVT:
1930 case BTA_DM_ROLE_CHG_EVT:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001931
The Android Open Source Project5738f832012-12-12 16:00:35 -08001932 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001933 BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001934 break;
1935 }
1936} /* btui_security_cback() */
1937
1938
1939/*******************************************************************************
1940**
1941** Function btif_dm_generic_evt
1942**
1943** Description Executes non-BTA upstream events in BTIF context
1944**
1945** Returns void
1946**
1947*******************************************************************************/
1948static void btif_dm_generic_evt(UINT16 event, char* p_param)
1949{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001950 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001951 switch(event)
1952 {
1953 case BTIF_DM_CB_DISCOVERY_STARTED:
1954 {
1955 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
1956 }
1957 break;
1958
1959 case BTIF_DM_CB_CREATE_BOND:
1960 {
Andre Eisenbach31a64002014-10-14 14:29:19 -07001961 pairing_cb.timeout_retries = NUM_TIMEOUT_RETRIES;
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07001962 btif_dm_create_bond_cb_t *create_bond_cb = (btif_dm_create_bond_cb_t*)p_param;
1963 btif_dm_cb_create_bond(&create_bond_cb->bdaddr, create_bond_cb->transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001964 }
1965 break;
1966
1967 case BTIF_DM_CB_REMOVE_BOND:
1968 {
1969 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
1970 }
1971 break;
1972
1973 case BTIF_DM_CB_HID_REMOTE_NAME:
1974 {
1975 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
1976 }
1977 break;
1978
1979 case BTIF_DM_CB_BOND_STATE_BONDING:
1980 {
1981 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
1982 }
1983 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001984 case BTIF_DM_CB_LE_TX_TEST:
1985 case BTIF_DM_CB_LE_RX_TEST:
1986 {
1987 uint8_t status;
1988 STREAM_TO_UINT8(status, p_param);
1989 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1990 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
1991 }
1992 break;
1993 case BTIF_DM_CB_LE_TEST_END:
1994 {
1995 uint8_t status;
1996 uint16_t count = 0;
1997 STREAM_TO_UINT8(status, p_param);
1998 if (status == 0)
1999 STREAM_TO_UINT16(count, p_param);
2000 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2001 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
2002 }
2003 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002004 default:
2005 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002006 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002007 }
2008 break;
2009 }
2010}
2011
2012/*******************************************************************************
2013**
2014** Function bte_dm_evt
2015**
2016** Description Switches context from BTE to BTIF for all DM events
2017**
2018** Returns void
2019**
2020*******************************************************************************/
2021
2022void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
2023{
2024 bt_status_t status;
2025
2026 /* switch context to btif task context (copy full union size for convenience) */
2027 status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event, (void*)p_data, sizeof(tBTA_DM_SEC), NULL);
2028
2029 /* catch any failed context transfers */
2030 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
2031}
2032
2033/*******************************************************************************
2034**
2035** Function bte_search_devices_evt
2036**
2037** Description Switches context from BTE to BTIF for DM search events
2038**
2039** Returns void
2040**
2041*******************************************************************************/
2042static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2043{
2044 UINT16 param_len = 0;
2045
2046 if (p_data)
2047 param_len += sizeof(tBTA_DM_SEARCH);
2048 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
2049 switch (event)
2050 {
2051 case BTA_DM_INQ_RES_EVT:
2052 {
2053 if (p_data->inq_res.p_eir)
2054 param_len += HCI_EXT_INQ_RESPONSE_LEN;
2055 }
2056 break;
2057
2058 case BTA_DM_DISC_RES_EVT:
2059 {
2060 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
2061 param_len += p_data->disc_res.raw_data_size;
2062 }
2063 break;
2064 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002065 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 -08002066
2067 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
2068 if (event == BTA_DM_INQ_RES_EVT)
2069 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
2070
2071 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
2072 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
2073}
2074
2075/*******************************************************************************
2076**
2077** Function bte_dm_search_services_evt
2078**
2079** Description Switches context from BTE to BTIF for DM search services
2080** event
2081**
2082** Returns void
2083**
2084*******************************************************************************/
2085static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2086{
2087 UINT16 param_len = 0;
2088 if (p_data)
2089 param_len += sizeof(tBTA_DM_SEARCH);
2090 switch (event)
2091 {
2092 case BTA_DM_DISC_RES_EVT:
2093 {
2094 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
2095 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
2096 }
2097 } break;
2098 }
2099 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
2100 * if raw_data is needed. */
2101 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
2102 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
2103}
2104
2105/*******************************************************************************
2106**
2107** Function bte_dm_remote_service_record_evt
2108**
2109** Description Switches context from BTE to BTIF for DM search service
2110** record event
2111**
2112** Returns void
2113**
2114*******************************************************************************/
2115static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2116{
2117 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
2118 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
2119}
2120
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002121#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07002122/*******************************************************************************
2123**
2124** Function bta_energy_info_cb
2125**
2126** Description Switches context from BTE to BTIF for DM energy info event
2127**
2128** Returns void
2129**
2130*******************************************************************************/
2131static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time,
2132 tBTA_DM_BLE_IDLE_TIME_MS idle_time,
2133 tBTA_DM_BLE_ENERGY_USED energy_used,
2134 tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status)
2135{
2136 BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld",
2137 status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
2138
2139 btif_activity_energy_info_cb_t btif_cb;
2140 btif_cb.status = status;
2141 btif_cb.ctrl_state = ctrl_state;
2142 btif_cb.tx_time = (uint64_t) tx_time;
2143 btif_cb.rx_time = (uint64_t) rx_time;
2144 btif_cb.idle_time =(uint64_t) idle_time;
2145 btif_cb.energy_used =(uint64_t) energy_used;
2146 btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
2147 (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL);
2148}
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002149#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07002150
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002151/*******************************************************************************
2152**
2153** Function bte_scan_filt_param_cfg_evt
2154**
2155** Description Scan filter param config event
2156**
2157** Returns void
2158**
2159*******************************************************************************/
2160static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
2161 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
2162 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
2163{
2164 /* This event occurs on calling BTA_DmBleCfgFilterCondition internally,
2165 ** and that is why there is no HAL callback
2166 */
2167 if(BTA_SUCCESS != status)
2168 {
2169 BTIF_TRACE_ERROR("%s, %d", __FUNCTION__, status);
2170 }
2171 else
2172 {
2173 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2174 }
2175}
2176
The Android Open Source Project5738f832012-12-12 16:00:35 -08002177/*****************************************************************************
2178**
2179** btif api functions (no context switch)
2180**
2181*****************************************************************************/
2182
2183/*******************************************************************************
2184**
2185** Function btif_dm_start_discovery
2186**
2187** Description Start device discovery/inquiry
2188**
2189** Returns bt_status_t
2190**
2191*******************************************************************************/
2192bt_status_t btif_dm_start_discovery(void)
2193{
2194 tBTA_DM_INQ inq_params;
2195 tBTA_SERVICE_MASK services = 0;
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002196 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002197
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002198 BTIF_TRACE_EVENT("%s", __FUNCTION__);
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002199
2200#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2201 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
2202 /* Cleanup anything remaining on index 0 */
2203 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
2204 bte_scan_filt_param_cfg_evt, 0);
2205
2206 /* Add an allow-all filter on index 0*/
2207 adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE;
2208 adv_filt_param.feat_seln = ALLOW_ALL_FILTER;
2209 adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;
2210 adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;
2211 adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE;
2212 adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE;
2213 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL,
2214 bte_scan_filt_param_cfg_evt, 0);
2215
The Android Open Source Project5738f832012-12-12 16:00:35 -08002216 /* TODO: Do we need to handle multiple inquiries at the same time? */
2217
2218 /* Set inquiry params and call API */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002219 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
Matthew Xie7f3e4292013-09-30 12:44:10 -07002220#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
2221 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
2222 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
2223 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
2224 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
2225#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002226#else
2227 inq_params.mode = BTA_DM_GENERAL_INQUIRY;
2228#endif
2229 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
2230
2231 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
2232 inq_params.report_dup = TRUE;
2233
2234 inq_params.filter_type = BTA_DM_INQ_CLR;
2235 /* TODO: Filter device by BDA needs to be implemented here */
2236
2237 /* Will be enabled to TRUE once inquiry busy level has been received */
2238 btif_dm_inquiry_in_progress = FALSE;
2239 /* find nearby devices */
2240 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
2241
2242 return BT_STATUS_SUCCESS;
2243}
2244
2245/*******************************************************************************
2246**
2247** Function btif_dm_cancel_discovery
2248**
2249** Description Cancels search
2250**
2251** Returns bt_status_t
2252**
2253*******************************************************************************/
2254bt_status_t btif_dm_cancel_discovery(void)
2255{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002256 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002257 BTA_DmSearchCancel();
2258 return BT_STATUS_SUCCESS;
2259}
2260
2261/*******************************************************************************
2262**
2263** Function btif_dm_create_bond
2264**
2265** Description Initiate bonding with the specified device
2266**
2267** Returns bt_status_t
2268**
2269*******************************************************************************/
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002270bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr, int transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002271{
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002272 btif_dm_create_bond_cb_t create_bond_cb;
2273 create_bond_cb.transport = transport;
2274 bdcpy(create_bond_cb.bdaddr.address, bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002275
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002276 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002277 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 -08002278 if (pairing_cb.state != BT_BOND_STATE_NONE)
2279 return BT_STATUS_BUSY;
2280
2281 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002282 (char *)&create_bond_cb, sizeof(btif_dm_create_bond_cb_t), NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002283
2284 return BT_STATUS_SUCCESS;
2285}
2286
2287/*******************************************************************************
2288**
2289** Function btif_dm_cancel_bond
2290**
2291** Description Initiate bonding with the specified device
2292**
2293** Returns bt_status_t
2294**
2295*******************************************************************************/
2296
2297bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
2298{
2299 bdstr_t bdstr;
2300
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002301 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 -08002302
2303 /* TODO:
2304 ** 1. Restore scan modes
2305 ** 2. special handling for HID devices
2306 */
2307 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2308 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002309
2310#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2311
2312 if (pairing_cb.is_ssp)
2313 {
2314 if (pairing_cb.is_le_only)
2315 {
2316 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2317 }
2318 else
Hemant Guptae1468692013-11-14 16:21:29 +05302319 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002320 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
Hemant Guptae1468692013-11-14 16:21:29 +05302321 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2322 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2323 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002324 }
2325 else
2326 {
2327 if (pairing_cb.is_le_only)
2328 {
2329 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2330 }
2331 else
2332 {
2333 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2334 }
2335 /* Cancel bonding, in case it is in ACL connection setup state */
2336 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2337 }
2338
2339#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002340 if (pairing_cb.is_ssp)
2341 {
2342 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2343 }
2344 else
2345 {
2346 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2347 }
2348 /* Cancel bonding, in case it is in ACL connection setup state */
2349 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2350 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002351#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002352 }
2353
2354 return BT_STATUS_SUCCESS;
2355}
2356
2357/*******************************************************************************
2358**
Kim Schulza9eb25c2013-09-30 10:55:52 +02002359** Function btif_dm_hh_open_failed
2360**
2361** Description informs the upper layers if the HH have failed during bonding
2362**
2363** Returns none
2364**
2365*******************************************************************************/
2366
2367void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
2368{
2369 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2370 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2371 {
2372 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2373 }
2374}
2375
2376/*******************************************************************************
2377**
The Android Open Source Project5738f832012-12-12 16:00:35 -08002378** Function btif_dm_remove_bond
2379**
2380** Description Removes bonding with the specified device
2381**
2382** Returns bt_status_t
2383**
2384*******************************************************************************/
2385
2386bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
2387{
2388 bdstr_t bdstr;
2389
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002390 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 -08002391 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2392 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2393
2394 return BT_STATUS_SUCCESS;
2395}
2396
2397/*******************************************************************************
2398**
2399** Function btif_dm_pin_reply
2400**
2401** Description BT legacy pairing - PIN code reply
2402**
2403** Returns bt_status_t
2404**
2405*******************************************************************************/
2406
2407bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2408 uint8_t pin_len, bt_pin_code_t *pin_code)
2409{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002410 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Hemant Gupta831423e2014-01-08 12:42:13 +05302411 if (pin_code == NULL)
2412 return BT_STATUS_FAIL;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002413#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002414
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002415 if (pairing_cb.is_le_only)
2416 {
2417 int i;
2418 UINT32 passkey = 0;
2419 int multi[] = {100000, 10000, 1000, 100, 10,1};
2420 BD_ADDR remote_bd_addr;
2421 bdcpy(remote_bd_addr, bd_addr->address);
2422 for (i = 0; i < 6; i++)
2423 {
2424 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2425 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002426 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002427 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2428
2429 }
2430 else
2431 {
2432 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2433 if (accept)
2434 pairing_cb.pin_code_len = pin_len;
2435 }
2436#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002437 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2438
2439 if (accept)
2440 pairing_cb.pin_code_len = pin_len;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002441#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002442 return BT_STATUS_SUCCESS;
2443}
2444
2445/*******************************************************************************
2446**
2447** Function btif_dm_ssp_reply
2448**
2449** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2450**
2451** Returns bt_status_t
2452**
2453*******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002454bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2455 bt_ssp_variant_t variant, uint8_t accept,
2456 uint32_t passkey)
2457{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08002458 UNUSED(passkey);
2459
The Android Open Source Project5738f832012-12-12 16:00:35 -08002460 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2461 {
2462 /* This is not implemented in the stack.
2463 * For devices with display, this is not needed
2464 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002465 BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002466 return BT_STATUS_FAIL;
2467 }
2468 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002469 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002470#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2471 if (pairing_cb.is_le_only)
2472 {
2473 if (accept)
2474 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2475 else
2476 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2477 }
2478 else
2479 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002480
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002481#else
2482 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2483#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002484 return BT_STATUS_SUCCESS;
2485}
2486
2487/*******************************************************************************
2488**
2489** Function btif_dm_get_adapter_property
2490**
2491** Description Queries the BTA for the adapter property
2492**
2493** Returns bt_status_t
2494**
2495*******************************************************************************/
2496bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2497{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002498 BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002499 switch (prop->type)
2500 {
2501 case BT_PROPERTY_BDNAME:
2502 {
2503 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
VenkatRaghavan VijayaRaghavan1d8e6b82015-02-05 22:20:39 -08002504 strncpy((char *)bd_name->name,btif_get_default_local_name(),
2505 sizeof(bd_name->name) - 1);
2506 bd_name->name[sizeof(bd_name->name) - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002507 prop->len = strlen((char *)bd_name->name);
2508 }
2509 break;
2510
2511 case BT_PROPERTY_ADAPTER_SCAN_MODE:
2512 {
2513 /* if the storage does not have it. Most likely app never set it. Default is NONE */
2514 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2515 *mode = BT_SCAN_MODE_NONE;
2516 prop->len = sizeof(bt_scan_mode_t);
2517 }
2518 break;
2519
2520 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2521 {
2522 uint32_t *tmt = (uint32_t*)prop->val;
2523 *tmt = 120; /* default to 120s, if not found in NV */
2524 prop->len = sizeof(uint32_t);
2525 }
2526 break;
2527
2528 default:
2529 prop->len = 0;
2530 return BT_STATUS_FAIL;
2531 }
2532 return BT_STATUS_SUCCESS;
2533}
2534
2535/*******************************************************************************
2536**
2537** Function btif_dm_get_remote_services
2538**
2539** Description Start SDP to get remote services
2540**
2541** Returns bt_status_t
2542**
2543*******************************************************************************/
2544bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2545{
2546 bdstr_t bdstr;
2547
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002548 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 -08002549
2550 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2551 bte_dm_search_services_evt, TRUE);
2552
2553 return BT_STATUS_SUCCESS;
2554}
2555
2556/*******************************************************************************
2557**
2558** Function btif_dm_get_remote_service_record
2559**
2560** Description Start SDP to get remote service record
2561**
2562**
2563** Returns bt_status_t
2564*******************************************************************************/
2565bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2566 bt_uuid_t *uuid)
2567{
2568 tSDP_UUID sdp_uuid;
2569 bdstr_t bdstr;
2570
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002571 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 -08002572
2573 sdp_uuid.len = MAX_UUID_SIZE;
2574 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2575
2576 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2577 bte_dm_remote_service_record_evt, TRUE);
2578
2579 return BT_STATUS_SUCCESS;
2580}
2581
2582void btif_dm_execute_service_request(UINT16 event, char *p_param)
2583{
2584 BOOLEAN b_enable = FALSE;
2585 bt_status_t status;
2586 if (event == BTIF_DM_ENABLE_SERVICE)
2587 {
2588 b_enable = TRUE;
2589 }
2590 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2591 if (status == BT_STATUS_SUCCESS)
2592 {
2593 bt_property_t property;
2594 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2595
2596 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2597 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2598 sizeof(local_uuids), local_uuids);
2599 btif_storage_get_adapter_property(&property);
2600 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2601 BT_STATUS_SUCCESS, 1, &property);
2602 }
2603 return;
2604}
2605
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002606void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2607 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2608{
2609 UINT8 yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2610 /* if local initiated:
2611 ** 1. set DD + MITM
2612 ** if remote initiated:
2613 ** 1. Copy over the auth_req from peer's io_rsp
2614 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2615 ** as a fallback set MITM+GB if peer had MITM set
2616 */
2617 UNUSED (bd_addr);
2618 UNUSED (p_io_cap);
2619 UNUSED (p_oob_data);
2620
2621
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002622 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002623 if(pairing_cb.is_local_initiated)
2624 {
2625 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2626 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2627 }
2628 else if (!is_orig)
2629 {
2630 /* peer initiated paring. They probably know what they want.
2631 ** Copy the mitm from peer device.
2632 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002633 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002634 __FUNCTION__, pairing_cb.auth_req);
2635 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2636
2637 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2638 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2639 *p_auth_req |= BTA_AUTH_SP_YES;
2640 }
2641 else if (yes_no_bit)
2642 {
2643 /* set the general bonding bit for stored device */
2644 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2645 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002646 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002647}
2648
2649void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2650 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2651{
2652 UNUSED (bd_addr);
2653 UNUSED (oob_data);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002654
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002655 if(auth_req & BTA_AUTH_BONDS)
2656 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002657 BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002658 pairing_cb.auth_req = auth_req;
2659 pairing_cb.io_cap = io_cap;
2660 }
2661}
2662
The Android Open Source Project5738f832012-12-12 16:00:35 -08002663#if (BTM_OOB_INCLUDED == TRUE)
2664void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data)
2665{
2666 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2667 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2668 {
2669 *p_oob_data = FALSE;
2670 }
2671 else
2672 {
2673 *p_oob_data = TRUE;
2674 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002675 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 -08002676}
2677#endif /* BTM_OOB_INCLUDED */
2678
2679#ifdef BTIF_DM_OOB_TEST
2680void btif_dm_load_local_oob(void)
2681{
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002682 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002683 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002684 BTIF_TRACE_DEBUG("btif_dm_load_local_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002685 if (prop_oob[0] != '3')
2686 {
2687#if (BTM_OOB_INCLUDED == TRUE)
2688 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2689 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2690 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002691 BTIF_TRACE_DEBUG("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002692 BTA_DmLocalOob();
2693 }
2694#else
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002695 BTIF_TRACE_ERROR("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002696#endif
2697 }
2698}
2699
2700void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2701{
2702 FILE *fp;
2703 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2704 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2705 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002706 char prop_oob[PROPERTY_VALUE_MAX];
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002707 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: valid=%d", valid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002708 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2709 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2710 valid)
2711 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002712 BTIF_TRACE_DEBUG("save local OOB data in memory");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002713 memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2714 memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2715 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002716 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002717 if (prop_oob[0] == '1')
2718 path = path_a;
2719 else if (prop_oob[0] == '2')
2720 path = path_b;
2721 if (path)
2722 {
2723 fp = fopen(path, "wb+");
2724 if (fp == NULL)
2725 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002726 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 -08002727 }
2728 else
2729 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002730 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 -08002731 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2732 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2733 fclose(fp);
2734 }
2735 }
2736 }
2737}
2738BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2739{
2740 char t[128];
2741 FILE *fp;
2742 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2743 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2744 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002745 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002746 BOOLEAN result = FALSE;
2747 bt_bdaddr_t bt_bd_addr;
2748 bdcpy(oob_cb.oob_bdaddr, bd_addr);
2749 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002750 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002751 if (prop_oob[0] == '1')
2752 path = path_b;
2753 else if (prop_oob[0] == '2')
2754 path = path_a;
2755 if (path)
2756 {
2757 fp = fopen(path, "rb");
2758 if (fp == NULL)
2759 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002760 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 -08002761 return FALSE;
2762 }
2763 else
2764 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002765 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob: read OOB data from %s",path);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002766 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2767 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2768 fclose(fp);
2769 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002770 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: TRUE");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002771 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2772 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2773 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002774 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002775 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2776 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
2777 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 -07002778 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: c = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002779 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2780 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
2781 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 -07002782 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: r = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002783 bdcpy(bt_bd_addr.address, bd_addr);
2784 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2785 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2786 result = TRUE;
2787 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002788 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob result=%d",result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002789 return result;
2790}
2791#endif /* BTIF_DM_OOB_TEST */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002792#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2793
2794static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2795{
2796 bt_bdaddr_t bd_addr;
2797 bt_bdname_t bd_name;
2798 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08002799 int dev_type;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002800
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002801 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002802
2803 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08002804 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
2805 {
2806 dev_type = BT_DEVICE_TYPE_BLE;
2807 }
2808 btif_dm_update_ble_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2809 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002810 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2811 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2812
2813 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2814 pairing_cb.is_ssp = FALSE;
2815 cod = COD_UNCLASSIFIED;
2816
2817 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2818 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2819 p_ssp_key_notif->passkey);
2820}
2821
2822/*******************************************************************************
2823**
2824** Function btif_dm_ble_auth_cmpl_evt
2825**
2826** Description Executes authentication complete event in btif context
2827**
2828** Returns void
2829**
2830*******************************************************************************/
2831static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2832{
2833 /* Save link key, if not temporary */
2834 bt_bdaddr_t bd_addr;
2835 bt_status_t status = BT_STATUS_FAIL;
2836 bt_bond_state_t state = BT_BOND_STATE_NONE;
2837
2838 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2839 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2840 {
2841 /* store keys */
2842 }
2843 if (p_auth_cmpl->success)
2844 {
2845 status = BT_STATUS_SUCCESS;
2846 state = BT_BOND_STATE_BONDED;
2847
2848 btif_dm_save_ble_bonding_keys();
2849 BTA_GATTC_Refresh(bd_addr.address);
2850 btif_dm_get_remote_services(&bd_addr);
2851 }
2852 else
2853 {
2854 /*Map the HCI fail reason to bt status */
2855 switch (p_auth_cmpl->fail_reason)
2856 {
Priti Aghera156c52b2014-07-09 14:58:19 -07002857 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
2858 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
2859 btif_dm_remove_ble_bonding_keys();
2860 status = BT_STATUS_AUTH_FAILURE;
2861 break;
2862 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
2863 status = BT_STATUS_AUTH_REJECTED;
2864 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002865 default:
Andre Eisenbachca22ac42013-02-13 17:02:11 +09002866 btif_dm_remove_ble_bonding_keys();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002867 status = BT_STATUS_FAIL;
2868 break;
2869 }
2870 }
2871 bond_state_changed(status, &bd_addr, state);
2872}
2873
2874
2875
2876void btif_dm_load_ble_local_keys(void)
2877{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002878 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2879
2880 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2881 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2882 {
2883 ble_local_key_cb.is_er_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002884 BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002885 }
2886
2887 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
2888 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
2889 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
2890 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
2891 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
2892 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
2893 {
2894 ble_local_key_cb.is_id_keys_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002895 BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002896 }
2897
2898}
2899void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
2900 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
2901{
2902 if (ble_local_key_cb.is_er_rcvd )
2903 {
2904 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2905 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2906 }
2907
2908 if (ble_local_key_cb.is_id_keys_rcvd)
2909 {
2910 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
2911 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
2912 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
2913 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
2914 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002915 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002916}
2917
2918void btif_dm_save_ble_bonding_keys(void)
2919{
2920
2921 bt_bdaddr_t bd_addr;
2922
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002923 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002924
2925 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2926
2927 if (pairing_cb.ble.is_penc_key_rcvd)
2928 {
2929 btif_storage_add_ble_bonding_key(&bd_addr,
2930 (char *) &pairing_cb.ble.penc_key,
2931 BTIF_DM_LE_KEY_PENC,
2932 sizeof(btif_dm_ble_penc_keys_t));
2933 }
2934
2935 if (pairing_cb.ble.is_pid_key_rcvd)
2936 {
2937 btif_storage_add_ble_bonding_key(&bd_addr,
Andre Eisenbach5e808462014-10-21 12:37:53 -07002938 (char *) &pairing_cb.ble.pid_key,
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002939 BTIF_DM_LE_KEY_PID,
Andre Eisenbach5e808462014-10-21 12:37:53 -07002940 sizeof(btif_dm_ble_pid_keys_t));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002941 }
2942
2943
2944 if (pairing_cb.ble.is_pcsrk_key_rcvd)
2945 {
2946 btif_storage_add_ble_bonding_key(&bd_addr,
2947 (char *) &pairing_cb.ble.pcsrk_key,
2948 BTIF_DM_LE_KEY_PCSRK,
2949 sizeof(btif_dm_ble_pcsrk_keys_t));
2950 }
2951
2952
2953 if (pairing_cb.ble.is_lenc_key_rcvd)
2954 {
2955 btif_storage_add_ble_bonding_key(&bd_addr,
2956 (char *) &pairing_cb.ble.lenc_key,
2957 BTIF_DM_LE_KEY_LENC,
2958 sizeof(btif_dm_ble_lenc_keys_t));
2959 }
2960
2961 if (pairing_cb.ble.is_lcsrk_key_rcvd)
2962 {
2963 btif_storage_add_ble_bonding_key(&bd_addr,
2964 (char *) &pairing_cb.ble.lcsrk_key,
2965 BTIF_DM_LE_KEY_LCSRK,
2966 sizeof(btif_dm_ble_lcsrk_keys_t));
2967 }
2968
2969}
2970
2971
2972void btif_dm_remove_ble_bonding_keys(void)
2973{
2974 bt_bdaddr_t bd_addr;
2975
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002976 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002977
2978 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2979 btif_storage_remove_ble_bonding_keys(&bd_addr);
2980}
2981
2982
2983/*******************************************************************************
2984**
2985** Function btif_dm_ble_sec_req_evt
2986**
2987** Description Eprocess security request event in btif context
2988**
2989** Returns void
2990**
2991*******************************************************************************/
2992void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
2993{
2994 bt_bdaddr_t bd_addr;
2995 bt_bdname_t bd_name;
2996 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08002997 int dev_type;
2998
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002999 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003000
3001 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3002 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003003 BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003004 return;
3005 }
3006
3007 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08003008 if (!btif_get_device_type(p_ble_req->bd_addr, &dev_type))
3009 {
3010 dev_type = BT_DEVICE_TYPE_BLE;
3011 }
3012 btif_dm_update_ble_remote_properties(p_ble_req->bd_addr, p_ble_req->bd_name,
3013 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003014
3015 bdcpy(bd_addr.address, p_ble_req->bd_addr);
3016 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
3017
3018 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3019
Andre Eisenbach89363762015-01-26 13:49:36 -08003020 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003021 pairing_cb.is_le_only = TRUE;
3022 pairing_cb.is_ssp = TRUE;
3023
3024 cod = COD_UNCLASSIFIED;
3025
3026 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
3027 BT_SSP_VARIANT_CONSENT, 0);
3028}
3029
3030
3031
3032/*******************************************************************************
3033**
3034** Function btif_dm_ble_passkey_req_evt
3035**
3036** Description Executes pin request event in btif context
3037**
3038** Returns void
3039**
3040*******************************************************************************/
3041static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
3042{
3043 bt_bdaddr_t bd_addr;
3044 bt_bdname_t bd_name;
3045 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08003046 int dev_type;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003047
3048 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08003049 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
3050 {
3051 dev_type = BT_DEVICE_TYPE_BLE;
3052 }
3053 btif_dm_update_ble_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,
3054 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003055
3056 bdcpy(bd_addr.address, p_pin_req->bd_addr);
3057 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
3058
3059 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3060 pairing_cb.is_le_only = TRUE;
3061
3062 cod = COD_UNCLASSIFIED;
3063
3064 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
3065 &bd_addr, &bd_name, cod);
3066}
3067
3068
3069void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
3070 tBT_DEVICE_TYPE dev_type)
3071{
3072 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
3073}
3074
3075static void btif_dm_ble_tx_test_cback(void *p)
3076{
3077 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
3078 (char *)p, 1, NULL);
3079}
3080
3081static void btif_dm_ble_rx_test_cback(void *p)
3082{
3083 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
3084 (char *)p, 1, NULL);
3085}
3086
3087static void btif_dm_ble_test_end_cback(void *p)
3088{
3089 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
3090 (char *)p, 3, NULL);
3091}
3092/*******************************************************************************
3093**
3094** Function btif_le_test_mode
3095**
3096** Description Sends a HCI BLE Test command to the Controller
3097**
3098** Returns BT_STATUS_SUCCESS on success
3099**
3100*******************************************************************************/
3101bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
3102{
3103 switch (opcode) {
3104 case HCI_BLE_TRANSMITTER_TEST:
3105 if (len != 3) return BT_STATUS_PARM_INVALID;
3106 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
3107 break;
3108 case HCI_BLE_RECEIVER_TEST:
3109 if (len != 1) return BT_STATUS_PARM_INVALID;
3110 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
3111 break;
3112 case HCI_BLE_TEST_END:
3113 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
3114 break;
3115 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003116 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003117 return BT_STATUS_UNSUPPORTED;
3118 }
3119 return BT_STATUS_SUCCESS;
3120}
3121
3122#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08003123
3124void btif_dm_on_disable()
3125{
3126 /* cancel any pending pairing requests */
3127 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3128 {
3129 bt_bdaddr_t bd_addr;
3130
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003131 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003132 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3133 btif_dm_cancel_bond(&bd_addr);
3134 }
3135}
Matthew Xie1e5109b2012-11-09 18:26:26 -08003136
Satya Callojie5ba8842014-07-03 17:18:02 -07003137/*******************************************************************************
3138**
3139** Function btif_dm_read_energy_info
3140**
3141** Description Reads the energy info from controller
3142**
3143** Returns void
3144**
3145*******************************************************************************/
3146void btif_dm_read_energy_info()
3147{
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003148#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07003149 BTA_DmBleGetEnergyInfo(bta_energy_info_cb);
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003150#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07003151}
3152
Matthew Xie1e5109b2012-11-09 18:26:26 -08003153static char* btif_get_default_local_name() {
3154 if (btif_default_local_name[0] == '\0')
3155 {
3156 int max_len = sizeof(btif_default_local_name) - 1;
3157 if (BTM_DEF_LOCAL_NAME[0] != '\0')
3158 {
3159 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
3160 }
3161 else
3162 {
3163 char prop_model[PROPERTY_VALUE_MAX];
3164 property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
3165 strncpy(btif_default_local_name, prop_model, max_len);
3166 }
3167 btif_default_local_name[max_len] = '\0';
3168 }
3169 return btif_default_local_name;
3170}