blob: 96ba84a255abd8f94254bcde6dc648f92b072253 [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
Priti Agherac0edf9f2014-06-26 11:23:51 -070080#define MAX_SDP_BL_ENTRIES 3
81
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080082typedef struct
83{
The Android Open Source Project5738f832012-12-12 16:00:35 -080084 bt_bond_state_t state;
85 BD_ADDR bd_addr;
86 UINT8 is_temp;
87 UINT8 pin_code_len;
88 UINT8 is_ssp;
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -070089 UINT8 auth_req;
90 UINT8 io_cap;
The Android Open Source Project5738f832012-12-12 16:00:35 -080091 UINT8 autopair_attempts;
92 UINT8 is_local_initiated;
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -070093 UINT8 sdp_attempts;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080094#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
95 BOOLEAN is_le_only;
96 btif_dm_ble_cb_t ble;
97#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -080098} btif_dm_pairing_cb_t;
99
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800100
101typedef struct
102{
103 UINT8 ir[BT_OCTET16_LEN];
104 UINT8 irk[BT_OCTET16_LEN];
105 UINT8 dhk[BT_OCTET16_LEN];
106}btif_dm_local_key_id_t;
107
108typedef struct
109{
110 BOOLEAN is_er_rcvd;
111 UINT8 er[BT_OCTET16_LEN];
112 BOOLEAN is_id_keys_rcvd;
113 btif_dm_local_key_id_t id_keys; /* ID kyes */
114
115}btif_dm_local_key_cb_t;
116
117typedef struct
118{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800119 BD_ADDR bd_addr;
120 BD_NAME bd_name;
121} btif_dm_remote_name_t;
122
123typedef struct
124{
125 BT_OCTET16 sp_c;
126 BT_OCTET16 sp_r;
127 BD_ADDR oob_bdaddr; /* peer bdaddr*/
128} btif_dm_oob_cb_t;
Satya Callojie5ba8842014-07-03 17:18:02 -0700129
130typedef struct
131{
132 uint8_t status;
133 uint8_t ctrl_state;
134 uint64_t tx_time;
135 uint64_t rx_time;
136 uint64_t idle_time;
137 uint64_t energy_used;
138} btif_activity_energy_info_cb_t;
139
Priti Agherac0edf9f2014-06-26 11:23:51 -0700140typedef struct
141{
142 unsigned int manufact_id;
143}skip_sdp_entry_t;
144
The Android Open Source Project5738f832012-12-12 16:00:35 -0800145#define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id))
146
Priti Agherac0edf9f2014-06-26 11:23:51 -0700147#define MAX_SDP_BL_ENTRIES 3
148#define UUID_HUMAN_INTERFACE_DEVICE "00001124-0000-1000-8000-00805f9b34fb"
149
150static skip_sdp_entry_t sdp_blacklist[] = {{76}}; //Apple Mouse and Keyboard
151
152
The Android Open Source Project5738f832012-12-12 16:00:35 -0800153/* This flag will be true if HCI_Inquiry is in progress */
154static BOOLEAN btif_dm_inquiry_in_progress = FALSE;
155
Priti Agherac0edf9f2014-06-26 11:23:51 -0700156
157
Matthew Xie1e5109b2012-11-09 18:26:26 -0800158/************************************************************************************
159** Static variables
160************************************************************************************/
161static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'};
162
The Android Open Source Project5738f832012-12-12 16:00:35 -0800163/******************************************************************************
164** Static functions
165******************************************************************************/
166static btif_dm_pairing_cb_t pairing_cb;
167static btif_dm_oob_cb_t oob_cb;
168static void btif_dm_generic_evt(UINT16 event, char* p_param);
169static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr);
170static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name);
171static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
172 DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800173#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
174static btif_dm_local_key_cb_t ble_local_key_cb;
175static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif);
176static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl);
177static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req);
178#endif
Satya Calloji6e2d9db2014-07-08 16:18:58 -0700179
180static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
181 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
182 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status);
183
Matthew Xie1e5109b2012-11-09 18:26:26 -0800184static char* btif_get_default_local_name();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800185/******************************************************************************
186** Externs
187******************************************************************************/
188extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
189extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
190extern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
191extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
Hemant Gupta10256872013-08-19 18:33:01 +0530192extern bt_status_t btif_hf_client_execute_service(BOOLEAN b_enable);
Hemant Gupta2dc99992014-04-18 12:54:08 +0530193extern bt_status_t btif_mce_execute_service(BOOLEAN b_enable);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800194extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700195extern 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 -0800196
197
198/******************************************************************************
199** Functions
200******************************************************************************/
201
202bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
203 BOOLEAN b_enable)
204{
205 /* Check the service_ID and invoke the profile's BT state changed API */
206 switch (service_id)
207 {
208 case BTA_HFP_SERVICE_ID:
209 case BTA_HSP_SERVICE_ID:
210 {
211 btif_hf_execute_service(b_enable);
212 }break;
213 case BTA_A2DP_SERVICE_ID:
214 {
215 btif_av_execute_service(b_enable);
216 }break;
217 case BTA_HID_SERVICE_ID:
218 {
219 btif_hh_execute_service(b_enable);
220 }break;
Hemant Gupta10256872013-08-19 18:33:01 +0530221 case BTA_HFP_HS_SERVICE_ID:
222 {
223 btif_hf_client_execute_service(b_enable);
224 }break;
Hemant Gupta2dc99992014-04-18 12:54:08 +0530225 case BTA_MAP_SERVICE_ID:
226 {
227 btif_mce_execute_service(b_enable);
228 }break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800229 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700230 BTIF_TRACE_ERROR("%s: Unknown service being enabled", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800231 return BT_STATUS_FAIL;
232 }
233 return BT_STATUS_SUCCESS;
234}
235
236/*******************************************************************************
237**
238** Function check_eir_remote_name
239**
240** Description Check if remote name is in the EIR data
241**
242** Returns TRUE if remote name found
243** Populate p_remote_name, if provided and remote name found
244**
245*******************************************************************************/
246static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
247 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
248{
249 UINT8 *p_eir_remote_name = NULL;
250 UINT8 remote_name_len = 0;
251
252 /* Check EIR for remote name and services */
253 if (p_search_data->inq_res.p_eir)
254 {
255 p_eir_remote_name = BTA_CheckEirData(p_search_data->inq_res.p_eir,
256 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
257 if (!p_eir_remote_name)
258 {
259 p_eir_remote_name = BTA_CheckEirData(p_search_data->inq_res.p_eir,
260 BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
261 }
262
263 if (p_eir_remote_name)
264 {
265 if (remote_name_len > BD_NAME_LEN)
266 remote_name_len = BD_NAME_LEN;
267
268 if (p_remote_name && p_remote_name_len)
269 {
270 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
271 *(p_remote_name + remote_name_len) = 0;
272 *p_remote_name_len = remote_name_len;
273 }
274
275 return TRUE;
276 }
277 }
278
279 return FALSE;
280
281}
282
283/*******************************************************************************
284**
285** Function check_cached_remote_name
286**
287** Description Check if remote name is in the NVRAM cache
288**
289** Returns TRUE if remote name found
290** Populate p_remote_name, if provided and remote name found
291**
292*******************************************************************************/
293static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
294 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
295{
296 bt_bdname_t bdname;
297 bt_bdaddr_t remote_bdaddr;
298 bt_property_t prop_name;
299
300 /* check if we already have it in our btif_storage cache */
301 bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
302 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
303 sizeof(bt_bdname_t), &bdname);
304 if (btif_storage_get_remote_device_property(
305 &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
306 {
307 if (p_remote_name && p_remote_name_len)
308 {
309 strcpy((char *)p_remote_name, (char *)bdname.name);
310 *p_remote_name_len = strlen((char *)p_remote_name);
311 }
312 return TRUE;
313 }
314
315 return FALSE;
316}
317
318BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
319{
320 uint32_t remote_cod;
321 bt_property_t prop_name;
322
323 /* check if we already have it in our btif_storage cache */
324 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
325 sizeof(uint32_t), &remote_cod);
326 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
327 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700328 BTIF_TRACE_ERROR("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800329 if ((remote_cod & 0x7ff) == cod)
330 return TRUE;
331 }
332
333 return FALSE;
334}
335
Priti Agheraebb1d752012-11-27 18:03:22 -0800336BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
337{
338 uint32_t remote_cod;
339 bt_property_t prop_name;
340
341 /* check if we already have it in our btif_storage cache */
342 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
343 sizeof(uint32_t), &remote_cod);
344 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
345 &prop_name) == BT_STATUS_SUCCESS)
346 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700347 BTIF_TRACE_DEBUG("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
Priti Agheraebb1d752012-11-27 18:03:22 -0800348 if ((remote_cod & 0x700) == cod)
349 return TRUE;
350 }
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700351 return FALSE;
352}
Priti Agheraebb1d752012-11-27 18:03:22 -0800353
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700354BOOLEAN check_hid_le(const bt_bdaddr_t *remote_bdaddr)
355{
356 uint32_t remote_dev_type;
357 bt_property_t prop_name;
358
359 /* check if we already have it in our btif_storage cache */
360 BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE,
361 sizeof(uint32_t), &remote_dev_type);
362 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
363 &prop_name) == BT_STATUS_SUCCESS)
364 {
365 if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE)
366 {
367 bdstr_t bdstr;
368 bd2str(remote_bdaddr, &bdstr);
369 if(btif_config_exist("Remote", bdstr, "HidAppId"))
370 return TRUE;
371 }
372 }
Priti Agheraebb1d752012-11-27 18:03:22 -0800373 return FALSE;
374}
375
Priti Agherac0edf9f2014-06-26 11:23:51 -0700376/*****************************************************************************
377**
378** Function check_sdp_bl
379**
380** Description Checks if a given device is blacklisted to skip sdp
381**
382** Parameters skip_sdp_entry
383**
384** Returns TRUE if the device is present in blacklist, else FALSE
385**
386*******************************************************************************/
387BOOLEAN check_sdp_bl(const bt_bdaddr_t *remote_bdaddr)
388{
389 UINT8 i = 0;
390 UINT16 manufacturer = 0;
391 UINT8 lmp_ver = 0;
392 UINT16 lmp_subver = 0;
393 tBTM_STATUS btm_status;
394 bt_property_t prop_name;
395 bt_remote_version_t info;
396 bt_status_t status;
397
398
399 if (remote_bdaddr == NULL)
400 return FALSE;
401
402/* fetch additional info about remote device used in iop query */
403 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)remote_bdaddr, &lmp_ver,
404 &manufacturer, &lmp_subver);
405
406
407
408 /* if not available yet, try fetching from config database */
409 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO,
410 sizeof(bt_remote_version_t), &info);
411
412 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
413 &prop_name) != BT_STATUS_SUCCESS)
414 {
415
416 return FALSE;
417 }
418 manufacturer = info.manufacturer;
419
420 for (int i = 0; i < MAX_SDP_BL_ENTRIES; i++)
421 {
422 if (manufacturer == sdp_blacklist[i].manufact_id)
423 return TRUE;
424 }
425 return FALSE;
426}
427
428
The Android Open Source Project5738f832012-12-12 16:00:35 -0800429static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
430{
431 /* Send bonding state only once - based on outgoing/incoming we may receive duplicates */
432 if ( (pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING) )
433 return;
434
435 if (pairing_cb.is_temp)
436 {
437 state = BT_BOND_STATE_NONE;
438 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700439 BTIF_TRACE_DEBUG("%s: state=%d prev_state=%d", __FUNCTION__, state, pairing_cb.state);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800440
441 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
442
443 if (state == BT_BOND_STATE_BONDING)
444 {
445 pairing_cb.state = state;
446 bdcpy(pairing_cb.bd_addr, bd_addr->address);
447 }
448 else
449 {
450 memset(&pairing_cb, 0, sizeof(pairing_cb));
451 }
452
453}
454
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800455/* store remote version in bt config to always have access
456 to it post pairing*/
457static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
458{
459 bt_property_t property;
460 UINT8 lmp_ver = 0;
461 UINT16 lmp_subver = 0;
462 UINT16 mfct_set = 0;
463 tBTM_STATUS btm_status;
464 bt_remote_version_t info;
465 bt_status_t status;
466 bdstr_t bdstr;
467
468 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
469 &mfct_set, &lmp_subver);
470
471 ALOGD("remote version info [%s]: %x, %x, %x", bd2str(p_bd, &bdstr),
472 lmp_ver, mfct_set, lmp_subver);
473
474 if (btm_status == BTM_SUCCESS)
475 {
476 /* always update cache to ensure we have availability whenever BTM API
477 is not populated */
478 info.manufacturer = mfct_set;
479 info.sub_ver = lmp_subver;
480 info.version = lmp_ver;
481 BTIF_STORAGE_FILL_PROPERTY(&property,
482 BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
483 &info);
484 status = btif_storage_set_remote_device_property(p_bd, &property);
485 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
486 }
487}
488
The Android Open Source Project5738f832012-12-12 16:00:35 -0800489
490static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
491 DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
492{
493 int num_properties = 0;
494 bt_property_t properties[3];
495 bt_bdaddr_t bdaddr;
496 bt_status_t status;
497 UINT32 cod;
498 bt_device_type_t dev_type;
499
500 memset(properties, 0, sizeof(properties));
501 bdcpy(bdaddr.address, bd_addr);
502
503 /* remote name */
504 if (strlen((const char *) bd_name))
505 {
506 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
507 BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
508 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
509 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
510 num_properties++;
511 }
512
513 /* class of device */
514 cod = devclass2uint(dev_class);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700515 BTIF_TRACE_DEBUG("%s():cod is 0x%06x", __FUNCTION__, cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800516 if ( cod == 0) {
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530517 /* Try to retrieve cod from storage */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700518 BTIF_TRACE_DEBUG("%s():cod is 0, checking cod from storage", __FUNCTION__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530519 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
520 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
521 status = btif_storage_get_remote_device_property(&bdaddr, &properties[num_properties]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700522 BTIF_TRACE_DEBUG("%s():cod retreived from storage is 0x%06x", __FUNCTION__, cod);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530523 if ( cod == 0) {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700524 BTIF_TRACE_DEBUG("%s():cod is again 0, set as unclassified", __FUNCTION__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530525 cod = COD_UNCLASSIFIED;
526 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800527 }
528
529 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
530 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
531 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
532 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
533 num_properties++;
534
535 /* device type */
536 dev_type = device_type;
537 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
538 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
539 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
540 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
541 num_properties++;
542
543 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
544 status, &bdaddr, num_properties, properties);
545}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800546
547/*******************************************************************************
548**
549** Function btif_dm_cb_hid_remote_name
550**
551** Description Remote name callback for HID device. Called in btif context
552** Special handling for HID devices
553**
554** Returns void
555**
556*******************************************************************************/
557static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
558{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700559 BTIF_TRACE_DEBUG("%s: status=%d pairing_cb.state=%d", __FUNCTION__, p_remote_name->status, pairing_cb.state);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800560 if (pairing_cb.state == BT_BOND_STATE_BONDING)
561 {
562 bt_bdaddr_t remote_bd;
563
564 bdcpy(remote_bd.address, pairing_cb.bd_addr);
565
566 if (p_remote_name->status == BTM_SUCCESS)
567 {
568 bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
569 }
570 else
571 bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
572 }
573}
574
The Android Open Source Project5738f832012-12-12 16:00:35 -0800575/*******************************************************************************
576**
577** Function btif_dm_cb_create_bond
578**
579** Description Create bond initiated from the BTIF thread context
580** Special handling for HID devices
581**
582** Returns void
583**
584*******************************************************************************/
585static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr)
586{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800587 BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800588 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800589
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800590#if BLE_INCLUDED == TRUE
591 int device_type;
592 int addr_type;
593 bdstr_t bdstr;
594 bd2str(bd_addr, &bdstr);
595 if(btif_config_get_int("Remote", (char const *)&bdstr,"DevType", &device_type) &&
596 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
597 (device_type == BT_DEVICE_TYPE_BLE))
598 {
599 BTA_DmAddBleDevice(bd_addr->address, addr_type, BT_DEVICE_TYPE_BLE);
600 }
601#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800602
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800603#if BLE_INCLUDED == TRUE
604 if(is_hid && device_type != BT_DEVICE_TYPE_BLE)
605#else
606 if(is_hid)
607#endif
608 {
609 int status;
610 status = btif_hh_connect(bd_addr);
611 if(status != BT_STATUS_SUCCESS)
612 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800613 }
614 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800615 {
The Android Open Source Project5738f832012-12-12 16:00:35 -0800616 BTA_DmBond ((UINT8 *)bd_addr->address);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800617 }
618 /* Track originator of bond creation */
619 pairing_cb.is_local_initiated = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800620
621}
622
623/*******************************************************************************
624**
625** Function btif_dm_cb_remove_bond
626**
627** Description remove bond initiated from the BTIF thread context
628** Special handling for HID devices
629**
630** Returns void
631**
632*******************************************************************************/
633void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
634{
635 bdstr_t bdstr;
636 /*special handling for HID devices */
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700637 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there
638 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
639#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
640 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
641#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800642 {
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700643 BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800644 }
645}
646
647/*******************************************************************************
648**
Andre Eisenbach249f6002014-06-18 12:20:37 -0700649** Function btif_dm_get_connection_state
650**
651** Description Returns whether the remote device is currently connected
652**
653** Returns 0 if not connected
654**
655*******************************************************************************/
656uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr)
657{
658 return BTA_DmGetConnectionState((UINT8 *)bd_addr->address);
659}
660
661/*******************************************************************************
662**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800663** Function search_devices_copy_cb
664**
665** Description Deep copy callback for search devices event
666**
667** Returns void
668**
669*******************************************************************************/
670static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
671{
672 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
673 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
674
675 if (!p_src)
676 return;
677
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700678 BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800679 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
680 switch (event)
681 {
682 case BTA_DM_INQ_RES_EVT:
683 {
684 if (p_src_data->inq_res.p_eir)
685 {
686 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
687 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
688 }
689 }
690 break;
691
692 case BTA_DM_DISC_RES_EVT:
693 {
694 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
695 {
696 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
697 memcpy(p_dest_data->disc_res.p_raw_data,
698 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
699 }
700 }
701 break;
702 }
703}
704
705static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
706{
707 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
708 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
709
710 if (!p_src)
711 return;
712 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
713 switch (event)
714 {
715 case BTA_DM_DISC_RES_EVT:
716 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530717 if (p_src_data->disc_res.result == BTA_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800718 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530719 if (p_src_data->disc_res.num_uuids > 0)
720 {
721 p_dest_data->disc_res.p_uuid_list =
722 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
723 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
724 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
725 GKI_freebuf(p_src_data->disc_res.p_uuid_list);
726 }
727 if (p_src_data->disc_res.p_raw_data != NULL)
728 {
729 GKI_freebuf(p_src_data->disc_res.p_raw_data);
730 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800731 }
732 } break;
733 }
734}
735/******************************************************************************
736**
737** BTIF DM callback events
738**
739*****************************************************************************/
740
741/*******************************************************************************
742**
743** Function btif_dm_pin_req_evt
744**
745** Description Executes pin request event in btif context
746**
747** Returns void
748**
749*******************************************************************************/
750static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
751{
752 bt_bdaddr_t bd_addr;
753 bt_bdname_t bd_name;
754 UINT32 cod;
755 bt_pin_code_t pin_code;
756
757 /* Remote properties update */
758 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
759 p_pin_req->dev_class, BT_DEVICE_TYPE_BREDR);
760
761 bdcpy(bd_addr.address, p_pin_req->bd_addr);
762 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
763
764 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
765
766 cod = devclass2uint(p_pin_req->dev_class);
767
768 if ( cod == 0) {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700769 BTIF_TRACE_DEBUG("%s():cod is 0, set as unclassified", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800770 cod = COD_UNCLASSIFIED;
771 }
772
773 /* check for auto pair possiblity only if bond was initiated by local device */
774 if (pairing_cb.is_local_initiated)
775 {
776 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
777 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
778 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
779 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
780 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
781 check_cod(&bd_addr, COD_HID_POINTING))
782 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700783 BTIF_TRACE_DEBUG("%s()cod matches for auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800784 /* Check if this device can be auto paired */
785 if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) &&
786 (pairing_cb.autopair_attempts == 0))
787 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700788 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800789 pin_code.pin[0] = 0x30;
790 pin_code.pin[1] = 0x30;
791 pin_code.pin[2] = 0x30;
792 pin_code.pin[3] = 0x30;
793
794 pairing_cb.autopair_attempts++;
795 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
796 return;
797 }
798 }
799 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
800 check_cod(&bd_addr, COD_HID_COMBO))
801 {
802 if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) &&
803 (pairing_cb.autopair_attempts == 0))
804 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700805 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800806 pin_code.pin[0] = 0x30;
807 pin_code.pin[1] = 0x30;
808 pin_code.pin[2] = 0x30;
809 pin_code.pin[3] = 0x30;
810
811 pairing_cb.autopair_attempts++;
812 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
813 return;
814 }
815 }
816 }
817 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
818 &bd_addr, &bd_name, cod);
819}
820
821/*******************************************************************************
822**
823** Function btif_dm_ssp_cfm_req_evt
824**
825** Description Executes SSP confirm request event in btif context
826**
827** Returns void
828**
829*******************************************************************************/
830static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
831{
832 bt_bdaddr_t bd_addr;
833 bt_bdname_t bd_name;
834 UINT32 cod;
835 BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
836
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700837 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800838
839 /* Remote properties update */
840 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
841 p_ssp_cfm_req->dev_class, BT_DEVICE_TYPE_BREDR);
842
843 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
844 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
845
846 /* Set the pairing_cb based on the local & remote authentication requirements */
847 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
848
849 /* if just_works and bonding bit is not set treat this as temporary */
850 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 -0800851 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
852 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800853 pairing_cb.is_temp = TRUE;
854 else
855 pairing_cb.is_temp = FALSE;
856
857 pairing_cb.is_ssp = TRUE;
858
859 /* If JustWorks auto-accept */
860 if (p_ssp_cfm_req->just_works)
861 {
862 /* Pairing consent for JustWorks needed if:
863 * 1. Incoming pairing is detected AND
864 * 2. local IO capabilities are DisplayYesNo AND
865 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
866 */
867 if ((is_incoming) && ((p_ssp_cfm_req->loc_io_caps == 0x01) &&
868 (p_ssp_cfm_req->rmt_io_caps == 0x00 || p_ssp_cfm_req->rmt_io_caps == 0x03)))
869 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700870 BTIF_TRACE_EVENT("%s: User consent needed for incoming pairing request. loc_io_caps: %d, rmt_io_caps: %d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800871 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
872 }
873 else
874 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700875 BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800876 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
877 return;
878 }
879 }
880
881 cod = devclass2uint(p_ssp_cfm_req->dev_class);
882
883 if ( cod == 0) {
884 ALOGD("cod is 0, set as unclassified");
885 cod = COD_UNCLASSIFIED;
886 }
887
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -0700888 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800889 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
890 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
891 p_ssp_cfm_req->num_val);
892}
893
894static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
895{
896 bt_bdaddr_t bd_addr;
897 bt_bdname_t bd_name;
898 UINT32 cod;
899
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700900 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800901
902 /* Remote properties update */
903 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
904 p_ssp_key_notif->dev_class, BT_DEVICE_TYPE_BREDR);
905
906 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
907 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
908
909 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
910 pairing_cb.is_ssp = TRUE;
911 cod = devclass2uint(p_ssp_key_notif->dev_class);
912
913 if ( cod == 0) {
914 ALOGD("cod is 0, set as unclassified");
915 cod = COD_UNCLASSIFIED;
916 }
917
918 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
919 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
920 p_ssp_key_notif->passkey);
921}
922/*******************************************************************************
923**
924** Function btif_dm_auth_cmpl_evt
925**
926** Description Executes authentication complete event in btif context
927**
928** Returns void
929**
930*******************************************************************************/
931static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
932{
933 /* Save link key, if not temporary */
934 bt_bdaddr_t bd_addr;
935 bt_status_t status = BT_STATUS_FAIL;
936 bt_bond_state_t state = BT_BOND_STATE_NONE;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700937 BOOLEAN skip_sdp = FALSE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800938
939 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
940 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
941 {
942 if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) || (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
943 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) || (!pairing_cb.is_temp))
944 {
945 bt_status_t ret;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700946 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, is_temp=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800947 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.is_temp);
948 ret = btif_storage_add_bonded_device(&bd_addr,
949 p_auth_cmpl->key, p_auth_cmpl->key_type,
950 pairing_cb.pin_code_len);
951 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
952 }
953 else
954 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700955 BTIF_TRACE_DEBUG("%s: Temporary key. Not storing. key_type=0x%x, is_temp=%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800956 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.is_temp);
Hemant Guptab820aec2013-12-24 19:59:57 +0530957 if(pairing_cb.is_temp)
958 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700959 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
Hemant Guptab820aec2013-12-24 19:59:57 +0530960 __FUNCTION__);
961 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
962 return;
963 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800964 }
965 }
Priti Agherac0edf9f2014-06-26 11:23:51 -0700966
967 // Skip SDP for certain HID Devices
The Android Open Source Project5738f832012-12-12 16:00:35 -0800968 if (p_auth_cmpl->success)
969 {
970 status = BT_STATUS_SUCCESS;
971 state = BT_BOND_STATE_BONDED;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700972 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800973
Priti Agherac0edf9f2014-06-26 11:23:51 -0700974 if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr, COD_HID_MAJOR))
975 {
976 ALOGW("%s:skip SDP",
977 __FUNCTION__);
978 skip_sdp = TRUE;
979 }
980 if(!pairing_cb.is_local_initiated && skip_sdp)
981 {
982 bond_state_changed(status, &bd_addr, state);
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -0800983
Priti Agherac0edf9f2014-06-26 11:23:51 -0700984 ALOGW("%s: Incoming HID Connection",__FUNCTION__);
985 bt_property_t prop;
986 bt_bdaddr_t bd_addr;
987 bt_uuid_t uuid;
988 char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE;
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -0800989
Priti Agherac0edf9f2014-06-26 11:23:51 -0700990 string_to_uuid(uuid_str, &uuid);
991
992 prop.type = BT_PROPERTY_UUIDS;
993 prop.val = uuid.uu;
994 prop.len = MAX_UUID_SIZE;
995
996 /* Send the event to the BTIF */
997 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
998 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
999 }
1000 else
1001 {
1002 /* Trigger SDP on the device */
1003 pairing_cb.sdp_attempts = 1;;
1004
1005 if(btif_dm_inquiry_in_progress)
1006 btif_dm_cancel_discovery();
1007
1008 btif_dm_get_remote_services(&bd_addr);
1009 }
1010 /* Do not call bond_state_changed_cb yet. Wait till fetch remote service is complete */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001011 }
1012 else
1013 {
1014 /*Map the HCI fail reason to bt status */
1015 switch(p_auth_cmpl->fail_reason)
1016 {
1017 case HCI_ERR_PAGE_TIMEOUT:
1018 case HCI_ERR_CONNECTION_TOUT:
1019 status = BT_STATUS_RMT_DEV_DOWN;
1020 break;
1021
Hemant Guptaaef7a672013-07-31 19:00:12 +05301022 case HCI_ERR_PAIRING_NOT_ALLOWED:
1023 status = BT_STATUS_AUTH_REJECTED;
1024 break;
1025
Hemant Guptab4801442014-01-07 18:11:15 +05301026 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
1027 status = BT_STATUS_AUTH_FAILURE;
1028 break;
1029
The Android Open Source Project5738f832012-12-12 16:00:35 -08001030 /* map the auth failure codes, so we can retry pairing if necessary */
1031 case HCI_ERR_AUTH_FAILURE:
Hemant Gupta59a88ec2014-03-19 19:01:35 +05301032 case HCI_ERR_KEY_MISSING:
Zhihai Xua7ea8092013-11-27 14:10:53 +05301033 btif_storage_remove_bonded_device(&bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001034 case HCI_ERR_HOST_REJECT_SECURITY:
1035 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
1036 case HCI_ERR_UNIT_KEY_USED:
1037 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
1038 case HCI_ERR_INSUFFCIENT_SECURITY:
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301039 case HCI_ERR_PEER_USER:
1040 case HCI_ERR_UNSPECIFIED:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001041 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d",
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301042 __FUNCTION__, p_auth_cmpl->fail_reason);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001043 if (pairing_cb.autopair_attempts == 1)
1044 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001045 BTIF_TRACE_DEBUG("%s(): Adding device to blacklist ", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001046
1047 /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class */
1048 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
1049 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
1050 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
1051 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
1052 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
1053 check_cod(&bd_addr, COD_HID_POINTING))
1054 {
1055 btif_storage_add_device_to_autopair_blacklist (&bd_addr);
1056 }
1057 pairing_cb.autopair_attempts++;
1058
1059 /* Create the Bond once again */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001060 BTIF_TRACE_DEBUG("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001061 btif_dm_cb_create_bond (&bd_addr);
1062 return;
1063 }
1064 else
1065 {
1066 /* if autopair attempts are more than 1, or not attempted */
1067 status = BT_STATUS_AUTH_FAILURE;
1068 }
1069 break;
1070
1071 default:
1072 status = BT_STATUS_FAIL;
1073 }
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301074 /* Special Handling for HID Devices */
1075 if (check_cod(&bd_addr, COD_HID_POINTING)) {
1076 /* Remove Device as bonded in nvram as authentication failed */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001077 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __FUNCTION__);
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301078 btif_storage_remove_bonded_device(&bd_addr);
1079 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001080 bond_state_changed(status, &bd_addr, state);
1081 }
1082}
1083
1084/******************************************************************************
1085**
1086** Function btif_dm_search_devices_evt
1087**
1088** Description Executes search devices callback events in btif context
1089**
1090** Returns void
1091**
1092******************************************************************************/
1093static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
1094{
1095 tBTA_DM_SEARCH *p_search_data;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001096 BTIF_TRACE_EVENT("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001097
1098 switch (event)
1099 {
1100 case BTA_DM_DISC_RES_EVT:
1101 {
1102 p_search_data = (tBTA_DM_SEARCH *)p_param;
1103 /* Remote name update */
1104 if (strlen((const char *) p_search_data->disc_res.bd_name))
1105 {
1106 bt_property_t properties[1];
1107 bt_bdaddr_t bdaddr;
1108 bt_status_t status;
1109
1110 properties[0].type = BT_PROPERTY_BDNAME;
1111 properties[0].val = p_search_data->disc_res.bd_name;
1112 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
1113 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
1114
1115 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
1116 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
1117 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1118 status, &bdaddr, 1, properties);
1119 }
1120 /* TODO: Services? */
1121 }
1122 break;
1123
1124 case BTA_DM_INQ_RES_EVT:
1125 {
1126 /* inquiry result */
1127 UINT32 cod;
1128 UINT8 *p_eir_remote_name = NULL;
1129 bt_bdname_t bdname;
1130 bt_bdaddr_t bdaddr;
1131 UINT8 remote_name_len;
1132 UINT8 *p_cached_name = NULL;
1133 tBTA_SERVICE_MASK services = 0;
1134 bdstr_t bdstr;
1135
1136 p_search_data = (tBTA_DM_SEARCH *)p_param;
1137 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
1138
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001139 BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __FUNCTION__, bd2str(&bdaddr, &bdstr),
The Android Open Source Project5738f832012-12-12 16:00:35 -08001140#if (BLE_INCLUDED == TRUE)
1141 p_search_data->inq_res.device_type);
1142#else
1143 BT_DEVICE_TYPE_BREDR);
1144#endif
1145 bdname.name[0] = 0;
1146
1147 cod = devclass2uint (p_search_data->inq_res.dev_class);
1148
1149 if ( cod == 0) {
1150 ALOGD("cod is 0, set as unclassified");
1151 cod = COD_UNCLASSIFIED;
1152 }
1153
1154 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1155 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1156
1157 /* Check EIR for remote name and services */
1158 if (p_search_data->inq_res.p_eir)
1159 {
1160 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001161 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001162 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */
1163 }
1164
1165
1166 {
1167 bt_property_t properties[5];
1168 bt_device_type_t dev_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001169 UINT8 addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001170 uint32_t num_properties = 0;
1171 bt_status_t status;
1172
1173 memset(properties, 0, sizeof(properties));
1174 /* BD_ADDR */
1175 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1176 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1177 num_properties++;
1178 /* BD_NAME */
1179 /* Don't send BDNAME if it is empty */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001180 if (bdname.name[0])
1181 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001182 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1183 BT_PROPERTY_BDNAME,
1184 strlen((char *)bdname.name), &bdname);
1185 num_properties++;
1186 }
1187
1188 /* DEV_CLASS */
1189 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1190 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1191 num_properties++;
1192 /* DEV_TYPE */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001193#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001194 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1195 dev_type = p_search_data->inq_res.device_type;
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001196 addr_type = p_search_data->inq_res.ble_addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001197#else
1198 dev_type = BT_DEVICE_TYPE_BREDR;
1199#endif
1200 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1201 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1202 num_properties++;
1203 /* RSSI */
1204 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1205 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1206 &(p_search_data->inq_res.rssi));
1207 num_properties++;
1208
1209 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1210 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001211#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1212 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -07001213 if (( dev_type == BT_DEVICE_TYPE_DUMO)&&
1214 (p_search_data->inq_res.flag & BTA_BLE_DMT_CONTROLLER_SPT) &&
1215 (p_search_data->inq_res.flag & BTA_BLE_DMT_HOST_SPT))
1216 {
1217 btif_storage_set_dmt_support_type (&bdaddr, TRUE);
1218 }
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001219 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1220#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001221 /* Callback to notify upper layer of device */
1222 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1223 num_properties, properties);
1224 }
1225 }
1226 break;
1227
1228 case BTA_DM_INQ_CMPL_EVT:
1229 {
Satya Calloji6e2d9db2014-07-08 16:18:58 -07001230#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1231 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1232 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1233 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1234 bte_scan_filt_param_cfg_evt, 0);
1235#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001236 }
1237 break;
1238 case BTA_DM_DISC_CMPL_EVT:
1239 {
1240 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1241 }
1242 break;
1243 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1244 {
1245 /* if inquiry is not in progress and we get a cancel event, then
1246 * it means we are done with inquiry, but remote_name fetches are in
1247 * progress
1248 *
1249 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt
1250 * but instead wait for the cancel_cmpl_evt via the Busy Level
1251 *
1252 */
1253 if (btif_dm_inquiry_in_progress == FALSE)
1254 {
1255 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1256 }
1257 }
1258 break;
1259 }
1260}
1261
1262/*******************************************************************************
1263**
1264** Function btif_dm_search_services_evt
1265**
1266** Description Executes search services event in btif context
1267**
1268** Returns void
1269**
1270*******************************************************************************/
1271static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1272{
1273 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1274
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001275 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001276 switch (event)
1277 {
1278 case BTA_DM_DISC_RES_EVT:
1279 {
1280 bt_uuid_t uuid_arr[BT_MAX_NUM_UUIDS]; /* Max 32 services */
1281 bt_property_t prop;
1282 uint32_t i = 0, j = 0;
1283 bt_bdaddr_t bd_addr;
1284 bt_status_t ret;
1285
1286 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1287
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001288 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001289 p_data->disc_res.result, p_data->disc_res.services);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001290 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1291 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1292 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1293 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001294 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001295 pairing_cb.sdp_attempts++;
1296 btif_dm_get_remote_services(&bd_addr);
1297 return;
1298 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001299 prop.type = BT_PROPERTY_UUIDS;
1300 prop.len = 0;
1301 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1302 {
1303 prop.val = p_data->disc_res.p_uuid_list;
1304 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1305 for (i=0; i < p_data->disc_res.num_uuids; i++)
1306 {
1307 char temp[256];
1308 uuid_to_string((bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i*MAX_UUID_SIZE)), temp);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001309 BTIF_TRACE_ERROR("Index: %d uuid:%s", i, temp);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001310 }
1311 }
1312
1313 /* onUuidChanged requires getBondedDevices to be populated.
1314 ** bond_state_changed needs to be sent prior to remote_device_property
1315 */
1316 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1317 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0)&&
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001318 pairing_cb.sdp_attempts > 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001319 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001320 BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001321 __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001322 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001323 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1324 }
1325
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001326 if(p_data->disc_res.num_uuids != 0)
1327 {
1328 /* Also write this to the NVRAM */
1329 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1330 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1331 /* Send the event to the BTIF */
1332 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1333 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1334 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001335 }
1336 break;
1337
1338 case BTA_DM_DISC_CMPL_EVT:
1339 /* fixme */
1340 break;
1341
Matthew Xie607e3b72013-08-15 19:30:48 -07001342#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001343 case BTA_DM_DISC_BLE_RES_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001344 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__,
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001345 p_data->disc_ble_res.service.uu.uuid16);
1346 bt_uuid_t uuid;
1347 int i = 0;
1348 int j = 15;
1349 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1350 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001351 BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001352 bt_property_t prop;
1353 bt_bdaddr_t bd_addr;
1354 char temp[256];
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001355 bt_status_t ret;
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001356
1357 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1358
1359 while(i < j )
1360 {
1361 unsigned char c = uuid.uu[j];
1362 uuid.uu[j] = uuid.uu[i];
1363 uuid.uu[i] = c;
1364 i++;
1365 j--;
1366 }
1367
1368 uuid_to_string(&uuid, temp);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001369 BTIF_TRACE_ERROR(" uuid:%s", temp);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001370
1371 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1372 prop.type = BT_PROPERTY_UUIDS;
1373 prop.val = uuid.uu;
1374 prop.len = MAX_UUID_SIZE;
1375
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001376 /* Also write this to the NVRAM */
1377 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1378 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1379
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001380 /* Send the event to the BTIF */
1381 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1382 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1383
1384 }
1385 break;
Matthew Xie607e3b72013-08-15 19:30:48 -07001386#endif /* BLE_INCLUDED */
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001387
The Android Open Source Project5738f832012-12-12 16:00:35 -08001388 default:
1389 {
1390 ASSERTC(0, "unhandled search services event", event);
1391 }
1392 break;
1393 }
1394}
1395
1396/*******************************************************************************
1397**
1398** Function btif_dm_remote_service_record_evt
1399**
1400** Description Executes search service record event in btif context
1401**
1402** Returns void
1403**
1404*******************************************************************************/
1405static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1406{
1407 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1408
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001409 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001410 switch (event)
1411 {
1412 case BTA_DM_DISC_RES_EVT:
1413 {
1414 bt_service_record_t rec;
1415 bt_property_t prop;
1416 uint32_t i = 0;
1417 bt_bdaddr_t bd_addr;
1418
1419 memset(&rec, 0, sizeof(bt_service_record_t));
1420 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1421
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001422 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001423 p_data->disc_res.result, p_data->disc_res.services);
1424 prop.type = BT_PROPERTY_SERVICE_RECORD;
1425 prop.val = (void*)&rec;
1426 prop.len = sizeof(rec);
1427
1428 /* disc_res.result is overloaded with SCN. Cannot check result */
1429 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1430 /* TODO: Get the UUID as well */
1431 rec.channel = p_data->disc_res.result - 3;
1432 /* TODO: Need to get the service name using p_raw_data */
1433 rec.name[0] = 0;
1434
1435 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1436 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1437 }
1438 break;
1439
1440 default:
1441 {
1442 ASSERTC(0, "unhandled remote service record event", event);
1443 }
1444 break;
1445 }
1446}
1447
1448/*******************************************************************************
1449**
1450** Function btif_dm_upstreams_cback
1451**
1452** Description Executes UPSTREAMS events in btif context
1453**
1454** Returns void
1455**
1456*******************************************************************************/
1457static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1458{
1459 tBTA_DM_SEC_EVT dm_event = (tBTA_DM_SEC_EVT)event;
1460 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1461 tBTA_SERVICE_MASK service_mask;
1462 uint32_t i;
1463 bt_bdaddr_t bd_addr;
1464
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001465 BTIF_TRACE_EVENT("btif_dm_upstreams_cback ev: %s", dump_dm_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001466
1467 switch (event)
1468 {
1469 case BTA_DM_ENABLE_EVT:
1470 {
1471 BD_NAME bdname;
1472 bt_status_t status;
1473 bt_property_t prop;
1474 prop.type = BT_PROPERTY_BDNAME;
1475 prop.len = BD_NAME_LEN;
1476 prop.val = (void*)bdname;
1477
1478 status = btif_storage_get_adapter_property(&prop);
Matthew Xie1e5109b2012-11-09 18:26:26 -08001479 if (status == BT_STATUS_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001480 {
1481 /* A name exists in the storage. Make this the device name */
1482 BTA_DmSetDeviceName((char*)prop.val);
1483 }
Matthew Xie1e5109b2012-11-09 18:26:26 -08001484 else
1485 {
1486 /* Storage does not have a name yet.
1487 * Use the default name and write it to the chip
1488 */
1489 BTA_DmSetDeviceName(btif_get_default_local_name());
1490 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001491
1492 /* for each of the enabled services in the mask, trigger the profile
1493 * enable */
1494 service_mask = btif_get_enabled_services_mask();
1495 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1496 {
1497 if (service_mask &
1498 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1499 {
1500 btif_in_execute_service_request(i, TRUE);
1501 }
1502 }
1503 /* clear control blocks */
1504 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1505
1506 /* This function will also trigger the adapter_properties_cb
1507 ** and bonded_devices_info_cb
1508 */
1509 btif_storage_load_bonded_devices();
1510
1511 btif_storage_load_autopair_device_list();
1512
1513 btif_enable_bluetooth_evt(p_data->enable.status, p_data->enable.bd_addr);
Satya Calloji0943c102014-05-12 09:13:02 -07001514
Wei Wangbf0e4b22014-05-19 23:23:35 -07001515 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Calloji0943c102014-05-12 09:13:02 -07001516 /* Enable local privacy */
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001517 /*TODO Should this call be exposed to JAVA...? */
Satya Calloji0943c102014-05-12 09:13:02 -07001518 BTA_DmBleConfigLocalPrivacy(TRUE);
Wei Wangbf0e4b22014-05-19 23:23:35 -07001519 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001520 }
1521 break;
1522
1523 case BTA_DM_DISABLE_EVT:
1524 /* for each of the enabled services in the mask, trigger the profile
1525 * disable */
1526 service_mask = btif_get_enabled_services_mask();
1527 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1528 {
1529 if (service_mask &
1530 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1531 {
1532 btif_in_execute_service_request(i, FALSE);
1533 }
1534 }
1535 btif_disable_bluetooth_evt();
1536 break;
1537
1538 case BTA_DM_PIN_REQ_EVT:
1539 btif_dm_pin_req_evt(&p_data->pin_req);
1540 break;
1541
1542 case BTA_DM_AUTH_CMPL_EVT:
1543 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1544 break;
1545
1546 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1547 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1548 {
1549 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1550 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1551 }
1552 break;
1553
1554 case BTA_DM_SP_CFM_REQ_EVT:
1555 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1556 break;
1557 case BTA_DM_SP_KEY_NOTIF_EVT:
1558 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1559 break;
1560
1561 case BTA_DM_DEV_UNPAIRED_EVT:
1562 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1563
1564 /*special handling for HID devices */
1565 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -07001566 btif_hh_remove_device(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001567 #endif
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001568 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1569 btif_storage_remove_ble_bonding_keys(&bd_addr);
1570 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001571 btif_storage_remove_bonded_device(&bd_addr);
1572 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1573 break;
1574
1575 case BTA_DM_BUSY_LEVEL_EVT:
1576 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001577
1578 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001579 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001580 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001581 {
1582 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1583 BT_DISCOVERY_STARTED);
1584 btif_dm_inquiry_in_progress = TRUE;
1585 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001586 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001587 {
1588 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1589 BT_DISCOVERY_STOPPED);
1590 btif_dm_inquiry_in_progress = FALSE;
1591 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001592 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001593 {
1594 btif_dm_inquiry_in_progress = FALSE;
1595 }
1596 }
1597 }break;
1598
1599 case BTA_DM_LINK_UP_EVT:
1600 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001601 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001602
1603 btif_update_remote_version_property(&bd_addr);
1604
The Android Open Source Project5738f832012-12-12 16:00:35 -08001605 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1606 &bd_addr, BT_ACL_STATE_CONNECTED);
1607 break;
1608
1609 case BTA_DM_LINK_DOWN_EVT:
1610 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001611 BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001612 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1613 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1614 break;
1615
1616 case BTA_DM_HW_ERROR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001617 BTIF_TRACE_ERROR("Received H/W Error. ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001618 /* Flush storage data */
1619 btif_config_flush();
1620 usleep(100000); /* 100milliseconds */
1621 /* Killing the process to force a restart as part of fault tolerance */
1622 kill(getpid(), SIGKILL);
1623 break;
1624
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001625#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1626 case BTA_DM_BLE_KEY_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001627 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT key_type=0x%02x ", p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001628
1629 /* If this pairing is by-product of local initiated GATT client Read or Write,
1630 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1631 have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1632 if (pairing_cb.state != BT_BOND_STATE_BONDING)
1633 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001634 BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001635 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1636 BT_BOND_STATE_BONDING);
1637 }
1638 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1639 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001640 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001641 break;
1642 }
1643
1644 switch (p_data->ble_key.key_type)
1645 {
1646 case BTA_LE_KEY_PENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001647 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001648 pairing_cb.ble.is_penc_key_rcvd = TRUE;
1649 memcpy(pairing_cb.ble.penc_key.ltk,p_data->ble_key.key_value.penc_key.ltk, 16);
1650 memcpy(pairing_cb.ble.penc_key.rand, p_data->ble_key.key_value.penc_key.rand,8);
1651 pairing_cb.ble.penc_key.ediv = p_data->ble_key.key_value.penc_key.ediv;
1652 pairing_cb.ble.penc_key.sec_level = p_data->ble_key.key_value.penc_key.sec_level;
1653
1654 for (i=0; i<16; i++)
1655 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001656 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.ltk[%d]=0x%02x",i,pairing_cb.ble.penc_key.ltk[i]);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001657 }
1658 for (i=0; i<8; i++)
1659 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001660 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.rand[%d]=0x%02x",i,pairing_cb.ble.penc_key.rand[i]);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001661 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001662 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.ediv=0x%04x",pairing_cb.ble.penc_key.ediv);
1663 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.sec_level=0x%02x",pairing_cb.ble.penc_key.sec_level);
1664 BTIF_TRACE_DEBUG("pairing_cb.ble.penc_key.key_size=0x%02x",pairing_cb.ble.penc_key.key_size);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001665 break;
1666
1667 case BTA_LE_KEY_PID:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001668 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001669 pairing_cb.ble.is_pid_key_rcvd = TRUE;
1670 memcpy(pairing_cb.ble.pid_key, p_data->ble_key.key_value.pid_key.irk, 16);
1671 for (i=0; i<16; i++)
1672 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001673 BTIF_TRACE_DEBUG("pairing_cb.ble.pid_key[%d]=0x%02x",i,pairing_cb.ble.pid_key[i]);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001674 }
1675 break;
1676
1677 case BTA_LE_KEY_PCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001678 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001679 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1680 pairing_cb.ble.pcsrk_key.counter = p_data->ble_key.key_value.pcsrk_key.counter;
1681 pairing_cb.ble.pcsrk_key.sec_level = p_data->ble_key.key_value.pcsrk_key.sec_level;
1682 memcpy(pairing_cb.ble.pcsrk_key.csrk,p_data->ble_key.key_value.pcsrk_key.csrk,16);
1683
1684 for (i=0; i<16; i++)
1685 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001686 BTIF_TRACE_DEBUG("pairing_cb.ble.pcsrk_key.csrk[%d]=0x%02x",i,pairing_cb.ble.pcsrk_key.csrk[i]);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001687 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001688 BTIF_TRACE_DEBUG("pairing_cb.ble.pcsrk_key.counter=0x%08x",pairing_cb.ble.pcsrk_key.counter);
1689 BTIF_TRACE_DEBUG("pairing_cb.ble.pcsrk_key.sec_level=0x%02x",pairing_cb.ble.pcsrk_key.sec_level);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001690 break;
1691
1692 case BTA_LE_KEY_LENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001693 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001694 pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1695 pairing_cb.ble.lenc_key.div = p_data->ble_key.key_value.lenc_key.div;
1696 pairing_cb.ble.lenc_key.key_size = p_data->ble_key.key_value.lenc_key.key_size;
1697 pairing_cb.ble.lenc_key.sec_level = p_data->ble_key.key_value.lenc_key.sec_level;
1698
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001699 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.div=0x%04x",pairing_cb.ble.lenc_key.div);
1700 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.key_size=0x%02x",pairing_cb.ble.lenc_key.key_size);
1701 BTIF_TRACE_DEBUG("pairing_cb.ble.lenc_key.sec_level=0x%02x",pairing_cb.ble.lenc_key.sec_level);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001702 break;
1703
1704
1705
1706 case BTA_LE_KEY_LCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001707 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001708 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1709 pairing_cb.ble.lcsrk_key.counter = p_data->ble_key.key_value.lcsrk_key.counter;
1710 pairing_cb.ble.lcsrk_key.div = p_data->ble_key.key_value.lcsrk_key.div;
1711 pairing_cb.ble.lcsrk_key.sec_level = p_data->ble_key.key_value.lcsrk_key.sec_level;
1712
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001713 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.div=0x%04x",pairing_cb.ble.lcsrk_key.div);
1714 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.counter=0x%08x",pairing_cb.ble.lcsrk_key.counter);
1715 BTIF_TRACE_DEBUG("pairing_cb.ble.lcsrk_key.sec_level=0x%02x",pairing_cb.ble.lcsrk_key.sec_level);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001716
1717 break;
1718
1719 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001720 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001721 break;
1722 }
1723
1724 break;
1725 case BTA_DM_BLE_SEC_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001726 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001727 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1728 break;
1729 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001730 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001731 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1732 break;
1733 case BTA_DM_BLE_PASSKEY_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001734 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001735 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1736 break;
1737 case BTA_DM_BLE_OOB_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001738 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001739 break;
1740 case BTA_DM_BLE_LOCAL_IR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001741 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001742 ble_local_key_cb.is_id_keys_rcvd = TRUE;
1743 memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1744 memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1745 memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1746 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1747 BTIF_DM_LE_LOCAL_KEY_IR,
1748 BT_OCTET16_LEN);
1749 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1750 BTIF_DM_LE_LOCAL_KEY_IRK,
1751 BT_OCTET16_LEN);
1752 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1753 BTIF_DM_LE_LOCAL_KEY_DHK,
1754 BT_OCTET16_LEN);
1755 break;
1756 case BTA_DM_BLE_LOCAL_ER_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001757 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001758 ble_local_key_cb.is_er_rcvd = TRUE;
1759 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1760 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1761 BTIF_DM_LE_LOCAL_KEY_ER,
1762 BT_OCTET16_LEN);
1763 break;
1764
1765 case BTA_DM_BLE_AUTH_CMPL_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001766 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001767 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1768 break;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001769
1770 case BTA_DM_LE_FEATURES_READ:
1771 {
1772 tBTM_BLE_VSC_CB cmn_vsc_cb;
1773 bt_local_le_features_t local_le_features;
1774 char buf[512];
1775 bt_property_t prop;
1776 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1777 prop.val = (void*)buf;
1778 prop.len = sizeof(buf);
1779
1780 /* LE features are not stored in storage. Should be retrived from stack */
1781 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1782 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1783
1784 prop.len = sizeof (bt_local_le_features_t);
1785 if (cmn_vsc_cb.filter_support == 1)
1786 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1787 else
1788 local_le_features.max_adv_filter_supported = 0;
1789 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1790 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1791 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
Satya Callojid773c2c2014-07-29 22:08:55 -07001792 local_le_features.scan_result_storage_size_hibyte =
1793 (cmn_vsc_cb.tot_scan_results_strg >> 8) & (0xFF);
1794 local_le_features.scan_result_storage_size_lobyte =
1795 (cmn_vsc_cb.tot_scan_results_strg) & (0xFF);
Satya Callojiefaddcb2014-07-28 23:22:05 -07001796 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001797 memcpy(prop.val, &local_le_features, prop.len);
1798 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
1799 break;
1800 }
Satya Callojie5ba8842014-07-03 17:18:02 -07001801
1802 case BTA_DM_ENER_INFO_READ:
1803 {
1804 btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param;
1805 bt_activity_energy_info energy_info;
1806 energy_info.status = p_ener_data->status;
1807 energy_info.ctrl_state = p_ener_data->ctrl_state;
1808 energy_info.rx_time = p_ener_data->rx_time;
1809 energy_info.tx_time = p_ener_data->tx_time;
1810 energy_info.idle_time = p_ener_data->idle_time;
1811 energy_info.energy_used = p_ener_data->energy_used;
1812 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info);
1813 break;
1814 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001815#endif
1816
The Android Open Source Project5738f832012-12-12 16:00:35 -08001817 case BTA_DM_AUTHORIZE_EVT:
1818 case BTA_DM_SIG_STRENGTH_EVT:
1819 case BTA_DM_SP_RMT_OOB_EVT:
1820 case BTA_DM_SP_KEYPRESS_EVT:
1821 case BTA_DM_ROLE_CHG_EVT:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001822
The Android Open Source Project5738f832012-12-12 16:00:35 -08001823 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001824 BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001825 break;
1826 }
1827} /* btui_security_cback() */
1828
1829
1830/*******************************************************************************
1831**
1832** Function btif_dm_generic_evt
1833**
1834** Description Executes non-BTA upstream events in BTIF context
1835**
1836** Returns void
1837**
1838*******************************************************************************/
1839static void btif_dm_generic_evt(UINT16 event, char* p_param)
1840{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001841 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001842 switch(event)
1843 {
1844 case BTIF_DM_CB_DISCOVERY_STARTED:
1845 {
1846 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
1847 }
1848 break;
1849
1850 case BTIF_DM_CB_CREATE_BOND:
1851 {
1852 btif_dm_cb_create_bond((bt_bdaddr_t *)p_param);
1853 }
1854 break;
1855
1856 case BTIF_DM_CB_REMOVE_BOND:
1857 {
1858 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
1859 }
1860 break;
1861
1862 case BTIF_DM_CB_HID_REMOTE_NAME:
1863 {
1864 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
1865 }
1866 break;
1867
1868 case BTIF_DM_CB_BOND_STATE_BONDING:
1869 {
1870 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
1871 }
1872 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001873 case BTIF_DM_CB_LE_TX_TEST:
1874 case BTIF_DM_CB_LE_RX_TEST:
1875 {
1876 uint8_t status;
1877 STREAM_TO_UINT8(status, p_param);
1878 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1879 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
1880 }
1881 break;
1882 case BTIF_DM_CB_LE_TEST_END:
1883 {
1884 uint8_t status;
1885 uint16_t count = 0;
1886 STREAM_TO_UINT8(status, p_param);
1887 if (status == 0)
1888 STREAM_TO_UINT16(count, p_param);
1889 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1890 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
1891 }
1892 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001893 default:
1894 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001895 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001896 }
1897 break;
1898 }
1899}
1900
1901/*******************************************************************************
1902**
1903** Function bte_dm_evt
1904**
1905** Description Switches context from BTE to BTIF for all DM events
1906**
1907** Returns void
1908**
1909*******************************************************************************/
1910
1911void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
1912{
1913 bt_status_t status;
1914
1915 /* switch context to btif task context (copy full union size for convenience) */
1916 status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event, (void*)p_data, sizeof(tBTA_DM_SEC), NULL);
1917
1918 /* catch any failed context transfers */
1919 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
1920}
1921
1922/*******************************************************************************
1923**
1924** Function bte_search_devices_evt
1925**
1926** Description Switches context from BTE to BTIF for DM search events
1927**
1928** Returns void
1929**
1930*******************************************************************************/
1931static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1932{
1933 UINT16 param_len = 0;
1934
1935 if (p_data)
1936 param_len += sizeof(tBTA_DM_SEARCH);
1937 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
1938 switch (event)
1939 {
1940 case BTA_DM_INQ_RES_EVT:
1941 {
1942 if (p_data->inq_res.p_eir)
1943 param_len += HCI_EXT_INQ_RESPONSE_LEN;
1944 }
1945 break;
1946
1947 case BTA_DM_DISC_RES_EVT:
1948 {
1949 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
1950 param_len += p_data->disc_res.raw_data_size;
1951 }
1952 break;
1953 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001954 BTIF_TRACE_DEBUG("%s event=%s param_len=%d", __FUNCTION__, dump_dm_search_event(event), param_len);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001955
1956 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
1957 if (event == BTA_DM_INQ_RES_EVT)
1958 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
1959
1960 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
1961 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
1962}
1963
1964/*******************************************************************************
1965**
1966** Function bte_dm_search_services_evt
1967**
1968** Description Switches context from BTE to BTIF for DM search services
1969** event
1970**
1971** Returns void
1972**
1973*******************************************************************************/
1974static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
1975{
1976 UINT16 param_len = 0;
1977 if (p_data)
1978 param_len += sizeof(tBTA_DM_SEARCH);
1979 switch (event)
1980 {
1981 case BTA_DM_DISC_RES_EVT:
1982 {
1983 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
1984 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
1985 }
1986 } break;
1987 }
1988 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
1989 * if raw_data is needed. */
1990 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
1991 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
1992}
1993
1994/*******************************************************************************
1995**
1996** Function bte_dm_remote_service_record_evt
1997**
1998** Description Switches context from BTE to BTIF for DM search service
1999** record event
2000**
2001** Returns void
2002**
2003*******************************************************************************/
2004static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2005{
2006 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
2007 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
2008}
2009
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002010#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07002011/*******************************************************************************
2012**
2013** Function bta_energy_info_cb
2014**
2015** Description Switches context from BTE to BTIF for DM energy info event
2016**
2017** Returns void
2018**
2019*******************************************************************************/
2020static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time,
2021 tBTA_DM_BLE_IDLE_TIME_MS idle_time,
2022 tBTA_DM_BLE_ENERGY_USED energy_used,
2023 tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status)
2024{
2025 BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld",
2026 status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
2027
2028 btif_activity_energy_info_cb_t btif_cb;
2029 btif_cb.status = status;
2030 btif_cb.ctrl_state = ctrl_state;
2031 btif_cb.tx_time = (uint64_t) tx_time;
2032 btif_cb.rx_time = (uint64_t) rx_time;
2033 btif_cb.idle_time =(uint64_t) idle_time;
2034 btif_cb.energy_used =(uint64_t) energy_used;
2035 btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
2036 (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL);
2037}
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002038#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07002039
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002040/*******************************************************************************
2041**
2042** Function bte_scan_filt_param_cfg_evt
2043**
2044** Description Scan filter param config event
2045**
2046** Returns void
2047**
2048*******************************************************************************/
2049static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
2050 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
2051 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
2052{
2053 /* This event occurs on calling BTA_DmBleCfgFilterCondition internally,
2054 ** and that is why there is no HAL callback
2055 */
2056 if(BTA_SUCCESS != status)
2057 {
2058 BTIF_TRACE_ERROR("%s, %d", __FUNCTION__, status);
2059 }
2060 else
2061 {
2062 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2063 }
2064}
2065
The Android Open Source Project5738f832012-12-12 16:00:35 -08002066/*****************************************************************************
2067**
2068** btif api functions (no context switch)
2069**
2070*****************************************************************************/
2071
2072/*******************************************************************************
2073**
2074** Function btif_dm_start_discovery
2075**
2076** Description Start device discovery/inquiry
2077**
2078** Returns bt_status_t
2079**
2080*******************************************************************************/
2081bt_status_t btif_dm_start_discovery(void)
2082{
2083 tBTA_DM_INQ inq_params;
2084 tBTA_SERVICE_MASK services = 0;
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002085 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002086
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002087 BTIF_TRACE_EVENT("%s", __FUNCTION__);
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002088
2089#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2090 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
2091 /* Cleanup anything remaining on index 0 */
2092 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
2093 bte_scan_filt_param_cfg_evt, 0);
2094
2095 /* Add an allow-all filter on index 0*/
2096 adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE;
2097 adv_filt_param.feat_seln = ALLOW_ALL_FILTER;
2098 adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;
2099 adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;
2100 adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE;
2101 adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE;
2102 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL,
2103 bte_scan_filt_param_cfg_evt, 0);
2104
The Android Open Source Project5738f832012-12-12 16:00:35 -08002105 /* TODO: Do we need to handle multiple inquiries at the same time? */
2106
2107 /* Set inquiry params and call API */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002108 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
Matthew Xie7f3e4292013-09-30 12:44:10 -07002109#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
2110 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
2111 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
2112 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
2113 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
2114#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002115#else
2116 inq_params.mode = BTA_DM_GENERAL_INQUIRY;
2117#endif
2118 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
2119
2120 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
2121 inq_params.report_dup = TRUE;
2122
2123 inq_params.filter_type = BTA_DM_INQ_CLR;
2124 /* TODO: Filter device by BDA needs to be implemented here */
2125
2126 /* Will be enabled to TRUE once inquiry busy level has been received */
2127 btif_dm_inquiry_in_progress = FALSE;
2128 /* find nearby devices */
2129 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
2130
2131 return BT_STATUS_SUCCESS;
2132}
2133
2134/*******************************************************************************
2135**
2136** Function btif_dm_cancel_discovery
2137**
2138** Description Cancels search
2139**
2140** Returns bt_status_t
2141**
2142*******************************************************************************/
2143bt_status_t btif_dm_cancel_discovery(void)
2144{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002145 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002146 BTA_DmSearchCancel();
2147 return BT_STATUS_SUCCESS;
2148}
2149
2150/*******************************************************************************
2151**
2152** Function btif_dm_create_bond
2153**
2154** Description Initiate bonding with the specified device
2155**
2156** Returns bt_status_t
2157**
2158*******************************************************************************/
2159bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr)
2160{
2161 bdstr_t bdstr;
2162
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002163 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *) bd_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002164 if (pairing_cb.state != BT_BOND_STATE_NONE)
2165 return BT_STATUS_BUSY;
2166
2167 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
2168 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2169
2170 return BT_STATUS_SUCCESS;
2171}
2172
2173/*******************************************************************************
2174**
2175** Function btif_dm_cancel_bond
2176**
2177** Description Initiate bonding with the specified device
2178**
2179** Returns bt_status_t
2180**
2181*******************************************************************************/
2182
2183bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
2184{
2185 bdstr_t bdstr;
2186
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002187 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *)bd_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002188
2189 /* TODO:
2190 ** 1. Restore scan modes
2191 ** 2. special handling for HID devices
2192 */
2193 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2194 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002195
2196#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2197
2198 if (pairing_cb.is_ssp)
2199 {
2200 if (pairing_cb.is_le_only)
2201 {
2202 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2203 }
2204 else
Hemant Guptae1468692013-11-14 16:21:29 +05302205 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002206 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
Hemant Guptae1468692013-11-14 16:21:29 +05302207 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2208 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2209 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002210 }
2211 else
2212 {
2213 if (pairing_cb.is_le_only)
2214 {
2215 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2216 }
2217 else
2218 {
2219 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2220 }
2221 /* Cancel bonding, in case it is in ACL connection setup state */
2222 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2223 }
2224
2225#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002226 if (pairing_cb.is_ssp)
2227 {
2228 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2229 }
2230 else
2231 {
2232 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2233 }
2234 /* Cancel bonding, in case it is in ACL connection setup state */
2235 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2236 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002237#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002238 }
2239
2240 return BT_STATUS_SUCCESS;
2241}
2242
2243/*******************************************************************************
2244**
Kim Schulza9eb25c2013-09-30 10:55:52 +02002245** Function btif_dm_hh_open_failed
2246**
2247** Description informs the upper layers if the HH have failed during bonding
2248**
2249** Returns none
2250**
2251*******************************************************************************/
2252
2253void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
2254{
2255 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2256 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2257 {
2258 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2259 }
2260}
2261
2262/*******************************************************************************
2263**
The Android Open Source Project5738f832012-12-12 16:00:35 -08002264** Function btif_dm_remove_bond
2265**
2266** Description Removes bonding with the specified device
2267**
2268** Returns bt_status_t
2269**
2270*******************************************************************************/
2271
2272bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
2273{
2274 bdstr_t bdstr;
2275
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002276 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bd2str((bt_bdaddr_t *)bd_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002277 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2278 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2279
2280 return BT_STATUS_SUCCESS;
2281}
2282
2283/*******************************************************************************
2284**
2285** Function btif_dm_pin_reply
2286**
2287** Description BT legacy pairing - PIN code reply
2288**
2289** Returns bt_status_t
2290**
2291*******************************************************************************/
2292
2293bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2294 uint8_t pin_len, bt_pin_code_t *pin_code)
2295{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002296 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Hemant Gupta831423e2014-01-08 12:42:13 +05302297 if (pin_code == NULL)
2298 return BT_STATUS_FAIL;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002299#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002300
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002301 if (pairing_cb.is_le_only)
2302 {
2303 int i;
2304 UINT32 passkey = 0;
2305 int multi[] = {100000, 10000, 1000, 100, 10,1};
2306 BD_ADDR remote_bd_addr;
2307 bdcpy(remote_bd_addr, bd_addr->address);
2308 for (i = 0; i < 6; i++)
2309 {
2310 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2311 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002312 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002313 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2314
2315 }
2316 else
2317 {
2318 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2319 if (accept)
2320 pairing_cb.pin_code_len = pin_len;
2321 }
2322#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002323 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2324
2325 if (accept)
2326 pairing_cb.pin_code_len = pin_len;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002327#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002328 return BT_STATUS_SUCCESS;
2329}
2330
2331/*******************************************************************************
2332**
2333** Function btif_dm_ssp_reply
2334**
2335** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2336**
2337** Returns bt_status_t
2338**
2339*******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002340bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2341 bt_ssp_variant_t variant, uint8_t accept,
2342 uint32_t passkey)
2343{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08002344 UNUSED(passkey);
2345
The Android Open Source Project5738f832012-12-12 16:00:35 -08002346 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2347 {
2348 /* This is not implemented in the stack.
2349 * For devices with display, this is not needed
2350 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002351 BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002352 return BT_STATUS_FAIL;
2353 }
2354 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002355 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002356#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2357 if (pairing_cb.is_le_only)
2358 {
2359 if (accept)
2360 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2361 else
2362 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2363 }
2364 else
2365 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002366
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002367#else
2368 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2369#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002370 return BT_STATUS_SUCCESS;
2371}
2372
2373/*******************************************************************************
2374**
2375** Function btif_dm_get_adapter_property
2376**
2377** Description Queries the BTA for the adapter property
2378**
2379** Returns bt_status_t
2380**
2381*******************************************************************************/
2382bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2383{
2384 bt_status_t status;
2385
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002386 BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002387 switch (prop->type)
2388 {
2389 case BT_PROPERTY_BDNAME:
2390 {
2391 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
Matthew Xie1e5109b2012-11-09 18:26:26 -08002392 strcpy((char *)bd_name->name, btif_get_default_local_name());
The Android Open Source Project5738f832012-12-12 16:00:35 -08002393 prop->len = strlen((char *)bd_name->name);
2394 }
2395 break;
2396
2397 case BT_PROPERTY_ADAPTER_SCAN_MODE:
2398 {
2399 /* if the storage does not have it. Most likely app never set it. Default is NONE */
2400 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2401 *mode = BT_SCAN_MODE_NONE;
2402 prop->len = sizeof(bt_scan_mode_t);
2403 }
2404 break;
2405
2406 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2407 {
2408 uint32_t *tmt = (uint32_t*)prop->val;
2409 *tmt = 120; /* default to 120s, if not found in NV */
2410 prop->len = sizeof(uint32_t);
2411 }
2412 break;
2413
2414 default:
2415 prop->len = 0;
2416 return BT_STATUS_FAIL;
2417 }
2418 return BT_STATUS_SUCCESS;
2419}
2420
2421/*******************************************************************************
2422**
2423** Function btif_dm_get_remote_services
2424**
2425** Description Start SDP to get remote services
2426**
2427** Returns bt_status_t
2428**
2429*******************************************************************************/
2430bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2431{
2432 bdstr_t bdstr;
2433
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002434 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002435
2436 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2437 bte_dm_search_services_evt, TRUE);
2438
2439 return BT_STATUS_SUCCESS;
2440}
2441
2442/*******************************************************************************
2443**
2444** Function btif_dm_get_remote_service_record
2445**
2446** Description Start SDP to get remote service record
2447**
2448**
2449** Returns bt_status_t
2450*******************************************************************************/
2451bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2452 bt_uuid_t *uuid)
2453{
2454 tSDP_UUID sdp_uuid;
2455 bdstr_t bdstr;
2456
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002457 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bd2str(remote_addr, &bdstr));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002458
2459 sdp_uuid.len = MAX_UUID_SIZE;
2460 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2461
2462 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2463 bte_dm_remote_service_record_evt, TRUE);
2464
2465 return BT_STATUS_SUCCESS;
2466}
2467
2468void btif_dm_execute_service_request(UINT16 event, char *p_param)
2469{
2470 BOOLEAN b_enable = FALSE;
2471 bt_status_t status;
2472 if (event == BTIF_DM_ENABLE_SERVICE)
2473 {
2474 b_enable = TRUE;
2475 }
2476 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2477 if (status == BT_STATUS_SUCCESS)
2478 {
2479 bt_property_t property;
2480 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2481
2482 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2483 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2484 sizeof(local_uuids), local_uuids);
2485 btif_storage_get_adapter_property(&property);
2486 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2487 BT_STATUS_SUCCESS, 1, &property);
2488 }
2489 return;
2490}
2491
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002492void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2493 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2494{
2495 UINT8 yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2496 /* if local initiated:
2497 ** 1. set DD + MITM
2498 ** if remote initiated:
2499 ** 1. Copy over the auth_req from peer's io_rsp
2500 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2501 ** as a fallback set MITM+GB if peer had MITM set
2502 */
2503 UNUSED (bd_addr);
2504 UNUSED (p_io_cap);
2505 UNUSED (p_oob_data);
2506
2507
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002508 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002509 if(pairing_cb.is_local_initiated)
2510 {
2511 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2512 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2513 }
2514 else if (!is_orig)
2515 {
2516 /* peer initiated paring. They probably know what they want.
2517 ** Copy the mitm from peer device.
2518 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002519 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002520 __FUNCTION__, pairing_cb.auth_req);
2521 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2522
2523 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2524 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2525 *p_auth_req |= BTA_AUTH_SP_YES;
2526 }
2527 else if (yes_no_bit)
2528 {
2529 /* set the general bonding bit for stored device */
2530 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2531 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002532 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002533}
2534
2535void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2536 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2537{
2538 UNUSED (bd_addr);
2539 UNUSED (oob_data);
2540
2541 if(auth_req & BTA_AUTH_BONDS)
2542 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002543 BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002544 pairing_cb.auth_req = auth_req;
2545 pairing_cb.io_cap = io_cap;
2546 }
2547}
2548
The Android Open Source Project5738f832012-12-12 16:00:35 -08002549#if (BTM_OOB_INCLUDED == TRUE)
2550void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data)
2551{
2552 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2553 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2554 {
2555 *p_oob_data = FALSE;
2556 }
2557 else
2558 {
2559 *p_oob_data = TRUE;
2560 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002561 BTIF_TRACE_DEBUG("btif_dm_set_oob_for_io_req *p_oob_data=%d", *p_oob_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002562}
2563#endif /* BTM_OOB_INCLUDED */
2564
2565#ifdef BTIF_DM_OOB_TEST
2566void btif_dm_load_local_oob(void)
2567{
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002568 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002569 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002570 BTIF_TRACE_DEBUG("btif_dm_load_local_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002571 if (prop_oob[0] != '3')
2572 {
2573#if (BTM_OOB_INCLUDED == TRUE)
2574 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2575 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2576 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002577 BTIF_TRACE_DEBUG("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002578 BTA_DmLocalOob();
2579 }
2580#else
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002581 BTIF_TRACE_ERROR("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002582#endif
2583 }
2584}
2585
2586void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2587{
2588 FILE *fp;
2589 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2590 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2591 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002592 char prop_oob[PROPERTY_VALUE_MAX];
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002593 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: valid=%d", valid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002594 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2595 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2596 valid)
2597 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002598 BTIF_TRACE_DEBUG("save local OOB data in memory");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002599 memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2600 memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2601 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002602 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002603 if (prop_oob[0] == '1')
2604 path = path_a;
2605 else if (prop_oob[0] == '2')
2606 path = path_b;
2607 if (path)
2608 {
2609 fp = fopen(path, "wb+");
2610 if (fp == NULL)
2611 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002612 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: failed to save local OOB data to %s", path);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002613 }
2614 else
2615 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002616 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: save local OOB data into file %s",path);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002617 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2618 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2619 fclose(fp);
2620 }
2621 }
2622 }
2623}
2624BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2625{
2626 char t[128];
2627 FILE *fp;
2628 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2629 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2630 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002631 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002632 BOOLEAN result = FALSE;
2633 bt_bdaddr_t bt_bd_addr;
2634 bdcpy(oob_cb.oob_bdaddr, bd_addr);
2635 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002636 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002637 if (prop_oob[0] == '1')
2638 path = path_b;
2639 else if (prop_oob[0] == '2')
2640 path = path_a;
2641 if (path)
2642 {
2643 fp = fopen(path, "rb");
2644 if (fp == NULL)
2645 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002646 BTIF_TRACE_DEBUG("btapp_dm_rmt_oob_reply: failed to read OOB keys from %s",path);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002647 return FALSE;
2648 }
2649 else
2650 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002651 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob: read OOB data from %s",path);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002652 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2653 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2654 fclose(fp);
2655 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002656 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: TRUE");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002657 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2658 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2659 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002660 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002661 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2662 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
2663 p_c[8], p_c[9], p_c[10], p_c[11], p_c[12], p_c[13], p_c[14], p_c[15]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002664 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: c = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002665 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2666 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
2667 p_r[8], p_r[9], p_r[10], p_r[11], p_r[12], p_r[13], p_r[14], p_r[15]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002668 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: r = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002669 bdcpy(bt_bd_addr.address, bd_addr);
2670 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2671 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2672 result = TRUE;
2673 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002674 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob result=%d",result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002675 return result;
2676}
2677#endif /* BTIF_DM_OOB_TEST */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002678#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2679
2680static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2681{
2682 bt_bdaddr_t bd_addr;
2683 bt_bdname_t bd_name;
2684 UINT32 cod;
2685
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002686 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002687
2688 /* Remote name update */
2689 btif_update_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2690 NULL, BT_DEVICE_TYPE_BLE);
2691 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2692 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2693
2694 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2695 pairing_cb.is_ssp = FALSE;
2696 cod = COD_UNCLASSIFIED;
2697
2698 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2699 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2700 p_ssp_key_notif->passkey);
2701}
2702
2703/*******************************************************************************
2704**
2705** Function btif_dm_ble_auth_cmpl_evt
2706**
2707** Description Executes authentication complete event in btif context
2708**
2709** Returns void
2710**
2711*******************************************************************************/
2712static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2713{
2714 /* Save link key, if not temporary */
2715 bt_bdaddr_t bd_addr;
2716 bt_status_t status = BT_STATUS_FAIL;
2717 bt_bond_state_t state = BT_BOND_STATE_NONE;
2718
2719 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2720 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2721 {
2722 /* store keys */
2723 }
2724 if (p_auth_cmpl->success)
2725 {
2726 status = BT_STATUS_SUCCESS;
2727 state = BT_BOND_STATE_BONDED;
2728
2729 btif_dm_save_ble_bonding_keys();
2730 BTA_GATTC_Refresh(bd_addr.address);
2731 btif_dm_get_remote_services(&bd_addr);
2732 }
2733 else
2734 {
2735 /*Map the HCI fail reason to bt status */
2736 switch (p_auth_cmpl->fail_reason)
2737 {
Priti Aghera156c52b2014-07-09 14:58:19 -07002738 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
2739 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
2740 btif_dm_remove_ble_bonding_keys();
2741 status = BT_STATUS_AUTH_FAILURE;
2742 break;
2743 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
2744 status = BT_STATUS_AUTH_REJECTED;
2745 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002746 default:
Andre Eisenbachca22ac42013-02-13 17:02:11 +09002747 btif_dm_remove_ble_bonding_keys();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002748 status = BT_STATUS_FAIL;
2749 break;
2750 }
2751 }
2752 bond_state_changed(status, &bd_addr, state);
2753}
2754
2755
2756
2757void btif_dm_load_ble_local_keys(void)
2758{
2759 bt_status_t bt_status;
2760
2761 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2762
2763 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2764 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2765 {
2766 ble_local_key_cb.is_er_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002767 BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002768 }
2769
2770 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
2771 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
2772 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
2773 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
2774 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
2775 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
2776 {
2777 ble_local_key_cb.is_id_keys_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002778 BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002779 }
2780
2781}
2782void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
2783 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
2784{
2785 if (ble_local_key_cb.is_er_rcvd )
2786 {
2787 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2788 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2789 }
2790
2791 if (ble_local_key_cb.is_id_keys_rcvd)
2792 {
2793 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
2794 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
2795 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
2796 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
2797 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002798 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002799}
2800
2801void btif_dm_save_ble_bonding_keys(void)
2802{
2803
2804 bt_bdaddr_t bd_addr;
2805
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002806 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002807
2808 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2809
2810 if (pairing_cb.ble.is_penc_key_rcvd)
2811 {
2812 btif_storage_add_ble_bonding_key(&bd_addr,
2813 (char *) &pairing_cb.ble.penc_key,
2814 BTIF_DM_LE_KEY_PENC,
2815 sizeof(btif_dm_ble_penc_keys_t));
2816 }
2817
2818 if (pairing_cb.ble.is_pid_key_rcvd)
2819 {
2820 btif_storage_add_ble_bonding_key(&bd_addr,
2821 (char *) &pairing_cb.ble.pid_key[0],
2822 BTIF_DM_LE_KEY_PID,
2823 BT_OCTET16_LEN);
2824 }
2825
2826
2827 if (pairing_cb.ble.is_pcsrk_key_rcvd)
2828 {
2829 btif_storage_add_ble_bonding_key(&bd_addr,
2830 (char *) &pairing_cb.ble.pcsrk_key,
2831 BTIF_DM_LE_KEY_PCSRK,
2832 sizeof(btif_dm_ble_pcsrk_keys_t));
2833 }
2834
2835
2836 if (pairing_cb.ble.is_lenc_key_rcvd)
2837 {
2838 btif_storage_add_ble_bonding_key(&bd_addr,
2839 (char *) &pairing_cb.ble.lenc_key,
2840 BTIF_DM_LE_KEY_LENC,
2841 sizeof(btif_dm_ble_lenc_keys_t));
2842 }
2843
2844 if (pairing_cb.ble.is_lcsrk_key_rcvd)
2845 {
2846 btif_storage_add_ble_bonding_key(&bd_addr,
2847 (char *) &pairing_cb.ble.lcsrk_key,
2848 BTIF_DM_LE_KEY_LCSRK,
2849 sizeof(btif_dm_ble_lcsrk_keys_t));
2850 }
2851
2852}
2853
2854
2855void btif_dm_remove_ble_bonding_keys(void)
2856{
2857 bt_bdaddr_t bd_addr;
2858
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002859 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002860
2861 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2862 btif_storage_remove_ble_bonding_keys(&bd_addr);
2863}
2864
2865
2866/*******************************************************************************
2867**
2868** Function btif_dm_ble_sec_req_evt
2869**
2870** Description Eprocess security request event in btif context
2871**
2872** Returns void
2873**
2874*******************************************************************************/
2875void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
2876{
2877 bt_bdaddr_t bd_addr;
2878 bt_bdname_t bd_name;
2879 UINT32 cod;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002880 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002881
2882 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2883 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002884 BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002885 return;
2886 }
2887
2888 /* Remote name update */
2889 btif_update_remote_properties(p_ble_req->bd_addr,p_ble_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
2890
2891 bdcpy(bd_addr.address, p_ble_req->bd_addr);
2892 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
2893
2894 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2895
2896 pairing_cb.is_temp = FALSE;
2897 pairing_cb.is_le_only = TRUE;
2898 pairing_cb.is_ssp = TRUE;
2899
2900 cod = COD_UNCLASSIFIED;
2901
2902 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
2903 BT_SSP_VARIANT_CONSENT, 0);
2904}
2905
2906
2907
2908/*******************************************************************************
2909**
2910** Function btif_dm_ble_passkey_req_evt
2911**
2912** Description Executes pin request event in btif context
2913**
2914** Returns void
2915**
2916*******************************************************************************/
2917static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
2918{
2919 bt_bdaddr_t bd_addr;
2920 bt_bdname_t bd_name;
2921 UINT32 cod;
2922
2923 /* Remote name update */
2924 btif_update_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,NULL,BT_DEVICE_TYPE_BLE);
2925
2926 bdcpy(bd_addr.address, p_pin_req->bd_addr);
2927 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
2928
2929 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2930 pairing_cb.is_le_only = TRUE;
2931
2932 cod = COD_UNCLASSIFIED;
2933
2934 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
2935 &bd_addr, &bd_name, cod);
2936}
2937
2938
2939void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
2940 tBT_DEVICE_TYPE dev_type)
2941{
2942 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
2943}
2944
2945static void btif_dm_ble_tx_test_cback(void *p)
2946{
2947 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
2948 (char *)p, 1, NULL);
2949}
2950
2951static void btif_dm_ble_rx_test_cback(void *p)
2952{
2953 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
2954 (char *)p, 1, NULL);
2955}
2956
2957static void btif_dm_ble_test_end_cback(void *p)
2958{
2959 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
2960 (char *)p, 3, NULL);
2961}
2962/*******************************************************************************
2963**
2964** Function btif_le_test_mode
2965**
2966** Description Sends a HCI BLE Test command to the Controller
2967**
2968** Returns BT_STATUS_SUCCESS on success
2969**
2970*******************************************************************************/
2971bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
2972{
2973 switch (opcode) {
2974 case HCI_BLE_TRANSMITTER_TEST:
2975 if (len != 3) return BT_STATUS_PARM_INVALID;
2976 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
2977 break;
2978 case HCI_BLE_RECEIVER_TEST:
2979 if (len != 1) return BT_STATUS_PARM_INVALID;
2980 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
2981 break;
2982 case HCI_BLE_TEST_END:
2983 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
2984 break;
2985 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002986 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002987 return BT_STATUS_UNSUPPORTED;
2988 }
2989 return BT_STATUS_SUCCESS;
2990}
2991
2992#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002993
2994void btif_dm_on_disable()
2995{
2996 /* cancel any pending pairing requests */
2997 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2998 {
2999 bt_bdaddr_t bd_addr;
3000
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003001 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003002 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3003 btif_dm_cancel_bond(&bd_addr);
3004 }
3005}
Matthew Xie1e5109b2012-11-09 18:26:26 -08003006
Satya Callojie5ba8842014-07-03 17:18:02 -07003007/*******************************************************************************
3008**
3009** Function btif_dm_read_energy_info
3010**
3011** Description Reads the energy info from controller
3012**
3013** Returns void
3014**
3015*******************************************************************************/
3016void btif_dm_read_energy_info()
3017{
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003018#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07003019 BTA_DmBleGetEnergyInfo(bta_energy_info_cb);
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003020#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07003021}
3022
Matthew Xie1e5109b2012-11-09 18:26:26 -08003023static char* btif_get_default_local_name() {
3024 if (btif_default_local_name[0] == '\0')
3025 {
3026 int max_len = sizeof(btif_default_local_name) - 1;
3027 if (BTM_DEF_LOCAL_NAME[0] != '\0')
3028 {
3029 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
3030 }
3031 else
3032 {
3033 char prop_model[PROPERTY_VALUE_MAX];
3034 property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
3035 strncpy(btif_default_local_name, prop_model, max_len);
3036 }
3037 btif_default_local_name[max_len] = '\0';
3038 }
3039 return btif_default_local_name;
3040}