blob: 49553324997a1703db9e5667507ccd67a5de77d1 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
3 * Copyright (C) 2009-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19/************************************************************************************
20 *
21 * Filename: btif_dm.c
22 *
23 * Description: Contains Device Management (DM) related functionality
24 *
25 *
26 ***********************************************************************************/
Sharvil Nanavati44802762014-12-23 23:08:58 -080027
Chris Mantonf8027002015-03-12 09:22:48 -070028#define LOG_TAG "bt_btif_dm"
Sharvil Nanavati44802762014-12-23 23:08:58 -080029
Scott James Remnant933926c2015-04-02 15:22:14 -070030#include <signal.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080031#include <stdio.h>
32#include <stdlib.h>
Scott James Remnant933926c2015-04-02 15:22:14 -070033#include <string.h>
34#include <sys/types.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080035#include <unistd.h>
36
37#include <hardware/bluetooth.h>
38
The Android Open Source Project5738f832012-12-12 16:00:35 -080039#include <cutils/properties.h>
40#include "gki.h"
41#include "btu.h"
Sharvil Nanavati95b74f22015-03-12 15:55:21 -070042#include "btcore/include/bdaddr.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080043#include "bta_api.h"
44#include "btif_api.h"
45#include "btif_util.h"
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080046#include "btif_dm.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080047#include "btif_storage.h"
48#include "btif_hh.h"
49#include "btif_config.h"
50
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080051#include "bta_gatt_api.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080052#include "include/stack_config.h"
Chris Mantonf8027002015-03-12 09:22:48 -070053
Sharvil Nanavati44802762014-12-23 23:08:58 -080054#include "osi/include/log.h"
Andre Eisenbach31a64002014-10-14 14:29:19 -070055
56/******************************************************************************
57** Device specific workarounds
58******************************************************************************/
59
60/**
61 * The devices below have proven problematic during the pairing process, often
62 * requiring multiple retries to complete pairing. To avoid degrading the user
63 * experience for other devices, explicitely blacklist troubled devices here.
64 */
65static const UINT8 blacklist_pairing_retries[][3] = {
66 {0x9C, 0xDF, 0x03} // BMW car kits (Harman/Becker)
67};
68
69BOOLEAN blacklistPairingRetries(BD_ADDR bd_addr)
70{
71 const unsigned blacklist_size = sizeof(blacklist_pairing_retries)
72 / sizeof(blacklist_pairing_retries[0]);
73 for (unsigned i = 0; i != blacklist_size; ++i)
74 {
75 if (blacklist_pairing_retries[i][0] == bd_addr[0] &&
76 blacklist_pairing_retries[i][1] == bd_addr[1] &&
77 blacklist_pairing_retries[i][2] == bd_addr[2])
78 return TRUE;
79 }
80 return FALSE;
81}
82
The Android Open Source Project5738f832012-12-12 16:00:35 -080083/******************************************************************************
84** Constants & Macros
85******************************************************************************/
86
87#define COD_UNCLASSIFIED ((0x1F) << 8)
Priti Agheraebb1d752012-11-27 18:03:22 -080088#define COD_HID_KEYBOARD 0x0540
89#define COD_HID_POINTING 0x0580
90#define COD_HID_COMBO 0x05C0
91#define COD_HID_MAJOR 0x0500
92#define COD_AV_HEADSETS 0x0404
93#define COD_AV_HANDSFREE 0x0408
94#define COD_AV_HEADPHONES 0x0418
95#define COD_AV_PORTABLE_AUDIO 0x041C
96#define COD_AV_HIFI_AUDIO 0x0428
The Android Open Source Project5738f832012-12-12 16:00:35 -080097
98
99#define BTIF_DM_DEFAULT_INQ_MAX_RESULTS 0
100#define BTIF_DM_DEFAULT_INQ_MAX_DURATION 10
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700101#define BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING 2
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800102
Andre Eisenbach31a64002014-10-14 14:29:19 -0700103#define NUM_TIMEOUT_RETRIES 5
104
Matthew Xie1e5109b2012-11-09 18:26:26 -0800105#define PROPERTY_PRODUCT_MODEL "ro.product.model"
Matthew Xiea30d95a2013-09-18 12:30:36 -0700106#define DEFAULT_LOCAL_NAME_MAX 31
Matthew Xie1e5109b2012-11-09 18:26:26 -0800107#if (DEFAULT_LOCAL_NAME_MAX > BTM_MAX_LOC_BD_NAME_LEN)
108 #error "default btif local name size exceeds stack supported length"
109#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800110
Matthew Xie7f3e4292013-09-30 12:44:10 -0700111#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
112#define BTIF_DM_INTERLEAVE_DURATION_BR_ONE 2
113#define BTIF_DM_INTERLEAVE_DURATION_LE_ONE 2
114#define BTIF_DM_INTERLEAVE_DURATION_BR_TWO 3
115#define BTIF_DM_INTERLEAVE_DURATION_LE_TWO 4
116#endif
117
Priti Agherac0edf9f2014-06-26 11:23:51 -0700118#define MAX_SDP_BL_ENTRIES 3
119
Andre Eisenbach89363762015-01-26 13:49:36 -0800120#define BOND_TYPE_UNKNOWN 0
121#define BOND_TYPE_PERSISTENT 1
122#define BOND_TYPE_TEMPORARY 2
123
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800124#define ENCRYPTED_BREDR 2
125#define ENCRYPTED_LE 4
126
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800127typedef struct
128{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800129 bt_bond_state_t state;
130 BD_ADDR bd_addr;
Andre Eisenbach89363762015-01-26 13:49:36 -0800131 UINT8 bond_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800132 UINT8 pin_code_len;
133 UINT8 is_ssp;
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -0700134 UINT8 auth_req;
135 UINT8 io_cap;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800136 UINT8 autopair_attempts;
Andre Eisenbach31a64002014-10-14 14:29:19 -0700137 UINT8 timeout_retries;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800138 UINT8 is_local_initiated;
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700139 UINT8 sdp_attempts;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800140#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
141 BOOLEAN is_le_only;
142 btif_dm_ble_cb_t ble;
143#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800144} btif_dm_pairing_cb_t;
145
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800146
147typedef struct
148{
149 UINT8 ir[BT_OCTET16_LEN];
150 UINT8 irk[BT_OCTET16_LEN];
151 UINT8 dhk[BT_OCTET16_LEN];
152}btif_dm_local_key_id_t;
153
154typedef struct
155{
156 BOOLEAN is_er_rcvd;
157 UINT8 er[BT_OCTET16_LEN];
158 BOOLEAN is_id_keys_rcvd;
159 btif_dm_local_key_id_t id_keys; /* ID kyes */
160
161}btif_dm_local_key_cb_t;
162
163typedef struct
164{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800165 BD_ADDR bd_addr;
166 BD_NAME bd_name;
167} btif_dm_remote_name_t;
168
169typedef struct
170{
171 BT_OCTET16 sp_c;
172 BT_OCTET16 sp_r;
173 BD_ADDR oob_bdaddr; /* peer bdaddr*/
174} btif_dm_oob_cb_t;
Satya Callojie5ba8842014-07-03 17:18:02 -0700175
176typedef struct
177{
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700178 bt_bdaddr_t bdaddr;
179 UINT8 transport; /* 0=Unknown, 1=BR/EDR, 2=LE */
180} btif_dm_create_bond_cb_t;
181
182typedef struct
183{
Satya Callojie5ba8842014-07-03 17:18:02 -0700184 uint8_t status;
185 uint8_t ctrl_state;
186 uint64_t tx_time;
187 uint64_t rx_time;
188 uint64_t idle_time;
189 uint64_t energy_used;
190} btif_activity_energy_info_cb_t;
191
Priti Agherac0edf9f2014-06-26 11:23:51 -0700192typedef struct
193{
194 unsigned int manufact_id;
195}skip_sdp_entry_t;
196
The Android Open Source Project5738f832012-12-12 16:00:35 -0800197#define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id))
198
Priti Agherac0edf9f2014-06-26 11:23:51 -0700199#define MAX_SDP_BL_ENTRIES 3
200#define UUID_HUMAN_INTERFACE_DEVICE "00001124-0000-1000-8000-00805f9b34fb"
201
202static skip_sdp_entry_t sdp_blacklist[] = {{76}}; //Apple Mouse and Keyboard
203
204
The Android Open Source Project5738f832012-12-12 16:00:35 -0800205/* This flag will be true if HCI_Inquiry is in progress */
206static BOOLEAN btif_dm_inquiry_in_progress = FALSE;
207
Priti Agherac0edf9f2014-06-26 11:23:51 -0700208
209
Matthew Xie1e5109b2012-11-09 18:26:26 -0800210/************************************************************************************
211** Static variables
212************************************************************************************/
213static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'};
214
The Android Open Source Project5738f832012-12-12 16:00:35 -0800215/******************************************************************************
216** Static functions
217******************************************************************************/
218static btif_dm_pairing_cb_t pairing_cb;
219static btif_dm_oob_cb_t oob_cb;
220static void btif_dm_generic_evt(UINT16 event, char* p_param);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700221static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800222static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name);
223static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
224 DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800225#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
226static btif_dm_local_key_cb_t ble_local_key_cb;
227static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif);
228static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl);
229static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req);
230#endif
Satya Calloji6e2d9db2014-07-08 16:18:58 -0700231
232static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
233 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
234 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status);
235
Matthew Xie1e5109b2012-11-09 18:26:26 -0800236static char* btif_get_default_local_name();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800237/******************************************************************************
238** Externs
239******************************************************************************/
240extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
241extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
242extern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
243extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
Hemant Gupta10256872013-08-19 18:33:01 +0530244extern bt_status_t btif_hf_client_execute_service(BOOLEAN b_enable);
Hemant Gupta2dc99992014-04-18 12:54:08 +0530245extern bt_status_t btif_mce_execute_service(BOOLEAN b_enable);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800246extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700247extern 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 -0800248
249
250/******************************************************************************
251** Functions
252******************************************************************************/
253
254bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
255 BOOLEAN b_enable)
256{
257 /* Check the service_ID and invoke the profile's BT state changed API */
258 switch (service_id)
259 {
260 case BTA_HFP_SERVICE_ID:
261 case BTA_HSP_SERVICE_ID:
262 {
263 btif_hf_execute_service(b_enable);
264 }break;
Sharvil Nanavati9609cee2014-10-15 18:30:49 -0700265 case BTA_A2DP_SOURCE_SERVICE_ID:
The Android Open Source Project5738f832012-12-12 16:00:35 -0800266 {
267 btif_av_execute_service(b_enable);
268 }break;
269 case BTA_HID_SERVICE_ID:
270 {
271 btif_hh_execute_service(b_enable);
272 }break;
Hemant Gupta10256872013-08-19 18:33:01 +0530273 case BTA_HFP_HS_SERVICE_ID:
274 {
275 btif_hf_client_execute_service(b_enable);
276 }break;
Hemant Gupta2dc99992014-04-18 12:54:08 +0530277 case BTA_MAP_SERVICE_ID:
278 {
279 btif_mce_execute_service(b_enable);
280 }break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800281 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700282 BTIF_TRACE_ERROR("%s: Unknown service being enabled", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800283 return BT_STATUS_FAIL;
284 }
285 return BT_STATUS_SUCCESS;
286}
287
288/*******************************************************************************
289**
290** Function check_eir_remote_name
291**
292** Description Check if remote name is in the EIR data
293**
294** Returns TRUE if remote name found
295** Populate p_remote_name, if provided and remote name found
296**
297*******************************************************************************/
298static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
299 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
300{
301 UINT8 *p_eir_remote_name = NULL;
302 UINT8 remote_name_len = 0;
303
304 /* Check EIR for remote name and services */
305 if (p_search_data->inq_res.p_eir)
306 {
Zach Johnsona50fc882014-10-30 21:02:58 -0700307 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800308 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
309 if (!p_eir_remote_name)
310 {
Zach Johnsona50fc882014-10-30 21:02:58 -0700311 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800312 BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
313 }
314
315 if (p_eir_remote_name)
316 {
317 if (remote_name_len > BD_NAME_LEN)
318 remote_name_len = BD_NAME_LEN;
319
320 if (p_remote_name && p_remote_name_len)
321 {
322 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
323 *(p_remote_name + remote_name_len) = 0;
324 *p_remote_name_len = remote_name_len;
325 }
326
327 return TRUE;
328 }
329 }
330
331 return FALSE;
332
333}
334
335/*******************************************************************************
336**
337** Function check_cached_remote_name
338**
339** Description Check if remote name is in the NVRAM cache
340**
341** Returns TRUE if remote name found
342** Populate p_remote_name, if provided and remote name found
343**
344*******************************************************************************/
345static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
346 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
347{
348 bt_bdname_t bdname;
349 bt_bdaddr_t remote_bdaddr;
350 bt_property_t prop_name;
351
352 /* check if we already have it in our btif_storage cache */
353 bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
354 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
355 sizeof(bt_bdname_t), &bdname);
356 if (btif_storage_get_remote_device_property(
357 &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
358 {
359 if (p_remote_name && p_remote_name_len)
360 {
361 strcpy((char *)p_remote_name, (char *)bdname.name);
362 *p_remote_name_len = strlen((char *)p_remote_name);
363 }
364 return TRUE;
365 }
366
367 return FALSE;
368}
369
370BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
371{
372 uint32_t remote_cod;
373 bt_property_t prop_name;
374
375 /* check if we already have it in our btif_storage cache */
376 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
377 sizeof(uint32_t), &remote_cod);
378 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
379 {
Chris Mantonf8027002015-03-12 09:22:48 -0700380 LOG_INFO("%s remote_cod = 0x%08x cod = 0x%08x", __func__, remote_cod, cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800381 if ((remote_cod & 0x7ff) == cod)
382 return TRUE;
383 }
384
385 return FALSE;
386}
387
Priti Agheraebb1d752012-11-27 18:03:22 -0800388BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
389{
390 uint32_t remote_cod;
391 bt_property_t prop_name;
392
393 /* check if we already have it in our btif_storage cache */
394 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
395 sizeof(uint32_t), &remote_cod);
396 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
397 &prop_name) == BT_STATUS_SUCCESS)
398 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700399 BTIF_TRACE_DEBUG("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
Priti Agheraebb1d752012-11-27 18:03:22 -0800400 if ((remote_cod & 0x700) == cod)
401 return TRUE;
402 }
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700403 return FALSE;
404}
Priti Agheraebb1d752012-11-27 18:03:22 -0800405
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700406BOOLEAN check_hid_le(const bt_bdaddr_t *remote_bdaddr)
407{
408 uint32_t remote_dev_type;
409 bt_property_t prop_name;
410
411 /* check if we already have it in our btif_storage cache */
412 BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE,
413 sizeof(uint32_t), &remote_dev_type);
414 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
415 &prop_name) == BT_STATUS_SUCCESS)
416 {
417 if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE)
418 {
419 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -0700420 bdaddr_to_string(remote_bdaddr, bdstr, sizeof(bdstr));
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700421 if(btif_config_exist(bdstr, "HidAppId"))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700422 return TRUE;
423 }
424 }
Priti Agheraebb1d752012-11-27 18:03:22 -0800425 return FALSE;
426}
427
Priti Agherac0edf9f2014-06-26 11:23:51 -0700428/*****************************************************************************
429**
430** Function check_sdp_bl
431**
432** Description Checks if a given device is blacklisted to skip sdp
433**
434** Parameters skip_sdp_entry
435**
436** Returns TRUE if the device is present in blacklist, else FALSE
437**
438*******************************************************************************/
439BOOLEAN check_sdp_bl(const bt_bdaddr_t *remote_bdaddr)
440{
441 UINT8 i = 0;
442 UINT16 manufacturer = 0;
443 UINT8 lmp_ver = 0;
444 UINT16 lmp_subver = 0;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700445 bt_property_t prop_name;
446 bt_remote_version_t info;
447 bt_status_t status;
448
449
450 if (remote_bdaddr == NULL)
451 return FALSE;
452
453/* fetch additional info about remote device used in iop query */
Sharvil Nanavatif1c764f2015-02-23 17:31:48 -0800454 BTM_ReadRemoteVersion(*(BD_ADDR*)remote_bdaddr, &lmp_ver,
Priti Agherac0edf9f2014-06-26 11:23:51 -0700455 &manufacturer, &lmp_subver);
456
457
458
459 /* if not available yet, try fetching from config database */
460 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO,
461 sizeof(bt_remote_version_t), &info);
462
463 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
464 &prop_name) != BT_STATUS_SUCCESS)
465 {
466
467 return FALSE;
468 }
469 manufacturer = info.manufacturer;
470
471 for (int i = 0; i < MAX_SDP_BL_ENTRIES; i++)
472 {
473 if (manufacturer == sdp_blacklist[i].manufact_id)
474 return TRUE;
475 }
476 return FALSE;
477}
478
479
The Android Open Source Project5738f832012-12-12 16:00:35 -0800480static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
481{
482 /* Send bonding state only once - based on outgoing/incoming we may receive duplicates */
483 if ( (pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING) )
484 return;
485
Andre Eisenbach89363762015-01-26 13:49:36 -0800486 if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800487 {
488 state = BT_BOND_STATE_NONE;
489 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700490 BTIF_TRACE_DEBUG("%s: state=%d prev_state=%d", __FUNCTION__, state, pairing_cb.state);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800491
492 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
493
494 if (state == BT_BOND_STATE_BONDING)
495 {
496 pairing_cb.state = state;
497 bdcpy(pairing_cb.bd_addr, bd_addr->address);
498 }
499 else
500 {
501 memset(&pairing_cb, 0, sizeof(pairing_cb));
502 }
503
504}
505
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800506/* store remote version in bt config to always have access
507 to it post pairing*/
508static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
509{
510 bt_property_t property;
511 UINT8 lmp_ver = 0;
512 UINT16 lmp_subver = 0;
513 UINT16 mfct_set = 0;
514 tBTM_STATUS btm_status;
515 bt_remote_version_t info;
516 bt_status_t status;
517 bdstr_t bdstr;
518
519 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
520 &mfct_set, &lmp_subver);
521
Sharvil Nanavati44802762014-12-23 23:08:58 -0800522 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 -0800523 lmp_ver, mfct_set, lmp_subver);
524
525 if (btm_status == BTM_SUCCESS)
526 {
527 /* always update cache to ensure we have availability whenever BTM API
528 is not populated */
529 info.manufacturer = mfct_set;
530 info.sub_ver = lmp_subver;
531 info.version = lmp_ver;
532 BTIF_STORAGE_FILL_PROPERTY(&property,
533 BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
534 &info);
535 status = btif_storage_set_remote_device_property(p_bd, &property);
536 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
537 }
538}
539
The Android Open Source Project5738f832012-12-12 16:00:35 -0800540
541static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
542 DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
543{
544 int num_properties = 0;
545 bt_property_t properties[3];
546 bt_bdaddr_t bdaddr;
547 bt_status_t status;
548 UINT32 cod;
549 bt_device_type_t dev_type;
550
551 memset(properties, 0, sizeof(properties));
552 bdcpy(bdaddr.address, bd_addr);
553
554 /* remote name */
555 if (strlen((const char *) bd_name))
556 {
557 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
558 BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
559 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
560 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
561 num_properties++;
562 }
563
564 /* class of device */
565 cod = devclass2uint(dev_class);
Chris Mantonf8027002015-03-12 09:22:48 -0700566 BTIF_TRACE_DEBUG("%s cod is 0x%06x", __func__, cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800567 if ( cod == 0) {
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530568 /* Try to retrieve cod from storage */
Chris Mantonf8027002015-03-12 09:22:48 -0700569 BTIF_TRACE_DEBUG("%s cod is 0, checking cod from storage", __func__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530570 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
571 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
572 status = btif_storage_get_remote_device_property(&bdaddr, &properties[num_properties]);
Chris Mantonf8027002015-03-12 09:22:48 -0700573 BTIF_TRACE_DEBUG("%s cod retrieved from storage is 0x%06x", __func__, cod);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530574 if ( cod == 0) {
Chris Mantonf8027002015-03-12 09:22:48 -0700575 BTIF_TRACE_DEBUG("%s cod is again 0, set as unclassified", __func__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530576 cod = COD_UNCLASSIFIED;
577 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800578 }
579
580 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
581 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
582 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
583 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
584 num_properties++;
585
586 /* device type */
587 dev_type = device_type;
588 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
589 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
590 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
591 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
592 num_properties++;
593
594 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
595 status, &bdaddr, num_properties, properties);
596}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800597
598/*******************************************************************************
599**
600** Function btif_dm_cb_hid_remote_name
601**
602** Description Remote name callback for HID device. Called in btif context
603** Special handling for HID devices
604**
605** Returns void
606**
607*******************************************************************************/
608static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
609{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700610 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 -0800611 if (pairing_cb.state == BT_BOND_STATE_BONDING)
612 {
613 bt_bdaddr_t remote_bd;
614
615 bdcpy(remote_bd.address, pairing_cb.bd_addr);
616
617 if (p_remote_name->status == BTM_SUCCESS)
618 {
619 bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
620 }
621 else
622 bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
623 }
624}
625
The Android Open Source Project5738f832012-12-12 16:00:35 -0800626/*******************************************************************************
627**
628** Function btif_dm_cb_create_bond
629**
630** Description Create bond initiated from the BTIF thread context
631** Special handling for HID devices
632**
633** Returns void
634**
635*******************************************************************************/
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700636static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800637{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800638 BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800639 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800640
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800641#if BLE_INCLUDED == TRUE
642 int device_type;
643 int addr_type;
644 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -0700645 bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr));
Matthew Xie64c54792014-09-16 00:55:03 -0700646 if (transport == BT_TRANSPORT_LE)
647 {
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700648 if (!btif_config_get_int((char const *)&bdstr,"DevType", &device_type))
Matthew Xie64c54792014-09-16 00:55:03 -0700649 {
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700650 btif_config_set_int(bdstr, "DevType", BT_DEVICE_TYPE_BLE);
Matthew Xie64c54792014-09-16 00:55:03 -0700651 }
652 if (btif_storage_get_remote_addr_type(bd_addr, &addr_type) != BT_STATUS_SUCCESS)
653 {
654 btif_storage_set_remote_addr_type(bd_addr, BLE_ADDR_PUBLIC);
655 }
656 }
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700657 if((btif_config_get_int((char const *)&bdstr,"DevType", &device_type) &&
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800658 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
lungtsai_lin3baff792014-08-29 13:47:47 +0800659 (device_type == BT_DEVICE_TYPE_BLE)) || (transport == BT_TRANSPORT_LE))
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800660 {
661 BTA_DmAddBleDevice(bd_addr->address, addr_type, BT_DEVICE_TYPE_BLE);
662 }
663#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800664
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800665#if BLE_INCLUDED == TRUE
666 if(is_hid && device_type != BT_DEVICE_TYPE_BLE)
667#else
668 if(is_hid)
669#endif
670 {
671 int status;
672 status = btif_hh_connect(bd_addr);
673 if(status != BT_STATUS_SUCCESS)
674 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800675 }
676 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800677 {
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700678 BTA_DmBondByTransport((UINT8 *)bd_addr->address, transport);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800679 }
680 /* Track originator of bond creation */
681 pairing_cb.is_local_initiated = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800682
683}
684
685/*******************************************************************************
686**
687** Function btif_dm_cb_remove_bond
688**
689** Description remove bond initiated from the BTIF thread context
690** Special handling for HID devices
691**
692** Returns void
693**
694*******************************************************************************/
695void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
696{
697 bdstr_t bdstr;
698 /*special handling for HID devices */
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700699 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there
700 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
701#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
702 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
703#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800704 {
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700705 BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800706 }
707}
708
709/*******************************************************************************
710**
Andre Eisenbach249f6002014-06-18 12:20:37 -0700711** Function btif_dm_get_connection_state
712**
713** Description Returns whether the remote device is currently connected
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800714** and whether encryption is active for the connection
Andre Eisenbach249f6002014-06-18 12:20:37 -0700715**
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800716** Returns 0 if not connected; 1 if connected and > 1 if connection is
717** encrypted
Andre Eisenbach249f6002014-06-18 12:20:37 -0700718**
719*******************************************************************************/
720uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr)
721{
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800722 uint8_t *bda = (uint8_t*)bd_addr->address;
723 uint16_t rc = BTA_DmGetConnectionState(bda);
724
725 if (rc != 0)
726 {
727 uint8_t flags = 0;
728
729 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_BR_EDR);
730 BTIF_TRACE_DEBUG("%s: security flags (BR/EDR)=0x%02x", __FUNCTION__, flags);
731 if (flags & BTM_SEC_FLAG_ENCRYPTED)
732 rc |= ENCRYPTED_BREDR;
733
734 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_LE);
735 BTIF_TRACE_DEBUG("%s: security flags (LE)=0x%02x", __FUNCTION__, flags);
736 if (flags & BTM_SEC_FLAG_ENCRYPTED)
737 rc |= ENCRYPTED_LE;
738 }
739
740 return rc;
Andre Eisenbach249f6002014-06-18 12:20:37 -0700741}
742
743/*******************************************************************************
744**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800745** Function search_devices_copy_cb
746**
747** Description Deep copy callback for search devices event
748**
749** Returns void
750**
751*******************************************************************************/
752static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
753{
754 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
755 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
756
757 if (!p_src)
758 return;
759
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700760 BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800761 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
762 switch (event)
763 {
764 case BTA_DM_INQ_RES_EVT:
765 {
766 if (p_src_data->inq_res.p_eir)
767 {
768 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
769 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
770 }
771 }
772 break;
773
774 case BTA_DM_DISC_RES_EVT:
775 {
776 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
777 {
778 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
779 memcpy(p_dest_data->disc_res.p_raw_data,
780 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
781 }
782 }
783 break;
784 }
785}
786
787static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
788{
789 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
790 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
791
792 if (!p_src)
793 return;
794 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
795 switch (event)
796 {
797 case BTA_DM_DISC_RES_EVT:
798 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530799 if (p_src_data->disc_res.result == BTA_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800800 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530801 if (p_src_data->disc_res.num_uuids > 0)
802 {
803 p_dest_data->disc_res.p_uuid_list =
804 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
805 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
806 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
807 GKI_freebuf(p_src_data->disc_res.p_uuid_list);
808 }
809 if (p_src_data->disc_res.p_raw_data != NULL)
810 {
811 GKI_freebuf(p_src_data->disc_res.p_raw_data);
812 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800813 }
814 } break;
815 }
816}
817/******************************************************************************
818**
819** BTIF DM callback events
820**
821*****************************************************************************/
822
823/*******************************************************************************
824**
825** Function btif_dm_pin_req_evt
826**
827** Description Executes pin request event in btif context
828**
829** Returns void
830**
831*******************************************************************************/
832static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
833{
834 bt_bdaddr_t bd_addr;
835 bt_bdname_t bd_name;
836 UINT32 cod;
837 bt_pin_code_t pin_code;
Matthew Xie86f97ed2014-11-10 10:24:46 -0800838 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800839
840 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -0800841 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
842 {
843 dev_type = BT_DEVICE_TYPE_BREDR;
844 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800845 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -0800846 p_pin_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800847
848 bdcpy(bd_addr.address, p_pin_req->bd_addr);
849 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
850
851 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
852
853 cod = devclass2uint(p_pin_req->dev_class);
854
Chris Mantonf8027002015-03-12 09:22:48 -0700855 if (cod == 0) {
856 BTIF_TRACE_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800857 cod = COD_UNCLASSIFIED;
858 }
859
860 /* check for auto pair possiblity only if bond was initiated by local device */
861 if (pairing_cb.is_local_initiated)
862 {
863 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
864 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
865 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
866 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
867 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
868 check_cod(&bd_addr, COD_HID_POINTING))
869 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700870 BTIF_TRACE_DEBUG("%s()cod matches for auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800871 /* Check if this device can be auto paired */
872 if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) &&
873 (pairing_cb.autopair_attempts == 0))
874 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700875 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800876 pin_code.pin[0] = 0x30;
877 pin_code.pin[1] = 0x30;
878 pin_code.pin[2] = 0x30;
879 pin_code.pin[3] = 0x30;
880
881 pairing_cb.autopair_attempts++;
882 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
883 return;
884 }
885 }
886 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
887 check_cod(&bd_addr, COD_HID_COMBO))
888 {
889 if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) &&
890 (pairing_cb.autopair_attempts == 0))
891 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700892 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800893 pin_code.pin[0] = 0x30;
894 pin_code.pin[1] = 0x30;
895 pin_code.pin[2] = 0x30;
896 pin_code.pin[3] = 0x30;
897
898 pairing_cb.autopair_attempts++;
899 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
900 return;
901 }
902 }
903 }
904 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
905 &bd_addr, &bd_name, cod);
906}
907
908/*******************************************************************************
909**
910** Function btif_dm_ssp_cfm_req_evt
911**
912** Description Executes SSP confirm request event in btif context
913**
914** Returns void
915**
916*******************************************************************************/
917static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
918{
919 bt_bdaddr_t bd_addr;
920 bt_bdname_t bd_name;
921 UINT32 cod;
922 BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
Matthew Xie86f97ed2014-11-10 10:24:46 -0800923 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800924
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700925 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800926
927 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -0800928 if (!btif_get_device_type(p_ssp_cfm_req->bd_addr, &dev_type))
929 {
930 dev_type = BT_DEVICE_TYPE_BREDR;
931 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800932 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -0800933 p_ssp_cfm_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800934
935 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
936 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
937
938 /* Set the pairing_cb based on the local & remote authentication requirements */
939 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
940
941 /* if just_works and bonding bit is not set treat this as temporary */
942 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 -0800943 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
944 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
Andre Eisenbach89363762015-01-26 13:49:36 -0800945 pairing_cb.bond_type = BOND_TYPE_TEMPORARY;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800946 else
Andre Eisenbach89363762015-01-26 13:49:36 -0800947 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800948
949 pairing_cb.is_ssp = TRUE;
950
951 /* If JustWorks auto-accept */
952 if (p_ssp_cfm_req->just_works)
953 {
954 /* Pairing consent for JustWorks needed if:
955 * 1. Incoming pairing is detected AND
956 * 2. local IO capabilities are DisplayYesNo AND
957 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
958 */
959 if ((is_incoming) && ((p_ssp_cfm_req->loc_io_caps == 0x01) &&
960 (p_ssp_cfm_req->rmt_io_caps == 0x00 || p_ssp_cfm_req->rmt_io_caps == 0x03)))
961 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700962 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 -0800963 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
964 }
965 else
966 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700967 BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800968 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
969 return;
970 }
971 }
972
973 cod = devclass2uint(p_ssp_cfm_req->dev_class);
974
Chris Mantonf8027002015-03-12 09:22:48 -0700975 if (cod == 0) {
976 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800977 cod = COD_UNCLASSIFIED;
978 }
979
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700980 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800981 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
982 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
983 p_ssp_cfm_req->num_val);
984}
985
986static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
987{
988 bt_bdaddr_t bd_addr;
989 bt_bdname_t bd_name;
990 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -0800991 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800992
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700993 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800994
995 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -0800996 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
997 {
998 dev_type = BT_DEVICE_TYPE_BREDR;
999 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001000 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -08001001 p_ssp_key_notif->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001002
1003 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
1004 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
1005
1006 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1007 pairing_cb.is_ssp = TRUE;
1008 cod = devclass2uint(p_ssp_key_notif->dev_class);
1009
Chris Mantonf8027002015-03-12 09:22:48 -07001010 if (cod == 0) {
1011 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001012 cod = COD_UNCLASSIFIED;
1013 }
1014
1015 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
1016 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
1017 p_ssp_key_notif->passkey);
1018}
1019/*******************************************************************************
1020**
1021** Function btif_dm_auth_cmpl_evt
1022**
1023** Description Executes authentication complete event in btif context
1024**
1025** Returns void
1026**
1027*******************************************************************************/
1028static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
1029{
1030 /* Save link key, if not temporary */
1031 bt_bdaddr_t bd_addr;
1032 bt_status_t status = BT_STATUS_FAIL;
1033 bt_bond_state_t state = BT_BOND_STATE_NONE;
Priti Agherac0edf9f2014-06-26 11:23:51 -07001034 BOOLEAN skip_sdp = FALSE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001035
1036 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
1037 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
1038 {
1039 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 -08001040 (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 -08001041 {
1042 bt_status_t ret;
Andre Eisenbach89363762015-01-26 13:49:36 -08001043 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, bond_type=%d",
1044 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001045 ret = btif_storage_add_bonded_device(&bd_addr,
1046 p_auth_cmpl->key, p_auth_cmpl->key_type,
1047 pairing_cb.pin_code_len);
1048 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
1049 }
1050 else
1051 {
Andre Eisenbach89363762015-01-26 13:49:36 -08001052 BTIF_TRACE_DEBUG("%s: Temporary key. Not storing. key_type=0x%x, bond_type=%d",
1053 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1054 if(pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
Hemant Guptab820aec2013-12-24 19:59:57 +05301055 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001056 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
Hemant Guptab820aec2013-12-24 19:59:57 +05301057 __FUNCTION__);
1058 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1059 return;
1060 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001061 }
1062 }
Priti Agherac0edf9f2014-06-26 11:23:51 -07001063
1064 // Skip SDP for certain HID Devices
The Android Open Source Project5738f832012-12-12 16:00:35 -08001065 if (p_auth_cmpl->success)
1066 {
Andre Eisenbach31a64002014-10-14 14:29:19 -07001067 pairing_cb.timeout_retries = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001068 status = BT_STATUS_SUCCESS;
1069 state = BT_BOND_STATE_BONDED;
Priti Agherac0edf9f2014-06-26 11:23:51 -07001070 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001071
Priti Agherac0edf9f2014-06-26 11:23:51 -07001072 if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr, COD_HID_MAJOR))
1073 {
Sharvil Nanavati44802762014-12-23 23:08:58 -08001074 LOG_WARN("%s:skip SDP", __FUNCTION__);
Priti Agherac0edf9f2014-06-26 11:23:51 -07001075 skip_sdp = TRUE;
1076 }
1077 if(!pairing_cb.is_local_initiated && skip_sdp)
1078 {
1079 bond_state_changed(status, &bd_addr, state);
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -08001080
Sharvil Nanavati44802762014-12-23 23:08:58 -08001081 LOG_WARN("%s: Incoming HID Connection",__FUNCTION__);
Priti Agherac0edf9f2014-06-26 11:23:51 -07001082 bt_property_t prop;
1083 bt_bdaddr_t bd_addr;
1084 bt_uuid_t uuid;
1085 char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE;
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -08001086
Priti Agherac0edf9f2014-06-26 11:23:51 -07001087 string_to_uuid(uuid_str, &uuid);
1088
1089 prop.type = BT_PROPERTY_UUIDS;
1090 prop.val = uuid.uu;
1091 prop.len = MAX_UUID_SIZE;
1092
1093 /* Send the event to the BTIF */
1094 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1095 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1096 }
1097 else
1098 {
1099 /* Trigger SDP on the device */
1100 pairing_cb.sdp_attempts = 1;;
1101
1102 if(btif_dm_inquiry_in_progress)
1103 btif_dm_cancel_discovery();
1104
1105 btif_dm_get_remote_services(&bd_addr);
1106 }
1107 /* 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 -08001108 }
1109 else
1110 {
1111 /*Map the HCI fail reason to bt status */
1112 switch(p_auth_cmpl->fail_reason)
1113 {
1114 case HCI_ERR_PAGE_TIMEOUT:
Andre Eisenbach31a64002014-10-14 14:29:19 -07001115 if (blacklistPairingRetries(bd_addr.address) && pairing_cb.timeout_retries)
1116 {
1117 BTIF_TRACE_WARNING("%s() - Pairing timeout; retrying (%d) ...", __FUNCTION__, pairing_cb.timeout_retries);
1118 --pairing_cb.timeout_retries;
1119 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
1120 return;
1121 }
1122 /* Fall-through */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001123 case HCI_ERR_CONNECTION_TOUT:
1124 status = BT_STATUS_RMT_DEV_DOWN;
1125 break;
1126
Hemant Guptaaef7a672013-07-31 19:00:12 +05301127 case HCI_ERR_PAIRING_NOT_ALLOWED:
1128 status = BT_STATUS_AUTH_REJECTED;
1129 break;
1130
Hemant Guptab4801442014-01-07 18:11:15 +05301131 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
1132 status = BT_STATUS_AUTH_FAILURE;
1133 break;
1134
The Android Open Source Project5738f832012-12-12 16:00:35 -08001135 /* map the auth failure codes, so we can retry pairing if necessary */
1136 case HCI_ERR_AUTH_FAILURE:
Hemant Gupta59a88ec2014-03-19 19:01:35 +05301137 case HCI_ERR_KEY_MISSING:
Zhihai Xua7ea8092013-11-27 14:10:53 +05301138 btif_storage_remove_bonded_device(&bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001139 case HCI_ERR_HOST_REJECT_SECURITY:
1140 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
1141 case HCI_ERR_UNIT_KEY_USED:
1142 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
1143 case HCI_ERR_INSUFFCIENT_SECURITY:
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301144 case HCI_ERR_PEER_USER:
1145 case HCI_ERR_UNSPECIFIED:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001146 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d",
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301147 __FUNCTION__, p_auth_cmpl->fail_reason);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001148 if (pairing_cb.autopair_attempts == 1)
1149 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001150 BTIF_TRACE_DEBUG("%s(): Adding device to blacklist ", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001151
1152 /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class */
1153 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
1154 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
1155 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
1156 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
1157 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
1158 check_cod(&bd_addr, COD_HID_POINTING))
1159 {
1160 btif_storage_add_device_to_autopair_blacklist (&bd_addr);
1161 }
1162 pairing_cb.autopair_attempts++;
1163
1164 /* Create the Bond once again */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001165 BTIF_TRACE_DEBUG("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07001166 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001167 return;
1168 }
1169 else
1170 {
1171 /* if autopair attempts are more than 1, or not attempted */
1172 status = BT_STATUS_AUTH_FAILURE;
1173 }
1174 break;
1175
1176 default:
1177 status = BT_STATUS_FAIL;
1178 }
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301179 /* Special Handling for HID Devices */
1180 if (check_cod(&bd_addr, COD_HID_POINTING)) {
1181 /* Remove Device as bonded in nvram as authentication failed */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001182 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __FUNCTION__);
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301183 btif_storage_remove_bonded_device(&bd_addr);
1184 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001185 bond_state_changed(status, &bd_addr, state);
1186 }
1187}
1188
1189/******************************************************************************
1190**
1191** Function btif_dm_search_devices_evt
1192**
1193** Description Executes search devices callback events in btif context
1194**
1195** Returns void
1196**
1197******************************************************************************/
1198static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
1199{
1200 tBTA_DM_SEARCH *p_search_data;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001201 BTIF_TRACE_EVENT("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001202
1203 switch (event)
1204 {
1205 case BTA_DM_DISC_RES_EVT:
1206 {
1207 p_search_data = (tBTA_DM_SEARCH *)p_param;
1208 /* Remote name update */
1209 if (strlen((const char *) p_search_data->disc_res.bd_name))
1210 {
1211 bt_property_t properties[1];
1212 bt_bdaddr_t bdaddr;
1213 bt_status_t status;
1214
1215 properties[0].type = BT_PROPERTY_BDNAME;
1216 properties[0].val = p_search_data->disc_res.bd_name;
1217 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
1218 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
1219
1220 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
1221 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
1222 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1223 status, &bdaddr, 1, properties);
1224 }
1225 /* TODO: Services? */
1226 }
1227 break;
1228
1229 case BTA_DM_INQ_RES_EVT:
1230 {
1231 /* inquiry result */
1232 UINT32 cod;
1233 UINT8 *p_eir_remote_name = NULL;
1234 bt_bdname_t bdname;
1235 bt_bdaddr_t bdaddr;
1236 UINT8 remote_name_len;
1237 UINT8 *p_cached_name = NULL;
1238 tBTA_SERVICE_MASK services = 0;
1239 bdstr_t bdstr;
1240
1241 p_search_data = (tBTA_DM_SEARCH *)p_param;
1242 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
1243
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07001244 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 -08001245#if (BLE_INCLUDED == TRUE)
1246 p_search_data->inq_res.device_type);
1247#else
1248 BT_DEVICE_TYPE_BREDR);
1249#endif
1250 bdname.name[0] = 0;
1251
1252 cod = devclass2uint (p_search_data->inq_res.dev_class);
1253
Chris Mantonf8027002015-03-12 09:22:48 -07001254 if (cod == 0) {
1255 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001256 cod = COD_UNCLASSIFIED;
1257 }
1258
1259 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1260 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1261
1262 /* Check EIR for remote name and services */
1263 if (p_search_data->inq_res.p_eir)
1264 {
1265 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001266 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001267 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */
1268 }
1269
1270
1271 {
1272 bt_property_t properties[5];
1273 bt_device_type_t dev_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001274 UINT8 addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001275 uint32_t num_properties = 0;
1276 bt_status_t status;
1277
1278 memset(properties, 0, sizeof(properties));
1279 /* BD_ADDR */
1280 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1281 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1282 num_properties++;
1283 /* BD_NAME */
1284 /* Don't send BDNAME if it is empty */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001285 if (bdname.name[0])
1286 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001287 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1288 BT_PROPERTY_BDNAME,
1289 strlen((char *)bdname.name), &bdname);
1290 num_properties++;
1291 }
1292
1293 /* DEV_CLASS */
1294 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1295 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1296 num_properties++;
1297 /* DEV_TYPE */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001298#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001299 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1300 dev_type = p_search_data->inq_res.device_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001301 addr_type = p_search_data->inq_res.ble_addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001302#else
1303 dev_type = BT_DEVICE_TYPE_BREDR;
1304#endif
1305 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1306 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1307 num_properties++;
1308 /* RSSI */
1309 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1310 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1311 &(p_search_data->inq_res.rssi));
1312 num_properties++;
1313
1314 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1315 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001316#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1317 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001318 if (( dev_type == BT_DEVICE_TYPE_DUMO)&&
1319 (p_search_data->inq_res.flag & BTA_BLE_DMT_CONTROLLER_SPT) &&
1320 (p_search_data->inq_res.flag & BTA_BLE_DMT_HOST_SPT))
1321 {
1322 btif_storage_set_dmt_support_type (&bdaddr, TRUE);
1323 }
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001324 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1325#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001326 /* Callback to notify upper layer of device */
1327 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1328 num_properties, properties);
1329 }
1330 }
1331 break;
1332
1333 case BTA_DM_INQ_CMPL_EVT:
1334 {
Satya Calloji6e2d9db2014-07-08 16:18:58 -07001335#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1336 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1337 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1338 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1339 bte_scan_filt_param_cfg_evt, 0);
1340#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001341 }
1342 break;
1343 case BTA_DM_DISC_CMPL_EVT:
1344 {
1345 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1346 }
1347 break;
1348 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1349 {
1350 /* if inquiry is not in progress and we get a cancel event, then
1351 * it means we are done with inquiry, but remote_name fetches are in
1352 * progress
1353 *
1354 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt
1355 * but instead wait for the cancel_cmpl_evt via the Busy Level
1356 *
1357 */
1358 if (btif_dm_inquiry_in_progress == FALSE)
1359 {
Nitin Arora7b85efa2014-09-26 14:05:24 -07001360#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1361 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1362 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1363 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1364 bte_scan_filt_param_cfg_evt, 0);
1365#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001366 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1367 }
1368 }
1369 break;
1370 }
1371}
1372
1373/*******************************************************************************
1374**
1375** Function btif_dm_search_services_evt
1376**
1377** Description Executes search services event in btif context
1378**
1379** Returns void
1380**
1381*******************************************************************************/
1382static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1383{
1384 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1385
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001386 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001387 switch (event)
1388 {
1389 case BTA_DM_DISC_RES_EVT:
1390 {
1391 bt_uuid_t uuid_arr[BT_MAX_NUM_UUIDS]; /* Max 32 services */
1392 bt_property_t prop;
1393 uint32_t i = 0, j = 0;
1394 bt_bdaddr_t bd_addr;
1395 bt_status_t ret;
1396
1397 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1398
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001399 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001400 p_data->disc_res.result, p_data->disc_res.services);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001401 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1402 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1403 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1404 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001405 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001406 pairing_cb.sdp_attempts++;
1407 btif_dm_get_remote_services(&bd_addr);
1408 return;
1409 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001410 prop.type = BT_PROPERTY_UUIDS;
1411 prop.len = 0;
1412 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1413 {
1414 prop.val = p_data->disc_res.p_uuid_list;
1415 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1416 for (i=0; i < p_data->disc_res.num_uuids; i++)
1417 {
1418 char temp[256];
Chris Manton8ff3fea2015-01-07 13:59:14 -08001419 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 -07001420 LOG_INFO("%s index:%d uuid:%s", __func__, i, temp);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001421 }
1422 }
1423
1424 /* onUuidChanged requires getBondedDevices to be populated.
1425 ** bond_state_changed needs to be sent prior to remote_device_property
1426 */
1427 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1428 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0)&&
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001429 pairing_cb.sdp_attempts > 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001430 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001431 BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001432 __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001433 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001434 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1435 }
1436
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001437 if(p_data->disc_res.num_uuids != 0)
1438 {
1439 /* Also write this to the NVRAM */
1440 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1441 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1442 /* Send the event to the BTIF */
1443 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1444 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1445 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001446 }
1447 break;
1448
1449 case BTA_DM_DISC_CMPL_EVT:
1450 /* fixme */
1451 break;
1452
Matthew Xie607e3b72013-08-15 19:30:48 -07001453#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001454 case BTA_DM_DISC_BLE_RES_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001455 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__,
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001456 p_data->disc_ble_res.service.uu.uuid16);
1457 bt_uuid_t uuid;
1458 int i = 0;
1459 int j = 15;
1460 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1461 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001462 BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001463 bt_property_t prop;
1464 bt_bdaddr_t bd_addr;
1465 char temp[256];
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001466 bt_status_t ret;
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001467
1468 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1469
1470 while(i < j )
1471 {
1472 unsigned char c = uuid.uu[j];
1473 uuid.uu[j] = uuid.uu[i];
1474 uuid.uu[i] = c;
1475 i++;
1476 j--;
1477 }
1478
Chris Manton8ff3fea2015-01-07 13:59:14 -08001479 uuid_to_string_legacy(&uuid, temp);
Chris Mantonf8027002015-03-12 09:22:48 -07001480 LOG_INFO("%s uuid:%s", __func__, temp);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001481
1482 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1483 prop.type = BT_PROPERTY_UUIDS;
1484 prop.val = uuid.uu;
1485 prop.len = MAX_UUID_SIZE;
1486
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001487 /* Also write this to the NVRAM */
1488 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1489 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1490
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001491 /* Send the event to the BTIF */
1492 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1493 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1494
1495 }
1496 break;
Matthew Xie607e3b72013-08-15 19:30:48 -07001497#endif /* BLE_INCLUDED */
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001498
The Android Open Source Project5738f832012-12-12 16:00:35 -08001499 default:
1500 {
1501 ASSERTC(0, "unhandled search services event", event);
1502 }
1503 break;
1504 }
1505}
1506
1507/*******************************************************************************
1508**
1509** Function btif_dm_remote_service_record_evt
1510**
1511** Description Executes search service record event in btif context
1512**
1513** Returns void
1514**
1515*******************************************************************************/
1516static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1517{
1518 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1519
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001520 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001521 switch (event)
1522 {
1523 case BTA_DM_DISC_RES_EVT:
1524 {
1525 bt_service_record_t rec;
1526 bt_property_t prop;
1527 uint32_t i = 0;
1528 bt_bdaddr_t bd_addr;
1529
1530 memset(&rec, 0, sizeof(bt_service_record_t));
1531 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1532
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001533 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001534 p_data->disc_res.result, p_data->disc_res.services);
1535 prop.type = BT_PROPERTY_SERVICE_RECORD;
1536 prop.val = (void*)&rec;
1537 prop.len = sizeof(rec);
1538
1539 /* disc_res.result is overloaded with SCN. Cannot check result */
1540 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1541 /* TODO: Get the UUID as well */
1542 rec.channel = p_data->disc_res.result - 3;
1543 /* TODO: Need to get the service name using p_raw_data */
1544 rec.name[0] = 0;
1545
1546 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1547 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1548 }
1549 break;
1550
1551 default:
1552 {
1553 ASSERTC(0, "unhandled remote service record event", event);
1554 }
1555 break;
1556 }
1557}
1558
1559/*******************************************************************************
1560**
1561** Function btif_dm_upstreams_cback
1562**
1563** Description Executes UPSTREAMS events in btif context
1564**
1565** Returns void
1566**
1567*******************************************************************************/
1568static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1569{
1570 tBTA_DM_SEC_EVT dm_event = (tBTA_DM_SEC_EVT)event;
1571 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1572 tBTA_SERVICE_MASK service_mask;
1573 uint32_t i;
1574 bt_bdaddr_t bd_addr;
1575
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001576 BTIF_TRACE_EVENT("btif_dm_upstreams_cback ev: %s", dump_dm_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001577
1578 switch (event)
1579 {
1580 case BTA_DM_ENABLE_EVT:
1581 {
1582 BD_NAME bdname;
1583 bt_status_t status;
1584 bt_property_t prop;
1585 prop.type = BT_PROPERTY_BDNAME;
1586 prop.len = BD_NAME_LEN;
1587 prop.val = (void*)bdname;
1588
1589 status = btif_storage_get_adapter_property(&prop);
Matthew Xie1e5109b2012-11-09 18:26:26 -08001590 if (status == BT_STATUS_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001591 {
1592 /* A name exists in the storage. Make this the device name */
1593 BTA_DmSetDeviceName((char*)prop.val);
1594 }
Matthew Xie1e5109b2012-11-09 18:26:26 -08001595 else
1596 {
1597 /* Storage does not have a name yet.
1598 * Use the default name and write it to the chip
1599 */
1600 BTA_DmSetDeviceName(btif_get_default_local_name());
1601 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001602
Andre Eisenbacha015a832014-09-11 14:09:40 -07001603#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1604 /* Enable local privacy */
Andre Eisenbach3e0dc732014-10-24 09:55:34 -07001605 BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED);
Andre Eisenbacha015a832014-09-11 14:09:40 -07001606#endif
1607
The Android Open Source Project5738f832012-12-12 16:00:35 -08001608 /* for each of the enabled services in the mask, trigger the profile
1609 * enable */
1610 service_mask = btif_get_enabled_services_mask();
1611 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1612 {
1613 if (service_mask &
1614 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1615 {
1616 btif_in_execute_service_request(i, TRUE);
1617 }
1618 }
1619 /* clear control blocks */
1620 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1621
1622 /* This function will also trigger the adapter_properties_cb
1623 ** and bonded_devices_info_cb
1624 */
1625 btif_storage_load_bonded_devices();
1626
1627 btif_storage_load_autopair_device_list();
1628
Zach Johnson39110ec2014-10-06 13:15:00 -07001629 btif_enable_bluetooth_evt(p_data->enable.status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001630 }
1631 break;
1632
1633 case BTA_DM_DISABLE_EVT:
1634 /* for each of the enabled services in the mask, trigger the profile
1635 * disable */
1636 service_mask = btif_get_enabled_services_mask();
1637 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1638 {
1639 if (service_mask &
1640 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1641 {
1642 btif_in_execute_service_request(i, FALSE);
1643 }
1644 }
1645 btif_disable_bluetooth_evt();
1646 break;
1647
1648 case BTA_DM_PIN_REQ_EVT:
1649 btif_dm_pin_req_evt(&p_data->pin_req);
1650 break;
1651
1652 case BTA_DM_AUTH_CMPL_EVT:
1653 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1654 break;
1655
1656 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1657 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1658 {
1659 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1660 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1661 }
1662 break;
1663
1664 case BTA_DM_SP_CFM_REQ_EVT:
1665 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1666 break;
1667 case BTA_DM_SP_KEY_NOTIF_EVT:
1668 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1669 break;
1670
1671 case BTA_DM_DEV_UNPAIRED_EVT:
1672 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1673
1674 /*special handling for HID devices */
1675 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -07001676 btif_hh_remove_device(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001677 #endif
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001678 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1679 btif_storage_remove_ble_bonding_keys(&bd_addr);
1680 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001681 btif_storage_remove_bonded_device(&bd_addr);
1682 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1683 break;
1684
1685 case BTA_DM_BUSY_LEVEL_EVT:
1686 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001687
1688 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001689 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001690 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001691 {
1692 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1693 BT_DISCOVERY_STARTED);
1694 btif_dm_inquiry_in_progress = TRUE;
1695 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001696 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001697 {
1698 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1699 BT_DISCOVERY_STOPPED);
1700 btif_dm_inquiry_in_progress = FALSE;
1701 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001702 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001703 {
1704 btif_dm_inquiry_in_progress = FALSE;
1705 }
1706 }
1707 }break;
1708
1709 case BTA_DM_LINK_UP_EVT:
1710 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001711 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001712
1713 btif_update_remote_version_property(&bd_addr);
1714
The Android Open Source Project5738f832012-12-12 16:00:35 -08001715 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1716 &bd_addr, BT_ACL_STATE_CONNECTED);
1717 break;
1718
1719 case BTA_DM_LINK_DOWN_EVT:
1720 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001721 BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001722 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1723 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1724 break;
1725
1726 case BTA_DM_HW_ERROR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001727 BTIF_TRACE_ERROR("Received H/W Error. ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001728 /* Flush storage data */
1729 btif_config_flush();
1730 usleep(100000); /* 100milliseconds */
1731 /* Killing the process to force a restart as part of fault tolerance */
1732 kill(getpid(), SIGKILL);
1733 break;
1734
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001735#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1736 case BTA_DM_BLE_KEY_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001737 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 -08001738
1739 /* If this pairing is by-product of local initiated GATT client Read or Write,
1740 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1741 have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1742 if (pairing_cb.state != BT_BOND_STATE_BONDING)
1743 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001744 BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001745 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1746 BT_BOND_STATE_BONDING);
1747 }
1748 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1749 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001750 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001751 break;
1752 }
1753
1754 switch (p_data->ble_key.key_type)
1755 {
1756 case BTA_LE_KEY_PENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001757 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001758 pairing_cb.ble.is_penc_key_rcvd = TRUE;
1759 memcpy(pairing_cb.ble.penc_key.ltk,p_data->ble_key.key_value.penc_key.ltk, 16);
1760 memcpy(pairing_cb.ble.penc_key.rand, p_data->ble_key.key_value.penc_key.rand,8);
1761 pairing_cb.ble.penc_key.ediv = p_data->ble_key.key_value.penc_key.ediv;
1762 pairing_cb.ble.penc_key.sec_level = p_data->ble_key.key_value.penc_key.sec_level;
1763
1764 for (i=0; i<16; i++)
1765 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001766 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 -08001767 }
1768 for (i=0; i<8; i++)
1769 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001770 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 -08001771 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001772 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.ediv=0x%04x",pairing_cb.ble.penc_key.ediv);
1773 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.sec_level=0x%02x",pairing_cb.ble.penc_key.sec_level);
1774 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 -08001775 break;
1776
1777 case BTA_LE_KEY_PID:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001778 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001779 pairing_cb.ble.is_pid_key_rcvd = TRUE;
Andre Eisenbach5e808462014-10-21 12:37:53 -07001780 pairing_cb.ble.pid_key.addr_type = p_data->ble_key.key_value.pid_key.addr_type;
1781 memcpy(pairing_cb.ble.pid_key.irk, p_data->ble_key.key_value.pid_key.irk, 16);
1782 memcpy(pairing_cb.ble.pid_key.static_addr,
1783 p_data->ble_key.key_value.pid_key.static_addr,BD_ADDR_LEN);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001784 for (i=0; i<16; i++)
1785 {
Andre Eisenbach5e808462014-10-21 12:37:53 -07001786 BTIF_TRACE_DEBUG("pairing_cb.ble.pid_key.irk[%d]=0x%02x"
1787 ,i,pairing_cb.ble.pid_key.irk[i]);
1788 }
1789 for (i=0; i<BD_ADDR_LEN; i++)
1790 {
1791 BTIF_TRACE_DEBUG("piaring_cb.ble.pid_address[%d] = %x"
1792 ,i, pairing_cb.ble.pid_key.static_addr[i]);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001793 }
1794 break;
1795
1796 case BTA_LE_KEY_PCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001797 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001798 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1799 pairing_cb.ble.pcsrk_key.counter = p_data->ble_key.key_value.pcsrk_key.counter;
1800 pairing_cb.ble.pcsrk_key.sec_level = p_data->ble_key.key_value.pcsrk_key.sec_level;
1801 memcpy(pairing_cb.ble.pcsrk_key.csrk,p_data->ble_key.key_value.pcsrk_key.csrk,16);
1802
1803 for (i=0; i<16; i++)
1804 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001805 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 -08001806 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001807 BTIF_TRACE_DEBUG("pairing_cb.ble.pcsrk_key.counter=0x%08x",pairing_cb.ble.pcsrk_key.counter);
1808 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 -08001809 break;
1810
1811 case BTA_LE_KEY_LENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001812 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001813 pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1814 pairing_cb.ble.lenc_key.div = p_data->ble_key.key_value.lenc_key.div;
1815 pairing_cb.ble.lenc_key.key_size = p_data->ble_key.key_value.lenc_key.key_size;
1816 pairing_cb.ble.lenc_key.sec_level = p_data->ble_key.key_value.lenc_key.sec_level;
1817
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001818 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.div=0x%04x",pairing_cb.ble.lenc_key.div);
1819 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.key_size=0x%02x",pairing_cb.ble.lenc_key.key_size);
1820 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 -08001821 break;
1822
1823
1824
1825 case BTA_LE_KEY_LCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001826 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001827 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1828 pairing_cb.ble.lcsrk_key.counter = p_data->ble_key.key_value.lcsrk_key.counter;
1829 pairing_cb.ble.lcsrk_key.div = p_data->ble_key.key_value.lcsrk_key.div;
1830 pairing_cb.ble.lcsrk_key.sec_level = p_data->ble_key.key_value.lcsrk_key.sec_level;
1831
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001832 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.div=0x%04x",pairing_cb.ble.lcsrk_key.div);
1833 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.counter=0x%08x",pairing_cb.ble.lcsrk_key.counter);
1834 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 -08001835
1836 break;
1837
1838 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001839 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001840 break;
1841 }
1842
1843 break;
1844 case BTA_DM_BLE_SEC_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001845 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001846 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1847 break;
1848 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001849 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001850 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1851 break;
1852 case BTA_DM_BLE_PASSKEY_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001853 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001854 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1855 break;
1856 case BTA_DM_BLE_OOB_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001857 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001858 break;
1859 case BTA_DM_BLE_LOCAL_IR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001860 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001861 ble_local_key_cb.is_id_keys_rcvd = TRUE;
1862 memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1863 memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1864 memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1865 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1866 BTIF_DM_LE_LOCAL_KEY_IR,
1867 BT_OCTET16_LEN);
1868 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1869 BTIF_DM_LE_LOCAL_KEY_IRK,
1870 BT_OCTET16_LEN);
1871 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1872 BTIF_DM_LE_LOCAL_KEY_DHK,
1873 BT_OCTET16_LEN);
1874 break;
1875 case BTA_DM_BLE_LOCAL_ER_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001876 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001877 ble_local_key_cb.is_er_rcvd = TRUE;
1878 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1879 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1880 BTIF_DM_LE_LOCAL_KEY_ER,
1881 BT_OCTET16_LEN);
1882 break;
1883
1884 case BTA_DM_BLE_AUTH_CMPL_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001885 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001886 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1887 break;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001888
1889 case BTA_DM_LE_FEATURES_READ:
1890 {
1891 tBTM_BLE_VSC_CB cmn_vsc_cb;
1892 bt_local_le_features_t local_le_features;
1893 char buf[512];
1894 bt_property_t prop;
1895 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1896 prop.val = (void*)buf;
1897 prop.len = sizeof(buf);
1898
1899 /* LE features are not stored in storage. Should be retrived from stack */
1900 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1901 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1902
1903 prop.len = sizeof (bt_local_le_features_t);
1904 if (cmn_vsc_cb.filter_support == 1)
1905 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1906 else
1907 local_le_features.max_adv_filter_supported = 0;
1908 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1909 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1910 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
Satya Callojid773c2c2014-07-29 22:08:55 -07001911 local_le_features.scan_result_storage_size_hibyte =
1912 (cmn_vsc_cb.tot_scan_results_strg >> 8) & (0xFF);
1913 local_le_features.scan_result_storage_size_lobyte =
1914 (cmn_vsc_cb.tot_scan_results_strg) & (0xFF);
Satya Callojiefaddcb2014-07-28 23:22:05 -07001915 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001916 memcpy(prop.val, &local_le_features, prop.len);
1917 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
1918 break;
1919 }
Satya Callojie5ba8842014-07-03 17:18:02 -07001920
1921 case BTA_DM_ENER_INFO_READ:
1922 {
1923 btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param;
1924 bt_activity_energy_info energy_info;
1925 energy_info.status = p_ener_data->status;
1926 energy_info.ctrl_state = p_ener_data->ctrl_state;
1927 energy_info.rx_time = p_ener_data->rx_time;
1928 energy_info.tx_time = p_ener_data->tx_time;
1929 energy_info.idle_time = p_ener_data->idle_time;
1930 energy_info.energy_used = p_ener_data->energy_used;
1931 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info);
1932 break;
1933 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001934#endif
1935
The Android Open Source Project5738f832012-12-12 16:00:35 -08001936 case BTA_DM_AUTHORIZE_EVT:
1937 case BTA_DM_SIG_STRENGTH_EVT:
1938 case BTA_DM_SP_RMT_OOB_EVT:
1939 case BTA_DM_SP_KEYPRESS_EVT:
1940 case BTA_DM_ROLE_CHG_EVT:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001941
The Android Open Source Project5738f832012-12-12 16:00:35 -08001942 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001943 BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001944 break;
1945 }
1946} /* btui_security_cback() */
1947
1948
1949/*******************************************************************************
1950**
1951** Function btif_dm_generic_evt
1952**
1953** Description Executes non-BTA upstream events in BTIF context
1954**
1955** Returns void
1956**
1957*******************************************************************************/
1958static void btif_dm_generic_evt(UINT16 event, char* p_param)
1959{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001960 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001961 switch(event)
1962 {
1963 case BTIF_DM_CB_DISCOVERY_STARTED:
1964 {
1965 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
1966 }
1967 break;
1968
1969 case BTIF_DM_CB_CREATE_BOND:
1970 {
Andre Eisenbach31a64002014-10-14 14:29:19 -07001971 pairing_cb.timeout_retries = NUM_TIMEOUT_RETRIES;
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07001972 btif_dm_create_bond_cb_t *create_bond_cb = (btif_dm_create_bond_cb_t*)p_param;
1973 btif_dm_cb_create_bond(&create_bond_cb->bdaddr, create_bond_cb->transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001974 }
1975 break;
1976
1977 case BTIF_DM_CB_REMOVE_BOND:
1978 {
1979 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
1980 }
1981 break;
1982
1983 case BTIF_DM_CB_HID_REMOTE_NAME:
1984 {
1985 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
1986 }
1987 break;
1988
1989 case BTIF_DM_CB_BOND_STATE_BONDING:
1990 {
1991 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
1992 }
1993 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001994 case BTIF_DM_CB_LE_TX_TEST:
1995 case BTIF_DM_CB_LE_RX_TEST:
1996 {
1997 uint8_t status;
1998 STREAM_TO_UINT8(status, p_param);
1999 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2000 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
2001 }
2002 break;
2003 case BTIF_DM_CB_LE_TEST_END:
2004 {
2005 uint8_t status;
2006 uint16_t count = 0;
2007 STREAM_TO_UINT8(status, p_param);
2008 if (status == 0)
2009 STREAM_TO_UINT16(count, p_param);
2010 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2011 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
2012 }
2013 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002014 default:
2015 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002016 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002017 }
2018 break;
2019 }
2020}
2021
2022/*******************************************************************************
2023**
2024** Function bte_dm_evt
2025**
2026** Description Switches context from BTE to BTIF for all DM events
2027**
2028** Returns void
2029**
2030*******************************************************************************/
2031
2032void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
2033{
2034 bt_status_t status;
2035
2036 /* switch context to btif task context (copy full union size for convenience) */
2037 status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event, (void*)p_data, sizeof(tBTA_DM_SEC), NULL);
2038
2039 /* catch any failed context transfers */
2040 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
2041}
2042
2043/*******************************************************************************
2044**
2045** Function bte_search_devices_evt
2046**
2047** Description Switches context from BTE to BTIF for DM search events
2048**
2049** Returns void
2050**
2051*******************************************************************************/
2052static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2053{
2054 UINT16 param_len = 0;
2055
2056 if (p_data)
2057 param_len += sizeof(tBTA_DM_SEARCH);
2058 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
2059 switch (event)
2060 {
2061 case BTA_DM_INQ_RES_EVT:
2062 {
2063 if (p_data->inq_res.p_eir)
2064 param_len += HCI_EXT_INQ_RESPONSE_LEN;
2065 }
2066 break;
2067
2068 case BTA_DM_DISC_RES_EVT:
2069 {
2070 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
2071 param_len += p_data->disc_res.raw_data_size;
2072 }
2073 break;
2074 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002075 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 -08002076
2077 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
2078 if (event == BTA_DM_INQ_RES_EVT)
2079 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
2080
2081 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
2082 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
2083}
2084
2085/*******************************************************************************
2086**
2087** Function bte_dm_search_services_evt
2088**
2089** Description Switches context from BTE to BTIF for DM search services
2090** event
2091**
2092** Returns void
2093**
2094*******************************************************************************/
2095static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2096{
2097 UINT16 param_len = 0;
2098 if (p_data)
2099 param_len += sizeof(tBTA_DM_SEARCH);
2100 switch (event)
2101 {
2102 case BTA_DM_DISC_RES_EVT:
2103 {
2104 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
2105 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
2106 }
2107 } break;
2108 }
2109 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
2110 * if raw_data is needed. */
2111 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
2112 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
2113}
2114
2115/*******************************************************************************
2116**
2117** Function bte_dm_remote_service_record_evt
2118**
2119** Description Switches context from BTE to BTIF for DM search service
2120** record event
2121**
2122** Returns void
2123**
2124*******************************************************************************/
2125static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2126{
2127 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
2128 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
2129}
2130
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002131#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07002132/*******************************************************************************
2133**
2134** Function bta_energy_info_cb
2135**
2136** Description Switches context from BTE to BTIF for DM energy info event
2137**
2138** Returns void
2139**
2140*******************************************************************************/
2141static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time,
2142 tBTA_DM_BLE_IDLE_TIME_MS idle_time,
2143 tBTA_DM_BLE_ENERGY_USED energy_used,
2144 tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status)
2145{
2146 BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld",
2147 status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
2148
2149 btif_activity_energy_info_cb_t btif_cb;
2150 btif_cb.status = status;
2151 btif_cb.ctrl_state = ctrl_state;
2152 btif_cb.tx_time = (uint64_t) tx_time;
2153 btif_cb.rx_time = (uint64_t) rx_time;
2154 btif_cb.idle_time =(uint64_t) idle_time;
2155 btif_cb.energy_used =(uint64_t) energy_used;
2156 btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
2157 (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL);
2158}
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002159#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07002160
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002161/*******************************************************************************
2162**
2163** Function bte_scan_filt_param_cfg_evt
2164**
2165** Description Scan filter param config event
2166**
2167** Returns void
2168**
2169*******************************************************************************/
2170static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
2171 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
2172 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
2173{
2174 /* This event occurs on calling BTA_DmBleCfgFilterCondition internally,
2175 ** and that is why there is no HAL callback
2176 */
2177 if(BTA_SUCCESS != status)
2178 {
2179 BTIF_TRACE_ERROR("%s, %d", __FUNCTION__, status);
2180 }
2181 else
2182 {
2183 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2184 }
2185}
2186
The Android Open Source Project5738f832012-12-12 16:00:35 -08002187/*****************************************************************************
2188**
2189** btif api functions (no context switch)
2190**
2191*****************************************************************************/
2192
2193/*******************************************************************************
2194**
2195** Function btif_dm_start_discovery
2196**
2197** Description Start device discovery/inquiry
2198**
2199** Returns bt_status_t
2200**
2201*******************************************************************************/
2202bt_status_t btif_dm_start_discovery(void)
2203{
2204 tBTA_DM_INQ inq_params;
2205 tBTA_SERVICE_MASK services = 0;
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002206 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002207
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002208 BTIF_TRACE_EVENT("%s", __FUNCTION__);
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002209
2210#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2211 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
2212 /* Cleanup anything remaining on index 0 */
2213 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
2214 bte_scan_filt_param_cfg_evt, 0);
2215
2216 /* Add an allow-all filter on index 0*/
2217 adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE;
2218 adv_filt_param.feat_seln = ALLOW_ALL_FILTER;
2219 adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;
2220 adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;
2221 adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE;
2222 adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE;
2223 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL,
2224 bte_scan_filt_param_cfg_evt, 0);
2225
The Android Open Source Project5738f832012-12-12 16:00:35 -08002226 /* TODO: Do we need to handle multiple inquiries at the same time? */
2227
2228 /* Set inquiry params and call API */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002229 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
Matthew Xie7f3e4292013-09-30 12:44:10 -07002230#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
2231 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
2232 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
2233 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
2234 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
2235#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002236#else
2237 inq_params.mode = BTA_DM_GENERAL_INQUIRY;
2238#endif
2239 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
2240
2241 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
2242 inq_params.report_dup = TRUE;
2243
2244 inq_params.filter_type = BTA_DM_INQ_CLR;
2245 /* TODO: Filter device by BDA needs to be implemented here */
2246
2247 /* Will be enabled to TRUE once inquiry busy level has been received */
2248 btif_dm_inquiry_in_progress = FALSE;
2249 /* find nearby devices */
2250 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
2251
2252 return BT_STATUS_SUCCESS;
2253}
2254
2255/*******************************************************************************
2256**
2257** Function btif_dm_cancel_discovery
2258**
2259** Description Cancels search
2260**
2261** Returns bt_status_t
2262**
2263*******************************************************************************/
2264bt_status_t btif_dm_cancel_discovery(void)
2265{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002266 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002267 BTA_DmSearchCancel();
2268 return BT_STATUS_SUCCESS;
2269}
2270
2271/*******************************************************************************
2272**
2273** Function btif_dm_create_bond
2274**
2275** Description Initiate bonding with the specified device
2276**
2277** Returns bt_status_t
2278**
2279*******************************************************************************/
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002280bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr, int transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002281{
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002282 btif_dm_create_bond_cb_t create_bond_cb;
2283 create_bond_cb.transport = transport;
2284 bdcpy(create_bond_cb.bdaddr.address, bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002285
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002286 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002287 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 -08002288 if (pairing_cb.state != BT_BOND_STATE_NONE)
2289 return BT_STATUS_BUSY;
2290
2291 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002292 (char *)&create_bond_cb, sizeof(btif_dm_create_bond_cb_t), NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002293
2294 return BT_STATUS_SUCCESS;
2295}
2296
2297/*******************************************************************************
2298**
2299** Function btif_dm_cancel_bond
2300**
2301** Description Initiate bonding with the specified device
2302**
2303** Returns bt_status_t
2304**
2305*******************************************************************************/
2306
2307bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
2308{
2309 bdstr_t bdstr;
2310
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002311 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 -08002312
2313 /* TODO:
2314 ** 1. Restore scan modes
2315 ** 2. special handling for HID devices
2316 */
2317 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2318 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002319
2320#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2321
2322 if (pairing_cb.is_ssp)
2323 {
2324 if (pairing_cb.is_le_only)
2325 {
2326 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2327 }
2328 else
Hemant Guptae1468692013-11-14 16:21:29 +05302329 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002330 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
Hemant Guptae1468692013-11-14 16:21:29 +05302331 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2332 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2333 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002334 }
2335 else
2336 {
2337 if (pairing_cb.is_le_only)
2338 {
2339 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2340 }
2341 else
2342 {
2343 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2344 }
2345 /* Cancel bonding, in case it is in ACL connection setup state */
2346 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2347 }
2348
2349#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002350 if (pairing_cb.is_ssp)
2351 {
2352 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2353 }
2354 else
2355 {
2356 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2357 }
2358 /* Cancel bonding, in case it is in ACL connection setup state */
2359 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2360 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002361#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002362 }
2363
2364 return BT_STATUS_SUCCESS;
2365}
2366
2367/*******************************************************************************
2368**
Kim Schulza9eb25c2013-09-30 10:55:52 +02002369** Function btif_dm_hh_open_failed
2370**
2371** Description informs the upper layers if the HH have failed during bonding
2372**
2373** Returns none
2374**
2375*******************************************************************************/
2376
2377void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
2378{
2379 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2380 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2381 {
2382 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2383 }
2384}
2385
2386/*******************************************************************************
2387**
The Android Open Source Project5738f832012-12-12 16:00:35 -08002388** Function btif_dm_remove_bond
2389**
2390** Description Removes bonding with the specified device
2391**
2392** Returns bt_status_t
2393**
2394*******************************************************************************/
2395
2396bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
2397{
2398 bdstr_t bdstr;
2399
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002400 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 -08002401 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2402 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2403
2404 return BT_STATUS_SUCCESS;
2405}
2406
2407/*******************************************************************************
2408**
2409** Function btif_dm_pin_reply
2410**
2411** Description BT legacy pairing - PIN code reply
2412**
2413** Returns bt_status_t
2414**
2415*******************************************************************************/
2416
2417bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2418 uint8_t pin_len, bt_pin_code_t *pin_code)
2419{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002420 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Hemant Gupta831423e2014-01-08 12:42:13 +05302421 if (pin_code == NULL)
2422 return BT_STATUS_FAIL;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002423#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002424
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002425 if (pairing_cb.is_le_only)
2426 {
2427 int i;
2428 UINT32 passkey = 0;
2429 int multi[] = {100000, 10000, 1000, 100, 10,1};
2430 BD_ADDR remote_bd_addr;
2431 bdcpy(remote_bd_addr, bd_addr->address);
2432 for (i = 0; i < 6; i++)
2433 {
2434 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2435 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002436 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002437 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2438
2439 }
2440 else
2441 {
2442 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2443 if (accept)
2444 pairing_cb.pin_code_len = pin_len;
2445 }
2446#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002447 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2448
2449 if (accept)
2450 pairing_cb.pin_code_len = pin_len;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002451#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002452 return BT_STATUS_SUCCESS;
2453}
2454
2455/*******************************************************************************
2456**
2457** Function btif_dm_ssp_reply
2458**
2459** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2460**
2461** Returns bt_status_t
2462**
2463*******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002464bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2465 bt_ssp_variant_t variant, uint8_t accept,
2466 uint32_t passkey)
2467{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08002468 UNUSED(passkey);
2469
The Android Open Source Project5738f832012-12-12 16:00:35 -08002470 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2471 {
2472 /* This is not implemented in the stack.
2473 * For devices with display, this is not needed
2474 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002475 BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002476 return BT_STATUS_FAIL;
2477 }
2478 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002479 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002480#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2481 if (pairing_cb.is_le_only)
2482 {
2483 if (accept)
2484 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2485 else
2486 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2487 }
2488 else
2489 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002490
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002491#else
2492 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2493#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002494 return BT_STATUS_SUCCESS;
2495}
2496
2497/*******************************************************************************
2498**
2499** Function btif_dm_get_adapter_property
2500**
2501** Description Queries the BTA for the adapter property
2502**
2503** Returns bt_status_t
2504**
2505*******************************************************************************/
2506bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2507{
2508 bt_status_t status;
2509
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002510 BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002511 switch (prop->type)
2512 {
2513 case BT_PROPERTY_BDNAME:
2514 {
2515 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
VenkatRaghavan VijayaRaghavan4540f592015-02-05 04:40:47 -08002516 strncpy((char *)bd_name->name, (char *)btif_get_default_local_name(),
VenkatRaghavan VijayaRaghavan1d8e6b82015-02-05 22:20:39 -08002517 sizeof(bd_name->name) - 1);
2518 bd_name->name[sizeof(bd_name->name) - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002519 prop->len = strlen((char *)bd_name->name);
2520 }
2521 break;
2522
2523 case BT_PROPERTY_ADAPTER_SCAN_MODE:
2524 {
2525 /* if the storage does not have it. Most likely app never set it. Default is NONE */
2526 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2527 *mode = BT_SCAN_MODE_NONE;
2528 prop->len = sizeof(bt_scan_mode_t);
2529 }
2530 break;
2531
2532 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2533 {
2534 uint32_t *tmt = (uint32_t*)prop->val;
2535 *tmt = 120; /* default to 120s, if not found in NV */
2536 prop->len = sizeof(uint32_t);
2537 }
2538 break;
2539
2540 default:
2541 prop->len = 0;
2542 return BT_STATUS_FAIL;
2543 }
2544 return BT_STATUS_SUCCESS;
2545}
2546
2547/*******************************************************************************
2548**
2549** Function btif_dm_get_remote_services
2550**
2551** Description Start SDP to get remote services
2552**
2553** Returns bt_status_t
2554**
2555*******************************************************************************/
2556bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2557{
2558 bdstr_t bdstr;
2559
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002560 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 -08002561
2562 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2563 bte_dm_search_services_evt, TRUE);
2564
2565 return BT_STATUS_SUCCESS;
2566}
2567
2568/*******************************************************************************
2569**
2570** Function btif_dm_get_remote_service_record
2571**
2572** Description Start SDP to get remote service record
2573**
2574**
2575** Returns bt_status_t
2576*******************************************************************************/
2577bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2578 bt_uuid_t *uuid)
2579{
2580 tSDP_UUID sdp_uuid;
2581 bdstr_t bdstr;
2582
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002583 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 -08002584
2585 sdp_uuid.len = MAX_UUID_SIZE;
2586 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2587
2588 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2589 bte_dm_remote_service_record_evt, TRUE);
2590
2591 return BT_STATUS_SUCCESS;
2592}
2593
2594void btif_dm_execute_service_request(UINT16 event, char *p_param)
2595{
2596 BOOLEAN b_enable = FALSE;
2597 bt_status_t status;
2598 if (event == BTIF_DM_ENABLE_SERVICE)
2599 {
2600 b_enable = TRUE;
2601 }
2602 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2603 if (status == BT_STATUS_SUCCESS)
2604 {
2605 bt_property_t property;
2606 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2607
2608 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2609 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2610 sizeof(local_uuids), local_uuids);
2611 btif_storage_get_adapter_property(&property);
2612 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2613 BT_STATUS_SUCCESS, 1, &property);
2614 }
2615 return;
2616}
2617
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002618void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2619 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2620{
2621 UINT8 yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2622 /* if local initiated:
2623 ** 1. set DD + MITM
2624 ** if remote initiated:
2625 ** 1. Copy over the auth_req from peer's io_rsp
2626 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2627 ** as a fallback set MITM+GB if peer had MITM set
2628 */
2629 UNUSED (bd_addr);
2630 UNUSED (p_io_cap);
2631 UNUSED (p_oob_data);
2632
2633
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002634 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002635 if(pairing_cb.is_local_initiated)
2636 {
2637 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2638 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2639 }
2640 else if (!is_orig)
2641 {
2642 /* peer initiated paring. They probably know what they want.
2643 ** Copy the mitm from peer device.
2644 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002645 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002646 __FUNCTION__, pairing_cb.auth_req);
2647 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2648
2649 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2650 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2651 *p_auth_req |= BTA_AUTH_SP_YES;
2652 }
2653 else if (yes_no_bit)
2654 {
2655 /* set the general bonding bit for stored device */
2656 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2657 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002658 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002659}
2660
2661void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2662 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2663{
2664 UNUSED (bd_addr);
2665 UNUSED (oob_data);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002666
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002667 if(auth_req & BTA_AUTH_BONDS)
2668 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002669 BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002670 pairing_cb.auth_req = auth_req;
2671 pairing_cb.io_cap = io_cap;
2672 }
2673}
2674
The Android Open Source Project5738f832012-12-12 16:00:35 -08002675#if (BTM_OOB_INCLUDED == TRUE)
2676void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data)
2677{
2678 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2679 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2680 {
2681 *p_oob_data = FALSE;
2682 }
2683 else
2684 {
2685 *p_oob_data = TRUE;
2686 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002687 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 -08002688}
2689#endif /* BTM_OOB_INCLUDED */
2690
2691#ifdef BTIF_DM_OOB_TEST
2692void btif_dm_load_local_oob(void)
2693{
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002694 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002695 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002696 BTIF_TRACE_DEBUG("btif_dm_load_local_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002697 if (prop_oob[0] != '3')
2698 {
2699#if (BTM_OOB_INCLUDED == TRUE)
2700 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2701 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2702 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002703 BTIF_TRACE_DEBUG("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002704 BTA_DmLocalOob();
2705 }
2706#else
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002707 BTIF_TRACE_ERROR("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002708#endif
2709 }
2710}
2711
2712void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2713{
2714 FILE *fp;
2715 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2716 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2717 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002718 char prop_oob[PROPERTY_VALUE_MAX];
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002719 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: valid=%d", valid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002720 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2721 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2722 valid)
2723 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002724 BTIF_TRACE_DEBUG("save local OOB data in memory");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002725 memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2726 memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2727 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002728 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002729 if (prop_oob[0] == '1')
2730 path = path_a;
2731 else if (prop_oob[0] == '2')
2732 path = path_b;
2733 if (path)
2734 {
2735 fp = fopen(path, "wb+");
2736 if (fp == NULL)
2737 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002738 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 -08002739 }
2740 else
2741 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002742 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 -08002743 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2744 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2745 fclose(fp);
2746 }
2747 }
2748 }
2749}
2750BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2751{
2752 char t[128];
2753 FILE *fp;
2754 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2755 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2756 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002757 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002758 BOOLEAN result = FALSE;
2759 bt_bdaddr_t bt_bd_addr;
2760 bdcpy(oob_cb.oob_bdaddr, bd_addr);
2761 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002762 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002763 if (prop_oob[0] == '1')
2764 path = path_b;
2765 else if (prop_oob[0] == '2')
2766 path = path_a;
2767 if (path)
2768 {
2769 fp = fopen(path, "rb");
2770 if (fp == NULL)
2771 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002772 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 -08002773 return FALSE;
2774 }
2775 else
2776 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002777 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob: read OOB data from %s",path);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002778 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2779 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2780 fclose(fp);
2781 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002782 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: TRUE");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002783 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2784 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2785 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002786 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002787 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2788 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
2789 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 -07002790 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: c = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002791 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2792 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
2793 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 -07002794 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: r = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002795 bdcpy(bt_bd_addr.address, bd_addr);
2796 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2797 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2798 result = TRUE;
2799 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002800 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob result=%d",result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002801 return result;
2802}
2803#endif /* BTIF_DM_OOB_TEST */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002804#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2805
2806static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2807{
2808 bt_bdaddr_t bd_addr;
2809 bt_bdname_t bd_name;
2810 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08002811 int dev_type;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002812
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002813 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002814
2815 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08002816 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
2817 {
2818 dev_type = BT_DEVICE_TYPE_BLE;
2819 }
2820 btif_dm_update_ble_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2821 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002822 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2823 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2824
2825 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2826 pairing_cb.is_ssp = FALSE;
2827 cod = COD_UNCLASSIFIED;
2828
2829 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2830 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2831 p_ssp_key_notif->passkey);
2832}
2833
2834/*******************************************************************************
2835**
2836** Function btif_dm_ble_auth_cmpl_evt
2837**
2838** Description Executes authentication complete event in btif context
2839**
2840** Returns void
2841**
2842*******************************************************************************/
2843static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2844{
2845 /* Save link key, if not temporary */
2846 bt_bdaddr_t bd_addr;
2847 bt_status_t status = BT_STATUS_FAIL;
2848 bt_bond_state_t state = BT_BOND_STATE_NONE;
2849
2850 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2851 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2852 {
2853 /* store keys */
2854 }
2855 if (p_auth_cmpl->success)
2856 {
2857 status = BT_STATUS_SUCCESS;
2858 state = BT_BOND_STATE_BONDED;
2859
2860 btif_dm_save_ble_bonding_keys();
2861 BTA_GATTC_Refresh(bd_addr.address);
2862 btif_dm_get_remote_services(&bd_addr);
2863 }
2864 else
2865 {
2866 /*Map the HCI fail reason to bt status */
2867 switch (p_auth_cmpl->fail_reason)
2868 {
Priti Aghera156c52b2014-07-09 14:58:19 -07002869 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
2870 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
2871 btif_dm_remove_ble_bonding_keys();
2872 status = BT_STATUS_AUTH_FAILURE;
2873 break;
2874 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
2875 status = BT_STATUS_AUTH_REJECTED;
2876 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002877 default:
Andre Eisenbachca22ac42013-02-13 17:02:11 +09002878 btif_dm_remove_ble_bonding_keys();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002879 status = BT_STATUS_FAIL;
2880 break;
2881 }
2882 }
2883 bond_state_changed(status, &bd_addr, state);
2884}
2885
2886
2887
2888void btif_dm_load_ble_local_keys(void)
2889{
2890 bt_status_t bt_status;
2891
2892 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2893
2894 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2895 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2896 {
2897 ble_local_key_cb.is_er_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002898 BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002899 }
2900
2901 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
2902 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
2903 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
2904 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
2905 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
2906 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
2907 {
2908 ble_local_key_cb.is_id_keys_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002909 BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002910 }
2911
2912}
2913void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
2914 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
2915{
2916 if (ble_local_key_cb.is_er_rcvd )
2917 {
2918 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2919 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2920 }
2921
2922 if (ble_local_key_cb.is_id_keys_rcvd)
2923 {
2924 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
2925 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
2926 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
2927 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
2928 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002929 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002930}
2931
2932void btif_dm_save_ble_bonding_keys(void)
2933{
2934
2935 bt_bdaddr_t bd_addr;
2936
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002937 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002938
2939 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2940
2941 if (pairing_cb.ble.is_penc_key_rcvd)
2942 {
2943 btif_storage_add_ble_bonding_key(&bd_addr,
2944 (char *) &pairing_cb.ble.penc_key,
2945 BTIF_DM_LE_KEY_PENC,
2946 sizeof(btif_dm_ble_penc_keys_t));
2947 }
2948
2949 if (pairing_cb.ble.is_pid_key_rcvd)
2950 {
2951 btif_storage_add_ble_bonding_key(&bd_addr,
Andre Eisenbach5e808462014-10-21 12:37:53 -07002952 (char *) &pairing_cb.ble.pid_key,
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002953 BTIF_DM_LE_KEY_PID,
Andre Eisenbach5e808462014-10-21 12:37:53 -07002954 sizeof(btif_dm_ble_pid_keys_t));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002955 }
2956
2957
2958 if (pairing_cb.ble.is_pcsrk_key_rcvd)
2959 {
2960 btif_storage_add_ble_bonding_key(&bd_addr,
2961 (char *) &pairing_cb.ble.pcsrk_key,
2962 BTIF_DM_LE_KEY_PCSRK,
2963 sizeof(btif_dm_ble_pcsrk_keys_t));
2964 }
2965
2966
2967 if (pairing_cb.ble.is_lenc_key_rcvd)
2968 {
2969 btif_storage_add_ble_bonding_key(&bd_addr,
2970 (char *) &pairing_cb.ble.lenc_key,
2971 BTIF_DM_LE_KEY_LENC,
2972 sizeof(btif_dm_ble_lenc_keys_t));
2973 }
2974
2975 if (pairing_cb.ble.is_lcsrk_key_rcvd)
2976 {
2977 btif_storage_add_ble_bonding_key(&bd_addr,
2978 (char *) &pairing_cb.ble.lcsrk_key,
2979 BTIF_DM_LE_KEY_LCSRK,
2980 sizeof(btif_dm_ble_lcsrk_keys_t));
2981 }
2982
2983}
2984
2985
2986void btif_dm_remove_ble_bonding_keys(void)
2987{
2988 bt_bdaddr_t bd_addr;
2989
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002990 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002991
2992 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2993 btif_storage_remove_ble_bonding_keys(&bd_addr);
2994}
2995
2996
2997/*******************************************************************************
2998**
2999** Function btif_dm_ble_sec_req_evt
3000**
3001** Description Eprocess security request event in btif context
3002**
3003** Returns void
3004**
3005*******************************************************************************/
3006void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
3007{
3008 bt_bdaddr_t bd_addr;
3009 bt_bdname_t bd_name;
3010 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08003011 int dev_type;
3012
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003013 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003014
3015 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3016 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003017 BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003018 return;
3019 }
3020
3021 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08003022 if (!btif_get_device_type(p_ble_req->bd_addr, &dev_type))
3023 {
3024 dev_type = BT_DEVICE_TYPE_BLE;
3025 }
3026 btif_dm_update_ble_remote_properties(p_ble_req->bd_addr, p_ble_req->bd_name,
3027 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003028
3029 bdcpy(bd_addr.address, p_ble_req->bd_addr);
3030 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
3031
3032 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3033
Andre Eisenbach89363762015-01-26 13:49:36 -08003034 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003035 pairing_cb.is_le_only = TRUE;
3036 pairing_cb.is_ssp = TRUE;
3037
3038 cod = COD_UNCLASSIFIED;
3039
3040 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
3041 BT_SSP_VARIANT_CONSENT, 0);
3042}
3043
3044
3045
3046/*******************************************************************************
3047**
3048** Function btif_dm_ble_passkey_req_evt
3049**
3050** Description Executes pin request event in btif context
3051**
3052** Returns void
3053**
3054*******************************************************************************/
3055static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
3056{
3057 bt_bdaddr_t bd_addr;
3058 bt_bdname_t bd_name;
3059 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08003060 int dev_type;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003061
3062 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08003063 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
3064 {
3065 dev_type = BT_DEVICE_TYPE_BLE;
3066 }
3067 btif_dm_update_ble_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,
3068 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003069
3070 bdcpy(bd_addr.address, p_pin_req->bd_addr);
3071 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
3072
3073 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3074 pairing_cb.is_le_only = TRUE;
3075
3076 cod = COD_UNCLASSIFIED;
3077
3078 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
3079 &bd_addr, &bd_name, cod);
3080}
3081
3082
3083void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
3084 tBT_DEVICE_TYPE dev_type)
3085{
3086 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
3087}
3088
3089static void btif_dm_ble_tx_test_cback(void *p)
3090{
3091 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
3092 (char *)p, 1, NULL);
3093}
3094
3095static void btif_dm_ble_rx_test_cback(void *p)
3096{
3097 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
3098 (char *)p, 1, NULL);
3099}
3100
3101static void btif_dm_ble_test_end_cback(void *p)
3102{
3103 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
3104 (char *)p, 3, NULL);
3105}
3106/*******************************************************************************
3107**
3108** Function btif_le_test_mode
3109**
3110** Description Sends a HCI BLE Test command to the Controller
3111**
3112** Returns BT_STATUS_SUCCESS on success
3113**
3114*******************************************************************************/
3115bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
3116{
3117 switch (opcode) {
3118 case HCI_BLE_TRANSMITTER_TEST:
3119 if (len != 3) return BT_STATUS_PARM_INVALID;
3120 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
3121 break;
3122 case HCI_BLE_RECEIVER_TEST:
3123 if (len != 1) return BT_STATUS_PARM_INVALID;
3124 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
3125 break;
3126 case HCI_BLE_TEST_END:
3127 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
3128 break;
3129 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003130 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003131 return BT_STATUS_UNSUPPORTED;
3132 }
3133 return BT_STATUS_SUCCESS;
3134}
3135
3136#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08003137
3138void btif_dm_on_disable()
3139{
3140 /* cancel any pending pairing requests */
3141 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3142 {
3143 bt_bdaddr_t bd_addr;
3144
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003145 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003146 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3147 btif_dm_cancel_bond(&bd_addr);
3148 }
3149}
Matthew Xie1e5109b2012-11-09 18:26:26 -08003150
Satya Callojie5ba8842014-07-03 17:18:02 -07003151/*******************************************************************************
3152**
3153** Function btif_dm_read_energy_info
3154**
3155** Description Reads the energy info from controller
3156**
3157** Returns void
3158**
3159*******************************************************************************/
3160void btif_dm_read_energy_info()
3161{
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003162#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07003163 BTA_DmBleGetEnergyInfo(bta_energy_info_cb);
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003164#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07003165}
3166
Matthew Xie1e5109b2012-11-09 18:26:26 -08003167static char* btif_get_default_local_name() {
3168 if (btif_default_local_name[0] == '\0')
3169 {
3170 int max_len = sizeof(btif_default_local_name) - 1;
3171 if (BTM_DEF_LOCAL_NAME[0] != '\0')
3172 {
3173 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
3174 }
3175 else
3176 {
3177 char prop_model[PROPERTY_VALUE_MAX];
3178 property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
3179 strncpy(btif_default_local_name, prop_model, max_len);
3180 }
3181 btif_default_local_name[max_len] = '\0';
3182 }
3183 return btif_default_local_name;
3184}