blob: 8ab913758f96b42ec0c96327e3a5d9e47f2461cb [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);
504
505
The Android Open Source Project5738f832012-12-12 16:00:35 -0800506 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800507
508 if (is_hid){
509
The Android Open Source Project5738f832012-12-12 16:00:35 -0800510 int status;
511 status = btif_hh_connect(bd_addr);
512 if(status != BT_STATUS_SUCCESS)
513 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
514 }
515 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800516 {
Matthew Xiefc4b2f12013-05-06 20:51:02 -0700517#if BLE_INCLUDED == TRUE
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800518 int device_type;
519 int addr_type;
520 bdstr_t bdstr;
521 bd2str(bd_addr, &bdstr);
522 if(btif_config_get_int("Remote", (char const *)&bdstr,"DevType", &device_type) &&
523 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
524 (device_type == BT_DEVICE_TYPE_BLE))
525 {
526 BTA_DmAddBleDevice(bd_addr->address, addr_type, BT_DEVICE_TYPE_BLE);
527 }
Matthew Xiefc4b2f12013-05-06 20:51:02 -0700528#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800529 BTA_DmBond ((UINT8 *)bd_addr->address);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800530 }
531 /* Track originator of bond creation */
532 pairing_cb.is_local_initiated = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800533
534}
535
536/*******************************************************************************
537**
538** Function btif_dm_cb_remove_bond
539**
540** Description remove bond initiated from the BTIF thread context
541** Special handling for HID devices
542**
543** Returns void
544**
545*******************************************************************************/
546void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
547{
548 bdstr_t bdstr;
549 /*special handling for HID devices */
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700550 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there
551 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
552#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
553 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
554#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800555 {
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700556 BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800557 }
558}
559
560/*******************************************************************************
561**
Andre Eisenbach249f6002014-06-18 12:20:37 -0700562** Function btif_dm_get_connection_state
563**
564** Description Returns whether the remote device is currently connected
565**
566** Returns 0 if not connected
567**
568*******************************************************************************/
569uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr)
570{
571 return BTA_DmGetConnectionState((UINT8 *)bd_addr->address);
572}
573
574/*******************************************************************************
575**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800576** Function search_devices_copy_cb
577**
578** Description Deep copy callback for search devices event
579**
580** Returns void
581**
582*******************************************************************************/
583static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
584{
585 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
586 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
587
588 if (!p_src)
589 return;
590
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700591 BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800592 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
593 switch (event)
594 {
595 case BTA_DM_INQ_RES_EVT:
596 {
597 if (p_src_data->inq_res.p_eir)
598 {
599 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
600 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
601 }
602 }
603 break;
604
605 case BTA_DM_DISC_RES_EVT:
606 {
607 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
608 {
609 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
610 memcpy(p_dest_data->disc_res.p_raw_data,
611 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
612 }
613 }
614 break;
615 }
616}
617
618static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
619{
620 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
621 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
622
623 if (!p_src)
624 return;
625 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
626 switch (event)
627 {
628 case BTA_DM_DISC_RES_EVT:
629 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530630 if (p_src_data->disc_res.result == BTA_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800631 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530632 if (p_src_data->disc_res.num_uuids > 0)
633 {
634 p_dest_data->disc_res.p_uuid_list =
635 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
636 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
637 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
638 GKI_freebuf(p_src_data->disc_res.p_uuid_list);
639 }
640 if (p_src_data->disc_res.p_raw_data != NULL)
641 {
642 GKI_freebuf(p_src_data->disc_res.p_raw_data);
643 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800644 }
645 } break;
646 }
647}
648/******************************************************************************
649**
650** BTIF DM callback events
651**
652*****************************************************************************/
653
654/*******************************************************************************
655**
656** Function btif_dm_pin_req_evt
657**
658** Description Executes pin request event in btif context
659**
660** Returns void
661**
662*******************************************************************************/
663static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
664{
665 bt_bdaddr_t bd_addr;
666 bt_bdname_t bd_name;
667 UINT32 cod;
668 bt_pin_code_t pin_code;
669
670 /* Remote properties update */
671 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
672 p_pin_req->dev_class, BT_DEVICE_TYPE_BREDR);
673
674 bdcpy(bd_addr.address, p_pin_req->bd_addr);
675 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
676
677 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
678
679 cod = devclass2uint(p_pin_req->dev_class);
680
681 if ( cod == 0) {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700682 BTIF_TRACE_DEBUG("%s():cod is 0, set as unclassified", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800683 cod = COD_UNCLASSIFIED;
684 }
685
686 /* check for auto pair possiblity only if bond was initiated by local device */
687 if (pairing_cb.is_local_initiated)
688 {
689 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
690 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
691 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
692 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
693 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
694 check_cod(&bd_addr, COD_HID_POINTING))
695 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700696 BTIF_TRACE_DEBUG("%s()cod matches for auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800697 /* Check if this device can be auto paired */
698 if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) &&
699 (pairing_cb.autopair_attempts == 0))
700 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700701 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800702 pin_code.pin[0] = 0x30;
703 pin_code.pin[1] = 0x30;
704 pin_code.pin[2] = 0x30;
705 pin_code.pin[3] = 0x30;
706
707 pairing_cb.autopair_attempts++;
708 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
709 return;
710 }
711 }
712 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
713 check_cod(&bd_addr, COD_HID_COMBO))
714 {
715 if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) &&
716 (pairing_cb.autopair_attempts == 0))
717 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700718 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800719 pin_code.pin[0] = 0x30;
720 pin_code.pin[1] = 0x30;
721 pin_code.pin[2] = 0x30;
722 pin_code.pin[3] = 0x30;
723
724 pairing_cb.autopair_attempts++;
725 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
726 return;
727 }
728 }
729 }
730 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
731 &bd_addr, &bd_name, cod);
732}
733
734/*******************************************************************************
735**
736** Function btif_dm_ssp_cfm_req_evt
737**
738** Description Executes SSP confirm request event in btif context
739**
740** Returns void
741**
742*******************************************************************************/
743static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
744{
745 bt_bdaddr_t bd_addr;
746 bt_bdname_t bd_name;
747 UINT32 cod;
748 BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
749
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700750 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800751
752 /* Remote properties update */
753 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
754 p_ssp_cfm_req->dev_class, BT_DEVICE_TYPE_BREDR);
755
756 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
757 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
758
759 /* Set the pairing_cb based on the local & remote authentication requirements */
760 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
761
762 /* if just_works and bonding bit is not set treat this as temporary */
763 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 -0800764 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
765 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800766 pairing_cb.is_temp = TRUE;
767 else
768 pairing_cb.is_temp = FALSE;
769
770 pairing_cb.is_ssp = TRUE;
771
772 /* If JustWorks auto-accept */
773 if (p_ssp_cfm_req->just_works)
774 {
775 /* Pairing consent for JustWorks needed if:
776 * 1. Incoming pairing is detected AND
777 * 2. local IO capabilities are DisplayYesNo AND
778 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
779 */
780 if ((is_incoming) && ((p_ssp_cfm_req->loc_io_caps == 0x01) &&
781 (p_ssp_cfm_req->rmt_io_caps == 0x00 || p_ssp_cfm_req->rmt_io_caps == 0x03)))
782 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700783 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 -0800784 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
785 }
786 else
787 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700788 BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800789 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
790 return;
791 }
792 }
793
794 cod = devclass2uint(p_ssp_cfm_req->dev_class);
795
796 if ( cod == 0) {
797 ALOGD("cod is 0, set as unclassified");
798 cod = COD_UNCLASSIFIED;
799 }
800
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700801 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800802 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
803 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
804 p_ssp_cfm_req->num_val);
805}
806
807static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
808{
809 bt_bdaddr_t bd_addr;
810 bt_bdname_t bd_name;
811 UINT32 cod;
812
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700813 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800814
815 /* Remote properties update */
816 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
817 p_ssp_key_notif->dev_class, BT_DEVICE_TYPE_BREDR);
818
819 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
820 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
821
822 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
823 pairing_cb.is_ssp = TRUE;
824 cod = devclass2uint(p_ssp_key_notif->dev_class);
825
826 if ( cod == 0) {
827 ALOGD("cod is 0, set as unclassified");
828 cod = COD_UNCLASSIFIED;
829 }
830
831 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
832 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
833 p_ssp_key_notif->passkey);
834}
835/*******************************************************************************
836**
837** Function btif_dm_auth_cmpl_evt
838**
839** Description Executes authentication complete event in btif context
840**
841** Returns void
842**
843*******************************************************************************/
844static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
845{
846 /* Save link key, if not temporary */
847 bt_bdaddr_t bd_addr;
848 bt_status_t status = BT_STATUS_FAIL;
849 bt_bond_state_t state = BT_BOND_STATE_NONE;
850
851 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
852 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
853 {
854 if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) || (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
855 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) || (!pairing_cb.is_temp))
856 {
857 bt_status_t ret;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700858 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, is_temp=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800859 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.is_temp);
860 ret = btif_storage_add_bonded_device(&bd_addr,
861 p_auth_cmpl->key, p_auth_cmpl->key_type,
862 pairing_cb.pin_code_len);
863 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
864 }
865 else
866 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700867 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 -0800868 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.is_temp);
Hemant Guptab820aec2013-12-24 19:59:57 +0530869 if(pairing_cb.is_temp)
870 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700871 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
Hemant Guptab820aec2013-12-24 19:59:57 +0530872 __FUNCTION__);
873 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
874 return;
875 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800876 }
877 }
878 if (p_auth_cmpl->success)
879 {
880 status = BT_STATUS_SUCCESS;
881 state = BT_BOND_STATE_BONDED;
882
883 /* Trigger SDP on the device */
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700884 pairing_cb.sdp_attempts = 1;;
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -0800885
886 if(btif_dm_inquiry_in_progress)
887 btif_dm_cancel_discovery();
888
The Android Open Source Project5738f832012-12-12 16:00:35 -0800889 btif_dm_get_remote_services(&bd_addr);
890 /* Do not call bond_state_changed_cb yet. Wait till fetch remote service is complete */
891 }
892 else
893 {
894 /*Map the HCI fail reason to bt status */
895 switch(p_auth_cmpl->fail_reason)
896 {
897 case HCI_ERR_PAGE_TIMEOUT:
898 case HCI_ERR_CONNECTION_TOUT:
899 status = BT_STATUS_RMT_DEV_DOWN;
900 break;
901
Hemant Guptaaef7a672013-07-31 19:00:12 +0530902 case HCI_ERR_PAIRING_NOT_ALLOWED:
903 status = BT_STATUS_AUTH_REJECTED;
904 break;
905
Hemant Guptab4801442014-01-07 18:11:15 +0530906 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
907 status = BT_STATUS_AUTH_FAILURE;
908 break;
909
The Android Open Source Project5738f832012-12-12 16:00:35 -0800910 /* map the auth failure codes, so we can retry pairing if necessary */
911 case HCI_ERR_AUTH_FAILURE:
Hemant Gupta59a88ec2014-03-19 19:01:35 +0530912 case HCI_ERR_KEY_MISSING:
Zhihai Xua7ea8092013-11-27 14:10:53 +0530913 btif_storage_remove_bonded_device(&bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800914 case HCI_ERR_HOST_REJECT_SECURITY:
915 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
916 case HCI_ERR_UNIT_KEY_USED:
917 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
918 case HCI_ERR_INSUFFCIENT_SECURITY:
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530919 case HCI_ERR_PEER_USER:
920 case HCI_ERR_UNSPECIFIED:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700921 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d",
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530922 __FUNCTION__, p_auth_cmpl->fail_reason);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800923 if (pairing_cb.autopair_attempts == 1)
924 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700925 BTIF_TRACE_DEBUG("%s(): Adding device to blacklist ", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800926
927 /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class */
928 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
929 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
930 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
931 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
932 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
933 check_cod(&bd_addr, COD_HID_POINTING))
934 {
935 btif_storage_add_device_to_autopair_blacklist (&bd_addr);
936 }
937 pairing_cb.autopair_attempts++;
938
939 /* Create the Bond once again */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700940 BTIF_TRACE_DEBUG("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800941 btif_dm_cb_create_bond (&bd_addr);
942 return;
943 }
944 else
945 {
946 /* if autopair attempts are more than 1, or not attempted */
947 status = BT_STATUS_AUTH_FAILURE;
948 }
949 break;
950
951 default:
952 status = BT_STATUS_FAIL;
953 }
Zhihai Xu8d2128d2013-12-13 16:09:21 +0530954 /* Special Handling for HID Devices */
955 if (check_cod(&bd_addr, COD_HID_POINTING)) {
956 /* Remove Device as bonded in nvram as authentication failed */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700957 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __FUNCTION__);
Zhihai Xu8d2128d2013-12-13 16:09:21 +0530958 btif_storage_remove_bonded_device(&bd_addr);
959 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800960 bond_state_changed(status, &bd_addr, state);
961 }
962}
963
964/******************************************************************************
965**
966** Function btif_dm_search_devices_evt
967**
968** Description Executes search devices callback events in btif context
969**
970** Returns void
971**
972******************************************************************************/
973static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
974{
975 tBTA_DM_SEARCH *p_search_data;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700976 BTIF_TRACE_EVENT("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800977
978 switch (event)
979 {
980 case BTA_DM_DISC_RES_EVT:
981 {
982 p_search_data = (tBTA_DM_SEARCH *)p_param;
983 /* Remote name update */
984 if (strlen((const char *) p_search_data->disc_res.bd_name))
985 {
986 bt_property_t properties[1];
987 bt_bdaddr_t bdaddr;
988 bt_status_t status;
989
990 properties[0].type = BT_PROPERTY_BDNAME;
991 properties[0].val = p_search_data->disc_res.bd_name;
992 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
993 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
994
995 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
996 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
997 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
998 status, &bdaddr, 1, properties);
999 }
1000 /* TODO: Services? */
1001 }
1002 break;
1003
1004 case BTA_DM_INQ_RES_EVT:
1005 {
1006 /* inquiry result */
1007 UINT32 cod;
1008 UINT8 *p_eir_remote_name = NULL;
1009 bt_bdname_t bdname;
1010 bt_bdaddr_t bdaddr;
1011 UINT8 remote_name_len;
1012 UINT8 *p_cached_name = NULL;
1013 tBTA_SERVICE_MASK services = 0;
1014 bdstr_t bdstr;
1015
1016 p_search_data = (tBTA_DM_SEARCH *)p_param;
1017 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
1018
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001019 BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __FUNCTION__, bd2str(&bdaddr, &bdstr),
The Android Open Source Project5738f832012-12-12 16:00:35 -08001020#if (BLE_INCLUDED == TRUE)
1021 p_search_data->inq_res.device_type);
1022#else
1023 BT_DEVICE_TYPE_BREDR);
1024#endif
1025 bdname.name[0] = 0;
1026
1027 cod = devclass2uint (p_search_data->inq_res.dev_class);
1028
1029 if ( cod == 0) {
1030 ALOGD("cod is 0, set as unclassified");
1031 cod = COD_UNCLASSIFIED;
1032 }
1033
1034 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1035 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1036
1037 /* Check EIR for remote name and services */
1038 if (p_search_data->inq_res.p_eir)
1039 {
1040 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001041 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001042 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */
1043 }
1044
1045
1046 {
1047 bt_property_t properties[5];
1048 bt_device_type_t dev_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001049 UINT8 addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001050 uint32_t num_properties = 0;
1051 bt_status_t status;
1052
1053 memset(properties, 0, sizeof(properties));
1054 /* BD_ADDR */
1055 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1056 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1057 num_properties++;
1058 /* BD_NAME */
1059 /* Don't send BDNAME if it is empty */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001060 if (bdname.name[0])
1061 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001062 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1063 BT_PROPERTY_BDNAME,
1064 strlen((char *)bdname.name), &bdname);
1065 num_properties++;
1066 }
1067
1068 /* DEV_CLASS */
1069 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1070 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1071 num_properties++;
1072 /* DEV_TYPE */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001073#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001074 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1075 dev_type = p_search_data->inq_res.device_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001076 addr_type = p_search_data->inq_res.ble_addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001077#else
1078 dev_type = BT_DEVICE_TYPE_BREDR;
1079#endif
1080 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1081 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1082 num_properties++;
1083 /* RSSI */
1084 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1085 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1086 &(p_search_data->inq_res.rssi));
1087 num_properties++;
1088
1089 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1090 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001091#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1092 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001093 if (( dev_type == BT_DEVICE_TYPE_DUMO)&&
1094 (p_search_data->inq_res.flag & BTA_BLE_DMT_CONTROLLER_SPT) &&
1095 (p_search_data->inq_res.flag & BTA_BLE_DMT_HOST_SPT))
1096 {
1097 btif_storage_set_dmt_support_type (&bdaddr, TRUE);
1098 }
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001099 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1100#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001101 /* Callback to notify upper layer of device */
1102 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1103 num_properties, properties);
1104 }
1105 }
1106 break;
1107
1108 case BTA_DM_INQ_CMPL_EVT:
1109 {
1110 }
1111 break;
1112 case BTA_DM_DISC_CMPL_EVT:
1113 {
1114 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1115 }
1116 break;
1117 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1118 {
1119 /* if inquiry is not in progress and we get a cancel event, then
1120 * it means we are done with inquiry, but remote_name fetches are in
1121 * progress
1122 *
1123 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt
1124 * but instead wait for the cancel_cmpl_evt via the Busy Level
1125 *
1126 */
1127 if (btif_dm_inquiry_in_progress == FALSE)
1128 {
1129 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1130 }
1131 }
1132 break;
1133 }
1134}
1135
1136/*******************************************************************************
1137**
1138** Function btif_dm_search_services_evt
1139**
1140** Description Executes search services event in btif context
1141**
1142** Returns void
1143**
1144*******************************************************************************/
1145static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1146{
1147 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1148
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001149 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001150 switch (event)
1151 {
1152 case BTA_DM_DISC_RES_EVT:
1153 {
1154 bt_uuid_t uuid_arr[BT_MAX_NUM_UUIDS]; /* Max 32 services */
1155 bt_property_t prop;
1156 uint32_t i = 0, j = 0;
1157 bt_bdaddr_t bd_addr;
1158 bt_status_t ret;
1159
1160 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1161
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001162 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001163 p_data->disc_res.result, p_data->disc_res.services);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001164 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1165 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1166 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1167 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001168 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001169 pairing_cb.sdp_attempts++;
1170 btif_dm_get_remote_services(&bd_addr);
1171 return;
1172 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001173 prop.type = BT_PROPERTY_UUIDS;
1174 prop.len = 0;
1175 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1176 {
1177 prop.val = p_data->disc_res.p_uuid_list;
1178 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1179 for (i=0; i < p_data->disc_res.num_uuids; i++)
1180 {
1181 char temp[256];
1182 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 -07001183 BTIF_TRACE_ERROR("Index: %d uuid:%s", i, temp);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001184 }
1185 }
1186
1187 /* onUuidChanged requires getBondedDevices to be populated.
1188 ** bond_state_changed needs to be sent prior to remote_device_property
1189 */
1190 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1191 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0)&&
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001192 pairing_cb.sdp_attempts > 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001193 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001194 BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001195 __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001196 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001197 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1198 }
1199
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001200 if(p_data->disc_res.num_uuids != 0)
1201 {
1202 /* Also write this to the NVRAM */
1203 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1204 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1205 /* Send the event to the BTIF */
1206 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1207 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1208 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001209 }
1210 break;
1211
1212 case BTA_DM_DISC_CMPL_EVT:
1213 /* fixme */
1214 break;
1215
Matthew Xie607e3b72013-08-15 19:30:48 -07001216#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001217 case BTA_DM_DISC_BLE_RES_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001218 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__,
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001219 p_data->disc_ble_res.service.uu.uuid16);
1220 bt_uuid_t uuid;
1221 int i = 0;
1222 int j = 15;
1223 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1224 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001225 BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001226 bt_property_t prop;
1227 bt_bdaddr_t bd_addr;
1228 char temp[256];
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001229 bt_status_t ret;
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001230
1231 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1232
1233 while(i < j )
1234 {
1235 unsigned char c = uuid.uu[j];
1236 uuid.uu[j] = uuid.uu[i];
1237 uuid.uu[i] = c;
1238 i++;
1239 j--;
1240 }
1241
1242 uuid_to_string(&uuid, temp);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001243 BTIF_TRACE_ERROR(" uuid:%s", temp);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001244
1245 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1246 prop.type = BT_PROPERTY_UUIDS;
1247 prop.val = uuid.uu;
1248 prop.len = MAX_UUID_SIZE;
1249
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001250 /* Also write this to the NVRAM */
1251 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1252 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1253
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001254 /* Send the event to the BTIF */
1255 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1256 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1257
1258 }
1259 break;
Matthew Xie607e3b72013-08-15 19:30:48 -07001260#endif /* BLE_INCLUDED */
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001261
The Android Open Source Project5738f832012-12-12 16:00:35 -08001262 default:
1263 {
1264 ASSERTC(0, "unhandled search services event", event);
1265 }
1266 break;
1267 }
1268}
1269
1270/*******************************************************************************
1271**
1272** Function btif_dm_remote_service_record_evt
1273**
1274** Description Executes search service record event in btif context
1275**
1276** Returns void
1277**
1278*******************************************************************************/
1279static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1280{
1281 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1282
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001283 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001284 switch (event)
1285 {
1286 case BTA_DM_DISC_RES_EVT:
1287 {
1288 bt_service_record_t rec;
1289 bt_property_t prop;
1290 uint32_t i = 0;
1291 bt_bdaddr_t bd_addr;
1292
1293 memset(&rec, 0, sizeof(bt_service_record_t));
1294 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1295
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001296 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001297 p_data->disc_res.result, p_data->disc_res.services);
1298 prop.type = BT_PROPERTY_SERVICE_RECORD;
1299 prop.val = (void*)&rec;
1300 prop.len = sizeof(rec);
1301
1302 /* disc_res.result is overloaded with SCN. Cannot check result */
1303 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1304 /* TODO: Get the UUID as well */
1305 rec.channel = p_data->disc_res.result - 3;
1306 /* TODO: Need to get the service name using p_raw_data */
1307 rec.name[0] = 0;
1308
1309 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1310 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1311 }
1312 break;
1313
1314 default:
1315 {
1316 ASSERTC(0, "unhandled remote service record event", event);
1317 }
1318 break;
1319 }
1320}
1321
1322/*******************************************************************************
1323**
1324** Function btif_dm_upstreams_cback
1325**
1326** Description Executes UPSTREAMS events in btif context
1327**
1328** Returns void
1329**
1330*******************************************************************************/
1331static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1332{
1333 tBTA_DM_SEC_EVT dm_event = (tBTA_DM_SEC_EVT)event;
1334 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1335 tBTA_SERVICE_MASK service_mask;
1336 uint32_t i;
1337 bt_bdaddr_t bd_addr;
1338
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001339 BTIF_TRACE_EVENT("btif_dm_upstreams_cback ev: %s", dump_dm_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001340
1341 switch (event)
1342 {
1343 case BTA_DM_ENABLE_EVT:
1344 {
1345 BD_NAME bdname;
1346 bt_status_t status;
1347 bt_property_t prop;
1348 prop.type = BT_PROPERTY_BDNAME;
1349 prop.len = BD_NAME_LEN;
1350 prop.val = (void*)bdname;
1351
1352 status = btif_storage_get_adapter_property(&prop);
Matthew Xie1e5109b2012-11-09 18:26:26 -08001353 if (status == BT_STATUS_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001354 {
1355 /* A name exists in the storage. Make this the device name */
1356 BTA_DmSetDeviceName((char*)prop.val);
1357 }
Matthew Xie1e5109b2012-11-09 18:26:26 -08001358 else
1359 {
1360 /* Storage does not have a name yet.
1361 * Use the default name and write it to the chip
1362 */
1363 BTA_DmSetDeviceName(btif_get_default_local_name());
1364 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001365
1366 /* for each of the enabled services in the mask, trigger the profile
1367 * enable */
1368 service_mask = btif_get_enabled_services_mask();
1369 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1370 {
1371 if (service_mask &
1372 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1373 {
1374 btif_in_execute_service_request(i, TRUE);
1375 }
1376 }
1377 /* clear control blocks */
1378 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1379
1380 /* This function will also trigger the adapter_properties_cb
1381 ** and bonded_devices_info_cb
1382 */
1383 btif_storage_load_bonded_devices();
1384
1385 btif_storage_load_autopair_device_list();
1386
1387 btif_enable_bluetooth_evt(p_data->enable.status, p_data->enable.bd_addr);
Satya Calloji0943c102014-05-12 09:13:02 -07001388
Wei Wangbf0e4b22014-05-19 23:23:35 -07001389 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Calloji0943c102014-05-12 09:13:02 -07001390 /* Enable local privacy */
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001391 /*TODO Should this call be exposed to JAVA...? */
Satya Calloji0943c102014-05-12 09:13:02 -07001392 BTA_DmBleConfigLocalPrivacy(TRUE);
Wei Wangbf0e4b22014-05-19 23:23:35 -07001393 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001394 }
1395 break;
1396
1397 case BTA_DM_DISABLE_EVT:
1398 /* for each of the enabled services in the mask, trigger the profile
1399 * disable */
1400 service_mask = btif_get_enabled_services_mask();
1401 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1402 {
1403 if (service_mask &
1404 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1405 {
1406 btif_in_execute_service_request(i, FALSE);
1407 }
1408 }
1409 btif_disable_bluetooth_evt();
1410 break;
1411
1412 case BTA_DM_PIN_REQ_EVT:
1413 btif_dm_pin_req_evt(&p_data->pin_req);
1414 break;
1415
1416 case BTA_DM_AUTH_CMPL_EVT:
1417 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1418 break;
1419
1420 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1421 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1422 {
1423 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1424 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1425 }
1426 break;
1427
1428 case BTA_DM_SP_CFM_REQ_EVT:
1429 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1430 break;
1431 case BTA_DM_SP_KEY_NOTIF_EVT:
1432 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1433 break;
1434
1435 case BTA_DM_DEV_UNPAIRED_EVT:
1436 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1437
1438 /*special handling for HID devices */
1439 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -07001440 btif_hh_remove_device(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001441 #endif
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001442 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1443 btif_storage_remove_ble_bonding_keys(&bd_addr);
1444 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001445 btif_storage_remove_bonded_device(&bd_addr);
1446 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1447 break;
1448
1449 case BTA_DM_BUSY_LEVEL_EVT:
1450 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001451
1452 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001453 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001454 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001455 {
1456 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1457 BT_DISCOVERY_STARTED);
1458 btif_dm_inquiry_in_progress = TRUE;
1459 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001460 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001461 {
1462 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1463 BT_DISCOVERY_STOPPED);
1464 btif_dm_inquiry_in_progress = FALSE;
1465 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001466 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001467 {
1468 btif_dm_inquiry_in_progress = FALSE;
1469 }
1470 }
1471 }break;
1472
1473 case BTA_DM_LINK_UP_EVT:
1474 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001475 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001476
1477 btif_update_remote_version_property(&bd_addr);
1478
The Android Open Source Project5738f832012-12-12 16:00:35 -08001479 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1480 &bd_addr, BT_ACL_STATE_CONNECTED);
1481 break;
1482
1483 case BTA_DM_LINK_DOWN_EVT:
1484 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001485 BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001486 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1487 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1488 break;
1489
1490 case BTA_DM_HW_ERROR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001491 BTIF_TRACE_ERROR("Received H/W Error. ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001492 /* Flush storage data */
1493 btif_config_flush();
1494 usleep(100000); /* 100milliseconds */
1495 /* Killing the process to force a restart as part of fault tolerance */
1496 kill(getpid(), SIGKILL);
1497 break;
1498
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001499#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1500 case BTA_DM_BLE_KEY_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001501 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 -08001502
1503 /* If this pairing is by-product of local initiated GATT client Read or Write,
1504 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1505 have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1506 if (pairing_cb.state != BT_BOND_STATE_BONDING)
1507 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001508 BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001509 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1510 BT_BOND_STATE_BONDING);
1511 }
1512 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1513 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001514 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001515 break;
1516 }
1517
1518 switch (p_data->ble_key.key_type)
1519 {
1520 case BTA_LE_KEY_PENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001521 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001522 pairing_cb.ble.is_penc_key_rcvd = TRUE;
1523 memcpy(pairing_cb.ble.penc_key.ltk,p_data->ble_key.key_value.penc_key.ltk, 16);
1524 memcpy(pairing_cb.ble.penc_key.rand, p_data->ble_key.key_value.penc_key.rand,8);
1525 pairing_cb.ble.penc_key.ediv = p_data->ble_key.key_value.penc_key.ediv;
1526 pairing_cb.ble.penc_key.sec_level = p_data->ble_key.key_value.penc_key.sec_level;
1527
1528 for (i=0; i<16; i++)
1529 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001530 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 -08001531 }
1532 for (i=0; i<8; i++)
1533 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001534 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 -08001535 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001536 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.ediv=0x%04x",pairing_cb.ble.penc_key.ediv);
1537 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.sec_level=0x%02x",pairing_cb.ble.penc_key.sec_level);
1538 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 -08001539 break;
1540
1541 case BTA_LE_KEY_PID:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001542 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001543 pairing_cb.ble.is_pid_key_rcvd = TRUE;
1544 memcpy(pairing_cb.ble.pid_key, p_data->ble_key.key_value.pid_key.irk, 16);
1545 for (i=0; i<16; i++)
1546 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001547 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 -08001548 }
1549 break;
1550
1551 case BTA_LE_KEY_PCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001552 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001553 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1554 pairing_cb.ble.pcsrk_key.counter = p_data->ble_key.key_value.pcsrk_key.counter;
1555 pairing_cb.ble.pcsrk_key.sec_level = p_data->ble_key.key_value.pcsrk_key.sec_level;
1556 memcpy(pairing_cb.ble.pcsrk_key.csrk,p_data->ble_key.key_value.pcsrk_key.csrk,16);
1557
1558 for (i=0; i<16; i++)
1559 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001560 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 -08001561 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001562 BTIF_TRACE_DEBUG("pairing_cb.ble.pcsrk_key.counter=0x%08x",pairing_cb.ble.pcsrk_key.counter);
1563 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 -08001564 break;
1565
1566 case BTA_LE_KEY_LENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001567 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001568 pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1569 pairing_cb.ble.lenc_key.div = p_data->ble_key.key_value.lenc_key.div;
1570 pairing_cb.ble.lenc_key.key_size = p_data->ble_key.key_value.lenc_key.key_size;
1571 pairing_cb.ble.lenc_key.sec_level = p_data->ble_key.key_value.lenc_key.sec_level;
1572
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001573 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.div=0x%04x",pairing_cb.ble.lenc_key.div);
1574 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.key_size=0x%02x",pairing_cb.ble.lenc_key.key_size);
1575 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 -08001576 break;
1577
1578
1579
1580 case BTA_LE_KEY_LCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001581 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001582 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1583 pairing_cb.ble.lcsrk_key.counter = p_data->ble_key.key_value.lcsrk_key.counter;
1584 pairing_cb.ble.lcsrk_key.div = p_data->ble_key.key_value.lcsrk_key.div;
1585 pairing_cb.ble.lcsrk_key.sec_level = p_data->ble_key.key_value.lcsrk_key.sec_level;
1586
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001587 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.div=0x%04x",pairing_cb.ble.lcsrk_key.div);
1588 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.counter=0x%08x",pairing_cb.ble.lcsrk_key.counter);
1589 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 -08001590
1591 break;
1592
1593 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001594 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001595 break;
1596 }
1597
1598 break;
1599 case BTA_DM_BLE_SEC_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001600 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001601 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1602 break;
1603 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001604 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001605 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1606 break;
1607 case BTA_DM_BLE_PASSKEY_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001608 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001609 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1610 break;
1611 case BTA_DM_BLE_OOB_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001612 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001613 break;
1614 case BTA_DM_BLE_LOCAL_IR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001615 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001616 ble_local_key_cb.is_id_keys_rcvd = TRUE;
1617 memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1618 memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1619 memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1620 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1621 BTIF_DM_LE_LOCAL_KEY_IR,
1622 BT_OCTET16_LEN);
1623 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1624 BTIF_DM_LE_LOCAL_KEY_IRK,
1625 BT_OCTET16_LEN);
1626 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1627 BTIF_DM_LE_LOCAL_KEY_DHK,
1628 BT_OCTET16_LEN);
1629 break;
1630 case BTA_DM_BLE_LOCAL_ER_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001631 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001632 ble_local_key_cb.is_er_rcvd = TRUE;
1633 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1634 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1635 BTIF_DM_LE_LOCAL_KEY_ER,
1636 BT_OCTET16_LEN);
1637 break;
1638
1639 case BTA_DM_BLE_AUTH_CMPL_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001640 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001641 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1642 break;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001643
1644 case BTA_DM_LE_FEATURES_READ:
1645 {
1646 tBTM_BLE_VSC_CB cmn_vsc_cb;
1647 bt_local_le_features_t local_le_features;
1648 char buf[512];
1649 bt_property_t prop;
1650 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1651 prop.val = (void*)buf;
1652 prop.len = sizeof(buf);
1653
1654 /* LE features are not stored in storage. Should be retrived from stack */
1655 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1656 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1657
1658 prop.len = sizeof (bt_local_le_features_t);
1659 if (cmn_vsc_cb.filter_support == 1)
1660 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1661 else
1662 local_le_features.max_adv_filter_supported = 0;
1663 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1664 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1665 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
1666 local_le_features.scan_result_storage_size = cmn_vsc_cb.tot_scan_results_strg;
1667 memcpy(prop.val, &local_le_features, prop.len);
1668 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
1669 break;
1670 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001671#endif
1672
The Android Open Source Project5738f832012-12-12 16:00:35 -08001673 case BTA_DM_AUTHORIZE_EVT:
1674 case BTA_DM_SIG_STRENGTH_EVT:
1675 case BTA_DM_SP_RMT_OOB_EVT:
1676 case BTA_DM_SP_KEYPRESS_EVT:
1677 case BTA_DM_ROLE_CHG_EVT:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001678
The Android Open Source Project5738f832012-12-12 16:00:35 -08001679 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001680 BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001681 break;
1682 }
1683} /* btui_security_cback() */
1684
1685
1686/*******************************************************************************
1687**
1688** Function btif_dm_generic_evt
1689**
1690** Description Executes non-BTA upstream events in BTIF context
1691**
1692** Returns void
1693**
1694*******************************************************************************/
1695static void btif_dm_generic_evt(UINT16 event, char* p_param)
1696{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001697 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001698 switch(event)
1699 {
1700 case BTIF_DM_CB_DISCOVERY_STARTED:
1701 {
1702 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
1703 }
1704 break;
1705
1706 case BTIF_DM_CB_CREATE_BOND:
1707 {
1708 btif_dm_cb_create_bond((bt_bdaddr_t *)p_param);
1709 }
1710 break;
1711
1712 case BTIF_DM_CB_REMOVE_BOND:
1713 {
1714 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
1715 }
1716 break;
1717
1718 case BTIF_DM_CB_HID_REMOTE_NAME:
1719 {
1720 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
1721 }
1722 break;
1723
1724 case BTIF_DM_CB_BOND_STATE_BONDING:
1725 {
1726 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
1727 }
1728 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001729 case BTIF_DM_CB_LE_TX_TEST:
1730 case BTIF_DM_CB_LE_RX_TEST:
1731 {
1732 uint8_t status;
1733 STREAM_TO_UINT8(status, p_param);
1734 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1735 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
1736 }
1737 break;
1738 case BTIF_DM_CB_LE_TEST_END:
1739 {
1740 uint8_t status;
1741 uint16_t count = 0;
1742 STREAM_TO_UINT8(status, p_param);
1743 if (status == 0)
1744 STREAM_TO_UINT16(count, p_param);
1745 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1746 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
1747 }
1748 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001749 default:
1750 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001751 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001752 }
1753 break;
1754 }
1755}
1756
1757/*******************************************************************************
1758**
1759** Function bte_dm_evt
1760**
1761** Description Switches context from BTE to BTIF for all DM events
1762**
1763** Returns void
1764**
1765*******************************************************************************/
1766
1767void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
1768{
1769 bt_status_t status;
1770
1771 /* switch context to btif task context (copy full union size for convenience) */
1772 status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event, (void*)p_data, sizeof(tBTA_DM_SEC), NULL);
1773
1774 /* catch any failed context transfers */
1775 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
1776}
1777
1778/*******************************************************************************
1779**
1780** Function bte_search_devices_evt
1781**
1782** Description Switches context from BTE to BTIF for DM search events
1783**
1784** Returns void
1785**
1786*******************************************************************************/
1787static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1788{
1789 UINT16 param_len = 0;
1790
1791 if (p_data)
1792 param_len += sizeof(tBTA_DM_SEARCH);
1793 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
1794 switch (event)
1795 {
1796 case BTA_DM_INQ_RES_EVT:
1797 {
1798 if (p_data->inq_res.p_eir)
1799 param_len += HCI_EXT_INQ_RESPONSE_LEN;
1800 }
1801 break;
1802
1803 case BTA_DM_DISC_RES_EVT:
1804 {
1805 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
1806 param_len += p_data->disc_res.raw_data_size;
1807 }
1808 break;
1809 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001810 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 -08001811
1812 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
1813 if (event == BTA_DM_INQ_RES_EVT)
1814 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
1815
1816 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
1817 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
1818}
1819
1820/*******************************************************************************
1821**
1822** Function bte_dm_search_services_evt
1823**
1824** Description Switches context from BTE to BTIF for DM search services
1825** event
1826**
1827** Returns void
1828**
1829*******************************************************************************/
1830static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1831{
1832 UINT16 param_len = 0;
1833 if (p_data)
1834 param_len += sizeof(tBTA_DM_SEARCH);
1835 switch (event)
1836 {
1837 case BTA_DM_DISC_RES_EVT:
1838 {
1839 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
1840 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
1841 }
1842 } break;
1843 }
1844 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
1845 * if raw_data is needed. */
1846 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
1847 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
1848}
1849
1850/*******************************************************************************
1851**
1852** Function bte_dm_remote_service_record_evt
1853**
1854** Description Switches context from BTE to BTIF for DM search service
1855** record event
1856**
1857** Returns void
1858**
1859*******************************************************************************/
1860static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1861{
1862 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
1863 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
1864}
1865
1866/*****************************************************************************
1867**
1868** btif api functions (no context switch)
1869**
1870*****************************************************************************/
1871
1872/*******************************************************************************
1873**
1874** Function btif_dm_start_discovery
1875**
1876** Description Start device discovery/inquiry
1877**
1878** Returns bt_status_t
1879**
1880*******************************************************************************/
1881bt_status_t btif_dm_start_discovery(void)
1882{
1883 tBTA_DM_INQ inq_params;
1884 tBTA_SERVICE_MASK services = 0;
1885
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001886 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001887 /* TODO: Do we need to handle multiple inquiries at the same time? */
1888
1889 /* Set inquiry params and call API */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001890#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001891 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
Matthew Xie7f3e4292013-09-30 12:44:10 -07001892#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
1893 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
1894 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
1895 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
1896 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
1897#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001898#else
1899 inq_params.mode = BTA_DM_GENERAL_INQUIRY;
1900#endif
1901 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
1902
1903 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
1904 inq_params.report_dup = TRUE;
1905
1906 inq_params.filter_type = BTA_DM_INQ_CLR;
1907 /* TODO: Filter device by BDA needs to be implemented here */
1908
1909 /* Will be enabled to TRUE once inquiry busy level has been received */
1910 btif_dm_inquiry_in_progress = FALSE;
1911 /* find nearby devices */
1912 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
1913
1914 return BT_STATUS_SUCCESS;
1915}
1916
1917/*******************************************************************************
1918**
1919** Function btif_dm_cancel_discovery
1920**
1921** Description Cancels search
1922**
1923** Returns bt_status_t
1924**
1925*******************************************************************************/
1926bt_status_t btif_dm_cancel_discovery(void)
1927{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001928 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001929 BTA_DmSearchCancel();
1930 return BT_STATUS_SUCCESS;
1931}
1932
1933/*******************************************************************************
1934**
1935** Function btif_dm_create_bond
1936**
1937** Description Initiate bonding with the specified device
1938**
1939** Returns bt_status_t
1940**
1941*******************************************************************************/
1942bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr)
1943{
1944 bdstr_t bdstr;
1945
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001946 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 -08001947 if (pairing_cb.state != BT_BOND_STATE_NONE)
1948 return BT_STATUS_BUSY;
1949
1950 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
1951 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
1952
1953 return BT_STATUS_SUCCESS;
1954}
1955
1956/*******************************************************************************
1957**
1958** Function btif_dm_cancel_bond
1959**
1960** Description Initiate bonding with the specified device
1961**
1962** Returns bt_status_t
1963**
1964*******************************************************************************/
1965
1966bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
1967{
1968 bdstr_t bdstr;
1969
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001970 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 -08001971
1972 /* TODO:
1973 ** 1. Restore scan modes
1974 ** 2. special handling for HID devices
1975 */
1976 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1977 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001978
1979#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1980
1981 if (pairing_cb.is_ssp)
1982 {
1983 if (pairing_cb.is_le_only)
1984 {
1985 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
1986 }
1987 else
Hemant Guptae1468692013-11-14 16:21:29 +05301988 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001989 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
Hemant Guptae1468692013-11-14 16:21:29 +05301990 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
1991 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
1992 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001993 }
1994 else
1995 {
1996 if (pairing_cb.is_le_only)
1997 {
1998 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
1999 }
2000 else
2001 {
2002 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2003 }
2004 /* Cancel bonding, in case it is in ACL connection setup state */
2005 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2006 }
2007
2008#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002009 if (pairing_cb.is_ssp)
2010 {
2011 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2012 }
2013 else
2014 {
2015 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2016 }
2017 /* Cancel bonding, in case it is in ACL connection setup state */
2018 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2019 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002020#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002021 }
2022
2023 return BT_STATUS_SUCCESS;
2024}
2025
2026/*******************************************************************************
2027**
Kim Schulza9eb25c2013-09-30 10:55:52 +02002028** Function btif_dm_hh_open_failed
2029**
2030** Description informs the upper layers if the HH have failed during bonding
2031**
2032** Returns none
2033**
2034*******************************************************************************/
2035
2036void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
2037{
2038 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2039 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2040 {
2041 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2042 }
2043}
2044
2045/*******************************************************************************
2046**
The Android Open Source Project5738f832012-12-12 16:00:35 -08002047** Function btif_dm_remove_bond
2048**
2049** Description Removes bonding with the specified device
2050**
2051** Returns bt_status_t
2052**
2053*******************************************************************************/
2054
2055bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
2056{
2057 bdstr_t bdstr;
2058
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002059 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 -08002060 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2061 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2062
2063 return BT_STATUS_SUCCESS;
2064}
2065
2066/*******************************************************************************
2067**
2068** Function btif_dm_pin_reply
2069**
2070** Description BT legacy pairing - PIN code reply
2071**
2072** Returns bt_status_t
2073**
2074*******************************************************************************/
2075
2076bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2077 uint8_t pin_len, bt_pin_code_t *pin_code)
2078{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002079 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Hemant Gupta831423e2014-01-08 12:42:13 +05302080 if (pin_code == NULL)
2081 return BT_STATUS_FAIL;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002082#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002083
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002084 if (pairing_cb.is_le_only)
2085 {
2086 int i;
2087 UINT32 passkey = 0;
2088 int multi[] = {100000, 10000, 1000, 100, 10,1};
2089 BD_ADDR remote_bd_addr;
2090 bdcpy(remote_bd_addr, bd_addr->address);
2091 for (i = 0; i < 6; i++)
2092 {
2093 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2094 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002095 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002096 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2097
2098 }
2099 else
2100 {
2101 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2102 if (accept)
2103 pairing_cb.pin_code_len = pin_len;
2104 }
2105#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002106 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2107
2108 if (accept)
2109 pairing_cb.pin_code_len = pin_len;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002110#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002111 return BT_STATUS_SUCCESS;
2112}
2113
2114/*******************************************************************************
2115**
2116** Function btif_dm_ssp_reply
2117**
2118** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2119**
2120** Returns bt_status_t
2121**
2122*******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002123bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2124 bt_ssp_variant_t variant, uint8_t accept,
2125 uint32_t passkey)
2126{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08002127 UNUSED(passkey);
2128
The Android Open Source Project5738f832012-12-12 16:00:35 -08002129 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2130 {
2131 /* This is not implemented in the stack.
2132 * For devices with display, this is not needed
2133 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002134 BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002135 return BT_STATUS_FAIL;
2136 }
2137 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002138 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002139#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2140 if (pairing_cb.is_le_only)
2141 {
2142 if (accept)
2143 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2144 else
2145 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2146 }
2147 else
2148 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002149
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002150#else
2151 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2152#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002153 return BT_STATUS_SUCCESS;
2154}
2155
2156/*******************************************************************************
2157**
2158** Function btif_dm_get_adapter_property
2159**
2160** Description Queries the BTA for the adapter property
2161**
2162** Returns bt_status_t
2163**
2164*******************************************************************************/
2165bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2166{
2167 bt_status_t status;
2168
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002169 BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002170 switch (prop->type)
2171 {
2172 case BT_PROPERTY_BDNAME:
2173 {
2174 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
Matthew Xie1e5109b2012-11-09 18:26:26 -08002175 strcpy((char *)bd_name->name, btif_get_default_local_name());
The Android Open Source Project5738f832012-12-12 16:00:35 -08002176 prop->len = strlen((char *)bd_name->name);
2177 }
2178 break;
2179
2180 case BT_PROPERTY_ADAPTER_SCAN_MODE:
2181 {
2182 /* if the storage does not have it. Most likely app never set it. Default is NONE */
2183 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2184 *mode = BT_SCAN_MODE_NONE;
2185 prop->len = sizeof(bt_scan_mode_t);
2186 }
2187 break;
2188
2189 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2190 {
2191 uint32_t *tmt = (uint32_t*)prop->val;
2192 *tmt = 120; /* default to 120s, if not found in NV */
2193 prop->len = sizeof(uint32_t);
2194 }
2195 break;
2196
2197 default:
2198 prop->len = 0;
2199 return BT_STATUS_FAIL;
2200 }
2201 return BT_STATUS_SUCCESS;
2202}
2203
2204/*******************************************************************************
2205**
2206** Function btif_dm_get_remote_services
2207**
2208** Description Start SDP to get remote services
2209**
2210** Returns bt_status_t
2211**
2212*******************************************************************************/
2213bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2214{
2215 bdstr_t bdstr;
2216
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002217 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002218
2219 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2220 bte_dm_search_services_evt, TRUE);
2221
2222 return BT_STATUS_SUCCESS;
2223}
2224
2225/*******************************************************************************
2226**
2227** Function btif_dm_get_remote_service_record
2228**
2229** Description Start SDP to get remote service record
2230**
2231**
2232** Returns bt_status_t
2233*******************************************************************************/
2234bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2235 bt_uuid_t *uuid)
2236{
2237 tSDP_UUID sdp_uuid;
2238 bdstr_t bdstr;
2239
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002240 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002241
2242 sdp_uuid.len = MAX_UUID_SIZE;
2243 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2244
2245 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2246 bte_dm_remote_service_record_evt, TRUE);
2247
2248 return BT_STATUS_SUCCESS;
2249}
2250
2251void btif_dm_execute_service_request(UINT16 event, char *p_param)
2252{
2253 BOOLEAN b_enable = FALSE;
2254 bt_status_t status;
2255 if (event == BTIF_DM_ENABLE_SERVICE)
2256 {
2257 b_enable = TRUE;
2258 }
2259 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2260 if (status == BT_STATUS_SUCCESS)
2261 {
2262 bt_property_t property;
2263 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2264
2265 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2266 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2267 sizeof(local_uuids), local_uuids);
2268 btif_storage_get_adapter_property(&property);
2269 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2270 BT_STATUS_SUCCESS, 1, &property);
2271 }
2272 return;
2273}
2274
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002275void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2276 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2277{
2278 UINT8 yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2279 /* if local initiated:
2280 ** 1. set DD + MITM
2281 ** if remote initiated:
2282 ** 1. Copy over the auth_req from peer's io_rsp
2283 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2284 ** as a fallback set MITM+GB if peer had MITM set
2285 */
2286 UNUSED (bd_addr);
2287 UNUSED (p_io_cap);
2288 UNUSED (p_oob_data);
2289
2290
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002291 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002292 if(pairing_cb.is_local_initiated)
2293 {
2294 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2295 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2296 }
2297 else if (!is_orig)
2298 {
2299 /* peer initiated paring. They probably know what they want.
2300 ** Copy the mitm from peer device.
2301 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002302 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002303 __FUNCTION__, pairing_cb.auth_req);
2304 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2305
2306 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2307 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2308 *p_auth_req |= BTA_AUTH_SP_YES;
2309 }
2310 else if (yes_no_bit)
2311 {
2312 /* set the general bonding bit for stored device */
2313 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2314 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002315 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002316}
2317
2318void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2319 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2320{
2321 UNUSED (bd_addr);
2322 UNUSED (oob_data);
2323
2324 if(auth_req & BTA_AUTH_BONDS)
2325 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002326 BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002327 pairing_cb.auth_req = auth_req;
2328 pairing_cb.io_cap = io_cap;
2329 }
2330}
2331
The Android Open Source Project5738f832012-12-12 16:00:35 -08002332#if (BTM_OOB_INCLUDED == TRUE)
2333void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data)
2334{
2335 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2336 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2337 {
2338 *p_oob_data = FALSE;
2339 }
2340 else
2341 {
2342 *p_oob_data = TRUE;
2343 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002344 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 -08002345}
2346#endif /* BTM_OOB_INCLUDED */
2347
2348#ifdef BTIF_DM_OOB_TEST
2349void btif_dm_load_local_oob(void)
2350{
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002351 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002352 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002353 BTIF_TRACE_DEBUG("btif_dm_load_local_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002354 if (prop_oob[0] != '3')
2355 {
2356#if (BTM_OOB_INCLUDED == TRUE)
2357 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2358 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2359 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002360 BTIF_TRACE_DEBUG("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002361 BTA_DmLocalOob();
2362 }
2363#else
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002364 BTIF_TRACE_ERROR("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002365#endif
2366 }
2367}
2368
2369void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2370{
2371 FILE *fp;
2372 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2373 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2374 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002375 char prop_oob[PROPERTY_VALUE_MAX];
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002376 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: valid=%d", valid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002377 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2378 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2379 valid)
2380 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002381 BTIF_TRACE_DEBUG("save local OOB data in memory");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002382 memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2383 memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2384 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002385 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002386 if (prop_oob[0] == '1')
2387 path = path_a;
2388 else if (prop_oob[0] == '2')
2389 path = path_b;
2390 if (path)
2391 {
2392 fp = fopen(path, "wb+");
2393 if (fp == NULL)
2394 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002395 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 -08002396 }
2397 else
2398 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002399 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 -08002400 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2401 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2402 fclose(fp);
2403 }
2404 }
2405 }
2406}
2407BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2408{
2409 char t[128];
2410 FILE *fp;
2411 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2412 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2413 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002414 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002415 BOOLEAN result = FALSE;
2416 bt_bdaddr_t bt_bd_addr;
2417 bdcpy(oob_cb.oob_bdaddr, bd_addr);
2418 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002419 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002420 if (prop_oob[0] == '1')
2421 path = path_b;
2422 else if (prop_oob[0] == '2')
2423 path = path_a;
2424 if (path)
2425 {
2426 fp = fopen(path, "rb");
2427 if (fp == NULL)
2428 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002429 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 -08002430 return FALSE;
2431 }
2432 else
2433 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002434 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob: read OOB data from %s",path);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002435 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2436 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2437 fclose(fp);
2438 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002439 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: TRUE");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002440 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2441 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2442 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002443 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002444 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2445 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
2446 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 -07002447 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: c = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002448 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2449 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
2450 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 -07002451 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: r = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002452 bdcpy(bt_bd_addr.address, bd_addr);
2453 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2454 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2455 result = TRUE;
2456 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002457 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob result=%d",result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002458 return result;
2459}
2460#endif /* BTIF_DM_OOB_TEST */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002461#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2462
2463static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2464{
2465 bt_bdaddr_t bd_addr;
2466 bt_bdname_t bd_name;
2467 UINT32 cod;
2468
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002469 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002470
2471 /* Remote name update */
2472 btif_update_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2473 NULL, BT_DEVICE_TYPE_BLE);
2474 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2475 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2476
2477 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2478 pairing_cb.is_ssp = FALSE;
2479 cod = COD_UNCLASSIFIED;
2480
2481 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2482 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2483 p_ssp_key_notif->passkey);
2484}
2485
2486/*******************************************************************************
2487**
2488** Function btif_dm_ble_auth_cmpl_evt
2489**
2490** Description Executes authentication complete event in btif context
2491**
2492** Returns void
2493**
2494*******************************************************************************/
2495static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2496{
2497 /* Save link key, if not temporary */
2498 bt_bdaddr_t bd_addr;
2499 bt_status_t status = BT_STATUS_FAIL;
2500 bt_bond_state_t state = BT_BOND_STATE_NONE;
2501
2502 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2503 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2504 {
2505 /* store keys */
2506 }
2507 if (p_auth_cmpl->success)
2508 {
2509 status = BT_STATUS_SUCCESS;
2510 state = BT_BOND_STATE_BONDED;
2511
2512 btif_dm_save_ble_bonding_keys();
2513 BTA_GATTC_Refresh(bd_addr.address);
2514 btif_dm_get_remote_services(&bd_addr);
2515 }
2516 else
2517 {
2518 /*Map the HCI fail reason to bt status */
2519 switch (p_auth_cmpl->fail_reason)
2520 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002521 default:
Andre Eisenbachca22ac42013-02-13 17:02:11 +09002522 btif_dm_remove_ble_bonding_keys();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002523 status = BT_STATUS_FAIL;
2524 break;
2525 }
2526 }
2527 bond_state_changed(status, &bd_addr, state);
2528}
2529
2530
2531
2532void btif_dm_load_ble_local_keys(void)
2533{
2534 bt_status_t bt_status;
2535
2536 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2537
2538 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2539 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2540 {
2541 ble_local_key_cb.is_er_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002542 BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002543 }
2544
2545 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
2546 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
2547 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
2548 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
2549 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
2550 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
2551 {
2552 ble_local_key_cb.is_id_keys_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002553 BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002554 }
2555
2556}
2557void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
2558 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
2559{
2560 if (ble_local_key_cb.is_er_rcvd )
2561 {
2562 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2563 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2564 }
2565
2566 if (ble_local_key_cb.is_id_keys_rcvd)
2567 {
2568 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
2569 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
2570 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
2571 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
2572 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002573 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002574}
2575
2576void btif_dm_save_ble_bonding_keys(void)
2577{
2578
2579 bt_bdaddr_t bd_addr;
2580
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002581 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002582
2583 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2584
2585 if (pairing_cb.ble.is_penc_key_rcvd)
2586 {
2587 btif_storage_add_ble_bonding_key(&bd_addr,
2588 (char *) &pairing_cb.ble.penc_key,
2589 BTIF_DM_LE_KEY_PENC,
2590 sizeof(btif_dm_ble_penc_keys_t));
2591 }
2592
2593 if (pairing_cb.ble.is_pid_key_rcvd)
2594 {
2595 btif_storage_add_ble_bonding_key(&bd_addr,
2596 (char *) &pairing_cb.ble.pid_key[0],
2597 BTIF_DM_LE_KEY_PID,
2598 BT_OCTET16_LEN);
2599 }
2600
2601
2602 if (pairing_cb.ble.is_pcsrk_key_rcvd)
2603 {
2604 btif_storage_add_ble_bonding_key(&bd_addr,
2605 (char *) &pairing_cb.ble.pcsrk_key,
2606 BTIF_DM_LE_KEY_PCSRK,
2607 sizeof(btif_dm_ble_pcsrk_keys_t));
2608 }
2609
2610
2611 if (pairing_cb.ble.is_lenc_key_rcvd)
2612 {
2613 btif_storage_add_ble_bonding_key(&bd_addr,
2614 (char *) &pairing_cb.ble.lenc_key,
2615 BTIF_DM_LE_KEY_LENC,
2616 sizeof(btif_dm_ble_lenc_keys_t));
2617 }
2618
2619 if (pairing_cb.ble.is_lcsrk_key_rcvd)
2620 {
2621 btif_storage_add_ble_bonding_key(&bd_addr,
2622 (char *) &pairing_cb.ble.lcsrk_key,
2623 BTIF_DM_LE_KEY_LCSRK,
2624 sizeof(btif_dm_ble_lcsrk_keys_t));
2625 }
2626
2627}
2628
2629
2630void btif_dm_remove_ble_bonding_keys(void)
2631{
2632 bt_bdaddr_t bd_addr;
2633
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002634 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002635
2636 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2637 btif_storage_remove_ble_bonding_keys(&bd_addr);
2638}
2639
2640
2641/*******************************************************************************
2642**
2643** Function btif_dm_ble_sec_req_evt
2644**
2645** Description Eprocess security request event in btif context
2646**
2647** Returns void
2648**
2649*******************************************************************************/
2650void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
2651{
2652 bt_bdaddr_t bd_addr;
2653 bt_bdname_t bd_name;
2654 UINT32 cod;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002655 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002656
2657 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2658 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002659 BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002660 return;
2661 }
2662
2663 /* Remote name update */
2664 btif_update_remote_properties(p_ble_req->bd_addr,p_ble_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
2665
2666 bdcpy(bd_addr.address, p_ble_req->bd_addr);
2667 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
2668
2669 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2670
2671 pairing_cb.is_temp = FALSE;
2672 pairing_cb.is_le_only = TRUE;
2673 pairing_cb.is_ssp = TRUE;
2674
2675 cod = COD_UNCLASSIFIED;
2676
2677 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
2678 BT_SSP_VARIANT_CONSENT, 0);
2679}
2680
2681
2682
2683/*******************************************************************************
2684**
2685** Function btif_dm_ble_passkey_req_evt
2686**
2687** Description Executes pin request event in btif context
2688**
2689** Returns void
2690**
2691*******************************************************************************/
2692static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
2693{
2694 bt_bdaddr_t bd_addr;
2695 bt_bdname_t bd_name;
2696 UINT32 cod;
2697
2698 /* Remote name update */
2699 btif_update_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
2700
2701 bdcpy(bd_addr.address, p_pin_req->bd_addr);
2702 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
2703
2704 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2705 pairing_cb.is_le_only = TRUE;
2706
2707 cod = COD_UNCLASSIFIED;
2708
2709 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
2710 &bd_addr, &bd_name, cod);
2711}
2712
2713
2714void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
2715 tBT_DEVICE_TYPE dev_type)
2716{
2717 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
2718}
2719
2720static void btif_dm_ble_tx_test_cback(void *p)
2721{
2722 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
2723 (char *)p, 1, NULL);
2724}
2725
2726static void btif_dm_ble_rx_test_cback(void *p)
2727{
2728 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
2729 (char *)p, 1, NULL);
2730}
2731
2732static void btif_dm_ble_test_end_cback(void *p)
2733{
2734 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
2735 (char *)p, 3, NULL);
2736}
2737/*******************************************************************************
2738**
2739** Function btif_le_test_mode
2740**
2741** Description Sends a HCI BLE Test command to the Controller
2742**
2743** Returns BT_STATUS_SUCCESS on success
2744**
2745*******************************************************************************/
2746bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
2747{
2748 switch (opcode) {
2749 case HCI_BLE_TRANSMITTER_TEST:
2750 if (len != 3) return BT_STATUS_PARM_INVALID;
2751 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
2752 break;
2753 case HCI_BLE_RECEIVER_TEST:
2754 if (len != 1) return BT_STATUS_PARM_INVALID;
2755 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
2756 break;
2757 case HCI_BLE_TEST_END:
2758 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
2759 break;
2760 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002761 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002762 return BT_STATUS_UNSUPPORTED;
2763 }
2764 return BT_STATUS_SUCCESS;
2765}
2766
2767#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002768
2769void btif_dm_on_disable()
2770{
2771 /* cancel any pending pairing requests */
2772 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2773 {
2774 bt_bdaddr_t bd_addr;
2775
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002776 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002777 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2778 btif_dm_cancel_bond(&bd_addr);
2779 }
2780}
Matthew Xie1e5109b2012-11-09 18:26:26 -08002781
2782static char* btif_get_default_local_name() {
2783 if (btif_default_local_name[0] == '\0')
2784 {
2785 int max_len = sizeof(btif_default_local_name) - 1;
2786 if (BTM_DEF_LOCAL_NAME[0] != '\0')
2787 {
2788 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
2789 }
2790 else
2791 {
2792 char prop_model[PROPERTY_VALUE_MAX];
2793 property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
2794 strncpy(btif_default_local_name, prop_model, max_len);
2795 }
2796 btif_default_local_name[max_len] = '\0';
2797 }
2798 return btif_default_local_name;
2799}