blob: 42e5885583c0bc244387b8a6f971a75dd7cc4bfc [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 ***********************************************************************************/
27#include <stdio.h>
28#include <stdlib.h>
29#include <unistd.h>
30
31#include <hardware/bluetooth.h>
32
33#include <utils/Log.h>
34#include <cutils/properties.h>
35#include "gki.h"
36#include "btu.h"
37#include "bd.h"
38#include "bta_api.h"
39#include "btif_api.h"
40#include "btif_util.h"
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080041#include "btif_dm.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080042#include "btif_storage.h"
43#include "btif_hh.h"
44#include "btif_config.h"
45
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080046#include "bta_gatt_api.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080047/******************************************************************************
48** Constants & Macros
49******************************************************************************/
50
51#define COD_UNCLASSIFIED ((0x1F) << 8)
Priti Agheraebb1d752012-11-27 18:03:22 -080052#define COD_HID_KEYBOARD 0x0540
53#define COD_HID_POINTING 0x0580
54#define COD_HID_COMBO 0x05C0
55#define COD_HID_MAJOR 0x0500
56#define COD_AV_HEADSETS 0x0404
57#define COD_AV_HANDSFREE 0x0408
58#define COD_AV_HEADPHONES 0x0418
59#define COD_AV_PORTABLE_AUDIO 0x041C
60#define COD_AV_HIFI_AUDIO 0x0428
The Android Open Source Project5738f832012-12-12 16:00:35 -080061
62
63#define BTIF_DM_DEFAULT_INQ_MAX_RESULTS 0
64#define BTIF_DM_DEFAULT_INQ_MAX_DURATION 10
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -070065#define BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING 2
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080066
Matthew Xie1e5109b2012-11-09 18:26:26 -080067#define PROPERTY_PRODUCT_MODEL "ro.product.model"
Matthew Xiea30d95a2013-09-18 12:30:36 -070068#define DEFAULT_LOCAL_NAME_MAX 31
Matthew Xie1e5109b2012-11-09 18:26:26 -080069#if (DEFAULT_LOCAL_NAME_MAX > BTM_MAX_LOC_BD_NAME_LEN)
70 #error "default btif local name size exceeds stack supported length"
71#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080072
Matthew Xie7f3e4292013-09-30 12:44:10 -070073#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
74#define BTIF_DM_INTERLEAVE_DURATION_BR_ONE 2
75#define BTIF_DM_INTERLEAVE_DURATION_LE_ONE 2
76#define BTIF_DM_INTERLEAVE_DURATION_BR_TWO 3
77#define BTIF_DM_INTERLEAVE_DURATION_LE_TWO 4
78#endif
79
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080080typedef struct
81{
The Android Open Source Project5738f832012-12-12 16:00:35 -080082 bt_bond_state_t state;
83 BD_ADDR bd_addr;
84 UINT8 is_temp;
85 UINT8 pin_code_len;
86 UINT8 is_ssp;
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -070087 UINT8 auth_req;
88 UINT8 io_cap;
The Android Open Source Project5738f832012-12-12 16:00:35 -080089 UINT8 autopair_attempts;
90 UINT8 is_local_initiated;
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -070091 UINT8 sdp_attempts;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080092#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
93 BOOLEAN is_le_only;
94 btif_dm_ble_cb_t ble;
95#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -080096} btif_dm_pairing_cb_t;
97
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080098
99typedef struct
100{
101 UINT8 ir[BT_OCTET16_LEN];
102 UINT8 irk[BT_OCTET16_LEN];
103 UINT8 dhk[BT_OCTET16_LEN];
104}btif_dm_local_key_id_t;
105
106typedef struct
107{
108 BOOLEAN is_er_rcvd;
109 UINT8 er[BT_OCTET16_LEN];
110 BOOLEAN is_id_keys_rcvd;
111 btif_dm_local_key_id_t id_keys; /* ID kyes */
112
113}btif_dm_local_key_cb_t;
114
115typedef struct
116{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800117 BD_ADDR bd_addr;
118 BD_NAME bd_name;
119} btif_dm_remote_name_t;
120
121typedef struct
122{
123 BT_OCTET16 sp_c;
124 BT_OCTET16 sp_r;
125 BD_ADDR oob_bdaddr; /* peer bdaddr*/
126} btif_dm_oob_cb_t;
Satya Callojie5ba8842014-07-03 17:18:02 -0700127
128typedef struct
129{
130 uint8_t status;
131 uint8_t ctrl_state;
132 uint64_t tx_time;
133 uint64_t rx_time;
134 uint64_t idle_time;
135 uint64_t energy_used;
136} btif_activity_energy_info_cb_t;
137
The Android Open Source Project5738f832012-12-12 16:00:35 -0800138#define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id))
139
140/* This flag will be true if HCI_Inquiry is in progress */
141static BOOLEAN btif_dm_inquiry_in_progress = FALSE;
142
Matthew Xie1e5109b2012-11-09 18:26:26 -0800143/************************************************************************************
144** Static variables
145************************************************************************************/
146static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'};
147
The Android Open Source Project5738f832012-12-12 16:00:35 -0800148/******************************************************************************
149** Static functions
150******************************************************************************/
151static btif_dm_pairing_cb_t pairing_cb;
152static btif_dm_oob_cb_t oob_cb;
153static void btif_dm_generic_evt(UINT16 event, char* p_param);
154static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr);
155static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name);
156static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
157 DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800158#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
159static btif_dm_local_key_cb_t ble_local_key_cb;
160static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif);
161static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl);
162static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req);
163#endif
Satya Calloji6e2d9db2014-07-08 16:18:58 -0700164
165static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
166 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
167 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status);
168
Matthew Xie1e5109b2012-11-09 18:26:26 -0800169static char* btif_get_default_local_name();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800170/******************************************************************************
171** Externs
172******************************************************************************/
173extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
174extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
175extern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
176extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
Hemant Gupta10256872013-08-19 18:33:01 +0530177extern bt_status_t btif_hf_client_execute_service(BOOLEAN b_enable);
Hemant Gupta2dc99992014-04-18 12:54:08 +0530178extern bt_status_t btif_mce_execute_service(BOOLEAN b_enable);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800179extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700180extern 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 -0800181
182
183/******************************************************************************
184** Functions
185******************************************************************************/
186
187bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
188 BOOLEAN b_enable)
189{
190 /* Check the service_ID and invoke the profile's BT state changed API */
191 switch (service_id)
192 {
193 case BTA_HFP_SERVICE_ID:
194 case BTA_HSP_SERVICE_ID:
195 {
196 btif_hf_execute_service(b_enable);
197 }break;
198 case BTA_A2DP_SERVICE_ID:
199 {
200 btif_av_execute_service(b_enable);
201 }break;
202 case BTA_HID_SERVICE_ID:
203 {
204 btif_hh_execute_service(b_enable);
205 }break;
Hemant Gupta10256872013-08-19 18:33:01 +0530206 case BTA_HFP_HS_SERVICE_ID:
207 {
208 btif_hf_client_execute_service(b_enable);
209 }break;
Hemant Gupta2dc99992014-04-18 12:54:08 +0530210 case BTA_MAP_SERVICE_ID:
211 {
212 btif_mce_execute_service(b_enable);
213 }break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800214 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700215 BTIF_TRACE_ERROR("%s: Unknown service being enabled", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800216 return BT_STATUS_FAIL;
217 }
218 return BT_STATUS_SUCCESS;
219}
220
221/*******************************************************************************
222**
223** Function check_eir_remote_name
224**
225** Description Check if remote name is in the EIR data
226**
227** Returns TRUE if remote name found
228** Populate p_remote_name, if provided and remote name found
229**
230*******************************************************************************/
231static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
232 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
233{
234 UINT8 *p_eir_remote_name = NULL;
235 UINT8 remote_name_len = 0;
236
237 /* Check EIR for remote name and services */
238 if (p_search_data->inq_res.p_eir)
239 {
240 p_eir_remote_name = BTA_CheckEirData(p_search_data->inq_res.p_eir,
241 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
242 if (!p_eir_remote_name)
243 {
244 p_eir_remote_name = BTA_CheckEirData(p_search_data->inq_res.p_eir,
245 BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
246 }
247
248 if (p_eir_remote_name)
249 {
250 if (remote_name_len > BD_NAME_LEN)
251 remote_name_len = BD_NAME_LEN;
252
253 if (p_remote_name && p_remote_name_len)
254 {
255 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
256 *(p_remote_name + remote_name_len) = 0;
257 *p_remote_name_len = remote_name_len;
258 }
259
260 return TRUE;
261 }
262 }
263
264 return FALSE;
265
266}
267
268/*******************************************************************************
269**
270** Function check_cached_remote_name
271**
272** Description Check if remote name is in the NVRAM cache
273**
274** Returns TRUE if remote name found
275** Populate p_remote_name, if provided and remote name found
276**
277*******************************************************************************/
278static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
279 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
280{
281 bt_bdname_t bdname;
282 bt_bdaddr_t remote_bdaddr;
283 bt_property_t prop_name;
284
285 /* check if we already have it in our btif_storage cache */
286 bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
287 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
288 sizeof(bt_bdname_t), &bdname);
289 if (btif_storage_get_remote_device_property(
290 &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
291 {
292 if (p_remote_name && p_remote_name_len)
293 {
294 strcpy((char *)p_remote_name, (char *)bdname.name);
295 *p_remote_name_len = strlen((char *)p_remote_name);
296 }
297 return TRUE;
298 }
299
300 return FALSE;
301}
302
303BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
304{
305 uint32_t remote_cod;
306 bt_property_t prop_name;
307
308 /* check if we already have it in our btif_storage cache */
309 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
310 sizeof(uint32_t), &remote_cod);
311 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
312 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700313 BTIF_TRACE_ERROR("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800314 if ((remote_cod & 0x7ff) == cod)
315 return TRUE;
316 }
317
318 return FALSE;
319}
320
Priti Agheraebb1d752012-11-27 18:03:22 -0800321BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
322{
323 uint32_t remote_cod;
324 bt_property_t prop_name;
325
326 /* check if we already have it in our btif_storage cache */
327 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
328 sizeof(uint32_t), &remote_cod);
329 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
330 &prop_name) == BT_STATUS_SUCCESS)
331 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700332 BTIF_TRACE_DEBUG("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
Priti Agheraebb1d752012-11-27 18:03:22 -0800333 if ((remote_cod & 0x700) == cod)
334 return TRUE;
335 }
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700336 return FALSE;
337}
Priti Agheraebb1d752012-11-27 18:03:22 -0800338
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700339BOOLEAN check_hid_le(const bt_bdaddr_t *remote_bdaddr)
340{
341 uint32_t remote_dev_type;
342 bt_property_t prop_name;
343
344 /* check if we already have it in our btif_storage cache */
345 BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE,
346 sizeof(uint32_t), &remote_dev_type);
347 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
348 &prop_name) == BT_STATUS_SUCCESS)
349 {
350 if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE)
351 {
352 bdstr_t bdstr;
353 bd2str(remote_bdaddr, &bdstr);
354 if(btif_config_exist("Remote", bdstr, "HidAppId"))
355 return TRUE;
356 }
357 }
Priti Agheraebb1d752012-11-27 18:03:22 -0800358 return FALSE;
359}
360
The Android Open Source Project5738f832012-12-12 16:00:35 -0800361static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
362{
363 /* Send bonding state only once - based on outgoing/incoming we may receive duplicates */
364 if ( (pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING) )
365 return;
366
367 if (pairing_cb.is_temp)
368 {
369 state = BT_BOND_STATE_NONE;
370 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700371 BTIF_TRACE_DEBUG("%s: state=%d prev_state=%d", __FUNCTION__, state, pairing_cb.state);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800372
373 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
374
375 if (state == BT_BOND_STATE_BONDING)
376 {
377 pairing_cb.state = state;
378 bdcpy(pairing_cb.bd_addr, bd_addr->address);
379 }
380 else
381 {
382 memset(&pairing_cb, 0, sizeof(pairing_cb));
383 }
384
385}
386
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800387/* store remote version in bt config to always have access
388 to it post pairing*/
389static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
390{
391 bt_property_t property;
392 UINT8 lmp_ver = 0;
393 UINT16 lmp_subver = 0;
394 UINT16 mfct_set = 0;
395 tBTM_STATUS btm_status;
396 bt_remote_version_t info;
397 bt_status_t status;
398 bdstr_t bdstr;
399
400 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
401 &mfct_set, &lmp_subver);
402
403 ALOGD("remote version info [%s]: %x, %x, %x", bd2str(p_bd, &bdstr),
404 lmp_ver, mfct_set, lmp_subver);
405
406 if (btm_status == BTM_SUCCESS)
407 {
408 /* always update cache to ensure we have availability whenever BTM API
409 is not populated */
410 info.manufacturer = mfct_set;
411 info.sub_ver = lmp_subver;
412 info.version = lmp_ver;
413 BTIF_STORAGE_FILL_PROPERTY(&property,
414 BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
415 &info);
416 status = btif_storage_set_remote_device_property(p_bd, &property);
417 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
418 }
419}
420
The Android Open Source Project5738f832012-12-12 16:00:35 -0800421
422static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
423 DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
424{
425 int num_properties = 0;
426 bt_property_t properties[3];
427 bt_bdaddr_t bdaddr;
428 bt_status_t status;
429 UINT32 cod;
430 bt_device_type_t dev_type;
431
432 memset(properties, 0, sizeof(properties));
433 bdcpy(bdaddr.address, bd_addr);
434
435 /* remote name */
436 if (strlen((const char *) bd_name))
437 {
438 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
439 BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
440 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
441 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
442 num_properties++;
443 }
444
445 /* class of device */
446 cod = devclass2uint(dev_class);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700447 BTIF_TRACE_DEBUG("%s():cod is 0x%06x", __FUNCTION__, cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800448 if ( cod == 0) {
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530449 /* Try to retrieve cod from storage */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700450 BTIF_TRACE_DEBUG("%s():cod is 0, checking cod from storage", __FUNCTION__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530451 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
452 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
453 status = btif_storage_get_remote_device_property(&bdaddr, &properties[num_properties]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700454 BTIF_TRACE_DEBUG("%s():cod retreived from storage is 0x%06x", __FUNCTION__, cod);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530455 if ( cod == 0) {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700456 BTIF_TRACE_DEBUG("%s():cod is again 0, set as unclassified", __FUNCTION__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530457 cod = COD_UNCLASSIFIED;
458 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800459 }
460
461 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
462 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
463 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
464 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
465 num_properties++;
466
467 /* device type */
468 dev_type = device_type;
469 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
470 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
471 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
472 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
473 num_properties++;
474
475 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
476 status, &bdaddr, num_properties, properties);
477}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800478
479/*******************************************************************************
480**
481** Function btif_dm_cb_hid_remote_name
482**
483** Description Remote name callback for HID device. Called in btif context
484** Special handling for HID devices
485**
486** Returns void
487**
488*******************************************************************************/
489static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
490{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700491 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 -0800492 if (pairing_cb.state == BT_BOND_STATE_BONDING)
493 {
494 bt_bdaddr_t remote_bd;
495
496 bdcpy(remote_bd.address, pairing_cb.bd_addr);
497
498 if (p_remote_name->status == BTM_SUCCESS)
499 {
500 bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
501 }
502 else
503 bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
504 }
505}
506
The Android Open Source Project5738f832012-12-12 16:00:35 -0800507/*******************************************************************************
508**
509** Function btif_dm_cb_create_bond
510**
511** Description Create bond initiated from the BTIF thread context
512** Special handling for HID devices
513**
514** Returns void
515**
516*******************************************************************************/
517static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr)
518{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800519 BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800520 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800521
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800522#if BLE_INCLUDED == TRUE
523 int device_type;
524 int addr_type;
525 bdstr_t bdstr;
526 bd2str(bd_addr, &bdstr);
527 if(btif_config_get_int("Remote", (char const *)&bdstr,"DevType", &device_type) &&
528 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
529 (device_type == BT_DEVICE_TYPE_BLE))
530 {
531 BTA_DmAddBleDevice(bd_addr->address, addr_type, BT_DEVICE_TYPE_BLE);
532 }
533#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800534
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800535#if BLE_INCLUDED == TRUE
536 if(is_hid && device_type != BT_DEVICE_TYPE_BLE)
537#else
538 if(is_hid)
539#endif
540 {
541 int status;
542 status = btif_hh_connect(bd_addr);
543 if(status != BT_STATUS_SUCCESS)
544 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800545 }
546 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800547 {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800548 BTA_DmBond ((UINT8 *)bd_addr->address);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800549 }
550 /* Track originator of bond creation */
551 pairing_cb.is_local_initiated = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800552
553}
554
555/*******************************************************************************
556**
557** Function btif_dm_cb_remove_bond
558**
559** Description remove bond initiated from the BTIF thread context
560** Special handling for HID devices
561**
562** Returns void
563**
564*******************************************************************************/
565void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
566{
567 bdstr_t bdstr;
568 /*special handling for HID devices */
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700569 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there
570 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
571#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
572 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
573#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800574 {
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700575 BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800576 }
577}
578
579/*******************************************************************************
580**
Andre Eisenbach249f6002014-06-18 12:20:37 -0700581** Function btif_dm_get_connection_state
582**
583** Description Returns whether the remote device is currently connected
584**
585** Returns 0 if not connected
586**
587*******************************************************************************/
588uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr)
589{
590 return BTA_DmGetConnectionState((UINT8 *)bd_addr->address);
591}
592
593/*******************************************************************************
594**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800595** Function search_devices_copy_cb
596**
597** Description Deep copy callback for search devices event
598**
599** Returns void
600**
601*******************************************************************************/
602static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
603{
604 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
605 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
606
607 if (!p_src)
608 return;
609
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700610 BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800611 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
612 switch (event)
613 {
614 case BTA_DM_INQ_RES_EVT:
615 {
616 if (p_src_data->inq_res.p_eir)
617 {
618 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
619 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
620 }
621 }
622 break;
623
624 case BTA_DM_DISC_RES_EVT:
625 {
626 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
627 {
628 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
629 memcpy(p_dest_data->disc_res.p_raw_data,
630 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
631 }
632 }
633 break;
634 }
635}
636
637static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
638{
639 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
640 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
641
642 if (!p_src)
643 return;
644 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
645 switch (event)
646 {
647 case BTA_DM_DISC_RES_EVT:
648 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530649 if (p_src_data->disc_res.result == BTA_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800650 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530651 if (p_src_data->disc_res.num_uuids > 0)
652 {
653 p_dest_data->disc_res.p_uuid_list =
654 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
655 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
656 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
657 GKI_freebuf(p_src_data->disc_res.p_uuid_list);
658 }
659 if (p_src_data->disc_res.p_raw_data != NULL)
660 {
661 GKI_freebuf(p_src_data->disc_res.p_raw_data);
662 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800663 }
664 } break;
665 }
666}
667/******************************************************************************
668**
669** BTIF DM callback events
670**
671*****************************************************************************/
672
673/*******************************************************************************
674**
675** Function btif_dm_pin_req_evt
676**
677** Description Executes pin request event in btif context
678**
679** Returns void
680**
681*******************************************************************************/
682static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
683{
684 bt_bdaddr_t bd_addr;
685 bt_bdname_t bd_name;
686 UINT32 cod;
687 bt_pin_code_t pin_code;
688
689 /* Remote properties update */
690 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
691 p_pin_req->dev_class, BT_DEVICE_TYPE_BREDR);
692
693 bdcpy(bd_addr.address, p_pin_req->bd_addr);
694 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
695
696 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
697
698 cod = devclass2uint(p_pin_req->dev_class);
699
700 if ( cod == 0) {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700701 BTIF_TRACE_DEBUG("%s():cod is 0, set as unclassified", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800702 cod = COD_UNCLASSIFIED;
703 }
704
705 /* check for auto pair possiblity only if bond was initiated by local device */
706 if (pairing_cb.is_local_initiated)
707 {
708 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
709 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
710 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
711 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
712 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
713 check_cod(&bd_addr, COD_HID_POINTING))
714 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700715 BTIF_TRACE_DEBUG("%s()cod matches for auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800716 /* Check if this device can be auto paired */
717 if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) &&
718 (pairing_cb.autopair_attempts == 0))
719 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700720 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800721 pin_code.pin[0] = 0x30;
722 pin_code.pin[1] = 0x30;
723 pin_code.pin[2] = 0x30;
724 pin_code.pin[3] = 0x30;
725
726 pairing_cb.autopair_attempts++;
727 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
728 return;
729 }
730 }
731 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
732 check_cod(&bd_addr, COD_HID_COMBO))
733 {
734 if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) &&
735 (pairing_cb.autopair_attempts == 0))
736 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700737 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800738 pin_code.pin[0] = 0x30;
739 pin_code.pin[1] = 0x30;
740 pin_code.pin[2] = 0x30;
741 pin_code.pin[3] = 0x30;
742
743 pairing_cb.autopair_attempts++;
744 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
745 return;
746 }
747 }
748 }
749 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
750 &bd_addr, &bd_name, cod);
751}
752
753/*******************************************************************************
754**
755** Function btif_dm_ssp_cfm_req_evt
756**
757** Description Executes SSP confirm request event in btif context
758**
759** Returns void
760**
761*******************************************************************************/
762static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
763{
764 bt_bdaddr_t bd_addr;
765 bt_bdname_t bd_name;
766 UINT32 cod;
767 BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
768
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700769 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800770
771 /* Remote properties update */
772 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
773 p_ssp_cfm_req->dev_class, BT_DEVICE_TYPE_BREDR);
774
775 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
776 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
777
778 /* Set the pairing_cb based on the local & remote authentication requirements */
779 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
780
781 /* if just_works and bonding bit is not set treat this as temporary */
782 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 -0800783 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
784 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800785 pairing_cb.is_temp = TRUE;
786 else
787 pairing_cb.is_temp = FALSE;
788
789 pairing_cb.is_ssp = TRUE;
790
791 /* If JustWorks auto-accept */
792 if (p_ssp_cfm_req->just_works)
793 {
794 /* Pairing consent for JustWorks needed if:
795 * 1. Incoming pairing is detected AND
796 * 2. local IO capabilities are DisplayYesNo AND
797 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
798 */
799 if ((is_incoming) && ((p_ssp_cfm_req->loc_io_caps == 0x01) &&
800 (p_ssp_cfm_req->rmt_io_caps == 0x00 || p_ssp_cfm_req->rmt_io_caps == 0x03)))
801 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700802 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 -0800803 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
804 }
805 else
806 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700807 BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800808 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
809 return;
810 }
811 }
812
813 cod = devclass2uint(p_ssp_cfm_req->dev_class);
814
815 if ( cod == 0) {
816 ALOGD("cod is 0, set as unclassified");
817 cod = COD_UNCLASSIFIED;
818 }
819
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700820 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800821 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
822 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
823 p_ssp_cfm_req->num_val);
824}
825
826static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
827{
828 bt_bdaddr_t bd_addr;
829 bt_bdname_t bd_name;
830 UINT32 cod;
831
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700832 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800833
834 /* Remote properties update */
835 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
836 p_ssp_key_notif->dev_class, BT_DEVICE_TYPE_BREDR);
837
838 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
839 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
840
841 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
842 pairing_cb.is_ssp = TRUE;
843 cod = devclass2uint(p_ssp_key_notif->dev_class);
844
845 if ( cod == 0) {
846 ALOGD("cod is 0, set as unclassified");
847 cod = COD_UNCLASSIFIED;
848 }
849
850 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
851 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
852 p_ssp_key_notif->passkey);
853}
854/*******************************************************************************
855**
856** Function btif_dm_auth_cmpl_evt
857**
858** Description Executes authentication complete event in btif context
859**
860** Returns void
861**
862*******************************************************************************/
863static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
864{
865 /* Save link key, if not temporary */
866 bt_bdaddr_t bd_addr;
867 bt_status_t status = BT_STATUS_FAIL;
868 bt_bond_state_t state = BT_BOND_STATE_NONE;
869
870 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
871 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
872 {
873 if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) || (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
874 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) || (!pairing_cb.is_temp))
875 {
876 bt_status_t ret;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700877 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, is_temp=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800878 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.is_temp);
879 ret = btif_storage_add_bonded_device(&bd_addr,
880 p_auth_cmpl->key, p_auth_cmpl->key_type,
881 pairing_cb.pin_code_len);
882 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
883 }
884 else
885 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700886 BTIF_TRACE_DEBUG("%s: Temporary key. Not storing. key_type=0x%x, is_temp=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800887 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.is_temp);
Hemant Guptab820aec2013-12-24 19:59:57 +0530888 if(pairing_cb.is_temp)
889 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700890 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
Hemant Guptab820aec2013-12-24 19:59:57 +0530891 __FUNCTION__);
892 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
893 return;
894 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800895 }
896 }
897 if (p_auth_cmpl->success)
898 {
899 status = BT_STATUS_SUCCESS;
900 state = BT_BOND_STATE_BONDED;
901
902 /* Trigger SDP on the device */
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700903 pairing_cb.sdp_attempts = 1;;
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -0800904
905 if(btif_dm_inquiry_in_progress)
906 btif_dm_cancel_discovery();
907
The Android Open Source Project5738f832012-12-12 16:00:35 -0800908 btif_dm_get_remote_services(&bd_addr);
909 /* Do not call bond_state_changed_cb yet. Wait till fetch remote service is complete */
910 }
911 else
912 {
913 /*Map the HCI fail reason to bt status */
914 switch(p_auth_cmpl->fail_reason)
915 {
916 case HCI_ERR_PAGE_TIMEOUT:
917 case HCI_ERR_CONNECTION_TOUT:
918 status = BT_STATUS_RMT_DEV_DOWN;
919 break;
920
Hemant Guptaaef7a672013-07-31 19:00:12 +0530921 case HCI_ERR_PAIRING_NOT_ALLOWED:
922 status = BT_STATUS_AUTH_REJECTED;
923 break;
924
Hemant Guptab4801442014-01-07 18:11:15 +0530925 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
926 status = BT_STATUS_AUTH_FAILURE;
927 break;
928
The Android Open Source Project5738f832012-12-12 16:00:35 -0800929 /* map the auth failure codes, so we can retry pairing if necessary */
930 case HCI_ERR_AUTH_FAILURE:
Hemant Gupta59a88ec2014-03-19 19:01:35 +0530931 case HCI_ERR_KEY_MISSING:
Zhihai Xua7ea8092013-11-27 14:10:53 +0530932 btif_storage_remove_bonded_device(&bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800933 case HCI_ERR_HOST_REJECT_SECURITY:
934 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
935 case HCI_ERR_UNIT_KEY_USED:
936 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
937 case HCI_ERR_INSUFFCIENT_SECURITY:
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530938 case HCI_ERR_PEER_USER:
939 case HCI_ERR_UNSPECIFIED:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700940 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d",
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530941 __FUNCTION__, p_auth_cmpl->fail_reason);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800942 if (pairing_cb.autopair_attempts == 1)
943 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700944 BTIF_TRACE_DEBUG("%s(): Adding device to blacklist ", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800945
946 /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class */
947 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
948 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
949 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
950 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
951 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
952 check_cod(&bd_addr, COD_HID_POINTING))
953 {
954 btif_storage_add_device_to_autopair_blacklist (&bd_addr);
955 }
956 pairing_cb.autopair_attempts++;
957
958 /* Create the Bond once again */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700959 BTIF_TRACE_DEBUG("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800960 btif_dm_cb_create_bond (&bd_addr);
961 return;
962 }
963 else
964 {
965 /* if autopair attempts are more than 1, or not attempted */
966 status = BT_STATUS_AUTH_FAILURE;
967 }
968 break;
969
970 default:
971 status = BT_STATUS_FAIL;
972 }
Zhihai Xu8d2128d2013-12-13 16:09:21 +0530973 /* Special Handling for HID Devices */
974 if (check_cod(&bd_addr, COD_HID_POINTING)) {
975 /* Remove Device as bonded in nvram as authentication failed */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700976 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __FUNCTION__);
Zhihai Xu8d2128d2013-12-13 16:09:21 +0530977 btif_storage_remove_bonded_device(&bd_addr);
978 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800979 bond_state_changed(status, &bd_addr, state);
980 }
981}
982
983/******************************************************************************
984**
985** Function btif_dm_search_devices_evt
986**
987** Description Executes search devices callback events in btif context
988**
989** Returns void
990**
991******************************************************************************/
992static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
993{
994 tBTA_DM_SEARCH *p_search_data;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700995 BTIF_TRACE_EVENT("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800996
997 switch (event)
998 {
999 case BTA_DM_DISC_RES_EVT:
1000 {
1001 p_search_data = (tBTA_DM_SEARCH *)p_param;
1002 /* Remote name update */
1003 if (strlen((const char *) p_search_data->disc_res.bd_name))
1004 {
1005 bt_property_t properties[1];
1006 bt_bdaddr_t bdaddr;
1007 bt_status_t status;
1008
1009 properties[0].type = BT_PROPERTY_BDNAME;
1010 properties[0].val = p_search_data->disc_res.bd_name;
1011 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
1012 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
1013
1014 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
1015 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
1016 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1017 status, &bdaddr, 1, properties);
1018 }
1019 /* TODO: Services? */
1020 }
1021 break;
1022
1023 case BTA_DM_INQ_RES_EVT:
1024 {
1025 /* inquiry result */
1026 UINT32 cod;
1027 UINT8 *p_eir_remote_name = NULL;
1028 bt_bdname_t bdname;
1029 bt_bdaddr_t bdaddr;
1030 UINT8 remote_name_len;
1031 UINT8 *p_cached_name = NULL;
1032 tBTA_SERVICE_MASK services = 0;
1033 bdstr_t bdstr;
1034
1035 p_search_data = (tBTA_DM_SEARCH *)p_param;
1036 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
1037
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001038 BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __FUNCTION__, bd2str(&bdaddr, &bdstr),
The Android Open Source Project5738f832012-12-12 16:00:35 -08001039#if (BLE_INCLUDED == TRUE)
1040 p_search_data->inq_res.device_type);
1041#else
1042 BT_DEVICE_TYPE_BREDR);
1043#endif
1044 bdname.name[0] = 0;
1045
1046 cod = devclass2uint (p_search_data->inq_res.dev_class);
1047
1048 if ( cod == 0) {
1049 ALOGD("cod is 0, set as unclassified");
1050 cod = COD_UNCLASSIFIED;
1051 }
1052
1053 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1054 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1055
1056 /* Check EIR for remote name and services */
1057 if (p_search_data->inq_res.p_eir)
1058 {
1059 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001060 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001061 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */
1062 }
1063
1064
1065 {
1066 bt_property_t properties[5];
1067 bt_device_type_t dev_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001068 UINT8 addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001069 uint32_t num_properties = 0;
1070 bt_status_t status;
1071
1072 memset(properties, 0, sizeof(properties));
1073 /* BD_ADDR */
1074 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1075 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1076 num_properties++;
1077 /* BD_NAME */
1078 /* Don't send BDNAME if it is empty */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001079 if (bdname.name[0])
1080 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001081 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1082 BT_PROPERTY_BDNAME,
1083 strlen((char *)bdname.name), &bdname);
1084 num_properties++;
1085 }
1086
1087 /* DEV_CLASS */
1088 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1089 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1090 num_properties++;
1091 /* DEV_TYPE */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001092#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001093 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1094 dev_type = p_search_data->inq_res.device_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001095 addr_type = p_search_data->inq_res.ble_addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001096#else
1097 dev_type = BT_DEVICE_TYPE_BREDR;
1098#endif
1099 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1100 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1101 num_properties++;
1102 /* RSSI */
1103 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1104 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1105 &(p_search_data->inq_res.rssi));
1106 num_properties++;
1107
1108 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1109 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001110#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1111 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001112 if (( dev_type == BT_DEVICE_TYPE_DUMO)&&
1113 (p_search_data->inq_res.flag & BTA_BLE_DMT_CONTROLLER_SPT) &&
1114 (p_search_data->inq_res.flag & BTA_BLE_DMT_HOST_SPT))
1115 {
1116 btif_storage_set_dmt_support_type (&bdaddr, TRUE);
1117 }
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001118 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1119#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001120 /* Callback to notify upper layer of device */
1121 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1122 num_properties, properties);
1123 }
1124 }
1125 break;
1126
1127 case BTA_DM_INQ_CMPL_EVT:
1128 {
Satya Calloji6e2d9db2014-07-08 16:18:58 -07001129#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1130 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1131 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1132 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1133 bte_scan_filt_param_cfg_evt, 0);
1134#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001135 }
1136 break;
1137 case BTA_DM_DISC_CMPL_EVT:
1138 {
1139 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1140 }
1141 break;
1142 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1143 {
1144 /* if inquiry is not in progress and we get a cancel event, then
1145 * it means we are done with inquiry, but remote_name fetches are in
1146 * progress
1147 *
1148 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt
1149 * but instead wait for the cancel_cmpl_evt via the Busy Level
1150 *
1151 */
1152 if (btif_dm_inquiry_in_progress == FALSE)
1153 {
1154 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1155 }
1156 }
1157 break;
1158 }
1159}
1160
1161/*******************************************************************************
1162**
1163** Function btif_dm_search_services_evt
1164**
1165** Description Executes search services event in btif context
1166**
1167** Returns void
1168**
1169*******************************************************************************/
1170static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1171{
1172 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1173
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001174 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001175 switch (event)
1176 {
1177 case BTA_DM_DISC_RES_EVT:
1178 {
1179 bt_uuid_t uuid_arr[BT_MAX_NUM_UUIDS]; /* Max 32 services */
1180 bt_property_t prop;
1181 uint32_t i = 0, j = 0;
1182 bt_bdaddr_t bd_addr;
1183 bt_status_t ret;
1184
1185 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1186
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001187 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001188 p_data->disc_res.result, p_data->disc_res.services);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001189 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1190 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1191 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1192 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001193 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001194 pairing_cb.sdp_attempts++;
1195 btif_dm_get_remote_services(&bd_addr);
1196 return;
1197 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001198 prop.type = BT_PROPERTY_UUIDS;
1199 prop.len = 0;
1200 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1201 {
1202 prop.val = p_data->disc_res.p_uuid_list;
1203 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1204 for (i=0; i < p_data->disc_res.num_uuids; i++)
1205 {
1206 char temp[256];
1207 uuid_to_string((bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i*MAX_UUID_SIZE)), temp);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001208 BTIF_TRACE_ERROR("Index: %d uuid:%s", i, temp);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001209 }
1210 }
1211
1212 /* onUuidChanged requires getBondedDevices to be populated.
1213 ** bond_state_changed needs to be sent prior to remote_device_property
1214 */
1215 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1216 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0)&&
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001217 pairing_cb.sdp_attempts > 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001218 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001219 BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001220 __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001221 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001222 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1223 }
1224
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001225 if(p_data->disc_res.num_uuids != 0)
1226 {
1227 /* Also write this to the NVRAM */
1228 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1229 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1230 /* Send the event to the BTIF */
1231 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1232 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1233 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001234 }
1235 break;
1236
1237 case BTA_DM_DISC_CMPL_EVT:
1238 /* fixme */
1239 break;
1240
Matthew Xie607e3b72013-08-15 19:30:48 -07001241#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001242 case BTA_DM_DISC_BLE_RES_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001243 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__,
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001244 p_data->disc_ble_res.service.uu.uuid16);
1245 bt_uuid_t uuid;
1246 int i = 0;
1247 int j = 15;
1248 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1249 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001250 BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001251 bt_property_t prop;
1252 bt_bdaddr_t bd_addr;
1253 char temp[256];
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001254 bt_status_t ret;
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001255
1256 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1257
1258 while(i < j )
1259 {
1260 unsigned char c = uuid.uu[j];
1261 uuid.uu[j] = uuid.uu[i];
1262 uuid.uu[i] = c;
1263 i++;
1264 j--;
1265 }
1266
1267 uuid_to_string(&uuid, temp);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001268 BTIF_TRACE_ERROR(" uuid:%s", temp);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001269
1270 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1271 prop.type = BT_PROPERTY_UUIDS;
1272 prop.val = uuid.uu;
1273 prop.len = MAX_UUID_SIZE;
1274
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001275 /* Also write this to the NVRAM */
1276 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1277 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1278
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001279 /* Send the event to the BTIF */
1280 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1281 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1282
1283 }
1284 break;
Matthew Xie607e3b72013-08-15 19:30:48 -07001285#endif /* BLE_INCLUDED */
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001286
The Android Open Source Project5738f832012-12-12 16:00:35 -08001287 default:
1288 {
1289 ASSERTC(0, "unhandled search services event", event);
1290 }
1291 break;
1292 }
1293}
1294
1295/*******************************************************************************
1296**
1297** Function btif_dm_remote_service_record_evt
1298**
1299** Description Executes search service record event in btif context
1300**
1301** Returns void
1302**
1303*******************************************************************************/
1304static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1305{
1306 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1307
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001308 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001309 switch (event)
1310 {
1311 case BTA_DM_DISC_RES_EVT:
1312 {
1313 bt_service_record_t rec;
1314 bt_property_t prop;
1315 uint32_t i = 0;
1316 bt_bdaddr_t bd_addr;
1317
1318 memset(&rec, 0, sizeof(bt_service_record_t));
1319 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1320
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001321 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001322 p_data->disc_res.result, p_data->disc_res.services);
1323 prop.type = BT_PROPERTY_SERVICE_RECORD;
1324 prop.val = (void*)&rec;
1325 prop.len = sizeof(rec);
1326
1327 /* disc_res.result is overloaded with SCN. Cannot check result */
1328 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1329 /* TODO: Get the UUID as well */
1330 rec.channel = p_data->disc_res.result - 3;
1331 /* TODO: Need to get the service name using p_raw_data */
1332 rec.name[0] = 0;
1333
1334 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1335 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1336 }
1337 break;
1338
1339 default:
1340 {
1341 ASSERTC(0, "unhandled remote service record event", event);
1342 }
1343 break;
1344 }
1345}
1346
1347/*******************************************************************************
1348**
1349** Function btif_dm_upstreams_cback
1350**
1351** Description Executes UPSTREAMS events in btif context
1352**
1353** Returns void
1354**
1355*******************************************************************************/
1356static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1357{
1358 tBTA_DM_SEC_EVT dm_event = (tBTA_DM_SEC_EVT)event;
1359 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1360 tBTA_SERVICE_MASK service_mask;
1361 uint32_t i;
1362 bt_bdaddr_t bd_addr;
1363
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001364 BTIF_TRACE_EVENT("btif_dm_upstreams_cback ev: %s", dump_dm_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001365
1366 switch (event)
1367 {
1368 case BTA_DM_ENABLE_EVT:
1369 {
1370 BD_NAME bdname;
1371 bt_status_t status;
1372 bt_property_t prop;
1373 prop.type = BT_PROPERTY_BDNAME;
1374 prop.len = BD_NAME_LEN;
1375 prop.val = (void*)bdname;
1376
1377 status = btif_storage_get_adapter_property(&prop);
Matthew Xie1e5109b2012-11-09 18:26:26 -08001378 if (status == BT_STATUS_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001379 {
1380 /* A name exists in the storage. Make this the device name */
1381 BTA_DmSetDeviceName((char*)prop.val);
1382 }
Matthew Xie1e5109b2012-11-09 18:26:26 -08001383 else
1384 {
1385 /* Storage does not have a name yet.
1386 * Use the default name and write it to the chip
1387 */
1388 BTA_DmSetDeviceName(btif_get_default_local_name());
1389 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001390
1391 /* for each of the enabled services in the mask, trigger the profile
1392 * enable */
1393 service_mask = btif_get_enabled_services_mask();
1394 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1395 {
1396 if (service_mask &
1397 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1398 {
1399 btif_in_execute_service_request(i, TRUE);
1400 }
1401 }
1402 /* clear control blocks */
1403 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1404
1405 /* This function will also trigger the adapter_properties_cb
1406 ** and bonded_devices_info_cb
1407 */
1408 btif_storage_load_bonded_devices();
1409
1410 btif_storage_load_autopair_device_list();
1411
1412 btif_enable_bluetooth_evt(p_data->enable.status, p_data->enable.bd_addr);
Satya Calloji0943c102014-05-12 09:13:02 -07001413
Wei Wangbf0e4b22014-05-19 23:23:35 -07001414 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Calloji0943c102014-05-12 09:13:02 -07001415 /* Enable local privacy */
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001416 /*TODO Should this call be exposed to JAVA...? */
Satya Calloji0943c102014-05-12 09:13:02 -07001417 BTA_DmBleConfigLocalPrivacy(TRUE);
Wei Wangbf0e4b22014-05-19 23:23:35 -07001418 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001419 }
1420 break;
1421
1422 case BTA_DM_DISABLE_EVT:
1423 /* for each of the enabled services in the mask, trigger the profile
1424 * disable */
1425 service_mask = btif_get_enabled_services_mask();
1426 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1427 {
1428 if (service_mask &
1429 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1430 {
1431 btif_in_execute_service_request(i, FALSE);
1432 }
1433 }
1434 btif_disable_bluetooth_evt();
1435 break;
1436
1437 case BTA_DM_PIN_REQ_EVT:
1438 btif_dm_pin_req_evt(&p_data->pin_req);
1439 break;
1440
1441 case BTA_DM_AUTH_CMPL_EVT:
1442 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1443 break;
1444
1445 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1446 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1447 {
1448 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1449 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1450 }
1451 break;
1452
1453 case BTA_DM_SP_CFM_REQ_EVT:
1454 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1455 break;
1456 case BTA_DM_SP_KEY_NOTIF_EVT:
1457 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1458 break;
1459
1460 case BTA_DM_DEV_UNPAIRED_EVT:
1461 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1462
1463 /*special handling for HID devices */
1464 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -07001465 btif_hh_remove_device(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001466 #endif
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001467 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1468 btif_storage_remove_ble_bonding_keys(&bd_addr);
1469 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001470 btif_storage_remove_bonded_device(&bd_addr);
1471 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1472 break;
1473
1474 case BTA_DM_BUSY_LEVEL_EVT:
1475 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001476
1477 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001478 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001479 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001480 {
1481 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1482 BT_DISCOVERY_STARTED);
1483 btif_dm_inquiry_in_progress = TRUE;
1484 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001485 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001486 {
1487 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1488 BT_DISCOVERY_STOPPED);
1489 btif_dm_inquiry_in_progress = FALSE;
1490 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001491 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001492 {
1493 btif_dm_inquiry_in_progress = FALSE;
1494 }
1495 }
1496 }break;
1497
1498 case BTA_DM_LINK_UP_EVT:
1499 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001500 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001501
1502 btif_update_remote_version_property(&bd_addr);
1503
The Android Open Source Project5738f832012-12-12 16:00:35 -08001504 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1505 &bd_addr, BT_ACL_STATE_CONNECTED);
1506 break;
1507
1508 case BTA_DM_LINK_DOWN_EVT:
1509 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001510 BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001511 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1512 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1513 break;
1514
1515 case BTA_DM_HW_ERROR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001516 BTIF_TRACE_ERROR("Received H/W Error. ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001517 /* Flush storage data */
1518 btif_config_flush();
1519 usleep(100000); /* 100milliseconds */
1520 /* Killing the process to force a restart as part of fault tolerance */
1521 kill(getpid(), SIGKILL);
1522 break;
1523
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001524#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1525 case BTA_DM_BLE_KEY_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001526 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 -08001527
1528 /* If this pairing is by-product of local initiated GATT client Read or Write,
1529 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1530 have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1531 if (pairing_cb.state != BT_BOND_STATE_BONDING)
1532 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001533 BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001534 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1535 BT_BOND_STATE_BONDING);
1536 }
1537 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1538 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001539 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001540 break;
1541 }
1542
1543 switch (p_data->ble_key.key_type)
1544 {
1545 case BTA_LE_KEY_PENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001546 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001547 pairing_cb.ble.is_penc_key_rcvd = TRUE;
1548 memcpy(pairing_cb.ble.penc_key.ltk,p_data->ble_key.key_value.penc_key.ltk, 16);
1549 memcpy(pairing_cb.ble.penc_key.rand, p_data->ble_key.key_value.penc_key.rand,8);
1550 pairing_cb.ble.penc_key.ediv = p_data->ble_key.key_value.penc_key.ediv;
1551 pairing_cb.ble.penc_key.sec_level = p_data->ble_key.key_value.penc_key.sec_level;
1552
1553 for (i=0; i<16; i++)
1554 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001555 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 -08001556 }
1557 for (i=0; i<8; i++)
1558 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001559 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 -08001560 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001561 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.ediv=0x%04x",pairing_cb.ble.penc_key.ediv);
1562 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.sec_level=0x%02x",pairing_cb.ble.penc_key.sec_level);
1563 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 -08001564 break;
1565
1566 case BTA_LE_KEY_PID:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001567 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001568 pairing_cb.ble.is_pid_key_rcvd = TRUE;
1569 memcpy(pairing_cb.ble.pid_key, p_data->ble_key.key_value.pid_key.irk, 16);
1570 for (i=0; i<16; i++)
1571 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001572 BTIF_TRACE_DEBUG("pairing_cb.ble.pid_key[%d]=0x%02x",i,pairing_cb.ble.pid_key[i]);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001573 }
1574 break;
1575
1576 case BTA_LE_KEY_PCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001577 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001578 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1579 pairing_cb.ble.pcsrk_key.counter = p_data->ble_key.key_value.pcsrk_key.counter;
1580 pairing_cb.ble.pcsrk_key.sec_level = p_data->ble_key.key_value.pcsrk_key.sec_level;
1581 memcpy(pairing_cb.ble.pcsrk_key.csrk,p_data->ble_key.key_value.pcsrk_key.csrk,16);
1582
1583 for (i=0; i<16; i++)
1584 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001585 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 -08001586 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001587 BTIF_TRACE_DEBUG("pairing_cb.ble.pcsrk_key.counter=0x%08x",pairing_cb.ble.pcsrk_key.counter);
1588 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 -08001589 break;
1590
1591 case BTA_LE_KEY_LENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001592 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001593 pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1594 pairing_cb.ble.lenc_key.div = p_data->ble_key.key_value.lenc_key.div;
1595 pairing_cb.ble.lenc_key.key_size = p_data->ble_key.key_value.lenc_key.key_size;
1596 pairing_cb.ble.lenc_key.sec_level = p_data->ble_key.key_value.lenc_key.sec_level;
1597
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001598 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.div=0x%04x",pairing_cb.ble.lenc_key.div);
1599 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.key_size=0x%02x",pairing_cb.ble.lenc_key.key_size);
1600 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 -08001601 break;
1602
1603
1604
1605 case BTA_LE_KEY_LCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001606 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001607 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1608 pairing_cb.ble.lcsrk_key.counter = p_data->ble_key.key_value.lcsrk_key.counter;
1609 pairing_cb.ble.lcsrk_key.div = p_data->ble_key.key_value.lcsrk_key.div;
1610 pairing_cb.ble.lcsrk_key.sec_level = p_data->ble_key.key_value.lcsrk_key.sec_level;
1611
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001612 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.div=0x%04x",pairing_cb.ble.lcsrk_key.div);
1613 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.counter=0x%08x",pairing_cb.ble.lcsrk_key.counter);
1614 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 -08001615
1616 break;
1617
1618 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001619 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001620 break;
1621 }
1622
1623 break;
1624 case BTA_DM_BLE_SEC_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001625 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001626 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1627 break;
1628 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001629 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001630 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1631 break;
1632 case BTA_DM_BLE_PASSKEY_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001633 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001634 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1635 break;
1636 case BTA_DM_BLE_OOB_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001637 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001638 break;
1639 case BTA_DM_BLE_LOCAL_IR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001640 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001641 ble_local_key_cb.is_id_keys_rcvd = TRUE;
1642 memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1643 memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1644 memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1645 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1646 BTIF_DM_LE_LOCAL_KEY_IR,
1647 BT_OCTET16_LEN);
1648 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1649 BTIF_DM_LE_LOCAL_KEY_IRK,
1650 BT_OCTET16_LEN);
1651 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1652 BTIF_DM_LE_LOCAL_KEY_DHK,
1653 BT_OCTET16_LEN);
1654 break;
1655 case BTA_DM_BLE_LOCAL_ER_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001656 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001657 ble_local_key_cb.is_er_rcvd = TRUE;
1658 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1659 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1660 BTIF_DM_LE_LOCAL_KEY_ER,
1661 BT_OCTET16_LEN);
1662 break;
1663
1664 case BTA_DM_BLE_AUTH_CMPL_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001665 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001666 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1667 break;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001668
1669 case BTA_DM_LE_FEATURES_READ:
1670 {
1671 tBTM_BLE_VSC_CB cmn_vsc_cb;
1672 bt_local_le_features_t local_le_features;
1673 char buf[512];
1674 bt_property_t prop;
1675 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1676 prop.val = (void*)buf;
1677 prop.len = sizeof(buf);
1678
1679 /* LE features are not stored in storage. Should be retrived from stack */
1680 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1681 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1682
1683 prop.len = sizeof (bt_local_le_features_t);
1684 if (cmn_vsc_cb.filter_support == 1)
1685 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1686 else
1687 local_le_features.max_adv_filter_supported = 0;
1688 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1689 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1690 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
1691 local_le_features.scan_result_storage_size = cmn_vsc_cb.tot_scan_results_strg;
1692 memcpy(prop.val, &local_le_features, prop.len);
1693 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
1694 break;
1695 }
Satya Callojie5ba8842014-07-03 17:18:02 -07001696
1697 case BTA_DM_ENER_INFO_READ:
1698 {
1699 btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param;
1700 bt_activity_energy_info energy_info;
1701 energy_info.status = p_ener_data->status;
1702 energy_info.ctrl_state = p_ener_data->ctrl_state;
1703 energy_info.rx_time = p_ener_data->rx_time;
1704 energy_info.tx_time = p_ener_data->tx_time;
1705 energy_info.idle_time = p_ener_data->idle_time;
1706 energy_info.energy_used = p_ener_data->energy_used;
1707 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info);
1708 break;
1709 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001710#endif
1711
The Android Open Source Project5738f832012-12-12 16:00:35 -08001712 case BTA_DM_AUTHORIZE_EVT:
1713 case BTA_DM_SIG_STRENGTH_EVT:
1714 case BTA_DM_SP_RMT_OOB_EVT:
1715 case BTA_DM_SP_KEYPRESS_EVT:
1716 case BTA_DM_ROLE_CHG_EVT:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001717
The Android Open Source Project5738f832012-12-12 16:00:35 -08001718 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001719 BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001720 break;
1721 }
1722} /* btui_security_cback() */
1723
1724
1725/*******************************************************************************
1726**
1727** Function btif_dm_generic_evt
1728**
1729** Description Executes non-BTA upstream events in BTIF context
1730**
1731** Returns void
1732**
1733*******************************************************************************/
1734static void btif_dm_generic_evt(UINT16 event, char* p_param)
1735{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001736 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001737 switch(event)
1738 {
1739 case BTIF_DM_CB_DISCOVERY_STARTED:
1740 {
1741 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
1742 }
1743 break;
1744
1745 case BTIF_DM_CB_CREATE_BOND:
1746 {
1747 btif_dm_cb_create_bond((bt_bdaddr_t *)p_param);
1748 }
1749 break;
1750
1751 case BTIF_DM_CB_REMOVE_BOND:
1752 {
1753 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
1754 }
1755 break;
1756
1757 case BTIF_DM_CB_HID_REMOTE_NAME:
1758 {
1759 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
1760 }
1761 break;
1762
1763 case BTIF_DM_CB_BOND_STATE_BONDING:
1764 {
1765 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
1766 }
1767 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001768 case BTIF_DM_CB_LE_TX_TEST:
1769 case BTIF_DM_CB_LE_RX_TEST:
1770 {
1771 uint8_t status;
1772 STREAM_TO_UINT8(status, p_param);
1773 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1774 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
1775 }
1776 break;
1777 case BTIF_DM_CB_LE_TEST_END:
1778 {
1779 uint8_t status;
1780 uint16_t count = 0;
1781 STREAM_TO_UINT8(status, p_param);
1782 if (status == 0)
1783 STREAM_TO_UINT16(count, p_param);
1784 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1785 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
1786 }
1787 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001788 default:
1789 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001790 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001791 }
1792 break;
1793 }
1794}
1795
1796/*******************************************************************************
1797**
1798** Function bte_dm_evt
1799**
1800** Description Switches context from BTE to BTIF for all DM events
1801**
1802** Returns void
1803**
1804*******************************************************************************/
1805
1806void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
1807{
1808 bt_status_t status;
1809
1810 /* switch context to btif task context (copy full union size for convenience) */
1811 status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event, (void*)p_data, sizeof(tBTA_DM_SEC), NULL);
1812
1813 /* catch any failed context transfers */
1814 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
1815}
1816
1817/*******************************************************************************
1818**
1819** Function bte_search_devices_evt
1820**
1821** Description Switches context from BTE to BTIF for DM search events
1822**
1823** Returns void
1824**
1825*******************************************************************************/
1826static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1827{
1828 UINT16 param_len = 0;
1829
1830 if (p_data)
1831 param_len += sizeof(tBTA_DM_SEARCH);
1832 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
1833 switch (event)
1834 {
1835 case BTA_DM_INQ_RES_EVT:
1836 {
1837 if (p_data->inq_res.p_eir)
1838 param_len += HCI_EXT_INQ_RESPONSE_LEN;
1839 }
1840 break;
1841
1842 case BTA_DM_DISC_RES_EVT:
1843 {
1844 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
1845 param_len += p_data->disc_res.raw_data_size;
1846 }
1847 break;
1848 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001849 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 -08001850
1851 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
1852 if (event == BTA_DM_INQ_RES_EVT)
1853 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
1854
1855 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
1856 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
1857}
1858
1859/*******************************************************************************
1860**
1861** Function bte_dm_search_services_evt
1862**
1863** Description Switches context from BTE to BTIF for DM search services
1864** event
1865**
1866** Returns void
1867**
1868*******************************************************************************/
1869static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1870{
1871 UINT16 param_len = 0;
1872 if (p_data)
1873 param_len += sizeof(tBTA_DM_SEARCH);
1874 switch (event)
1875 {
1876 case BTA_DM_DISC_RES_EVT:
1877 {
1878 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
1879 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
1880 }
1881 } break;
1882 }
1883 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
1884 * if raw_data is needed. */
1885 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
1886 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
1887}
1888
1889/*******************************************************************************
1890**
1891** Function bte_dm_remote_service_record_evt
1892**
1893** Description Switches context from BTE to BTIF for DM search service
1894** record event
1895**
1896** Returns void
1897**
1898*******************************************************************************/
1899static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1900{
1901 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
1902 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
1903}
1904
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07001905#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07001906/*******************************************************************************
1907**
1908** Function bta_energy_info_cb
1909**
1910** Description Switches context from BTE to BTIF for DM energy info event
1911**
1912** Returns void
1913**
1914*******************************************************************************/
1915static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time,
1916 tBTA_DM_BLE_IDLE_TIME_MS idle_time,
1917 tBTA_DM_BLE_ENERGY_USED energy_used,
1918 tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status)
1919{
1920 BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld",
1921 status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
1922
1923 btif_activity_energy_info_cb_t btif_cb;
1924 btif_cb.status = status;
1925 btif_cb.ctrl_state = ctrl_state;
1926 btif_cb.tx_time = (uint64_t) tx_time;
1927 btif_cb.rx_time = (uint64_t) rx_time;
1928 btif_cb.idle_time =(uint64_t) idle_time;
1929 btif_cb.energy_used =(uint64_t) energy_used;
1930 btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
1931 (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL);
1932}
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07001933#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07001934
Satya Calloji6e2d9db2014-07-08 16:18:58 -07001935/*******************************************************************************
1936**
1937** Function bte_scan_filt_param_cfg_evt
1938**
1939** Description Scan filter param config event
1940**
1941** Returns void
1942**
1943*******************************************************************************/
1944static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
1945 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
1946 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
1947{
1948 /* This event occurs on calling BTA_DmBleCfgFilterCondition internally,
1949 ** and that is why there is no HAL callback
1950 */
1951 if(BTA_SUCCESS != status)
1952 {
1953 BTIF_TRACE_ERROR("%s, %d", __FUNCTION__, status);
1954 }
1955 else
1956 {
1957 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
1958 }
1959}
1960
The Android Open Source Project5738f832012-12-12 16:00:35 -08001961/*****************************************************************************
1962**
1963** btif api functions (no context switch)
1964**
1965*****************************************************************************/
1966
1967/*******************************************************************************
1968**
1969** Function btif_dm_start_discovery
1970**
1971** Description Start device discovery/inquiry
1972**
1973** Returns bt_status_t
1974**
1975*******************************************************************************/
1976bt_status_t btif_dm_start_discovery(void)
1977{
1978 tBTA_DM_INQ inq_params;
1979 tBTA_SERVICE_MASK services = 0;
Satya Calloji6e2d9db2014-07-08 16:18:58 -07001980 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001981
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001982 BTIF_TRACE_EVENT("%s", __FUNCTION__);
Satya Calloji6e2d9db2014-07-08 16:18:58 -07001983
1984#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1985 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1986 /* Cleanup anything remaining on index 0 */
1987 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1988 bte_scan_filt_param_cfg_evt, 0);
1989
1990 /* Add an allow-all filter on index 0*/
1991 adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE;
1992 adv_filt_param.feat_seln = ALLOW_ALL_FILTER;
1993 adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;
1994 adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;
1995 adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE;
1996 adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE;
1997 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL,
1998 bte_scan_filt_param_cfg_evt, 0);
1999
The Android Open Source Project5738f832012-12-12 16:00:35 -08002000 /* TODO: Do we need to handle multiple inquiries at the same time? */
2001
2002 /* Set inquiry params and call API */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002003 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
Matthew Xie7f3e4292013-09-30 12:44:10 -07002004#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
2005 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
2006 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
2007 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
2008 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
2009#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002010#else
2011 inq_params.mode = BTA_DM_GENERAL_INQUIRY;
2012#endif
2013 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
2014
2015 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
2016 inq_params.report_dup = TRUE;
2017
2018 inq_params.filter_type = BTA_DM_INQ_CLR;
2019 /* TODO: Filter device by BDA needs to be implemented here */
2020
2021 /* Will be enabled to TRUE once inquiry busy level has been received */
2022 btif_dm_inquiry_in_progress = FALSE;
2023 /* find nearby devices */
2024 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
2025
2026 return BT_STATUS_SUCCESS;
2027}
2028
2029/*******************************************************************************
2030**
2031** Function btif_dm_cancel_discovery
2032**
2033** Description Cancels search
2034**
2035** Returns bt_status_t
2036**
2037*******************************************************************************/
2038bt_status_t btif_dm_cancel_discovery(void)
2039{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002040 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002041 BTA_DmSearchCancel();
2042 return BT_STATUS_SUCCESS;
2043}
2044
2045/*******************************************************************************
2046**
2047** Function btif_dm_create_bond
2048**
2049** Description Initiate bonding with the specified device
2050**
2051** Returns bt_status_t
2052**
2053*******************************************************************************/
2054bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr)
2055{
2056 bdstr_t bdstr;
2057
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002058 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *) bd_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002059 if (pairing_cb.state != BT_BOND_STATE_NONE)
2060 return BT_STATUS_BUSY;
2061
2062 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
2063 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2064
2065 return BT_STATUS_SUCCESS;
2066}
2067
2068/*******************************************************************************
2069**
2070** Function btif_dm_cancel_bond
2071**
2072** Description Initiate bonding with the specified device
2073**
2074** Returns bt_status_t
2075**
2076*******************************************************************************/
2077
2078bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
2079{
2080 bdstr_t bdstr;
2081
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002082 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *)bd_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002083
2084 /* TODO:
2085 ** 1. Restore scan modes
2086 ** 2. special handling for HID devices
2087 */
2088 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2089 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002090
2091#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2092
2093 if (pairing_cb.is_ssp)
2094 {
2095 if (pairing_cb.is_le_only)
2096 {
2097 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2098 }
2099 else
Hemant Guptae1468692013-11-14 16:21:29 +05302100 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002101 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
Hemant Guptae1468692013-11-14 16:21:29 +05302102 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2103 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2104 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002105 }
2106 else
2107 {
2108 if (pairing_cb.is_le_only)
2109 {
2110 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2111 }
2112 else
2113 {
2114 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2115 }
2116 /* Cancel bonding, in case it is in ACL connection setup state */
2117 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2118 }
2119
2120#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002121 if (pairing_cb.is_ssp)
2122 {
2123 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2124 }
2125 else
2126 {
2127 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2128 }
2129 /* Cancel bonding, in case it is in ACL connection setup state */
2130 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2131 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002132#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002133 }
2134
2135 return BT_STATUS_SUCCESS;
2136}
2137
2138/*******************************************************************************
2139**
Kim Schulza9eb25c2013-09-30 10:55:52 +02002140** Function btif_dm_hh_open_failed
2141**
2142** Description informs the upper layers if the HH have failed during bonding
2143**
2144** Returns none
2145**
2146*******************************************************************************/
2147
2148void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
2149{
2150 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2151 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2152 {
2153 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2154 }
2155}
2156
2157/*******************************************************************************
2158**
The Android Open Source Project5738f832012-12-12 16:00:35 -08002159** Function btif_dm_remove_bond
2160**
2161** Description Removes bonding with the specified device
2162**
2163** Returns bt_status_t
2164**
2165*******************************************************************************/
2166
2167bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
2168{
2169 bdstr_t bdstr;
2170
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002171 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *)bd_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002172 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2173 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2174
2175 return BT_STATUS_SUCCESS;
2176}
2177
2178/*******************************************************************************
2179**
2180** Function btif_dm_pin_reply
2181**
2182** Description BT legacy pairing - PIN code reply
2183**
2184** Returns bt_status_t
2185**
2186*******************************************************************************/
2187
2188bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2189 uint8_t pin_len, bt_pin_code_t *pin_code)
2190{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002191 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Hemant Gupta831423e2014-01-08 12:42:13 +05302192 if (pin_code == NULL)
2193 return BT_STATUS_FAIL;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002194#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002195
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002196 if (pairing_cb.is_le_only)
2197 {
2198 int i;
2199 UINT32 passkey = 0;
2200 int multi[] = {100000, 10000, 1000, 100, 10,1};
2201 BD_ADDR remote_bd_addr;
2202 bdcpy(remote_bd_addr, bd_addr->address);
2203 for (i = 0; i < 6; i++)
2204 {
2205 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2206 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002207 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002208 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2209
2210 }
2211 else
2212 {
2213 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2214 if (accept)
2215 pairing_cb.pin_code_len = pin_len;
2216 }
2217#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002218 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2219
2220 if (accept)
2221 pairing_cb.pin_code_len = pin_len;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002222#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002223 return BT_STATUS_SUCCESS;
2224}
2225
2226/*******************************************************************************
2227**
2228** Function btif_dm_ssp_reply
2229**
2230** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2231**
2232** Returns bt_status_t
2233**
2234*******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002235bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2236 bt_ssp_variant_t variant, uint8_t accept,
2237 uint32_t passkey)
2238{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08002239 UNUSED(passkey);
2240
The Android Open Source Project5738f832012-12-12 16:00:35 -08002241 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2242 {
2243 /* This is not implemented in the stack.
2244 * For devices with display, this is not needed
2245 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002246 BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002247 return BT_STATUS_FAIL;
2248 }
2249 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002250 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002251#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2252 if (pairing_cb.is_le_only)
2253 {
2254 if (accept)
2255 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2256 else
2257 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2258 }
2259 else
2260 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002261
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002262#else
2263 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2264#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002265 return BT_STATUS_SUCCESS;
2266}
2267
2268/*******************************************************************************
2269**
2270** Function btif_dm_get_adapter_property
2271**
2272** Description Queries the BTA for the adapter property
2273**
2274** Returns bt_status_t
2275**
2276*******************************************************************************/
2277bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2278{
2279 bt_status_t status;
2280
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002281 BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002282 switch (prop->type)
2283 {
2284 case BT_PROPERTY_BDNAME:
2285 {
2286 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
Matthew Xie1e5109b2012-11-09 18:26:26 -08002287 strcpy((char *)bd_name->name, btif_get_default_local_name());
The Android Open Source Project5738f832012-12-12 16:00:35 -08002288 prop->len = strlen((char *)bd_name->name);
2289 }
2290 break;
2291
2292 case BT_PROPERTY_ADAPTER_SCAN_MODE:
2293 {
2294 /* if the storage does not have it. Most likely app never set it. Default is NONE */
2295 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2296 *mode = BT_SCAN_MODE_NONE;
2297 prop->len = sizeof(bt_scan_mode_t);
2298 }
2299 break;
2300
2301 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2302 {
2303 uint32_t *tmt = (uint32_t*)prop->val;
2304 *tmt = 120; /* default to 120s, if not found in NV */
2305 prop->len = sizeof(uint32_t);
2306 }
2307 break;
2308
2309 default:
2310 prop->len = 0;
2311 return BT_STATUS_FAIL;
2312 }
2313 return BT_STATUS_SUCCESS;
2314}
2315
2316/*******************************************************************************
2317**
2318** Function btif_dm_get_remote_services
2319**
2320** Description Start SDP to get remote services
2321**
2322** Returns bt_status_t
2323**
2324*******************************************************************************/
2325bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2326{
2327 bdstr_t bdstr;
2328
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002329 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002330
2331 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2332 bte_dm_search_services_evt, TRUE);
2333
2334 return BT_STATUS_SUCCESS;
2335}
2336
2337/*******************************************************************************
2338**
2339** Function btif_dm_get_remote_service_record
2340**
2341** Description Start SDP to get remote service record
2342**
2343**
2344** Returns bt_status_t
2345*******************************************************************************/
2346bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2347 bt_uuid_t *uuid)
2348{
2349 tSDP_UUID sdp_uuid;
2350 bdstr_t bdstr;
2351
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002352 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002353
2354 sdp_uuid.len = MAX_UUID_SIZE;
2355 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2356
2357 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2358 bte_dm_remote_service_record_evt, TRUE);
2359
2360 return BT_STATUS_SUCCESS;
2361}
2362
2363void btif_dm_execute_service_request(UINT16 event, char *p_param)
2364{
2365 BOOLEAN b_enable = FALSE;
2366 bt_status_t status;
2367 if (event == BTIF_DM_ENABLE_SERVICE)
2368 {
2369 b_enable = TRUE;
2370 }
2371 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2372 if (status == BT_STATUS_SUCCESS)
2373 {
2374 bt_property_t property;
2375 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2376
2377 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2378 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2379 sizeof(local_uuids), local_uuids);
2380 btif_storage_get_adapter_property(&property);
2381 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2382 BT_STATUS_SUCCESS, 1, &property);
2383 }
2384 return;
2385}
2386
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002387void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2388 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2389{
2390 UINT8 yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2391 /* if local initiated:
2392 ** 1. set DD + MITM
2393 ** if remote initiated:
2394 ** 1. Copy over the auth_req from peer's io_rsp
2395 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2396 ** as a fallback set MITM+GB if peer had MITM set
2397 */
2398 UNUSED (bd_addr);
2399 UNUSED (p_io_cap);
2400 UNUSED (p_oob_data);
2401
2402
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002403 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002404 if(pairing_cb.is_local_initiated)
2405 {
2406 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2407 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2408 }
2409 else if (!is_orig)
2410 {
2411 /* peer initiated paring. They probably know what they want.
2412 ** Copy the mitm from peer device.
2413 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002414 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002415 __FUNCTION__, pairing_cb.auth_req);
2416 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2417
2418 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2419 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2420 *p_auth_req |= BTA_AUTH_SP_YES;
2421 }
2422 else if (yes_no_bit)
2423 {
2424 /* set the general bonding bit for stored device */
2425 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2426 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002427 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002428}
2429
2430void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2431 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2432{
2433 UNUSED (bd_addr);
2434 UNUSED (oob_data);
2435
2436 if(auth_req & BTA_AUTH_BONDS)
2437 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002438 BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002439 pairing_cb.auth_req = auth_req;
2440 pairing_cb.io_cap = io_cap;
2441 }
2442}
2443
The Android Open Source Project5738f832012-12-12 16:00:35 -08002444#if (BTM_OOB_INCLUDED == TRUE)
2445void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data)
2446{
2447 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2448 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2449 {
2450 *p_oob_data = FALSE;
2451 }
2452 else
2453 {
2454 *p_oob_data = TRUE;
2455 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002456 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 -08002457}
2458#endif /* BTM_OOB_INCLUDED */
2459
2460#ifdef BTIF_DM_OOB_TEST
2461void btif_dm_load_local_oob(void)
2462{
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002463 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002464 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002465 BTIF_TRACE_DEBUG("btif_dm_load_local_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002466 if (prop_oob[0] != '3')
2467 {
2468#if (BTM_OOB_INCLUDED == TRUE)
2469 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2470 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2471 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002472 BTIF_TRACE_DEBUG("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002473 BTA_DmLocalOob();
2474 }
2475#else
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002476 BTIF_TRACE_ERROR("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002477#endif
2478 }
2479}
2480
2481void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2482{
2483 FILE *fp;
2484 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2485 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2486 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002487 char prop_oob[PROPERTY_VALUE_MAX];
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002488 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: valid=%d", valid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002489 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2490 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2491 valid)
2492 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002493 BTIF_TRACE_DEBUG("save local OOB data in memory");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002494 memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2495 memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2496 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002497 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002498 if (prop_oob[0] == '1')
2499 path = path_a;
2500 else if (prop_oob[0] == '2')
2501 path = path_b;
2502 if (path)
2503 {
2504 fp = fopen(path, "wb+");
2505 if (fp == NULL)
2506 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002507 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 -08002508 }
2509 else
2510 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002511 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 -08002512 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2513 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2514 fclose(fp);
2515 }
2516 }
2517 }
2518}
2519BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2520{
2521 char t[128];
2522 FILE *fp;
2523 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2524 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2525 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002526 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002527 BOOLEAN result = FALSE;
2528 bt_bdaddr_t bt_bd_addr;
2529 bdcpy(oob_cb.oob_bdaddr, bd_addr);
2530 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002531 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002532 if (prop_oob[0] == '1')
2533 path = path_b;
2534 else if (prop_oob[0] == '2')
2535 path = path_a;
2536 if (path)
2537 {
2538 fp = fopen(path, "rb");
2539 if (fp == NULL)
2540 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002541 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 -08002542 return FALSE;
2543 }
2544 else
2545 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002546 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob: read OOB data from %s",path);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002547 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2548 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2549 fclose(fp);
2550 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002551 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: TRUE");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002552 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2553 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2554 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002555 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002556 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2557 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
2558 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 -07002559 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: c = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002560 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2561 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
2562 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 -07002563 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: r = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002564 bdcpy(bt_bd_addr.address, bd_addr);
2565 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2566 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2567 result = TRUE;
2568 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002569 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob result=%d",result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002570 return result;
2571}
2572#endif /* BTIF_DM_OOB_TEST */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002573#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2574
2575static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2576{
2577 bt_bdaddr_t bd_addr;
2578 bt_bdname_t bd_name;
2579 UINT32 cod;
2580
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002581 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002582
2583 /* Remote name update */
2584 btif_update_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2585 NULL, BT_DEVICE_TYPE_BLE);
2586 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2587 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2588
2589 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2590 pairing_cb.is_ssp = FALSE;
2591 cod = COD_UNCLASSIFIED;
2592
2593 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2594 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2595 p_ssp_key_notif->passkey);
2596}
2597
2598/*******************************************************************************
2599**
2600** Function btif_dm_ble_auth_cmpl_evt
2601**
2602** Description Executes authentication complete event in btif context
2603**
2604** Returns void
2605**
2606*******************************************************************************/
2607static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2608{
2609 /* Save link key, if not temporary */
2610 bt_bdaddr_t bd_addr;
2611 bt_status_t status = BT_STATUS_FAIL;
2612 bt_bond_state_t state = BT_BOND_STATE_NONE;
2613
2614 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2615 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2616 {
2617 /* store keys */
2618 }
2619 if (p_auth_cmpl->success)
2620 {
2621 status = BT_STATUS_SUCCESS;
2622 state = BT_BOND_STATE_BONDED;
2623
2624 btif_dm_save_ble_bonding_keys();
2625 BTA_GATTC_Refresh(bd_addr.address);
2626 btif_dm_get_remote_services(&bd_addr);
2627 }
2628 else
2629 {
2630 /*Map the HCI fail reason to bt status */
2631 switch (p_auth_cmpl->fail_reason)
2632 {
Priti Aghera156c52b2014-07-09 14:58:19 -07002633 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
2634 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
2635 btif_dm_remove_ble_bonding_keys();
2636 status = BT_STATUS_AUTH_FAILURE;
2637 break;
2638 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
2639 status = BT_STATUS_AUTH_REJECTED;
2640 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002641 default:
Andre Eisenbachca22ac42013-02-13 17:02:11 +09002642 btif_dm_remove_ble_bonding_keys();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002643 status = BT_STATUS_FAIL;
2644 break;
2645 }
2646 }
2647 bond_state_changed(status, &bd_addr, state);
2648}
2649
2650
2651
2652void btif_dm_load_ble_local_keys(void)
2653{
2654 bt_status_t bt_status;
2655
2656 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2657
2658 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2659 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2660 {
2661 ble_local_key_cb.is_er_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002662 BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002663 }
2664
2665 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
2666 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
2667 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
2668 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
2669 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
2670 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
2671 {
2672 ble_local_key_cb.is_id_keys_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002673 BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002674 }
2675
2676}
2677void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
2678 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
2679{
2680 if (ble_local_key_cb.is_er_rcvd )
2681 {
2682 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2683 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2684 }
2685
2686 if (ble_local_key_cb.is_id_keys_rcvd)
2687 {
2688 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
2689 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
2690 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
2691 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
2692 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002693 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002694}
2695
2696void btif_dm_save_ble_bonding_keys(void)
2697{
2698
2699 bt_bdaddr_t bd_addr;
2700
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002701 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002702
2703 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2704
2705 if (pairing_cb.ble.is_penc_key_rcvd)
2706 {
2707 btif_storage_add_ble_bonding_key(&bd_addr,
2708 (char *) &pairing_cb.ble.penc_key,
2709 BTIF_DM_LE_KEY_PENC,
2710 sizeof(btif_dm_ble_penc_keys_t));
2711 }
2712
2713 if (pairing_cb.ble.is_pid_key_rcvd)
2714 {
2715 btif_storage_add_ble_bonding_key(&bd_addr,
2716 (char *) &pairing_cb.ble.pid_key[0],
2717 BTIF_DM_LE_KEY_PID,
2718 BT_OCTET16_LEN);
2719 }
2720
2721
2722 if (pairing_cb.ble.is_pcsrk_key_rcvd)
2723 {
2724 btif_storage_add_ble_bonding_key(&bd_addr,
2725 (char *) &pairing_cb.ble.pcsrk_key,
2726 BTIF_DM_LE_KEY_PCSRK,
2727 sizeof(btif_dm_ble_pcsrk_keys_t));
2728 }
2729
2730
2731 if (pairing_cb.ble.is_lenc_key_rcvd)
2732 {
2733 btif_storage_add_ble_bonding_key(&bd_addr,
2734 (char *) &pairing_cb.ble.lenc_key,
2735 BTIF_DM_LE_KEY_LENC,
2736 sizeof(btif_dm_ble_lenc_keys_t));
2737 }
2738
2739 if (pairing_cb.ble.is_lcsrk_key_rcvd)
2740 {
2741 btif_storage_add_ble_bonding_key(&bd_addr,
2742 (char *) &pairing_cb.ble.lcsrk_key,
2743 BTIF_DM_LE_KEY_LCSRK,
2744 sizeof(btif_dm_ble_lcsrk_keys_t));
2745 }
2746
2747}
2748
2749
2750void btif_dm_remove_ble_bonding_keys(void)
2751{
2752 bt_bdaddr_t bd_addr;
2753
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002754 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002755
2756 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2757 btif_storage_remove_ble_bonding_keys(&bd_addr);
2758}
2759
2760
2761/*******************************************************************************
2762**
2763** Function btif_dm_ble_sec_req_evt
2764**
2765** Description Eprocess security request event in btif context
2766**
2767** Returns void
2768**
2769*******************************************************************************/
2770void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
2771{
2772 bt_bdaddr_t bd_addr;
2773 bt_bdname_t bd_name;
2774 UINT32 cod;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002775 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002776
2777 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2778 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002779 BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002780 return;
2781 }
2782
2783 /* Remote name update */
2784 btif_update_remote_properties(p_ble_req->bd_addr,p_ble_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
2785
2786 bdcpy(bd_addr.address, p_ble_req->bd_addr);
2787 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
2788
2789 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2790
2791 pairing_cb.is_temp = FALSE;
2792 pairing_cb.is_le_only = TRUE;
2793 pairing_cb.is_ssp = TRUE;
2794
2795 cod = COD_UNCLASSIFIED;
2796
2797 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
2798 BT_SSP_VARIANT_CONSENT, 0);
2799}
2800
2801
2802
2803/*******************************************************************************
2804**
2805** Function btif_dm_ble_passkey_req_evt
2806**
2807** Description Executes pin request event in btif context
2808**
2809** Returns void
2810**
2811*******************************************************************************/
2812static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
2813{
2814 bt_bdaddr_t bd_addr;
2815 bt_bdname_t bd_name;
2816 UINT32 cod;
2817
2818 /* Remote name update */
2819 btif_update_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
2820
2821 bdcpy(bd_addr.address, p_pin_req->bd_addr);
2822 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
2823
2824 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2825 pairing_cb.is_le_only = TRUE;
2826
2827 cod = COD_UNCLASSIFIED;
2828
2829 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
2830 &bd_addr, &bd_name, cod);
2831}
2832
2833
2834void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
2835 tBT_DEVICE_TYPE dev_type)
2836{
2837 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
2838}
2839
2840static void btif_dm_ble_tx_test_cback(void *p)
2841{
2842 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
2843 (char *)p, 1, NULL);
2844}
2845
2846static void btif_dm_ble_rx_test_cback(void *p)
2847{
2848 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
2849 (char *)p, 1, NULL);
2850}
2851
2852static void btif_dm_ble_test_end_cback(void *p)
2853{
2854 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
2855 (char *)p, 3, NULL);
2856}
2857/*******************************************************************************
2858**
2859** Function btif_le_test_mode
2860**
2861** Description Sends a HCI BLE Test command to the Controller
2862**
2863** Returns BT_STATUS_SUCCESS on success
2864**
2865*******************************************************************************/
2866bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
2867{
2868 switch (opcode) {
2869 case HCI_BLE_TRANSMITTER_TEST:
2870 if (len != 3) return BT_STATUS_PARM_INVALID;
2871 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
2872 break;
2873 case HCI_BLE_RECEIVER_TEST:
2874 if (len != 1) return BT_STATUS_PARM_INVALID;
2875 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
2876 break;
2877 case HCI_BLE_TEST_END:
2878 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
2879 break;
2880 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002881 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002882 return BT_STATUS_UNSUPPORTED;
2883 }
2884 return BT_STATUS_SUCCESS;
2885}
2886
2887#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002888
2889void btif_dm_on_disable()
2890{
2891 /* cancel any pending pairing requests */
2892 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2893 {
2894 bt_bdaddr_t bd_addr;
2895
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002896 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002897 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2898 btif_dm_cancel_bond(&bd_addr);
2899 }
2900}
Matthew Xie1e5109b2012-11-09 18:26:26 -08002901
Satya Callojie5ba8842014-07-03 17:18:02 -07002902/*******************************************************************************
2903**
2904** Function btif_dm_read_energy_info
2905**
2906** Description Reads the energy info from controller
2907**
2908** Returns void
2909**
2910*******************************************************************************/
2911void btif_dm_read_energy_info()
2912{
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002913#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07002914 BTA_DmBleGetEnergyInfo(bta_energy_info_cb);
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002915#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07002916}
2917
Matthew Xie1e5109b2012-11-09 18:26:26 -08002918static char* btif_get_default_local_name() {
2919 if (btif_default_local_name[0] == '\0')
2920 {
2921 int max_len = sizeof(btif_default_local_name) - 1;
2922 if (BTM_DEF_LOCAL_NAME[0] != '\0')
2923 {
2924 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
2925 }
2926 else
2927 {
2928 char prop_model[PROPERTY_VALUE_MAX];
2929 property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
2930 strncpy(btif_default_local_name, prop_model, max_len);
2931 }
2932 btif_default_local_name[max_len] = '\0';
2933 }
2934 return btif_default_local_name;
2935}