blob: e72a95e45f4f4f78572331cb398a3ded847a1223 [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:
Zhihai Xua7ea8092013-11-27 14:10:53 +0530896 btif_storage_remove_bonded_device(&bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800897 case HCI_ERR_HOST_REJECT_SECURITY:
898 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
899 case HCI_ERR_UNIT_KEY_USED:
900 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
901 case HCI_ERR_INSUFFCIENT_SECURITY:
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530902 case HCI_ERR_PEER_USER:
903 case HCI_ERR_UNSPECIFIED:
904 BTIF_TRACE_DEBUG2(" %s() Authentication fail reason %d",
905 __FUNCTION__, p_auth_cmpl->fail_reason);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800906 if (pairing_cb.autopair_attempts == 1)
907 {
908 BTIF_TRACE_DEBUG1("%s(): Adding device to blacklist ", __FUNCTION__);
909
910 /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class */
911 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
912 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
913 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
914 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
915 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
916 check_cod(&bd_addr, COD_HID_POINTING))
917 {
918 btif_storage_add_device_to_autopair_blacklist (&bd_addr);
919 }
920 pairing_cb.autopair_attempts++;
921
922 /* Create the Bond once again */
923 BTIF_TRACE_DEBUG1("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
924 btif_dm_cb_create_bond (&bd_addr);
925 return;
926 }
927 else
928 {
929 /* if autopair attempts are more than 1, or not attempted */
930 status = BT_STATUS_AUTH_FAILURE;
931 }
932 break;
933
934 default:
935 status = BT_STATUS_FAIL;
936 }
Zhihai Xu8d2128d2013-12-13 16:09:21 +0530937 /* Special Handling for HID Devices */
938 if (check_cod(&bd_addr, COD_HID_POINTING)) {
939 /* Remove Device as bonded in nvram as authentication failed */
940 BTIF_TRACE_DEBUG1("%s(): removing hid pointing device from nvram", __FUNCTION__);
941 btif_storage_remove_bonded_device(&bd_addr);
942 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800943 bond_state_changed(status, &bd_addr, state);
944 }
945}
946
947/******************************************************************************
948**
949** Function btif_dm_search_devices_evt
950**
951** Description Executes search devices callback events in btif context
952**
953** Returns void
954**
955******************************************************************************/
956static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
957{
958 tBTA_DM_SEARCH *p_search_data;
959 BTIF_TRACE_EVENT2("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
960
961 switch (event)
962 {
963 case BTA_DM_DISC_RES_EVT:
964 {
965 p_search_data = (tBTA_DM_SEARCH *)p_param;
966 /* Remote name update */
967 if (strlen((const char *) p_search_data->disc_res.bd_name))
968 {
969 bt_property_t properties[1];
970 bt_bdaddr_t bdaddr;
971 bt_status_t status;
972
973 properties[0].type = BT_PROPERTY_BDNAME;
974 properties[0].val = p_search_data->disc_res.bd_name;
975 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
976 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
977
978 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
979 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
980 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
981 status, &bdaddr, 1, properties);
982 }
983 /* TODO: Services? */
984 }
985 break;
986
987 case BTA_DM_INQ_RES_EVT:
988 {
989 /* inquiry result */
990 UINT32 cod;
991 UINT8 *p_eir_remote_name = NULL;
992 bt_bdname_t bdname;
993 bt_bdaddr_t bdaddr;
994 UINT8 remote_name_len;
995 UINT8 *p_cached_name = NULL;
996 tBTA_SERVICE_MASK services = 0;
997 bdstr_t bdstr;
998
999 p_search_data = (tBTA_DM_SEARCH *)p_param;
1000 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
1001
1002 BTIF_TRACE_DEBUG3("%s() %s device_type = 0x%x\n", __FUNCTION__, bd2str(&bdaddr, &bdstr),
1003#if (BLE_INCLUDED == TRUE)
1004 p_search_data->inq_res.device_type);
1005#else
1006 BT_DEVICE_TYPE_BREDR);
1007#endif
1008 bdname.name[0] = 0;
1009
1010 cod = devclass2uint (p_search_data->inq_res.dev_class);
1011
1012 if ( cod == 0) {
1013 ALOGD("cod is 0, set as unclassified");
1014 cod = COD_UNCLASSIFIED;
1015 }
1016
1017 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1018 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1019
1020 /* Check EIR for remote name and services */
1021 if (p_search_data->inq_res.p_eir)
1022 {
1023 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
1024 BTIF_TRACE_DEBUG2("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
1025 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */
1026 }
1027
1028
1029 {
1030 bt_property_t properties[5];
1031 bt_device_type_t dev_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001032 UINT8 addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001033 uint32_t num_properties = 0;
1034 bt_status_t status;
1035
1036 memset(properties, 0, sizeof(properties));
1037 /* BD_ADDR */
1038 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1039 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1040 num_properties++;
1041 /* BD_NAME */
1042 /* Don't send BDNAME if it is empty */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001043 if (bdname.name[0])
1044 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001045 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1046 BT_PROPERTY_BDNAME,
1047 strlen((char *)bdname.name), &bdname);
1048 num_properties++;
1049 }
1050
1051 /* DEV_CLASS */
1052 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1053 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1054 num_properties++;
1055 /* DEV_TYPE */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001056#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001057 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1058 dev_type = p_search_data->inq_res.device_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001059 addr_type = p_search_data->inq_res.ble_addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001060#else
1061 dev_type = BT_DEVICE_TYPE_BREDR;
1062#endif
1063 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1064 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1065 num_properties++;
1066 /* RSSI */
1067 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1068 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1069 &(p_search_data->inq_res.rssi));
1070 num_properties++;
1071
1072 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1073 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001074#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1075 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
1076 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1077#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001078 /* Callback to notify upper layer of device */
1079 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1080 num_properties, properties);
1081 }
1082 }
1083 break;
1084
1085 case BTA_DM_INQ_CMPL_EVT:
1086 {
1087 }
1088 break;
1089 case BTA_DM_DISC_CMPL_EVT:
1090 {
1091 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1092 }
1093 break;
1094 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1095 {
1096 /* if inquiry is not in progress and we get a cancel event, then
1097 * it means we are done with inquiry, but remote_name fetches are in
1098 * progress
1099 *
1100 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt
1101 * but instead wait for the cancel_cmpl_evt via the Busy Level
1102 *
1103 */
1104 if (btif_dm_inquiry_in_progress == FALSE)
1105 {
1106 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1107 }
1108 }
1109 break;
1110 }
1111}
1112
1113/*******************************************************************************
1114**
1115** Function btif_dm_search_services_evt
1116**
1117** Description Executes search services event in btif context
1118**
1119** Returns void
1120**
1121*******************************************************************************/
1122static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1123{
1124 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1125
1126 BTIF_TRACE_EVENT2("%s: event = %d", __FUNCTION__, event);
1127 switch (event)
1128 {
1129 case BTA_DM_DISC_RES_EVT:
1130 {
1131 bt_uuid_t uuid_arr[BT_MAX_NUM_UUIDS]; /* Max 32 services */
1132 bt_property_t prop;
1133 uint32_t i = 0, j = 0;
1134 bt_bdaddr_t bd_addr;
1135 bt_status_t ret;
1136
1137 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1138
1139 BTIF_TRACE_DEBUG3("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
1140 p_data->disc_res.result, p_data->disc_res.services);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001141 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1142 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1143 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1144 {
1145 BTIF_TRACE_WARNING1("%s:SDP failed after bonding re-attempting", __FUNCTION__);
1146 pairing_cb.sdp_attempts++;
1147 btif_dm_get_remote_services(&bd_addr);
1148 return;
1149 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001150 prop.type = BT_PROPERTY_UUIDS;
1151 prop.len = 0;
1152 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1153 {
1154 prop.val = p_data->disc_res.p_uuid_list;
1155 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1156 for (i=0; i < p_data->disc_res.num_uuids; i++)
1157 {
1158 char temp[256];
1159 uuid_to_string((bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i*MAX_UUID_SIZE)), temp);
1160 BTIF_TRACE_ERROR2("Index: %d uuid:%s", i, temp);
1161 }
1162 }
1163
1164 /* onUuidChanged requires getBondedDevices to be populated.
1165 ** bond_state_changed needs to be sent prior to remote_device_property
1166 */
1167 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1168 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0)&&
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001169 pairing_cb.sdp_attempts > 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001170 {
1171 BTIF_TRACE_DEBUG1("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
1172 __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001173 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001174 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1175 }
1176
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001177 if(p_data->disc_res.num_uuids != 0)
1178 {
1179 /* Also write this to the NVRAM */
1180 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1181 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1182 /* Send the event to the BTIF */
1183 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1184 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1185 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001186 }
1187 break;
1188
1189 case BTA_DM_DISC_CMPL_EVT:
1190 /* fixme */
1191 break;
1192
Matthew Xie607e3b72013-08-15 19:30:48 -07001193#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001194 case BTA_DM_DISC_BLE_RES_EVT:
1195 BTIF_TRACE_DEBUG2("%s:, services 0x%x)", __FUNCTION__,
1196 p_data->disc_ble_res.service.uu.uuid16);
1197 bt_uuid_t uuid;
1198 int i = 0;
1199 int j = 15;
1200 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1201 {
1202 BTIF_TRACE_DEBUG1("%s: Found HOGP UUID",__FUNCTION__);
1203 bt_property_t prop;
1204 bt_bdaddr_t bd_addr;
1205 char temp[256];
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001206 bt_status_t ret;
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001207
1208 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1209
1210 while(i < j )
1211 {
1212 unsigned char c = uuid.uu[j];
1213 uuid.uu[j] = uuid.uu[i];
1214 uuid.uu[i] = c;
1215 i++;
1216 j--;
1217 }
1218
1219 uuid_to_string(&uuid, temp);
1220 BTIF_TRACE_ERROR1(" uuid:%s", temp);
1221
1222 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1223 prop.type = BT_PROPERTY_UUIDS;
1224 prop.val = uuid.uu;
1225 prop.len = MAX_UUID_SIZE;
1226
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001227 /* Also write this to the NVRAM */
1228 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1229 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1230
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001231 /* Send the event to the BTIF */
1232 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1233 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1234
1235 }
1236 break;
Matthew Xie607e3b72013-08-15 19:30:48 -07001237#endif /* BLE_INCLUDED */
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001238
The Android Open Source Project5738f832012-12-12 16:00:35 -08001239 default:
1240 {
1241 ASSERTC(0, "unhandled search services event", event);
1242 }
1243 break;
1244 }
1245}
1246
1247/*******************************************************************************
1248**
1249** Function btif_dm_remote_service_record_evt
1250**
1251** Description Executes search service record event in btif context
1252**
1253** Returns void
1254**
1255*******************************************************************************/
1256static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1257{
1258 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1259
1260 BTIF_TRACE_EVENT2("%s: event = %d", __FUNCTION__, event);
1261 switch (event)
1262 {
1263 case BTA_DM_DISC_RES_EVT:
1264 {
1265 bt_service_record_t rec;
1266 bt_property_t prop;
1267 uint32_t i = 0;
1268 bt_bdaddr_t bd_addr;
1269
1270 memset(&rec, 0, sizeof(bt_service_record_t));
1271 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1272
1273 BTIF_TRACE_DEBUG3("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
1274 p_data->disc_res.result, p_data->disc_res.services);
1275 prop.type = BT_PROPERTY_SERVICE_RECORD;
1276 prop.val = (void*)&rec;
1277 prop.len = sizeof(rec);
1278
1279 /* disc_res.result is overloaded with SCN. Cannot check result */
1280 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1281 /* TODO: Get the UUID as well */
1282 rec.channel = p_data->disc_res.result - 3;
1283 /* TODO: Need to get the service name using p_raw_data */
1284 rec.name[0] = 0;
1285
1286 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1287 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1288 }
1289 break;
1290
1291 default:
1292 {
1293 ASSERTC(0, "unhandled remote service record event", event);
1294 }
1295 break;
1296 }
1297}
1298
1299/*******************************************************************************
1300**
1301** Function btif_dm_upstreams_cback
1302**
1303** Description Executes UPSTREAMS events in btif context
1304**
1305** Returns void
1306**
1307*******************************************************************************/
1308static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1309{
1310 tBTA_DM_SEC_EVT dm_event = (tBTA_DM_SEC_EVT)event;
1311 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1312 tBTA_SERVICE_MASK service_mask;
1313 uint32_t i;
1314 bt_bdaddr_t bd_addr;
1315
1316 BTIF_TRACE_EVENT1("btif_dm_upstreams_cback ev: %s", dump_dm_event(event));
1317
1318 switch (event)
1319 {
1320 case BTA_DM_ENABLE_EVT:
1321 {
1322 BD_NAME bdname;
1323 bt_status_t status;
1324 bt_property_t prop;
1325 prop.type = BT_PROPERTY_BDNAME;
1326 prop.len = BD_NAME_LEN;
1327 prop.val = (void*)bdname;
1328
1329 status = btif_storage_get_adapter_property(&prop);
Matthew Xie1e5109b2012-11-09 18:26:26 -08001330 if (status == BT_STATUS_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001331 {
1332 /* A name exists in the storage. Make this the device name */
1333 BTA_DmSetDeviceName((char*)prop.val);
1334 }
Matthew Xie1e5109b2012-11-09 18:26:26 -08001335 else
1336 {
1337 /* Storage does not have a name yet.
1338 * Use the default name and write it to the chip
1339 */
1340 BTA_DmSetDeviceName(btif_get_default_local_name());
1341 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001342
1343 /* for each of the enabled services in the mask, trigger the profile
1344 * enable */
1345 service_mask = btif_get_enabled_services_mask();
1346 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1347 {
1348 if (service_mask &
1349 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1350 {
1351 btif_in_execute_service_request(i, TRUE);
1352 }
1353 }
1354 /* clear control blocks */
1355 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1356
1357 /* This function will also trigger the adapter_properties_cb
1358 ** and bonded_devices_info_cb
1359 */
1360 btif_storage_load_bonded_devices();
1361
1362 btif_storage_load_autopair_device_list();
1363
1364 btif_enable_bluetooth_evt(p_data->enable.status, p_data->enable.bd_addr);
1365 }
1366 break;
1367
1368 case BTA_DM_DISABLE_EVT:
1369 /* for each of the enabled services in the mask, trigger the profile
1370 * disable */
1371 service_mask = btif_get_enabled_services_mask();
1372 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1373 {
1374 if (service_mask &
1375 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1376 {
1377 btif_in_execute_service_request(i, FALSE);
1378 }
1379 }
1380 btif_disable_bluetooth_evt();
1381 break;
1382
1383 case BTA_DM_PIN_REQ_EVT:
1384 btif_dm_pin_req_evt(&p_data->pin_req);
1385 break;
1386
1387 case BTA_DM_AUTH_CMPL_EVT:
1388 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1389 break;
1390
1391 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1392 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1393 {
1394 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1395 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1396 }
1397 break;
1398
1399 case BTA_DM_SP_CFM_REQ_EVT:
1400 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1401 break;
1402 case BTA_DM_SP_KEY_NOTIF_EVT:
1403 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1404 break;
1405
1406 case BTA_DM_DEV_UNPAIRED_EVT:
1407 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1408
1409 /*special handling for HID devices */
1410 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -07001411 btif_hh_remove_device(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001412 #endif
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001413 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1414 btif_storage_remove_ble_bonding_keys(&bd_addr);
1415 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001416 btif_storage_remove_bonded_device(&bd_addr);
1417 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1418 break;
1419
1420 case BTA_DM_BUSY_LEVEL_EVT:
1421 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001422
1423 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001424 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001425 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001426 {
1427 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1428 BT_DISCOVERY_STARTED);
1429 btif_dm_inquiry_in_progress = TRUE;
1430 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001431 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001432 {
1433 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1434 BT_DISCOVERY_STOPPED);
1435 btif_dm_inquiry_in_progress = FALSE;
1436 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001437 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001438 {
1439 btif_dm_inquiry_in_progress = FALSE;
1440 }
1441 }
1442 }break;
1443
1444 case BTA_DM_LINK_UP_EVT:
1445 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
1446 BTIF_TRACE_DEBUG0("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001447
1448 btif_update_remote_version_property(&bd_addr);
1449
The Android Open Source Project5738f832012-12-12 16:00:35 -08001450 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1451 &bd_addr, BT_ACL_STATE_CONNECTED);
1452 break;
1453
1454 case BTA_DM_LINK_DOWN_EVT:
1455 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1456 BTIF_TRACE_DEBUG0("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
1457 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1458 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1459 break;
1460
1461 case BTA_DM_HW_ERROR_EVT:
1462 BTIF_TRACE_ERROR0("Received H/W Error. ");
1463 /* Flush storage data */
1464 btif_config_flush();
1465 usleep(100000); /* 100milliseconds */
1466 /* Killing the process to force a restart as part of fault tolerance */
1467 kill(getpid(), SIGKILL);
1468 break;
1469
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001470#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1471 case BTA_DM_BLE_KEY_EVT:
1472 BTIF_TRACE_DEBUG1("BTA_DM_BLE_KEY_EVT key_type=0x%02x ", p_data->ble_key.key_type);
1473
1474 /* If this pairing is by-product of local initiated GATT client Read or Write,
1475 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1476 have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1477 if (pairing_cb.state != BT_BOND_STATE_BONDING)
1478 {
1479 BTIF_TRACE_DEBUG0("Bond state not sent to App so far.Notify the app now");
1480 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1481 BT_BOND_STATE_BONDING);
1482 }
1483 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1484 {
1485 BTIF_TRACE_ERROR1("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
1486 break;
1487 }
1488
1489 switch (p_data->ble_key.key_type)
1490 {
1491 case BTA_LE_KEY_PENC:
1492 BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_PENC");
1493 pairing_cb.ble.is_penc_key_rcvd = TRUE;
1494 memcpy(pairing_cb.ble.penc_key.ltk,p_data->ble_key.key_value.penc_key.ltk, 16);
1495 memcpy(pairing_cb.ble.penc_key.rand, p_data->ble_key.key_value.penc_key.rand,8);
1496 pairing_cb.ble.penc_key.ediv = p_data->ble_key.key_value.penc_key.ediv;
1497 pairing_cb.ble.penc_key.sec_level = p_data->ble_key.key_value.penc_key.sec_level;
1498
1499 for (i=0; i<16; i++)
1500 {
1501 BTIF_TRACE_DEBUG2("pairing_cb.ble.penc_key.ltk[%d]=0x%02x",i,pairing_cb.ble.penc_key.ltk[i]);
1502 }
1503 for (i=0; i<8; i++)
1504 {
1505 BTIF_TRACE_DEBUG2("pairing_cb.ble.penc_key.rand[%d]=0x%02x",i,pairing_cb.ble.penc_key.rand[i]);
1506 }
1507 BTIF_TRACE_DEBUG1("pairing_cb.ble.penc_key.ediv=0x%04x",pairing_cb.ble.penc_key.ediv);
1508 BTIF_TRACE_DEBUG1("pairing_cb.ble.penc_key.sec_level=0x%02x",pairing_cb.ble.penc_key.sec_level);
1509 BTIF_TRACE_DEBUG1("pairing_cb.ble.penc_key.key_size=0x%02x",pairing_cb.ble.penc_key.key_size);
1510 break;
1511
1512 case BTA_LE_KEY_PID:
1513 BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_PID");
1514 pairing_cb.ble.is_pid_key_rcvd = TRUE;
1515 memcpy(pairing_cb.ble.pid_key, p_data->ble_key.key_value.pid_key.irk, 16);
1516 for (i=0; i<16; i++)
1517 {
1518 BTIF_TRACE_DEBUG2("pairing_cb.ble.pid_key[%d]=0x%02x",i,pairing_cb.ble.pid_key[i]);
1519 }
1520 break;
1521
1522 case BTA_LE_KEY_PCSRK:
1523 BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_PCSRK");
1524 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1525 pairing_cb.ble.pcsrk_key.counter = p_data->ble_key.key_value.pcsrk_key.counter;
1526 pairing_cb.ble.pcsrk_key.sec_level = p_data->ble_key.key_value.pcsrk_key.sec_level;
1527 memcpy(pairing_cb.ble.pcsrk_key.csrk,p_data->ble_key.key_value.pcsrk_key.csrk,16);
1528
1529 for (i=0; i<16; i++)
1530 {
1531 BTIF_TRACE_DEBUG2("pairing_cb.ble.pcsrk_key.csrk[%d]=0x%02x",i,pairing_cb.ble.pcsrk_key.csrk[i]);
1532 }
1533 BTIF_TRACE_DEBUG1("pairing_cb.ble.pcsrk_key.counter=0x%08x",pairing_cb.ble.pcsrk_key.counter);
1534 BTIF_TRACE_DEBUG1("pairing_cb.ble.pcsrk_key.sec_level=0x%02x",pairing_cb.ble.pcsrk_key.sec_level);
1535 break;
1536
1537 case BTA_LE_KEY_LENC:
1538 BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_LENC");
1539 pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1540 pairing_cb.ble.lenc_key.div = p_data->ble_key.key_value.lenc_key.div;
1541 pairing_cb.ble.lenc_key.key_size = p_data->ble_key.key_value.lenc_key.key_size;
1542 pairing_cb.ble.lenc_key.sec_level = p_data->ble_key.key_value.lenc_key.sec_level;
1543
1544 BTIF_TRACE_DEBUG1("pairing_cb.ble.lenc_key.div=0x%04x",pairing_cb.ble.lenc_key.div);
1545 BTIF_TRACE_DEBUG1("pairing_cb.ble.lenc_key.key_size=0x%02x",pairing_cb.ble.lenc_key.key_size);
1546 BTIF_TRACE_DEBUG1("pairing_cb.ble.lenc_key.sec_level=0x%02x",pairing_cb.ble.lenc_key.sec_level);
1547 break;
1548
1549
1550
1551 case BTA_LE_KEY_LCSRK:
1552 BTIF_TRACE_DEBUG0("Rcv BTA_LE_KEY_LCSRK");
1553 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1554 pairing_cb.ble.lcsrk_key.counter = p_data->ble_key.key_value.lcsrk_key.counter;
1555 pairing_cb.ble.lcsrk_key.div = p_data->ble_key.key_value.lcsrk_key.div;
1556 pairing_cb.ble.lcsrk_key.sec_level = p_data->ble_key.key_value.lcsrk_key.sec_level;
1557
1558 BTIF_TRACE_DEBUG1("pairing_cb.ble.lcsrk_key.div=0x%04x",pairing_cb.ble.lcsrk_key.div);
1559 BTIF_TRACE_DEBUG1("pairing_cb.ble.lcsrk_key.counter=0x%08x",pairing_cb.ble.lcsrk_key.counter);
1560 BTIF_TRACE_DEBUG1("pairing_cb.ble.lcsrk_key.sec_level=0x%02x",pairing_cb.ble.lcsrk_key.sec_level);
1561
1562 break;
1563
1564 default:
1565 BTIF_TRACE_ERROR1("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
1566 break;
1567 }
1568
1569 break;
1570 case BTA_DM_BLE_SEC_REQ_EVT:
1571 BTIF_TRACE_DEBUG0("BTA_DM_BLE_SEC_REQ_EVT. ");
1572 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1573 break;
1574 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
1575 BTIF_TRACE_DEBUG0("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
1576 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1577 break;
1578 case BTA_DM_BLE_PASSKEY_REQ_EVT:
1579 BTIF_TRACE_DEBUG0("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
1580 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1581 break;
1582 case BTA_DM_BLE_OOB_REQ_EVT:
1583 BTIF_TRACE_DEBUG0("BTA_DM_BLE_OOB_REQ_EVT. ");
1584 break;
1585 case BTA_DM_BLE_LOCAL_IR_EVT:
1586 BTIF_TRACE_DEBUG0("BTA_DM_BLE_LOCAL_IR_EVT. ");
1587 ble_local_key_cb.is_id_keys_rcvd = TRUE;
1588 memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1589 memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1590 memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1591 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1592 BTIF_DM_LE_LOCAL_KEY_IR,
1593 BT_OCTET16_LEN);
1594 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1595 BTIF_DM_LE_LOCAL_KEY_IRK,
1596 BT_OCTET16_LEN);
1597 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1598 BTIF_DM_LE_LOCAL_KEY_DHK,
1599 BT_OCTET16_LEN);
1600 break;
1601 case BTA_DM_BLE_LOCAL_ER_EVT:
1602 BTIF_TRACE_DEBUG0("BTA_DM_BLE_LOCAL_ER_EVT. ");
1603 ble_local_key_cb.is_er_rcvd = TRUE;
1604 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1605 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1606 BTIF_DM_LE_LOCAL_KEY_ER,
1607 BT_OCTET16_LEN);
1608 break;
1609
1610 case BTA_DM_BLE_AUTH_CMPL_EVT:
1611 BTIF_TRACE_DEBUG0("BTA_DM_BLE_KEY_EVT. ");
1612 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1613 break;
1614#endif
1615
The Android Open Source Project5738f832012-12-12 16:00:35 -08001616 case BTA_DM_AUTHORIZE_EVT:
1617 case BTA_DM_SIG_STRENGTH_EVT:
1618 case BTA_DM_SP_RMT_OOB_EVT:
1619 case BTA_DM_SP_KEYPRESS_EVT:
1620 case BTA_DM_ROLE_CHG_EVT:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001621
The Android Open Source Project5738f832012-12-12 16:00:35 -08001622 default:
1623 BTIF_TRACE_WARNING1( "btif_dm_cback : unhandled event (%d)", event );
1624 break;
1625 }
1626} /* btui_security_cback() */
1627
1628
1629/*******************************************************************************
1630**
1631** Function btif_dm_generic_evt
1632**
1633** Description Executes non-BTA upstream events in BTIF context
1634**
1635** Returns void
1636**
1637*******************************************************************************/
1638static void btif_dm_generic_evt(UINT16 event, char* p_param)
1639{
1640 BTIF_TRACE_EVENT2("%s: event=%d", __FUNCTION__, event);
1641 switch(event)
1642 {
1643 case BTIF_DM_CB_DISCOVERY_STARTED:
1644 {
1645 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
1646 }
1647 break;
1648
1649 case BTIF_DM_CB_CREATE_BOND:
1650 {
1651 btif_dm_cb_create_bond((bt_bdaddr_t *)p_param);
1652 }
1653 break;
1654
1655 case BTIF_DM_CB_REMOVE_BOND:
1656 {
1657 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
1658 }
1659 break;
1660
1661 case BTIF_DM_CB_HID_REMOTE_NAME:
1662 {
1663 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
1664 }
1665 break;
1666
1667 case BTIF_DM_CB_BOND_STATE_BONDING:
1668 {
1669 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
1670 }
1671 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001672 case BTIF_DM_CB_LE_TX_TEST:
1673 case BTIF_DM_CB_LE_RX_TEST:
1674 {
1675 uint8_t status;
1676 STREAM_TO_UINT8(status, p_param);
1677 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1678 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
1679 }
1680 break;
1681 case BTIF_DM_CB_LE_TEST_END:
1682 {
1683 uint8_t status;
1684 uint16_t count = 0;
1685 STREAM_TO_UINT8(status, p_param);
1686 if (status == 0)
1687 STREAM_TO_UINT16(count, p_param);
1688 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1689 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
1690 }
1691 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001692 default:
1693 {
1694 BTIF_TRACE_WARNING2("%s : Unknown event 0x%x", __FUNCTION__, event);
1695 }
1696 break;
1697 }
1698}
1699
1700/*******************************************************************************
1701**
1702** Function bte_dm_evt
1703**
1704** Description Switches context from BTE to BTIF for all DM events
1705**
1706** Returns void
1707**
1708*******************************************************************************/
1709
1710void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
1711{
1712 bt_status_t status;
1713
1714 /* switch context to btif task context (copy full union size for convenience) */
1715 status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event, (void*)p_data, sizeof(tBTA_DM_SEC), NULL);
1716
1717 /* catch any failed context transfers */
1718 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
1719}
1720
1721/*******************************************************************************
1722**
1723** Function bte_search_devices_evt
1724**
1725** Description Switches context from BTE to BTIF for DM search events
1726**
1727** Returns void
1728**
1729*******************************************************************************/
1730static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1731{
1732 UINT16 param_len = 0;
1733
1734 if (p_data)
1735 param_len += sizeof(tBTA_DM_SEARCH);
1736 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
1737 switch (event)
1738 {
1739 case BTA_DM_INQ_RES_EVT:
1740 {
1741 if (p_data->inq_res.p_eir)
1742 param_len += HCI_EXT_INQ_RESPONSE_LEN;
1743 }
1744 break;
1745
1746 case BTA_DM_DISC_RES_EVT:
1747 {
1748 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
1749 param_len += p_data->disc_res.raw_data_size;
1750 }
1751 break;
1752 }
1753 BTIF_TRACE_DEBUG3("%s event=%s param_len=%d", __FUNCTION__, dump_dm_search_event(event), param_len);
1754
1755 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
1756 if (event == BTA_DM_INQ_RES_EVT)
1757 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
1758
1759 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
1760 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
1761}
1762
1763/*******************************************************************************
1764**
1765** Function bte_dm_search_services_evt
1766**
1767** Description Switches context from BTE to BTIF for DM search services
1768** event
1769**
1770** Returns void
1771**
1772*******************************************************************************/
1773static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1774{
1775 UINT16 param_len = 0;
1776 if (p_data)
1777 param_len += sizeof(tBTA_DM_SEARCH);
1778 switch (event)
1779 {
1780 case BTA_DM_DISC_RES_EVT:
1781 {
1782 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
1783 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
1784 }
1785 } break;
1786 }
1787 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
1788 * if raw_data is needed. */
1789 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
1790 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
1791}
1792
1793/*******************************************************************************
1794**
1795** Function bte_dm_remote_service_record_evt
1796**
1797** Description Switches context from BTE to BTIF for DM search service
1798** record event
1799**
1800** Returns void
1801**
1802*******************************************************************************/
1803static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1804{
1805 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
1806 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
1807}
1808
1809/*****************************************************************************
1810**
1811** btif api functions (no context switch)
1812**
1813*****************************************************************************/
1814
1815/*******************************************************************************
1816**
1817** Function btif_dm_start_discovery
1818**
1819** Description Start device discovery/inquiry
1820**
1821** Returns bt_status_t
1822**
1823*******************************************************************************/
1824bt_status_t btif_dm_start_discovery(void)
1825{
1826 tBTA_DM_INQ inq_params;
1827 tBTA_SERVICE_MASK services = 0;
1828
1829 BTIF_TRACE_EVENT1("%s", __FUNCTION__);
1830 /* TODO: Do we need to handle multiple inquiries at the same time? */
1831
1832 /* Set inquiry params and call API */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001833#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001834 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
Matthew Xie7f3e4292013-09-30 12:44:10 -07001835#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
1836 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
1837 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
1838 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
1839 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
1840#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001841#else
1842 inq_params.mode = BTA_DM_GENERAL_INQUIRY;
1843#endif
1844 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
1845
1846 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
1847 inq_params.report_dup = TRUE;
1848
1849 inq_params.filter_type = BTA_DM_INQ_CLR;
1850 /* TODO: Filter device by BDA needs to be implemented here */
1851
1852 /* Will be enabled to TRUE once inquiry busy level has been received */
1853 btif_dm_inquiry_in_progress = FALSE;
1854 /* find nearby devices */
1855 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
1856
1857 return BT_STATUS_SUCCESS;
1858}
1859
1860/*******************************************************************************
1861**
1862** Function btif_dm_cancel_discovery
1863**
1864** Description Cancels search
1865**
1866** Returns bt_status_t
1867**
1868*******************************************************************************/
1869bt_status_t btif_dm_cancel_discovery(void)
1870{
1871 BTIF_TRACE_EVENT1("%s", __FUNCTION__);
1872 BTA_DmSearchCancel();
1873 return BT_STATUS_SUCCESS;
1874}
1875
1876/*******************************************************************************
1877**
1878** Function btif_dm_create_bond
1879**
1880** Description Initiate bonding with the specified device
1881**
1882** Returns bt_status_t
1883**
1884*******************************************************************************/
1885bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr)
1886{
1887 bdstr_t bdstr;
1888
1889 BTIF_TRACE_EVENT2("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *) bd_addr, &bdstr));
1890 if (pairing_cb.state != BT_BOND_STATE_NONE)
1891 return BT_STATUS_BUSY;
1892
1893 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
1894 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
1895
1896 return BT_STATUS_SUCCESS;
1897}
1898
1899/*******************************************************************************
1900**
1901** Function btif_dm_cancel_bond
1902**
1903** Description Initiate bonding with the specified device
1904**
1905** Returns bt_status_t
1906**
1907*******************************************************************************/
1908
1909bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
1910{
1911 bdstr_t bdstr;
1912
1913 BTIF_TRACE_EVENT2("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *)bd_addr, &bdstr));
1914
1915 /* TODO:
1916 ** 1. Restore scan modes
1917 ** 2. special handling for HID devices
1918 */
1919 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1920 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001921
1922#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1923
1924 if (pairing_cb.is_ssp)
1925 {
1926 if (pairing_cb.is_le_only)
1927 {
1928 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
1929 }
1930 else
1931 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
1932 }
1933 else
1934 {
1935 if (pairing_cb.is_le_only)
1936 {
1937 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
1938 }
1939 else
1940 {
1941 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
1942 }
1943 /* Cancel bonding, in case it is in ACL connection setup state */
1944 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
1945 }
1946
1947#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08001948 if (pairing_cb.is_ssp)
1949 {
1950 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
1951 }
1952 else
1953 {
1954 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
1955 }
1956 /* Cancel bonding, in case it is in ACL connection setup state */
1957 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
1958 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001959#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001960 }
1961
1962 return BT_STATUS_SUCCESS;
1963}
1964
1965/*******************************************************************************
1966**
Kim Schulza9eb25c2013-09-30 10:55:52 +02001967** Function btif_dm_hh_open_failed
1968**
1969** Description informs the upper layers if the HH have failed during bonding
1970**
1971** Returns none
1972**
1973*******************************************************************************/
1974
1975void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
1976{
1977 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
1978 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
1979 {
1980 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
1981 }
1982}
1983
1984/*******************************************************************************
1985**
The Android Open Source Project5738f832012-12-12 16:00:35 -08001986** Function btif_dm_remove_bond
1987**
1988** Description Removes bonding with the specified device
1989**
1990** Returns bt_status_t
1991**
1992*******************************************************************************/
1993
1994bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
1995{
1996 bdstr_t bdstr;
1997
1998 BTIF_TRACE_EVENT2("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *)bd_addr, &bdstr));
1999 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2000 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2001
2002 return BT_STATUS_SUCCESS;
2003}
2004
2005/*******************************************************************************
2006**
2007** Function btif_dm_pin_reply
2008**
2009** Description BT legacy pairing - PIN code reply
2010**
2011** Returns bt_status_t
2012**
2013*******************************************************************************/
2014
2015bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2016 uint8_t pin_len, bt_pin_code_t *pin_code)
2017{
2018 BTIF_TRACE_EVENT2("%s: accept=%d", __FUNCTION__, accept);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002019#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002020
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002021 if (pairing_cb.is_le_only)
2022 {
2023 int i;
2024 UINT32 passkey = 0;
2025 int multi[] = {100000, 10000, 1000, 100, 10,1};
2026 BD_ADDR remote_bd_addr;
2027 bdcpy(remote_bd_addr, bd_addr->address);
2028 for (i = 0; i < 6; i++)
2029 {
2030 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2031 }
2032 BTIF_TRACE_DEBUG1("btif_dm_pin_reply: passkey: %d", passkey);
2033 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2034
2035 }
2036 else
2037 {
2038 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2039 if (accept)
2040 pairing_cb.pin_code_len = pin_len;
2041 }
2042#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002043 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2044
2045 if (accept)
2046 pairing_cb.pin_code_len = pin_len;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002047#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002048 return BT_STATUS_SUCCESS;
2049}
2050
2051/*******************************************************************************
2052**
2053** Function btif_dm_ssp_reply
2054**
2055** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2056**
2057** Returns bt_status_t
2058**
2059*******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002060bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2061 bt_ssp_variant_t variant, uint8_t accept,
2062 uint32_t passkey)
2063{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08002064 UNUSED(passkey);
2065
The Android Open Source Project5738f832012-12-12 16:00:35 -08002066 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2067 {
2068 /* This is not implemented in the stack.
2069 * For devices with display, this is not needed
2070 */
2071 BTIF_TRACE_WARNING1("%s: Not implemented", __FUNCTION__);
2072 return BT_STATUS_FAIL;
2073 }
2074 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
2075 BTIF_TRACE_EVENT2("%s: accept=%d", __FUNCTION__, accept);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002076#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2077 if (pairing_cb.is_le_only)
2078 {
2079 if (accept)
2080 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2081 else
2082 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2083 }
2084 else
2085 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002086
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002087#else
2088 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2089#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002090 return BT_STATUS_SUCCESS;
2091}
2092
2093/*******************************************************************************
2094**
2095** Function btif_dm_get_adapter_property
2096**
2097** Description Queries the BTA for the adapter property
2098**
2099** Returns bt_status_t
2100**
2101*******************************************************************************/
2102bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2103{
2104 bt_status_t status;
2105
2106 BTIF_TRACE_EVENT2("%s: type=0x%x", __FUNCTION__, prop->type);
2107 switch (prop->type)
2108 {
2109 case BT_PROPERTY_BDNAME:
2110 {
2111 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
Matthew Xie1e5109b2012-11-09 18:26:26 -08002112 strcpy((char *)bd_name->name, btif_get_default_local_name());
The Android Open Source Project5738f832012-12-12 16:00:35 -08002113 prop->len = strlen((char *)bd_name->name);
2114 }
2115 break;
2116
2117 case BT_PROPERTY_ADAPTER_SCAN_MODE:
2118 {
2119 /* if the storage does not have it. Most likely app never set it. Default is NONE */
2120 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2121 *mode = BT_SCAN_MODE_NONE;
2122 prop->len = sizeof(bt_scan_mode_t);
2123 }
2124 break;
2125
2126 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2127 {
2128 uint32_t *tmt = (uint32_t*)prop->val;
2129 *tmt = 120; /* default to 120s, if not found in NV */
2130 prop->len = sizeof(uint32_t);
2131 }
2132 break;
2133
2134 default:
2135 prop->len = 0;
2136 return BT_STATUS_FAIL;
2137 }
2138 return BT_STATUS_SUCCESS;
2139}
2140
2141/*******************************************************************************
2142**
2143** Function btif_dm_get_remote_services
2144**
2145** Description Start SDP to get remote services
2146**
2147** Returns bt_status_t
2148**
2149*******************************************************************************/
2150bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2151{
2152 bdstr_t bdstr;
2153
2154 BTIF_TRACE_EVENT2("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
2155
2156 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2157 bte_dm_search_services_evt, TRUE);
2158
2159 return BT_STATUS_SUCCESS;
2160}
2161
2162/*******************************************************************************
2163**
2164** Function btif_dm_get_remote_service_record
2165**
2166** Description Start SDP to get remote service record
2167**
2168**
2169** Returns bt_status_t
2170*******************************************************************************/
2171bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2172 bt_uuid_t *uuid)
2173{
2174 tSDP_UUID sdp_uuid;
2175 bdstr_t bdstr;
2176
2177 BTIF_TRACE_EVENT2("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
2178
2179 sdp_uuid.len = MAX_UUID_SIZE;
2180 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2181
2182 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2183 bte_dm_remote_service_record_evt, TRUE);
2184
2185 return BT_STATUS_SUCCESS;
2186}
2187
2188void btif_dm_execute_service_request(UINT16 event, char *p_param)
2189{
2190 BOOLEAN b_enable = FALSE;
2191 bt_status_t status;
2192 if (event == BTIF_DM_ENABLE_SERVICE)
2193 {
2194 b_enable = TRUE;
2195 }
2196 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2197 if (status == BT_STATUS_SUCCESS)
2198 {
2199 bt_property_t property;
2200 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2201
2202 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2203 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2204 sizeof(local_uuids), local_uuids);
2205 btif_storage_get_adapter_property(&property);
2206 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2207 BT_STATUS_SUCCESS, 1, &property);
2208 }
2209 return;
2210}
2211
2212#if (BTM_OOB_INCLUDED == TRUE)
2213void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data)
2214{
2215 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2216 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2217 {
2218 *p_oob_data = FALSE;
2219 }
2220 else
2221 {
2222 *p_oob_data = TRUE;
2223 }
2224 BTIF_TRACE_DEBUG1("btif_dm_set_oob_for_io_req *p_oob_data=%d", *p_oob_data);
2225}
2226#endif /* BTM_OOB_INCLUDED */
2227
2228#ifdef BTIF_DM_OOB_TEST
2229void btif_dm_load_local_oob(void)
2230{
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002231 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002232 property_get("service.brcm.bt.oob", prop_oob, "3");
2233 BTIF_TRACE_DEBUG1("btif_dm_load_local_oob prop_oob = %s",prop_oob);
2234 if (prop_oob[0] != '3')
2235 {
2236#if (BTM_OOB_INCLUDED == TRUE)
2237 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2238 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2239 {
2240 BTIF_TRACE_DEBUG0("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
2241 BTA_DmLocalOob();
2242 }
2243#else
2244 BTIF_TRACE_ERROR0("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
2245#endif
2246 }
2247}
2248
2249void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2250{
2251 FILE *fp;
2252 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2253 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2254 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002255 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002256 BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob: valid=%d", valid);
2257 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2258 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2259 valid)
2260 {
2261 BTIF_TRACE_DEBUG0("save local OOB data in memory");
2262 memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2263 memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2264 property_get("service.brcm.bt.oob", prop_oob, "3");
2265 BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
2266 if (prop_oob[0] == '1')
2267 path = path_a;
2268 else if (prop_oob[0] == '2')
2269 path = path_b;
2270 if (path)
2271 {
2272 fp = fopen(path, "wb+");
2273 if (fp == NULL)
2274 {
2275 BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob: failed to save local OOB data to %s", path);
2276 }
2277 else
2278 {
2279 BTIF_TRACE_DEBUG1("btif_dm_proc_loc_oob: save local OOB data into file %s",path);
2280 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2281 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2282 fclose(fp);
2283 }
2284 }
2285 }
2286}
2287BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2288{
2289 char t[128];
2290 FILE *fp;
2291 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2292 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2293 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002294 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002295 BOOLEAN result = FALSE;
2296 bt_bdaddr_t bt_bd_addr;
2297 bdcpy(oob_cb.oob_bdaddr, bd_addr);
2298 property_get("service.brcm.bt.oob", prop_oob, "3");
2299 BTIF_TRACE_DEBUG1("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
2300 if (prop_oob[0] == '1')
2301 path = path_b;
2302 else if (prop_oob[0] == '2')
2303 path = path_a;
2304 if (path)
2305 {
2306 fp = fopen(path, "rb");
2307 if (fp == NULL)
2308 {
2309 BTIF_TRACE_DEBUG1("btapp_dm_rmt_oob_reply: failed to read OOB keys from %s",path);
2310 return FALSE;
2311 }
2312 else
2313 {
2314 BTIF_TRACE_DEBUG1("btif_dm_proc_rmt_oob: read OOB data from %s",path);
2315 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2316 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2317 fclose(fp);
2318 }
2319 BTIF_TRACE_DEBUG0("----btif_dm_proc_rmt_oob: TRUE");
2320 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2321 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2322 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
2323 BTIF_TRACE_DEBUG1("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
2324 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2325 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
2326 p_c[8], p_c[9], p_c[10], p_c[11], p_c[12], p_c[13], p_c[14], p_c[15]);
2327 BTIF_TRACE_DEBUG1("----btif_dm_proc_rmt_oob: c = %s",t);
2328 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2329 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
2330 p_r[8], p_r[9], p_r[10], p_r[11], p_r[12], p_r[13], p_r[14], p_r[15]);
2331 BTIF_TRACE_DEBUG1("----btif_dm_proc_rmt_oob: r = %s",t);
2332 bdcpy(bt_bd_addr.address, bd_addr);
2333 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2334 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2335 result = TRUE;
2336 }
2337 BTIF_TRACE_DEBUG1("btif_dm_proc_rmt_oob result=%d",result);
2338 return result;
2339}
2340#endif /* BTIF_DM_OOB_TEST */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002341#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2342
2343static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2344{
2345 bt_bdaddr_t bd_addr;
2346 bt_bdname_t bd_name;
2347 UINT32 cod;
2348
2349 BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
2350
2351 /* Remote name update */
2352 btif_update_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2353 NULL, BT_DEVICE_TYPE_BLE);
2354 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2355 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2356
2357 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2358 pairing_cb.is_ssp = FALSE;
2359 cod = COD_UNCLASSIFIED;
2360
2361 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2362 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2363 p_ssp_key_notif->passkey);
2364}
2365
2366/*******************************************************************************
2367**
2368** Function btif_dm_ble_auth_cmpl_evt
2369**
2370** Description Executes authentication complete event in btif context
2371**
2372** Returns void
2373**
2374*******************************************************************************/
2375static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2376{
2377 /* Save link key, if not temporary */
2378 bt_bdaddr_t bd_addr;
2379 bt_status_t status = BT_STATUS_FAIL;
2380 bt_bond_state_t state = BT_BOND_STATE_NONE;
2381
2382 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2383 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2384 {
2385 /* store keys */
2386 }
2387 if (p_auth_cmpl->success)
2388 {
2389 status = BT_STATUS_SUCCESS;
2390 state = BT_BOND_STATE_BONDED;
2391
2392 btif_dm_save_ble_bonding_keys();
2393 BTA_GATTC_Refresh(bd_addr.address);
2394 btif_dm_get_remote_services(&bd_addr);
2395 }
2396 else
2397 {
2398 /*Map the HCI fail reason to bt status */
2399 switch (p_auth_cmpl->fail_reason)
2400 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002401 default:
Andre Eisenbachca22ac42013-02-13 17:02:11 +09002402 btif_dm_remove_ble_bonding_keys();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002403 status = BT_STATUS_FAIL;
2404 break;
2405 }
2406 }
2407 bond_state_changed(status, &bd_addr, state);
2408}
2409
2410
2411
2412void btif_dm_load_ble_local_keys(void)
2413{
2414 bt_status_t bt_status;
2415
2416 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2417
2418 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2419 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2420 {
2421 ble_local_key_cb.is_er_rcvd = TRUE;
2422 BTIF_TRACE_DEBUG1("%s BLE ER key loaded",__FUNCTION__ );
2423 }
2424
2425 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
2426 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
2427 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
2428 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
2429 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
2430 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
2431 {
2432 ble_local_key_cb.is_id_keys_rcvd = TRUE;
2433 BTIF_TRACE_DEBUG1("%s BLE ID keys loaded",__FUNCTION__ );
2434 }
2435
2436}
2437void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
2438 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
2439{
2440 if (ble_local_key_cb.is_er_rcvd )
2441 {
2442 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2443 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2444 }
2445
2446 if (ble_local_key_cb.is_id_keys_rcvd)
2447 {
2448 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
2449 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
2450 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
2451 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
2452 }
2453 BTIF_TRACE_DEBUG2("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
2454}
2455
2456void btif_dm_save_ble_bonding_keys(void)
2457{
2458
2459 bt_bdaddr_t bd_addr;
2460
2461 BTIF_TRACE_DEBUG1("%s",__FUNCTION__ );
2462
2463 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2464
2465 if (pairing_cb.ble.is_penc_key_rcvd)
2466 {
2467 btif_storage_add_ble_bonding_key(&bd_addr,
2468 (char *) &pairing_cb.ble.penc_key,
2469 BTIF_DM_LE_KEY_PENC,
2470 sizeof(btif_dm_ble_penc_keys_t));
2471 }
2472
2473 if (pairing_cb.ble.is_pid_key_rcvd)
2474 {
2475 btif_storage_add_ble_bonding_key(&bd_addr,
2476 (char *) &pairing_cb.ble.pid_key[0],
2477 BTIF_DM_LE_KEY_PID,
2478 BT_OCTET16_LEN);
2479 }
2480
2481
2482 if (pairing_cb.ble.is_pcsrk_key_rcvd)
2483 {
2484 btif_storage_add_ble_bonding_key(&bd_addr,
2485 (char *) &pairing_cb.ble.pcsrk_key,
2486 BTIF_DM_LE_KEY_PCSRK,
2487 sizeof(btif_dm_ble_pcsrk_keys_t));
2488 }
2489
2490
2491 if (pairing_cb.ble.is_lenc_key_rcvd)
2492 {
2493 btif_storage_add_ble_bonding_key(&bd_addr,
2494 (char *) &pairing_cb.ble.lenc_key,
2495 BTIF_DM_LE_KEY_LENC,
2496 sizeof(btif_dm_ble_lenc_keys_t));
2497 }
2498
2499 if (pairing_cb.ble.is_lcsrk_key_rcvd)
2500 {
2501 btif_storage_add_ble_bonding_key(&bd_addr,
2502 (char *) &pairing_cb.ble.lcsrk_key,
2503 BTIF_DM_LE_KEY_LCSRK,
2504 sizeof(btif_dm_ble_lcsrk_keys_t));
2505 }
2506
2507}
2508
2509
2510void btif_dm_remove_ble_bonding_keys(void)
2511{
2512 bt_bdaddr_t bd_addr;
2513
2514 BTIF_TRACE_DEBUG1("%s",__FUNCTION__ );
2515
2516 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2517 btif_storage_remove_ble_bonding_keys(&bd_addr);
2518}
2519
2520
2521/*******************************************************************************
2522**
2523** Function btif_dm_ble_sec_req_evt
2524**
2525** Description Eprocess security request event in btif context
2526**
2527** Returns void
2528**
2529*******************************************************************************/
2530void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
2531{
2532 bt_bdaddr_t bd_addr;
2533 bt_bdname_t bd_name;
2534 UINT32 cod;
2535 BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
2536
2537 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2538 {
2539 BTIF_TRACE_DEBUG1("%s Discard security request", __FUNCTION__);
2540 return;
2541 }
2542
2543 /* Remote name update */
2544 btif_update_remote_properties(p_ble_req->bd_addr,p_ble_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
2545
2546 bdcpy(bd_addr.address, p_ble_req->bd_addr);
2547 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
2548
2549 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2550
2551 pairing_cb.is_temp = FALSE;
2552 pairing_cb.is_le_only = TRUE;
2553 pairing_cb.is_ssp = TRUE;
2554
2555 cod = COD_UNCLASSIFIED;
2556
2557 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
2558 BT_SSP_VARIANT_CONSENT, 0);
2559}
2560
2561
2562
2563/*******************************************************************************
2564**
2565** Function btif_dm_ble_passkey_req_evt
2566**
2567** Description Executes pin request event in btif context
2568**
2569** Returns void
2570**
2571*******************************************************************************/
2572static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
2573{
2574 bt_bdaddr_t bd_addr;
2575 bt_bdname_t bd_name;
2576 UINT32 cod;
2577
2578 /* Remote name update */
2579 btif_update_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
2580
2581 bdcpy(bd_addr.address, p_pin_req->bd_addr);
2582 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
2583
2584 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2585 pairing_cb.is_le_only = TRUE;
2586
2587 cod = COD_UNCLASSIFIED;
2588
2589 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
2590 &bd_addr, &bd_name, cod);
2591}
2592
2593
2594void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
2595 tBT_DEVICE_TYPE dev_type)
2596{
2597 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
2598}
2599
2600static void btif_dm_ble_tx_test_cback(void *p)
2601{
2602 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
2603 (char *)p, 1, NULL);
2604}
2605
2606static void btif_dm_ble_rx_test_cback(void *p)
2607{
2608 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
2609 (char *)p, 1, NULL);
2610}
2611
2612static void btif_dm_ble_test_end_cback(void *p)
2613{
2614 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
2615 (char *)p, 3, NULL);
2616}
2617/*******************************************************************************
2618**
2619** Function btif_le_test_mode
2620**
2621** Description Sends a HCI BLE Test command to the Controller
2622**
2623** Returns BT_STATUS_SUCCESS on success
2624**
2625*******************************************************************************/
2626bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
2627{
2628 switch (opcode) {
2629 case HCI_BLE_TRANSMITTER_TEST:
2630 if (len != 3) return BT_STATUS_PARM_INVALID;
2631 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
2632 break;
2633 case HCI_BLE_RECEIVER_TEST:
2634 if (len != 1) return BT_STATUS_PARM_INVALID;
2635 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
2636 break;
2637 case HCI_BLE_TEST_END:
2638 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
2639 break;
2640 default:
2641 BTIF_TRACE_ERROR2("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
2642 return BT_STATUS_UNSUPPORTED;
2643 }
2644 return BT_STATUS_SUCCESS;
2645}
2646
2647#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002648
2649void btif_dm_on_disable()
2650{
2651 /* cancel any pending pairing requests */
2652 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2653 {
2654 bt_bdaddr_t bd_addr;
2655
2656 BTIF_TRACE_DEBUG1("%s: Cancel pending pairing request", __FUNCTION__);
2657 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2658 btif_dm_cancel_bond(&bd_addr);
2659 }
2660}
Matthew Xie1e5109b2012-11-09 18:26:26 -08002661
2662static char* btif_get_default_local_name() {
2663 if (btif_default_local_name[0] == '\0')
2664 {
2665 int max_len = sizeof(btif_default_local_name) - 1;
2666 if (BTM_DEF_LOCAL_NAME[0] != '\0')
2667 {
2668 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
2669 }
2670 else
2671 {
2672 char prop_model[PROPERTY_VALUE_MAX];
2673 property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
2674 strncpy(btif_default_local_name, prop_model, max_len);
2675 }
2676 btif_default_local_name[max_len] = '\0';
2677 }
2678 return btif_default_local_name;
2679}