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