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