blob: 268190439813c2a6158df00623157b53924f73b1 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
Hemant Gupta94c15ec2013-11-11 12:33:44 +05303 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
4 * Not a Contribution.
The Android Open Source Project5738f832012-12-12 16:00:35 -08005 * Copyright (C) 2009-2012 Broadcom Corporation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at:
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 ******************************************************************************/
20
21/************************************************************************************
22 *
23 * Filename: btif_dm.c
24 *
25 * Description: Contains Device Management (DM) related functionality
26 *
27 *
28 ***********************************************************************************/
Sharvil Nanavati44802762014-12-23 23:08:58 -080029
Chris Mantonf8027002015-03-12 09:22:48 -070030#define LOG_TAG "bt_btif_dm"
Sharvil Nanavati44802762014-12-23 23:08:58 -080031
Andre Eisenbach8a057242015-04-29 22:27:15 -070032#include <assert.h>
Scott James Remnant933926c2015-04-02 15:22:14 -070033#include <signal.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080034#include <stdio.h>
35#include <stdlib.h>
Ian Coolidgec7503db2015-01-24 02:01:26 -080036#include <string.h>
Scott James Remnant933926c2015-04-02 15:22:14 -070037#include <sys/types.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080038#include <unistd.h>
39
40#include <hardware/bluetooth.h>
41
The Android Open Source Project5738f832012-12-12 16:00:35 -080042#include <cutils/properties.h>
43#include "gki.h"
44#include "btu.h"
Sharvil Nanavati95b74f22015-03-12 15:55:21 -070045#include "btcore/include/bdaddr.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080046#include "bta_api.h"
47#include "btif_api.h"
48#include "btif_util.h"
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080049#include "btif_dm.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080050#include "btif_storage.h"
51#include "btif_hh.h"
Hemant Gupta94c15ec2013-11-11 12:33:44 +053052#include "btif_hd.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080053#include "btif_config.h"
Kim Schulz8372aa52015-03-25 10:39:40 +010054#include "btif_sdp.h"
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080055#include "bta_gatt_api.h"
Andre Eisenbach27c4e632015-07-06 15:43:15 -070056#include "device/include/interop.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080057#include "include/stack_config.h"
58#include "osi/include/log.h"
Andre Eisenbach8a057242015-04-29 22:27:15 -070059#include "osi/include/allocator.h"
Pavlin Radoslavov65f9c042015-07-16 17:40:28 -070060#include "stack/btm/btm_int.h"
Andre Eisenbach31a64002014-10-14 14:29:19 -070061
62/******************************************************************************
The Android Open Source Project5738f832012-12-12 16:00:35 -080063** Constants & Macros
64******************************************************************************/
65
66#define COD_UNCLASSIFIED ((0x1F) << 8)
Priti Agheraebb1d752012-11-27 18:03:22 -080067#define COD_HID_KEYBOARD 0x0540
68#define COD_HID_POINTING 0x0580
69#define COD_HID_COMBO 0x05C0
70#define COD_HID_MAJOR 0x0500
71#define COD_AV_HEADSETS 0x0404
72#define COD_AV_HANDSFREE 0x0408
73#define COD_AV_HEADPHONES 0x0418
74#define COD_AV_PORTABLE_AUDIO 0x041C
75#define COD_AV_HIFI_AUDIO 0x0428
The Android Open Source Project5738f832012-12-12 16:00:35 -080076
77
78#define BTIF_DM_DEFAULT_INQ_MAX_RESULTS 0
79#define BTIF_DM_DEFAULT_INQ_MAX_DURATION 10
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -070080#define BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING 2
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080081
Andre Eisenbach31a64002014-10-14 14:29:19 -070082#define NUM_TIMEOUT_RETRIES 5
83
Matthew Xie1e5109b2012-11-09 18:26:26 -080084#define PROPERTY_PRODUCT_MODEL "ro.product.model"
Matthew Xiea30d95a2013-09-18 12:30:36 -070085#define DEFAULT_LOCAL_NAME_MAX 31
Matthew Xie1e5109b2012-11-09 18:26:26 -080086#if (DEFAULT_LOCAL_NAME_MAX > BTM_MAX_LOC_BD_NAME_LEN)
87 #error "default btif local name size exceeds stack supported length"
88#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080089
Matthew Xie7f3e4292013-09-30 12:44:10 -070090#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
91#define BTIF_DM_INTERLEAVE_DURATION_BR_ONE 2
92#define BTIF_DM_INTERLEAVE_DURATION_LE_ONE 2
93#define BTIF_DM_INTERLEAVE_DURATION_BR_TWO 3
94#define BTIF_DM_INTERLEAVE_DURATION_LE_TWO 4
95#endif
96
Priti Agherac0edf9f2014-06-26 11:23:51 -070097#define MAX_SDP_BL_ENTRIES 3
98
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -080099#define ENCRYPTED_BREDR 2
100#define ENCRYPTED_LE 4
101
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800102typedef struct
103{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800104 bt_bond_state_t state;
Andre Eisenbach12871662015-05-08 17:42:10 -0700105 bt_bdaddr_t static_bdaddr;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800106 BD_ADDR bd_addr;
Pavlin Radoslavov65f9c042015-07-16 17:40:28 -0700107 tBTM_BOND_TYPE bond_type;
Chaojing Sune2805532015-04-22 13:40:21 -0700108 UINT8 pin_code_len;
109 UINT8 is_ssp;
110 UINT8 auth_req;
111 UINT8 io_cap;
112 UINT8 autopair_attempts;
113 UINT8 timeout_retries;
114 UINT8 is_local_initiated;
115 UINT8 sdp_attempts;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800116#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Chaojing Sune2805532015-04-22 13:40:21 -0700117 BOOLEAN is_le_only;
118 BOOLEAN is_le_nc; /* LE Numeric comparison */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800119 btif_dm_ble_cb_t ble;
120#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800121} btif_dm_pairing_cb_t;
122
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800123
124typedef struct
125{
126 UINT8 ir[BT_OCTET16_LEN];
127 UINT8 irk[BT_OCTET16_LEN];
128 UINT8 dhk[BT_OCTET16_LEN];
129}btif_dm_local_key_id_t;
130
131typedef struct
132{
133 BOOLEAN is_er_rcvd;
134 UINT8 er[BT_OCTET16_LEN];
135 BOOLEAN is_id_keys_rcvd;
136 btif_dm_local_key_id_t id_keys; /* ID kyes */
137
138}btif_dm_local_key_cb_t;
139
140typedef struct
141{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800142 BD_ADDR bd_addr;
143 BD_NAME bd_name;
144} btif_dm_remote_name_t;
145
146typedef struct
147{
148 BT_OCTET16 sp_c;
149 BT_OCTET16 sp_r;
150 BD_ADDR oob_bdaddr; /* peer bdaddr*/
151} btif_dm_oob_cb_t;
Satya Callojie5ba8842014-07-03 17:18:02 -0700152
153typedef struct
154{
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700155 bt_bdaddr_t bdaddr;
156 UINT8 transport; /* 0=Unknown, 1=BR/EDR, 2=LE */
157} btif_dm_create_bond_cb_t;
158
159typedef struct
160{
Satya Callojie5ba8842014-07-03 17:18:02 -0700161 uint8_t status;
162 uint8_t ctrl_state;
163 uint64_t tx_time;
164 uint64_t rx_time;
165 uint64_t idle_time;
166 uint64_t energy_used;
167} btif_activity_energy_info_cb_t;
168
Nitin Srivastavab30e7122014-08-25 18:17:48 +0530169typedef struct
170{
171 unsigned int manufact_id;
172}skip_sdp_entry_t;
173
174
The Android Open Source Project5738f832012-12-12 16:00:35 -0800175#define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id))
176
Priti Agherac0edf9f2014-06-26 11:23:51 -0700177#define UUID_HUMAN_INTERFACE_DEVICE "00001124-0000-1000-8000-00805f9b34fb"
178
The Android Open Source Project5738f832012-12-12 16:00:35 -0800179/* This flag will be true if HCI_Inquiry is in progress */
180static BOOLEAN btif_dm_inquiry_in_progress = FALSE;
181
Matthew Xie1e5109b2012-11-09 18:26:26 -0800182/************************************************************************************
183** Static variables
184************************************************************************************/
185static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'};
186
The Android Open Source Project5738f832012-12-12 16:00:35 -0800187/******************************************************************************
188** Static functions
189******************************************************************************/
190static btif_dm_pairing_cb_t pairing_cb;
191static btif_dm_oob_cb_t oob_cb;
Nitin Srivastavab30e7122014-08-25 18:17:48 +0530192static UINT16 num_active_br_edr_links;
193static UINT16 num_active_le_links;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800194static void btif_dm_generic_evt(UINT16 event, char* p_param);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700195static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800196static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name);
197static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
198 DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800199#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
200static btif_dm_local_key_cb_t ble_local_key_cb;
201static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif);
202static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl);
203static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req);
Satya Calloji444a8da2015-03-06 10:38:22 -0800204static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF *p_notif_req) ;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800205#endif
Satya Calloji6e2d9db2014-07-08 16:18:58 -0700206
207static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
208 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
209 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status);
210
Matthew Xie1e5109b2012-11-09 18:26:26 -0800211static char* btif_get_default_local_name();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800212/******************************************************************************
213** Externs
214******************************************************************************/
215extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
216extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
217extern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
Rakesh Iyer9c8dfac2015-04-08 12:25:37 -0700218extern bt_status_t btif_av_sink_execute_service(BOOLEAN b_enable);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800219extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
Hemant Gupta10256872013-08-19 18:33:01 +0530220extern bt_status_t btif_hf_client_execute_service(BOOLEAN b_enable);
Kim Schulz8372aa52015-03-25 10:39:40 +0100221extern bt_status_t btif_sdp_execute_service(BOOLEAN b_enable);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800222extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
Hemant Gupta94c15ec2013-11-11 12:33:44 +0530223extern bt_status_t btif_hd_execute_service(BOOLEAN b_enable);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700224extern void bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16);
Anubhav Gupta791692a2013-12-06 17:11:11 +0530225extern void btif_av_move_idle(bt_bdaddr_t bd_addr);
Nitin Srivastavab30e7122014-08-25 18:17:48 +0530226extern void btif_av_trigger_suspend();
227extern BOOLEAN btif_av_get_ongoing_multicast();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800228
229/******************************************************************************
230** Functions
231******************************************************************************/
232
Andre Eisenbach8a057242015-04-29 22:27:15 -0700233static void btif_dm_data_copy(uint16_t event, char *dst, char *src)
234{
235 tBTA_DM_SEC *dst_dm_sec = (tBTA_DM_SEC*)dst;
236 tBTA_DM_SEC *src_dm_sec = (tBTA_DM_SEC*)src;
237
238 if (!src_dm_sec)
239 return;
240
241 assert(dst_dm_sec);
242 memcpy(dst_dm_sec, src_dm_sec, sizeof(tBTA_DM_SEC));
243
244 if (event == BTA_DM_BLE_KEY_EVT)
245 {
246 dst_dm_sec->ble_key.p_key_value = osi_malloc(sizeof(tBTM_LE_KEY_VALUE));
247 assert(src_dm_sec->ble_key.p_key_value);
248 assert(dst_dm_sec->ble_key.p_key_value);
249 memcpy(dst_dm_sec->ble_key.p_key_value, src_dm_sec->ble_key.p_key_value, sizeof(tBTM_LE_KEY_VALUE));
250 }
251}
252
253static void btif_dm_data_free(uint16_t event, tBTA_DM_SEC *dm_sec)
254{
255 if (event == BTA_DM_BLE_KEY_EVT)
256 osi_free(dm_sec->ble_key.p_key_value);
257}
258
The Android Open Source Project5738f832012-12-12 16:00:35 -0800259bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
260 BOOLEAN b_enable)
261{
Kim Schulz8372aa52015-03-25 10:39:40 +0100262 BTIF_TRACE_DEBUG("%s service_id: %d", __FUNCTION__, service_id);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800263 /* Check the service_ID and invoke the profile's BT state changed API */
264 switch (service_id)
265 {
266 case BTA_HFP_SERVICE_ID:
267 case BTA_HSP_SERVICE_ID:
268 {
269 btif_hf_execute_service(b_enable);
270 }break;
Sharvil Nanavati9609cee2014-10-15 18:30:49 -0700271 case BTA_A2DP_SOURCE_SERVICE_ID:
The Android Open Source Project5738f832012-12-12 16:00:35 -0800272 {
273 btif_av_execute_service(b_enable);
274 }break;
Rakesh Iyer9c8dfac2015-04-08 12:25:37 -0700275 case BTA_A2DP_SINK_SERVICE_ID:
276 {
277 btif_av_sink_execute_service(b_enable);
278 }break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800279 case BTA_HID_SERVICE_ID:
280 {
281 btif_hh_execute_service(b_enable);
282 }break;
Hemant Gupta10256872013-08-19 18:33:01 +0530283 case BTA_HFP_HS_SERVICE_ID:
284 {
285 btif_hf_client_execute_service(b_enable);
286 }break;
Kim Schulz8372aa52015-03-25 10:39:40 +0100287 case BTA_SDP_SERVICE_ID:
Hemant Gupta2dc99992014-04-18 12:54:08 +0530288 {
Kim Schulz8372aa52015-03-25 10:39:40 +0100289 btif_sdp_execute_service(b_enable);
Hemant Gupta2dc99992014-04-18 12:54:08 +0530290 }break;
Hemant Gupta94c15ec2013-11-11 12:33:44 +0530291 case BTA_HIDD_SERVICE_ID:
292 {
293 btif_hd_execute_service(b_enable);
294 }break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800295 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700296 BTIF_TRACE_ERROR("%s: Unknown service being enabled", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800297 return BT_STATUS_FAIL;
298 }
299 return BT_STATUS_SUCCESS;
300}
301
302/*******************************************************************************
303**
304** Function check_eir_remote_name
305**
306** Description Check if remote name is in the EIR data
307**
308** Returns TRUE if remote name found
309** Populate p_remote_name, if provided and remote name found
310**
311*******************************************************************************/
312static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
313 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
314{
315 UINT8 *p_eir_remote_name = NULL;
316 UINT8 remote_name_len = 0;
317
318 /* Check EIR for remote name and services */
319 if (p_search_data->inq_res.p_eir)
320 {
Zach Johnsona50fc882014-10-30 21:02:58 -0700321 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800322 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
323 if (!p_eir_remote_name)
324 {
Zach Johnsona50fc882014-10-30 21:02:58 -0700325 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
The Android Open Source Project5738f832012-12-12 16:00:35 -0800326 BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
327 }
328
329 if (p_eir_remote_name)
330 {
331 if (remote_name_len > BD_NAME_LEN)
332 remote_name_len = BD_NAME_LEN;
333
334 if (p_remote_name && p_remote_name_len)
335 {
336 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
337 *(p_remote_name + remote_name_len) = 0;
338 *p_remote_name_len = remote_name_len;
339 }
340
341 return TRUE;
342 }
343 }
344
345 return FALSE;
346
347}
348
349/*******************************************************************************
350**
351** Function check_cached_remote_name
352**
353** Description Check if remote name is in the NVRAM cache
354**
355** Returns TRUE if remote name found
356** Populate p_remote_name, if provided and remote name found
357**
358*******************************************************************************/
359static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
360 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
361{
362 bt_bdname_t bdname;
363 bt_bdaddr_t remote_bdaddr;
364 bt_property_t prop_name;
365
366 /* check if we already have it in our btif_storage cache */
367 bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
368 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
369 sizeof(bt_bdname_t), &bdname);
370 if (btif_storage_get_remote_device_property(
371 &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
372 {
373 if (p_remote_name && p_remote_name_len)
374 {
375 strcpy((char *)p_remote_name, (char *)bdname.name);
376 *p_remote_name_len = strlen((char *)p_remote_name);
377 }
378 return TRUE;
379 }
380
381 return FALSE;
382}
383
384BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
385{
386 uint32_t remote_cod;
387 bt_property_t prop_name;
388
389 /* check if we already have it in our btif_storage cache */
390 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
391 sizeof(uint32_t), &remote_cod);
392 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
393 {
Chris Mantonf8027002015-03-12 09:22:48 -0700394 LOG_INFO("%s remote_cod = 0x%08x cod = 0x%08x", __func__, remote_cod, cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800395 if ((remote_cod & 0x7ff) == cod)
396 return TRUE;
397 }
398
399 return FALSE;
400}
401
Priti Agheraebb1d752012-11-27 18:03:22 -0800402BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
403{
404 uint32_t remote_cod;
405 bt_property_t prop_name;
406
407 /* check if we already have it in our btif_storage cache */
408 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
409 sizeof(uint32_t), &remote_cod);
410 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
411 &prop_name) == BT_STATUS_SUCCESS)
412 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700413 BTIF_TRACE_DEBUG("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
Hemant Guptae4def912014-09-18 14:19:57 +0530414 if ((remote_cod & 0x700) == cod) {
415 BTIF_TRACE_DEBUG("%s: returning TRUE", __FUNCTION__);
Priti Agheraebb1d752012-11-27 18:03:22 -0800416 return TRUE;
Hemant Guptae4def912014-09-18 14:19:57 +0530417 }
Priti Agheraebb1d752012-11-27 18:03:22 -0800418 }
Hemant Guptae4def912014-09-18 14:19:57 +0530419 BTIF_TRACE_DEBUG("%s: returning FALSE", __FUNCTION__);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700420 return FALSE;
421}
Priti Agheraebb1d752012-11-27 18:03:22 -0800422
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700423BOOLEAN check_hid_le(const bt_bdaddr_t *remote_bdaddr)
424{
425 uint32_t remote_dev_type;
426 bt_property_t prop_name;
427
428 /* check if we already have it in our btif_storage cache */
429 BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE,
430 sizeof(uint32_t), &remote_dev_type);
431 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
432 &prop_name) == BT_STATUS_SUCCESS)
433 {
434 if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE)
435 {
436 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -0700437 bdaddr_to_string(remote_bdaddr, bdstr, sizeof(bdstr));
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700438 if(btif_config_exist(bdstr, "HidAppId"))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700439 return TRUE;
440 }
441 }
Priti Agheraebb1d752012-11-27 18:03:22 -0800442 return FALSE;
443}
444
Priti Agherac0edf9f2014-06-26 11:23:51 -0700445/*****************************************************************************
446**
Hemant Guptada33b462015-04-26 10:32:57 +0530447** Function check_if_auth_bl
448**
449** Description Checks if a given device is blacklisted to skip authentication
450**
451** Parameters remote_bdaddr
452**
453** Returns TRUE if the device is present in blacklist, else FALSE
454**
455*******************************************************************************/
456static bool check_if_auth_bl(bt_bdaddr_t * remote_bdaddr)
457{
458 if (remote_bdaddr == NULL) {
459 LOG_WARN("%s: remote_bdaddr = NULL, returning false", __func__);
460 return FALSE;
461 }
462
463 bt_property_t prop_name;
464 bt_bdname_t bdname;
465
466 if (interop_addr_match(INTEROP_DISABLE_AUTH_FOR_HID_POINTING, remote_bdaddr))
467 return TRUE;
468
469 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
470 sizeof(bt_bdname_t), &bdname);
471 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
472 &prop_name) != BT_STATUS_SUCCESS)
473 {
474 LOG_WARN("%s: BT_PROPERTY_BDNAME failed, returning false", __func__);
475 return FALSE;
476 }
477
478 if (bdname.name != NULL &&
479 interop_name_match(INTEROP_DISABLE_SDP_AFTER_PAIRING, (const char *)bdname.name))
480 return TRUE;
481
482 char bdstr[20] = {0};
483 LOG_DEBUG("%s: %s is not in blacklist for skipping auth", __func__,
484 bdaddr_to_string(remote_bdaddr, bdstr, sizeof(bdstr)));
485 return FALSE;
486}
487
488/*****************************************************************************
489**
Priti Agherac0edf9f2014-06-26 11:23:51 -0700490** Function check_sdp_bl
491**
492** Description Checks if a given device is blacklisted to skip sdp
493**
Hemant Guptada33b462015-04-26 10:32:57 +0530494** Parameters remote_bdaddr
Priti Agherac0edf9f2014-06-26 11:23:51 -0700495**
496** Returns TRUE if the device is present in blacklist, else FALSE
497**
498*******************************************************************************/
499BOOLEAN check_sdp_bl(const bt_bdaddr_t *remote_bdaddr)
500{
Hemant Guptada33b462015-04-26 10:32:57 +0530501 if (remote_bdaddr == NULL) {
502 LOG_WARN("%s: remote_bdaddr = NULL, returning false", __func__);
503 return FALSE;
504 }
505
506 if (interop_addr_match(INTEROP_DISABLE_SDP_AFTER_PAIRING, remote_bdaddr)) {
507 LOG_WARN("%s: device is in blacklist for skipping sdp", __func__);
508 return TRUE;
509 }
510
511 bt_property_t prop_name;
512 bt_bdname_t bdname;
513 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
514 sizeof(bt_bdname_t), &bdname);
515 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
516 &prop_name) != BT_STATUS_SUCCESS)
517 {
518 LOG_WARN("%s: BT_PROPERTY_BDNAME failed, returning false", __func__);
519 return FALSE;
520 }
521
522 if (bdname.name != NULL &&
523 interop_name_match(INTEROP_DISABLE_SDP_AFTER_PAIRING, (const char *)bdname.name))
524 return TRUE;
525
Priti Agherac0edf9f2014-06-26 11:23:51 -0700526 UINT16 manufacturer = 0;
527 UINT8 lmp_ver = 0;
528 UINT16 lmp_subver = 0;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700529 bt_remote_version_t info;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700530
Hemant Guptada33b462015-04-26 10:32:57 +0530531 /* fetch additional info about remote device used in iop query */
Sharvil Nanavatif1c764f2015-02-23 17:31:48 -0800532 BTM_ReadRemoteVersion(*(BD_ADDR*)remote_bdaddr, &lmp_ver,
Priti Agherac0edf9f2014-06-26 11:23:51 -0700533 &manufacturer, &lmp_subver);
534
Hemant Guptada33b462015-04-26 10:32:57 +0530535 /* if not available yet, try fetching from config database */
Priti Agherac0edf9f2014-06-26 11:23:51 -0700536 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO,
537 sizeof(bt_remote_version_t), &info);
538
539 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
540 &prop_name) != BT_STATUS_SUCCESS)
541 {
542
Hemant Guptada33b462015-04-26 10:32:57 +0530543 APPL_TRACE_WARNING("%s: BT_PROPERTY_REMOTE_VERSION_INFO failed, returning false", __func__);
Priti Agherac0edf9f2014-06-26 11:23:51 -0700544 return FALSE;
545 }
546 manufacturer = info.manufacturer;
547
Hemant Guptada33b462015-04-26 10:32:57 +0530548 if (manufacturer != 0 &&
549 interop_manufacturer_match(INTEROP_DISABLE_SDP_AFTER_PAIRING, manufacturer))
550 return TRUE;
551
552 char bdstr[20] = {0};
553 LOG_DEBUG("%s: %s is not in blacklist for skipping sdp", __func__,
554 bdaddr_to_string(remote_bdaddr, bdstr, sizeof(bdstr)));
Priti Agherac0edf9f2014-06-26 11:23:51 -0700555 return FALSE;
556}
557
The Android Open Source Project5738f832012-12-12 16:00:35 -0800558static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
559{
Chaojing Sune2805532015-04-22 13:40:21 -0700560 // Send bonding state only once - based on outgoing/incoming we may receive duplicates
561 if ((pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING))
562 {
563 // Cross key pairing so send callback for static address
Andre Eisenbache87ef6d2015-05-01 13:14:59 -0700564 if (!bdaddr_is_empty(&pairing_cb.static_bdaddr))
565 {
Chaojing Sune2805532015-04-22 13:40:21 -0700566 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
Andre Eisenbache87ef6d2015-05-01 13:14:59 -0700567 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800568 return;
Chaojing Sune2805532015-04-22 13:40:21 -0700569 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800570
Andre Eisenbach89363762015-01-26 13:49:36 -0800571 if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
Chaojing Sune2805532015-04-22 13:40:21 -0700572 state = BT_BOND_STATE_NONE;
573
574 BTIF_TRACE_DEBUG("%s: state=%d, prev_state=%d, sdp_attempts = %d", __func__,
575 state, pairing_cb.state, pairing_cb.sdp_attempts);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800576
577 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
578
579 if (state == BT_BOND_STATE_BONDING)
580 {
581 pairing_cb.state = state;
582 bdcpy(pairing_cb.bd_addr, bd_addr->address);
Chaojing Sune2805532015-04-22 13:40:21 -0700583 } else {
584 if (!pairing_cb.sdp_attempts)
585 memset(&pairing_cb, 0, sizeof(pairing_cb));
586 else
587 BTIF_TRACE_DEBUG("%s: BR-EDR service discovery active", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800588 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800589}
590
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800591/* store remote version in bt config to always have access
592 to it post pairing*/
593static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
594{
595 bt_property_t property;
596 UINT8 lmp_ver = 0;
597 UINT16 lmp_subver = 0;
598 UINT16 mfct_set = 0;
599 tBTM_STATUS btm_status;
600 bt_remote_version_t info;
601 bt_status_t status;
602 bdstr_t bdstr;
603
604 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
605 &mfct_set, &lmp_subver);
606
Sharvil Nanavati44802762014-12-23 23:08:58 -0800607 LOG_DEBUG("remote version info [%s]: %x, %x, %x", bdaddr_to_string(p_bd, bdstr, sizeof(bdstr)),
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800608 lmp_ver, mfct_set, lmp_subver);
609
610 if (btm_status == BTM_SUCCESS)
611 {
Chaojing Sune2805532015-04-22 13:40:21 -0700612 // Always update cache to ensure we have availability whenever BTM API is not populated
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800613 info.manufacturer = mfct_set;
614 info.sub_ver = lmp_subver;
615 info.version = lmp_ver;
616 BTIF_STORAGE_FILL_PROPERTY(&property,
617 BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
618 &info);
619 status = btif_storage_set_remote_device_property(p_bd, &property);
620 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
621 }
622}
623
The Android Open Source Project5738f832012-12-12 16:00:35 -0800624
625static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
626 DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
627{
628 int num_properties = 0;
629 bt_property_t properties[3];
630 bt_bdaddr_t bdaddr;
631 bt_status_t status;
632 UINT32 cod;
633 bt_device_type_t dev_type;
634
635 memset(properties, 0, sizeof(properties));
636 bdcpy(bdaddr.address, bd_addr);
637
638 /* remote name */
639 if (strlen((const char *) bd_name))
640 {
641 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
642 BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
643 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
644 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
645 num_properties++;
646 }
647
648 /* class of device */
649 cod = devclass2uint(dev_class);
Chris Mantonf8027002015-03-12 09:22:48 -0700650 BTIF_TRACE_DEBUG("%s cod is 0x%06x", __func__, cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800651 if ( cod == 0) {
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530652 /* Try to retrieve cod from storage */
Chris Mantonf8027002015-03-12 09:22:48 -0700653 BTIF_TRACE_DEBUG("%s cod is 0, checking cod from storage", __func__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530654 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
655 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
656 status = btif_storage_get_remote_device_property(&bdaddr, &properties[num_properties]);
Chris Mantonf8027002015-03-12 09:22:48 -0700657 BTIF_TRACE_DEBUG("%s cod retrieved from storage is 0x%06x", __func__, cod);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530658 if ( cod == 0) {
Chris Mantonf8027002015-03-12 09:22:48 -0700659 BTIF_TRACE_DEBUG("%s cod is again 0, set as unclassified", __func__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530660 cod = COD_UNCLASSIFIED;
661 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800662 }
663
664 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
665 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
666 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
667 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
668 num_properties++;
669
670 /* device type */
Chaojing Sune2805532015-04-22 13:40:21 -0700671 bt_property_t prop_name;
672 uint8_t remote_dev_type;
673 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_TYPE_OF_DEVICE,
674 sizeof(uint8_t), &remote_dev_type);
675 if (btif_storage_get_remote_device_property(&bdaddr, &prop_name) == BT_STATUS_SUCCESS)
676 dev_type = remote_dev_type | device_type;
677 else
678 dev_type = device_type;
679
The Android Open Source Project5738f832012-12-12 16:00:35 -0800680 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
681 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
682 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
683 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
684 num_properties++;
685
686 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
687 status, &bdaddr, num_properties, properties);
688}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800689
690/*******************************************************************************
691**
692** Function btif_dm_cb_hid_remote_name
693**
694** Description Remote name callback for HID device. Called in btif context
695** Special handling for HID devices
696**
697** Returns void
698**
699*******************************************************************************/
700static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
701{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700702 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 -0800703 if (pairing_cb.state == BT_BOND_STATE_BONDING)
704 {
705 bt_bdaddr_t remote_bd;
706
707 bdcpy(remote_bd.address, pairing_cb.bd_addr);
708
709 if (p_remote_name->status == BTM_SUCCESS)
710 {
711 bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
712 }
713 else
714 bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
715 }
716}
717
The Android Open Source Project5738f832012-12-12 16:00:35 -0800718/*******************************************************************************
719**
720** Function btif_dm_cb_create_bond
721**
722** Description Create bond initiated from the BTIF thread context
723** Special handling for HID devices
724**
725** Returns void
726**
727*******************************************************************************/
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700728static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800729{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800730 BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800731 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800732
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800733#if BLE_INCLUDED == TRUE
734 int device_type;
735 int addr_type;
736 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -0700737 bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr));
Matthew Xie64c54792014-09-16 00:55:03 -0700738 if (transport == BT_TRANSPORT_LE)
739 {
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700740 if (!btif_config_get_int((char const *)&bdstr,"DevType", &device_type))
Matthew Xie64c54792014-09-16 00:55:03 -0700741 {
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700742 btif_config_set_int(bdstr, "DevType", BT_DEVICE_TYPE_BLE);
Matthew Xie64c54792014-09-16 00:55:03 -0700743 }
744 if (btif_storage_get_remote_addr_type(bd_addr, &addr_type) != BT_STATUS_SUCCESS)
745 {
746 btif_storage_set_remote_addr_type(bd_addr, BLE_ADDR_PUBLIC);
747 }
748 }
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700749 if((btif_config_get_int((char const *)&bdstr,"DevType", &device_type) &&
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800750 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
Chaojing Sune2805532015-04-22 13:40:21 -0700751 (device_type & BT_DEVICE_TYPE_BLE) == BT_DEVICE_TYPE_BLE) || (transport == BT_TRANSPORT_LE))
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800752 {
Chaojing Sune2805532015-04-22 13:40:21 -0700753 BTA_DmAddBleDevice(bd_addr->address, addr_type, device_type);
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800754 }
755#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800756
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800757#if BLE_INCLUDED == TRUE
Chaojing Sune2805532015-04-22 13:40:21 -0700758 if(is_hid && (device_type & BT_DEVICE_TYPE_BLE) == 0)
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800759#else
760 if(is_hid)
761#endif
762 {
763 int status;
764 status = btif_hh_connect(bd_addr);
765 if(status != BT_STATUS_SUCCESS)
766 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800767 }
768 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800769 {
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700770 BTA_DmBondByTransport((UINT8 *)bd_addr->address, transport);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800771 }
772 /* Track originator of bond creation */
773 pairing_cb.is_local_initiated = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800774
775}
776
777/*******************************************************************************
778**
779** Function btif_dm_cb_remove_bond
780**
781** Description remove bond initiated from the BTIF thread context
782** Special handling for HID devices
783**
784** Returns void
785**
786*******************************************************************************/
787void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
788{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800789 /*special handling for HID devices */
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700790 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there
791 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
792#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
793 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
794#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800795 {
Chaojing Sune2805532015-04-22 13:40:21 -0700796 BTIF_TRACE_DEBUG("%s: Removing HH device", __func__);
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700797 BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800798 }
799}
800
801/*******************************************************************************
802**
Andre Eisenbach249f6002014-06-18 12:20:37 -0700803** Function btif_dm_get_connection_state
804**
805** Description Returns whether the remote device is currently connected
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800806** and whether encryption is active for the connection
Andre Eisenbach249f6002014-06-18 12:20:37 -0700807**
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800808** Returns 0 if not connected; 1 if connected and > 1 if connection is
809** encrypted
Andre Eisenbach249f6002014-06-18 12:20:37 -0700810**
811*******************************************************************************/
812uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr)
813{
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800814 uint8_t *bda = (uint8_t*)bd_addr->address;
815 uint16_t rc = BTA_DmGetConnectionState(bda);
816
817 if (rc != 0)
818 {
819 uint8_t flags = 0;
820
821 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_BR_EDR);
822 BTIF_TRACE_DEBUG("%s: security flags (BR/EDR)=0x%02x", __FUNCTION__, flags);
823 if (flags & BTM_SEC_FLAG_ENCRYPTED)
824 rc |= ENCRYPTED_BREDR;
825
826 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_LE);
827 BTIF_TRACE_DEBUG("%s: security flags (LE)=0x%02x", __FUNCTION__, flags);
828 if (flags & BTM_SEC_FLAG_ENCRYPTED)
829 rc |= ENCRYPTED_LE;
830 }
831
832 return rc;
Andre Eisenbach249f6002014-06-18 12:20:37 -0700833}
834
835/*******************************************************************************
836**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800837** Function search_devices_copy_cb
838**
839** Description Deep copy callback for search devices event
840**
841** Returns void
842**
843*******************************************************************************/
844static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
845{
846 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
847 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
848
849 if (!p_src)
850 return;
851
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700852 BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800853 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
854 switch (event)
855 {
856 case BTA_DM_INQ_RES_EVT:
857 {
858 if (p_src_data->inq_res.p_eir)
859 {
860 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
861 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
862 }
863 }
864 break;
865
866 case BTA_DM_DISC_RES_EVT:
867 {
868 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
869 {
870 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
871 memcpy(p_dest_data->disc_res.p_raw_data,
872 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
873 }
874 }
875 break;
876 }
877}
878
879static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
880{
881 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
882 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
883
884 if (!p_src)
885 return;
886 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
887 switch (event)
888 {
889 case BTA_DM_DISC_RES_EVT:
890 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530891 if (p_src_data->disc_res.result == BTA_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800892 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530893 if (p_src_data->disc_res.num_uuids > 0)
894 {
895 p_dest_data->disc_res.p_uuid_list =
896 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
897 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
898 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
899 GKI_freebuf(p_src_data->disc_res.p_uuid_list);
900 }
901 if (p_src_data->disc_res.p_raw_data != NULL)
902 {
903 GKI_freebuf(p_src_data->disc_res.p_raw_data);
904 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800905 }
906 } break;
907 }
908}
909/******************************************************************************
910**
911** BTIF DM callback events
912**
913*****************************************************************************/
914
915/*******************************************************************************
916**
917** Function btif_dm_pin_req_evt
918**
919** Description Executes pin request event in btif context
920**
921** Returns void
922**
923*******************************************************************************/
924static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
925{
926 bt_bdaddr_t bd_addr;
927 bt_bdname_t bd_name;
928 UINT32 cod;
929 bt_pin_code_t pin_code;
Matthew Xie86f97ed2014-11-10 10:24:46 -0800930 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800931
932 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -0800933 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
934 {
935 dev_type = BT_DEVICE_TYPE_BREDR;
936 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800937 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -0800938 p_pin_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800939
940 bdcpy(bd_addr.address, p_pin_req->bd_addr);
941 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
942
943 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
944
945 cod = devclass2uint(p_pin_req->dev_class);
946
Chris Mantonf8027002015-03-12 09:22:48 -0700947 if (cod == 0) {
948 BTIF_TRACE_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800949 cod = COD_UNCLASSIFIED;
950 }
951
952 /* check for auto pair possiblity only if bond was initiated by local device */
Casper Bonde818d0f22015-05-21 11:08:45 +0200953 if (pairing_cb.is_local_initiated && (p_pin_req->min_16_digit == FALSE))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800954 {
Srinu Jellabf691e82014-11-10 18:30:26 +0530955 if (bdcmp(pairing_cb.bd_addr, bd_addr.address))
956 {
957 /* Pin code from different device reject it as we dont support more than 1 pairing */
958 BTIF_TRACE_DEBUG("%s()rejecting pairing request", __FUNCTION__);
959 BTA_DmPinReply( (UINT8*)bd_addr.address, FALSE, 0, NULL);
960 return;
961 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800962 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
963 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
964 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
965 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
966 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
967 check_cod(&bd_addr, COD_HID_POINTING))
968 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700969 BTIF_TRACE_DEBUG("%s()cod matches for auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800970 /* Check if this device can be auto paired */
971 if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) &&
972 (pairing_cb.autopair_attempts == 0))
973 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700974 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800975 pin_code.pin[0] = 0x30;
976 pin_code.pin[1] = 0x30;
977 pin_code.pin[2] = 0x30;
978 pin_code.pin[3] = 0x30;
979
980 pairing_cb.autopair_attempts++;
981 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
982 return;
983 }
984 }
985 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
986 check_cod(&bd_addr, COD_HID_COMBO))
987 {
988 if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) &&
989 (pairing_cb.autopair_attempts == 0))
990 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700991 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800992 pin_code.pin[0] = 0x30;
993 pin_code.pin[1] = 0x30;
994 pin_code.pin[2] = 0x30;
995 pin_code.pin[3] = 0x30;
996
997 pairing_cb.autopair_attempts++;
998 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
999 return;
1000 }
1001 }
1002 }
1003 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
Casper Bonde818d0f22015-05-21 11:08:45 +02001004 &bd_addr, &bd_name, cod, p_pin_req->min_16_digit);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001005}
1006
1007/*******************************************************************************
1008**
1009** Function btif_dm_ssp_cfm_req_evt
1010**
1011** Description Executes SSP confirm request event in btif context
1012**
1013** Returns void
1014**
1015*******************************************************************************/
1016static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
1017{
1018 bt_bdaddr_t bd_addr;
1019 bt_bdname_t bd_name;
1020 UINT32 cod;
1021 BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
Matthew Xie86f97ed2014-11-10 10:24:46 -08001022 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001023
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001024 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001025
1026 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08001027 if (!btif_get_device_type(p_ssp_cfm_req->bd_addr, &dev_type))
1028 {
1029 dev_type = BT_DEVICE_TYPE_BREDR;
1030 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001031 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -08001032 p_ssp_cfm_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001033
1034 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
1035 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
1036
1037 /* Set the pairing_cb based on the local & remote authentication requirements */
1038 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1039
1040 /* if just_works and bonding bit is not set treat this as temporary */
1041 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 -08001042 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
1043 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
Andre Eisenbach89363762015-01-26 13:49:36 -08001044 pairing_cb.bond_type = BOND_TYPE_TEMPORARY;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001045 else
Andre Eisenbach89363762015-01-26 13:49:36 -08001046 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001047
Pavlin Radoslavov65f9c042015-07-16 17:40:28 -07001048 btm_set_bond_type_dev(p_ssp_cfm_req->bd_addr, pairing_cb.bond_type);
1049
The Android Open Source Project5738f832012-12-12 16:00:35 -08001050 pairing_cb.is_ssp = TRUE;
1051
1052 /* If JustWorks auto-accept */
1053 if (p_ssp_cfm_req->just_works)
1054 {
1055 /* Pairing consent for JustWorks needed if:
Andre Eisenbachaf753a22015-06-08 20:43:00 -07001056 * 1. Incoming (non-temporary) pairing is detected AND
The Android Open Source Project5738f832012-12-12 16:00:35 -08001057 * 2. local IO capabilities are DisplayYesNo AND
1058 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
1059 */
Andre Eisenbachaf753a22015-06-08 20:43:00 -07001060 if (is_incoming && pairing_cb.bond_type != BOND_TYPE_TEMPORARY &&
1061 ((p_ssp_cfm_req->loc_io_caps == HCI_IO_CAP_DISPLAY_YESNO) &&
1062 (p_ssp_cfm_req->rmt_io_caps == HCI_IO_CAP_DISPLAY_ONLY ||
1063 p_ssp_cfm_req->rmt_io_caps == HCI_IO_CAP_NO_IO)))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001064 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001065 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 -08001066 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
1067 }
1068 else
1069 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001070 BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001071 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
1072 return;
1073 }
1074 }
1075
1076 cod = devclass2uint(p_ssp_cfm_req->dev_class);
1077
Chris Mantonf8027002015-03-12 09:22:48 -07001078 if (cod == 0) {
1079 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001080 cod = COD_UNCLASSIFIED;
1081 }
1082
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001083 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001084 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
1085 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
1086 p_ssp_cfm_req->num_val);
1087}
1088
1089static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
1090{
1091 bt_bdaddr_t bd_addr;
1092 bt_bdname_t bd_name;
1093 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08001094 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001095
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001096 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001097
1098 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08001099 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
1100 {
1101 dev_type = BT_DEVICE_TYPE_BREDR;
1102 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001103 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -08001104 p_ssp_key_notif->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001105
1106 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
1107 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
1108
1109 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1110 pairing_cb.is_ssp = TRUE;
1111 cod = devclass2uint(p_ssp_key_notif->dev_class);
1112
Chris Mantonf8027002015-03-12 09:22:48 -07001113 if (cod == 0) {
1114 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001115 cod = COD_UNCLASSIFIED;
1116 }
1117
1118 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
1119 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
1120 p_ssp_key_notif->passkey);
1121}
1122/*******************************************************************************
1123**
1124** Function btif_dm_auth_cmpl_evt
1125**
1126** Description Executes authentication complete event in btif context
1127**
1128** Returns void
1129**
1130*******************************************************************************/
1131static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
1132{
1133 /* Save link key, if not temporary */
1134 bt_bdaddr_t bd_addr;
1135 bt_status_t status = BT_STATUS_FAIL;
1136 bt_bond_state_t state = BT_BOND_STATE_NONE;
Priti Agherac0edf9f2014-06-26 11:23:51 -07001137 BOOLEAN skip_sdp = FALSE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001138
Chaojing Sune2805532015-04-22 13:40:21 -07001139 BTIF_TRACE_DEBUG("%s: bond state=%d", __func__, pairing_cb.state);
1140
The Android Open Source Project5738f832012-12-12 16:00:35 -08001141 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
1142 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
1143 {
Srinu Jella21331c62015-06-16 19:38:45 +05301144 if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) ||
1145 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
1146 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) ||
1147 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB_P_256) ||
1148 pairing_cb.bond_type == BOND_TYPE_PERSISTENT)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001149 {
1150 bt_status_t ret;
Andre Eisenbach89363762015-01-26 13:49:36 -08001151 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, bond_type=%d",
1152 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001153 ret = btif_storage_add_bonded_device(&bd_addr,
1154 p_auth_cmpl->key, p_auth_cmpl->key_type,
1155 pairing_cb.pin_code_len);
1156 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
1157 }
1158 else
1159 {
Andre Eisenbach89363762015-01-26 13:49:36 -08001160 BTIF_TRACE_DEBUG("%s: Temporary key. Not storing. key_type=0x%x, bond_type=%d",
1161 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1162 if(pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
Hemant Guptab820aec2013-12-24 19:59:57 +05301163 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001164 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
Hemant Guptab820aec2013-12-24 19:59:57 +05301165 __FUNCTION__);
Pavlin Radoslavov65f9c042015-07-16 17:40:28 -07001166 btif_storage_remove_bonded_device(&bd_addr);
Hemant Guptab820aec2013-12-24 19:59:57 +05301167 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1168 return;
1169 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001170 }
1171 }
Priti Agherac0edf9f2014-06-26 11:23:51 -07001172
1173 // Skip SDP for certain HID Devices
The Android Open Source Project5738f832012-12-12 16:00:35 -08001174 if (p_auth_cmpl->success)
1175 {
Andre Eisenbach12871662015-05-08 17:42:10 -07001176#if BLE_INCLUDED == TRUE
Chaojing Sune2805532015-04-22 13:40:21 -07001177 btif_storage_set_remote_addr_type(&bd_addr, p_auth_cmpl->addr_type);
Andre Eisenbach12871662015-05-08 17:42:10 -07001178#endif
Chaojing Sune2805532015-04-22 13:40:21 -07001179 btif_update_remote_properties(p_auth_cmpl->bd_addr,
1180 p_auth_cmpl->bd_name, NULL, p_auth_cmpl->dev_type);
Andre Eisenbach31a64002014-10-14 14:29:19 -07001181 pairing_cb.timeout_retries = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001182
Priti Agherac0edf9f2014-06-26 11:23:51 -07001183 if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr, COD_HID_MAJOR))
1184 {
Hemant Guptada33b462015-04-26 10:32:57 +05301185 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -08001186
Hemant Guptada33b462015-04-26 10:32:57 +05301187 BTIF_TRACE_DEBUG("%s: HID Connection from "
1188 "blacklisted device, skipping sdp",__FUNCTION__);
Priti Agherac0edf9f2014-06-26 11:23:51 -07001189 bt_property_t prop;
Priti Agherac0edf9f2014-06-26 11:23:51 -07001190 bt_uuid_t uuid;
1191 char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE;
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -08001192
Priti Agherac0edf9f2014-06-26 11:23:51 -07001193 string_to_uuid(uuid_str, &uuid);
1194
1195 prop.type = BT_PROPERTY_UUIDS;
1196 prop.val = uuid.uu;
1197 prop.len = MAX_UUID_SIZE;
1198
Hemant Guptada33b462015-04-26 10:32:57 +05301199 /* Also write this to the NVRAM */
1200 status = btif_storage_set_remote_device_property(&bd_addr, &prop);
1201 ASSERTC(status == BT_STATUS_SUCCESS, "storing remote services failed", status);
Priti Agherac0edf9f2014-06-26 11:23:51 -07001202 /* Send the event to the BTIF */
1203 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1204 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1205 }
1206 else
1207 {
Hemant Guptada33b462015-04-26 10:32:57 +05301208 status = BT_STATUS_SUCCESS;
1209 state = BT_BOND_STATE_BONDED;
1210
Priti Agherac0edf9f2014-06-26 11:23:51 -07001211 /* Trigger SDP on the device */
1212 pairing_cb.sdp_attempts = 1;;
Andre Eisenbach12871662015-05-08 17:42:10 -07001213
1214#if BLE_INCLUDED == TRUE
Chaojing Sune2805532015-04-22 13:40:21 -07001215 /* If bonded due to cross-key, save the static address too*/
1216 if(pairing_cb.state == BT_BOND_STATE_BONDING &&
1217 (bdcmp(p_auth_cmpl->bd_addr, pairing_cb.bd_addr) != 0))
1218 {
1219 BTIF_TRACE_DEBUG("%s: bonding initiated due to cross key, adding static address",
1220 __func__);
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07001221 bdcpy(pairing_cb.static_bdaddr.address, p_auth_cmpl->bd_addr);
Chaojing Sune2805532015-04-22 13:40:21 -07001222 }
Andre Eisenbach12871662015-05-08 17:42:10 -07001223#endif
Priti Agherac0edf9f2014-06-26 11:23:51 -07001224
1225 if(btif_dm_inquiry_in_progress)
1226 btif_dm_cancel_discovery();
1227
1228 btif_dm_get_remote_services(&bd_addr);
Chaojing Sune2805532015-04-22 13:40:21 -07001229 }
1230 // Do not call bond_state_changed_cb yet. Wait until remote service discovery is complete
The Android Open Source Project5738f832012-12-12 16:00:35 -08001231 }
1232 else
1233 {
Chaojing Sune2805532015-04-22 13:40:21 -07001234 // Map the HCI fail reason to bt status
The Android Open Source Project5738f832012-12-12 16:00:35 -08001235 switch(p_auth_cmpl->fail_reason)
1236 {
1237 case HCI_ERR_PAGE_TIMEOUT:
Hemant Guptada33b462015-04-26 10:32:57 +05301238 if (interop_addr_match(INTEROP_AUTO_RETRY_PAIRING, &bd_addr)
Andre Eisenbach27c4e632015-07-06 15:43:15 -07001239 && pairing_cb.timeout_retries)
Andre Eisenbach31a64002014-10-14 14:29:19 -07001240 {
1241 BTIF_TRACE_WARNING("%s() - Pairing timeout; retrying (%d) ...", __FUNCTION__, pairing_cb.timeout_retries);
1242 --pairing_cb.timeout_retries;
1243 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
1244 return;
1245 }
1246 /* Fall-through */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001247 case HCI_ERR_CONNECTION_TOUT:
1248 status = BT_STATUS_RMT_DEV_DOWN;
1249 break;
1250
Hemant Guptaaef7a672013-07-31 19:00:12 +05301251 case HCI_ERR_PAIRING_NOT_ALLOWED:
1252 status = BT_STATUS_AUTH_REJECTED;
1253 break;
1254
Hemant Guptab4801442014-01-07 18:11:15 +05301255 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
1256 status = BT_STATUS_AUTH_FAILURE;
1257 break;
1258
The Android Open Source Project5738f832012-12-12 16:00:35 -08001259 /* map the auth failure codes, so we can retry pairing if necessary */
1260 case HCI_ERR_AUTH_FAILURE:
Hemant Gupta59a88ec2014-03-19 19:01:35 +05301261 case HCI_ERR_KEY_MISSING:
Zhihai Xua7ea8092013-11-27 14:10:53 +05301262 btif_storage_remove_bonded_device(&bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001263 case HCI_ERR_HOST_REJECT_SECURITY:
1264 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
1265 case HCI_ERR_UNIT_KEY_USED:
1266 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
1267 case HCI_ERR_INSUFFCIENT_SECURITY:
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301268 case HCI_ERR_PEER_USER:
1269 case HCI_ERR_UNSPECIFIED:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001270 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d",
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301271 __FUNCTION__, p_auth_cmpl->fail_reason);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001272 if (pairing_cb.autopair_attempts == 1)
1273 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001274 BTIF_TRACE_DEBUG("%s(): Adding device to blacklist ", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001275
1276 /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class */
1277 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
1278 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
1279 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
1280 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
1281 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
1282 check_cod(&bd_addr, COD_HID_POINTING))
1283 {
1284 btif_storage_add_device_to_autopair_blacklist (&bd_addr);
1285 }
1286 pairing_cb.autopair_attempts++;
1287
1288 /* Create the Bond once again */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001289 BTIF_TRACE_DEBUG("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07001290 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001291 return;
1292 }
1293 else
1294 {
1295 /* if autopair attempts are more than 1, or not attempted */
1296 status = BT_STATUS_AUTH_FAILURE;
1297 }
1298 break;
1299
1300 default:
1301 status = BT_STATUS_FAIL;
1302 }
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301303 /* Special Handling for HID Devices */
1304 if (check_cod(&bd_addr, COD_HID_POINTING)) {
1305 /* Remove Device as bonded in nvram as authentication failed */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001306 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __FUNCTION__);
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301307 btif_storage_remove_bonded_device(&bd_addr);
1308 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001309 bond_state_changed(status, &bd_addr, state);
1310 }
1311}
1312
1313/******************************************************************************
1314**
1315** Function btif_dm_search_devices_evt
1316**
1317** Description Executes search devices callback events in btif context
1318**
1319** Returns void
1320**
1321******************************************************************************/
1322static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
1323{
1324 tBTA_DM_SEARCH *p_search_data;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001325 BTIF_TRACE_EVENT("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001326
1327 switch (event)
1328 {
1329 case BTA_DM_DISC_RES_EVT:
1330 {
1331 p_search_data = (tBTA_DM_SEARCH *)p_param;
1332 /* Remote name update */
1333 if (strlen((const char *) p_search_data->disc_res.bd_name))
1334 {
1335 bt_property_t properties[1];
1336 bt_bdaddr_t bdaddr;
1337 bt_status_t status;
1338
1339 properties[0].type = BT_PROPERTY_BDNAME;
1340 properties[0].val = p_search_data->disc_res.bd_name;
1341 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
1342 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
1343
1344 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
1345 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
1346 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1347 status, &bdaddr, 1, properties);
1348 }
1349 /* TODO: Services? */
1350 }
1351 break;
1352
1353 case BTA_DM_INQ_RES_EVT:
1354 {
1355 /* inquiry result */
1356 UINT32 cod;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001357 bt_bdname_t bdname;
1358 bt_bdaddr_t bdaddr;
1359 UINT8 remote_name_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001360 tBTA_SERVICE_MASK services = 0;
1361 bdstr_t bdstr;
1362
1363 p_search_data = (tBTA_DM_SEARCH *)p_param;
1364 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
1365
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07001366 BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __FUNCTION__, bdaddr_to_string(&bdaddr, bdstr, sizeof(bdstr)),
The Android Open Source Project5738f832012-12-12 16:00:35 -08001367#if (BLE_INCLUDED == TRUE)
1368 p_search_data->inq_res.device_type);
1369#else
1370 BT_DEVICE_TYPE_BREDR);
1371#endif
1372 bdname.name[0] = 0;
1373
1374 cod = devclass2uint (p_search_data->inq_res.dev_class);
1375
Chris Mantonf8027002015-03-12 09:22:48 -07001376 if (cod == 0) {
1377 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001378 cod = COD_UNCLASSIFIED;
1379 }
1380
1381 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1382 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1383
1384 /* Check EIR for remote name and services */
1385 if (p_search_data->inq_res.p_eir)
1386 {
1387 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001388 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001389 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */
1390 }
1391
1392
1393 {
1394 bt_property_t properties[5];
1395 bt_device_type_t dev_type;
1396 uint32_t num_properties = 0;
1397 bt_status_t status;
Nitin Aroraac728402015-06-26 18:09:37 -07001398 int addr_type = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001399
1400 memset(properties, 0, sizeof(properties));
1401 /* BD_ADDR */
1402 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1403 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1404 num_properties++;
1405 /* BD_NAME */
1406 /* Don't send BDNAME if it is empty */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001407 if (bdname.name[0])
1408 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001409 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1410 BT_PROPERTY_BDNAME,
1411 strlen((char *)bdname.name), &bdname);
1412 num_properties++;
1413 }
1414
1415 /* DEV_CLASS */
1416 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1417 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1418 num_properties++;
1419 /* DEV_TYPE */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001420#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001421 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
Nitin Aroraac728402015-06-26 18:09:37 -07001422
1423 /* Verify if the device is dual mode in NVRAM */
1424 int stored_device_type = 0;
1425 if (btif_get_device_type(bdaddr.address, &stored_device_type) &&
1426 ((stored_device_type == BT_DEVICE_TYPE_BLE &&
1427 p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BREDR) ||
1428 (stored_device_type == BT_DEVICE_TYPE_BREDR &&
1429 p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE))) {
1430 dev_type = BT_DEVICE_TYPE_DUMO;
1431 } else {
1432 dev_type = p_search_data->inq_res.device_type;
1433 }
1434
1435 if (p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE)
1436 addr_type = p_search_data->inq_res.ble_addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001437#else
1438 dev_type = BT_DEVICE_TYPE_BREDR;
1439#endif
1440 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1441 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1442 num_properties++;
1443 /* RSSI */
1444 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1445 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1446 &(p_search_data->inq_res.rssi));
1447 num_properties++;
1448
1449 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1450 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001451#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1452 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
1453 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1454#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001455 /* Callback to notify upper layer of device */
1456 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1457 num_properties, properties);
1458 }
1459 }
1460 break;
1461
1462 case BTA_DM_INQ_CMPL_EVT:
1463 {
Satya Calloji6e2d9db2014-07-08 16:18:58 -07001464#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1465 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1466 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1467 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1468 bte_scan_filt_param_cfg_evt, 0);
1469#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001470 }
1471 break;
1472 case BTA_DM_DISC_CMPL_EVT:
1473 {
1474 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1475 }
1476 break;
1477 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1478 {
1479 /* if inquiry is not in progress and we get a cancel event, then
1480 * it means we are done with inquiry, but remote_name fetches are in
1481 * progress
1482 *
1483 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt
1484 * but instead wait for the cancel_cmpl_evt via the Busy Level
1485 *
1486 */
1487 if (btif_dm_inquiry_in_progress == FALSE)
1488 {
Nitin Arora7b85efa2014-09-26 14:05:24 -07001489#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1490 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1491 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1492 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1493 bte_scan_filt_param_cfg_evt, 0);
1494#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001495 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1496 }
1497 }
1498 break;
1499 }
1500}
1501
1502/*******************************************************************************
1503**
1504** Function btif_dm_search_services_evt
1505**
1506** Description Executes search services event in btif context
1507**
1508** Returns void
1509**
1510*******************************************************************************/
1511static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1512{
1513 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1514
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001515 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001516 switch (event)
1517 {
1518 case BTA_DM_DISC_RES_EVT:
1519 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001520 bt_property_t prop;
Bernhard Rosenkränzer104e3f22014-11-12 21:53:08 +01001521 uint32_t i = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001522 bt_bdaddr_t bd_addr;
1523 bt_status_t ret;
1524
1525 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1526
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001527 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001528 p_data->disc_res.result, p_data->disc_res.services);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001529 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1530 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1531 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1532 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001533 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001534 pairing_cb.sdp_attempts++;
1535 btif_dm_get_remote_services(&bd_addr);
1536 return;
1537 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001538 prop.type = BT_PROPERTY_UUIDS;
1539 prop.len = 0;
1540 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1541 {
1542 prop.val = p_data->disc_res.p_uuid_list;
1543 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1544 for (i=0; i < p_data->disc_res.num_uuids; i++)
1545 {
1546 char temp[256];
Chris Manton8ff3fea2015-01-07 13:59:14 -08001547 uuid_to_string_legacy((bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i*MAX_UUID_SIZE)), temp);
Chris Mantonf8027002015-03-12 09:22:48 -07001548 LOG_INFO("%s index:%d uuid:%s", __func__, i, temp);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001549 }
1550 }
1551
1552 /* onUuidChanged requires getBondedDevices to be populated.
1553 ** bond_state_changed needs to be sent prior to remote_device_property
1554 */
1555 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
Chaojing Sune2805532015-04-22 13:40:21 -07001556 ((bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0) ||
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07001557 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr.address) == 0)) &&
Chaojing Sune2805532015-04-22 13:40:21 -07001558 pairing_cb.sdp_attempts > 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001559 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001560 BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001561 __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001562 pairing_cb.sdp_attempts = 0;
Chaojing Sune2805532015-04-22 13:40:21 -07001563
1564 // If bonding occured due to cross-key pairing, send bonding callback
1565 // for static address now
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07001566 if (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr.address) == 0)
Chaojing Sune2805532015-04-22 13:40:21 -07001567 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1568
The Android Open Source Project5738f832012-12-12 16:00:35 -08001569 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1570 }
1571
Chaojing Sune2805532015-04-22 13:40:21 -07001572 if (p_data->disc_res.num_uuids != 0)
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001573 {
1574 /* Also write this to the NVRAM */
1575 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1576 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1577 /* Send the event to the BTIF */
1578 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1579 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1580 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001581 }
1582 break;
1583
1584 case BTA_DM_DISC_CMPL_EVT:
1585 /* fixme */
1586 break;
1587
Matthew Xie607e3b72013-08-15 19:30:48 -07001588#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001589 case BTA_DM_DISC_BLE_RES_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001590 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__,
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001591 p_data->disc_ble_res.service.uu.uuid16);
1592 bt_uuid_t uuid;
1593 int i = 0;
1594 int j = 15;
1595 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1596 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001597 BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001598 bt_property_t prop;
1599 bt_bdaddr_t bd_addr;
1600 char temp[256];
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001601 bt_status_t ret;
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001602
1603 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1604
1605 while(i < j )
1606 {
1607 unsigned char c = uuid.uu[j];
1608 uuid.uu[j] = uuid.uu[i];
1609 uuid.uu[i] = c;
1610 i++;
1611 j--;
1612 }
1613
Chris Manton8ff3fea2015-01-07 13:59:14 -08001614 uuid_to_string_legacy(&uuid, temp);
Chris Mantonf8027002015-03-12 09:22:48 -07001615 LOG_INFO("%s uuid:%s", __func__, temp);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001616
1617 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1618 prop.type = BT_PROPERTY_UUIDS;
1619 prop.val = uuid.uu;
1620 prop.len = MAX_UUID_SIZE;
1621
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001622 /* Also write this to the NVRAM */
1623 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1624 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1625
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001626 /* Send the event to the BTIF */
1627 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1628 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1629
1630 }
1631 break;
Matthew Xie607e3b72013-08-15 19:30:48 -07001632#endif /* BLE_INCLUDED */
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001633
The Android Open Source Project5738f832012-12-12 16:00:35 -08001634 default:
1635 {
1636 ASSERTC(0, "unhandled search services event", event);
1637 }
1638 break;
1639 }
1640}
1641
1642/*******************************************************************************
1643**
1644** Function btif_dm_remote_service_record_evt
1645**
1646** Description Executes search service record event in btif context
1647**
1648** Returns void
1649**
1650*******************************************************************************/
1651static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1652{
1653 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1654
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001655 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001656 switch (event)
1657 {
1658 case BTA_DM_DISC_RES_EVT:
1659 {
1660 bt_service_record_t rec;
1661 bt_property_t prop;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001662 bt_bdaddr_t bd_addr;
1663
1664 memset(&rec, 0, sizeof(bt_service_record_t));
1665 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1666
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001667 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001668 p_data->disc_res.result, p_data->disc_res.services);
1669 prop.type = BT_PROPERTY_SERVICE_RECORD;
1670 prop.val = (void*)&rec;
1671 prop.len = sizeof(rec);
1672
1673 /* disc_res.result is overloaded with SCN. Cannot check result */
1674 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1675 /* TODO: Get the UUID as well */
1676 rec.channel = p_data->disc_res.result - 3;
1677 /* TODO: Need to get the service name using p_raw_data */
1678 rec.name[0] = 0;
1679
1680 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1681 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1682 }
1683 break;
1684
1685 default:
1686 {
1687 ASSERTC(0, "unhandled remote service record event", event);
1688 }
1689 break;
1690 }
1691}
1692
1693/*******************************************************************************
1694**
1695** Function btif_dm_upstreams_cback
1696**
1697** Description Executes UPSTREAMS events in btif context
1698**
1699** Returns void
1700**
1701*******************************************************************************/
1702static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1703{
The Android Open Source Project5738f832012-12-12 16:00:35 -08001704 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1705 tBTA_SERVICE_MASK service_mask;
1706 uint32_t i;
1707 bt_bdaddr_t bd_addr;
1708
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001709 BTIF_TRACE_EVENT("btif_dm_upstreams_cback ev: %s", dump_dm_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001710
1711 switch (event)
1712 {
1713 case BTA_DM_ENABLE_EVT:
1714 {
1715 BD_NAME bdname;
1716 bt_status_t status;
1717 bt_property_t prop;
1718 prop.type = BT_PROPERTY_BDNAME;
1719 prop.len = BD_NAME_LEN;
1720 prop.val = (void*)bdname;
1721
1722 status = btif_storage_get_adapter_property(&prop);
Matthew Xie1e5109b2012-11-09 18:26:26 -08001723 if (status == BT_STATUS_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001724 {
1725 /* A name exists in the storage. Make this the device name */
1726 BTA_DmSetDeviceName((char*)prop.val);
1727 }
Matthew Xie1e5109b2012-11-09 18:26:26 -08001728 else
1729 {
1730 /* Storage does not have a name yet.
1731 * Use the default name and write it to the chip
1732 */
1733 BTA_DmSetDeviceName(btif_get_default_local_name());
1734 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001735
Andre Eisenbacha015a832014-09-11 14:09:40 -07001736#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1737 /* Enable local privacy */
Andre Eisenbach3e0dc732014-10-24 09:55:34 -07001738 BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED);
Andre Eisenbacha015a832014-09-11 14:09:40 -07001739#endif
1740
The Android Open Source Project5738f832012-12-12 16:00:35 -08001741 /* for each of the enabled services in the mask, trigger the profile
1742 * enable */
1743 service_mask = btif_get_enabled_services_mask();
1744 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1745 {
1746 if (service_mask &
1747 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1748 {
1749 btif_in_execute_service_request(i, TRUE);
1750 }
1751 }
1752 /* clear control blocks */
1753 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
Satya Calloji877123f2015-04-23 23:39:49 -07001754 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001755
1756 /* This function will also trigger the adapter_properties_cb
1757 ** and bonded_devices_info_cb
1758 */
1759 btif_storage_load_bonded_devices();
1760
1761 btif_storage_load_autopair_device_list();
1762
Zach Johnson39110ec2014-10-06 13:15:00 -07001763 btif_enable_bluetooth_evt(p_data->enable.status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001764 }
1765 break;
1766
1767 case BTA_DM_DISABLE_EVT:
1768 /* for each of the enabled services in the mask, trigger the profile
1769 * disable */
1770 service_mask = btif_get_enabled_services_mask();
1771 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1772 {
1773 if (service_mask &
1774 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1775 {
1776 btif_in_execute_service_request(i, FALSE);
1777 }
1778 }
1779 btif_disable_bluetooth_evt();
1780 break;
1781
1782 case BTA_DM_PIN_REQ_EVT:
1783 btif_dm_pin_req_evt(&p_data->pin_req);
1784 break;
1785
1786 case BTA_DM_AUTH_CMPL_EVT:
1787 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1788 break;
1789
1790 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1791 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1792 {
1793 bdcpy(bd_addr.address, pairing_cb.bd_addr);
Pavlin Radoslavov65f9c042015-07-16 17:40:28 -07001794 btm_set_bond_type_dev(pairing_cb.bd_addr, BOND_TYPE_UNKNOWN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001795 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
Subramanian Srinivasan19bc89c2014-03-13 12:44:03 -07001796 btif_dm_remove_bond(&bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001797 }
1798 break;
1799
1800 case BTA_DM_SP_CFM_REQ_EVT:
1801 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1802 break;
1803 case BTA_DM_SP_KEY_NOTIF_EVT:
1804 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1805 break;
1806
1807 case BTA_DM_DEV_UNPAIRED_EVT:
1808 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
Pavlin Radoslavov65f9c042015-07-16 17:40:28 -07001809 btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001810
1811 /*special handling for HID devices */
1812 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -07001813 btif_hh_remove_device(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001814 #endif
Hemant Gupta94c15ec2013-11-11 12:33:44 +05301815 #if (defined(BTA_HD_INCLUDED) && (BTA_HD_INCLUDED == TRUE))
1816 btif_hd_remove_device(bd_addr);
1817 #endif
1818 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1819 btif_storage_remove_ble_bonding_keys(&bd_addr);
1820 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001821 btif_storage_remove_bonded_device(&bd_addr);
1822 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1823 break;
1824
1825 case BTA_DM_BUSY_LEVEL_EVT:
1826 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001827
1828 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001829 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001830 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001831 {
1832 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1833 BT_DISCOVERY_STARTED);
1834 btif_dm_inquiry_in_progress = TRUE;
1835 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001836 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001837 {
1838 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1839 BT_DISCOVERY_STOPPED);
1840 btif_dm_inquiry_in_progress = FALSE;
1841 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001842 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001843 {
1844 btif_dm_inquiry_in_progress = FALSE;
1845 }
1846 }
1847 }break;
1848
1849 case BTA_DM_LINK_UP_EVT:
1850 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001851 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001852
Nitin Srivastavab30e7122014-08-25 18:17:48 +05301853 if(p_data->link_up.link_type == BT_TRANSPORT_LE)
1854 {
1855 num_active_le_links++;
1856 BTIF_TRACE_DEBUG("num_active_le_links is %d ",
1857 num_active_le_links);
1858 }
1859
1860 if(p_data->link_up.link_type == BT_TRANSPORT_BR_EDR)
1861 {
1862 num_active_br_edr_links++;
1863 BTIF_TRACE_DEBUG("num_active_br_edr_links is %d ",
1864 num_active_br_edr_links);
1865 }
1866 /* When tuchtones are enabled and 2 EDR HS are connected, if new
1867 * connection is initated, then tuch tones are send to both connected HS
1868 * over A2dp.Stream will be suspended after 3 secs and if remote has
1869 * initiated play in this duartion, multicast must not be enabled with
1870 * 3 ACL's, hence trigger a2dp suspend.
1871 * During active muisc streaming no new connection can happen, hence
1872 * We will get this only when multistreaming is happening due to tuchtones
1873 */
1874 if (btif_av_get_ongoing_multicast())
1875 {
1876 // trigger a2dp suspend
1877 btif_av_trigger_suspend();
1878 }
1879
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001880 btif_update_remote_version_property(&bd_addr);
1881
The Android Open Source Project5738f832012-12-12 16:00:35 -08001882 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1883 &bd_addr, BT_ACL_STATE_CONNECTED);
1884 break;
1885
1886 case BTA_DM_LINK_DOWN_EVT:
1887 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
Nitin Srivastavab30e7122014-08-25 18:17:48 +05301888
Pavlin Radoslavov65f9c042015-07-16 17:40:28 -07001889 btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);
Nitin Srivastavab30e7122014-08-25 18:17:48 +05301890
1891 BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
1892 if (num_active_le_links > 0 &&
1893 p_data->link_down.link_type == BT_TRANSPORT_LE)
1894 {
1895 num_active_le_links--;
1896 BTIF_TRACE_DEBUG("num_active_le_links is %d ",num_active_le_links);
1897 }
1898
1899 if (num_active_br_edr_links > 0 &&
1900 p_data->link_down.link_type == BT_TRANSPORT_BR_EDR)
1901 {
1902 num_active_br_edr_links--;
1903 BTIF_TRACE_DEBUG("num_active_br_edr_links is %d ",num_active_br_edr_links);
1904 }
Anubhav Gupta791692a2013-12-06 17:11:11 +05301905 btif_av_move_idle(bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001906 BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001907 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1908 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1909 break;
1910
1911 case BTA_DM_HW_ERROR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001912 BTIF_TRACE_ERROR("Received H/W Error. ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001913 /* Flush storage data */
1914 btif_config_flush();
1915 usleep(100000); /* 100milliseconds */
1916 /* Killing the process to force a restart as part of fault tolerance */
1917 kill(getpid(), SIGKILL);
1918 break;
1919
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001920#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1921 case BTA_DM_BLE_KEY_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001922 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 -08001923
1924 /* If this pairing is by-product of local initiated GATT client Read or Write,
1925 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1926 have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1927 if (pairing_cb.state != BT_BOND_STATE_BONDING)
1928 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001929 BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001930 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1931 BT_BOND_STATE_BONDING);
1932 }
1933 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1934 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001935 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001936 break;
1937 }
1938
1939 switch (p_data->ble_key.key_type)
1940 {
1941 case BTA_LE_KEY_PENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001942 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001943 pairing_cb.ble.is_penc_key_rcvd = TRUE;
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07001944 pairing_cb.ble.penc_key = p_data->ble_key.p_key_value->penc_key;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001945 break;
1946
1947 case BTA_LE_KEY_PID:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001948 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001949 pairing_cb.ble.is_pid_key_rcvd = TRUE;
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07001950 pairing_cb.ble.pid_key = p_data->ble_key.p_key_value->pid_key;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001951 break;
1952
1953 case BTA_LE_KEY_PCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001954 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001955 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07001956 pairing_cb.ble.pcsrk_key = p_data->ble_key.p_key_value->pcsrk_key;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001957 break;
1958
1959 case BTA_LE_KEY_LENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001960 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001961 pairing_cb.ble.is_lenc_key_rcvd = TRUE;
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07001962 pairing_cb.ble.lenc_key = p_data->ble_key.p_key_value->lenc_key;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001963 break;
1964
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001965 case BTA_LE_KEY_LCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001966 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001967 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07001968 pairing_cb.ble.lcsrk_key = p_data->ble_key.p_key_value->lcsrk_key;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001969 break;
1970
Satya Calloji444a8da2015-03-06 10:38:22 -08001971 case BTA_LE_KEY_LID:
1972 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LID");
1973 pairing_cb.ble.is_lidk_key_rcvd = TRUE;
1974 break;
1975
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001976 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001977 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001978 break;
1979 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001980 break;
1981 case BTA_DM_BLE_SEC_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001982 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001983 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1984 break;
1985 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001986 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001987 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1988 break;
1989 case BTA_DM_BLE_PASSKEY_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001990 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001991 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1992 break;
Satya Calloji444a8da2015-03-06 10:38:22 -08001993 case BTA_DM_BLE_NC_REQ_EVT:
1994 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
1995 btif_dm_ble_key_nc_req_evt(&p_data->key_notif);
1996 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001997 case BTA_DM_BLE_OOB_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001998 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001999 break;
2000 case BTA_DM_BLE_LOCAL_IR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002001 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002002 ble_local_key_cb.is_id_keys_rcvd = TRUE;
Satya Calloji444a8da2015-03-06 10:38:22 -08002003 memcpy(&ble_local_key_cb.id_keys.irk[0],
2004 &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
2005 memcpy(&ble_local_key_cb.id_keys.ir[0],
2006 &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
2007 memcpy(&ble_local_key_cb.id_keys.dhk[0],
2008 &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002009 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
Satya Calloji7bd40382015-05-19 14:42:32 -07002010 BTIF_DM_LE_LOCAL_KEY_IRK,
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002011 BT_OCTET16_LEN);
2012 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
Satya Calloji7bd40382015-05-19 14:42:32 -07002013 BTIF_DM_LE_LOCAL_KEY_IR,
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002014 BT_OCTET16_LEN);
2015 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
2016 BTIF_DM_LE_LOCAL_KEY_DHK,
2017 BT_OCTET16_LEN);
2018 break;
2019 case BTA_DM_BLE_LOCAL_ER_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002020 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002021 ble_local_key_cb.is_er_rcvd = TRUE;
2022 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
2023 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
2024 BTIF_DM_LE_LOCAL_KEY_ER,
2025 BT_OCTET16_LEN);
2026 break;
2027
2028 case BTA_DM_BLE_AUTH_CMPL_EVT:
Satya Calloji444a8da2015-03-06 10:38:22 -08002029 BTIF_TRACE_DEBUG("BTA_DM_BLE_AUTH_CMPL_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002030 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
2031 break;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07002032
2033 case BTA_DM_LE_FEATURES_READ:
2034 {
2035 tBTM_BLE_VSC_CB cmn_vsc_cb;
2036 bt_local_le_features_t local_le_features;
2037 char buf[512];
2038 bt_property_t prop;
2039 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
2040 prop.val = (void*)buf;
2041 prop.len = sizeof(buf);
2042
2043 /* LE features are not stored in storage. Should be retrived from stack */
2044 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
2045 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
2046
2047 prop.len = sizeof (bt_local_le_features_t);
2048 if (cmn_vsc_cb.filter_support == 1)
2049 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
2050 else
2051 local_le_features.max_adv_filter_supported = 0;
2052 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
2053 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
2054 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
Satya Callojiefaddcb2014-07-28 23:22:05 -07002055 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
Satya Callojif5387072015-02-09 17:40:52 -08002056 local_le_features.scan_result_storage_size = cmn_vsc_cb.tot_scan_results_strg;
2057 local_le_features.version_supported = cmn_vsc_cb.version_supported;
2058 local_le_features.total_trackable_advertisers =
2059 cmn_vsc_cb.total_trackable_advertisers;
Satya Calloji5725fc62015-03-31 13:24:32 -07002060
2061 local_le_features.extended_scan_support = cmn_vsc_cb.extended_scan_support > 0;
2062 local_le_features.debug_logging_supported = cmn_vsc_cb.debug_logging_supported > 0;
2063
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07002064 memcpy(prop.val, &local_le_features, prop.len);
2065 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
2066 break;
2067 }
Satya Callojie5ba8842014-07-03 17:18:02 -07002068
2069 case BTA_DM_ENER_INFO_READ:
2070 {
2071 btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param;
2072 bt_activity_energy_info energy_info;
2073 energy_info.status = p_ener_data->status;
2074 energy_info.ctrl_state = p_ener_data->ctrl_state;
2075 energy_info.rx_time = p_ener_data->rx_time;
2076 energy_info.tx_time = p_ener_data->tx_time;
2077 energy_info.idle_time = p_ener_data->idle_time;
2078 energy_info.energy_used = p_ener_data->energy_used;
2079 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info);
2080 break;
2081 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002082#endif
2083
The Android Open Source Project5738f832012-12-12 16:00:35 -08002084 case BTA_DM_AUTHORIZE_EVT:
2085 case BTA_DM_SIG_STRENGTH_EVT:
2086 case BTA_DM_SP_RMT_OOB_EVT:
2087 case BTA_DM_SP_KEYPRESS_EVT:
2088 case BTA_DM_ROLE_CHG_EVT:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002089
The Android Open Source Project5738f832012-12-12 16:00:35 -08002090 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002091 BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event );
The Android Open Source Project5738f832012-12-12 16:00:35 -08002092 break;
2093 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002094
Andre Eisenbach8a057242015-04-29 22:27:15 -07002095 btif_dm_data_free(event, p_data);
2096}
The Android Open Source Project5738f832012-12-12 16:00:35 -08002097
2098/*******************************************************************************
2099**
2100** Function btif_dm_generic_evt
2101**
2102** Description Executes non-BTA upstream events in BTIF context
2103**
2104** Returns void
2105**
2106*******************************************************************************/
2107static void btif_dm_generic_evt(UINT16 event, char* p_param)
2108{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002109 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002110 switch(event)
2111 {
2112 case BTIF_DM_CB_DISCOVERY_STARTED:
2113 {
2114 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
2115 }
2116 break;
2117
2118 case BTIF_DM_CB_CREATE_BOND:
2119 {
Andre Eisenbach31a64002014-10-14 14:29:19 -07002120 pairing_cb.timeout_retries = NUM_TIMEOUT_RETRIES;
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002121 btif_dm_create_bond_cb_t *create_bond_cb = (btif_dm_create_bond_cb_t*)p_param;
2122 btif_dm_cb_create_bond(&create_bond_cb->bdaddr, create_bond_cb->transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002123 }
2124 break;
2125
2126 case BTIF_DM_CB_REMOVE_BOND:
2127 {
2128 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
2129 }
2130 break;
2131
2132 case BTIF_DM_CB_HID_REMOTE_NAME:
2133 {
2134 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
2135 }
2136 break;
2137
2138 case BTIF_DM_CB_BOND_STATE_BONDING:
2139 {
2140 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
2141 }
2142 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002143 case BTIF_DM_CB_LE_TX_TEST:
2144 case BTIF_DM_CB_LE_RX_TEST:
2145 {
2146 uint8_t status;
2147 STREAM_TO_UINT8(status, p_param);
2148 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2149 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
2150 }
2151 break;
2152 case BTIF_DM_CB_LE_TEST_END:
2153 {
2154 uint8_t status;
2155 uint16_t count = 0;
2156 STREAM_TO_UINT8(status, p_param);
2157 if (status == 0)
2158 STREAM_TO_UINT16(count, p_param);
2159 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2160 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
2161 }
2162 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002163 default:
2164 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002165 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002166 }
2167 break;
2168 }
2169}
2170
2171/*******************************************************************************
2172**
2173** Function bte_dm_evt
2174**
2175** Description Switches context from BTE to BTIF for all DM events
2176**
2177** Returns void
2178**
2179*******************************************************************************/
2180
2181void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
2182{
The Android Open Source Project5738f832012-12-12 16:00:35 -08002183 /* switch context to btif task context (copy full union size for convenience) */
Andre Eisenbach8a057242015-04-29 22:27:15 -07002184 bt_status_t status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event,
2185 (void*)p_data, sizeof(tBTA_DM_SEC), btif_dm_data_copy);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002186
2187 /* catch any failed context transfers */
2188 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
2189}
2190
2191/*******************************************************************************
2192**
2193** Function bte_search_devices_evt
2194**
2195** Description Switches context from BTE to BTIF for DM search events
2196**
2197** Returns void
2198**
2199*******************************************************************************/
2200static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2201{
2202 UINT16 param_len = 0;
2203
2204 if (p_data)
2205 param_len += sizeof(tBTA_DM_SEARCH);
2206 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
2207 switch (event)
2208 {
2209 case BTA_DM_INQ_RES_EVT:
2210 {
2211 if (p_data->inq_res.p_eir)
2212 param_len += HCI_EXT_INQ_RESPONSE_LEN;
2213 }
2214 break;
2215
2216 case BTA_DM_DISC_RES_EVT:
2217 {
2218 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
2219 param_len += p_data->disc_res.raw_data_size;
2220 }
2221 break;
2222 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002223 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 -08002224
2225 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
2226 if (event == BTA_DM_INQ_RES_EVT)
2227 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
2228
2229 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
2230 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
2231}
2232
2233/*******************************************************************************
2234**
2235** Function bte_dm_search_services_evt
2236**
2237** Description Switches context from BTE to BTIF for DM search services
2238** event
2239**
2240** Returns void
2241**
2242*******************************************************************************/
2243static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2244{
2245 UINT16 param_len = 0;
2246 if (p_data)
2247 param_len += sizeof(tBTA_DM_SEARCH);
2248 switch (event)
2249 {
2250 case BTA_DM_DISC_RES_EVT:
2251 {
2252 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
2253 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
2254 }
2255 } break;
2256 }
2257 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
2258 * if raw_data is needed. */
2259 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
2260 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
2261}
2262
2263/*******************************************************************************
2264**
2265** Function bte_dm_remote_service_record_evt
2266**
2267** Description Switches context from BTE to BTIF for DM search service
2268** record event
2269**
2270** Returns void
2271**
2272*******************************************************************************/
2273static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2274{
2275 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
2276 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
2277}
2278
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002279#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07002280/*******************************************************************************
2281**
2282** Function bta_energy_info_cb
2283**
2284** Description Switches context from BTE to BTIF for DM energy info event
2285**
2286** Returns void
2287**
2288*******************************************************************************/
2289static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time,
2290 tBTA_DM_BLE_IDLE_TIME_MS idle_time,
2291 tBTA_DM_BLE_ENERGY_USED energy_used,
2292 tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status)
2293{
2294 BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld",
2295 status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
2296
2297 btif_activity_energy_info_cb_t btif_cb;
2298 btif_cb.status = status;
2299 btif_cb.ctrl_state = ctrl_state;
2300 btif_cb.tx_time = (uint64_t) tx_time;
2301 btif_cb.rx_time = (uint64_t) rx_time;
2302 btif_cb.idle_time =(uint64_t) idle_time;
2303 btif_cb.energy_used =(uint64_t) energy_used;
2304 btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
2305 (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL);
2306}
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002307#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07002308
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002309/*******************************************************************************
2310**
2311** Function bte_scan_filt_param_cfg_evt
2312**
2313** Description Scan filter param config event
2314**
2315** Returns void
2316**
2317*******************************************************************************/
2318static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
2319 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
2320 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
2321{
2322 /* This event occurs on calling BTA_DmBleCfgFilterCondition internally,
2323 ** and that is why there is no HAL callback
2324 */
2325 if(BTA_SUCCESS != status)
2326 {
2327 BTIF_TRACE_ERROR("%s, %d", __FUNCTION__, status);
2328 }
2329 else
2330 {
2331 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2332 }
2333}
2334
The Android Open Source Project5738f832012-12-12 16:00:35 -08002335/*****************************************************************************
2336**
2337** btif api functions (no context switch)
2338**
2339*****************************************************************************/
2340
2341/*******************************************************************************
2342**
2343** Function btif_dm_start_discovery
2344**
2345** Description Start device discovery/inquiry
2346**
2347** Returns bt_status_t
2348**
2349*******************************************************************************/
2350bt_status_t btif_dm_start_discovery(void)
2351{
2352 tBTA_DM_INQ inq_params;
2353 tBTA_SERVICE_MASK services = 0;
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002354 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002355
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002356 BTIF_TRACE_EVENT("%s", __FUNCTION__);
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002357
2358#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2359 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
2360 /* Cleanup anything remaining on index 0 */
2361 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
2362 bte_scan_filt_param_cfg_evt, 0);
2363
2364 /* Add an allow-all filter on index 0*/
2365 adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE;
2366 adv_filt_param.feat_seln = ALLOW_ALL_FILTER;
2367 adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;
2368 adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;
2369 adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE;
2370 adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE;
2371 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL,
2372 bte_scan_filt_param_cfg_evt, 0);
2373
The Android Open Source Project5738f832012-12-12 16:00:35 -08002374 /* TODO: Do we need to handle multiple inquiries at the same time? */
2375
2376 /* Set inquiry params and call API */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002377 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
Matthew Xie7f3e4292013-09-30 12:44:10 -07002378#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
2379 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
2380 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
2381 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
2382 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
2383#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002384#else
2385 inq_params.mode = BTA_DM_GENERAL_INQUIRY;
2386#endif
2387 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
2388
2389 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
2390 inq_params.report_dup = TRUE;
2391
2392 inq_params.filter_type = BTA_DM_INQ_CLR;
2393 /* TODO: Filter device by BDA needs to be implemented here */
2394
2395 /* Will be enabled to TRUE once inquiry busy level has been received */
2396 btif_dm_inquiry_in_progress = FALSE;
2397 /* find nearby devices */
2398 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
2399
2400 return BT_STATUS_SUCCESS;
2401}
2402
2403/*******************************************************************************
2404**
2405** Function btif_dm_cancel_discovery
2406**
2407** Description Cancels search
2408**
2409** Returns bt_status_t
2410**
2411*******************************************************************************/
2412bt_status_t btif_dm_cancel_discovery(void)
2413{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002414 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002415 BTA_DmSearchCancel();
2416 return BT_STATUS_SUCCESS;
2417}
2418
2419/*******************************************************************************
2420**
2421** Function btif_dm_create_bond
2422**
2423** Description Initiate bonding with the specified device
2424**
2425** Returns bt_status_t
2426**
2427*******************************************************************************/
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002428bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr, int transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002429{
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002430 btif_dm_create_bond_cb_t create_bond_cb;
2431 create_bond_cb.transport = transport;
2432 bdcpy(create_bond_cb.bdaddr.address, bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002433
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002434 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002435 BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)), transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002436 if (pairing_cb.state != BT_BOND_STATE_NONE)
2437 return BT_STATUS_BUSY;
2438
2439 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002440 (char *)&create_bond_cb, sizeof(btif_dm_create_bond_cb_t), NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002441
2442 return BT_STATUS_SUCCESS;
2443}
2444
2445/*******************************************************************************
2446**
2447** Function btif_dm_cancel_bond
2448**
2449** Description Initiate bonding with the specified device
2450**
2451** Returns bt_status_t
2452**
2453*******************************************************************************/
2454
2455bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
2456{
2457 bdstr_t bdstr;
2458
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002459 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002460
2461 /* TODO:
2462 ** 1. Restore scan modes
2463 ** 2. special handling for HID devices
2464 */
2465 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2466 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002467
2468#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2469
2470 if (pairing_cb.is_ssp)
2471 {
2472 if (pairing_cb.is_le_only)
2473 {
2474 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2475 }
2476 else
Hemant Guptae1468692013-11-14 16:21:29 +05302477 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002478 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
Hemant Guptae1468692013-11-14 16:21:29 +05302479 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2480 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2481 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002482 }
2483 else
2484 {
2485 if (pairing_cb.is_le_only)
2486 {
2487 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2488 }
2489 else
2490 {
2491 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2492 }
2493 /* Cancel bonding, in case it is in ACL connection setup state */
2494 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2495 }
2496
2497#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002498 if (pairing_cb.is_ssp)
2499 {
2500 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2501 }
2502 else
2503 {
2504 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2505 }
2506 /* Cancel bonding, in case it is in ACL connection setup state */
2507 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2508 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002509#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002510 }
2511
2512 return BT_STATUS_SUCCESS;
2513}
2514
2515/*******************************************************************************
2516**
Hemant Guptada33b462015-04-26 10:32:57 +05302517** Function btif_dm_hh_open_success
2518**
2519** Description Checks if device is blacklisted, if yes takes appropriate action
2520**
2521** Returns none
2522**
2523*******************************************************************************/
2524
2525void btif_dm_hh_open_success(bt_bdaddr_t *bdaddr)
2526{
2527 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2528 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2529 {
2530 if (check_if_auth_bl(bdaddr)
2531 && check_cod_hid(bdaddr, COD_HID_MAJOR))
2532 {
2533 bt_status_t status;
2534 LINK_KEY link_key = {0};
2535 bond_state_changed(BT_STATUS_SUCCESS, bdaddr, BT_BOND_STATE_BONDED);
2536 BTIF_TRACE_DEBUG("%s: Device is blacklisted for authentication", __func__);
2537 bt_property_t prop;
2538 bt_uuid_t uuid;
2539 char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE;
2540 string_to_uuid(uuid_str, &uuid);
2541 prop.type = BT_PROPERTY_UUIDS;
2542 prop.val = uuid.uu;
2543 prop.len = MAX_UUID_SIZE;
2544 // Also write this to the NVRAM
2545 status = btif_storage_set_remote_device_property(bdaddr, &prop);
2546 ASSERTC(status == BT_STATUS_SUCCESS, "storing remote services failed", status);
2547 // Store fake link for device as bonded in nvram,
2548 // otherwise on device reboot/bt off-on device will not be shown in paired list.
2549 btif_storage_add_bonded_device(bdaddr, link_key, HCI_LKEY_TYPE_UNAUTH_COMB, 0);
2550 // Send the event to the BTIF
2551 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
2552 BT_STATUS_SUCCESS, bdaddr, 1, &prop);
2553 }
2554 }
2555}
2556
2557/*******************************************************************************
2558**
Kim Schulza9eb25c2013-09-30 10:55:52 +02002559** Function btif_dm_hh_open_failed
2560**
2561** Description informs the upper layers if the HH have failed during bonding
2562**
2563** Returns none
2564**
2565*******************************************************************************/
2566
2567void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
2568{
2569 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2570 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2571 {
2572 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2573 }
2574}
2575
2576/*******************************************************************************
2577**
The Android Open Source Project5738f832012-12-12 16:00:35 -08002578** Function btif_dm_remove_bond
2579**
2580** Description Removes bonding with the specified device
2581**
2582** Returns bt_status_t
2583**
2584*******************************************************************************/
2585
2586bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
2587{
2588 bdstr_t bdstr;
2589
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002590 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002591 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2592 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2593
2594 return BT_STATUS_SUCCESS;
2595}
2596
2597/*******************************************************************************
2598**
2599** Function btif_dm_pin_reply
2600**
2601** Description BT legacy pairing - PIN code reply
2602**
2603** Returns bt_status_t
2604**
2605*******************************************************************************/
2606
2607bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2608 uint8_t pin_len, bt_pin_code_t *pin_code)
2609{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002610 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Hemant Gupta831423e2014-01-08 12:42:13 +05302611 if (pin_code == NULL)
2612 return BT_STATUS_FAIL;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002613#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002614
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002615 if (pairing_cb.is_le_only)
2616 {
2617 int i;
2618 UINT32 passkey = 0;
2619 int multi[] = {100000, 10000, 1000, 100, 10,1};
2620 BD_ADDR remote_bd_addr;
2621 bdcpy(remote_bd_addr, bd_addr->address);
2622 for (i = 0; i < 6; i++)
2623 {
2624 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2625 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002626 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002627 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2628
2629 }
2630 else
2631 {
2632 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2633 if (accept)
2634 pairing_cb.pin_code_len = pin_len;
2635 }
2636#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002637 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2638
2639 if (accept)
2640 pairing_cb.pin_code_len = pin_len;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002641#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002642 return BT_STATUS_SUCCESS;
2643}
2644
2645/*******************************************************************************
2646**
2647** Function btif_dm_ssp_reply
2648**
2649** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2650**
2651** Returns bt_status_t
2652**
2653*******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002654bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2655 bt_ssp_variant_t variant, uint8_t accept,
2656 uint32_t passkey)
2657{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08002658 UNUSED(passkey);
2659
The Android Open Source Project5738f832012-12-12 16:00:35 -08002660 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2661 {
2662 /* This is not implemented in the stack.
2663 * For devices with display, this is not needed
2664 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002665 BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002666 return BT_STATUS_FAIL;
2667 }
2668 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002669 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002670#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2671 if (pairing_cb.is_le_only)
2672 {
Satya Calloji7fb0da62015-01-29 20:40:17 -08002673 if(pairing_cb.is_le_nc)
2674 {
2675 BTA_DmBleConfirmReply((UINT8 *)bd_addr->address,accept);
2676 } else {
2677 if (accept)
2678 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2679 else
2680 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2681 }
2682 } else {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002683 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
Satya Calloji7fb0da62015-01-29 20:40:17 -08002684 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002685#else
2686 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2687#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002688 return BT_STATUS_SUCCESS;
2689}
2690
2691/*******************************************************************************
2692**
2693** Function btif_dm_get_adapter_property
2694**
2695** Description Queries the BTA for the adapter property
2696**
2697** Returns bt_status_t
2698**
2699*******************************************************************************/
2700bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2701{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002702 BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002703 switch (prop->type)
2704 {
2705 case BT_PROPERTY_BDNAME:
2706 {
2707 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
VenkatRaghavan VijayaRaghavan4540f592015-02-05 04:40:47 -08002708 strncpy((char *)bd_name->name, (char *)btif_get_default_local_name(),
VenkatRaghavan VijayaRaghavan1d8e6b82015-02-05 22:20:39 -08002709 sizeof(bd_name->name) - 1);
2710 bd_name->name[sizeof(bd_name->name) - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002711 prop->len = strlen((char *)bd_name->name);
2712 }
2713 break;
2714
2715 case BT_PROPERTY_ADAPTER_SCAN_MODE:
2716 {
2717 /* if the storage does not have it. Most likely app never set it. Default is NONE */
2718 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2719 *mode = BT_SCAN_MODE_NONE;
2720 prop->len = sizeof(bt_scan_mode_t);
2721 }
2722 break;
2723
2724 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2725 {
2726 uint32_t *tmt = (uint32_t*)prop->val;
2727 *tmt = 120; /* default to 120s, if not found in NV */
2728 prop->len = sizeof(uint32_t);
2729 }
2730 break;
2731
2732 default:
2733 prop->len = 0;
2734 return BT_STATUS_FAIL;
2735 }
2736 return BT_STATUS_SUCCESS;
2737}
2738
2739/*******************************************************************************
2740**
2741** Function btif_dm_get_remote_services
2742**
2743** Description Start SDP to get remote services
2744**
2745** Returns bt_status_t
2746**
2747*******************************************************************************/
2748bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2749{
2750 bdstr_t bdstr;
2751
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002752 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr)));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002753
2754 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2755 bte_dm_search_services_evt, TRUE);
2756
2757 return BT_STATUS_SUCCESS;
2758}
2759
2760/*******************************************************************************
2761**
Nitin Arorab4365c52015-06-25 18:30:09 -07002762** Function btif_dm_get_remote_services_transport
2763**
2764** Description Start SDP to get remote services by transport
2765**
2766** Returns bt_status_t
2767**
2768*******************************************************************************/
2769bt_status_t btif_dm_get_remote_services_by_transport(bt_bdaddr_t *remote_addr, const int transport)
2770{
2771 BTIF_TRACE_EVENT("%s", __func__);
2772
2773 /* Set the mask extension */
2774 tBTA_SERVICE_MASK_EXT mask_ext;
2775 mask_ext.num_uuid = 0;
2776 mask_ext.p_uuid = NULL;
2777 mask_ext.srvc_mask = BTA_ALL_SERVICE_MASK;
2778
2779 BTA_DmDiscoverByTransport(remote_addr->address, &mask_ext,
2780 bte_dm_search_services_evt, TRUE, transport);
2781
2782 return BT_STATUS_SUCCESS;
2783}
2784
2785/*******************************************************************************
2786**
The Android Open Source Project5738f832012-12-12 16:00:35 -08002787** Function btif_dm_get_remote_service_record
2788**
2789** Description Start SDP to get remote service record
2790**
2791**
2792** Returns bt_status_t
2793*******************************************************************************/
2794bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2795 bt_uuid_t *uuid)
2796{
2797 tSDP_UUID sdp_uuid;
2798 bdstr_t bdstr;
2799
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002800 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr)));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002801
2802 sdp_uuid.len = MAX_UUID_SIZE;
2803 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2804
2805 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2806 bte_dm_remote_service_record_evt, TRUE);
2807
2808 return BT_STATUS_SUCCESS;
2809}
2810
2811void btif_dm_execute_service_request(UINT16 event, char *p_param)
2812{
2813 BOOLEAN b_enable = FALSE;
2814 bt_status_t status;
2815 if (event == BTIF_DM_ENABLE_SERVICE)
2816 {
2817 b_enable = TRUE;
2818 }
2819 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2820 if (status == BT_STATUS_SUCCESS)
2821 {
2822 bt_property_t property;
2823 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2824
2825 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2826 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2827 sizeof(local_uuids), local_uuids);
2828 btif_storage_get_adapter_property(&property);
2829 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2830 BT_STATUS_SUCCESS, 1, &property);
2831 }
2832 return;
2833}
2834
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002835void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2836 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2837{
2838 UINT8 yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2839 /* if local initiated:
2840 ** 1. set DD + MITM
2841 ** if remote initiated:
2842 ** 1. Copy over the auth_req from peer's io_rsp
2843 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2844 ** as a fallback set MITM+GB if peer had MITM set
2845 */
2846 UNUSED (bd_addr);
2847 UNUSED (p_io_cap);
2848 UNUSED (p_oob_data);
2849
2850
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002851 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002852 if(pairing_cb.is_local_initiated)
2853 {
2854 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2855 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2856 }
2857 else if (!is_orig)
2858 {
2859 /* peer initiated paring. They probably know what they want.
2860 ** Copy the mitm from peer device.
2861 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002862 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002863 __FUNCTION__, pairing_cb.auth_req);
2864 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2865
2866 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2867 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2868 *p_auth_req |= BTA_AUTH_SP_YES;
2869 }
2870 else if (yes_no_bit)
2871 {
2872 /* set the general bonding bit for stored device */
2873 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2874 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002875 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002876}
2877
2878void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2879 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2880{
2881 UNUSED (bd_addr);
2882 UNUSED (oob_data);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002883
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002884 if(auth_req & BTA_AUTH_BONDS)
2885 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002886 BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002887 pairing_cb.auth_req = auth_req;
2888 pairing_cb.io_cap = io_cap;
2889 }
2890}
2891
The Android Open Source Project5738f832012-12-12 16:00:35 -08002892#if (BTM_OOB_INCLUDED == TRUE)
2893void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data)
2894{
2895 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2896 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2897 {
2898 *p_oob_data = FALSE;
2899 }
2900 else
2901 {
2902 *p_oob_data = TRUE;
2903 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002904 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 -08002905}
2906#endif /* BTM_OOB_INCLUDED */
2907
2908#ifdef BTIF_DM_OOB_TEST
2909void btif_dm_load_local_oob(void)
2910{
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002911 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002912 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002913 BTIF_TRACE_DEBUG("btif_dm_load_local_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002914 if (prop_oob[0] != '3')
2915 {
2916#if (BTM_OOB_INCLUDED == TRUE)
2917 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2918 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2919 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002920 BTIF_TRACE_DEBUG("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002921 BTA_DmLocalOob();
2922 }
2923#else
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002924 BTIF_TRACE_ERROR("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002925#endif
2926 }
2927}
2928
2929void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2930{
2931 FILE *fp;
2932 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2933 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2934 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002935 char prop_oob[PROPERTY_VALUE_MAX];
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002936 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: valid=%d", valid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002937 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2938 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2939 valid)
2940 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002941 BTIF_TRACE_DEBUG("save local OOB data in memory");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002942 memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2943 memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2944 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002945 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002946 if (prop_oob[0] == '1')
2947 path = path_a;
2948 else if (prop_oob[0] == '2')
2949 path = path_b;
2950 if (path)
2951 {
2952 fp = fopen(path, "wb+");
2953 if (fp == NULL)
2954 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002955 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 -08002956 }
2957 else
2958 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002959 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 -08002960 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2961 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2962 fclose(fp);
2963 }
2964 }
2965 }
2966}
2967BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2968{
2969 char t[128];
2970 FILE *fp;
2971 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2972 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2973 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002974 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002975 BOOLEAN result = FALSE;
2976 bt_bdaddr_t bt_bd_addr;
2977 bdcpy(oob_cb.oob_bdaddr, bd_addr);
2978 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002979 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002980 if (prop_oob[0] == '1')
2981 path = path_b;
2982 else if (prop_oob[0] == '2')
2983 path = path_a;
2984 if (path)
2985 {
2986 fp = fopen(path, "rb");
2987 if (fp == NULL)
2988 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002989 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 -08002990 return FALSE;
2991 }
2992 else
2993 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002994 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob: read OOB data from %s",path);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002995 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2996 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2997 fclose(fp);
2998 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002999 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: TRUE");
The Android Open Source Project5738f832012-12-12 16:00:35 -08003000 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
3001 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
3002 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003003 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003004 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
3005 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
3006 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 -07003007 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: c = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003008 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
3009 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
3010 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 -07003011 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: r = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003012 bdcpy(bt_bd_addr.address, bd_addr);
3013 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
3014 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
3015 result = TRUE;
3016 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003017 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob result=%d",result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003018 return result;
3019}
3020#endif /* BTIF_DM_OOB_TEST */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003021#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
3022
3023static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
3024{
3025 bt_bdaddr_t bd_addr;
3026 bt_bdname_t bd_name;
3027 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08003028 int dev_type;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003029
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003030 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003031
3032 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08003033 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
3034 {
3035 dev_type = BT_DEVICE_TYPE_BLE;
3036 }
3037 btif_dm_update_ble_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
3038 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003039 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
3040 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
3041
3042 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3043 pairing_cb.is_ssp = FALSE;
3044 cod = COD_UNCLASSIFIED;
3045
3046 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
3047 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
3048 p_ssp_key_notif->passkey);
3049}
3050
3051/*******************************************************************************
3052**
3053** Function btif_dm_ble_auth_cmpl_evt
3054**
3055** Description Executes authentication complete event in btif context
3056**
3057** Returns void
3058**
3059*******************************************************************************/
3060static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
3061{
3062 /* Save link key, if not temporary */
3063 bt_bdaddr_t bd_addr;
3064 bt_status_t status = BT_STATUS_FAIL;
3065 bt_bond_state_t state = BT_BOND_STATE_NONE;
3066
3067 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
3068 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
3069 {
3070 /* store keys */
3071 }
3072 if (p_auth_cmpl->success)
3073 {
3074 status = BT_STATUS_SUCCESS;
3075 state = BT_BOND_STATE_BONDED;
Satya Calloji7cb76b42015-05-07 09:45:00 -07003076 int addr_type;
3077 bt_bdaddr_t bdaddr;
3078 bdcpy(bdaddr.address, p_auth_cmpl->bd_addr);
3079 if (btif_storage_get_remote_addr_type(&bdaddr, &addr_type) != BT_STATUS_SUCCESS)
3080 btif_storage_set_remote_addr_type(&bdaddr, p_auth_cmpl->addr_type);
Pavlin Radoslavov65f9c042015-07-16 17:40:28 -07003081
3082 /* Test for temporary bonding */
3083 if (btm_get_bond_type_dev(p_auth_cmpl->bd_addr) == BOND_TYPE_TEMPORARY) {
3084 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
3085 __func__);
3086 btif_storage_remove_bonded_device(&bdaddr);
3087 state = BT_BOND_STATE_NONE;
3088 } else {
3089 btif_dm_save_ble_bonding_keys();
3090 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003091 BTA_GATTC_Refresh(bd_addr.address);
Nitin Arorab4365c52015-06-25 18:30:09 -07003092 btif_dm_get_remote_services_by_transport(&bd_addr, BTA_GATT_TRANSPORT_LE);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003093 }
3094 else
3095 {
3096 /*Map the HCI fail reason to bt status */
3097 switch (p_auth_cmpl->fail_reason)
3098 {
Priti Aghera156c52b2014-07-09 14:58:19 -07003099 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
3100 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
3101 btif_dm_remove_ble_bonding_keys();
3102 status = BT_STATUS_AUTH_FAILURE;
3103 break;
3104 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
3105 status = BT_STATUS_AUTH_REJECTED;
3106 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003107 default:
Andre Eisenbachca22ac42013-02-13 17:02:11 +09003108 btif_dm_remove_ble_bonding_keys();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003109 status = BT_STATUS_FAIL;
3110 break;
3111 }
3112 }
3113 bond_state_changed(status, &bd_addr, state);
3114}
3115
3116
3117
3118void btif_dm_load_ble_local_keys(void)
3119{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003120 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
3121
3122 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
3123 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
3124 {
3125 ble_local_key_cb.is_er_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003126 BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003127 }
3128
3129 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
3130 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
3131 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
3132 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
3133 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
3134 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
3135 {
3136 ble_local_key_cb.is_id_keys_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003137 BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003138 }
3139
3140}
3141void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
3142 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
3143{
3144 if (ble_local_key_cb.is_er_rcvd )
3145 {
3146 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
3147 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
3148 }
3149
3150 if (ble_local_key_cb.is_id_keys_rcvd)
3151 {
3152 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
3153 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
3154 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
3155 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
3156 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003157 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003158}
3159
3160void btif_dm_save_ble_bonding_keys(void)
3161{
3162
3163 bt_bdaddr_t bd_addr;
3164
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003165 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003166
3167 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3168
3169 if (pairing_cb.ble.is_penc_key_rcvd)
3170 {
3171 btif_storage_add_ble_bonding_key(&bd_addr,
3172 (char *) &pairing_cb.ble.penc_key,
3173 BTIF_DM_LE_KEY_PENC,
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07003174 sizeof(tBTM_LE_PENC_KEYS));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003175 }
3176
3177 if (pairing_cb.ble.is_pid_key_rcvd)
3178 {
3179 btif_storage_add_ble_bonding_key(&bd_addr,
Andre Eisenbach5e808462014-10-21 12:37:53 -07003180 (char *) &pairing_cb.ble.pid_key,
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003181 BTIF_DM_LE_KEY_PID,
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07003182 sizeof(tBTM_LE_PID_KEYS));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003183 }
3184
3185
3186 if (pairing_cb.ble.is_pcsrk_key_rcvd)
3187 {
3188 btif_storage_add_ble_bonding_key(&bd_addr,
3189 (char *) &pairing_cb.ble.pcsrk_key,
3190 BTIF_DM_LE_KEY_PCSRK,
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07003191 sizeof(tBTM_LE_PCSRK_KEYS));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003192 }
3193
3194
3195 if (pairing_cb.ble.is_lenc_key_rcvd)
3196 {
3197 btif_storage_add_ble_bonding_key(&bd_addr,
3198 (char *) &pairing_cb.ble.lenc_key,
3199 BTIF_DM_LE_KEY_LENC,
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07003200 sizeof(tBTM_LE_LENC_KEYS));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003201 }
3202
3203 if (pairing_cb.ble.is_lcsrk_key_rcvd)
3204 {
3205 btif_storage_add_ble_bonding_key(&bd_addr,
3206 (char *) &pairing_cb.ble.lcsrk_key,
3207 BTIF_DM_LE_KEY_LCSRK,
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07003208 sizeof(tBTM_LE_LCSRK_KEYS));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003209 }
3210
Satya Calloji444a8da2015-03-06 10:38:22 -08003211 if (pairing_cb.ble.is_lidk_key_rcvd)
3212 {
3213 btif_storage_add_ble_bonding_key(&bd_addr,
3214 NULL,
3215 BTIF_DM_LE_KEY_LID,
3216 0);
3217 }
3218
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003219}
3220
3221
3222void btif_dm_remove_ble_bonding_keys(void)
3223{
3224 bt_bdaddr_t bd_addr;
3225
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003226 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003227
3228 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3229 btif_storage_remove_ble_bonding_keys(&bd_addr);
3230}
3231
3232
3233/*******************************************************************************
3234**
3235** Function btif_dm_ble_sec_req_evt
3236**
3237** Description Eprocess security request event in btif context
3238**
3239** Returns void
3240**
3241*******************************************************************************/
3242void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
3243{
3244 bt_bdaddr_t bd_addr;
3245 bt_bdname_t bd_name;
3246 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08003247 int dev_type;
3248
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003249 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003250
3251 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3252 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003253 BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003254 return;
3255 }
3256
3257 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08003258 if (!btif_get_device_type(p_ble_req->bd_addr, &dev_type))
3259 {
3260 dev_type = BT_DEVICE_TYPE_BLE;
3261 }
3262 btif_dm_update_ble_remote_properties(p_ble_req->bd_addr, p_ble_req->bd_name,
3263 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003264
3265 bdcpy(bd_addr.address, p_ble_req->bd_addr);
3266 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
3267
3268 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3269
Andre Eisenbach89363762015-01-26 13:49:36 -08003270 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003271 pairing_cb.is_le_only = TRUE;
Satya Calloji7fb0da62015-01-29 20:40:17 -08003272 pairing_cb.is_le_nc = FALSE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003273 pairing_cb.is_ssp = TRUE;
Pavlin Radoslavov65f9c042015-07-16 17:40:28 -07003274 btm_set_bond_type_dev(p_ble_req->bd_addr, pairing_cb.bond_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003275
3276 cod = COD_UNCLASSIFIED;
3277
3278 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
3279 BT_SSP_VARIANT_CONSENT, 0);
3280}
3281
3282
3283
3284/*******************************************************************************
3285**
3286** Function btif_dm_ble_passkey_req_evt
3287**
3288** Description Executes pin request event in btif context
3289**
3290** Returns void
3291**
3292*******************************************************************************/
3293static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
3294{
3295 bt_bdaddr_t bd_addr;
3296 bt_bdname_t bd_name;
3297 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08003298 int dev_type;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003299
3300 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08003301 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
3302 {
3303 dev_type = BT_DEVICE_TYPE_BLE;
3304 }
3305 btif_dm_update_ble_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,
3306 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003307
3308 bdcpy(bd_addr.address, p_pin_req->bd_addr);
3309 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
3310
3311 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3312 pairing_cb.is_le_only = TRUE;
3313
3314 cod = COD_UNCLASSIFIED;
3315
3316 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
Casper Bonde818d0f22015-05-21 11:08:45 +02003317 &bd_addr, &bd_name, cod, FALSE);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003318}
Satya Calloji444a8da2015-03-06 10:38:22 -08003319static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF *p_notif_req)
3320{
3321 /* TODO implement key notification for numeric comparison */
3322 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003323
Satya Calloji444a8da2015-03-06 10:38:22 -08003324 /* Remote name update */
3325 btif_update_remote_properties(p_notif_req->bd_addr , p_notif_req->bd_name,
3326 NULL, BT_DEVICE_TYPE_BLE);
3327
3328 bt_bdaddr_t bd_addr;
3329 bdcpy(bd_addr.address, p_notif_req->bd_addr);
3330
3331 bt_bdname_t bd_name;
3332 memcpy(bd_name.name, p_notif_req->bd_name, BD_NAME_LEN);
3333
3334 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3335 pairing_cb.is_ssp = FALSE;
Satya Calloji7fb0da62015-01-29 20:40:17 -08003336 pairing_cb.is_le_only = TRUE;
3337 pairing_cb.is_le_nc = TRUE;
Satya Calloji444a8da2015-03-06 10:38:22 -08003338
3339 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
3340 COD_UNCLASSIFIED, BT_SSP_VARIANT_PASSKEY_CONFIRMATION,
3341 p_notif_req->passkey);
3342}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003343
3344void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
3345 tBT_DEVICE_TYPE dev_type)
3346{
3347 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
3348}
3349
3350static void btif_dm_ble_tx_test_cback(void *p)
3351{
3352 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
3353 (char *)p, 1, NULL);
3354}
3355
3356static void btif_dm_ble_rx_test_cback(void *p)
3357{
3358 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
3359 (char *)p, 1, NULL);
3360}
3361
3362static void btif_dm_ble_test_end_cback(void *p)
3363{
3364 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
3365 (char *)p, 3, NULL);
3366}
3367/*******************************************************************************
3368**
3369** Function btif_le_test_mode
3370**
3371** Description Sends a HCI BLE Test command to the Controller
3372**
3373** Returns BT_STATUS_SUCCESS on success
3374**
3375*******************************************************************************/
3376bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
3377{
3378 switch (opcode) {
3379 case HCI_BLE_TRANSMITTER_TEST:
3380 if (len != 3) return BT_STATUS_PARM_INVALID;
3381 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
3382 break;
3383 case HCI_BLE_RECEIVER_TEST:
3384 if (len != 1) return BT_STATUS_PARM_INVALID;
3385 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
3386 break;
3387 case HCI_BLE_TEST_END:
3388 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
3389 break;
3390 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003391 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003392 return BT_STATUS_UNSUPPORTED;
3393 }
3394 return BT_STATUS_SUCCESS;
3395}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003396#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08003397
3398void btif_dm_on_disable()
3399{
Nitin Srivastavab30e7122014-08-25 18:17:48 +05303400 /* Cleanup static variables.*/
3401 num_active_br_edr_links = 0;
3402 num_active_le_links = 0;
3403
The Android Open Source Project5738f832012-12-12 16:00:35 -08003404 /* cancel any pending pairing requests */
3405 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3406 {
3407 bt_bdaddr_t bd_addr;
3408
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003409 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003410 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3411 btif_dm_cancel_bond(&bd_addr);
3412 }
3413}
Matthew Xie1e5109b2012-11-09 18:26:26 -08003414
Satya Callojie5ba8842014-07-03 17:18:02 -07003415/*******************************************************************************
3416**
3417** Function btif_dm_read_energy_info
3418**
3419** Description Reads the energy info from controller
3420**
3421** Returns void
3422**
3423*******************************************************************************/
3424void btif_dm_read_energy_info()
3425{
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003426#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07003427 BTA_DmBleGetEnergyInfo(bta_energy_info_cb);
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003428#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07003429}
3430
Matthew Xie1e5109b2012-11-09 18:26:26 -08003431static char* btif_get_default_local_name() {
3432 if (btif_default_local_name[0] == '\0')
3433 {
3434 int max_len = sizeof(btif_default_local_name) - 1;
3435 if (BTM_DEF_LOCAL_NAME[0] != '\0')
3436 {
3437 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
3438 }
3439 else
3440 {
3441 char prop_model[PROPERTY_VALUE_MAX];
3442 property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
3443 strncpy(btif_default_local_name, prop_model, max_len);
3444 }
3445 btif_default_local_name[max_len] = '\0';
3446 }
3447 return btif_default_local_name;
3448}
Nitin Srivastavab30e7122014-08-25 18:17:48 +05303449/*******************************************************************************
3450**
3451** Function btif_dm_get_br_edr_links.
3452**
3453** Description Returns number of active BR/EDR links.
3454**
3455** Returns UINT16
3456**
3457*******************************************************************************/
3458UINT16 btif_dm_get_br_edr_links()
3459{
3460 BTIF_TRACE_DEBUG("BR/EDR Link count: %d", num_active_br_edr_links);
3461 return num_active_br_edr_links;
3462}
3463
3464/*******************************************************************************
3465**
3466** Function btif_dm_get_le_links.
3467**
3468** Description Returns number of active LE links.
3469**
3470** Returns UINT16
3471**
3472*******************************************************************************/
3473UINT16 btif_dm_get_le_links()
3474{
3475 BTIF_TRACE_DEBUG("LE Link count: %d", num_active_le_links);
3476 return num_active_le_links;
3477}
3478