blob: 7b730bfca66e9d8119b82f7825df35432c9bd63e [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
Priti Agherac0edf9f2014-06-26 11:23:51 -0700169typedef struct
170{
171 unsigned int manufact_id;
172}skip_sdp_entry_t;
173
The Android Open Source Project5738f832012-12-12 16:00:35 -0800174#define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id))
175
Priti Agherac0edf9f2014-06-26 11:23:51 -0700176#define MAX_SDP_BL_ENTRIES 3
177#define UUID_HUMAN_INTERFACE_DEVICE "00001124-0000-1000-8000-00805f9b34fb"
178
179static skip_sdp_entry_t sdp_blacklist[] = {{76}}; //Apple Mouse and Keyboard
180
The Android Open Source Project5738f832012-12-12 16:00:35 -0800181/* This flag will be true if HCI_Inquiry is in progress */
182static BOOLEAN btif_dm_inquiry_in_progress = FALSE;
183
Priti Agherac0edf9f2014-06-26 11:23:51 -0700184
185
Matthew Xie1e5109b2012-11-09 18:26:26 -0800186/************************************************************************************
187** Static variables
188************************************************************************************/
189static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'};
190
The Android Open Source Project5738f832012-12-12 16:00:35 -0800191/******************************************************************************
192** Static functions
193******************************************************************************/
194static btif_dm_pairing_cb_t pairing_cb;
195static btif_dm_oob_cb_t oob_cb;
196static void btif_dm_generic_evt(UINT16 event, char* p_param);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700197static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800198static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name);
199static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
200 DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800201#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
202static btif_dm_local_key_cb_t ble_local_key_cb;
203static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif);
204static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl);
205static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req);
Satya Calloji444a8da2015-03-06 10:38:22 -0800206static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF *p_notif_req) ;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800207#endif
Satya Calloji6e2d9db2014-07-08 16:18:58 -0700208
209static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
210 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
211 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status);
212
Matthew Xie1e5109b2012-11-09 18:26:26 -0800213static char* btif_get_default_local_name();
The Android Open Source Project5738f832012-12-12 16:00:35 -0800214/******************************************************************************
215** Externs
216******************************************************************************/
217extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
218extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
219extern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
Rakesh Iyer9c8dfac2015-04-08 12:25:37 -0700220extern bt_status_t btif_av_sink_execute_service(BOOLEAN b_enable);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800221extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
Hemant Gupta10256872013-08-19 18:33:01 +0530222extern bt_status_t btif_hf_client_execute_service(BOOLEAN b_enable);
Kim Schulz8372aa52015-03-25 10:39:40 +0100223extern bt_status_t btif_sdp_execute_service(BOOLEAN b_enable);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800224extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
Hemant Gupta94c15ec2013-11-11 12:33:44 +0530225extern bt_status_t btif_hd_execute_service(BOOLEAN b_enable);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -0700226extern void bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800227
228
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**
447** Function check_sdp_bl
448**
449** Description Checks if a given device is blacklisted to skip sdp
450**
451** Parameters skip_sdp_entry
452**
453** Returns TRUE if the device is present in blacklist, else FALSE
454**
455*******************************************************************************/
456BOOLEAN check_sdp_bl(const bt_bdaddr_t *remote_bdaddr)
457{
Priti Agherac0edf9f2014-06-26 11:23:51 -0700458 UINT16 manufacturer = 0;
459 UINT8 lmp_ver = 0;
460 UINT16 lmp_subver = 0;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700461 bt_property_t prop_name;
462 bt_remote_version_t info;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700463
464
465 if (remote_bdaddr == NULL)
466 return FALSE;
467
468/* fetch additional info about remote device used in iop query */
Sharvil Nanavatif1c764f2015-02-23 17:31:48 -0800469 BTM_ReadRemoteVersion(*(BD_ADDR*)remote_bdaddr, &lmp_ver,
Priti Agherac0edf9f2014-06-26 11:23:51 -0700470 &manufacturer, &lmp_subver);
471
472
473
474 /* if not available yet, try fetching from config database */
475 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO,
476 sizeof(bt_remote_version_t), &info);
477
478 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
479 &prop_name) != BT_STATUS_SUCCESS)
480 {
481
482 return FALSE;
483 }
484 manufacturer = info.manufacturer;
485
486 for (int i = 0; i < MAX_SDP_BL_ENTRIES; i++)
487 {
488 if (manufacturer == sdp_blacklist[i].manufact_id)
489 return TRUE;
490 }
491 return FALSE;
492}
493
494
The Android Open Source Project5738f832012-12-12 16:00:35 -0800495static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
496{
Chaojing Sune2805532015-04-22 13:40:21 -0700497 // Send bonding state only once - based on outgoing/incoming we may receive duplicates
498 if ((pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING))
499 {
500 // Cross key pairing so send callback for static address
Andre Eisenbache87ef6d2015-05-01 13:14:59 -0700501 if (!bdaddr_is_empty(&pairing_cb.static_bdaddr))
502 {
Chaojing Sune2805532015-04-22 13:40:21 -0700503 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
Andre Eisenbache87ef6d2015-05-01 13:14:59 -0700504 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800505 return;
Chaojing Sune2805532015-04-22 13:40:21 -0700506 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800507
Andre Eisenbach89363762015-01-26 13:49:36 -0800508 if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
Chaojing Sune2805532015-04-22 13:40:21 -0700509 state = BT_BOND_STATE_NONE;
510
511 BTIF_TRACE_DEBUG("%s: state=%d, prev_state=%d, sdp_attempts = %d", __func__,
512 state, pairing_cb.state, pairing_cb.sdp_attempts);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800513
514 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
515
516 if (state == BT_BOND_STATE_BONDING)
517 {
518 pairing_cb.state = state;
519 bdcpy(pairing_cb.bd_addr, bd_addr->address);
Chaojing Sune2805532015-04-22 13:40:21 -0700520 } else {
521 if (!pairing_cb.sdp_attempts)
522 memset(&pairing_cb, 0, sizeof(pairing_cb));
523 else
524 BTIF_TRACE_DEBUG("%s: BR-EDR service discovery active", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800525 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800526}
527
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800528/* store remote version in bt config to always have access
529 to it post pairing*/
530static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
531{
532 bt_property_t property;
533 UINT8 lmp_ver = 0;
534 UINT16 lmp_subver = 0;
535 UINT16 mfct_set = 0;
536 tBTM_STATUS btm_status;
537 bt_remote_version_t info;
538 bt_status_t status;
539 bdstr_t bdstr;
540
541 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
542 &mfct_set, &lmp_subver);
543
Sharvil Nanavati44802762014-12-23 23:08:58 -0800544 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 -0800545 lmp_ver, mfct_set, lmp_subver);
546
547 if (btm_status == BTM_SUCCESS)
548 {
Chaojing Sune2805532015-04-22 13:40:21 -0700549 // Always update cache to ensure we have availability whenever BTM API is not populated
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800550 info.manufacturer = mfct_set;
551 info.sub_ver = lmp_subver;
552 info.version = lmp_ver;
553 BTIF_STORAGE_FILL_PROPERTY(&property,
554 BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
555 &info);
556 status = btif_storage_set_remote_device_property(p_bd, &property);
557 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
558 }
559}
560
The Android Open Source Project5738f832012-12-12 16:00:35 -0800561
562static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
563 DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
564{
565 int num_properties = 0;
566 bt_property_t properties[3];
567 bt_bdaddr_t bdaddr;
568 bt_status_t status;
569 UINT32 cod;
570 bt_device_type_t dev_type;
571
572 memset(properties, 0, sizeof(properties));
573 bdcpy(bdaddr.address, bd_addr);
574
575 /* remote name */
576 if (strlen((const char *) bd_name))
577 {
578 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
579 BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
580 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
581 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
582 num_properties++;
583 }
584
585 /* class of device */
586 cod = devclass2uint(dev_class);
Chris Mantonf8027002015-03-12 09:22:48 -0700587 BTIF_TRACE_DEBUG("%s cod is 0x%06x", __func__, cod);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800588 if ( cod == 0) {
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530589 /* Try to retrieve cod from storage */
Chris Mantonf8027002015-03-12 09:22:48 -0700590 BTIF_TRACE_DEBUG("%s cod is 0, checking cod from storage", __func__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530591 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
592 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
593 status = btif_storage_get_remote_device_property(&bdaddr, &properties[num_properties]);
Chris Mantonf8027002015-03-12 09:22:48 -0700594 BTIF_TRACE_DEBUG("%s cod retrieved from storage is 0x%06x", __func__, cod);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530595 if ( cod == 0) {
Chris Mantonf8027002015-03-12 09:22:48 -0700596 BTIF_TRACE_DEBUG("%s cod is again 0, set as unclassified", __func__);
Hemant Gupta87b7cce2013-11-28 13:07:10 +0530597 cod = COD_UNCLASSIFIED;
598 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800599 }
600
601 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
602 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
603 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
604 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
605 num_properties++;
606
607 /* device type */
Chaojing Sune2805532015-04-22 13:40:21 -0700608 bt_property_t prop_name;
609 uint8_t remote_dev_type;
610 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_TYPE_OF_DEVICE,
611 sizeof(uint8_t), &remote_dev_type);
612 if (btif_storage_get_remote_device_property(&bdaddr, &prop_name) == BT_STATUS_SUCCESS)
613 dev_type = remote_dev_type | device_type;
614 else
615 dev_type = device_type;
616
The Android Open Source Project5738f832012-12-12 16:00:35 -0800617 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
618 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
619 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
620 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
621 num_properties++;
622
623 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
624 status, &bdaddr, num_properties, properties);
625}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800626
627/*******************************************************************************
628**
629** Function btif_dm_cb_hid_remote_name
630**
631** Description Remote name callback for HID device. Called in btif context
632** Special handling for HID devices
633**
634** Returns void
635**
636*******************************************************************************/
637static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
638{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700639 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 -0800640 if (pairing_cb.state == BT_BOND_STATE_BONDING)
641 {
642 bt_bdaddr_t remote_bd;
643
644 bdcpy(remote_bd.address, pairing_cb.bd_addr);
645
646 if (p_remote_name->status == BTM_SUCCESS)
647 {
648 bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
649 }
650 else
651 bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
652 }
653}
654
The Android Open Source Project5738f832012-12-12 16:00:35 -0800655/*******************************************************************************
656**
657** Function btif_dm_cb_create_bond
658**
659** Description Create bond initiated from the BTIF thread context
660** Special handling for HID devices
661**
662** Returns void
663**
664*******************************************************************************/
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700665static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800666{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800667 BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800668 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800669
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800670#if BLE_INCLUDED == TRUE
671 int device_type;
672 int addr_type;
673 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -0700674 bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr));
Matthew Xie64c54792014-09-16 00:55:03 -0700675 if (transport == BT_TRANSPORT_LE)
676 {
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700677 if (!btif_config_get_int((char const *)&bdstr,"DevType", &device_type))
Matthew Xie64c54792014-09-16 00:55:03 -0700678 {
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700679 btif_config_set_int(bdstr, "DevType", BT_DEVICE_TYPE_BLE);
Matthew Xie64c54792014-09-16 00:55:03 -0700680 }
681 if (btif_storage_get_remote_addr_type(bd_addr, &addr_type) != BT_STATUS_SUCCESS)
682 {
683 btif_storage_set_remote_addr_type(bd_addr, BLE_ADDR_PUBLIC);
684 }
685 }
Sharvil Nanavati9d52f882014-08-19 09:50:18 -0700686 if((btif_config_get_int((char const *)&bdstr,"DevType", &device_type) &&
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800687 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
Chaojing Sune2805532015-04-22 13:40:21 -0700688 (device_type & BT_DEVICE_TYPE_BLE) == BT_DEVICE_TYPE_BLE) || (transport == BT_TRANSPORT_LE))
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800689 {
Chaojing Sune2805532015-04-22 13:40:21 -0700690 BTA_DmAddBleDevice(bd_addr->address, addr_type, device_type);
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800691 }
692#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800693
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800694#if BLE_INCLUDED == TRUE
Chaojing Sune2805532015-04-22 13:40:21 -0700695 if(is_hid && (device_type & BT_DEVICE_TYPE_BLE) == 0)
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800696#else
697 if(is_hid)
698#endif
699 {
700 int status;
701 status = btif_hh_connect(bd_addr);
702 if(status != BT_STATUS_SUCCESS)
703 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800704 }
705 else
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800706 {
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700707 BTA_DmBondByTransport((UINT8 *)bd_addr->address, transport);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800708 }
709 /* Track originator of bond creation */
710 pairing_cb.is_local_initiated = TRUE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800711
712}
713
714/*******************************************************************************
715**
716** Function btif_dm_cb_remove_bond
717**
718** Description remove bond initiated from the BTIF thread context
719** Special handling for HID devices
720**
721** Returns void
722**
723*******************************************************************************/
724void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
725{
The Android Open Source Project5738f832012-12-12 16:00:35 -0800726 /*special handling for HID devices */
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700727 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there
728 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
729#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
730 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
731#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800732 {
Chaojing Sune2805532015-04-22 13:40:21 -0700733 BTIF_TRACE_DEBUG("%s: Removing HH device", __func__);
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700734 BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800735 }
736}
737
738/*******************************************************************************
739**
Andre Eisenbach249f6002014-06-18 12:20:37 -0700740** Function btif_dm_get_connection_state
741**
742** Description Returns whether the remote device is currently connected
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800743** and whether encryption is active for the connection
Andre Eisenbach249f6002014-06-18 12:20:37 -0700744**
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800745** Returns 0 if not connected; 1 if connected and > 1 if connection is
746** encrypted
Andre Eisenbach249f6002014-06-18 12:20:37 -0700747**
748*******************************************************************************/
749uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr)
750{
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800751 uint8_t *bda = (uint8_t*)bd_addr->address;
752 uint16_t rc = BTA_DmGetConnectionState(bda);
753
754 if (rc != 0)
755 {
756 uint8_t flags = 0;
757
758 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_BR_EDR);
759 BTIF_TRACE_DEBUG("%s: security flags (BR/EDR)=0x%02x", __FUNCTION__, flags);
760 if (flags & BTM_SEC_FLAG_ENCRYPTED)
761 rc |= ENCRYPTED_BREDR;
762
763 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_LE);
764 BTIF_TRACE_DEBUG("%s: security flags (LE)=0x%02x", __FUNCTION__, flags);
765 if (flags & BTM_SEC_FLAG_ENCRYPTED)
766 rc |= ENCRYPTED_LE;
767 }
768
769 return rc;
Andre Eisenbach249f6002014-06-18 12:20:37 -0700770}
771
772/*******************************************************************************
773**
The Android Open Source Project5738f832012-12-12 16:00:35 -0800774** Function search_devices_copy_cb
775**
776** Description Deep copy callback for search devices event
777**
778** Returns void
779**
780*******************************************************************************/
781static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
782{
783 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
784 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
785
786 if (!p_src)
787 return;
788
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700789 BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800790 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
791 switch (event)
792 {
793 case BTA_DM_INQ_RES_EVT:
794 {
795 if (p_src_data->inq_res.p_eir)
796 {
797 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
798 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
799 }
800 }
801 break;
802
803 case BTA_DM_DISC_RES_EVT:
804 {
805 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
806 {
807 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
808 memcpy(p_dest_data->disc_res.p_raw_data,
809 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
810 }
811 }
812 break;
813 }
814}
815
816static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
817{
818 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
819 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
820
821 if (!p_src)
822 return;
823 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
824 switch (event)
825 {
826 case BTA_DM_DISC_RES_EVT:
827 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530828 if (p_src_data->disc_res.result == BTA_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800829 {
Kausik Sinnaswamy95664a92013-05-03 15:02:50 +0530830 if (p_src_data->disc_res.num_uuids > 0)
831 {
832 p_dest_data->disc_res.p_uuid_list =
833 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
834 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
835 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
836 GKI_freebuf(p_src_data->disc_res.p_uuid_list);
837 }
838 if (p_src_data->disc_res.p_raw_data != NULL)
839 {
840 GKI_freebuf(p_src_data->disc_res.p_raw_data);
841 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800842 }
843 } break;
844 }
845}
846/******************************************************************************
847**
848** BTIF DM callback events
849**
850*****************************************************************************/
851
852/*******************************************************************************
853**
854** Function btif_dm_pin_req_evt
855**
856** Description Executes pin request event in btif context
857**
858** Returns void
859**
860*******************************************************************************/
861static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
862{
863 bt_bdaddr_t bd_addr;
864 bt_bdname_t bd_name;
865 UINT32 cod;
866 bt_pin_code_t pin_code;
Matthew Xie86f97ed2014-11-10 10:24:46 -0800867 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800868
869 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -0800870 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
871 {
872 dev_type = BT_DEVICE_TYPE_BREDR;
873 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800874 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -0800875 p_pin_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800876
877 bdcpy(bd_addr.address, p_pin_req->bd_addr);
878 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
879
880 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
881
882 cod = devclass2uint(p_pin_req->dev_class);
883
Chris Mantonf8027002015-03-12 09:22:48 -0700884 if (cod == 0) {
885 BTIF_TRACE_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800886 cod = COD_UNCLASSIFIED;
887 }
888
889 /* check for auto pair possiblity only if bond was initiated by local device */
Casper Bonde818d0f22015-05-21 11:08:45 +0200890 if (pairing_cb.is_local_initiated && (p_pin_req->min_16_digit == FALSE))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800891 {
892 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
893 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
894 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
895 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
896 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
897 check_cod(&bd_addr, COD_HID_POINTING))
898 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700899 BTIF_TRACE_DEBUG("%s()cod matches for auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800900 /* Check if this device can be auto paired */
901 if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) &&
902 (pairing_cb.autopair_attempts == 0))
903 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700904 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800905 pin_code.pin[0] = 0x30;
906 pin_code.pin[1] = 0x30;
907 pin_code.pin[2] = 0x30;
908 pin_code.pin[3] = 0x30;
909
910 pairing_cb.autopair_attempts++;
911 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
912 return;
913 }
914 }
915 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
916 check_cod(&bd_addr, COD_HID_COMBO))
917 {
918 if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) &&
919 (pairing_cb.autopair_attempts == 0))
920 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700921 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800922 pin_code.pin[0] = 0x30;
923 pin_code.pin[1] = 0x30;
924 pin_code.pin[2] = 0x30;
925 pin_code.pin[3] = 0x30;
926
927 pairing_cb.autopair_attempts++;
928 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
929 return;
930 }
931 }
932 }
933 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
Casper Bonde818d0f22015-05-21 11:08:45 +0200934 &bd_addr, &bd_name, cod, p_pin_req->min_16_digit);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800935}
936
937/*******************************************************************************
938**
939** Function btif_dm_ssp_cfm_req_evt
940**
941** Description Executes SSP confirm request event in btif context
942**
943** Returns void
944**
945*******************************************************************************/
946static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
947{
948 bt_bdaddr_t bd_addr;
949 bt_bdname_t bd_name;
950 UINT32 cod;
951 BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
Matthew Xie86f97ed2014-11-10 10:24:46 -0800952 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800953
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700954 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800955
956 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -0800957 if (!btif_get_device_type(p_ssp_cfm_req->bd_addr, &dev_type))
958 {
959 dev_type = BT_DEVICE_TYPE_BREDR;
960 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800961 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -0800962 p_ssp_cfm_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800963
964 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
965 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
966
967 /* Set the pairing_cb based on the local & remote authentication requirements */
968 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
969
970 /* if just_works and bonding bit is not set treat this as temporary */
971 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 -0800972 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
973 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
Andre Eisenbach89363762015-01-26 13:49:36 -0800974 pairing_cb.bond_type = BOND_TYPE_TEMPORARY;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800975 else
Andre Eisenbach89363762015-01-26 13:49:36 -0800976 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800977
Pavlin Radoslavov65f9c042015-07-16 17:40:28 -0700978 btm_set_bond_type_dev(p_ssp_cfm_req->bd_addr, pairing_cb.bond_type);
979
The Android Open Source Project5738f832012-12-12 16:00:35 -0800980 pairing_cb.is_ssp = TRUE;
981
982 /* If JustWorks auto-accept */
983 if (p_ssp_cfm_req->just_works)
984 {
985 /* Pairing consent for JustWorks needed if:
Andre Eisenbachaf753a22015-06-08 20:43:00 -0700986 * 1. Incoming (non-temporary) pairing is detected AND
The Android Open Source Project5738f832012-12-12 16:00:35 -0800987 * 2. local IO capabilities are DisplayYesNo AND
988 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
989 */
Andre Eisenbachaf753a22015-06-08 20:43:00 -0700990 if (is_incoming && pairing_cb.bond_type != BOND_TYPE_TEMPORARY &&
991 ((p_ssp_cfm_req->loc_io_caps == HCI_IO_CAP_DISPLAY_YESNO) &&
992 (p_ssp_cfm_req->rmt_io_caps == HCI_IO_CAP_DISPLAY_ONLY ||
993 p_ssp_cfm_req->rmt_io_caps == HCI_IO_CAP_NO_IO)))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800994 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -0700995 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 -0800996 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
997 }
998 else
999 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001000 BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001001 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
1002 return;
1003 }
1004 }
1005
1006 cod = devclass2uint(p_ssp_cfm_req->dev_class);
1007
Chris Mantonf8027002015-03-12 09:22:48 -07001008 if (cod == 0) {
1009 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001010 cod = COD_UNCLASSIFIED;
1011 }
1012
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001013 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001014 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
1015 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
1016 p_ssp_cfm_req->num_val);
1017}
1018
1019static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
1020{
1021 bt_bdaddr_t bd_addr;
1022 bt_bdname_t bd_name;
1023 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08001024 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001025
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001026 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001027
1028 /* Remote properties update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08001029 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
1030 {
1031 dev_type = BT_DEVICE_TYPE_BREDR;
1032 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001033 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
Matthew Xie86f97ed2014-11-10 10:24:46 -08001034 p_ssp_key_notif->dev_class, (tBT_DEVICE_TYPE) dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001035
1036 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
1037 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
1038
1039 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1040 pairing_cb.is_ssp = TRUE;
1041 cod = devclass2uint(p_ssp_key_notif->dev_class);
1042
Chris Mantonf8027002015-03-12 09:22:48 -07001043 if (cod == 0) {
1044 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001045 cod = COD_UNCLASSIFIED;
1046 }
1047
1048 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
1049 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
1050 p_ssp_key_notif->passkey);
1051}
1052/*******************************************************************************
1053**
1054** Function btif_dm_auth_cmpl_evt
1055**
1056** Description Executes authentication complete event in btif context
1057**
1058** Returns void
1059**
1060*******************************************************************************/
1061static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
1062{
1063 /* Save link key, if not temporary */
1064 bt_bdaddr_t bd_addr;
1065 bt_status_t status = BT_STATUS_FAIL;
1066 bt_bond_state_t state = BT_BOND_STATE_NONE;
Priti Agherac0edf9f2014-06-26 11:23:51 -07001067 BOOLEAN skip_sdp = FALSE;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001068
Chaojing Sune2805532015-04-22 13:40:21 -07001069 BTIF_TRACE_DEBUG("%s: bond state=%d", __func__, pairing_cb.state);
1070
The Android Open Source Project5738f832012-12-12 16:00:35 -08001071 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
1072 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
1073 {
Srinu Jella21331c62015-06-16 19:38:45 +05301074 if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) ||
1075 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
1076 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) ||
1077 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB_P_256) ||
1078 pairing_cb.bond_type == BOND_TYPE_PERSISTENT)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001079 {
1080 bt_status_t ret;
Andre Eisenbach89363762015-01-26 13:49:36 -08001081 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, bond_type=%d",
1082 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001083 ret = btif_storage_add_bonded_device(&bd_addr,
1084 p_auth_cmpl->key, p_auth_cmpl->key_type,
1085 pairing_cb.pin_code_len);
1086 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
1087 }
1088 else
1089 {
Andre Eisenbach89363762015-01-26 13:49:36 -08001090 BTIF_TRACE_DEBUG("%s: Temporary key. Not storing. key_type=0x%x, bond_type=%d",
1091 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1092 if(pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
Hemant Guptab820aec2013-12-24 19:59:57 +05301093 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001094 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
Hemant Guptab820aec2013-12-24 19:59:57 +05301095 __FUNCTION__);
Pavlin Radoslavov65f9c042015-07-16 17:40:28 -07001096 btif_storage_remove_bonded_device(&bd_addr);
Hemant Guptab820aec2013-12-24 19:59:57 +05301097 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1098 return;
1099 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001100 }
1101 }
Priti Agherac0edf9f2014-06-26 11:23:51 -07001102
1103 // Skip SDP for certain HID Devices
The Android Open Source Project5738f832012-12-12 16:00:35 -08001104 if (p_auth_cmpl->success)
1105 {
Andre Eisenbach12871662015-05-08 17:42:10 -07001106#if BLE_INCLUDED == TRUE
Chaojing Sune2805532015-04-22 13:40:21 -07001107 btif_storage_set_remote_addr_type(&bd_addr, p_auth_cmpl->addr_type);
Andre Eisenbach12871662015-05-08 17:42:10 -07001108#endif
Chaojing Sune2805532015-04-22 13:40:21 -07001109 btif_update_remote_properties(p_auth_cmpl->bd_addr,
1110 p_auth_cmpl->bd_name, NULL, p_auth_cmpl->dev_type);
Andre Eisenbach31a64002014-10-14 14:29:19 -07001111 pairing_cb.timeout_retries = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001112 status = BT_STATUS_SUCCESS;
1113 state = BT_BOND_STATE_BONDED;
Priti Agherac0edf9f2014-06-26 11:23:51 -07001114 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001115
Priti Agherac0edf9f2014-06-26 11:23:51 -07001116 if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr, COD_HID_MAJOR))
1117 {
Sharvil Nanavati44802762014-12-23 23:08:58 -08001118 LOG_WARN("%s:skip SDP", __FUNCTION__);
Priti Agherac0edf9f2014-06-26 11:23:51 -07001119 skip_sdp = TRUE;
1120 }
1121 if(!pairing_cb.is_local_initiated && skip_sdp)
1122 {
1123 bond_state_changed(status, &bd_addr, state);
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -08001124
Sharvil Nanavati44802762014-12-23 23:08:58 -08001125 LOG_WARN("%s: Incoming HID Connection",__FUNCTION__);
Priti Agherac0edf9f2014-06-26 11:23:51 -07001126 bt_property_t prop;
1127 bt_bdaddr_t bd_addr;
1128 bt_uuid_t uuid;
1129 char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE;
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -08001130
Priti Agherac0edf9f2014-06-26 11:23:51 -07001131 string_to_uuid(uuid_str, &uuid);
1132
1133 prop.type = BT_PROPERTY_UUIDS;
1134 prop.val = uuid.uu;
1135 prop.len = MAX_UUID_SIZE;
1136
1137 /* Send the event to the BTIF */
1138 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1139 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1140 }
1141 else
1142 {
1143 /* Trigger SDP on the device */
1144 pairing_cb.sdp_attempts = 1;;
Andre Eisenbach12871662015-05-08 17:42:10 -07001145
1146#if BLE_INCLUDED == TRUE
Chaojing Sune2805532015-04-22 13:40:21 -07001147 /* If bonded due to cross-key, save the static address too*/
1148 if(pairing_cb.state == BT_BOND_STATE_BONDING &&
1149 (bdcmp(p_auth_cmpl->bd_addr, pairing_cb.bd_addr) != 0))
1150 {
1151 BTIF_TRACE_DEBUG("%s: bonding initiated due to cross key, adding static address",
1152 __func__);
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07001153 bdcpy(pairing_cb.static_bdaddr.address, p_auth_cmpl->bd_addr);
Chaojing Sune2805532015-04-22 13:40:21 -07001154 }
Andre Eisenbach12871662015-05-08 17:42:10 -07001155#endif
Priti Agherac0edf9f2014-06-26 11:23:51 -07001156
1157 if(btif_dm_inquiry_in_progress)
1158 btif_dm_cancel_discovery();
1159
1160 btif_dm_get_remote_services(&bd_addr);
Chaojing Sune2805532015-04-22 13:40:21 -07001161 }
1162 // 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 -08001163 }
1164 else
1165 {
Chaojing Sune2805532015-04-22 13:40:21 -07001166 // Map the HCI fail reason to bt status
The Android Open Source Project5738f832012-12-12 16:00:35 -08001167 switch(p_auth_cmpl->fail_reason)
1168 {
1169 case HCI_ERR_PAGE_TIMEOUT:
Andre Eisenbach27c4e632015-07-06 15:43:15 -07001170 if (interop_match(INTEROP_AUTO_RETRY_PAIRING, &bd_addr)
1171 && pairing_cb.timeout_retries)
Andre Eisenbach31a64002014-10-14 14:29:19 -07001172 {
1173 BTIF_TRACE_WARNING("%s() - Pairing timeout; retrying (%d) ...", __FUNCTION__, pairing_cb.timeout_retries);
1174 --pairing_cb.timeout_retries;
1175 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
1176 return;
1177 }
1178 /* Fall-through */
The Android Open Source Project5738f832012-12-12 16:00:35 -08001179 case HCI_ERR_CONNECTION_TOUT:
1180 status = BT_STATUS_RMT_DEV_DOWN;
1181 break;
1182
Hemant Guptaaef7a672013-07-31 19:00:12 +05301183 case HCI_ERR_PAIRING_NOT_ALLOWED:
1184 status = BT_STATUS_AUTH_REJECTED;
1185 break;
1186
Hemant Guptab4801442014-01-07 18:11:15 +05301187 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
1188 status = BT_STATUS_AUTH_FAILURE;
1189 break;
1190
The Android Open Source Project5738f832012-12-12 16:00:35 -08001191 /* map the auth failure codes, so we can retry pairing if necessary */
1192 case HCI_ERR_AUTH_FAILURE:
Hemant Gupta59a88ec2014-03-19 19:01:35 +05301193 case HCI_ERR_KEY_MISSING:
Zhihai Xua7ea8092013-11-27 14:10:53 +05301194 btif_storage_remove_bonded_device(&bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001195 case HCI_ERR_HOST_REJECT_SECURITY:
1196 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
1197 case HCI_ERR_UNIT_KEY_USED:
1198 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
1199 case HCI_ERR_INSUFFCIENT_SECURITY:
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301200 case HCI_ERR_PEER_USER:
1201 case HCI_ERR_UNSPECIFIED:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001202 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d",
Hemant Gupta87b7cce2013-11-28 13:07:10 +05301203 __FUNCTION__, p_auth_cmpl->fail_reason);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001204 if (pairing_cb.autopair_attempts == 1)
1205 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001206 BTIF_TRACE_DEBUG("%s(): Adding device to blacklist ", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001207
1208 /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class */
1209 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
1210 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
1211 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
1212 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
1213 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
1214 check_cod(&bd_addr, COD_HID_POINTING))
1215 {
1216 btif_storage_add_device_to_autopair_blacklist (&bd_addr);
1217 }
1218 pairing_cb.autopair_attempts++;
1219
1220 /* Create the Bond once again */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001221 BTIF_TRACE_DEBUG("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07001222 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001223 return;
1224 }
1225 else
1226 {
1227 /* if autopair attempts are more than 1, or not attempted */
1228 status = BT_STATUS_AUTH_FAILURE;
1229 }
1230 break;
1231
1232 default:
1233 status = BT_STATUS_FAIL;
1234 }
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301235 /* Special Handling for HID Devices */
1236 if (check_cod(&bd_addr, COD_HID_POINTING)) {
1237 /* Remove Device as bonded in nvram as authentication failed */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001238 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __FUNCTION__);
Zhihai Xu8d2128d2013-12-13 16:09:21 +05301239 btif_storage_remove_bonded_device(&bd_addr);
1240 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001241 bond_state_changed(status, &bd_addr, state);
1242 }
1243}
1244
1245/******************************************************************************
1246**
1247** Function btif_dm_search_devices_evt
1248**
1249** Description Executes search devices callback events in btif context
1250**
1251** Returns void
1252**
1253******************************************************************************/
1254static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
1255{
1256 tBTA_DM_SEARCH *p_search_data;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001257 BTIF_TRACE_EVENT("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001258
1259 switch (event)
1260 {
1261 case BTA_DM_DISC_RES_EVT:
1262 {
1263 p_search_data = (tBTA_DM_SEARCH *)p_param;
1264 /* Remote name update */
1265 if (strlen((const char *) p_search_data->disc_res.bd_name))
1266 {
1267 bt_property_t properties[1];
1268 bt_bdaddr_t bdaddr;
1269 bt_status_t status;
1270
1271 properties[0].type = BT_PROPERTY_BDNAME;
1272 properties[0].val = p_search_data->disc_res.bd_name;
1273 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
1274 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
1275
1276 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
1277 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
1278 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1279 status, &bdaddr, 1, properties);
1280 }
1281 /* TODO: Services? */
1282 }
1283 break;
1284
1285 case BTA_DM_INQ_RES_EVT:
1286 {
1287 /* inquiry result */
1288 UINT32 cod;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001289 bt_bdname_t bdname;
1290 bt_bdaddr_t bdaddr;
1291 UINT8 remote_name_len;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001292 tBTA_SERVICE_MASK services = 0;
1293 bdstr_t bdstr;
1294
1295 p_search_data = (tBTA_DM_SEARCH *)p_param;
1296 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
1297
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07001298 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 -08001299#if (BLE_INCLUDED == TRUE)
1300 p_search_data->inq_res.device_type);
1301#else
1302 BT_DEVICE_TYPE_BREDR);
1303#endif
1304 bdname.name[0] = 0;
1305
1306 cod = devclass2uint (p_search_data->inq_res.dev_class);
1307
Chris Mantonf8027002015-03-12 09:22:48 -07001308 if (cod == 0) {
1309 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001310 cod = COD_UNCLASSIFIED;
1311 }
1312
1313 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1314 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1315
1316 /* Check EIR for remote name and services */
1317 if (p_search_data->inq_res.p_eir)
1318 {
1319 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001320 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001321 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */
1322 }
1323
1324
1325 {
1326 bt_property_t properties[5];
1327 bt_device_type_t dev_type;
1328 uint32_t num_properties = 0;
1329 bt_status_t status;
Nitin Aroraac728402015-06-26 18:09:37 -07001330 int addr_type = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001331
1332 memset(properties, 0, sizeof(properties));
1333 /* BD_ADDR */
1334 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1335 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1336 num_properties++;
1337 /* BD_NAME */
1338 /* Don't send BDNAME if it is empty */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001339 if (bdname.name[0])
1340 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001341 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1342 BT_PROPERTY_BDNAME,
1343 strlen((char *)bdname.name), &bdname);
1344 num_properties++;
1345 }
1346
1347 /* DEV_CLASS */
1348 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1349 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1350 num_properties++;
1351 /* DEV_TYPE */
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001352#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08001353 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
Nitin Aroraac728402015-06-26 18:09:37 -07001354
1355 /* Verify if the device is dual mode in NVRAM */
1356 int stored_device_type = 0;
1357 if (btif_get_device_type(bdaddr.address, &stored_device_type) &&
1358 ((stored_device_type == BT_DEVICE_TYPE_BLE &&
1359 p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BREDR) ||
1360 (stored_device_type == BT_DEVICE_TYPE_BREDR &&
1361 p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE))) {
1362 dev_type = BT_DEVICE_TYPE_DUMO;
1363 } else {
1364 dev_type = p_search_data->inq_res.device_type;
1365 }
1366
1367 if (p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE)
1368 addr_type = p_search_data->inq_res.ble_addr_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001369#else
1370 dev_type = BT_DEVICE_TYPE_BREDR;
1371#endif
1372 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1373 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1374 num_properties++;
1375 /* RSSI */
1376 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1377 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1378 &(p_search_data->inq_res.rssi));
1379 num_properties++;
1380
1381 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1382 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
Andre Eisenbach5c44e452013-08-06 18:19:37 -07001383#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1384 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
1385 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1386#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001387 /* Callback to notify upper layer of device */
1388 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1389 num_properties, properties);
1390 }
1391 }
1392 break;
1393
1394 case BTA_DM_INQ_CMPL_EVT:
1395 {
Satya Calloji6e2d9db2014-07-08 16:18:58 -07001396#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1397 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1398 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1399 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1400 bte_scan_filt_param_cfg_evt, 0);
1401#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001402 }
1403 break;
1404 case BTA_DM_DISC_CMPL_EVT:
1405 {
1406 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1407 }
1408 break;
1409 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1410 {
1411 /* if inquiry is not in progress and we get a cancel event, then
1412 * it means we are done with inquiry, but remote_name fetches are in
1413 * progress
1414 *
1415 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt
1416 * but instead wait for the cancel_cmpl_evt via the Busy Level
1417 *
1418 */
1419 if (btif_dm_inquiry_in_progress == FALSE)
1420 {
Nitin Arora7b85efa2014-09-26 14:05:24 -07001421#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1422 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1423 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1424 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1425 bte_scan_filt_param_cfg_evt, 0);
1426#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001427 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1428 }
1429 }
1430 break;
1431 }
1432}
1433
1434/*******************************************************************************
1435**
1436** Function btif_dm_search_services_evt
1437**
1438** Description Executes search services event in btif context
1439**
1440** Returns void
1441**
1442*******************************************************************************/
1443static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1444{
1445 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1446
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001447 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001448 switch (event)
1449 {
1450 case BTA_DM_DISC_RES_EVT:
1451 {
The Android Open Source Project5738f832012-12-12 16:00:35 -08001452 bt_property_t prop;
Bernhard Rosenkränzer104e3f22014-11-12 21:53:08 +01001453 uint32_t i = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001454 bt_bdaddr_t bd_addr;
1455 bt_status_t ret;
1456
1457 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1458
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001459 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001460 p_data->disc_res.result, p_data->disc_res.services);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001461 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1462 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1463 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1464 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001465 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001466 pairing_cb.sdp_attempts++;
1467 btif_dm_get_remote_services(&bd_addr);
1468 return;
1469 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001470 prop.type = BT_PROPERTY_UUIDS;
1471 prop.len = 0;
1472 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1473 {
1474 prop.val = p_data->disc_res.p_uuid_list;
1475 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1476 for (i=0; i < p_data->disc_res.num_uuids; i++)
1477 {
1478 char temp[256];
Chris Manton8ff3fea2015-01-07 13:59:14 -08001479 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 -07001480 LOG_INFO("%s index:%d uuid:%s", __func__, i, temp);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001481 }
1482 }
1483
1484 /* onUuidChanged requires getBondedDevices to be populated.
1485 ** bond_state_changed needs to be sent prior to remote_device_property
1486 */
1487 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
Chaojing Sune2805532015-04-22 13:40:21 -07001488 ((bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0) ||
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07001489 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr.address) == 0)) &&
Chaojing Sune2805532015-04-22 13:40:21 -07001490 pairing_cb.sdp_attempts > 0)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001491 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001492 BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
The Android Open Source Project5738f832012-12-12 16:00:35 -08001493 __FUNCTION__);
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -07001494 pairing_cb.sdp_attempts = 0;
Chaojing Sune2805532015-04-22 13:40:21 -07001495
1496 // If bonding occured due to cross-key pairing, send bonding callback
1497 // for static address now
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07001498 if (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr.address) == 0)
Chaojing Sune2805532015-04-22 13:40:21 -07001499 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1500
The Android Open Source Project5738f832012-12-12 16:00:35 -08001501 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1502 }
1503
Chaojing Sune2805532015-04-22 13:40:21 -07001504 if (p_data->disc_res.num_uuids != 0)
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001505 {
1506 /* Also write this to the NVRAM */
1507 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1508 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1509 /* Send the event to the BTIF */
1510 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1511 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1512 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001513 }
1514 break;
1515
1516 case BTA_DM_DISC_CMPL_EVT:
1517 /* fixme */
1518 break;
1519
Matthew Xie607e3b72013-08-15 19:30:48 -07001520#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001521 case BTA_DM_DISC_BLE_RES_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001522 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__,
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001523 p_data->disc_ble_res.service.uu.uuid16);
1524 bt_uuid_t uuid;
1525 int i = 0;
1526 int j = 15;
1527 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1528 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001529 BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001530 bt_property_t prop;
1531 bt_bdaddr_t bd_addr;
1532 char temp[256];
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001533 bt_status_t ret;
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001534
1535 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1536
1537 while(i < j )
1538 {
1539 unsigned char c = uuid.uu[j];
1540 uuid.uu[j] = uuid.uu[i];
1541 uuid.uu[i] = c;
1542 i++;
1543 j--;
1544 }
1545
Chris Manton8ff3fea2015-01-07 13:59:14 -08001546 uuid_to_string_legacy(&uuid, temp);
Chris Mantonf8027002015-03-12 09:22:48 -07001547 LOG_INFO("%s uuid:%s", __func__, temp);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001548
1549 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1550 prop.type = BT_PROPERTY_UUIDS;
1551 prop.val = uuid.uu;
1552 prop.len = MAX_UUID_SIZE;
1553
Zhihai Xud7ee77b2013-11-05 18:06:54 -08001554 /* Also write this to the NVRAM */
1555 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1556 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1557
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001558 /* Send the event to the BTIF */
1559 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1560 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1561
1562 }
1563 break;
Matthew Xie607e3b72013-08-15 19:30:48 -07001564#endif /* BLE_INCLUDED */
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001565
The Android Open Source Project5738f832012-12-12 16:00:35 -08001566 default:
1567 {
1568 ASSERTC(0, "unhandled search services event", event);
1569 }
1570 break;
1571 }
1572}
1573
1574/*******************************************************************************
1575**
1576** Function btif_dm_remote_service_record_evt
1577**
1578** Description Executes search service record event in btif context
1579**
1580** Returns void
1581**
1582*******************************************************************************/
1583static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1584{
1585 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1586
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001587 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001588 switch (event)
1589 {
1590 case BTA_DM_DISC_RES_EVT:
1591 {
1592 bt_service_record_t rec;
1593 bt_property_t prop;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001594 bt_bdaddr_t bd_addr;
1595
1596 memset(&rec, 0, sizeof(bt_service_record_t));
1597 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1598
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001599 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
The Android Open Source Project5738f832012-12-12 16:00:35 -08001600 p_data->disc_res.result, p_data->disc_res.services);
1601 prop.type = BT_PROPERTY_SERVICE_RECORD;
1602 prop.val = (void*)&rec;
1603 prop.len = sizeof(rec);
1604
1605 /* disc_res.result is overloaded with SCN. Cannot check result */
1606 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1607 /* TODO: Get the UUID as well */
1608 rec.channel = p_data->disc_res.result - 3;
1609 /* TODO: Need to get the service name using p_raw_data */
1610 rec.name[0] = 0;
1611
1612 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1613 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1614 }
1615 break;
1616
1617 default:
1618 {
1619 ASSERTC(0, "unhandled remote service record event", event);
1620 }
1621 break;
1622 }
1623}
1624
1625/*******************************************************************************
1626**
1627** Function btif_dm_upstreams_cback
1628**
1629** Description Executes UPSTREAMS events in btif context
1630**
1631** Returns void
1632**
1633*******************************************************************************/
1634static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1635{
The Android Open Source Project5738f832012-12-12 16:00:35 -08001636 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1637 tBTA_SERVICE_MASK service_mask;
1638 uint32_t i;
1639 bt_bdaddr_t bd_addr;
1640
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001641 BTIF_TRACE_EVENT("btif_dm_upstreams_cback ev: %s", dump_dm_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001642
1643 switch (event)
1644 {
1645 case BTA_DM_ENABLE_EVT:
1646 {
1647 BD_NAME bdname;
1648 bt_status_t status;
1649 bt_property_t prop;
1650 prop.type = BT_PROPERTY_BDNAME;
1651 prop.len = BD_NAME_LEN;
1652 prop.val = (void*)bdname;
1653
1654 status = btif_storage_get_adapter_property(&prop);
Matthew Xie1e5109b2012-11-09 18:26:26 -08001655 if (status == BT_STATUS_SUCCESS)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001656 {
1657 /* A name exists in the storage. Make this the device name */
1658 BTA_DmSetDeviceName((char*)prop.val);
1659 }
Matthew Xie1e5109b2012-11-09 18:26:26 -08001660 else
1661 {
1662 /* Storage does not have a name yet.
1663 * Use the default name and write it to the chip
1664 */
1665 BTA_DmSetDeviceName(btif_get_default_local_name());
1666 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001667
Andre Eisenbacha015a832014-09-11 14:09:40 -07001668#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1669 /* Enable local privacy */
Andre Eisenbach3e0dc732014-10-24 09:55:34 -07001670 BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED);
Andre Eisenbacha015a832014-09-11 14:09:40 -07001671#endif
1672
The Android Open Source Project5738f832012-12-12 16:00:35 -08001673 /* for each of the enabled services in the mask, trigger the profile
1674 * enable */
1675 service_mask = btif_get_enabled_services_mask();
1676 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1677 {
1678 if (service_mask &
1679 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1680 {
1681 btif_in_execute_service_request(i, TRUE);
1682 }
1683 }
1684 /* clear control blocks */
1685 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
Satya Calloji877123f2015-04-23 23:39:49 -07001686 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001687
1688 /* This function will also trigger the adapter_properties_cb
1689 ** and bonded_devices_info_cb
1690 */
1691 btif_storage_load_bonded_devices();
1692
1693 btif_storage_load_autopair_device_list();
1694
Zach Johnson39110ec2014-10-06 13:15:00 -07001695 btif_enable_bluetooth_evt(p_data->enable.status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001696 }
1697 break;
1698
1699 case BTA_DM_DISABLE_EVT:
1700 /* for each of the enabled services in the mask, trigger the profile
1701 * disable */
1702 service_mask = btif_get_enabled_services_mask();
1703 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1704 {
1705 if (service_mask &
1706 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1707 {
1708 btif_in_execute_service_request(i, FALSE);
1709 }
1710 }
1711 btif_disable_bluetooth_evt();
1712 break;
1713
1714 case BTA_DM_PIN_REQ_EVT:
1715 btif_dm_pin_req_evt(&p_data->pin_req);
1716 break;
1717
1718 case BTA_DM_AUTH_CMPL_EVT:
1719 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1720 break;
1721
1722 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1723 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1724 {
1725 bdcpy(bd_addr.address, pairing_cb.bd_addr);
Pavlin Radoslavov65f9c042015-07-16 17:40:28 -07001726 btm_set_bond_type_dev(pairing_cb.bd_addr, BOND_TYPE_UNKNOWN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001727 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1728 }
1729 break;
1730
1731 case BTA_DM_SP_CFM_REQ_EVT:
1732 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1733 break;
1734 case BTA_DM_SP_KEY_NOTIF_EVT:
1735 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1736 break;
1737
1738 case BTA_DM_DEV_UNPAIRED_EVT:
1739 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
Pavlin Radoslavov65f9c042015-07-16 17:40:28 -07001740 btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001741
1742 /*special handling for HID devices */
1743 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -07001744 btif_hh_remove_device(bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001745 #endif
Hemant Gupta94c15ec2013-11-11 12:33:44 +05301746 #if (defined(BTA_HD_INCLUDED) && (BTA_HD_INCLUDED == TRUE))
1747 btif_hd_remove_device(bd_addr);
1748 #endif
1749 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1750 btif_storage_remove_ble_bonding_keys(&bd_addr);
1751 #endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08001752 btif_storage_remove_bonded_device(&bd_addr);
1753 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1754 break;
1755
1756 case BTA_DM_BUSY_LEVEL_EVT:
1757 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001758
1759 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001760 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001761 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001762 {
1763 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1764 BT_DISCOVERY_STARTED);
1765 btif_dm_inquiry_in_progress = TRUE;
1766 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001767 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001768 {
1769 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1770 BT_DISCOVERY_STOPPED);
1771 btif_dm_inquiry_in_progress = FALSE;
1772 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001773 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
The Android Open Source Project5738f832012-12-12 16:00:35 -08001774 {
1775 btif_dm_inquiry_in_progress = FALSE;
1776 }
1777 }
1778 }break;
1779
1780 case BTA_DM_LINK_UP_EVT:
1781 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001782 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001783
1784 btif_update_remote_version_property(&bd_addr);
1785
The Android Open Source Project5738f832012-12-12 16:00:35 -08001786 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1787 &bd_addr, BT_ACL_STATE_CONNECTED);
1788 break;
1789
1790 case BTA_DM_LINK_DOWN_EVT:
1791 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
Pavlin Radoslavov65f9c042015-07-16 17:40:28 -07001792 btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001793 BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001794 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1795 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1796 break;
1797
1798 case BTA_DM_HW_ERROR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001799 BTIF_TRACE_ERROR("Received H/W Error. ");
The Android Open Source Project5738f832012-12-12 16:00:35 -08001800 /* Flush storage data */
1801 btif_config_flush();
1802 usleep(100000); /* 100milliseconds */
1803 /* Killing the process to force a restart as part of fault tolerance */
1804 kill(getpid(), SIGKILL);
1805 break;
1806
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001807#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1808 case BTA_DM_BLE_KEY_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001809 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 -08001810
1811 /* If this pairing is by-product of local initiated GATT client Read or Write,
1812 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1813 have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1814 if (pairing_cb.state != BT_BOND_STATE_BONDING)
1815 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001816 BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001817 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1818 BT_BOND_STATE_BONDING);
1819 }
1820 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1821 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001822 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001823 break;
1824 }
1825
1826 switch (p_data->ble_key.key_type)
1827 {
1828 case BTA_LE_KEY_PENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001829 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001830 pairing_cb.ble.is_penc_key_rcvd = TRUE;
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07001831 pairing_cb.ble.penc_key = p_data->ble_key.p_key_value->penc_key;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001832 break;
1833
1834 case BTA_LE_KEY_PID:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001835 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001836 pairing_cb.ble.is_pid_key_rcvd = TRUE;
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07001837 pairing_cb.ble.pid_key = p_data->ble_key.p_key_value->pid_key;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001838 break;
1839
1840 case BTA_LE_KEY_PCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001841 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001842 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07001843 pairing_cb.ble.pcsrk_key = p_data->ble_key.p_key_value->pcsrk_key;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001844 break;
1845
1846 case BTA_LE_KEY_LENC:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001847 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001848 pairing_cb.ble.is_lenc_key_rcvd = TRUE;
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07001849 pairing_cb.ble.lenc_key = p_data->ble_key.p_key_value->lenc_key;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001850 break;
1851
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001852 case BTA_LE_KEY_LCSRK:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001853 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001854 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07001855 pairing_cb.ble.lcsrk_key = p_data->ble_key.p_key_value->lcsrk_key;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001856 break;
1857
Satya Calloji444a8da2015-03-06 10:38:22 -08001858 case BTA_LE_KEY_LID:
1859 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LID");
1860 pairing_cb.ble.is_lidk_key_rcvd = TRUE;
1861 break;
1862
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001863 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001864 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001865 break;
1866 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001867 break;
1868 case BTA_DM_BLE_SEC_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001869 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001870 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1871 break;
1872 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001873 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001874 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1875 break;
1876 case BTA_DM_BLE_PASSKEY_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001877 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001878 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1879 break;
Satya Calloji444a8da2015-03-06 10:38:22 -08001880 case BTA_DM_BLE_NC_REQ_EVT:
1881 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
1882 btif_dm_ble_key_nc_req_evt(&p_data->key_notif);
1883 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001884 case BTA_DM_BLE_OOB_REQ_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001885 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001886 break;
1887 case BTA_DM_BLE_LOCAL_IR_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001888 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001889 ble_local_key_cb.is_id_keys_rcvd = TRUE;
Satya Calloji444a8da2015-03-06 10:38:22 -08001890 memcpy(&ble_local_key_cb.id_keys.irk[0],
1891 &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1892 memcpy(&ble_local_key_cb.id_keys.ir[0],
1893 &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1894 memcpy(&ble_local_key_cb.id_keys.dhk[0],
1895 &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001896 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
Satya Calloji7bd40382015-05-19 14:42:32 -07001897 BTIF_DM_LE_LOCAL_KEY_IRK,
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001898 BT_OCTET16_LEN);
1899 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
Satya Calloji7bd40382015-05-19 14:42:32 -07001900 BTIF_DM_LE_LOCAL_KEY_IR,
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001901 BT_OCTET16_LEN);
1902 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1903 BTIF_DM_LE_LOCAL_KEY_DHK,
1904 BT_OCTET16_LEN);
1905 break;
1906 case BTA_DM_BLE_LOCAL_ER_EVT:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001907 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001908 ble_local_key_cb.is_er_rcvd = TRUE;
1909 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1910 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1911 BTIF_DM_LE_LOCAL_KEY_ER,
1912 BT_OCTET16_LEN);
1913 break;
1914
1915 case BTA_DM_BLE_AUTH_CMPL_EVT:
Satya Calloji444a8da2015-03-06 10:38:22 -08001916 BTIF_TRACE_DEBUG("BTA_DM_BLE_AUTH_CMPL_EVT. ");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001917 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1918 break;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001919
1920 case BTA_DM_LE_FEATURES_READ:
1921 {
1922 tBTM_BLE_VSC_CB cmn_vsc_cb;
1923 bt_local_le_features_t local_le_features;
1924 char buf[512];
1925 bt_property_t prop;
1926 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1927 prop.val = (void*)buf;
1928 prop.len = sizeof(buf);
1929
1930 /* LE features are not stored in storage. Should be retrived from stack */
1931 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1932 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1933
1934 prop.len = sizeof (bt_local_le_features_t);
1935 if (cmn_vsc_cb.filter_support == 1)
1936 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1937 else
1938 local_le_features.max_adv_filter_supported = 0;
1939 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1940 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1941 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
Satya Callojiefaddcb2014-07-28 23:22:05 -07001942 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
Satya Callojif5387072015-02-09 17:40:52 -08001943 local_le_features.scan_result_storage_size = cmn_vsc_cb.tot_scan_results_strg;
1944 local_le_features.version_supported = cmn_vsc_cb.version_supported;
1945 local_le_features.total_trackable_advertisers =
1946 cmn_vsc_cb.total_trackable_advertisers;
Satya Calloji5725fc62015-03-31 13:24:32 -07001947
1948 local_le_features.extended_scan_support = cmn_vsc_cb.extended_scan_support > 0;
1949 local_le_features.debug_logging_supported = cmn_vsc_cb.debug_logging_supported > 0;
1950
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001951 memcpy(prop.val, &local_le_features, prop.len);
1952 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
1953 break;
1954 }
Satya Callojie5ba8842014-07-03 17:18:02 -07001955
1956 case BTA_DM_ENER_INFO_READ:
1957 {
1958 btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param;
1959 bt_activity_energy_info energy_info;
1960 energy_info.status = p_ener_data->status;
1961 energy_info.ctrl_state = p_ener_data->ctrl_state;
1962 energy_info.rx_time = p_ener_data->rx_time;
1963 energy_info.tx_time = p_ener_data->tx_time;
1964 energy_info.idle_time = p_ener_data->idle_time;
1965 energy_info.energy_used = p_ener_data->energy_used;
1966 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info);
1967 break;
1968 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001969#endif
1970
The Android Open Source Project5738f832012-12-12 16:00:35 -08001971 case BTA_DM_AUTHORIZE_EVT:
1972 case BTA_DM_SIG_STRENGTH_EVT:
1973 case BTA_DM_SP_RMT_OOB_EVT:
1974 case BTA_DM_SP_KEYPRESS_EVT:
1975 case BTA_DM_ROLE_CHG_EVT:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001976
The Android Open Source Project5738f832012-12-12 16:00:35 -08001977 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001978 BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event );
The Android Open Source Project5738f832012-12-12 16:00:35 -08001979 break;
1980 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001981
Andre Eisenbach8a057242015-04-29 22:27:15 -07001982 btif_dm_data_free(event, p_data);
1983}
The Android Open Source Project5738f832012-12-12 16:00:35 -08001984
1985/*******************************************************************************
1986**
1987** Function btif_dm_generic_evt
1988**
1989** Description Executes non-BTA upstream events in BTIF context
1990**
1991** Returns void
1992**
1993*******************************************************************************/
1994static void btif_dm_generic_evt(UINT16 event, char* p_param)
1995{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07001996 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001997 switch(event)
1998 {
1999 case BTIF_DM_CB_DISCOVERY_STARTED:
2000 {
2001 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
2002 }
2003 break;
2004
2005 case BTIF_DM_CB_CREATE_BOND:
2006 {
Andre Eisenbach31a64002014-10-14 14:29:19 -07002007 pairing_cb.timeout_retries = NUM_TIMEOUT_RETRIES;
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002008 btif_dm_create_bond_cb_t *create_bond_cb = (btif_dm_create_bond_cb_t*)p_param;
2009 btif_dm_cb_create_bond(&create_bond_cb->bdaddr, create_bond_cb->transport);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002010 }
2011 break;
2012
2013 case BTIF_DM_CB_REMOVE_BOND:
2014 {
2015 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
2016 }
2017 break;
2018
2019 case BTIF_DM_CB_HID_REMOTE_NAME:
2020 {
2021 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
2022 }
2023 break;
2024
2025 case BTIF_DM_CB_BOND_STATE_BONDING:
2026 {
2027 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
2028 }
2029 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002030 case BTIF_DM_CB_LE_TX_TEST:
2031 case BTIF_DM_CB_LE_RX_TEST:
2032 {
2033 uint8_t status;
2034 STREAM_TO_UINT8(status, p_param);
2035 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2036 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
2037 }
2038 break;
2039 case BTIF_DM_CB_LE_TEST_END:
2040 {
2041 uint8_t status;
2042 uint16_t count = 0;
2043 STREAM_TO_UINT8(status, p_param);
2044 if (status == 0)
2045 STREAM_TO_UINT16(count, p_param);
2046 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2047 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
2048 }
2049 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002050 default:
2051 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002052 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002053 }
2054 break;
2055 }
2056}
2057
2058/*******************************************************************************
2059**
2060** Function bte_dm_evt
2061**
2062** Description Switches context from BTE to BTIF for all DM events
2063**
2064** Returns void
2065**
2066*******************************************************************************/
2067
2068void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
2069{
The Android Open Source Project5738f832012-12-12 16:00:35 -08002070 /* switch context to btif task context (copy full union size for convenience) */
Andre Eisenbach8a057242015-04-29 22:27:15 -07002071 bt_status_t status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event,
2072 (void*)p_data, sizeof(tBTA_DM_SEC), btif_dm_data_copy);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002073
2074 /* catch any failed context transfers */
2075 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
2076}
2077
2078/*******************************************************************************
2079**
2080** Function bte_search_devices_evt
2081**
2082** Description Switches context from BTE to BTIF for DM search events
2083**
2084** Returns void
2085**
2086*******************************************************************************/
2087static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2088{
2089 UINT16 param_len = 0;
2090
2091 if (p_data)
2092 param_len += sizeof(tBTA_DM_SEARCH);
2093 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
2094 switch (event)
2095 {
2096 case BTA_DM_INQ_RES_EVT:
2097 {
2098 if (p_data->inq_res.p_eir)
2099 param_len += HCI_EXT_INQ_RESPONSE_LEN;
2100 }
2101 break;
2102
2103 case BTA_DM_DISC_RES_EVT:
2104 {
2105 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
2106 param_len += p_data->disc_res.raw_data_size;
2107 }
2108 break;
2109 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002110 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 -08002111
2112 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
2113 if (event == BTA_DM_INQ_RES_EVT)
2114 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
2115
2116 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
2117 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
2118}
2119
2120/*******************************************************************************
2121**
2122** Function bte_dm_search_services_evt
2123**
2124** Description Switches context from BTE to BTIF for DM search services
2125** event
2126**
2127** Returns void
2128**
2129*******************************************************************************/
2130static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2131{
2132 UINT16 param_len = 0;
2133 if (p_data)
2134 param_len += sizeof(tBTA_DM_SEARCH);
2135 switch (event)
2136 {
2137 case BTA_DM_DISC_RES_EVT:
2138 {
2139 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
2140 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
2141 }
2142 } break;
2143 }
2144 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
2145 * if raw_data is needed. */
2146 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
2147 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
2148}
2149
2150/*******************************************************************************
2151**
2152** Function bte_dm_remote_service_record_evt
2153**
2154** Description Switches context from BTE to BTIF for DM search service
2155** record event
2156**
2157** Returns void
2158**
2159*******************************************************************************/
2160static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2161{
2162 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
2163 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
2164}
2165
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002166#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07002167/*******************************************************************************
2168**
2169** Function bta_energy_info_cb
2170**
2171** Description Switches context from BTE to BTIF for DM energy info event
2172**
2173** Returns void
2174**
2175*******************************************************************************/
2176static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time,
2177 tBTA_DM_BLE_IDLE_TIME_MS idle_time,
2178 tBTA_DM_BLE_ENERGY_USED energy_used,
2179 tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status)
2180{
2181 BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld",
2182 status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
2183
2184 btif_activity_energy_info_cb_t btif_cb;
2185 btif_cb.status = status;
2186 btif_cb.ctrl_state = ctrl_state;
2187 btif_cb.tx_time = (uint64_t) tx_time;
2188 btif_cb.rx_time = (uint64_t) rx_time;
2189 btif_cb.idle_time =(uint64_t) idle_time;
2190 btif_cb.energy_used =(uint64_t) energy_used;
2191 btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
2192 (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL);
2193}
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07002194#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07002195
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002196/*******************************************************************************
2197**
2198** Function bte_scan_filt_param_cfg_evt
2199**
2200** Description Scan filter param config event
2201**
2202** Returns void
2203**
2204*******************************************************************************/
2205static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
2206 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
2207 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
2208{
2209 /* This event occurs on calling BTA_DmBleCfgFilterCondition internally,
2210 ** and that is why there is no HAL callback
2211 */
2212 if(BTA_SUCCESS != status)
2213 {
2214 BTIF_TRACE_ERROR("%s, %d", __FUNCTION__, status);
2215 }
2216 else
2217 {
2218 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2219 }
2220}
2221
The Android Open Source Project5738f832012-12-12 16:00:35 -08002222/*****************************************************************************
2223**
2224** btif api functions (no context switch)
2225**
2226*****************************************************************************/
2227
2228/*******************************************************************************
2229**
2230** Function btif_dm_start_discovery
2231**
2232** Description Start device discovery/inquiry
2233**
2234** Returns bt_status_t
2235**
2236*******************************************************************************/
2237bt_status_t btif_dm_start_discovery(void)
2238{
2239 tBTA_DM_INQ inq_params;
2240 tBTA_SERVICE_MASK services = 0;
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002241 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002242
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002243 BTIF_TRACE_EVENT("%s", __FUNCTION__);
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002244
2245#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2246 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
2247 /* Cleanup anything remaining on index 0 */
2248 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
2249 bte_scan_filt_param_cfg_evt, 0);
2250
2251 /* Add an allow-all filter on index 0*/
2252 adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE;
2253 adv_filt_param.feat_seln = ALLOW_ALL_FILTER;
2254 adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;
2255 adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;
2256 adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE;
2257 adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE;
2258 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL,
2259 bte_scan_filt_param_cfg_evt, 0);
2260
The Android Open Source Project5738f832012-12-12 16:00:35 -08002261 /* TODO: Do we need to handle multiple inquiries at the same time? */
2262
2263 /* Set inquiry params and call API */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002264 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
Matthew Xie7f3e4292013-09-30 12:44:10 -07002265#if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
2266 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
2267 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
2268 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
2269 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
2270#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002271#else
2272 inq_params.mode = BTA_DM_GENERAL_INQUIRY;
2273#endif
2274 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
2275
2276 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
2277 inq_params.report_dup = TRUE;
2278
2279 inq_params.filter_type = BTA_DM_INQ_CLR;
2280 /* TODO: Filter device by BDA needs to be implemented here */
2281
2282 /* Will be enabled to TRUE once inquiry busy level has been received */
2283 btif_dm_inquiry_in_progress = FALSE;
2284 /* find nearby devices */
2285 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
2286
2287 return BT_STATUS_SUCCESS;
2288}
2289
2290/*******************************************************************************
2291**
2292** Function btif_dm_cancel_discovery
2293**
2294** Description Cancels search
2295**
2296** Returns bt_status_t
2297**
2298*******************************************************************************/
2299bt_status_t btif_dm_cancel_discovery(void)
2300{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002301 BTIF_TRACE_EVENT("%s", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002302 BTA_DmSearchCancel();
2303 return BT_STATUS_SUCCESS;
2304}
2305
2306/*******************************************************************************
2307**
2308** Function btif_dm_create_bond
2309**
2310** Description Initiate bonding with the specified device
2311**
2312** Returns bt_status_t
2313**
2314*******************************************************************************/
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002315bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr, int transport)
The Android Open Source Project5738f832012-12-12 16:00:35 -08002316{
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002317 btif_dm_create_bond_cb_t create_bond_cb;
2318 create_bond_cb.transport = transport;
2319 bdcpy(create_bond_cb.bdaddr.address, bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002320
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002321 bdstr_t bdstr;
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002322 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 -08002323 if (pairing_cb.state != BT_BOND_STATE_NONE)
2324 return BT_STATUS_BUSY;
2325
2326 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002327 (char *)&create_bond_cb, sizeof(btif_dm_create_bond_cb_t), NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002328
2329 return BT_STATUS_SUCCESS;
2330}
2331
2332/*******************************************************************************
2333**
2334** Function btif_dm_cancel_bond
2335**
2336** Description Initiate bonding with the specified device
2337**
2338** Returns bt_status_t
2339**
2340*******************************************************************************/
2341
2342bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
2343{
2344 bdstr_t bdstr;
2345
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002346 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 -08002347
2348 /* TODO:
2349 ** 1. Restore scan modes
2350 ** 2. special handling for HID devices
2351 */
2352 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2353 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002354
2355#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2356
2357 if (pairing_cb.is_ssp)
2358 {
2359 if (pairing_cb.is_le_only)
2360 {
2361 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2362 }
2363 else
Hemant Guptae1468692013-11-14 16:21:29 +05302364 {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002365 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
Hemant Guptae1468692013-11-14 16:21:29 +05302366 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2367 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2368 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002369 }
2370 else
2371 {
2372 if (pairing_cb.is_le_only)
2373 {
2374 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2375 }
2376 else
2377 {
2378 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2379 }
2380 /* Cancel bonding, in case it is in ACL connection setup state */
2381 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2382 }
2383
2384#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002385 if (pairing_cb.is_ssp)
2386 {
2387 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2388 }
2389 else
2390 {
2391 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2392 }
2393 /* Cancel bonding, in case it is in ACL connection setup state */
2394 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2395 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002396#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002397 }
2398
2399 return BT_STATUS_SUCCESS;
2400}
2401
2402/*******************************************************************************
2403**
Kim Schulza9eb25c2013-09-30 10:55:52 +02002404** Function btif_dm_hh_open_failed
2405**
2406** Description informs the upper layers if the HH have failed during bonding
2407**
2408** Returns none
2409**
2410*******************************************************************************/
2411
2412void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
2413{
2414 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2415 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2416 {
2417 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2418 }
2419}
2420
2421/*******************************************************************************
2422**
The Android Open Source Project5738f832012-12-12 16:00:35 -08002423** Function btif_dm_remove_bond
2424**
2425** Description Removes bonding with the specified device
2426**
2427** Returns bt_status_t
2428**
2429*******************************************************************************/
2430
2431bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
2432{
2433 bdstr_t bdstr;
2434
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002435 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 -08002436 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2437 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2438
2439 return BT_STATUS_SUCCESS;
2440}
2441
2442/*******************************************************************************
2443**
2444** Function btif_dm_pin_reply
2445**
2446** Description BT legacy pairing - PIN code reply
2447**
2448** Returns bt_status_t
2449**
2450*******************************************************************************/
2451
2452bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2453 uint8_t pin_len, bt_pin_code_t *pin_code)
2454{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002455 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Hemant Gupta831423e2014-01-08 12:42:13 +05302456 if (pin_code == NULL)
2457 return BT_STATUS_FAIL;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002458#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
The Android Open Source Project5738f832012-12-12 16:00:35 -08002459
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002460 if (pairing_cb.is_le_only)
2461 {
2462 int i;
2463 UINT32 passkey = 0;
2464 int multi[] = {100000, 10000, 1000, 100, 10,1};
2465 BD_ADDR remote_bd_addr;
2466 bdcpy(remote_bd_addr, bd_addr->address);
2467 for (i = 0; i < 6; i++)
2468 {
2469 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2470 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002471 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002472 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2473
2474 }
2475 else
2476 {
2477 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2478 if (accept)
2479 pairing_cb.pin_code_len = pin_len;
2480 }
2481#else
The Android Open Source Project5738f832012-12-12 16:00:35 -08002482 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2483
2484 if (accept)
2485 pairing_cb.pin_code_len = pin_len;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002486#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002487 return BT_STATUS_SUCCESS;
2488}
2489
2490/*******************************************************************************
2491**
2492** Function btif_dm_ssp_reply
2493**
2494** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2495**
2496** Returns bt_status_t
2497**
2498*******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002499bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2500 bt_ssp_variant_t variant, uint8_t accept,
2501 uint32_t passkey)
2502{
Mike J. Chen5cd8bff2014-01-31 18:16:59 -08002503 UNUSED(passkey);
2504
The Android Open Source Project5738f832012-12-12 16:00:35 -08002505 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2506 {
2507 /* This is not implemented in the stack.
2508 * For devices with display, this is not needed
2509 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002510 BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002511 return BT_STATUS_FAIL;
2512 }
2513 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002514 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002515#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2516 if (pairing_cb.is_le_only)
2517 {
Satya Calloji7fb0da62015-01-29 20:40:17 -08002518 if(pairing_cb.is_le_nc)
2519 {
2520 BTA_DmBleConfirmReply((UINT8 *)bd_addr->address,accept);
2521 } else {
2522 if (accept)
2523 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2524 else
2525 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2526 }
2527 } else {
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002528 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
Satya Calloji7fb0da62015-01-29 20:40:17 -08002529 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002530#else
2531 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2532#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08002533 return BT_STATUS_SUCCESS;
2534}
2535
2536/*******************************************************************************
2537**
2538** Function btif_dm_get_adapter_property
2539**
2540** Description Queries the BTA for the adapter property
2541**
2542** Returns bt_status_t
2543**
2544*******************************************************************************/
2545bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2546{
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002547 BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002548 switch (prop->type)
2549 {
2550 case BT_PROPERTY_BDNAME:
2551 {
2552 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
VenkatRaghavan VijayaRaghavan4540f592015-02-05 04:40:47 -08002553 strncpy((char *)bd_name->name, (char *)btif_get_default_local_name(),
VenkatRaghavan VijayaRaghavan1d8e6b82015-02-05 22:20:39 -08002554 sizeof(bd_name->name) - 1);
2555 bd_name->name[sizeof(bd_name->name) - 1] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002556 prop->len = strlen((char *)bd_name->name);
2557 }
2558 break;
2559
2560 case BT_PROPERTY_ADAPTER_SCAN_MODE:
2561 {
2562 /* if the storage does not have it. Most likely app never set it. Default is NONE */
2563 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2564 *mode = BT_SCAN_MODE_NONE;
2565 prop->len = sizeof(bt_scan_mode_t);
2566 }
2567 break;
2568
2569 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2570 {
2571 uint32_t *tmt = (uint32_t*)prop->val;
2572 *tmt = 120; /* default to 120s, if not found in NV */
2573 prop->len = sizeof(uint32_t);
2574 }
2575 break;
2576
2577 default:
2578 prop->len = 0;
2579 return BT_STATUS_FAIL;
2580 }
2581 return BT_STATUS_SUCCESS;
2582}
2583
2584/*******************************************************************************
2585**
2586** Function btif_dm_get_remote_services
2587**
2588** Description Start SDP to get remote services
2589**
2590** Returns bt_status_t
2591**
2592*******************************************************************************/
2593bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2594{
2595 bdstr_t bdstr;
2596
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002597 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 -08002598
2599 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2600 bte_dm_search_services_evt, TRUE);
2601
2602 return BT_STATUS_SUCCESS;
2603}
2604
2605/*******************************************************************************
2606**
Nitin Arorab4365c52015-06-25 18:30:09 -07002607** Function btif_dm_get_remote_services_transport
2608**
2609** Description Start SDP to get remote services by transport
2610**
2611** Returns bt_status_t
2612**
2613*******************************************************************************/
2614bt_status_t btif_dm_get_remote_services_by_transport(bt_bdaddr_t *remote_addr, const int transport)
2615{
2616 BTIF_TRACE_EVENT("%s", __func__);
2617
2618 /* Set the mask extension */
2619 tBTA_SERVICE_MASK_EXT mask_ext;
2620 mask_ext.num_uuid = 0;
2621 mask_ext.p_uuid = NULL;
2622 mask_ext.srvc_mask = BTA_ALL_SERVICE_MASK;
2623
2624 BTA_DmDiscoverByTransport(remote_addr->address, &mask_ext,
2625 bte_dm_search_services_evt, TRUE, transport);
2626
2627 return BT_STATUS_SUCCESS;
2628}
2629
2630/*******************************************************************************
2631**
The Android Open Source Project5738f832012-12-12 16:00:35 -08002632** Function btif_dm_get_remote_service_record
2633**
2634** Description Start SDP to get remote service record
2635**
2636**
2637** Returns bt_status_t
2638*******************************************************************************/
2639bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2640 bt_uuid_t *uuid)
2641{
2642 tSDP_UUID sdp_uuid;
2643 bdstr_t bdstr;
2644
Sharvil Nanavati8a6a89f2014-08-20 09:39:25 -07002645 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 -08002646
2647 sdp_uuid.len = MAX_UUID_SIZE;
2648 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2649
2650 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2651 bte_dm_remote_service_record_evt, TRUE);
2652
2653 return BT_STATUS_SUCCESS;
2654}
2655
2656void btif_dm_execute_service_request(UINT16 event, char *p_param)
2657{
2658 BOOLEAN b_enable = FALSE;
2659 bt_status_t status;
2660 if (event == BTIF_DM_ENABLE_SERVICE)
2661 {
2662 b_enable = TRUE;
2663 }
2664 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2665 if (status == BT_STATUS_SUCCESS)
2666 {
2667 bt_property_t property;
2668 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2669
2670 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2671 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2672 sizeof(local_uuids), local_uuids);
2673 btif_storage_get_adapter_property(&property);
2674 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2675 BT_STATUS_SUCCESS, 1, &property);
2676 }
2677 return;
2678}
2679
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002680void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2681 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2682{
2683 UINT8 yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2684 /* if local initiated:
2685 ** 1. set DD + MITM
2686 ** if remote initiated:
2687 ** 1. Copy over the auth_req from peer's io_rsp
2688 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2689 ** as a fallback set MITM+GB if peer had MITM set
2690 */
2691 UNUSED (bd_addr);
2692 UNUSED (p_io_cap);
2693 UNUSED (p_oob_data);
2694
2695
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002696 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002697 if(pairing_cb.is_local_initiated)
2698 {
2699 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2700 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2701 }
2702 else if (!is_orig)
2703 {
2704 /* peer initiated paring. They probably know what they want.
2705 ** Copy the mitm from peer device.
2706 */
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002707 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002708 __FUNCTION__, pairing_cb.auth_req);
2709 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2710
2711 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2712 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2713 *p_auth_req |= BTA_AUTH_SP_YES;
2714 }
2715 else if (yes_no_bit)
2716 {
2717 /* set the general bonding bit for stored device */
2718 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2719 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002720 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002721}
2722
2723void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2724 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2725{
2726 UNUSED (bd_addr);
2727 UNUSED (oob_data);
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -07002728
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002729 if(auth_req & BTA_AUTH_BONDS)
2730 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002731 BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002732 pairing_cb.auth_req = auth_req;
2733 pairing_cb.io_cap = io_cap;
2734 }
2735}
2736
The Android Open Source Project5738f832012-12-12 16:00:35 -08002737#if (BTM_OOB_INCLUDED == TRUE)
2738void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data)
2739{
2740 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2741 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2742 {
2743 *p_oob_data = FALSE;
2744 }
2745 else
2746 {
2747 *p_oob_data = TRUE;
2748 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002749 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 -08002750}
2751#endif /* BTM_OOB_INCLUDED */
2752
2753#ifdef BTIF_DM_OOB_TEST
2754void btif_dm_load_local_oob(void)
2755{
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002756 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002757 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002758 BTIF_TRACE_DEBUG("btif_dm_load_local_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002759 if (prop_oob[0] != '3')
2760 {
2761#if (BTM_OOB_INCLUDED == TRUE)
2762 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2763 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2764 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002765 BTIF_TRACE_DEBUG("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002766 BTA_DmLocalOob();
2767 }
2768#else
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002769 BTIF_TRACE_ERROR("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002770#endif
2771 }
2772}
2773
2774void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2775{
2776 FILE *fp;
2777 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2778 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2779 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002780 char prop_oob[PROPERTY_VALUE_MAX];
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002781 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: valid=%d", valid);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002782 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2783 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2784 valid)
2785 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002786 BTIF_TRACE_DEBUG("save local OOB data in memory");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002787 memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2788 memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2789 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002790 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002791 if (prop_oob[0] == '1')
2792 path = path_a;
2793 else if (prop_oob[0] == '2')
2794 path = path_b;
2795 if (path)
2796 {
2797 fp = fopen(path, "wb+");
2798 if (fp == NULL)
2799 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002800 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 -08002801 }
2802 else
2803 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002804 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 -08002805 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2806 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2807 fclose(fp);
2808 }
2809 }
2810 }
2811}
2812BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2813{
2814 char t[128];
2815 FILE *fp;
2816 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2817 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2818 char *path = NULL;
Nick Kralevichd70b7a82013-01-31 14:40:15 -08002819 char prop_oob[PROPERTY_VALUE_MAX];
The Android Open Source Project5738f832012-12-12 16:00:35 -08002820 BOOLEAN result = FALSE;
2821 bt_bdaddr_t bt_bd_addr;
2822 bdcpy(oob_cb.oob_bdaddr, bd_addr);
2823 property_get("service.brcm.bt.oob", prop_oob, "3");
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002824 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002825 if (prop_oob[0] == '1')
2826 path = path_b;
2827 else if (prop_oob[0] == '2')
2828 path = path_a;
2829 if (path)
2830 {
2831 fp = fopen(path, "rb");
2832 if (fp == NULL)
2833 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002834 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 -08002835 return FALSE;
2836 }
2837 else
2838 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002839 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob: read OOB data from %s",path);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002840 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2841 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2842 fclose(fp);
2843 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002844 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: TRUE");
The Android Open Source Project5738f832012-12-12 16:00:35 -08002845 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2846 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2847 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002848 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002849 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2850 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
2851 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 -07002852 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: c = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002853 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2854 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
2855 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 -07002856 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: r = %s",t);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002857 bdcpy(bt_bd_addr.address, bd_addr);
2858 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2859 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2860 result = TRUE;
2861 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002862 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob result=%d",result);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002863 return result;
2864}
2865#endif /* BTIF_DM_OOB_TEST */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002866#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2867
2868static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2869{
2870 bt_bdaddr_t bd_addr;
2871 bt_bdname_t bd_name;
2872 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08002873 int dev_type;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002874
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002875 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002876
2877 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08002878 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
2879 {
2880 dev_type = BT_DEVICE_TYPE_BLE;
2881 }
2882 btif_dm_update_ble_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2883 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002884 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2885 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2886
2887 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2888 pairing_cb.is_ssp = FALSE;
2889 cod = COD_UNCLASSIFIED;
2890
2891 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2892 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2893 p_ssp_key_notif->passkey);
2894}
2895
2896/*******************************************************************************
2897**
2898** Function btif_dm_ble_auth_cmpl_evt
2899**
2900** Description Executes authentication complete event in btif context
2901**
2902** Returns void
2903**
2904*******************************************************************************/
2905static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2906{
2907 /* Save link key, if not temporary */
2908 bt_bdaddr_t bd_addr;
2909 bt_status_t status = BT_STATUS_FAIL;
2910 bt_bond_state_t state = BT_BOND_STATE_NONE;
2911
2912 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2913 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2914 {
2915 /* store keys */
2916 }
2917 if (p_auth_cmpl->success)
2918 {
2919 status = BT_STATUS_SUCCESS;
2920 state = BT_BOND_STATE_BONDED;
Satya Calloji7cb76b42015-05-07 09:45:00 -07002921 int addr_type;
2922 bt_bdaddr_t bdaddr;
2923 bdcpy(bdaddr.address, p_auth_cmpl->bd_addr);
2924 if (btif_storage_get_remote_addr_type(&bdaddr, &addr_type) != BT_STATUS_SUCCESS)
2925 btif_storage_set_remote_addr_type(&bdaddr, p_auth_cmpl->addr_type);
Pavlin Radoslavov65f9c042015-07-16 17:40:28 -07002926
2927 /* Test for temporary bonding */
2928 if (btm_get_bond_type_dev(p_auth_cmpl->bd_addr) == BOND_TYPE_TEMPORARY) {
2929 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
2930 __func__);
2931 btif_storage_remove_bonded_device(&bdaddr);
2932 state = BT_BOND_STATE_NONE;
2933 } else {
2934 btif_dm_save_ble_bonding_keys();
2935 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002936 BTA_GATTC_Refresh(bd_addr.address);
Nitin Arorab4365c52015-06-25 18:30:09 -07002937 btif_dm_get_remote_services_by_transport(&bd_addr, BTA_GATT_TRANSPORT_LE);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002938 }
2939 else
2940 {
2941 /*Map the HCI fail reason to bt status */
2942 switch (p_auth_cmpl->fail_reason)
2943 {
Priti Aghera156c52b2014-07-09 14:58:19 -07002944 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
2945 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
2946 btif_dm_remove_ble_bonding_keys();
2947 status = BT_STATUS_AUTH_FAILURE;
2948 break;
2949 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
2950 status = BT_STATUS_AUTH_REJECTED;
2951 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002952 default:
Andre Eisenbachca22ac42013-02-13 17:02:11 +09002953 btif_dm_remove_ble_bonding_keys();
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002954 status = BT_STATUS_FAIL;
2955 break;
2956 }
2957 }
2958 bond_state_changed(status, &bd_addr, state);
2959}
2960
2961
2962
2963void btif_dm_load_ble_local_keys(void)
2964{
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002965 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2966
2967 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2968 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2969 {
2970 ble_local_key_cb.is_er_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002971 BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002972 }
2973
2974 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
2975 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
2976 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
2977 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
2978 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
2979 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
2980 {
2981 ble_local_key_cb.is_id_keys_rcvd = TRUE;
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07002982 BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002983 }
2984
2985}
2986void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
2987 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
2988{
2989 if (ble_local_key_cb.is_er_rcvd )
2990 {
2991 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2992 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2993 }
2994
2995 if (ble_local_key_cb.is_id_keys_rcvd)
2996 {
2997 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
2998 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
2999 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
3000 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
3001 }
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003002 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003003}
3004
3005void btif_dm_save_ble_bonding_keys(void)
3006{
3007
3008 bt_bdaddr_t bd_addr;
3009
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003010 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003011
3012 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3013
3014 if (pairing_cb.ble.is_penc_key_rcvd)
3015 {
3016 btif_storage_add_ble_bonding_key(&bd_addr,
3017 (char *) &pairing_cb.ble.penc_key,
3018 BTIF_DM_LE_KEY_PENC,
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07003019 sizeof(tBTM_LE_PENC_KEYS));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003020 }
3021
3022 if (pairing_cb.ble.is_pid_key_rcvd)
3023 {
3024 btif_storage_add_ble_bonding_key(&bd_addr,
Andre Eisenbach5e808462014-10-21 12:37:53 -07003025 (char *) &pairing_cb.ble.pid_key,
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003026 BTIF_DM_LE_KEY_PID,
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07003027 sizeof(tBTM_LE_PID_KEYS));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003028 }
3029
3030
3031 if (pairing_cb.ble.is_pcsrk_key_rcvd)
3032 {
3033 btif_storage_add_ble_bonding_key(&bd_addr,
3034 (char *) &pairing_cb.ble.pcsrk_key,
3035 BTIF_DM_LE_KEY_PCSRK,
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07003036 sizeof(tBTM_LE_PCSRK_KEYS));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003037 }
3038
3039
3040 if (pairing_cb.ble.is_lenc_key_rcvd)
3041 {
3042 btif_storage_add_ble_bonding_key(&bd_addr,
3043 (char *) &pairing_cb.ble.lenc_key,
3044 BTIF_DM_LE_KEY_LENC,
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07003045 sizeof(tBTM_LE_LENC_KEYS));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003046 }
3047
3048 if (pairing_cb.ble.is_lcsrk_key_rcvd)
3049 {
3050 btif_storage_add_ble_bonding_key(&bd_addr,
3051 (char *) &pairing_cb.ble.lcsrk_key,
3052 BTIF_DM_LE_KEY_LCSRK,
Andre Eisenbache87ef6d2015-05-01 13:14:59 -07003053 sizeof(tBTM_LE_LCSRK_KEYS));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003054 }
3055
Satya Calloji444a8da2015-03-06 10:38:22 -08003056 if (pairing_cb.ble.is_lidk_key_rcvd)
3057 {
3058 btif_storage_add_ble_bonding_key(&bd_addr,
3059 NULL,
3060 BTIF_DM_LE_KEY_LID,
3061 0);
3062 }
3063
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003064}
3065
3066
3067void btif_dm_remove_ble_bonding_keys(void)
3068{
3069 bt_bdaddr_t bd_addr;
3070
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003071 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003072
3073 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3074 btif_storage_remove_ble_bonding_keys(&bd_addr);
3075}
3076
3077
3078/*******************************************************************************
3079**
3080** Function btif_dm_ble_sec_req_evt
3081**
3082** Description Eprocess security request event in btif context
3083**
3084** Returns void
3085**
3086*******************************************************************************/
3087void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
3088{
3089 bt_bdaddr_t bd_addr;
3090 bt_bdname_t bd_name;
3091 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08003092 int dev_type;
3093
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003094 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003095
3096 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3097 {
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003098 BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003099 return;
3100 }
3101
3102 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08003103 if (!btif_get_device_type(p_ble_req->bd_addr, &dev_type))
3104 {
3105 dev_type = BT_DEVICE_TYPE_BLE;
3106 }
3107 btif_dm_update_ble_remote_properties(p_ble_req->bd_addr, p_ble_req->bd_name,
3108 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003109
3110 bdcpy(bd_addr.address, p_ble_req->bd_addr);
3111 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
3112
3113 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3114
Andre Eisenbach89363762015-01-26 13:49:36 -08003115 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003116 pairing_cb.is_le_only = TRUE;
Satya Calloji7fb0da62015-01-29 20:40:17 -08003117 pairing_cb.is_le_nc = FALSE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003118 pairing_cb.is_ssp = TRUE;
Pavlin Radoslavov65f9c042015-07-16 17:40:28 -07003119 btm_set_bond_type_dev(p_ble_req->bd_addr, pairing_cb.bond_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003120
3121 cod = COD_UNCLASSIFIED;
3122
3123 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
3124 BT_SSP_VARIANT_CONSENT, 0);
3125}
3126
3127
3128
3129/*******************************************************************************
3130**
3131** Function btif_dm_ble_passkey_req_evt
3132**
3133** Description Executes pin request event in btif context
3134**
3135** Returns void
3136**
3137*******************************************************************************/
3138static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
3139{
3140 bt_bdaddr_t bd_addr;
3141 bt_bdname_t bd_name;
3142 UINT32 cod;
Matthew Xie86f97ed2014-11-10 10:24:46 -08003143 int dev_type;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003144
3145 /* Remote name update */
Matthew Xie86f97ed2014-11-10 10:24:46 -08003146 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
3147 {
3148 dev_type = BT_DEVICE_TYPE_BLE;
3149 }
3150 btif_dm_update_ble_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,
3151 (tBT_DEVICE_TYPE) dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003152
3153 bdcpy(bd_addr.address, p_pin_req->bd_addr);
3154 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
3155
3156 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3157 pairing_cb.is_le_only = TRUE;
3158
3159 cod = COD_UNCLASSIFIED;
3160
3161 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
Casper Bonde818d0f22015-05-21 11:08:45 +02003162 &bd_addr, &bd_name, cod, FALSE);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003163}
Satya Calloji444a8da2015-03-06 10:38:22 -08003164static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF *p_notif_req)
3165{
3166 /* TODO implement key notification for numeric comparison */
3167 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003168
Satya Calloji444a8da2015-03-06 10:38:22 -08003169 /* Remote name update */
3170 btif_update_remote_properties(p_notif_req->bd_addr , p_notif_req->bd_name,
3171 NULL, BT_DEVICE_TYPE_BLE);
3172
3173 bt_bdaddr_t bd_addr;
3174 bdcpy(bd_addr.address, p_notif_req->bd_addr);
3175
3176 bt_bdname_t bd_name;
3177 memcpy(bd_name.name, p_notif_req->bd_name, BD_NAME_LEN);
3178
3179 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3180 pairing_cb.is_ssp = FALSE;
Satya Calloji7fb0da62015-01-29 20:40:17 -08003181 pairing_cb.is_le_only = TRUE;
3182 pairing_cb.is_le_nc = TRUE;
Satya Calloji444a8da2015-03-06 10:38:22 -08003183
3184 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
3185 COD_UNCLASSIFIED, BT_SSP_VARIANT_PASSKEY_CONFIRMATION,
3186 p_notif_req->passkey);
3187}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003188
3189void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
3190 tBT_DEVICE_TYPE dev_type)
3191{
3192 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
3193}
3194
3195static void btif_dm_ble_tx_test_cback(void *p)
3196{
3197 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
3198 (char *)p, 1, NULL);
3199}
3200
3201static void btif_dm_ble_rx_test_cback(void *p)
3202{
3203 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
3204 (char *)p, 1, NULL);
3205}
3206
3207static void btif_dm_ble_test_end_cback(void *p)
3208{
3209 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
3210 (char *)p, 3, NULL);
3211}
3212/*******************************************************************************
3213**
3214** Function btif_le_test_mode
3215**
3216** Description Sends a HCI BLE Test command to the Controller
3217**
3218** Returns BT_STATUS_SUCCESS on success
3219**
3220*******************************************************************************/
3221bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
3222{
3223 switch (opcode) {
3224 case HCI_BLE_TRANSMITTER_TEST:
3225 if (len != 3) return BT_STATUS_PARM_INVALID;
3226 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
3227 break;
3228 case HCI_BLE_RECEIVER_TEST:
3229 if (len != 1) return BT_STATUS_PARM_INVALID;
3230 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
3231 break;
3232 case HCI_BLE_TEST_END:
3233 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
3234 break;
3235 default:
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003236 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003237 return BT_STATUS_UNSUPPORTED;
3238 }
3239 return BT_STATUS_SUCCESS;
3240}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003241#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -08003242
3243void btif_dm_on_disable()
3244{
3245 /* cancel any pending pairing requests */
3246 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3247 {
3248 bt_bdaddr_t bd_addr;
3249
Sharvil Nanavatie8c3d752014-05-04 10:12:26 -07003250 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
The Android Open Source Project5738f832012-12-12 16:00:35 -08003251 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3252 btif_dm_cancel_bond(&bd_addr);
3253 }
3254}
Matthew Xie1e5109b2012-11-09 18:26:26 -08003255
Satya Callojie5ba8842014-07-03 17:18:02 -07003256/*******************************************************************************
3257**
3258** Function btif_dm_read_energy_info
3259**
3260** Description Reads the energy info from controller
3261**
3262** Returns void
3263**
3264*******************************************************************************/
3265void btif_dm_read_energy_info()
3266{
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003267#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
Satya Callojie5ba8842014-07-03 17:18:02 -07003268 BTA_DmBleGetEnergyInfo(bta_energy_info_cb);
Prerepa Viswanadham81b03192014-07-23 17:49:48 -07003269#endif
Satya Callojie5ba8842014-07-03 17:18:02 -07003270}
3271
Matthew Xie1e5109b2012-11-09 18:26:26 -08003272static char* btif_get_default_local_name() {
3273 if (btif_default_local_name[0] == '\0')
3274 {
3275 int max_len = sizeof(btif_default_local_name) - 1;
3276 if (BTM_DEF_LOCAL_NAME[0] != '\0')
3277 {
3278 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
3279 }
3280 else
3281 {
3282 char prop_model[PROPERTY_VALUE_MAX];
3283 property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
3284 strncpy(btif_default_local_name, prop_model, max_len);
3285 }
3286 btif_default_local_name[max_len] = '\0';
3287 }
3288 return btif_default_local_name;
3289}