blob: f72ed324588259281b00f416647b69f523987cd2 [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);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800162extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700163extern 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 -0800164
165
166/******************************************************************************
167** Functions
168******************************************************************************/
169
170bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
171 BOOLEAN b_enable)
172{
173 /* Check the service_ID and invoke the profile's BT state changed API */
174 switch (service_id)
175 {
176 case BTA_HFP_SERVICE_ID:
177 case BTA_HSP_SERVICE_ID:
178 {
179 btif_hf_execute_service(b_enable);
180 }break;
181 case BTA_A2DP_SERVICE_ID:
182 {
183 btif_av_execute_service(b_enable);
184 }break;
185 case BTA_HID_SERVICE_ID:
186 {
187 btif_hh_execute_service(b_enable);
188 }break;
Hemant Gupta10256872013-08-19 18:33:01 +0530189 case BTA_HFP_HS_SERVICE_ID:
190 {
191 btif_hf_client_execute_service(b_enable);
192 }break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800193 default:
194 BTIF_TRACE_ERROR1("%s: Unknown service being enabled", __FUNCTION__);
195 return BT_STATUS_FAIL;
196 }
197 return BT_STATUS_SUCCESS;
198}
199
200/*******************************************************************************
201**
202** Function check_eir_remote_name
203**
204** Description Check if remote name is in the EIR data
205**
206** Returns TRUE if remote name found
207** Populate p_remote_name, if provided and remote name found
208**
209*******************************************************************************/
210static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
211 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
212{
213 UINT8 *p_eir_remote_name = NULL;
214 UINT8 remote_name_len = 0;
215
216 /* Check EIR for remote name and services */
217 if (p_search_data->inq_res.p_eir)
218 {
219 p_eir_remote_name = BTA_CheckEirData(p_search_data->inq_res.p_eir,
220 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
221 if (!p_eir_remote_name)
222 {
223 p_eir_remote_name = BTA_CheckEirData(p_search_data->inq_res.p_eir,
224 BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
225 }
226
227 if (p_eir_remote_name)
228 {
229 if (remote_name_len > BD_NAME_LEN)
230 remote_name_len = BD_NAME_LEN;
231
232 if (p_remote_name && p_remote_name_len)
233 {
234 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
235 *(p_remote_name + remote_name_len) = 0;
236 *p_remote_name_len = remote_name_len;
237 }
238
239 return TRUE;
240 }
241 }
242
243 return FALSE;
244
245}
246
247/*******************************************************************************
248**
249** Function check_cached_remote_name
250**
251** Description Check if remote name is in the NVRAM cache
252**
253** Returns TRUE if remote name found
254** Populate p_remote_name, if provided and remote name found
255**
256*******************************************************************************/
257static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
258 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
259{
260 bt_bdname_t bdname;
261 bt_bdaddr_t remote_bdaddr;
262 bt_property_t prop_name;
263
264 /* check if we already have it in our btif_storage cache */
265 bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
266 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
267 sizeof(bt_bdname_t), &bdname);
268 if (btif_storage_get_remote_device_property(
269 &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
270 {
271 if (p_remote_name && p_remote_name_len)
272 {
273 strcpy((char *)p_remote_name, (char *)bdname.name);
274 *p_remote_name_len = strlen((char *)p_remote_name);
275 }
276 return TRUE;
277 }
278
279 return FALSE;
280}
281
282BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
283{
284 uint32_t remote_cod;
285 bt_property_t prop_name;
286
287 /* check if we already have it in our btif_storage cache */
288 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
289 sizeof(uint32_t), &remote_cod);
290 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
291 {
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530292 BTIF_TRACE_ERROR2("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800293 if ((remote_cod & 0x7ff) == cod)
294 return TRUE;
295 }
296
297 return FALSE;
298}
299
Priti Agheraebb1d752012-11-27 18:03:22 -0800300BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
301{
302 uint32_t remote_cod;
303 bt_property_t prop_name;
304
305 /* check if we already have it in our btif_storage cache */
306 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
307 sizeof(uint32_t), &remote_cod);
308 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
309 &prop_name) == BT_STATUS_SUCCESS)
310 {
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530311 BTIF_TRACE_DEBUG2("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
Priti Agheraebb1d752012-11-27 18:03:22 -0800312 if ((remote_cod & 0x700) == cod)
313 return TRUE;
314 }
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700315 return FALSE;
316}
Priti Agheraebb1d752012-11-27 18:03:22 -0800317
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700318BOOLEAN check_hid_le(const bt_bdaddr_t *remote_bdaddr)
319{
320 uint32_t remote_dev_type;
321 bt_property_t prop_name;
322
323 /* check if we already have it in our btif_storage cache */
324 BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE,
325 sizeof(uint32_t), &remote_dev_type);
326 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
327 &prop_name) == BT_STATUS_SUCCESS)
328 {
329 if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE)
330 {
331 bdstr_t bdstr;
332 bd2str(remote_bdaddr, &bdstr);
333 if(btif_config_exist("Remote", bdstr, "HidAppId"))
334 return TRUE;
335 }
336 }
Priti Agheraebb1d752012-11-27 18:03:22 -0800337 return FALSE;
338}
339
The Android Open Source Project5738f832012-12-12 16:00:35 -0800340static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
341{
342 /* Send bonding state only once - based on outgoing/incoming we may receive duplicates */
343 if ( (pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING) )
344 return;
345
346 if (pairing_cb.is_temp)
347 {
348 state = BT_BOND_STATE_NONE;
349 }
350 BTIF_TRACE_DEBUG3("%s: state=%d prev_state=%d", __FUNCTION__, state, pairing_cb.state);
351
352 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
353
354 if (state == BT_BOND_STATE_BONDING)
355 {
356 pairing_cb.state = state;
357 bdcpy(pairing_cb.bd_addr, bd_addr->address);
358 }
359 else
360 {
361 memset(&pairing_cb, 0, sizeof(pairing_cb));
362 }
363
364}
365
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800366/* store remote version in bt config to always have access
367 to it post pairing*/
368static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
369{
370 bt_property_t property;
371 UINT8 lmp_ver = 0;
372 UINT16 lmp_subver = 0;
373 UINT16 mfct_set = 0;
374 tBTM_STATUS btm_status;
375 bt_remote_version_t info;
376 bt_status_t status;
377 bdstr_t bdstr;
378
379 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
380 &mfct_set, &lmp_subver);
381
382 ALOGD("remote version info [%s]: %x, %x, %x", bd2str(p_bd, &bdstr),
383 lmp_ver, mfct_set, lmp_subver);
384
385 if (btm_status == BTM_SUCCESS)
386 {
387 /* always update cache to ensure we have availability whenever BTM API
388 is not populated */
389 info.manufacturer = mfct_set;
390 info.sub_ver = lmp_subver;
391 info.version = lmp_ver;
392 BTIF_STORAGE_FILL_PROPERTY(&property,
393 BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
394 &info);
395 status = btif_storage_set_remote_device_property(p_bd, &property);
396 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
397 }
398}
399
The Android Open Source Project5738f832012-12-12 16:00:35 -0800400
401static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
402 DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
403{
404 int num_properties = 0;
405 bt_property_t properties[3];
406 bt_bdaddr_t bdaddr;
407 bt_status_t status;
408 UINT32 cod;
409 bt_device_type_t dev_type;
410
411 memset(properties, 0, sizeof(properties));
412 bdcpy(bdaddr.address, bd_addr);
413
414 /* remote name */
415 if (strlen((const char *) bd_name))
416 {
417 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
418 BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
419 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
420 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
421 num_properties++;
422 }
423
424 /* class of device */
425 cod = devclass2uint(dev_class);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530426 BTIF_TRACE_DEBUG2("%s():cod is 0x%06x", __FUNCTION__, cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800427 if ( cod == 0) {
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530428 /* Try to retrieve cod from storage */
429 BTIF_TRACE_DEBUG1("%s():cod is 0, checking cod from storage", __FUNCTION__);
430 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
431 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
432 status = btif_storage_get_remote_device_property(&bdaddr, &properties[num_properties]);
433 BTIF_TRACE_DEBUG2("%s():cod retreived from storage is 0x%06x", __FUNCTION__, cod);
434 if ( cod == 0) {
435 BTIF_TRACE_DEBUG1("%s():cod is again 0, set as unclassified", __FUNCTION__);
436 cod = COD_UNCLASSIFIED;
437 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800438 }
439
440 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
441 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
442 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
443 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
444 num_properties++;
445
446 /* device type */
447 dev_type = device_type;
448 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
449 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
450 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
451 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
452 num_properties++;
453
454 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
455 status, &bdaddr, num_properties, properties);
456}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800457
458/*******************************************************************************
459**
460** Function btif_dm_cb_hid_remote_name
461**
462** Description Remote name callback for HID device. Called in btif context
463** Special handling for HID devices
464**
465** Returns void
466**
467*******************************************************************************/
468static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
469{
470 BTIF_TRACE_DEBUG3("%s: status=%d pairing_cb.state=%d", __FUNCTION__, p_remote_name->status, pairing_cb.state);
471 if (pairing_cb.state == BT_BOND_STATE_BONDING)
472 {
473 bt_bdaddr_t remote_bd;
474
475 bdcpy(remote_bd.address, pairing_cb.bd_addr);
476
477 if (p_remote_name->status == BTM_SUCCESS)
478 {
479 bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
480 }
481 else
482 bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
483 }
484}
485
The Android Open Source Project5738f832012-12-12 16:00:35 -0800486/*******************************************************************************
487**
488** Function btif_dm_cb_create_bond
489**
490** Description Create bond initiated from the BTIF thread context
491** Special handling for HID devices
492**
493** Returns void
494**
495*******************************************************************************/
496static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr)
497{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800498 BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
499
500
The Android Open Source Project5738f832012-12-12 16:00:35 -0800501 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800502
503 if (is_hid){
504
The Android Open Source Project5738f832012-12-12 16:00:35 -0800505 int status;
506 status = btif_hh_connect(bd_addr);
507 if(status != BT_STATUS_SUCCESS)
508 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
509 }
510 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800511 {
Matthew Xiefc4b2f12013-05-06 20:51:02 -0700512#if BLE_INCLUDED == TRUE
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800513 int device_type;
514 int addr_type;
515 bdstr_t bdstr;
516 bd2str(bd_addr, &bdstr);
517 if(btif_config_get_int("Remote", (char const *)&bdstr,"DevType", &device_type) &&
518 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
519 (device_type == BT_DEVICE_TYPE_BLE))
520 {
521 BTA_DmAddBleDevice(bd_addr->address, addr_type, BT_DEVICE_TYPE_BLE);
522 }
Matthew Xiefc4b2f12013-05-06 20:51:02 -0700523#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800524 BTA_DmBond ((UINT8 *)bd_addr->address);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800525 }
526 /* Track originator of bond creation */
527 pairing_cb.is_local_initiated = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800528
529}
530
531/*******************************************************************************
532**
533** Function btif_dm_cb_remove_bond
534**
535** Description remove bond initiated from the BTIF thread context
536** Special handling for HID devices
537**
538** Returns void
539**
540*******************************************************************************/
541void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
542{
543 bdstr_t bdstr;
544 /*special handling for HID devices */
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700545 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there
546 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
547#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
548 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
549#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800550 {
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700551 BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800552 }
553}
554
555/*******************************************************************************
556**
557** Function search_devices_copy_cb
558**
559** Description Deep copy callback for search devices event
560**
561** Returns void
562**
563*******************************************************************************/
564static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
565{
566 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
567 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
568
569 if (!p_src)
570 return;
571
572 BTIF_TRACE_DEBUG2("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
573 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
574 switch (event)
575 {
576 case BTA_DM_INQ_RES_EVT:
577 {
578 if (p_src_data->inq_res.p_eir)
579 {
580 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
581 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
582 }
583 }
584 break;
585
586 case BTA_DM_DISC_RES_EVT:
587 {
588 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
589 {
590 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
591 memcpy(p_dest_data->disc_res.p_raw_data,
592 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
593 }
594 }
595 break;
596 }
597}
598
599static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
600{
601 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
602 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
603
604 if (!p_src)
605 return;
606 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
607 switch (event)
608 {
609 case BTA_DM_DISC_RES_EVT:
610 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530611 if (p_src_data->disc_res.result == BTA_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800612 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530613 if (p_src_data->disc_res.num_uuids > 0)
614 {
615 p_dest_data->disc_res.p_uuid_list =
616 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
617 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
618 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
619 GKI_freebuf(p_src_data->disc_res.p_uuid_list);
620 }
621 if (p_src_data->disc_res.p_raw_data != NULL)
622 {
623 GKI_freebuf(p_src_data->disc_res.p_raw_data);
624 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800625 }
626 } break;
627 }
628}
629/******************************************************************************
630**
631** BTIF DM callback events
632**
633*****************************************************************************/
634
635/*******************************************************************************
636**
637** Function btif_dm_pin_req_evt
638**
639** Description Executes pin request event in btif context
640**
641** Returns void
642**
643*******************************************************************************/
644static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
645{
646 bt_bdaddr_t bd_addr;
647 bt_bdname_t bd_name;
648 UINT32 cod;
649 bt_pin_code_t pin_code;
650
651 /* Remote properties update */
652 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
653 p_pin_req->dev_class, BT_DEVICE_TYPE_BREDR);
654
655 bdcpy(bd_addr.address, p_pin_req->bd_addr);
656 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
657
658 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
659
660 cod = devclass2uint(p_pin_req->dev_class);
661
662 if ( cod == 0) {
663 BTIF_TRACE_DEBUG1("%s():cod is 0, set as unclassified", __FUNCTION__);
664 cod = COD_UNCLASSIFIED;
665 }
666
667 /* check for auto pair possiblity only if bond was initiated by local device */
668 if (pairing_cb.is_local_initiated)
669 {
670 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
671 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
672 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
673 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
674 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
675 check_cod(&bd_addr, COD_HID_POINTING))
676 {
677 BTIF_TRACE_DEBUG1("%s()cod matches for auto pair", __FUNCTION__);
678 /* Check if this device can be auto paired */
679 if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) &&
680 (pairing_cb.autopair_attempts == 0))
681 {
682 BTIF_TRACE_DEBUG1("%s() Attempting auto pair", __FUNCTION__);
683 pin_code.pin[0] = 0x30;
684 pin_code.pin[1] = 0x30;
685 pin_code.pin[2] = 0x30;
686 pin_code.pin[3] = 0x30;
687
688 pairing_cb.autopair_attempts++;
689 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
690 return;
691 }
692 }
693 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
694 check_cod(&bd_addr, COD_HID_COMBO))
695 {
696 if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) &&
697 (pairing_cb.autopair_attempts == 0))
698 {
699 BTIF_TRACE_DEBUG1("%s() Attempting auto pair", __FUNCTION__);
700 pin_code.pin[0] = 0x30;
701 pin_code.pin[1] = 0x30;
702 pin_code.pin[2] = 0x30;
703 pin_code.pin[3] = 0x30;
704
705 pairing_cb.autopair_attempts++;
706 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
707 return;
708 }
709 }
710 }
711 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
712 &bd_addr, &bd_name, cod);
713}
714
715/*******************************************************************************
716**
717** Function btif_dm_ssp_cfm_req_evt
718**
719** Description Executes SSP confirm request event in btif context
720**
721** Returns void
722**
723*******************************************************************************/
724static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
725{
726 bt_bdaddr_t bd_addr;
727 bt_bdname_t bd_name;
728 UINT32 cod;
729 BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
730
731 BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
732
733 /* Remote properties update */
734 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
735 p_ssp_cfm_req->dev_class, BT_DEVICE_TYPE_BREDR);
736
737 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
738 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
739
740 /* Set the pairing_cb based on the local & remote authentication requirements */
741 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
742
743 /* if just_works and bonding bit is not set treat this as temporary */
744 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 -0800745 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
746 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800747 pairing_cb.is_temp = TRUE;
748 else
749 pairing_cb.is_temp = FALSE;
750
751 pairing_cb.is_ssp = TRUE;
752
753 /* If JustWorks auto-accept */
754 if (p_ssp_cfm_req->just_works)
755 {
756 /* Pairing consent for JustWorks needed if:
757 * 1. Incoming pairing is detected AND
758 * 2. local IO capabilities are DisplayYesNo AND
759 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
760 */
761 if ((is_incoming) && ((p_ssp_cfm_req->loc_io_caps == 0x01) &&
762 (p_ssp_cfm_req->rmt_io_caps == 0x00 || p_ssp_cfm_req->rmt_io_caps == 0x03)))
763 {
764 BTIF_TRACE_EVENT3("%s: User consent needed for incoming pairing request. loc_io_caps: %d, rmt_io_caps: %d",
765 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
766 }
767 else
768 {
769 BTIF_TRACE_EVENT1("%s: Auto-accept JustWorks pairing", __FUNCTION__);
770 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
771 return;
772 }
773 }
774
775 cod = devclass2uint(p_ssp_cfm_req->dev_class);
776
777 if ( cod == 0) {
778 ALOGD("cod is 0, set as unclassified");
779 cod = COD_UNCLASSIFIED;
780 }
781
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700782 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800783 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
784 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
785 p_ssp_cfm_req->num_val);
786}
787
788static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
789{
790 bt_bdaddr_t bd_addr;
791 bt_bdname_t bd_name;
792 UINT32 cod;
793
794 BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
795
796 /* Remote properties update */
797 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
798 p_ssp_key_notif->dev_class, BT_DEVICE_TYPE_BREDR);
799
800 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
801 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
802
803 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
804 pairing_cb.is_ssp = TRUE;
805 cod = devclass2uint(p_ssp_key_notif->dev_class);
806
807 if ( cod == 0) {
808 ALOGD("cod is 0, set as unclassified");
809 cod = COD_UNCLASSIFIED;
810 }
811
812 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
813 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
814 p_ssp_key_notif->passkey);
815}
816/*******************************************************************************
817**
818** Function btif_dm_auth_cmpl_evt
819**
820** Description Executes authentication complete event in btif context
821**
822** Returns void
823**
824*******************************************************************************/
825static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
826{
827 /* Save link key, if not temporary */
828 bt_bdaddr_t bd_addr;
829 bt_status_t status = BT_STATUS_FAIL;
830 bt_bond_state_t state = BT_BOND_STATE_NONE;
831
832 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
833 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
834 {
835 if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) || (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
836 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) || (!pairing_cb.is_temp))
837 {
838 bt_status_t ret;
839 BTIF_TRACE_DEBUG3("%s: Storing link key. key_type=0x%x, is_temp=%d",
840 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.is_temp);
841 ret = btif_storage_add_bonded_device(&bd_addr,
842 p_auth_cmpl->key, p_auth_cmpl->key_type,
843 pairing_cb.pin_code_len);
844 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
845 }
846 else
847 {
848 BTIF_TRACE_DEBUG3("%s: Temporary key. Not storing. key_type=0x%x, is_temp=%d",
849 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.is_temp);
Hemant Guptab820aec2013-12-24 19:59:57 +0530850 if(pairing_cb.is_temp)
851 {
852 BTIF_TRACE_DEBUG1("%s: sending BT_BOND_STATE_NONE for Temp pairing",
853 __FUNCTION__);
854 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
855 return;
856 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800857 }
858 }
859 if (p_auth_cmpl->success)
860 {
861 status = BT_STATUS_SUCCESS;
862 state = BT_BOND_STATE_BONDED;
863
864 /* Trigger SDP on the device */
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700865 pairing_cb.sdp_attempts = 1;;
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -0800866
867 if(btif_dm_inquiry_in_progress)
868 btif_dm_cancel_discovery();
869
The Android Open Source Project5738f832012-12-12 16:00:35 -0800870 btif_dm_get_remote_services(&bd_addr);
871 /* Do not call bond_state_changed_cb yet. Wait till fetch remote service is complete */
872 }
873 else
874 {
875 /*Map the HCI fail reason to bt status */
876 switch(p_auth_cmpl->fail_reason)
877 {
878 case HCI_ERR_PAGE_TIMEOUT:
879 case HCI_ERR_CONNECTION_TOUT:
880 status = BT_STATUS_RMT_DEV_DOWN;
881 break;
882
883 /* map the auth failure codes, so we can retry pairing if necessary */
884 case HCI_ERR_AUTH_FAILURE:
Zhihai Xua7ea8092013-11-27 14:10:53 +0530885 btif_storage_remove_bonded_device(&bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800886 case HCI_ERR_HOST_REJECT_SECURITY:
887 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
888 case HCI_ERR_UNIT_KEY_USED:
889 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
890 case HCI_ERR_INSUFFCIENT_SECURITY:
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530891 case HCI_ERR_PEER_USER:
892 case HCI_ERR_UNSPECIFIED:
893 BTIF_TRACE_DEBUG2(" %s() Authentication fail reason %d",
894 __FUNCTION__, p_auth_cmpl->fail_reason);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800895 if (pairing_cb.autopair_attempts == 1)
896 {
897 BTIF_TRACE_DEBUG1("%s(): Adding device to blacklist ", __FUNCTION__);
898
899 /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class */
900 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
901 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
902 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
903 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
904 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
905 check_cod(&bd_addr, COD_HID_POINTING))
906 {
907 btif_storage_add_device_to_autopair_blacklist (&bd_addr);
908 }
909 pairing_cb.autopair_attempts++;
910
911 /* Create the Bond once again */
912 BTIF_TRACE_DEBUG1("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
913 btif_dm_cb_create_bond (&bd_addr);
914 return;
915 }
916 else
917 {
918 /* if autopair attempts are more than 1, or not attempted */
919 status = BT_STATUS_AUTH_FAILURE;
920 }
921 break;
922
923 default:
924 status = BT_STATUS_FAIL;
925 }
Zhihai Xu8d2128d2013-12-13 16:09:21 +0530926 /* Special Handling for HID Devices */
927 if (check_cod(&bd_addr, COD_HID_POINTING)) {
928 /* Remove Device as bonded in nvram as authentication failed */
929 BTIF_TRACE_DEBUG1("%s(): removing hid pointing device from nvram", __FUNCTION__);
930 btif_storage_remove_bonded_device(&bd_addr);
931 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800932 bond_state_changed(status, &bd_addr, state);
933 }
934}
935
936/******************************************************************************
937**
938** Function btif_dm_search_devices_evt
939**
940** Description Executes search devices callback events in btif context
941**
942** Returns void
943**
944******************************************************************************/
945static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
946{
947 tBTA_DM_SEARCH *p_search_data;
948 BTIF_TRACE_EVENT2("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
949
950 switch (event)
951 {
952 case BTA_DM_DISC_RES_EVT:
953 {
954 p_search_data = (tBTA_DM_SEARCH *)p_param;
955 /* Remote name update */
956 if (strlen((const char *) p_search_data->disc_res.bd_name))
957 {
958 bt_property_t properties[1];
959 bt_bdaddr_t bdaddr;
960 bt_status_t status;
961
962 properties[0].type = BT_PROPERTY_BDNAME;
963 properties[0].val = p_search_data->disc_res.bd_name;
964 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
965 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
966
967 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
968 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
969 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
970 status, &bdaddr, 1, properties);
971 }
972 /* TODO: Services? */
973 }
974 break;
975
976 case BTA_DM_INQ_RES_EVT:
977 {
978 /* inquiry result */
979 UINT32 cod;
980 UINT8 *p_eir_remote_name = NULL;
981 bt_bdname_t bdname;
982 bt_bdaddr_t bdaddr;
983 UINT8 remote_name_len;
984 UINT8 *p_cached_name = NULL;
985 tBTA_SERVICE_MASK services = 0;
986 bdstr_t bdstr;
987
988 p_search_data = (tBTA_DM_SEARCH *)p_param;
989 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
990
991 BTIF_TRACE_DEBUG3("%s() %s device_type = 0x%x\n", __FUNCTION__, bd2str(&bdaddr, &bdstr),
992#if (BLE_INCLUDED == TRUE)
993 p_search_data->inq_res.device_type);
994#else
995 BT_DEVICE_TYPE_BREDR);
996#endif
997 bdname.name[0] = 0;
998
999 cod = devclass2uint (p_search_data->inq_res.dev_class);
1000
1001 if ( cod == 0) {
1002 ALOGD("cod is 0, set as unclassified");
1003 cod = COD_UNCLASSIFIED;
1004 }
1005
1006 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1007 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1008
1009 /* Check EIR for remote name and services */
1010 if (p_search_data->inq_res.p_eir)
1011 {
1012 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
1013 BTIF_TRACE_DEBUG2("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
1014 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */
1015 }
1016
1017
1018 {
1019 bt_property_t properties[5];
1020 bt_device_type_t dev_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001021 UINT8 addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001022 uint32_t num_properties = 0;
1023 bt_status_t status;
1024
1025 memset(properties, 0, sizeof(properties));
1026 /* BD_ADDR */
1027 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1028 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1029 num_properties++;
1030 /* BD_NAME */
1031 /* Don't send BDNAME if it is empty */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001032 if (bdname.name[0])
1033 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001034 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1035 BT_PROPERTY_BDNAME,
1036 strlen((char *)bdname.name), &bdname);
1037 num_properties++;
1038 }
1039
1040 /* DEV_CLASS */
1041 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1042 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1043 num_properties++;
1044 /* DEV_TYPE */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001045#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001046 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1047 dev_type = p_search_data->inq_res.device_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001048 addr_type = p_search_data->inq_res.ble_addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001049#else
1050 dev_type = BT_DEVICE_TYPE_BREDR;
1051#endif
1052 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1053 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1054 num_properties++;
1055 /* RSSI */
1056 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1057 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1058 &(p_search_data->inq_res.rssi));
1059 num_properties++;
1060
1061 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1062 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001063#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1064 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001065 if (( dev_type == BT_DEVICE_TYPE_DUMO)&&
1066 (p_search_data->inq_res.flag & BTA_BLE_DMT_CONTROLLER_SPT) &&
1067 (p_search_data->inq_res.flag & BTA_BLE_DMT_HOST_SPT))
1068 {
1069 btif_storage_set_dmt_support_type (&bdaddr, TRUE);
1070 }
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001071 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1072#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001073 /* Callback to notify upper layer of device */
1074 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1075 num_properties, properties);
1076 }
1077 }
1078 break;
1079
1080 case BTA_DM_INQ_CMPL_EVT:
1081 {
1082 }
1083 break;
1084 case BTA_DM_DISC_CMPL_EVT:
1085 {
1086 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1087 }
1088 break;
1089 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1090 {
1091 /* if inquiry is not in progress and we get a cancel event, then
1092 * it means we are done with inquiry, but remote_name fetches are in
1093 * progress
1094 *
1095 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt
1096 * but instead wait for the cancel_cmpl_evt via the Busy Level
1097 *
1098 */
1099 if (btif_dm_inquiry_in_progress == FALSE)
1100 {
1101 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1102 }
1103 }
1104 break;
1105 }
1106}
1107
1108/*******************************************************************************
1109**
1110** Function btif_dm_search_services_evt
1111**
1112** Description Executes search services event in btif context
1113**
1114** Returns void
1115**
1116*******************************************************************************/
1117static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1118{
1119 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1120
1121 BTIF_TRACE_EVENT2("%s: event = %d", __FUNCTION__, event);
1122 switch (event)
1123 {
1124 case BTA_DM_DISC_RES_EVT:
1125 {
1126 bt_uuid_t uuid_arr[BT_MAX_NUM_UUIDS]; /* Max 32 services */
1127 bt_property_t prop;
1128 uint32_t i = 0, j = 0;
1129 bt_bdaddr_t bd_addr;
1130 bt_status_t ret;
1131
1132 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1133
1134 BTIF_TRACE_DEBUG3("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
1135 p_data->disc_res.result, p_data->disc_res.services);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001136 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1137 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1138 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1139 {
1140 BTIF_TRACE_WARNING1("%s:SDP failed after bonding re-attempting", __FUNCTION__);
1141 pairing_cb.sdp_attempts++;
1142 btif_dm_get_remote_services(&bd_addr);
1143 return;
1144 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001145 prop.type = BT_PROPERTY_UUIDS;
1146 prop.len = 0;
1147 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1148 {
1149 prop.val = p_data->disc_res.p_uuid_list;
1150 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1151 for (i=0; i < p_data->disc_res.num_uuids; i++)
1152 {
1153 char temp[256];
1154 uuid_to_string((bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i*MAX_UUID_SIZE)), temp);
1155 BTIF_TRACE_ERROR2("Index: %d uuid:%s", i, temp);
1156 }
1157 }
1158
1159 /* onUuidChanged requires getBondedDevices to be populated.
1160 ** bond_state_changed needs to be sent prior to remote_device_property
1161 */
1162 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1163 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0)&&
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001164 pairing_cb.sdp_attempts > 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001165 {
1166 BTIF_TRACE_DEBUG1("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
1167 __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001168 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001169 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1170 }
1171
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001172 if(p_data->disc_res.num_uuids != 0)
1173 {
1174 /* Also write this to the NVRAM */
1175 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1176 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1177 /* Send the event to the BTIF */
1178 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1179 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1180 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001181 }
1182 break;
1183
1184 case BTA_DM_DISC_CMPL_EVT:
1185 /* fixme */
1186 break;
1187
Matthew Xie607e3b72013-08-15 19:30:48 -07001188#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001189 case BTA_DM_DISC_BLE_RES_EVT:
1190 BTIF_TRACE_DEBUG2("%s:, services 0x%x)", __FUNCTION__,
1191 p_data->disc_ble_res.service.uu.uuid16);
1192 bt_uuid_t uuid;
1193 int i = 0;
1194 int j = 15;
1195 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1196 {
1197 BTIF_TRACE_DEBUG1("%s: Found HOGP UUID",__FUNCTION__);
1198 bt_property_t prop;
1199 bt_bdaddr_t bd_addr;
1200 char temp[256];
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001201 bt_status_t ret;
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001202
1203 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1204
1205 while(i < j )
1206 {
1207 unsigned char c = uuid.uu[j];
1208 uuid.uu[j] = uuid.uu[i];
1209 uuid.uu[i] = c;
1210 i++;
1211 j--;
1212 }
1213
1214 uuid_to_string(&uuid, temp);
1215 BTIF_TRACE_ERROR1(" uuid:%s", temp);
1216
1217 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1218 prop.type = BT_PROPERTY_UUIDS;
1219 prop.val = uuid.uu;
1220 prop.len = MAX_UUID_SIZE;
1221
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001222 /* Also write this to the NVRAM */
1223 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1224 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1225
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001226 /* Send the event to the BTIF */
1227 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1228 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1229
1230 }
1231 break;
Matthew Xie607e3b72013-08-15 19:30:48 -07001232#endif /* BLE_INCLUDED */
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001233
The Android Open Source Project5738f832012-12-12 16:00:35 -08001234 default:
1235 {
1236 ASSERTC(0, "unhandled search services event", event);
1237 }
1238 break;
1239 }
1240}
1241
1242/*******************************************************************************
1243**
1244** Function btif_dm_remote_service_record_evt
1245**
1246** Description Executes search service record event in btif context
1247**
1248** Returns void
1249**
1250*******************************************************************************/
1251static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1252{
1253 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1254
1255 BTIF_TRACE_EVENT2("%s: event = %d", __FUNCTION__, event);
1256 switch (event)
1257 {
1258 case BTA_DM_DISC_RES_EVT:
1259 {
1260 bt_service_record_t rec;
1261 bt_property_t prop;
1262 uint32_t i = 0;
1263 bt_bdaddr_t bd_addr;
1264
1265 memset(&rec, 0, sizeof(bt_service_record_t));
1266 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1267
1268 BTIF_TRACE_DEBUG3("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
1269 p_data->disc_res.result, p_data->disc_res.services);
1270 prop.type = BT_PROPERTY_SERVICE_RECORD;
1271 prop.val = (void*)&rec;
1272 prop.len = sizeof(rec);
1273
1274 /* disc_res.result is overloaded with SCN. Cannot check result */
1275 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1276 /* TODO: Get the UUID as well */
1277 rec.channel = p_data->disc_res.result - 3;
1278 /* TODO: Need to get the service name using p_raw_data */
1279 rec.name[0] = 0;
1280
1281 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1282 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1283 }
1284 break;
1285
1286 default:
1287 {
1288 ASSERTC(0, "unhandled remote service record event", event);
1289 }
1290 break;
1291 }
1292}
1293
1294/*******************************************************************************
1295**
1296** Function btif_dm_upstreams_cback
1297**
1298** Description Executes UPSTREAMS events in btif context
1299**
1300** Returns void
1301**
1302*******************************************************************************/
1303static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1304{
1305 tBTA_DM_SEC_EVT dm_event = (tBTA_DM_SEC_EVT)event;
1306 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1307 tBTA_SERVICE_MASK service_mask;
1308 uint32_t i;
1309 bt_bdaddr_t bd_addr;
1310
1311 BTIF_TRACE_EVENT1("btif_dm_upstreams_cback ev: %s", dump_dm_event(event));
1312
1313 switch (event)
1314 {
1315 case BTA_DM_ENABLE_EVT:
1316 {
1317 BD_NAME bdname;
1318 bt_status_t status;
1319 bt_property_t prop;
1320 prop.type = BT_PROPERTY_BDNAME;
1321 prop.len = BD_NAME_LEN;
1322 prop.val = (void*)bdname;
1323
1324 status = btif_storage_get_adapter_property(&prop);
Matthew Xie1e5109b2012-11-09 18:26:26 -08001325 if (status == BT_STATUS_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001326 {
1327 /* A name exists in the storage. Make this the device name */
1328 BTA_DmSetDeviceName((char*)prop.val);
1329 }
Matthew Xie1e5109b2012-11-09 18:26:26 -08001330 else
1331 {
1332 /* Storage does not have a name yet.
1333 * Use the default name and write it to the chip
1334 */
1335 BTA_DmSetDeviceName(btif_get_default_local_name());
1336 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001337
1338 /* for each of the enabled services in the mask, trigger the profile
1339 * enable */
1340 service_mask = btif_get_enabled_services_mask();
1341 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1342 {
1343 if (service_mask &
1344 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1345 {
1346 btif_in_execute_service_request(i, TRUE);
1347 }
1348 }
1349 /* clear control blocks */
1350 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1351
1352 /* This function will also trigger the adapter_properties_cb
1353 ** and bonded_devices_info_cb
1354 */
1355 btif_storage_load_bonded_devices();
1356
1357 btif_storage_load_autopair_device_list();
1358
1359 btif_enable_bluetooth_evt(p_data->enable.status, p_data->enable.bd_addr);
Satya Calloji0943c102014-05-12 09:13:02 -07001360
1361 /* Enable local privacy */
1362 BTA_DmBleConfigLocalPrivacy(TRUE);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001363 }
1364 break;
1365
1366 case BTA_DM_DISABLE_EVT:
1367 /* for each of the enabled services in the mask, trigger the profile
1368 * disable */
1369 service_mask = btif_get_enabled_services_mask();
1370 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1371 {
1372 if (service_mask &
1373 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1374 {
1375 btif_in_execute_service_request(i, FALSE);
1376 }
1377 }
1378 btif_disable_bluetooth_evt();
1379 break;
1380
1381 case BTA_DM_PIN_REQ_EVT:
1382 btif_dm_pin_req_evt(&p_data->pin_req);
1383 break;
1384
1385 case BTA_DM_AUTH_CMPL_EVT:
1386 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1387 break;
1388
1389 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1390 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1391 {
1392 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1393 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1394 }
1395 break;
1396
1397 case BTA_DM_SP_CFM_REQ_EVT:
1398 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1399 break;
1400 case BTA_DM_SP_KEY_NOTIF_EVT:
1401 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1402 break;
1403
1404 case BTA_DM_DEV_UNPAIRED_EVT:
1405 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1406
1407 /*special handling for HID devices */
1408 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -07001409 btif_hh_remove_device(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001410 #endif
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001411 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1412 btif_storage_remove_ble_bonding_keys(&bd_addr);
1413 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001414 btif_storage_remove_bonded_device(&bd_addr);
1415 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1416 break;
1417
1418 case BTA_DM_BUSY_LEVEL_EVT:
1419 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001420
1421 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001422 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001423 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001424 {
1425 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1426 BT_DISCOVERY_STARTED);
1427 btif_dm_inquiry_in_progress = TRUE;
1428 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001429 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001430 {
1431 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1432 BT_DISCOVERY_STOPPED);
1433 btif_dm_inquiry_in_progress = FALSE;
1434 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001435 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001436 {
1437 btif_dm_inquiry_in_progress = FALSE;
1438 }
1439 }
1440 }break;
1441
1442 case BTA_DM_LINK_UP_EVT:
1443 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
1444 BTIF_TRACE_DEBUG0("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001445
1446 btif_update_remote_version_property(&bd_addr);
1447
The Android Open Source Project5738f832012-12-12 16:00:35 -08001448 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1449 &bd_addr, BT_ACL_STATE_CONNECTED);
1450 break;
1451
1452 case BTA_DM_LINK_DOWN_EVT:
1453 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1454 BTIF_TRACE_DEBUG0("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
1455 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1456 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1457 break;
1458
1459 case BTA_DM_HW_ERROR_EVT:
1460 BTIF_TRACE_ERROR0("Received H/W Error. ");
1461 /* Flush storage data */
1462 btif_config_flush();
1463 usleep(100000); /* 100milliseconds */
1464 /* Killing the process to force a restart as part of fault tolerance */
1465 kill(getpid(), SIGKILL);
1466 break;
1467
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001468#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1469 case BTA_DM_BLE_KEY_EVT:
1470 BTIF_TRACE_DEBUG1("BTA_DM_BLE_KEY_EVT key_type=0x%02x ", p_data->ble_key.key_type);
1471
1472 /* If this pairing is by-product of local initiated GATT client Read or Write,
1473 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1474 have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1475 if (pairing_cb.state != BT_BOND_STATE_BONDING)
1476 {
1477 BTIF_TRACE_DEBUG0("Bond state not sent to App so far.Notify the app now");
1478 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1479 BT_BOND_STATE_BONDING);
1480 }
1481 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1482 {
1483 BTIF_TRACE_ERROR1("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
1484 break;
1485 }
1486
1487 switch (p_data->ble_key.key_type)
1488 {
1489 case BTA_LE_KEY_PENC:
1490 BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_PENC");
1491 pairing_cb.ble.is_penc_key_rcvd = TRUE;
1492 memcpy(pairing_cb.ble.penc_key.ltk,p_data->ble_key.key_value.penc_key.ltk, 16);
1493 memcpy(pairing_cb.ble.penc_key.rand, p_data->ble_key.key_value.penc_key.rand,8);
1494 pairing_cb.ble.penc_key.ediv = p_data->ble_key.key_value.penc_key.ediv;
1495 pairing_cb.ble.penc_key.sec_level = p_data->ble_key.key_value.penc_key.sec_level;
1496
1497 for (i=0; i<16; i++)
1498 {
1499 BTIF_TRACE_DEBUG2("pairing_cb.ble.penc_key.ltk[%d]=0x%02x",i,pairing_cb.ble.penc_key.ltk[i]);
1500 }
1501 for (i=0; i<8; i++)
1502 {
1503 BTIF_TRACE_DEBUG2("pairing_cb.ble.penc_key.rand[%d]=0x%02x",i,pairing_cb.ble.penc_key.rand[i]);
1504 }
1505 BTIF_TRACE_DEBUG1("pairing_cb.ble.penc_key.ediv=0x%04x",pairing_cb.ble.penc_key.ediv);
1506 BTIF_TRACE_DEBUG1("pairing_cb.ble.penc_key.sec_level=0x%02x",pairing_cb.ble.penc_key.sec_level);
1507 BTIF_TRACE_DEBUG1("pairing_cb.ble.penc_key.key_size=0x%02x",pairing_cb.ble.penc_key.key_size);
1508 break;
1509
1510 case BTA_LE_KEY_PID:
1511 BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_PID");
1512 pairing_cb.ble.is_pid_key_rcvd = TRUE;
1513 memcpy(pairing_cb.ble.pid_key, p_data->ble_key.key_value.pid_key.irk, 16);
1514 for (i=0; i<16; i++)
1515 {
1516 BTIF_TRACE_DEBUG2("pairing_cb.ble.pid_key[%d]=0x%02x",i,pairing_cb.ble.pid_key[i]);
1517 }
1518 break;
1519
1520 case BTA_LE_KEY_PCSRK:
1521 BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_PCSRK");
1522 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1523 pairing_cb.ble.pcsrk_key.counter = p_data->ble_key.key_value.pcsrk_key.counter;
1524 pairing_cb.ble.pcsrk_key.sec_level = p_data->ble_key.key_value.pcsrk_key.sec_level;
1525 memcpy(pairing_cb.ble.pcsrk_key.csrk,p_data->ble_key.key_value.pcsrk_key.csrk,16);
1526
1527 for (i=0; i<16; i++)
1528 {
1529 BTIF_TRACE_DEBUG2("pairing_cb.ble.pcsrk_key.csrk[%d]=0x%02x",i,pairing_cb.ble.pcsrk_key.csrk[i]);
1530 }
1531 BTIF_TRACE_DEBUG1("pairing_cb.ble.pcsrk_key.counter=0x%08x",pairing_cb.ble.pcsrk_key.counter);
1532 BTIF_TRACE_DEBUG1("pairing_cb.ble.pcsrk_key.sec_level=0x%02x",pairing_cb.ble.pcsrk_key.sec_level);
1533 break;
1534
1535 case BTA_LE_KEY_LENC:
1536 BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_LENC");
1537 pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1538 pairing_cb.ble.lenc_key.div = p_data->ble_key.key_value.lenc_key.div;
1539 pairing_cb.ble.lenc_key.key_size = p_data->ble_key.key_value.lenc_key.key_size;
1540 pairing_cb.ble.lenc_key.sec_level = p_data->ble_key.key_value.lenc_key.sec_level;
1541
1542 BTIF_TRACE_DEBUG1("pairing_cb.ble.lenc_key.div=0x%04x",pairing_cb.ble.lenc_key.div);
1543 BTIF_TRACE_DEBUG1("pairing_cb.ble.lenc_key.key_size=0x%02x",pairing_cb.ble.lenc_key.key_size);
1544 BTIF_TRACE_DEBUG1("pairing_cb.ble.lenc_key.sec_level=0x%02x",pairing_cb.ble.lenc_key.sec_level);
1545 break;
1546
1547
1548
1549 case BTA_LE_KEY_LCSRK:
1550 BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_LCSRK");
1551 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1552 pairing_cb.ble.lcsrk_key.counter = p_data->ble_key.key_value.lcsrk_key.counter;
1553 pairing_cb.ble.lcsrk_key.div = p_data->ble_key.key_value.lcsrk_key.div;
1554 pairing_cb.ble.lcsrk_key.sec_level = p_data->ble_key.key_value.lcsrk_key.sec_level;
1555
1556 BTIF_TRACE_DEBUG1("pairing_cb.ble.lcsrk_key.div=0x%04x",pairing_cb.ble.lcsrk_key.div);
1557 BTIF_TRACE_DEBUG1("pairing_cb.ble.lcsrk_key.counter=0x%08x",pairing_cb.ble.lcsrk_key.counter);
1558 BTIF_TRACE_DEBUG1("pairing_cb.ble.lcsrk_key.sec_level=0x%02x",pairing_cb.ble.lcsrk_key.sec_level);
1559
1560 break;
1561
1562 default:
1563 BTIF_TRACE_ERROR1("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
1564 break;
1565 }
1566
1567 break;
1568 case BTA_DM_BLE_SEC_REQ_EVT:
1569 BTIF_TRACE_DEBUG0("BTA_DM_BLE_SEC_REQ_EVT. ");
1570 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1571 break;
1572 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
1573 BTIF_TRACE_DEBUG0("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
1574 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1575 break;
1576 case BTA_DM_BLE_PASSKEY_REQ_EVT:
1577 BTIF_TRACE_DEBUG0("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
1578 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1579 break;
1580 case BTA_DM_BLE_OOB_REQ_EVT:
1581 BTIF_TRACE_DEBUG0("BTA_DM_BLE_OOB_REQ_EVT. ");
1582 break;
1583 case BTA_DM_BLE_LOCAL_IR_EVT:
1584 BTIF_TRACE_DEBUG0("BTA_DM_BLE_LOCAL_IR_EVT. ");
1585 ble_local_key_cb.is_id_keys_rcvd = TRUE;
1586 memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1587 memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1588 memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1589 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1590 BTIF_DM_LE_LOCAL_KEY_IR,
1591 BT_OCTET16_LEN);
1592 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1593 BTIF_DM_LE_LOCAL_KEY_IRK,
1594 BT_OCTET16_LEN);
1595 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1596 BTIF_DM_LE_LOCAL_KEY_DHK,
1597 BT_OCTET16_LEN);
1598 break;
1599 case BTA_DM_BLE_LOCAL_ER_EVT:
1600 BTIF_TRACE_DEBUG0("BTA_DM_BLE_LOCAL_ER_EVT. ");
1601 ble_local_key_cb.is_er_rcvd = TRUE;
1602 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1603 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1604 BTIF_DM_LE_LOCAL_KEY_ER,
1605 BT_OCTET16_LEN);
1606 break;
1607
1608 case BTA_DM_BLE_AUTH_CMPL_EVT:
1609 BTIF_TRACE_DEBUG0("BTA_DM_BLE_KEY_EVT. ");
1610 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1611 break;
1612#endif
1613
The Android Open Source Project5738f832012-12-12 16:00:35 -08001614 case BTA_DM_AUTHORIZE_EVT:
1615 case BTA_DM_SIG_STRENGTH_EVT:
1616 case BTA_DM_SP_RMT_OOB_EVT:
1617 case BTA_DM_SP_KEYPRESS_EVT:
1618 case BTA_DM_ROLE_CHG_EVT:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001619
The Android Open Source Project5738f832012-12-12 16:00:35 -08001620 default:
1621 BTIF_TRACE_WARNING1( "btif_dm_cback : unhandled event (%d)", event );
1622 break;
1623 }
1624} /* btui_security_cback() */
1625
1626
1627/*******************************************************************************
1628**
1629** Function btif_dm_generic_evt
1630**
1631** Description Executes non-BTA upstream events in BTIF context
1632**
1633** Returns void
1634**
1635*******************************************************************************/
1636static void btif_dm_generic_evt(UINT16 event, char* p_param)
1637{
1638 BTIF_TRACE_EVENT2("%s: event=%d", __FUNCTION__, event);
1639 switch(event)
1640 {
1641 case BTIF_DM_CB_DISCOVERY_STARTED:
1642 {
1643 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
1644 }
1645 break;
1646
1647 case BTIF_DM_CB_CREATE_BOND:
1648 {
1649 btif_dm_cb_create_bond((bt_bdaddr_t *)p_param);
1650 }
1651 break;
1652
1653 case BTIF_DM_CB_REMOVE_BOND:
1654 {
1655 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
1656 }
1657 break;
1658
1659 case BTIF_DM_CB_HID_REMOTE_NAME:
1660 {
1661 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
1662 }
1663 break;
1664
1665 case BTIF_DM_CB_BOND_STATE_BONDING:
1666 {
1667 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
1668 }
1669 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001670 case BTIF_DM_CB_LE_TX_TEST:
1671 case BTIF_DM_CB_LE_RX_TEST:
1672 {
1673 uint8_t status;
1674 STREAM_TO_UINT8(status, p_param);
1675 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1676 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
1677 }
1678 break;
1679 case BTIF_DM_CB_LE_TEST_END:
1680 {
1681 uint8_t status;
1682 uint16_t count = 0;
1683 STREAM_TO_UINT8(status, p_param);
1684 if (status == 0)
1685 STREAM_TO_UINT16(count, p_param);
1686 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1687 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
1688 }
1689 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001690 default:
1691 {
1692 BTIF_TRACE_WARNING2("%s : Unknown event 0x%x", __FUNCTION__, event);
1693 }
1694 break;
1695 }
1696}
1697
1698/*******************************************************************************
1699**
1700** Function bte_dm_evt
1701**
1702** Description Switches context from BTE to BTIF for all DM events
1703**
1704** Returns void
1705**
1706*******************************************************************************/
1707
1708void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
1709{
1710 bt_status_t status;
1711
1712 /* switch context to btif task context (copy full union size for convenience) */
1713 status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event, (void*)p_data, sizeof(tBTA_DM_SEC), NULL);
1714
1715 /* catch any failed context transfers */
1716 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
1717}
1718
1719/*******************************************************************************
1720**
1721** Function bte_search_devices_evt
1722**
1723** Description Switches context from BTE to BTIF for DM search events
1724**
1725** Returns void
1726**
1727*******************************************************************************/
1728static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1729{
1730 UINT16 param_len = 0;
1731
1732 if (p_data)
1733 param_len += sizeof(tBTA_DM_SEARCH);
1734 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
1735 switch (event)
1736 {
1737 case BTA_DM_INQ_RES_EVT:
1738 {
1739 if (p_data->inq_res.p_eir)
1740 param_len += HCI_EXT_INQ_RESPONSE_LEN;
1741 }
1742 break;
1743
1744 case BTA_DM_DISC_RES_EVT:
1745 {
1746 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
1747 param_len += p_data->disc_res.raw_data_size;
1748 }
1749 break;
1750 }
1751 BTIF_TRACE_DEBUG3("%s event=%s param_len=%d", __FUNCTION__, dump_dm_search_event(event), param_len);
1752
1753 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
1754 if (event == BTA_DM_INQ_RES_EVT)
1755 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
1756
1757 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
1758 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
1759}
1760
1761/*******************************************************************************
1762**
1763** Function bte_dm_search_services_evt
1764**
1765** Description Switches context from BTE to BTIF for DM search services
1766** event
1767**
1768** Returns void
1769**
1770*******************************************************************************/
1771static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1772{
1773 UINT16 param_len = 0;
1774 if (p_data)
1775 param_len += sizeof(tBTA_DM_SEARCH);
1776 switch (event)
1777 {
1778 case BTA_DM_DISC_RES_EVT:
1779 {
1780 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
1781 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
1782 }
1783 } break;
1784 }
1785 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
1786 * if raw_data is needed. */
1787 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
1788 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
1789}
1790
1791/*******************************************************************************
1792**
1793** Function bte_dm_remote_service_record_evt
1794**
1795** Description Switches context from BTE to BTIF for DM search service
1796** record event
1797**
1798** Returns void
1799**
1800*******************************************************************************/
1801static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1802{
1803 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
1804 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
1805}
1806
1807/*****************************************************************************
1808**
1809** btif api functions (no context switch)
1810**
1811*****************************************************************************/
1812
1813/*******************************************************************************
1814**
1815** Function btif_dm_start_discovery
1816**
1817** Description Start device discovery/inquiry
1818**
1819** Returns bt_status_t
1820**
1821*******************************************************************************/
1822bt_status_t btif_dm_start_discovery(void)
1823{
1824 tBTA_DM_INQ inq_params;
1825 tBTA_SERVICE_MASK services = 0;
1826
1827 BTIF_TRACE_EVENT1("%s", __FUNCTION__);
1828 /* TODO: Do we need to handle multiple inquiries at the same time? */
1829
1830 /* Set inquiry params and call API */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001831#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001832 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
Matthew Xie7f3e4292013-09-30 12:44:10 -07001833#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
1834 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
1835 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
1836 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
1837 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
1838#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001839#else
1840 inq_params.mode = BTA_DM_GENERAL_INQUIRY;
1841#endif
1842 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
1843
1844 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
1845 inq_params.report_dup = TRUE;
1846
1847 inq_params.filter_type = BTA_DM_INQ_CLR;
1848 /* TODO: Filter device by BDA needs to be implemented here */
1849
1850 /* Will be enabled to TRUE once inquiry busy level has been received */
1851 btif_dm_inquiry_in_progress = FALSE;
1852 /* find nearby devices */
1853 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
1854
1855 return BT_STATUS_SUCCESS;
1856}
1857
1858/*******************************************************************************
1859**
1860** Function btif_dm_cancel_discovery
1861**
1862** Description Cancels search
1863**
1864** Returns bt_status_t
1865**
1866*******************************************************************************/
1867bt_status_t btif_dm_cancel_discovery(void)
1868{
1869 BTIF_TRACE_EVENT1("%s", __FUNCTION__);
1870 BTA_DmSearchCancel();
1871 return BT_STATUS_SUCCESS;
1872}
1873
1874/*******************************************************************************
1875**
1876** Function btif_dm_create_bond
1877**
1878** Description Initiate bonding with the specified device
1879**
1880** Returns bt_status_t
1881**
1882*******************************************************************************/
1883bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr)
1884{
1885 bdstr_t bdstr;
1886
1887 BTIF_TRACE_EVENT2("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *) bd_addr, &bdstr));
1888 if (pairing_cb.state != BT_BOND_STATE_NONE)
1889 return BT_STATUS_BUSY;
1890
1891 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
1892 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
1893
1894 return BT_STATUS_SUCCESS;
1895}
1896
1897/*******************************************************************************
1898**
1899** Function btif_dm_cancel_bond
1900**
1901** Description Initiate bonding with the specified device
1902**
1903** Returns bt_status_t
1904**
1905*******************************************************************************/
1906
1907bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
1908{
1909 bdstr_t bdstr;
1910
1911 BTIF_TRACE_EVENT2("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *)bd_addr, &bdstr));
1912
1913 /* TODO:
1914 ** 1. Restore scan modes
1915 ** 2. special handling for HID devices
1916 */
1917 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1918 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001919
1920#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1921
1922 if (pairing_cb.is_ssp)
1923 {
1924 if (pairing_cb.is_le_only)
1925 {
1926 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
1927 }
1928 else
1929 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
1930 }
1931 else
1932 {
1933 if (pairing_cb.is_le_only)
1934 {
1935 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
1936 }
1937 else
1938 {
1939 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
1940 }
1941 /* Cancel bonding, in case it is in ACL connection setup state */
1942 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
1943 }
1944
1945#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08001946 if (pairing_cb.is_ssp)
1947 {
1948 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
1949 }
1950 else
1951 {
1952 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
1953 }
1954 /* Cancel bonding, in case it is in ACL connection setup state */
1955 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
1956 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001957#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001958 }
1959
1960 return BT_STATUS_SUCCESS;
1961}
1962
1963/*******************************************************************************
1964**
Kim Schulza9eb25c2013-09-30 10:55:52 +02001965** Function btif_dm_hh_open_failed
1966**
1967** Description informs the upper layers if the HH have failed during bonding
1968**
1969** Returns none
1970**
1971*******************************************************************************/
1972
1973void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
1974{
1975 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
1976 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
1977 {
1978 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
1979 }
1980}
1981
1982/*******************************************************************************
1983**
The Android Open Source Project5738f832012-12-12 16:00:35 -08001984** Function btif_dm_remove_bond
1985**
1986** Description Removes bonding with the specified device
1987**
1988** Returns bt_status_t
1989**
1990*******************************************************************************/
1991
1992bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
1993{
1994 bdstr_t bdstr;
1995
1996 BTIF_TRACE_EVENT2("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *)bd_addr, &bdstr));
1997 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
1998 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
1999
2000 return BT_STATUS_SUCCESS;
2001}
2002
2003/*******************************************************************************
2004**
2005** Function btif_dm_pin_reply
2006**
2007** Description BT legacy pairing - PIN code reply
2008**
2009** Returns bt_status_t
2010**
2011*******************************************************************************/
2012
2013bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2014 uint8_t pin_len, bt_pin_code_t *pin_code)
2015{
2016 BTIF_TRACE_EVENT2("%s: accept=%d", __FUNCTION__, accept);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002017#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002018
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002019 if (pairing_cb.is_le_only)
2020 {
2021 int i;
2022 UINT32 passkey = 0;
2023 int multi[] = {100000, 10000, 1000, 100, 10,1};
2024 BD_ADDR remote_bd_addr;
2025 bdcpy(remote_bd_addr, bd_addr->address);
2026 for (i = 0; i < 6; i++)
2027 {
2028 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2029 }
2030 BTIF_TRACE_DEBUG1("btif_dm_pin_reply: passkey: %d", passkey);
2031 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2032
2033 }
2034 else
2035 {
2036 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2037 if (accept)
2038 pairing_cb.pin_code_len = pin_len;
2039 }
2040#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002041 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2042
2043 if (accept)
2044 pairing_cb.pin_code_len = pin_len;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002045#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002046 return BT_STATUS_SUCCESS;
2047}
2048
2049/*******************************************************************************
2050**
2051** Function btif_dm_ssp_reply
2052**
2053** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2054**
2055** Returns bt_status_t
2056**
2057*******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002058bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2059 bt_ssp_variant_t variant, uint8_t accept,
2060 uint32_t passkey)
2061{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08002062 UNUSED(passkey);
2063
The Android Open Source Project5738f832012-12-12 16:00:35 -08002064 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2065 {
2066 /* This is not implemented in the stack.
2067 * For devices with display, this is not needed
2068 */
2069 BTIF_TRACE_WARNING1("%s: Not implemented", __FUNCTION__);
2070 return BT_STATUS_FAIL;
2071 }
2072 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
2073 BTIF_TRACE_EVENT2("%s: accept=%d", __FUNCTION__, accept);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002074#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2075 if (pairing_cb.is_le_only)
2076 {
2077 if (accept)
2078 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2079 else
2080 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2081 }
2082 else
2083 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002084
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002085#else
2086 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2087#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002088 return BT_STATUS_SUCCESS;
2089}
2090
2091/*******************************************************************************
2092**
2093** Function btif_dm_get_adapter_property
2094**
2095** Description Queries the BTA for the adapter property
2096**
2097** Returns bt_status_t
2098**
2099*******************************************************************************/
2100bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2101{
2102 bt_status_t status;
2103
2104 BTIF_TRACE_EVENT2("%s: type=0x%x", __FUNCTION__, prop->type);
2105 switch (prop->type)
2106 {
2107 case BT_PROPERTY_BDNAME:
2108 {
2109 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
Matthew Xie1e5109b2012-11-09 18:26:26 -08002110 strcpy((char *)bd_name->name, btif_get_default_local_name());
The Android Open Source Project5738f832012-12-12 16:00:35 -08002111 prop->len = strlen((char *)bd_name->name);
2112 }
2113 break;
2114
2115 case BT_PROPERTY_ADAPTER_SCAN_MODE:
2116 {
2117 /* if the storage does not have it. Most likely app never set it. Default is NONE */
2118 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2119 *mode = BT_SCAN_MODE_NONE;
2120 prop->len = sizeof(bt_scan_mode_t);
2121 }
2122 break;
2123
2124 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2125 {
2126 uint32_t *tmt = (uint32_t*)prop->val;
2127 *tmt = 120; /* default to 120s, if not found in NV */
2128 prop->len = sizeof(uint32_t);
2129 }
2130 break;
2131
2132 default:
2133 prop->len = 0;
2134 return BT_STATUS_FAIL;
2135 }
2136 return BT_STATUS_SUCCESS;
2137}
2138
2139/*******************************************************************************
2140**
2141** Function btif_dm_get_remote_services
2142**
2143** Description Start SDP to get remote services
2144**
2145** Returns bt_status_t
2146**
2147*******************************************************************************/
2148bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2149{
2150 bdstr_t bdstr;
2151
2152 BTIF_TRACE_EVENT2("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
2153
2154 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2155 bte_dm_search_services_evt, TRUE);
2156
2157 return BT_STATUS_SUCCESS;
2158}
2159
2160/*******************************************************************************
2161**
2162** Function btif_dm_get_remote_service_record
2163**
2164** Description Start SDP to get remote service record
2165**
2166**
2167** Returns bt_status_t
2168*******************************************************************************/
2169bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2170 bt_uuid_t *uuid)
2171{
2172 tSDP_UUID sdp_uuid;
2173 bdstr_t bdstr;
2174
2175 BTIF_TRACE_EVENT2("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
2176
2177 sdp_uuid.len = MAX_UUID_SIZE;
2178 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2179
2180 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2181 bte_dm_remote_service_record_evt, TRUE);
2182
2183 return BT_STATUS_SUCCESS;
2184}
2185
2186void btif_dm_execute_service_request(UINT16 event, char *p_param)
2187{
2188 BOOLEAN b_enable = FALSE;
2189 bt_status_t status;
2190 if (event == BTIF_DM_ENABLE_SERVICE)
2191 {
2192 b_enable = TRUE;
2193 }
2194 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2195 if (status == BT_STATUS_SUCCESS)
2196 {
2197 bt_property_t property;
2198 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2199
2200 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2201 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2202 sizeof(local_uuids), local_uuids);
2203 btif_storage_get_adapter_property(&property);
2204 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2205 BT_STATUS_SUCCESS, 1, &property);
2206 }
2207 return;
2208}
2209
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002210void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2211 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2212{
2213 UINT8 yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2214 /* if local initiated:
2215 ** 1. set DD + MITM
2216 ** if remote initiated:
2217 ** 1. Copy over the auth_req from peer's io_rsp
2218 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2219 ** as a fallback set MITM+GB if peer had MITM set
2220 */
2221 UNUSED (bd_addr);
2222 UNUSED (p_io_cap);
2223 UNUSED (p_oob_data);
2224
2225
2226 BTIF_TRACE_DEBUG2("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
2227 if(pairing_cb.is_local_initiated)
2228 {
2229 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2230 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2231 }
2232 else if (!is_orig)
2233 {
2234 /* peer initiated paring. They probably know what they want.
2235 ** Copy the mitm from peer device.
2236 */
2237 BTIF_TRACE_DEBUG2("%s: setting p_auth_req to peer's: %d",
2238 __FUNCTION__, pairing_cb.auth_req);
2239 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2240
2241 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2242 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2243 *p_auth_req |= BTA_AUTH_SP_YES;
2244 }
2245 else if (yes_no_bit)
2246 {
2247 /* set the general bonding bit for stored device */
2248 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2249 }
2250 BTIF_TRACE_DEBUG2("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
2251}
2252
2253void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2254 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2255{
2256 UNUSED (bd_addr);
2257 UNUSED (oob_data);
2258
2259 if(auth_req & BTA_AUTH_BONDS)
2260 {
2261 BTIF_TRACE_DEBUG2("%s auth_req:%d", __FUNCTION__, auth_req);
2262 pairing_cb.auth_req = auth_req;
2263 pairing_cb.io_cap = io_cap;
2264 }
2265}
2266
The Android Open Source Project5738f832012-12-12 16:00:35 -08002267#if (BTM_OOB_INCLUDED == TRUE)
2268void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data)
2269{
2270 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2271 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2272 {
2273 *p_oob_data = FALSE;
2274 }
2275 else
2276 {
2277 *p_oob_data = TRUE;
2278 }
2279 BTIF_TRACE_DEBUG1("btif_dm_set_oob_for_io_req *p_oob_data=%d", *p_oob_data);
2280}
2281#endif /* BTM_OOB_INCLUDED */
2282
2283#ifdef BTIF_DM_OOB_TEST
2284void btif_dm_load_local_oob(void)
2285{
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002286 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002287 property_get("service.brcm.bt.oob", prop_oob, "3");
2288 BTIF_TRACE_DEBUG1("btif_dm_load_local_oob prop_oob = %s",prop_oob);
2289 if (prop_oob[0] != '3')
2290 {
2291#if (BTM_OOB_INCLUDED == TRUE)
2292 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2293 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2294 {
2295 BTIF_TRACE_DEBUG0("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
2296 BTA_DmLocalOob();
2297 }
2298#else
2299 BTIF_TRACE_ERROR0("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
2300#endif
2301 }
2302}
2303
2304void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2305{
2306 FILE *fp;
2307 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2308 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2309 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002310 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002311 BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob: valid=%d", valid);
2312 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2313 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2314 valid)
2315 {
2316 BTIF_TRACE_DEBUG0("save local OOB data in memory");
2317 memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2318 memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2319 property_get("service.brcm.bt.oob", prop_oob, "3");
2320 BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
2321 if (prop_oob[0] == '1')
2322 path = path_a;
2323 else if (prop_oob[0] == '2')
2324 path = path_b;
2325 if (path)
2326 {
2327 fp = fopen(path, "wb+");
2328 if (fp == NULL)
2329 {
2330 BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob: failed to save local OOB data to %s", path);
2331 }
2332 else
2333 {
2334 BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob: save local OOB data into file %s",path);
2335 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2336 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2337 fclose(fp);
2338 }
2339 }
2340 }
2341}
2342BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2343{
2344 char t[128];
2345 FILE *fp;
2346 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2347 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2348 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002349 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002350 BOOLEAN result = FALSE;
2351 bt_bdaddr_t bt_bd_addr;
2352 bdcpy(oob_cb.oob_bdaddr, bd_addr);
2353 property_get("service.brcm.bt.oob", prop_oob, "3");
2354 BTIF_TRACE_DEBUG1("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
2355 if (prop_oob[0] == '1')
2356 path = path_b;
2357 else if (prop_oob[0] == '2')
2358 path = path_a;
2359 if (path)
2360 {
2361 fp = fopen(path, "rb");
2362 if (fp == NULL)
2363 {
2364 BTIF_TRACE_DEBUG1("btapp_dm_rmt_oob_reply: failed to read OOB keys from %s",path);
2365 return FALSE;
2366 }
2367 else
2368 {
2369 BTIF_TRACE_DEBUG1("btif_dm_proc_rmt_oob: read OOB data from %s",path);
2370 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2371 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2372 fclose(fp);
2373 }
2374 BTIF_TRACE_DEBUG0("----btif_dm_proc_rmt_oob: TRUE");
2375 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2376 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2377 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
2378 BTIF_TRACE_DEBUG1("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
2379 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2380 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
2381 p_c[8], p_c[9], p_c[10], p_c[11], p_c[12], p_c[13], p_c[14], p_c[15]);
2382 BTIF_TRACE_DEBUG1("----btif_dm_proc_rmt_oob: c = %s",t);
2383 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2384 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
2385 p_r[8], p_r[9], p_r[10], p_r[11], p_r[12], p_r[13], p_r[14], p_r[15]);
2386 BTIF_TRACE_DEBUG1("----btif_dm_proc_rmt_oob: r = %s",t);
2387 bdcpy(bt_bd_addr.address, bd_addr);
2388 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2389 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2390 result = TRUE;
2391 }
2392 BTIF_TRACE_DEBUG1("btif_dm_proc_rmt_oob result=%d",result);
2393 return result;
2394}
2395#endif /* BTIF_DM_OOB_TEST */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002396#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2397
2398static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2399{
2400 bt_bdaddr_t bd_addr;
2401 bt_bdname_t bd_name;
2402 UINT32 cod;
2403
2404 BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
2405
2406 /* Remote name update */
2407 btif_update_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2408 NULL, BT_DEVICE_TYPE_BLE);
2409 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2410 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2411
2412 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2413 pairing_cb.is_ssp = FALSE;
2414 cod = COD_UNCLASSIFIED;
2415
2416 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2417 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2418 p_ssp_key_notif->passkey);
2419}
2420
2421/*******************************************************************************
2422**
2423** Function btif_dm_ble_auth_cmpl_evt
2424**
2425** Description Executes authentication complete event in btif context
2426**
2427** Returns void
2428**
2429*******************************************************************************/
2430static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2431{
2432 /* Save link key, if not temporary */
2433 bt_bdaddr_t bd_addr;
2434 bt_status_t status = BT_STATUS_FAIL;
2435 bt_bond_state_t state = BT_BOND_STATE_NONE;
2436
2437 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2438 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2439 {
2440 /* store keys */
2441 }
2442 if (p_auth_cmpl->success)
2443 {
2444 status = BT_STATUS_SUCCESS;
2445 state = BT_BOND_STATE_BONDED;
2446
2447 btif_dm_save_ble_bonding_keys();
2448 BTA_GATTC_Refresh(bd_addr.address);
2449 btif_dm_get_remote_services(&bd_addr);
2450 }
2451 else
2452 {
2453 /*Map the HCI fail reason to bt status */
2454 switch (p_auth_cmpl->fail_reason)
2455 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002456 default:
Andre Eisenbachca22ac42013-02-13 17:02:11 +09002457 btif_dm_remove_ble_bonding_keys();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002458 status = BT_STATUS_FAIL;
2459 break;
2460 }
2461 }
2462 bond_state_changed(status, &bd_addr, state);
2463}
2464
2465
2466
2467void btif_dm_load_ble_local_keys(void)
2468{
2469 bt_status_t bt_status;
2470
2471 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2472
2473 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2474 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2475 {
2476 ble_local_key_cb.is_er_rcvd = TRUE;
2477 BTIF_TRACE_DEBUG1("%s BLE ER key loaded",__FUNCTION__ );
2478 }
2479
2480 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
2481 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
2482 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
2483 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
2484 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
2485 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
2486 {
2487 ble_local_key_cb.is_id_keys_rcvd = TRUE;
2488 BTIF_TRACE_DEBUG1("%s BLE ID keys loaded",__FUNCTION__ );
2489 }
2490
2491}
2492void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
2493 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
2494{
2495 if (ble_local_key_cb.is_er_rcvd )
2496 {
2497 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2498 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2499 }
2500
2501 if (ble_local_key_cb.is_id_keys_rcvd)
2502 {
2503 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
2504 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
2505 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
2506 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
2507 }
2508 BTIF_TRACE_DEBUG2("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
2509}
2510
2511void btif_dm_save_ble_bonding_keys(void)
2512{
2513
2514 bt_bdaddr_t bd_addr;
2515
2516 BTIF_TRACE_DEBUG1("%s",__FUNCTION__ );
2517
2518 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2519
2520 if (pairing_cb.ble.is_penc_key_rcvd)
2521 {
2522 btif_storage_add_ble_bonding_key(&bd_addr,
2523 (char *) &pairing_cb.ble.penc_key,
2524 BTIF_DM_LE_KEY_PENC,
2525 sizeof(btif_dm_ble_penc_keys_t));
2526 }
2527
2528 if (pairing_cb.ble.is_pid_key_rcvd)
2529 {
2530 btif_storage_add_ble_bonding_key(&bd_addr,
2531 (char *) &pairing_cb.ble.pid_key[0],
2532 BTIF_DM_LE_KEY_PID,
2533 BT_OCTET16_LEN);
2534 }
2535
2536
2537 if (pairing_cb.ble.is_pcsrk_key_rcvd)
2538 {
2539 btif_storage_add_ble_bonding_key(&bd_addr,
2540 (char *) &pairing_cb.ble.pcsrk_key,
2541 BTIF_DM_LE_KEY_PCSRK,
2542 sizeof(btif_dm_ble_pcsrk_keys_t));
2543 }
2544
2545
2546 if (pairing_cb.ble.is_lenc_key_rcvd)
2547 {
2548 btif_storage_add_ble_bonding_key(&bd_addr,
2549 (char *) &pairing_cb.ble.lenc_key,
2550 BTIF_DM_LE_KEY_LENC,
2551 sizeof(btif_dm_ble_lenc_keys_t));
2552 }
2553
2554 if (pairing_cb.ble.is_lcsrk_key_rcvd)
2555 {
2556 btif_storage_add_ble_bonding_key(&bd_addr,
2557 (char *) &pairing_cb.ble.lcsrk_key,
2558 BTIF_DM_LE_KEY_LCSRK,
2559 sizeof(btif_dm_ble_lcsrk_keys_t));
2560 }
2561
2562}
2563
2564
2565void btif_dm_remove_ble_bonding_keys(void)
2566{
2567 bt_bdaddr_t bd_addr;
2568
2569 BTIF_TRACE_DEBUG1("%s",__FUNCTION__ );
2570
2571 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2572 btif_storage_remove_ble_bonding_keys(&bd_addr);
2573}
2574
2575
2576/*******************************************************************************
2577**
2578** Function btif_dm_ble_sec_req_evt
2579**
2580** Description Eprocess security request event in btif context
2581**
2582** Returns void
2583**
2584*******************************************************************************/
2585void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
2586{
2587 bt_bdaddr_t bd_addr;
2588 bt_bdname_t bd_name;
2589 UINT32 cod;
2590 BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
2591
2592 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2593 {
2594 BTIF_TRACE_DEBUG1("%s Discard security request", __FUNCTION__);
2595 return;
2596 }
2597
2598 /* Remote name update */
2599 btif_update_remote_properties(p_ble_req->bd_addr,p_ble_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
2600
2601 bdcpy(bd_addr.address, p_ble_req->bd_addr);
2602 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
2603
2604 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2605
2606 pairing_cb.is_temp = FALSE;
2607 pairing_cb.is_le_only = TRUE;
2608 pairing_cb.is_ssp = TRUE;
2609
2610 cod = COD_UNCLASSIFIED;
2611
2612 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
2613 BT_SSP_VARIANT_CONSENT, 0);
2614}
2615
2616
2617
2618/*******************************************************************************
2619**
2620** Function btif_dm_ble_passkey_req_evt
2621**
2622** Description Executes pin request event in btif context
2623**
2624** Returns void
2625**
2626*******************************************************************************/
2627static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
2628{
2629 bt_bdaddr_t bd_addr;
2630 bt_bdname_t bd_name;
2631 UINT32 cod;
2632
2633 /* Remote name update */
2634 btif_update_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
2635
2636 bdcpy(bd_addr.address, p_pin_req->bd_addr);
2637 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
2638
2639 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2640 pairing_cb.is_le_only = TRUE;
2641
2642 cod = COD_UNCLASSIFIED;
2643
2644 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
2645 &bd_addr, &bd_name, cod);
2646}
2647
2648
2649void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
2650 tBT_DEVICE_TYPE dev_type)
2651{
2652 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
2653}
2654
2655static void btif_dm_ble_tx_test_cback(void *p)
2656{
2657 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
2658 (char *)p, 1, NULL);
2659}
2660
2661static void btif_dm_ble_rx_test_cback(void *p)
2662{
2663 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
2664 (char *)p, 1, NULL);
2665}
2666
2667static void btif_dm_ble_test_end_cback(void *p)
2668{
2669 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
2670 (char *)p, 3, NULL);
2671}
2672/*******************************************************************************
2673**
2674** Function btif_le_test_mode
2675**
2676** Description Sends a HCI BLE Test command to the Controller
2677**
2678** Returns BT_STATUS_SUCCESS on success
2679**
2680*******************************************************************************/
2681bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
2682{
2683 switch (opcode) {
2684 case HCI_BLE_TRANSMITTER_TEST:
2685 if (len != 3) return BT_STATUS_PARM_INVALID;
2686 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
2687 break;
2688 case HCI_BLE_RECEIVER_TEST:
2689 if (len != 1) return BT_STATUS_PARM_INVALID;
2690 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
2691 break;
2692 case HCI_BLE_TEST_END:
2693 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
2694 break;
2695 default:
2696 BTIF_TRACE_ERROR2("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
2697 return BT_STATUS_UNSUPPORTED;
2698 }
2699 return BT_STATUS_SUCCESS;
2700}
2701
2702#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002703
2704void btif_dm_on_disable()
2705{
2706 /* cancel any pending pairing requests */
2707 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2708 {
2709 bt_bdaddr_t bd_addr;
2710
2711 BTIF_TRACE_DEBUG1("%s: Cancel pending pairing request", __FUNCTION__);
2712 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2713 btif_dm_cancel_bond(&bd_addr);
2714 }
2715}
Matthew Xie1e5109b2012-11-09 18:26:26 -08002716
2717static char* btif_get_default_local_name() {
2718 if (btif_default_local_name[0] == '\0')
2719 {
2720 int max_len = sizeof(btif_default_local_name) - 1;
2721 if (BTM_DEF_LOCAL_NAME[0] != '\0')
2722 {
2723 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
2724 }
2725 else
2726 {
2727 char prop_model[PROPERTY_VALUE_MAX];
2728 property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
2729 strncpy(btif_default_local_name, prop_model, max_len);
2730 }
2731 btif_default_local_name[max_len] = '\0';
2732 }
2733 return btif_default_local_name;
2734}