blob: 7ec5948089d1709cc6734463fba6edb72149e900 [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;
127#define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id))
128
129/* This flag will be true if HCI_Inquiry is in progress */
130static BOOLEAN btif_dm_inquiry_in_progress = FALSE;
131
Matthew Xie1e5109b2012-11-09 18:26:26 -0800132/************************************************************************************
133** Static variables
134************************************************************************************/
135static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'};
136
The Android Open Source Project5738f832012-12-12 16:00:35 -0800137/******************************************************************************
138** Static functions
139******************************************************************************/
140static btif_dm_pairing_cb_t pairing_cb;
141static btif_dm_oob_cb_t oob_cb;
142static void btif_dm_generic_evt(UINT16 event, char* p_param);
143static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr);
144static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name);
145static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
146 DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800147#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
148static btif_dm_local_key_cb_t ble_local_key_cb;
149static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif);
150static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl);
151static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req);
152#endif
Matthew Xie1e5109b2012-11-09 18:26:26 -0800153static char* btif_get_default_local_name();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800154/******************************************************************************
155** Externs
156******************************************************************************/
157extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
158extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
159extern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
160extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
Hemant Gupta10256872013-08-19 18:33:01 +0530161extern bt_status_t btif_hf_client_execute_service(BOOLEAN b_enable);
Hemant Gupta2dc99992014-04-18 12:54:08 +0530162extern bt_status_t btif_mce_execute_service(BOOLEAN b_enable);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800163extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700164extern 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 -0800165
166
167/******************************************************************************
168** Functions
169******************************************************************************/
170
171bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
172 BOOLEAN b_enable)
173{
174 /* Check the service_ID and invoke the profile's BT state changed API */
175 switch (service_id)
176 {
177 case BTA_HFP_SERVICE_ID:
178 case BTA_HSP_SERVICE_ID:
179 {
180 btif_hf_execute_service(b_enable);
181 }break;
182 case BTA_A2DP_SERVICE_ID:
183 {
184 btif_av_execute_service(b_enable);
185 }break;
186 case BTA_HID_SERVICE_ID:
187 {
188 btif_hh_execute_service(b_enable);
189 }break;
Hemant Gupta10256872013-08-19 18:33:01 +0530190 case BTA_HFP_HS_SERVICE_ID:
191 {
192 btif_hf_client_execute_service(b_enable);
193 }break;
Hemant Gupta2dc99992014-04-18 12:54:08 +0530194 case BTA_MAP_SERVICE_ID:
195 {
196 btif_mce_execute_service(b_enable);
197 }break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800198 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700199 BTIF_TRACE_ERROR("%s: Unknown service being enabled", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800200 return BT_STATUS_FAIL;
201 }
202 return BT_STATUS_SUCCESS;
203}
204
205/*******************************************************************************
206**
207** Function check_eir_remote_name
208**
209** Description Check if remote name is in the EIR data
210**
211** Returns TRUE if remote name found
212** Populate p_remote_name, if provided and remote name found
213**
214*******************************************************************************/
215static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
216 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
217{
218 UINT8 *p_eir_remote_name = NULL;
219 UINT8 remote_name_len = 0;
220
221 /* Check EIR for remote name and services */
222 if (p_search_data->inq_res.p_eir)
223 {
224 p_eir_remote_name = BTA_CheckEirData(p_search_data->inq_res.p_eir,
225 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
226 if (!p_eir_remote_name)
227 {
228 p_eir_remote_name = BTA_CheckEirData(p_search_data->inq_res.p_eir,
229 BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
230 }
231
232 if (p_eir_remote_name)
233 {
234 if (remote_name_len > BD_NAME_LEN)
235 remote_name_len = BD_NAME_LEN;
236
237 if (p_remote_name && p_remote_name_len)
238 {
239 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
240 *(p_remote_name + remote_name_len) = 0;
241 *p_remote_name_len = remote_name_len;
242 }
243
244 return TRUE;
245 }
246 }
247
248 return FALSE;
249
250}
251
252/*******************************************************************************
253**
254** Function check_cached_remote_name
255**
256** Description Check if remote name is in the NVRAM cache
257**
258** Returns TRUE if remote name found
259** Populate p_remote_name, if provided and remote name found
260**
261*******************************************************************************/
262static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
263 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
264{
265 bt_bdname_t bdname;
266 bt_bdaddr_t remote_bdaddr;
267 bt_property_t prop_name;
268
269 /* check if we already have it in our btif_storage cache */
270 bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
271 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
272 sizeof(bt_bdname_t), &bdname);
273 if (btif_storage_get_remote_device_property(
274 &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
275 {
276 if (p_remote_name && p_remote_name_len)
277 {
278 strcpy((char *)p_remote_name, (char *)bdname.name);
279 *p_remote_name_len = strlen((char *)p_remote_name);
280 }
281 return TRUE;
282 }
283
284 return FALSE;
285}
286
287BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
288{
289 uint32_t remote_cod;
290 bt_property_t prop_name;
291
292 /* check if we already have it in our btif_storage cache */
293 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
294 sizeof(uint32_t), &remote_cod);
295 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
296 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700297 BTIF_TRACE_ERROR("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800298 if ((remote_cod & 0x7ff) == cod)
299 return TRUE;
300 }
301
302 return FALSE;
303}
304
Priti Agheraebb1d752012-11-27 18:03:22 -0800305BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
306{
307 uint32_t remote_cod;
308 bt_property_t prop_name;
309
310 /* check if we already have it in our btif_storage cache */
311 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
312 sizeof(uint32_t), &remote_cod);
313 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
314 &prop_name) == BT_STATUS_SUCCESS)
315 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700316 BTIF_TRACE_DEBUG("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
Priti Agheraebb1d752012-11-27 18:03:22 -0800317 if ((remote_cod & 0x700) == cod)
318 return TRUE;
319 }
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700320 return FALSE;
321}
Priti Agheraebb1d752012-11-27 18:03:22 -0800322
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700323BOOLEAN check_hid_le(const bt_bdaddr_t *remote_bdaddr)
324{
325 uint32_t remote_dev_type;
326 bt_property_t prop_name;
327
328 /* check if we already have it in our btif_storage cache */
329 BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE,
330 sizeof(uint32_t), &remote_dev_type);
331 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
332 &prop_name) == BT_STATUS_SUCCESS)
333 {
334 if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE)
335 {
336 bdstr_t bdstr;
337 bd2str(remote_bdaddr, &bdstr);
338 if(btif_config_exist("Remote", bdstr, "HidAppId"))
339 return TRUE;
340 }
341 }
Priti Agheraebb1d752012-11-27 18:03:22 -0800342 return FALSE;
343}
344
The Android Open Source Project5738f832012-12-12 16:00:35 -0800345static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
346{
347 /* Send bonding state only once - based on outgoing/incoming we may receive duplicates */
348 if ( (pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING) )
349 return;
350
351 if (pairing_cb.is_temp)
352 {
353 state = BT_BOND_STATE_NONE;
354 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700355 BTIF_TRACE_DEBUG("%s: state=%d prev_state=%d", __FUNCTION__, state, pairing_cb.state);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800356
357 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
358
359 if (state == BT_BOND_STATE_BONDING)
360 {
361 pairing_cb.state = state;
362 bdcpy(pairing_cb.bd_addr, bd_addr->address);
363 }
364 else
365 {
366 memset(&pairing_cb, 0, sizeof(pairing_cb));
367 }
368
369}
370
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800371/* store remote version in bt config to always have access
372 to it post pairing*/
373static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
374{
375 bt_property_t property;
376 UINT8 lmp_ver = 0;
377 UINT16 lmp_subver = 0;
378 UINT16 mfct_set = 0;
379 tBTM_STATUS btm_status;
380 bt_remote_version_t info;
381 bt_status_t status;
382 bdstr_t bdstr;
383
384 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
385 &mfct_set, &lmp_subver);
386
387 ALOGD("remote version info [%s]: %x, %x, %x", bd2str(p_bd, &bdstr),
388 lmp_ver, mfct_set, lmp_subver);
389
390 if (btm_status == BTM_SUCCESS)
391 {
392 /* always update cache to ensure we have availability whenever BTM API
393 is not populated */
394 info.manufacturer = mfct_set;
395 info.sub_ver = lmp_subver;
396 info.version = lmp_ver;
397 BTIF_STORAGE_FILL_PROPERTY(&property,
398 BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
399 &info);
400 status = btif_storage_set_remote_device_property(p_bd, &property);
401 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
402 }
403}
404
The Android Open Source Project5738f832012-12-12 16:00:35 -0800405
406static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
407 DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
408{
409 int num_properties = 0;
410 bt_property_t properties[3];
411 bt_bdaddr_t bdaddr;
412 bt_status_t status;
413 UINT32 cod;
414 bt_device_type_t dev_type;
415
416 memset(properties, 0, sizeof(properties));
417 bdcpy(bdaddr.address, bd_addr);
418
419 /* remote name */
420 if (strlen((const char *) bd_name))
421 {
422 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
423 BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
424 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
425 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
426 num_properties++;
427 }
428
429 /* class of device */
430 cod = devclass2uint(dev_class);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700431 BTIF_TRACE_DEBUG("%s():cod is 0x%06x", __FUNCTION__, cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800432 if ( cod == 0) {
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530433 /* Try to retrieve cod from storage */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700434 BTIF_TRACE_DEBUG("%s():cod is 0, checking cod from storage", __FUNCTION__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530435 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
436 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
437 status = btif_storage_get_remote_device_property(&bdaddr, &properties[num_properties]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700438 BTIF_TRACE_DEBUG("%s():cod retreived from storage is 0x%06x", __FUNCTION__, cod);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530439 if ( cod == 0) {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700440 BTIF_TRACE_DEBUG("%s():cod is again 0, set as unclassified", __FUNCTION__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530441 cod = COD_UNCLASSIFIED;
442 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800443 }
444
445 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
446 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
447 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
448 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
449 num_properties++;
450
451 /* device type */
452 dev_type = device_type;
453 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
454 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
455 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
456 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
457 num_properties++;
458
459 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
460 status, &bdaddr, num_properties, properties);
461}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800462
463/*******************************************************************************
464**
465** Function btif_dm_cb_hid_remote_name
466**
467** Description Remote name callback for HID device. Called in btif context
468** Special handling for HID devices
469**
470** Returns void
471**
472*******************************************************************************/
473static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
474{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700475 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 -0800476 if (pairing_cb.state == BT_BOND_STATE_BONDING)
477 {
478 bt_bdaddr_t remote_bd;
479
480 bdcpy(remote_bd.address, pairing_cb.bd_addr);
481
482 if (p_remote_name->status == BTM_SUCCESS)
483 {
484 bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
485 }
486 else
487 bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
488 }
489}
490
The Android Open Source Project5738f832012-12-12 16:00:35 -0800491/*******************************************************************************
492**
493** Function btif_dm_cb_create_bond
494**
495** Description Create bond initiated from the BTIF thread context
496** Special handling for HID devices
497**
498** Returns void
499**
500*******************************************************************************/
501static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr)
502{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800503 BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800504 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800505
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800506#if BLE_INCLUDED == TRUE
507 int device_type;
508 int addr_type;
509 bdstr_t bdstr;
510 bd2str(bd_addr, &bdstr);
511 if(btif_config_get_int("Remote", (char const *)&bdstr,"DevType", &device_type) &&
512 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
513 (device_type == BT_DEVICE_TYPE_BLE))
514 {
515 BTA_DmAddBleDevice(bd_addr->address, addr_type, BT_DEVICE_TYPE_BLE);
516 }
517#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800518
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800519#if BLE_INCLUDED == TRUE
520 if(is_hid && device_type != BT_DEVICE_TYPE_BLE)
521#else
522 if(is_hid)
523#endif
524 {
525 int status;
526 status = btif_hh_connect(bd_addr);
527 if(status != BT_STATUS_SUCCESS)
528 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800529 }
530 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800531 {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800532 BTA_DmBond ((UINT8 *)bd_addr->address);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800533 }
534 /* Track originator of bond creation */
535 pairing_cb.is_local_initiated = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800536
537}
538
539/*******************************************************************************
540**
541** Function btif_dm_cb_remove_bond
542**
543** Description remove bond initiated from the BTIF thread context
544** Special handling for HID devices
545**
546** Returns void
547**
548*******************************************************************************/
549void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
550{
551 bdstr_t bdstr;
552 /*special handling for HID devices */
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700553 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there
554 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
555#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
556 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
557#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800558 {
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700559 BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800560 }
561}
562
563/*******************************************************************************
564**
Andre Eisenbach249f6002014-06-18 12:20:37 -0700565** Function btif_dm_get_connection_state
566**
567** Description Returns whether the remote device is currently connected
568**
569** Returns 0 if not connected
570**
571*******************************************************************************/
572uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr)
573{
574 return BTA_DmGetConnectionState((UINT8 *)bd_addr->address);
575}
576
577/*******************************************************************************
578**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800579** Function search_devices_copy_cb
580**
581** Description Deep copy callback for search devices event
582**
583** Returns void
584**
585*******************************************************************************/
586static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
587{
588 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
589 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
590
591 if (!p_src)
592 return;
593
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700594 BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800595 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
596 switch (event)
597 {
598 case BTA_DM_INQ_RES_EVT:
599 {
600 if (p_src_data->inq_res.p_eir)
601 {
602 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
603 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
604 }
605 }
606 break;
607
608 case BTA_DM_DISC_RES_EVT:
609 {
610 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
611 {
612 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
613 memcpy(p_dest_data->disc_res.p_raw_data,
614 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
615 }
616 }
617 break;
618 }
619}
620
621static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
622{
623 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
624 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
625
626 if (!p_src)
627 return;
628 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
629 switch (event)
630 {
631 case BTA_DM_DISC_RES_EVT:
632 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530633 if (p_src_data->disc_res.result == BTA_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800634 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530635 if (p_src_data->disc_res.num_uuids > 0)
636 {
637 p_dest_data->disc_res.p_uuid_list =
638 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
639 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
640 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
641 GKI_freebuf(p_src_data->disc_res.p_uuid_list);
642 }
643 if (p_src_data->disc_res.p_raw_data != NULL)
644 {
645 GKI_freebuf(p_src_data->disc_res.p_raw_data);
646 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800647 }
648 } break;
649 }
650}
651/******************************************************************************
652**
653** BTIF DM callback events
654**
655*****************************************************************************/
656
657/*******************************************************************************
658**
659** Function btif_dm_pin_req_evt
660**
661** Description Executes pin request event in btif context
662**
663** Returns void
664**
665*******************************************************************************/
666static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
667{
668 bt_bdaddr_t bd_addr;
669 bt_bdname_t bd_name;
670 UINT32 cod;
671 bt_pin_code_t pin_code;
672
673 /* Remote properties update */
674 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
675 p_pin_req->dev_class, BT_DEVICE_TYPE_BREDR);
676
677 bdcpy(bd_addr.address, p_pin_req->bd_addr);
678 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
679
680 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
681
682 cod = devclass2uint(p_pin_req->dev_class);
683
684 if ( cod == 0) {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700685 BTIF_TRACE_DEBUG("%s():cod is 0, set as unclassified", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800686 cod = COD_UNCLASSIFIED;
687 }
688
689 /* check for auto pair possiblity only if bond was initiated by local device */
690 if (pairing_cb.is_local_initiated)
691 {
692 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
693 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
694 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
695 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
696 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
697 check_cod(&bd_addr, COD_HID_POINTING))
698 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700699 BTIF_TRACE_DEBUG("%s()cod matches for auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800700 /* Check if this device can be auto paired */
701 if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) &&
702 (pairing_cb.autopair_attempts == 0))
703 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700704 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800705 pin_code.pin[0] = 0x30;
706 pin_code.pin[1] = 0x30;
707 pin_code.pin[2] = 0x30;
708 pin_code.pin[3] = 0x30;
709
710 pairing_cb.autopair_attempts++;
711 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
712 return;
713 }
714 }
715 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
716 check_cod(&bd_addr, COD_HID_COMBO))
717 {
718 if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) &&
719 (pairing_cb.autopair_attempts == 0))
720 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700721 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800722 pin_code.pin[0] = 0x30;
723 pin_code.pin[1] = 0x30;
724 pin_code.pin[2] = 0x30;
725 pin_code.pin[3] = 0x30;
726
727 pairing_cb.autopair_attempts++;
728 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
729 return;
730 }
731 }
732 }
733 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
734 &bd_addr, &bd_name, cod);
735}
736
737/*******************************************************************************
738**
739** Function btif_dm_ssp_cfm_req_evt
740**
741** Description Executes SSP confirm request event in btif context
742**
743** Returns void
744**
745*******************************************************************************/
746static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
747{
748 bt_bdaddr_t bd_addr;
749 bt_bdname_t bd_name;
750 UINT32 cod;
751 BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
752
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700753 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800754
755 /* Remote properties update */
756 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
757 p_ssp_cfm_req->dev_class, BT_DEVICE_TYPE_BREDR);
758
759 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
760 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
761
762 /* Set the pairing_cb based on the local & remote authentication requirements */
763 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
764
765 /* if just_works and bonding bit is not set treat this as temporary */
766 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 -0800767 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
768 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800769 pairing_cb.is_temp = TRUE;
770 else
771 pairing_cb.is_temp = FALSE;
772
773 pairing_cb.is_ssp = TRUE;
774
775 /* If JustWorks auto-accept */
776 if (p_ssp_cfm_req->just_works)
777 {
778 /* Pairing consent for JustWorks needed if:
779 * 1. Incoming pairing is detected AND
780 * 2. local IO capabilities are DisplayYesNo AND
781 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
782 */
783 if ((is_incoming) && ((p_ssp_cfm_req->loc_io_caps == 0x01) &&
784 (p_ssp_cfm_req->rmt_io_caps == 0x00 || p_ssp_cfm_req->rmt_io_caps == 0x03)))
785 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700786 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 -0800787 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
788 }
789 else
790 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700791 BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800792 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
793 return;
794 }
795 }
796
797 cod = devclass2uint(p_ssp_cfm_req->dev_class);
798
799 if ( cod == 0) {
800 ALOGD("cod is 0, set as unclassified");
801 cod = COD_UNCLASSIFIED;
802 }
803
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700804 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800805 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
806 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
807 p_ssp_cfm_req->num_val);
808}
809
810static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
811{
812 bt_bdaddr_t bd_addr;
813 bt_bdname_t bd_name;
814 UINT32 cod;
815
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700816 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800817
818 /* Remote properties update */
819 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
820 p_ssp_key_notif->dev_class, BT_DEVICE_TYPE_BREDR);
821
822 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
823 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
824
825 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
826 pairing_cb.is_ssp = TRUE;
827 cod = devclass2uint(p_ssp_key_notif->dev_class);
828
829 if ( cod == 0) {
830 ALOGD("cod is 0, set as unclassified");
831 cod = COD_UNCLASSIFIED;
832 }
833
834 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
835 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
836 p_ssp_key_notif->passkey);
837}
838/*******************************************************************************
839**
840** Function btif_dm_auth_cmpl_evt
841**
842** Description Executes authentication complete event in btif context
843**
844** Returns void
845**
846*******************************************************************************/
847static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
848{
849 /* Save link key, if not temporary */
850 bt_bdaddr_t bd_addr;
851 bt_status_t status = BT_STATUS_FAIL;
852 bt_bond_state_t state = BT_BOND_STATE_NONE;
853
854 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
855 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
856 {
857 if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) || (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
858 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) || (!pairing_cb.is_temp))
859 {
860 bt_status_t ret;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700861 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, is_temp=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800862 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.is_temp);
863 ret = btif_storage_add_bonded_device(&bd_addr,
864 p_auth_cmpl->key, p_auth_cmpl->key_type,
865 pairing_cb.pin_code_len);
866 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
867 }
868 else
869 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700870 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 -0800871 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.is_temp);
Hemant Guptab820aec2013-12-24 19:59:57 +0530872 if(pairing_cb.is_temp)
873 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700874 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
Hemant Guptab820aec2013-12-24 19:59:57 +0530875 __FUNCTION__);
876 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
877 return;
878 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800879 }
880 }
881 if (p_auth_cmpl->success)
882 {
883 status = BT_STATUS_SUCCESS;
884 state = BT_BOND_STATE_BONDED;
885
886 /* Trigger SDP on the device */
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700887 pairing_cb.sdp_attempts = 1;;
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -0800888
889 if(btif_dm_inquiry_in_progress)
890 btif_dm_cancel_discovery();
891
The Android Open Source Project5738f832012-12-12 16:00:35 -0800892 btif_dm_get_remote_services(&bd_addr);
893 /* Do not call bond_state_changed_cb yet. Wait till fetch remote service is complete */
894 }
895 else
896 {
897 /*Map the HCI fail reason to bt status */
898 switch(p_auth_cmpl->fail_reason)
899 {
900 case HCI_ERR_PAGE_TIMEOUT:
901 case HCI_ERR_CONNECTION_TOUT:
902 status = BT_STATUS_RMT_DEV_DOWN;
903 break;
904
Hemant Guptaaef7a672013-07-31 19:00:12 +0530905 case HCI_ERR_PAIRING_NOT_ALLOWED:
906 status = BT_STATUS_AUTH_REJECTED;
907 break;
908
Hemant Guptab4801442014-01-07 18:11:15 +0530909 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
910 status = BT_STATUS_AUTH_FAILURE;
911 break;
912
The Android Open Source Project5738f832012-12-12 16:00:35 -0800913 /* map the auth failure codes, so we can retry pairing if necessary */
914 case HCI_ERR_AUTH_FAILURE:
Hemant Gupta59a88ec2014-03-19 19:01:35 +0530915 case HCI_ERR_KEY_MISSING:
Zhihai Xua7ea8092013-11-27 14:10:53 +0530916 btif_storage_remove_bonded_device(&bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800917 case HCI_ERR_HOST_REJECT_SECURITY:
918 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
919 case HCI_ERR_UNIT_KEY_USED:
920 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
921 case HCI_ERR_INSUFFCIENT_SECURITY:
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530922 case HCI_ERR_PEER_USER:
923 case HCI_ERR_UNSPECIFIED:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700924 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d",
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530925 __FUNCTION__, p_auth_cmpl->fail_reason);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800926 if (pairing_cb.autopair_attempts == 1)
927 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700928 BTIF_TRACE_DEBUG("%s(): Adding device to blacklist ", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800929
930 /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class */
931 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
932 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
933 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
934 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
935 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
936 check_cod(&bd_addr, COD_HID_POINTING))
937 {
938 btif_storage_add_device_to_autopair_blacklist (&bd_addr);
939 }
940 pairing_cb.autopair_attempts++;
941
942 /* Create the Bond once again */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700943 BTIF_TRACE_DEBUG("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800944 btif_dm_cb_create_bond (&bd_addr);
945 return;
946 }
947 else
948 {
949 /* if autopair attempts are more than 1, or not attempted */
950 status = BT_STATUS_AUTH_FAILURE;
951 }
952 break;
953
954 default:
955 status = BT_STATUS_FAIL;
956 }
Zhihai Xu8d2128d2013-12-13 16:09:21 +0530957 /* Special Handling for HID Devices */
958 if (check_cod(&bd_addr, COD_HID_POINTING)) {
959 /* Remove Device as bonded in nvram as authentication failed */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700960 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __FUNCTION__);
Zhihai Xu8d2128d2013-12-13 16:09:21 +0530961 btif_storage_remove_bonded_device(&bd_addr);
962 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800963 bond_state_changed(status, &bd_addr, state);
964 }
965}
966
967/******************************************************************************
968**
969** Function btif_dm_search_devices_evt
970**
971** Description Executes search devices callback events in btif context
972**
973** Returns void
974**
975******************************************************************************/
976static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
977{
978 tBTA_DM_SEARCH *p_search_data;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700979 BTIF_TRACE_EVENT("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800980
981 switch (event)
982 {
983 case BTA_DM_DISC_RES_EVT:
984 {
985 p_search_data = (tBTA_DM_SEARCH *)p_param;
986 /* Remote name update */
987 if (strlen((const char *) p_search_data->disc_res.bd_name))
988 {
989 bt_property_t properties[1];
990 bt_bdaddr_t bdaddr;
991 bt_status_t status;
992
993 properties[0].type = BT_PROPERTY_BDNAME;
994 properties[0].val = p_search_data->disc_res.bd_name;
995 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
996 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
997
998 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
999 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
1000 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1001 status, &bdaddr, 1, properties);
1002 }
1003 /* TODO: Services? */
1004 }
1005 break;
1006
1007 case BTA_DM_INQ_RES_EVT:
1008 {
1009 /* inquiry result */
1010 UINT32 cod;
1011 UINT8 *p_eir_remote_name = NULL;
1012 bt_bdname_t bdname;
1013 bt_bdaddr_t bdaddr;
1014 UINT8 remote_name_len;
1015 UINT8 *p_cached_name = NULL;
1016 tBTA_SERVICE_MASK services = 0;
1017 bdstr_t bdstr;
1018
1019 p_search_data = (tBTA_DM_SEARCH *)p_param;
1020 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
1021
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001022 BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __FUNCTION__, bd2str(&bdaddr, &bdstr),
The Android Open Source Project5738f832012-12-12 16:00:35 -08001023#if (BLE_INCLUDED == TRUE)
1024 p_search_data->inq_res.device_type);
1025#else
1026 BT_DEVICE_TYPE_BREDR);
1027#endif
1028 bdname.name[0] = 0;
1029
1030 cod = devclass2uint (p_search_data->inq_res.dev_class);
1031
1032 if ( cod == 0) {
1033 ALOGD("cod is 0, set as unclassified");
1034 cod = COD_UNCLASSIFIED;
1035 }
1036
1037 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1038 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1039
1040 /* Check EIR for remote name and services */
1041 if (p_search_data->inq_res.p_eir)
1042 {
1043 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001044 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001045 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */
1046 }
1047
1048
1049 {
1050 bt_property_t properties[5];
1051 bt_device_type_t dev_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001052 UINT8 addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001053 uint32_t num_properties = 0;
1054 bt_status_t status;
1055
1056 memset(properties, 0, sizeof(properties));
1057 /* BD_ADDR */
1058 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1059 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1060 num_properties++;
1061 /* BD_NAME */
1062 /* Don't send BDNAME if it is empty */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001063 if (bdname.name[0])
1064 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001065 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1066 BT_PROPERTY_BDNAME,
1067 strlen((char *)bdname.name), &bdname);
1068 num_properties++;
1069 }
1070
1071 /* DEV_CLASS */
1072 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1073 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1074 num_properties++;
1075 /* DEV_TYPE */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001076#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001077 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1078 dev_type = p_search_data->inq_res.device_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001079 addr_type = p_search_data->inq_res.ble_addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001080#else
1081 dev_type = BT_DEVICE_TYPE_BREDR;
1082#endif
1083 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1084 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1085 num_properties++;
1086 /* RSSI */
1087 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1088 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1089 &(p_search_data->inq_res.rssi));
1090 num_properties++;
1091
1092 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1093 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001094#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1095 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001096 if (( dev_type == BT_DEVICE_TYPE_DUMO)&&
1097 (p_search_data->inq_res.flag & BTA_BLE_DMT_CONTROLLER_SPT) &&
1098 (p_search_data->inq_res.flag & BTA_BLE_DMT_HOST_SPT))
1099 {
1100 btif_storage_set_dmt_support_type (&bdaddr, TRUE);
1101 }
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001102 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1103#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001104 /* Callback to notify upper layer of device */
1105 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1106 num_properties, properties);
1107 }
1108 }
1109 break;
1110
1111 case BTA_DM_INQ_CMPL_EVT:
1112 {
1113 }
1114 break;
1115 case BTA_DM_DISC_CMPL_EVT:
1116 {
1117 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1118 }
1119 break;
1120 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1121 {
1122 /* if inquiry is not in progress and we get a cancel event, then
1123 * it means we are done with inquiry, but remote_name fetches are in
1124 * progress
1125 *
1126 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt
1127 * but instead wait for the cancel_cmpl_evt via the Busy Level
1128 *
1129 */
1130 if (btif_dm_inquiry_in_progress == FALSE)
1131 {
1132 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1133 }
1134 }
1135 break;
1136 }
1137}
1138
1139/*******************************************************************************
1140**
1141** Function btif_dm_search_services_evt
1142**
1143** Description Executes search services event in btif context
1144**
1145** Returns void
1146**
1147*******************************************************************************/
1148static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1149{
1150 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1151
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001152 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001153 switch (event)
1154 {
1155 case BTA_DM_DISC_RES_EVT:
1156 {
1157 bt_uuid_t uuid_arr[BT_MAX_NUM_UUIDS]; /* Max 32 services */
1158 bt_property_t prop;
1159 uint32_t i = 0, j = 0;
1160 bt_bdaddr_t bd_addr;
1161 bt_status_t ret;
1162
1163 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1164
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001165 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001166 p_data->disc_res.result, p_data->disc_res.services);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001167 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1168 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1169 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1170 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001171 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001172 pairing_cb.sdp_attempts++;
1173 btif_dm_get_remote_services(&bd_addr);
1174 return;
1175 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001176 prop.type = BT_PROPERTY_UUIDS;
1177 prop.len = 0;
1178 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1179 {
1180 prop.val = p_data->disc_res.p_uuid_list;
1181 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1182 for (i=0; i < p_data->disc_res.num_uuids; i++)
1183 {
1184 char temp[256];
1185 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 -07001186 BTIF_TRACE_ERROR("Index: %d uuid:%s", i, temp);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001187 }
1188 }
1189
1190 /* onUuidChanged requires getBondedDevices to be populated.
1191 ** bond_state_changed needs to be sent prior to remote_device_property
1192 */
1193 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1194 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0)&&
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001195 pairing_cb.sdp_attempts > 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001196 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001197 BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001198 __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001199 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001200 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1201 }
1202
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001203 if(p_data->disc_res.num_uuids != 0)
1204 {
1205 /* Also write this to the NVRAM */
1206 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1207 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1208 /* Send the event to the BTIF */
1209 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1210 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1211 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001212 }
1213 break;
1214
1215 case BTA_DM_DISC_CMPL_EVT:
1216 /* fixme */
1217 break;
1218
Matthew Xie607e3b72013-08-15 19:30:48 -07001219#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001220 case BTA_DM_DISC_BLE_RES_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001221 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__,
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001222 p_data->disc_ble_res.service.uu.uuid16);
1223 bt_uuid_t uuid;
1224 int i = 0;
1225 int j = 15;
1226 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1227 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001228 BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001229 bt_property_t prop;
1230 bt_bdaddr_t bd_addr;
1231 char temp[256];
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001232 bt_status_t ret;
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001233
1234 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1235
1236 while(i < j )
1237 {
1238 unsigned char c = uuid.uu[j];
1239 uuid.uu[j] = uuid.uu[i];
1240 uuid.uu[i] = c;
1241 i++;
1242 j--;
1243 }
1244
1245 uuid_to_string(&uuid, temp);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001246 BTIF_TRACE_ERROR(" uuid:%s", temp);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001247
1248 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1249 prop.type = BT_PROPERTY_UUIDS;
1250 prop.val = uuid.uu;
1251 prop.len = MAX_UUID_SIZE;
1252
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001253 /* Also write this to the NVRAM */
1254 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1255 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1256
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001257 /* Send the event to the BTIF */
1258 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1259 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1260
1261 }
1262 break;
Matthew Xie607e3b72013-08-15 19:30:48 -07001263#endif /* BLE_INCLUDED */
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001264
The Android Open Source Project5738f832012-12-12 16:00:35 -08001265 default:
1266 {
1267 ASSERTC(0, "unhandled search services event", event);
1268 }
1269 break;
1270 }
1271}
1272
1273/*******************************************************************************
1274**
1275** Function btif_dm_remote_service_record_evt
1276**
1277** Description Executes search service record event in btif context
1278**
1279** Returns void
1280**
1281*******************************************************************************/
1282static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1283{
1284 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1285
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001286 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001287 switch (event)
1288 {
1289 case BTA_DM_DISC_RES_EVT:
1290 {
1291 bt_service_record_t rec;
1292 bt_property_t prop;
1293 uint32_t i = 0;
1294 bt_bdaddr_t bd_addr;
1295
1296 memset(&rec, 0, sizeof(bt_service_record_t));
1297 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1298
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001299 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001300 p_data->disc_res.result, p_data->disc_res.services);
1301 prop.type = BT_PROPERTY_SERVICE_RECORD;
1302 prop.val = (void*)&rec;
1303 prop.len = sizeof(rec);
1304
1305 /* disc_res.result is overloaded with SCN. Cannot check result */
1306 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1307 /* TODO: Get the UUID as well */
1308 rec.channel = p_data->disc_res.result - 3;
1309 /* TODO: Need to get the service name using p_raw_data */
1310 rec.name[0] = 0;
1311
1312 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1313 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1314 }
1315 break;
1316
1317 default:
1318 {
1319 ASSERTC(0, "unhandled remote service record event", event);
1320 }
1321 break;
1322 }
1323}
1324
1325/*******************************************************************************
1326**
1327** Function btif_dm_upstreams_cback
1328**
1329** Description Executes UPSTREAMS events in btif context
1330**
1331** Returns void
1332**
1333*******************************************************************************/
1334static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1335{
1336 tBTA_DM_SEC_EVT dm_event = (tBTA_DM_SEC_EVT)event;
1337 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1338 tBTA_SERVICE_MASK service_mask;
1339 uint32_t i;
1340 bt_bdaddr_t bd_addr;
1341
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001342 BTIF_TRACE_EVENT("btif_dm_upstreams_cback ev: %s", dump_dm_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001343
1344 switch (event)
1345 {
1346 case BTA_DM_ENABLE_EVT:
1347 {
1348 BD_NAME bdname;
1349 bt_status_t status;
1350 bt_property_t prop;
1351 prop.type = BT_PROPERTY_BDNAME;
1352 prop.len = BD_NAME_LEN;
1353 prop.val = (void*)bdname;
1354
1355 status = btif_storage_get_adapter_property(&prop);
Matthew Xie1e5109b2012-11-09 18:26:26 -08001356 if (status == BT_STATUS_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001357 {
1358 /* A name exists in the storage. Make this the device name */
1359 BTA_DmSetDeviceName((char*)prop.val);
1360 }
Matthew Xie1e5109b2012-11-09 18:26:26 -08001361 else
1362 {
1363 /* Storage does not have a name yet.
1364 * Use the default name and write it to the chip
1365 */
1366 BTA_DmSetDeviceName(btif_get_default_local_name());
1367 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001368
1369 /* for each of the enabled services in the mask, trigger the profile
1370 * enable */
1371 service_mask = btif_get_enabled_services_mask();
1372 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1373 {
1374 if (service_mask &
1375 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1376 {
1377 btif_in_execute_service_request(i, TRUE);
1378 }
1379 }
1380 /* clear control blocks */
1381 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1382
1383 /* This function will also trigger the adapter_properties_cb
1384 ** and bonded_devices_info_cb
1385 */
1386 btif_storage_load_bonded_devices();
1387
1388 btif_storage_load_autopair_device_list();
1389
1390 btif_enable_bluetooth_evt(p_data->enable.status, p_data->enable.bd_addr);
Satya Calloji0943c102014-05-12 09:13:02 -07001391
Wei Wangbf0e4b22014-05-19 23:23:35 -07001392 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Calloji0943c102014-05-12 09:13:02 -07001393 /* Enable local privacy */
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001394 /*TODO Should this call be exposed to JAVA...? */
Satya Calloji0943c102014-05-12 09:13:02 -07001395 BTA_DmBleConfigLocalPrivacy(TRUE);
Wei Wangbf0e4b22014-05-19 23:23:35 -07001396 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001397 }
1398 break;
1399
1400 case BTA_DM_DISABLE_EVT:
1401 /* for each of the enabled services in the mask, trigger the profile
1402 * disable */
1403 service_mask = btif_get_enabled_services_mask();
1404 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1405 {
1406 if (service_mask &
1407 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1408 {
1409 btif_in_execute_service_request(i, FALSE);
1410 }
1411 }
1412 btif_disable_bluetooth_evt();
1413 break;
1414
1415 case BTA_DM_PIN_REQ_EVT:
1416 btif_dm_pin_req_evt(&p_data->pin_req);
1417 break;
1418
1419 case BTA_DM_AUTH_CMPL_EVT:
1420 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1421 break;
1422
1423 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1424 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1425 {
1426 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1427 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1428 }
1429 break;
1430
1431 case BTA_DM_SP_CFM_REQ_EVT:
1432 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1433 break;
1434 case BTA_DM_SP_KEY_NOTIF_EVT:
1435 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1436 break;
1437
1438 case BTA_DM_DEV_UNPAIRED_EVT:
1439 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1440
1441 /*special handling for HID devices */
1442 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -07001443 btif_hh_remove_device(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001444 #endif
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001445 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1446 btif_storage_remove_ble_bonding_keys(&bd_addr);
1447 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001448 btif_storage_remove_bonded_device(&bd_addr);
1449 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1450 break;
1451
1452 case BTA_DM_BUSY_LEVEL_EVT:
1453 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001454
1455 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001456 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001457 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001458 {
1459 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1460 BT_DISCOVERY_STARTED);
1461 btif_dm_inquiry_in_progress = TRUE;
1462 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001463 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001464 {
1465 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1466 BT_DISCOVERY_STOPPED);
1467 btif_dm_inquiry_in_progress = FALSE;
1468 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001469 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001470 {
1471 btif_dm_inquiry_in_progress = FALSE;
1472 }
1473 }
1474 }break;
1475
1476 case BTA_DM_LINK_UP_EVT:
1477 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001478 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001479
1480 btif_update_remote_version_property(&bd_addr);
1481
The Android Open Source Project5738f832012-12-12 16:00:35 -08001482 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1483 &bd_addr, BT_ACL_STATE_CONNECTED);
1484 break;
1485
1486 case BTA_DM_LINK_DOWN_EVT:
1487 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001488 BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001489 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1490 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1491 break;
1492
1493 case BTA_DM_HW_ERROR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001494 BTIF_TRACE_ERROR("Received H/W Error. ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001495 /* Flush storage data */
1496 btif_config_flush();
1497 usleep(100000); /* 100milliseconds */
1498 /* Killing the process to force a restart as part of fault tolerance */
1499 kill(getpid(), SIGKILL);
1500 break;
1501
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001502#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1503 case BTA_DM_BLE_KEY_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001504 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 -08001505
1506 /* If this pairing is by-product of local initiated GATT client Read or Write,
1507 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1508 have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1509 if (pairing_cb.state != BT_BOND_STATE_BONDING)
1510 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001511 BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001512 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1513 BT_BOND_STATE_BONDING);
1514 }
1515 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1516 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001517 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001518 break;
1519 }
1520
1521 switch (p_data->ble_key.key_type)
1522 {
1523 case BTA_LE_KEY_PENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001524 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001525 pairing_cb.ble.is_penc_key_rcvd = TRUE;
1526 memcpy(pairing_cb.ble.penc_key.ltk,p_data->ble_key.key_value.penc_key.ltk, 16);
1527 memcpy(pairing_cb.ble.penc_key.rand, p_data->ble_key.key_value.penc_key.rand,8);
1528 pairing_cb.ble.penc_key.ediv = p_data->ble_key.key_value.penc_key.ediv;
1529 pairing_cb.ble.penc_key.sec_level = p_data->ble_key.key_value.penc_key.sec_level;
1530
1531 for (i=0; i<16; i++)
1532 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001533 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 -08001534 }
1535 for (i=0; i<8; i++)
1536 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001537 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 -08001538 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001539 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.ediv=0x%04x",pairing_cb.ble.penc_key.ediv);
1540 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.sec_level=0x%02x",pairing_cb.ble.penc_key.sec_level);
1541 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 -08001542 break;
1543
1544 case BTA_LE_KEY_PID:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001545 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001546 pairing_cb.ble.is_pid_key_rcvd = TRUE;
1547 memcpy(pairing_cb.ble.pid_key, p_data->ble_key.key_value.pid_key.irk, 16);
1548 for (i=0; i<16; i++)
1549 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001550 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 -08001551 }
1552 break;
1553
1554 case BTA_LE_KEY_PCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001555 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001556 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1557 pairing_cb.ble.pcsrk_key.counter = p_data->ble_key.key_value.pcsrk_key.counter;
1558 pairing_cb.ble.pcsrk_key.sec_level = p_data->ble_key.key_value.pcsrk_key.sec_level;
1559 memcpy(pairing_cb.ble.pcsrk_key.csrk,p_data->ble_key.key_value.pcsrk_key.csrk,16);
1560
1561 for (i=0; i<16; i++)
1562 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001563 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 -08001564 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001565 BTIF_TRACE_DEBUG("pairing_cb.ble.pcsrk_key.counter=0x%08x",pairing_cb.ble.pcsrk_key.counter);
1566 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 -08001567 break;
1568
1569 case BTA_LE_KEY_LENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001570 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001571 pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1572 pairing_cb.ble.lenc_key.div = p_data->ble_key.key_value.lenc_key.div;
1573 pairing_cb.ble.lenc_key.key_size = p_data->ble_key.key_value.lenc_key.key_size;
1574 pairing_cb.ble.lenc_key.sec_level = p_data->ble_key.key_value.lenc_key.sec_level;
1575
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001576 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.div=0x%04x",pairing_cb.ble.lenc_key.div);
1577 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.key_size=0x%02x",pairing_cb.ble.lenc_key.key_size);
1578 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 -08001579 break;
1580
1581
1582
1583 case BTA_LE_KEY_LCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001584 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001585 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1586 pairing_cb.ble.lcsrk_key.counter = p_data->ble_key.key_value.lcsrk_key.counter;
1587 pairing_cb.ble.lcsrk_key.div = p_data->ble_key.key_value.lcsrk_key.div;
1588 pairing_cb.ble.lcsrk_key.sec_level = p_data->ble_key.key_value.lcsrk_key.sec_level;
1589
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001590 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.div=0x%04x",pairing_cb.ble.lcsrk_key.div);
1591 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.counter=0x%08x",pairing_cb.ble.lcsrk_key.counter);
1592 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 -08001593
1594 break;
1595
1596 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001597 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001598 break;
1599 }
1600
1601 break;
1602 case BTA_DM_BLE_SEC_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001603 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001604 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1605 break;
1606 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001607 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001608 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1609 break;
1610 case BTA_DM_BLE_PASSKEY_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001611 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001612 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1613 break;
1614 case BTA_DM_BLE_OOB_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001615 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001616 break;
1617 case BTA_DM_BLE_LOCAL_IR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001618 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001619 ble_local_key_cb.is_id_keys_rcvd = TRUE;
1620 memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1621 memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1622 memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1623 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1624 BTIF_DM_LE_LOCAL_KEY_IR,
1625 BT_OCTET16_LEN);
1626 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1627 BTIF_DM_LE_LOCAL_KEY_IRK,
1628 BT_OCTET16_LEN);
1629 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1630 BTIF_DM_LE_LOCAL_KEY_DHK,
1631 BT_OCTET16_LEN);
1632 break;
1633 case BTA_DM_BLE_LOCAL_ER_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001634 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001635 ble_local_key_cb.is_er_rcvd = TRUE;
1636 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1637 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1638 BTIF_DM_LE_LOCAL_KEY_ER,
1639 BT_OCTET16_LEN);
1640 break;
1641
1642 case BTA_DM_BLE_AUTH_CMPL_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001643 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001644 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1645 break;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001646
1647 case BTA_DM_LE_FEATURES_READ:
1648 {
1649 tBTM_BLE_VSC_CB cmn_vsc_cb;
1650 bt_local_le_features_t local_le_features;
1651 char buf[512];
1652 bt_property_t prop;
1653 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1654 prop.val = (void*)buf;
1655 prop.len = sizeof(buf);
1656
1657 /* LE features are not stored in storage. Should be retrived from stack */
1658 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1659 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1660
1661 prop.len = sizeof (bt_local_le_features_t);
1662 if (cmn_vsc_cb.filter_support == 1)
1663 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1664 else
1665 local_le_features.max_adv_filter_supported = 0;
1666 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1667 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1668 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
1669 local_le_features.scan_result_storage_size = cmn_vsc_cb.tot_scan_results_strg;
1670 memcpy(prop.val, &local_le_features, prop.len);
1671 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
1672 break;
1673 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001674#endif
1675
The Android Open Source Project5738f832012-12-12 16:00:35 -08001676 case BTA_DM_AUTHORIZE_EVT:
1677 case BTA_DM_SIG_STRENGTH_EVT:
1678 case BTA_DM_SP_RMT_OOB_EVT:
1679 case BTA_DM_SP_KEYPRESS_EVT:
1680 case BTA_DM_ROLE_CHG_EVT:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001681
The Android Open Source Project5738f832012-12-12 16:00:35 -08001682 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001683 BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001684 break;
1685 }
1686} /* btui_security_cback() */
1687
1688
1689/*******************************************************************************
1690**
1691** Function btif_dm_generic_evt
1692**
1693** Description Executes non-BTA upstream events in BTIF context
1694**
1695** Returns void
1696**
1697*******************************************************************************/
1698static void btif_dm_generic_evt(UINT16 event, char* p_param)
1699{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001700 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001701 switch(event)
1702 {
1703 case BTIF_DM_CB_DISCOVERY_STARTED:
1704 {
1705 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
1706 }
1707 break;
1708
1709 case BTIF_DM_CB_CREATE_BOND:
1710 {
1711 btif_dm_cb_create_bond((bt_bdaddr_t *)p_param);
1712 }
1713 break;
1714
1715 case BTIF_DM_CB_REMOVE_BOND:
1716 {
1717 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
1718 }
1719 break;
1720
1721 case BTIF_DM_CB_HID_REMOTE_NAME:
1722 {
1723 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
1724 }
1725 break;
1726
1727 case BTIF_DM_CB_BOND_STATE_BONDING:
1728 {
1729 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
1730 }
1731 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001732 case BTIF_DM_CB_LE_TX_TEST:
1733 case BTIF_DM_CB_LE_RX_TEST:
1734 {
1735 uint8_t status;
1736 STREAM_TO_UINT8(status, p_param);
1737 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1738 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
1739 }
1740 break;
1741 case BTIF_DM_CB_LE_TEST_END:
1742 {
1743 uint8_t status;
1744 uint16_t count = 0;
1745 STREAM_TO_UINT8(status, p_param);
1746 if (status == 0)
1747 STREAM_TO_UINT16(count, p_param);
1748 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1749 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
1750 }
1751 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001752 default:
1753 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001754 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001755 }
1756 break;
1757 }
1758}
1759
1760/*******************************************************************************
1761**
1762** Function bte_dm_evt
1763**
1764** Description Switches context from BTE to BTIF for all DM events
1765**
1766** Returns void
1767**
1768*******************************************************************************/
1769
1770void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
1771{
1772 bt_status_t status;
1773
1774 /* switch context to btif task context (copy full union size for convenience) */
1775 status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event, (void*)p_data, sizeof(tBTA_DM_SEC), NULL);
1776
1777 /* catch any failed context transfers */
1778 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
1779}
1780
1781/*******************************************************************************
1782**
1783** Function bte_search_devices_evt
1784**
1785** Description Switches context from BTE to BTIF for DM search events
1786**
1787** Returns void
1788**
1789*******************************************************************************/
1790static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1791{
1792 UINT16 param_len = 0;
1793
1794 if (p_data)
1795 param_len += sizeof(tBTA_DM_SEARCH);
1796 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
1797 switch (event)
1798 {
1799 case BTA_DM_INQ_RES_EVT:
1800 {
1801 if (p_data->inq_res.p_eir)
1802 param_len += HCI_EXT_INQ_RESPONSE_LEN;
1803 }
1804 break;
1805
1806 case BTA_DM_DISC_RES_EVT:
1807 {
1808 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
1809 param_len += p_data->disc_res.raw_data_size;
1810 }
1811 break;
1812 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001813 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 -08001814
1815 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
1816 if (event == BTA_DM_INQ_RES_EVT)
1817 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
1818
1819 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
1820 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
1821}
1822
1823/*******************************************************************************
1824**
1825** Function bte_dm_search_services_evt
1826**
1827** Description Switches context from BTE to BTIF for DM search services
1828** event
1829**
1830** Returns void
1831**
1832*******************************************************************************/
1833static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1834{
1835 UINT16 param_len = 0;
1836 if (p_data)
1837 param_len += sizeof(tBTA_DM_SEARCH);
1838 switch (event)
1839 {
1840 case BTA_DM_DISC_RES_EVT:
1841 {
1842 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
1843 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
1844 }
1845 } break;
1846 }
1847 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
1848 * if raw_data is needed. */
1849 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
1850 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
1851}
1852
1853/*******************************************************************************
1854**
1855** Function bte_dm_remote_service_record_evt
1856**
1857** Description Switches context from BTE to BTIF for DM search service
1858** record event
1859**
1860** Returns void
1861**
1862*******************************************************************************/
1863static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1864{
1865 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
1866 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
1867}
1868
1869/*****************************************************************************
1870**
1871** btif api functions (no context switch)
1872**
1873*****************************************************************************/
1874
1875/*******************************************************************************
1876**
1877** Function btif_dm_start_discovery
1878**
1879** Description Start device discovery/inquiry
1880**
1881** Returns bt_status_t
1882**
1883*******************************************************************************/
1884bt_status_t btif_dm_start_discovery(void)
1885{
1886 tBTA_DM_INQ inq_params;
1887 tBTA_SERVICE_MASK services = 0;
1888
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001889 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001890 /* TODO: Do we need to handle multiple inquiries at the same time? */
1891
1892 /* Set inquiry params and call API */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001893#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001894 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
Matthew Xie7f3e4292013-09-30 12:44:10 -07001895#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
1896 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
1897 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
1898 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
1899 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
1900#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001901#else
1902 inq_params.mode = BTA_DM_GENERAL_INQUIRY;
1903#endif
1904 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
1905
1906 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
1907 inq_params.report_dup = TRUE;
1908
1909 inq_params.filter_type = BTA_DM_INQ_CLR;
1910 /* TODO: Filter device by BDA needs to be implemented here */
1911
1912 /* Will be enabled to TRUE once inquiry busy level has been received */
1913 btif_dm_inquiry_in_progress = FALSE;
1914 /* find nearby devices */
1915 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
1916
1917 return BT_STATUS_SUCCESS;
1918}
1919
1920/*******************************************************************************
1921**
1922** Function btif_dm_cancel_discovery
1923**
1924** Description Cancels search
1925**
1926** Returns bt_status_t
1927**
1928*******************************************************************************/
1929bt_status_t btif_dm_cancel_discovery(void)
1930{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001931 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001932 BTA_DmSearchCancel();
1933 return BT_STATUS_SUCCESS;
1934}
1935
1936/*******************************************************************************
1937**
1938** Function btif_dm_create_bond
1939**
1940** Description Initiate bonding with the specified device
1941**
1942** Returns bt_status_t
1943**
1944*******************************************************************************/
1945bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr)
1946{
1947 bdstr_t bdstr;
1948
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001949 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 -08001950 if (pairing_cb.state != BT_BOND_STATE_NONE)
1951 return BT_STATUS_BUSY;
1952
1953 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
1954 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
1955
1956 return BT_STATUS_SUCCESS;
1957}
1958
1959/*******************************************************************************
1960**
1961** Function btif_dm_cancel_bond
1962**
1963** Description Initiate bonding with the specified device
1964**
1965** Returns bt_status_t
1966**
1967*******************************************************************************/
1968
1969bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
1970{
1971 bdstr_t bdstr;
1972
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001973 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 -08001974
1975 /* TODO:
1976 ** 1. Restore scan modes
1977 ** 2. special handling for HID devices
1978 */
1979 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1980 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001981
1982#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1983
1984 if (pairing_cb.is_ssp)
1985 {
1986 if (pairing_cb.is_le_only)
1987 {
1988 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
1989 }
1990 else
Hemant Guptae1468692013-11-14 16:21:29 +05301991 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001992 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
Hemant Guptae1468692013-11-14 16:21:29 +05301993 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
1994 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
1995 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001996 }
1997 else
1998 {
1999 if (pairing_cb.is_le_only)
2000 {
2001 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2002 }
2003 else
2004 {
2005 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2006 }
2007 /* Cancel bonding, in case it is in ACL connection setup state */
2008 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2009 }
2010
2011#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002012 if (pairing_cb.is_ssp)
2013 {
2014 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2015 }
2016 else
2017 {
2018 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2019 }
2020 /* Cancel bonding, in case it is in ACL connection setup state */
2021 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2022 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002023#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002024 }
2025
2026 return BT_STATUS_SUCCESS;
2027}
2028
2029/*******************************************************************************
2030**
Kim Schulza9eb25c2013-09-30 10:55:52 +02002031** Function btif_dm_hh_open_failed
2032**
2033** Description informs the upper layers if the HH have failed during bonding
2034**
2035** Returns none
2036**
2037*******************************************************************************/
2038
2039void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
2040{
2041 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2042 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2043 {
2044 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2045 }
2046}
2047
2048/*******************************************************************************
2049**
The Android Open Source Project5738f832012-12-12 16:00:35 -08002050** Function btif_dm_remove_bond
2051**
2052** Description Removes bonding with the specified device
2053**
2054** Returns bt_status_t
2055**
2056*******************************************************************************/
2057
2058bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
2059{
2060 bdstr_t bdstr;
2061
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002062 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 -08002063 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2064 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2065
2066 return BT_STATUS_SUCCESS;
2067}
2068
2069/*******************************************************************************
2070**
2071** Function btif_dm_pin_reply
2072**
2073** Description BT legacy pairing - PIN code reply
2074**
2075** Returns bt_status_t
2076**
2077*******************************************************************************/
2078
2079bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2080 uint8_t pin_len, bt_pin_code_t *pin_code)
2081{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002082 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Hemant Gupta831423e2014-01-08 12:42:13 +05302083 if (pin_code == NULL)
2084 return BT_STATUS_FAIL;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002085#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002086
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002087 if (pairing_cb.is_le_only)
2088 {
2089 int i;
2090 UINT32 passkey = 0;
2091 int multi[] = {100000, 10000, 1000, 100, 10,1};
2092 BD_ADDR remote_bd_addr;
2093 bdcpy(remote_bd_addr, bd_addr->address);
2094 for (i = 0; i < 6; i++)
2095 {
2096 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2097 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002098 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002099 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2100
2101 }
2102 else
2103 {
2104 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2105 if (accept)
2106 pairing_cb.pin_code_len = pin_len;
2107 }
2108#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002109 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2110
2111 if (accept)
2112 pairing_cb.pin_code_len = pin_len;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002113#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002114 return BT_STATUS_SUCCESS;
2115}
2116
2117/*******************************************************************************
2118**
2119** Function btif_dm_ssp_reply
2120**
2121** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2122**
2123** Returns bt_status_t
2124**
2125*******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002126bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2127 bt_ssp_variant_t variant, uint8_t accept,
2128 uint32_t passkey)
2129{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08002130 UNUSED(passkey);
2131
The Android Open Source Project5738f832012-12-12 16:00:35 -08002132 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2133 {
2134 /* This is not implemented in the stack.
2135 * For devices with display, this is not needed
2136 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002137 BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002138 return BT_STATUS_FAIL;
2139 }
2140 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002141 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002142#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2143 if (pairing_cb.is_le_only)
2144 {
2145 if (accept)
2146 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2147 else
2148 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2149 }
2150 else
2151 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002152
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002153#else
2154 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2155#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002156 return BT_STATUS_SUCCESS;
2157}
2158
2159/*******************************************************************************
2160**
2161** Function btif_dm_get_adapter_property
2162**
2163** Description Queries the BTA for the adapter property
2164**
2165** Returns bt_status_t
2166**
2167*******************************************************************************/
2168bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2169{
2170 bt_status_t status;
2171
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002172 BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002173 switch (prop->type)
2174 {
2175 case BT_PROPERTY_BDNAME:
2176 {
2177 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
Matthew Xie1e5109b2012-11-09 18:26:26 -08002178 strcpy((char *)bd_name->name, btif_get_default_local_name());
The Android Open Source Project5738f832012-12-12 16:00:35 -08002179 prop->len = strlen((char *)bd_name->name);
2180 }
2181 break;
2182
2183 case BT_PROPERTY_ADAPTER_SCAN_MODE:
2184 {
2185 /* if the storage does not have it. Most likely app never set it. Default is NONE */
2186 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2187 *mode = BT_SCAN_MODE_NONE;
2188 prop->len = sizeof(bt_scan_mode_t);
2189 }
2190 break;
2191
2192 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2193 {
2194 uint32_t *tmt = (uint32_t*)prop->val;
2195 *tmt = 120; /* default to 120s, if not found in NV */
2196 prop->len = sizeof(uint32_t);
2197 }
2198 break;
2199
2200 default:
2201 prop->len = 0;
2202 return BT_STATUS_FAIL;
2203 }
2204 return BT_STATUS_SUCCESS;
2205}
2206
2207/*******************************************************************************
2208**
2209** Function btif_dm_get_remote_services
2210**
2211** Description Start SDP to get remote services
2212**
2213** Returns bt_status_t
2214**
2215*******************************************************************************/
2216bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2217{
2218 bdstr_t bdstr;
2219
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002220 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002221
2222 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2223 bte_dm_search_services_evt, TRUE);
2224
2225 return BT_STATUS_SUCCESS;
2226}
2227
2228/*******************************************************************************
2229**
2230** Function btif_dm_get_remote_service_record
2231**
2232** Description Start SDP to get remote service record
2233**
2234**
2235** Returns bt_status_t
2236*******************************************************************************/
2237bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2238 bt_uuid_t *uuid)
2239{
2240 tSDP_UUID sdp_uuid;
2241 bdstr_t bdstr;
2242
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002243 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002244
2245 sdp_uuid.len = MAX_UUID_SIZE;
2246 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2247
2248 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2249 bte_dm_remote_service_record_evt, TRUE);
2250
2251 return BT_STATUS_SUCCESS;
2252}
2253
2254void btif_dm_execute_service_request(UINT16 event, char *p_param)
2255{
2256 BOOLEAN b_enable = FALSE;
2257 bt_status_t status;
2258 if (event == BTIF_DM_ENABLE_SERVICE)
2259 {
2260 b_enable = TRUE;
2261 }
2262 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2263 if (status == BT_STATUS_SUCCESS)
2264 {
2265 bt_property_t property;
2266 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2267
2268 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2269 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2270 sizeof(local_uuids), local_uuids);
2271 btif_storage_get_adapter_property(&property);
2272 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2273 BT_STATUS_SUCCESS, 1, &property);
2274 }
2275 return;
2276}
2277
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002278void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2279 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2280{
2281 UINT8 yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2282 /* if local initiated:
2283 ** 1. set DD + MITM
2284 ** if remote initiated:
2285 ** 1. Copy over the auth_req from peer's io_rsp
2286 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2287 ** as a fallback set MITM+GB if peer had MITM set
2288 */
2289 UNUSED (bd_addr);
2290 UNUSED (p_io_cap);
2291 UNUSED (p_oob_data);
2292
2293
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002294 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002295 if(pairing_cb.is_local_initiated)
2296 {
2297 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2298 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2299 }
2300 else if (!is_orig)
2301 {
2302 /* peer initiated paring. They probably know what they want.
2303 ** Copy the mitm from peer device.
2304 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002305 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002306 __FUNCTION__, pairing_cb.auth_req);
2307 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2308
2309 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2310 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2311 *p_auth_req |= BTA_AUTH_SP_YES;
2312 }
2313 else if (yes_no_bit)
2314 {
2315 /* set the general bonding bit for stored device */
2316 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2317 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002318 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002319}
2320
2321void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2322 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2323{
2324 UNUSED (bd_addr);
2325 UNUSED (oob_data);
2326
2327 if(auth_req & BTA_AUTH_BONDS)
2328 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002329 BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002330 pairing_cb.auth_req = auth_req;
2331 pairing_cb.io_cap = io_cap;
2332 }
2333}
2334
The Android Open Source Project5738f832012-12-12 16:00:35 -08002335#if (BTM_OOB_INCLUDED == TRUE)
2336void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data)
2337{
2338 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2339 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2340 {
2341 *p_oob_data = FALSE;
2342 }
2343 else
2344 {
2345 *p_oob_data = TRUE;
2346 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002347 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 -08002348}
2349#endif /* BTM_OOB_INCLUDED */
2350
2351#ifdef BTIF_DM_OOB_TEST
2352void btif_dm_load_local_oob(void)
2353{
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002354 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002355 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002356 BTIF_TRACE_DEBUG("btif_dm_load_local_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002357 if (prop_oob[0] != '3')
2358 {
2359#if (BTM_OOB_INCLUDED == TRUE)
2360 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2361 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2362 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002363 BTIF_TRACE_DEBUG("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002364 BTA_DmLocalOob();
2365 }
2366#else
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002367 BTIF_TRACE_ERROR("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002368#endif
2369 }
2370}
2371
2372void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2373{
2374 FILE *fp;
2375 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2376 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2377 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002378 char prop_oob[PROPERTY_VALUE_MAX];
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002379 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: valid=%d", valid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002380 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2381 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2382 valid)
2383 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002384 BTIF_TRACE_DEBUG("save local OOB data in memory");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002385 memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2386 memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2387 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002388 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002389 if (prop_oob[0] == '1')
2390 path = path_a;
2391 else if (prop_oob[0] == '2')
2392 path = path_b;
2393 if (path)
2394 {
2395 fp = fopen(path, "wb+");
2396 if (fp == NULL)
2397 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002398 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 -08002399 }
2400 else
2401 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002402 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 -08002403 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2404 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2405 fclose(fp);
2406 }
2407 }
2408 }
2409}
2410BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2411{
2412 char t[128];
2413 FILE *fp;
2414 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2415 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2416 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002417 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002418 BOOLEAN result = FALSE;
2419 bt_bdaddr_t bt_bd_addr;
2420 bdcpy(oob_cb.oob_bdaddr, bd_addr);
2421 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002422 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002423 if (prop_oob[0] == '1')
2424 path = path_b;
2425 else if (prop_oob[0] == '2')
2426 path = path_a;
2427 if (path)
2428 {
2429 fp = fopen(path, "rb");
2430 if (fp == NULL)
2431 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002432 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 -08002433 return FALSE;
2434 }
2435 else
2436 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002437 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob: read OOB data from %s",path);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002438 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2439 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2440 fclose(fp);
2441 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002442 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: TRUE");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002443 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2444 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2445 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002446 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002447 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2448 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
2449 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 -07002450 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: c = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002451 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2452 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
2453 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 -07002454 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: r = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002455 bdcpy(bt_bd_addr.address, bd_addr);
2456 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2457 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2458 result = TRUE;
2459 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002460 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob result=%d",result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002461 return result;
2462}
2463#endif /* BTIF_DM_OOB_TEST */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002464#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2465
2466static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2467{
2468 bt_bdaddr_t bd_addr;
2469 bt_bdname_t bd_name;
2470 UINT32 cod;
2471
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002472 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002473
2474 /* Remote name update */
2475 btif_update_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2476 NULL, BT_DEVICE_TYPE_BLE);
2477 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2478 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2479
2480 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2481 pairing_cb.is_ssp = FALSE;
2482 cod = COD_UNCLASSIFIED;
2483
2484 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2485 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2486 p_ssp_key_notif->passkey);
2487}
2488
2489/*******************************************************************************
2490**
2491** Function btif_dm_ble_auth_cmpl_evt
2492**
2493** Description Executes authentication complete event in btif context
2494**
2495** Returns void
2496**
2497*******************************************************************************/
2498static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2499{
2500 /* Save link key, if not temporary */
2501 bt_bdaddr_t bd_addr;
2502 bt_status_t status = BT_STATUS_FAIL;
2503 bt_bond_state_t state = BT_BOND_STATE_NONE;
2504
2505 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2506 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2507 {
2508 /* store keys */
2509 }
2510 if (p_auth_cmpl->success)
2511 {
2512 status = BT_STATUS_SUCCESS;
2513 state = BT_BOND_STATE_BONDED;
2514
2515 btif_dm_save_ble_bonding_keys();
2516 BTA_GATTC_Refresh(bd_addr.address);
2517 btif_dm_get_remote_services(&bd_addr);
2518 }
2519 else
2520 {
2521 /*Map the HCI fail reason to bt status */
2522 switch (p_auth_cmpl->fail_reason)
2523 {
Priti Aghera156c52b2014-07-09 14:58:19 -07002524 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
2525 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
2526 btif_dm_remove_ble_bonding_keys();
2527 status = BT_STATUS_AUTH_FAILURE;
2528 break;
2529 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
2530 status = BT_STATUS_AUTH_REJECTED;
2531 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002532 default:
Andre Eisenbachca22ac42013-02-13 17:02:11 +09002533 btif_dm_remove_ble_bonding_keys();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002534 status = BT_STATUS_FAIL;
2535 break;
2536 }
2537 }
2538 bond_state_changed(status, &bd_addr, state);
2539}
2540
2541
2542
2543void btif_dm_load_ble_local_keys(void)
2544{
2545 bt_status_t bt_status;
2546
2547 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2548
2549 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2550 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2551 {
2552 ble_local_key_cb.is_er_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002553 BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002554 }
2555
2556 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
2557 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
2558 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
2559 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
2560 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
2561 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
2562 {
2563 ble_local_key_cb.is_id_keys_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002564 BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002565 }
2566
2567}
2568void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
2569 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
2570{
2571 if (ble_local_key_cb.is_er_rcvd )
2572 {
2573 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2574 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2575 }
2576
2577 if (ble_local_key_cb.is_id_keys_rcvd)
2578 {
2579 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
2580 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
2581 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
2582 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
2583 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002584 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002585}
2586
2587void btif_dm_save_ble_bonding_keys(void)
2588{
2589
2590 bt_bdaddr_t bd_addr;
2591
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002592 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002593
2594 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2595
2596 if (pairing_cb.ble.is_penc_key_rcvd)
2597 {
2598 btif_storage_add_ble_bonding_key(&bd_addr,
2599 (char *) &pairing_cb.ble.penc_key,
2600 BTIF_DM_LE_KEY_PENC,
2601 sizeof(btif_dm_ble_penc_keys_t));
2602 }
2603
2604 if (pairing_cb.ble.is_pid_key_rcvd)
2605 {
2606 btif_storage_add_ble_bonding_key(&bd_addr,
2607 (char *) &pairing_cb.ble.pid_key[0],
2608 BTIF_DM_LE_KEY_PID,
2609 BT_OCTET16_LEN);
2610 }
2611
2612
2613 if (pairing_cb.ble.is_pcsrk_key_rcvd)
2614 {
2615 btif_storage_add_ble_bonding_key(&bd_addr,
2616 (char *) &pairing_cb.ble.pcsrk_key,
2617 BTIF_DM_LE_KEY_PCSRK,
2618 sizeof(btif_dm_ble_pcsrk_keys_t));
2619 }
2620
2621
2622 if (pairing_cb.ble.is_lenc_key_rcvd)
2623 {
2624 btif_storage_add_ble_bonding_key(&bd_addr,
2625 (char *) &pairing_cb.ble.lenc_key,
2626 BTIF_DM_LE_KEY_LENC,
2627 sizeof(btif_dm_ble_lenc_keys_t));
2628 }
2629
2630 if (pairing_cb.ble.is_lcsrk_key_rcvd)
2631 {
2632 btif_storage_add_ble_bonding_key(&bd_addr,
2633 (char *) &pairing_cb.ble.lcsrk_key,
2634 BTIF_DM_LE_KEY_LCSRK,
2635 sizeof(btif_dm_ble_lcsrk_keys_t));
2636 }
2637
2638}
2639
2640
2641void btif_dm_remove_ble_bonding_keys(void)
2642{
2643 bt_bdaddr_t bd_addr;
2644
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002645 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002646
2647 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2648 btif_storage_remove_ble_bonding_keys(&bd_addr);
2649}
2650
2651
2652/*******************************************************************************
2653**
2654** Function btif_dm_ble_sec_req_evt
2655**
2656** Description Eprocess security request event in btif context
2657**
2658** Returns void
2659**
2660*******************************************************************************/
2661void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
2662{
2663 bt_bdaddr_t bd_addr;
2664 bt_bdname_t bd_name;
2665 UINT32 cod;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002666 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002667
2668 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2669 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002670 BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002671 return;
2672 }
2673
2674 /* Remote name update */
2675 btif_update_remote_properties(p_ble_req->bd_addr,p_ble_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
2676
2677 bdcpy(bd_addr.address, p_ble_req->bd_addr);
2678 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
2679
2680 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2681
2682 pairing_cb.is_temp = FALSE;
2683 pairing_cb.is_le_only = TRUE;
2684 pairing_cb.is_ssp = TRUE;
2685
2686 cod = COD_UNCLASSIFIED;
2687
2688 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
2689 BT_SSP_VARIANT_CONSENT, 0);
2690}
2691
2692
2693
2694/*******************************************************************************
2695**
2696** Function btif_dm_ble_passkey_req_evt
2697**
2698** Description Executes pin request event in btif context
2699**
2700** Returns void
2701**
2702*******************************************************************************/
2703static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
2704{
2705 bt_bdaddr_t bd_addr;
2706 bt_bdname_t bd_name;
2707 UINT32 cod;
2708
2709 /* Remote name update */
2710 btif_update_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
2711
2712 bdcpy(bd_addr.address, p_pin_req->bd_addr);
2713 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
2714
2715 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2716 pairing_cb.is_le_only = TRUE;
2717
2718 cod = COD_UNCLASSIFIED;
2719
2720 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
2721 &bd_addr, &bd_name, cod);
2722}
2723
2724
2725void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
2726 tBT_DEVICE_TYPE dev_type)
2727{
2728 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
2729}
2730
2731static void btif_dm_ble_tx_test_cback(void *p)
2732{
2733 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
2734 (char *)p, 1, NULL);
2735}
2736
2737static void btif_dm_ble_rx_test_cback(void *p)
2738{
2739 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
2740 (char *)p, 1, NULL);
2741}
2742
2743static void btif_dm_ble_test_end_cback(void *p)
2744{
2745 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
2746 (char *)p, 3, NULL);
2747}
2748/*******************************************************************************
2749**
2750** Function btif_le_test_mode
2751**
2752** Description Sends a HCI BLE Test command to the Controller
2753**
2754** Returns BT_STATUS_SUCCESS on success
2755**
2756*******************************************************************************/
2757bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
2758{
2759 switch (opcode) {
2760 case HCI_BLE_TRANSMITTER_TEST:
2761 if (len != 3) return BT_STATUS_PARM_INVALID;
2762 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
2763 break;
2764 case HCI_BLE_RECEIVER_TEST:
2765 if (len != 1) return BT_STATUS_PARM_INVALID;
2766 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
2767 break;
2768 case HCI_BLE_TEST_END:
2769 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
2770 break;
2771 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002772 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002773 return BT_STATUS_UNSUPPORTED;
2774 }
2775 return BT_STATUS_SUCCESS;
2776}
2777
2778#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002779
2780void btif_dm_on_disable()
2781{
2782 /* cancel any pending pairing requests */
2783 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2784 {
2785 bt_bdaddr_t bd_addr;
2786
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002787 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002788 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2789 btif_dm_cancel_bond(&bd_addr);
2790 }
2791}
Matthew Xie1e5109b2012-11-09 18:26:26 -08002792
2793static char* btif_get_default_local_name() {
2794 if (btif_default_local_name[0] == '\0')
2795 {
2796 int max_len = sizeof(btif_default_local_name) - 1;
2797 if (BTM_DEF_LOCAL_NAME[0] != '\0')
2798 {
2799 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
2800 }
2801 else
2802 {
2803 char prop_model[PROPERTY_VALUE_MAX];
2804 property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
2805 strncpy(btif_default_local_name, prop_model, max_len);
2806 }
2807 btif_default_local_name[max_len] = '\0';
2808 }
2809 return btif_default_local_name;
2810}