blob: 8b9a137c1ff1e0691d0b7aaecf001c7ed9577a1e [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
3 * Copyright (C) 2009-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
Myles Watsonee96a3c2016-11-23 14:49:54 -080019/*******************************************************************************
The Android Open Source Project5738f832012-12-12 16:00:35 -080020 *
21 * Filename: btif_dm.c
22 *
23 * Description: Contains Device Management (DM) related functionality
24 *
25 *
Myles Watsonee96a3c2016-11-23 14:49:54 -080026 ******************************************************************************/
Sharvil Nanavati44802762014-12-23 23:08:58 -080027
Marie Janssen49120dc2015-07-07 16:47:20 -070028#define LOG_TAG "bt_btif_dm"
29
Marie Janssen49a86702015-07-08 11:48:57 -070030#include "btif_dm.h"
31
Jack Hef2af1c42016-12-13 01:59:12 -080032#include <base/logging.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>
Ajay Panickerc138fe42016-02-05 16:50:20 -080038#include <time.h>
The Android Open Source Project5738f832012-12-12 16:00:35 -080039#include <unistd.h>
40
Marie Janssena5764682016-10-10 13:38:30 -070041#include <mutex>
42
The Android Open Source Project5738f832012-12-12 16:00:35 -080043#include <hardware/bluetooth.h>
44
Marie Janssendb554582015-06-26 14:53:46 -070045#include "bdaddr.h"
Myles Watson6bd442f2016-10-19 09:50:22 -070046#include "bt_common.h"
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080047#include "bta_gatt_api.h"
Marie Janssendb554582015-06-26 14:53:46 -070048#include "btif_api.h"
49#include "btif_config.h"
Marie Janssendb554582015-06-26 14:53:46 -070050#include "btif_hh.h"
51#include "btif_sdp.h"
52#include "btif_storage.h"
53#include "btif_util.h"
54#include "btu.h"
Andre Eisenbach27c4e632015-07-06 15:43:15 -070055#include "device/include/interop.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080056#include "include/stack_config.h"
Andre Eisenbach8a057242015-04-29 22:27:15 -070057#include "osi/include/allocator.h"
Marie Janssendb554582015-06-26 14:53:46 -070058#include "osi/include/log.h"
Ajay Panickerc138fe42016-02-05 16:50:20 -080059#include "osi/include/metrics.h"
Myles Watsond7ffd642016-10-27 10:27:36 -070060#include "osi/include/osi.h"
Jakub Pawlowski3eb4a482016-02-24 10:39:46 -080061#include "osi/include/properties.h"
Pavlin Radoslavov65f9c042015-07-16 17:40:28 -070062#include "stack/btm/btm_int.h"
Myles Watson6bd442f2016-10-19 09:50:22 -070063#include "stack_config.h"
Marie Janssendb554582015-06-26 14:53:46 -070064
Andre Eisenbach31a64002014-10-14 14:29:19 -070065/******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -070066 * Constants & Macros
67 *****************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -080068
Myles Watson6bd442f2016-10-19 09:50:22 -070069#define COD_MASK 0x07FF
Ajay Panicker0ef9e062016-03-10 15:30:16 -080070
The Android Open Source Project5738f832012-12-12 16:00:35 -080071#define COD_UNCLASSIFIED ((0x1F) << 8)
Myles Watson6bd442f2016-10-19 09:50:22 -070072#define COD_HID_KEYBOARD 0x0540
73#define COD_HID_POINTING 0x0580
74#define COD_HID_COMBO 0x05C0
75#define COD_HID_MAJOR 0x0500
76#define COD_HID_MASK 0x0700
77#define COD_AV_HEADSETS 0x0404
78#define COD_AV_HANDSFREE 0x0408
79#define COD_AV_HEADPHONES 0x0418
80#define COD_AV_PORTABLE_AUDIO 0x041C
81#define COD_AV_HIFI_AUDIO 0x0428
The Android Open Source Project5738f832012-12-12 16:00:35 -080082
Myles Watson6bd442f2016-10-19 09:50:22 -070083#define BTIF_DM_DEFAULT_INQ_MAX_RESULTS 0
84#define BTIF_DM_DEFAULT_INQ_MAX_DURATION 10
Ganesh Ganapathi Battaec7e2c82013-06-20 11:00:28 -070085#define BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING 2
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080086
Myles Watson6bd442f2016-10-19 09:50:22 -070087#define NUM_TIMEOUT_RETRIES 5
Andre Eisenbach31a64002014-10-14 14:29:19 -070088
Matthew Xie1e5109b2012-11-09 18:26:26 -080089#define PROPERTY_PRODUCT_MODEL "ro.product.model"
Myles Watson6bd442f2016-10-19 09:50:22 -070090#define DEFAULT_LOCAL_NAME_MAX 31
Matthew Xie1e5109b2012-11-09 18:26:26 -080091#if (DEFAULT_LOCAL_NAME_MAX > BTM_MAX_LOC_BD_NAME_LEN)
Myles Watson6bd442f2016-10-19 09:50:22 -070092#error "default btif local name size exceeds stack supported length"
Matthew Xie1e5109b2012-11-09 18:26:26 -080093#endif
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080094
Marie Janssenb7f64bc2016-06-22 12:52:19 -070095#if (BTA_HOST_INTERLEAVE_SEARCH == TRUE)
Myles Watson6bd442f2016-10-19 09:50:22 -070096#define BTIF_DM_INTERLEAVE_DURATION_BR_ONE 2
97#define BTIF_DM_INTERLEAVE_DURATION_LE_ONE 2
98#define BTIF_DM_INTERLEAVE_DURATION_BR_TWO 3
99#define BTIF_DM_INTERLEAVE_DURATION_LE_TWO 4
Matthew Xie7f3e4292013-09-30 12:44:10 -0700100#endif
101
Myles Watson6bd442f2016-10-19 09:50:22 -0700102#define ENCRYPTED_BREDR 2
103#define ENCRYPTED_LE 4
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800104
Myles Watson6bd442f2016-10-19 09:50:22 -0700105typedef struct {
106 bt_bond_state_t state;
107 bt_bdaddr_t static_bdaddr;
108 BD_ADDR bd_addr;
109 tBTM_BOND_TYPE bond_type;
110 uint8_t pin_code_len;
111 uint8_t is_ssp;
112 uint8_t auth_req;
113 uint8_t io_cap;
114 uint8_t autopair_attempts;
115 uint8_t timeout_retries;
116 uint8_t is_local_initiated;
117 uint8_t sdp_attempts;
Myles Watson6bd442f2016-10-19 09:50:22 -0700118 bool is_le_only;
119 bool is_le_nc; /* LE Numeric comparison */
120 btif_dm_ble_cb_t ble;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800121} btif_dm_pairing_cb_t;
122
Myles Watson6bd442f2016-10-19 09:50:22 -0700123typedef struct {
124 uint8_t ir[BT_OCTET16_LEN];
125 uint8_t irk[BT_OCTET16_LEN];
126 uint8_t dhk[BT_OCTET16_LEN];
127} btif_dm_local_key_id_t;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800128
Myles Watson6bd442f2016-10-19 09:50:22 -0700129typedef struct {
130 bool is_er_rcvd;
131 uint8_t er[BT_OCTET16_LEN];
132 bool is_id_keys_rcvd;
133 btif_dm_local_key_id_t id_keys; /* ID kyes */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800134
Myles Watson6bd442f2016-10-19 09:50:22 -0700135} btif_dm_local_key_cb_t;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800136
Myles Watson6bd442f2016-10-19 09:50:22 -0700137typedef struct {
138 BD_ADDR bd_addr;
139 BD_NAME bd_name;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800140} btif_dm_remote_name_t;
141
Jakub Pawlowski1a5bb5f2015-12-01 12:14:22 -0800142/* this structure holds optional OOB data for remote device */
Myles Watson6bd442f2016-10-19 09:50:22 -0700143typedef struct {
144 BD_ADDR bdaddr; /* peer bdaddr */
145 bt_out_of_band_data_t oob_data;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800146} btif_dm_oob_cb_t;
Satya Callojie5ba8842014-07-03 17:18:02 -0700147
Myles Watson6bd442f2016-10-19 09:50:22 -0700148typedef struct {
149 bt_bdaddr_t bdaddr;
150 uint8_t transport; /* 0=Unknown, 1=BR/EDR, 2=LE */
Andre Eisenbachb0daa5d2014-08-04 17:50:10 -0700151} btif_dm_create_bond_cb_t;
152
Myles Watson6bd442f2016-10-19 09:50:22 -0700153typedef struct {
154 uint8_t status;
155 uint8_t ctrl_state;
156 uint64_t tx_time;
157 uint64_t rx_time;
158 uint64_t idle_time;
159 uint64_t energy_used;
Satya Callojie5ba8842014-07-03 17:18:02 -0700160} btif_activity_energy_info_cb_t;
161
Myles Watson6bd442f2016-10-19 09:50:22 -0700162typedef struct { unsigned int manufact_id; } skip_sdp_entry_t;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700163
Myles Watson6bd442f2016-10-19 09:50:22 -0700164typedef enum {
165 BTIF_DM_FUNC_CREATE_BOND,
166 BTIF_DM_FUNC_CANCEL_BOND,
167 BTIF_DM_FUNC_REMOVE_BOND,
168 BTIF_DM_FUNC_BOND_STATE_CHANGED,
Ajay Panickerc138fe42016-02-05 16:50:20 -0800169} bt_bond_function_t;
170
Myles Watson6bd442f2016-10-19 09:50:22 -0700171typedef struct {
172 bt_bdaddr_t bd_addr;
173 bt_bond_function_t function;
174 bt_bond_state_t state;
175 struct timespec timestamp;
Ajay Panickerc138fe42016-02-05 16:50:20 -0800176} btif_bond_event_t;
177
Myles Watson6bd442f2016-10-19 09:50:22 -0700178#define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800179
Priti Agherac0edf9f2014-06-26 11:23:51 -0700180#define UUID_HUMAN_INTERFACE_DEVICE "00001124-0000-1000-8000-00805f9b34fb"
181
Ajay Panickerc138fe42016-02-05 16:50:20 -0800182#define MAX_BTIF_BOND_EVENT_ENTRIES 15
183
Myles Watson6bd442f2016-10-19 09:50:22 -0700184static skip_sdp_entry_t sdp_blacklist[] = {{76}}; // Apple Mouse and Keyboard
Priti Agherac0edf9f2014-06-26 11:23:51 -0700185
The Android Open Source Project5738f832012-12-12 16:00:35 -0800186/* This flag will be true if HCI_Inquiry is in progress */
Marie Janssenb7f64bc2016-06-22 12:52:19 -0700187static bool btif_dm_inquiry_in_progress = false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800188
Myles Watsonee96a3c2016-11-23 14:49:54 -0800189/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -0700190 * Static variables
Myles Watsonee96a3c2016-11-23 14:49:54 -0800191 ******************************************************************************/
Myles Watson6bd442f2016-10-19 09:50:22 -0700192static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX + 1] = {'\0'};
Adam Lesinski0620f972015-12-02 22:15:08 -0800193static uid_set_t* uid_set = NULL;
Matthew Xie1e5109b2012-11-09 18:26:26 -0800194
Ajay Panickerc138fe42016-02-05 16:50:20 -0800195/* A circular array to keep track of the most recent bond events */
196static btif_bond_event_t btif_dm_bond_events[MAX_BTIF_BOND_EVENT_ENTRIES + 1];
197
Marie Janssena5764682016-10-10 13:38:30 -0700198static std::mutex bond_event_lock;
Ajay Panickerc138fe42016-02-05 16:50:20 -0800199
200/* |btif_num_bond_events| keeps track of the total number of events and can be
201 greater than |MAX_BTIF_BOND_EVENT_ENTRIES| */
202static size_t btif_num_bond_events = 0;
203static size_t btif_events_start_index = 0;
204static size_t btif_events_end_index = 0;
205
The Android Open Source Project5738f832012-12-12 16:00:35 -0800206/******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -0700207 * Static functions
208 *****************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -0800209static btif_dm_pairing_cb_t pairing_cb;
Myles Watson6bd442f2016-10-19 09:50:22 -0700210static btif_dm_oob_cb_t oob_cb;
Marie Janssenb7f64bc2016-06-22 12:52:19 -0700211static void btif_dm_generic_evt(uint16_t event, char* p_param);
Myles Watson6bd442f2016-10-19 09:50:22 -0700212static void btif_dm_cb_create_bond(bt_bdaddr_t* bd_addr,
213 tBTA_TRANSPORT transport);
214static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME* p_remote_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800215static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
Myles Watson6bd442f2016-10-19 09:50:22 -0700216 DEV_CLASS dev_class,
217 tBT_DEVICE_TYPE dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800218static btif_dm_local_key_cb_t ble_local_key_cb;
Myles Watson6bd442f2016-10-19 09:50:22 -0700219static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF* p_ssp_key_notif);
220static void btif_dm_ble_auth_cmpl_evt(tBTA_DM_AUTH_CMPL* p_auth_cmpl);
221static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ* p_pin_req);
222static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF* p_notif_req);
223static void btif_dm_ble_oob_req_evt(tBTA_DM_SP_RMT_OOB* req_oob_type);
224static void btif_dm_ble_sc_oob_req_evt(tBTA_DM_SP_RMT_OOB* req_oob_type);
Satya Calloji6e2d9db2014-07-08 16:18:58 -0700225
Marie Janssenb7f64bc2016-06-22 12:52:19 -0700226static void bte_scan_filt_param_cfg_evt(uint8_t action_type,
Myles Watson6bd442f2016-10-19 09:50:22 -0700227 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
228 tBTA_DM_BLE_REF_VALUE ref_value,
229 tBTA_STATUS status);
Satya Calloji6e2d9db2014-07-08 16:18:58 -0700230
Matthew Xie1e5109b2012-11-09 18:26:26 -0800231static char* btif_get_default_local_name();
Ajay Panickerc138fe42016-02-05 16:50:20 -0800232
Myles Watson6bd442f2016-10-19 09:50:22 -0700233static void btif_stats_add_bond_event(const bt_bdaddr_t* bd_addr,
Ajay Panickerc138fe42016-02-05 16:50:20 -0800234 bt_bond_function_t function,
235 bt_bond_state_t state);
236
The Android Open Source Project5738f832012-12-12 16:00:35 -0800237/******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -0700238 * Externs
239 *****************************************************************************/
Marie Janssenb7f64bc2016-06-22 12:52:19 -0700240extern bt_status_t btif_hf_execute_service(bool b_enable);
241extern bt_status_t btif_av_execute_service(bool b_enable);
242extern bt_status_t btif_av_sink_execute_service(bool b_enable);
243extern bt_status_t btif_hh_execute_service(bool b_enable);
244extern bt_status_t btif_hf_client_execute_service(bool b_enable);
245extern bt_status_t btif_sdp_execute_service(bool b_enable);
Myles Watson6bd442f2016-10-19 09:50:22 -0700246extern int btif_hh_connect(bt_bdaddr_t* bd_addr);
247extern void bta_gatt_convert_uuid16_to_uuid128(uint8_t uuid_128[LEN_UUID_128],
248 uint16_t uuid_16);
Ayan Ghosh583b8172013-12-06 17:11:11 +0530249extern void btif_av_move_idle(bt_bdaddr_t bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800250
The Android Open Source Project5738f832012-12-12 16:00:35 -0800251/******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -0700252 * Functions
253 *****************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -0800254
Myles Watson6bd442f2016-10-19 09:50:22 -0700255static bool is_empty_128bit(uint8_t* data) {
256 static const uint8_t zero[16] = {0};
257 return !memcmp(zero, data, sizeof(zero));
Jakub Pawlowski1a5bb5f2015-12-01 12:14:22 -0800258}
259
Myles Watson6bd442f2016-10-19 09:50:22 -0700260static void btif_dm_data_copy(uint16_t event, char* dst, char* src) {
261 tBTA_DM_SEC* dst_dm_sec = (tBTA_DM_SEC*)dst;
262 tBTA_DM_SEC* src_dm_sec = (tBTA_DM_SEC*)src;
Andre Eisenbach8a057242015-04-29 22:27:15 -0700263
Myles Watson6bd442f2016-10-19 09:50:22 -0700264 if (!src_dm_sec) return;
Andre Eisenbach8a057242015-04-29 22:27:15 -0700265
Jack Hef2af1c42016-12-13 01:59:12 -0800266 CHECK(dst_dm_sec);
Myles Watson6bd442f2016-10-19 09:50:22 -0700267 maybe_non_aligned_memcpy(dst_dm_sec, src_dm_sec, sizeof(*src_dm_sec));
Andre Eisenbach8a057242015-04-29 22:27:15 -0700268
Myles Watson6bd442f2016-10-19 09:50:22 -0700269 if (event == BTA_DM_BLE_KEY_EVT) {
270 dst_dm_sec->ble_key.p_key_value =
271 (tBTM_LE_KEY_VALUE*)osi_malloc(sizeof(tBTM_LE_KEY_VALUE));
Jack Hef2af1c42016-12-13 01:59:12 -0800272 CHECK(src_dm_sec->ble_key.p_key_value);
Myles Watson6bd442f2016-10-19 09:50:22 -0700273 memcpy(dst_dm_sec->ble_key.p_key_value, src_dm_sec->ble_key.p_key_value,
274 sizeof(tBTM_LE_KEY_VALUE));
275 }
Andre Eisenbach8a057242015-04-29 22:27:15 -0700276}
277
Myles Watson6bd442f2016-10-19 09:50:22 -0700278static void btif_dm_data_free(uint16_t event, tBTA_DM_SEC* dm_sec) {
279 if (event == BTA_DM_BLE_KEY_EVT)
280 osi_free_and_reset((void**)&dm_sec->ble_key.p_key_value);
Andre Eisenbach8a057242015-04-29 22:27:15 -0700281}
282
Myles Watson6bd442f2016-10-19 09:50:22 -0700283void btif_dm_init(uid_set_t* set) { uid_set = set; }
Adam Lesinski0620f972015-12-02 22:15:08 -0800284
Myles Watson6bd442f2016-10-19 09:50:22 -0700285void btif_dm_cleanup(void) {
286 if (uid_set) {
287 uid_set_destroy(uid_set);
288 uid_set = NULL;
289 }
Adam Lesinski0620f972015-12-02 22:15:08 -0800290}
291
The Android Open Source Project5738f832012-12-12 16:00:35 -0800292bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
Myles Watson6bd442f2016-10-19 09:50:22 -0700293 bool b_enable) {
294 BTIF_TRACE_DEBUG("%s service_id: %d", __func__, service_id);
295 /* Check the service_ID and invoke the profile's BT state changed API */
296 switch (service_id) {
297 case BTA_HFP_SERVICE_ID:
298 case BTA_HSP_SERVICE_ID: {
299 btif_hf_execute_service(b_enable);
300 } break;
301 case BTA_A2DP_SOURCE_SERVICE_ID: {
302 btif_av_execute_service(b_enable);
303 } break;
304 case BTA_A2DP_SINK_SERVICE_ID: {
305 btif_av_sink_execute_service(b_enable);
306 } break;
307 case BTA_HID_SERVICE_ID: {
308 btif_hh_execute_service(b_enable);
309 } break;
310 case BTA_HFP_HS_SERVICE_ID: {
311 btif_hf_client_execute_service(b_enable);
312 } break;
313 case BTA_SDP_SERVICE_ID: {
314 btif_sdp_execute_service(b_enable);
315 } break;
316 default:
317 BTIF_TRACE_ERROR("%s: Unknown service %d being %s", __func__, service_id,
318 (b_enable) ? "enabled" : "disabled");
319 return BT_STATUS_FAIL;
320 }
321 return BT_STATUS_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800322}
323
324/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -0700325 *
326 * Function check_eir_remote_name
327 *
328 * Description Check if remote name is in the EIR data
329 *
330 * Returns true if remote name found
331 * Populate p_remote_name, if provided and remote name found
332 *
333 ******************************************************************************/
334static bool check_eir_remote_name(tBTA_DM_SEARCH* p_search_data,
335 uint8_t* p_remote_name,
336 uint8_t* p_remote_name_len) {
337 uint8_t* p_eir_remote_name = NULL;
338 uint8_t remote_name_len = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800339
Myles Watson6bd442f2016-10-19 09:50:22 -0700340 /* Check EIR for remote name and services */
341 if (p_search_data->inq_res.p_eir) {
342 p_eir_remote_name =
343 BTM_CheckEirData(p_search_data->inq_res.p_eir,
344 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
345 if (!p_eir_remote_name) {
346 p_eir_remote_name =
347 BTM_CheckEirData(p_search_data->inq_res.p_eir,
348 BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800349 }
350
Myles Watson6bd442f2016-10-19 09:50:22 -0700351 if (p_eir_remote_name) {
352 if (remote_name_len > BD_NAME_LEN) remote_name_len = BD_NAME_LEN;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800353
Myles Watson6bd442f2016-10-19 09:50:22 -0700354 if (p_remote_name && p_remote_name_len) {
355 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
356 *(p_remote_name + remote_name_len) = 0;
357 *p_remote_name_len = remote_name_len;
358 }
359
360 return true;
361 }
362 }
363
364 return false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800365}
366
367/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -0700368 *
369 * Function check_cached_remote_name
370 *
371 * Description Check if remote name is in the NVRAM cache
372 *
373 * Returns true if remote name found
374 * Populate p_remote_name, if provided and remote name found
375 *
376 ******************************************************************************/
377static bool check_cached_remote_name(tBTA_DM_SEARCH* p_search_data,
378 uint8_t* p_remote_name,
379 uint8_t* p_remote_name_len) {
380 bt_bdname_t bdname;
381 bt_bdaddr_t remote_bdaddr;
382 bt_property_t prop_name;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800383
Myles Watson6bd442f2016-10-19 09:50:22 -0700384 /* check if we already have it in our btif_storage cache */
385 bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
386 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
387 sizeof(bt_bdname_t), &bdname);
388 if (btif_storage_get_remote_device_property(&remote_bdaddr, &prop_name) ==
389 BT_STATUS_SUCCESS) {
390 if (p_remote_name && p_remote_name_len) {
391 strcpy((char*)p_remote_name, (char*)bdname.name);
392 *p_remote_name_len = strlen((char*)p_remote_name);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800393 }
Myles Watson6bd442f2016-10-19 09:50:22 -0700394 return true;
395 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800396
Myles Watson6bd442f2016-10-19 09:50:22 -0700397 return false;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800398}
399
Myles Watson6bd442f2016-10-19 09:50:22 -0700400static uint32_t get_cod(const bt_bdaddr_t* remote_bdaddr) {
401 uint32_t remote_cod;
402 bt_property_t prop_name;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800403
Myles Watson6bd442f2016-10-19 09:50:22 -0700404 /* check if we already have it in our btif_storage cache */
405 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
406 sizeof(uint32_t), &remote_cod);
407 if (btif_storage_get_remote_device_property(
408 (bt_bdaddr_t*)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS) {
409 LOG_INFO(LOG_TAG, "%s remote_cod = 0x%08x", __func__, remote_cod);
410 return remote_cod & COD_MASK;
411 }
412
413 return 0;
414}
415
416bool check_cod(const bt_bdaddr_t* remote_bdaddr, uint32_t cod) {
417 return get_cod(remote_bdaddr) == cod;
418}
419
420bool check_cod_hid(const bt_bdaddr_t* remote_bdaddr) {
421 return (get_cod(remote_bdaddr) & COD_HID_MASK) == COD_HID_MAJOR;
422}
423
424bool check_hid_le(const bt_bdaddr_t* remote_bdaddr) {
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(
432 (bt_bdaddr_t*)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS) {
433 if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE) {
434 bdstr_t bdstr;
435 bdaddr_to_string(remote_bdaddr, bdstr, sizeof(bdstr));
436 if (btif_config_exist(bdstr, "HidAppId")) return true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800437 }
Myles Watson6bd442f2016-10-19 09:50:22 -0700438 }
439 return false;
Priti Agheraebb1d752012-11-27 18:03:22 -0800440}
441
Priti Agherac0edf9f2014-06-26 11:23:51 -0700442/*****************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -0700443 *
444 * Function check_sdp_bl
445 *
446 * Description Checks if a given device is blacklisted to skip sdp
447 *
448 * Parameters skip_sdp_entry
449 *
450 * Returns true if the device is present in blacklist, else false
451 *
452 ******************************************************************************/
453bool check_sdp_bl(const bt_bdaddr_t* remote_bdaddr) {
454 uint16_t manufacturer = 0;
455 uint8_t lmp_ver = 0;
456 uint16_t lmp_subver = 0;
457 bt_property_t prop_name;
458 bt_remote_version_t info;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700459
Myles Watson6bd442f2016-10-19 09:50:22 -0700460 if (remote_bdaddr == NULL) return false;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700461
Myles Watson6bd442f2016-10-19 09:50:22 -0700462 /* fetch additional info about remote device used in iop query */
463 BTM_ReadRemoteVersion(*(BD_ADDR*)remote_bdaddr, &lmp_ver, &manufacturer,
464 &lmp_subver);
Priti Agherac0edf9f2014-06-26 11:23:51 -0700465
Myles Watson6bd442f2016-10-19 09:50:22 -0700466 /* if not available yet, try fetching from config database */
467 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO,
468 sizeof(bt_remote_version_t), &info);
Priti Agherac0edf9f2014-06-26 11:23:51 -0700469
Myles Watson6bd442f2016-10-19 09:50:22 -0700470 if (btif_storage_get_remote_device_property(
471 (bt_bdaddr_t*)remote_bdaddr, &prop_name) != BT_STATUS_SUCCESS) {
Marie Janssenb7f64bc2016-06-22 12:52:19 -0700472 return false;
Myles Watson6bd442f2016-10-19 09:50:22 -0700473 }
474 manufacturer = info.manufacturer;
475
476 for (unsigned int i = 0; i < ARRAY_SIZE(sdp_blacklist); i++) {
477 if (manufacturer == sdp_blacklist[i].manufact_id) return true;
478 }
479 return false;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700480}
481
Myles Watson6bd442f2016-10-19 09:50:22 -0700482static void bond_state_changed(bt_status_t status, bt_bdaddr_t* bd_addr,
483 bt_bond_state_t state) {
484 btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_BOND_STATE_CHANGED, state);
Ajay Panickerc138fe42016-02-05 16:50:20 -0800485
Myles Watson6bd442f2016-10-19 09:50:22 -0700486 // Send bonding state only once - based on outgoing/incoming we may receive
487 // duplicates
488 if ((pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING)) {
489 // Cross key pairing so send callback for static address
490 if (!bdaddr_is_empty(&pairing_cb.static_bdaddr)) {
491 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
Chaojing Sune2805532015-04-22 13:40:21 -0700492 }
Myles Watson6bd442f2016-10-19 09:50:22 -0700493 return;
494 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800495
Myles Watson6bd442f2016-10-19 09:50:22 -0700496 if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY) state = BT_BOND_STATE_NONE;
Chaojing Sune2805532015-04-22 13:40:21 -0700497
Myles Watson6bd442f2016-10-19 09:50:22 -0700498 BTIF_TRACE_DEBUG("%s: state=%d, prev_state=%d, sdp_attempts = %d", __func__,
499 state, pairing_cb.state, pairing_cb.sdp_attempts);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800500
Myles Watson6bd442f2016-10-19 09:50:22 -0700501 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800502
Myles Watson6bd442f2016-10-19 09:50:22 -0700503 if (state == BT_BOND_STATE_BONDING) {
504 pairing_cb.state = state;
505 bdcpy(pairing_cb.bd_addr, bd_addr->address);
506 } else {
507 if (!pairing_cb.sdp_attempts)
508 memset(&pairing_cb, 0, sizeof(pairing_cb));
509 else
510 BTIF_TRACE_DEBUG("%s: BR-EDR service discovery active", __func__);
511 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800512}
513
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800514/* store remote version in bt config to always have access
515 to it post pairing*/
Myles Watson6bd442f2016-10-19 09:50:22 -0700516static void btif_update_remote_version_property(bt_bdaddr_t* p_bd) {
517 bt_property_t property;
518 uint8_t lmp_ver = 0;
519 uint16_t lmp_subver = 0;
520 uint16_t mfct_set = 0;
521 tBTM_STATUS btm_status;
522 bt_remote_version_t info;
523 bt_status_t status;
524 bdstr_t bdstr;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800525
Myles Watson6bd442f2016-10-19 09:50:22 -0700526 btm_status =
527 BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver, &mfct_set, &lmp_subver);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800528
Myles Watson6bd442f2016-10-19 09:50:22 -0700529 LOG_DEBUG(LOG_TAG, "remote version info [%s]: %x, %x, %x",
530 bdaddr_to_string(p_bd, bdstr, sizeof(bdstr)), lmp_ver, mfct_set,
531 lmp_subver);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800532
Myles Watson6bd442f2016-10-19 09:50:22 -0700533 if (btm_status == BTM_SUCCESS) {
534 // Always update cache to ensure we have availability whenever BTM API is
535 // not populated
536 info.manufacturer = mfct_set;
537 info.sub_ver = lmp_subver;
538 info.version = lmp_ver;
539 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_REMOTE_VERSION_INFO,
540 sizeof(bt_remote_version_t), &info);
541 status = btif_storage_set_remote_device_property(p_bd, &property);
542 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version",
543 status);
544 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800545}
546
The Android Open Source Project5738f832012-12-12 16:00:35 -0800547static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
Myles Watson6bd442f2016-10-19 09:50:22 -0700548 DEV_CLASS dev_class,
549 tBT_DEVICE_TYPE device_type) {
550 int num_properties = 0;
551 bt_property_t properties[3];
552 bt_bdaddr_t bdaddr;
553 bt_status_t status;
554 uint32_t cod;
555 bt_device_type_t dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800556
Myles Watson6bd442f2016-10-19 09:50:22 -0700557 memset(properties, 0, sizeof(properties));
558 bdcpy(bdaddr.address, bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800559
Myles Watson6bd442f2016-10-19 09:50:22 -0700560 /* remote name */
561 if (strlen((const char*)bd_name)) {
562 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], BT_PROPERTY_BDNAME,
563 strlen((char*)bd_name), bd_name);
564 status = btif_storage_set_remote_device_property(
565 &bdaddr, &properties[num_properties]);
566 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name",
567 status);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800568 num_properties++;
Myles Watson6bd442f2016-10-19 09:50:22 -0700569 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800570
Myles Watson6bd442f2016-10-19 09:50:22 -0700571 /* class of device */
572 cod = devclass2uint(dev_class);
573 BTIF_TRACE_DEBUG("%s cod is 0x%06x", __func__, cod);
574 if (cod == 0) {
575 /* Try to retrieve cod from storage */
576 BTIF_TRACE_DEBUG("%s cod is 0, checking cod from storage", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800577 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
Myles Watson6bd442f2016-10-19 09:50:22 -0700578 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
579 status = btif_storage_get_remote_device_property(
580 &bdaddr, &properties[num_properties]);
581 BTIF_TRACE_DEBUG("%s cod retrieved from storage is 0x%06x", __func__, cod);
582 if (cod == 0) {
583 BTIF_TRACE_DEBUG("%s cod is again 0, set as unclassified", __func__);
584 cod = COD_UNCLASSIFIED;
585 }
586 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800587
Myles Watson6bd442f2016-10-19 09:50:22 -0700588 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
589 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
590 status = btif_storage_set_remote_device_property(&bdaddr,
591 &properties[num_properties]);
592 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class",
593 status);
594 num_properties++;
595
596 /* device type */
597 bt_property_t prop_name;
598 uint8_t remote_dev_type;
599 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_TYPE_OF_DEVICE,
600 sizeof(uint8_t), &remote_dev_type);
601 if (btif_storage_get_remote_device_property(&bdaddr, &prop_name) ==
602 BT_STATUS_SUCCESS)
603 dev_type = (bt_device_type_t)(remote_dev_type | device_type);
604 else
605 dev_type = (bt_device_type_t)device_type;
606
607 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
608 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type),
609 &dev_type);
610 status = btif_storage_set_remote_device_property(&bdaddr,
611 &properties[num_properties]);
612 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type",
613 status);
614 num_properties++;
615
616 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, status, &bdaddr,
617 num_properties, properties);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800618}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800619
620/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -0700621 *
622 * Function btif_dm_cb_hid_remote_name
623 *
624 * Description Remote name callback for HID device. Called in btif context
625 * Special handling for HID devices
626 *
627 * Returns void
628 *
629 ******************************************************************************/
630static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME* p_remote_name) {
631 BTIF_TRACE_DEBUG("%s: status=%d pairing_cb.state=%d", __func__,
632 p_remote_name->status, pairing_cb.state);
633 if (pairing_cb.state == BT_BOND_STATE_BONDING) {
634 bt_bdaddr_t remote_bd;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800635
Myles Watson6bd442f2016-10-19 09:50:22 -0700636 bdcpy(remote_bd.address, pairing_cb.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800637
Myles Watson6bd442f2016-10-19 09:50:22 -0700638 if (p_remote_name->status == BTM_SUCCESS) {
639 bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
640 } else
641 bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
642 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800643}
644
The Android Open Source Project5738f832012-12-12 16:00:35 -0800645/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -0700646 *
647 * Function btif_dm_cb_create_bond
648 *
649 * Description Create bond initiated from the BTIF thread context
650 * Special handling for HID devices
651 *
652 * Returns void
653 *
654 ******************************************************************************/
655static void btif_dm_cb_create_bond(bt_bdaddr_t* bd_addr,
656 tBTA_TRANSPORT transport) {
657 bool is_hid = check_cod(bd_addr, COD_HID_POINTING);
658 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800659
Myles Watson6bd442f2016-10-19 09:50:22 -0700660 int device_type;
661 int addr_type;
662 bdstr_t bdstr;
663 bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr));
664 if (transport == BT_TRANSPORT_LE) {
665 if (!btif_config_get_int((char const*)&bdstr, "DevType", &device_type)) {
666 btif_config_set_int(bdstr, "DevType", BT_DEVICE_TYPE_BLE);
Matthew Xie64c54792014-09-16 00:55:03 -0700667 }
Myles Watson6bd442f2016-10-19 09:50:22 -0700668 if (btif_storage_get_remote_addr_type(bd_addr, &addr_type) !=
669 BT_STATUS_SUCCESS) {
Jakub Pawlowski7921e8f2016-11-09 16:39:10 -0800670 // Try to read address type. OOB pairing might have set it earlier, but
671 // didn't store it, it defaults to BLE_ADDR_PUBLIC
672 uint8_t tmp_dev_type;
673 uint8_t tmp_addr_type;
674 BTM_ReadDevInfo(bd_addr->address, &tmp_dev_type, &tmp_addr_type);
675 addr_type = tmp_addr_type;
676
677 btif_storage_set_remote_addr_type(bd_addr, addr_type);
Thomas.TT_Lin2772dac2014-07-18 12:10:59 +0800678 }
Myles Watson6bd442f2016-10-19 09:50:22 -0700679 }
680 if ((btif_config_get_int((char const*)&bdstr, "DevType", &device_type) &&
681 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) ==
682 BT_STATUS_SUCCESS) &&
683 (device_type & BT_DEVICE_TYPE_BLE) == BT_DEVICE_TYPE_BLE) ||
684 (transport == BT_TRANSPORT_LE)) {
685 BTA_DmAddBleDevice(bd_addr->address, addr_type, device_type);
686 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800687
Myles Watson90088882016-11-15 16:33:22 -0800688 if (is_hid && (device_type & BT_DEVICE_TYPE_BLE) == 0) {
Myles Watson6bd442f2016-10-19 09:50:22 -0700689 bt_status_t status;
690 status = (bt_status_t)btif_hh_connect(bd_addr);
691 if (status != BT_STATUS_SUCCESS)
692 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
693 } else {
694 BTA_DmBondByTransport((uint8_t*)bd_addr->address, transport);
695 }
696 /* Track originator of bond creation */
697 pairing_cb.is_local_initiated = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800698}
699
700/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -0700701 *
702 * Function btif_dm_cb_remove_bond
703 *
704 * Description remove bond initiated from the BTIF thread context
705 * Special handling for HID devices
706 *
707 * Returns void
708 *
709 ******************************************************************************/
710void btif_dm_cb_remove_bond(bt_bdaddr_t* bd_addr) {
711/*special handling for HID devices */
712/* VUP needs to be sent if its a HID Device. The HID HOST module will check if
713there
714is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
Marie Janssenb7f64bc2016-06-22 12:52:19 -0700715#if (BTA_HH_INCLUDED == TRUE)
Myles Watson6bd442f2016-10-19 09:50:22 -0700716 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
Ganesh Ganapathi Batta390c94d2013-05-15 17:58:35 -0700717#endif
Myles Watson6bd442f2016-10-19 09:50:22 -0700718 {
719 BTIF_TRACE_DEBUG("%s: Removing HH device", __func__);
720 BTA_DmRemoveDevice((uint8_t*)bd_addr->address);
721 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800722}
723
724/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -0700725 *
726 * Function btif_dm_get_connection_state
727 *
728 * Description Returns whether the remote device is currently connected
729 * and whether encryption is active for the connection
730 *
731 * Returns 0 if not connected; 1 if connected and > 1 if connection is
732 * encrypted
733 *
734 ******************************************************************************/
735uint16_t btif_dm_get_connection_state(const bt_bdaddr_t* bd_addr) {
736 uint8_t* bda = (uint8_t*)bd_addr->address;
737 uint16_t rc = BTA_DmGetConnectionState(bda);
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800738
Myles Watson6bd442f2016-10-19 09:50:22 -0700739 if (rc != 0) {
740 uint8_t flags = 0;
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800741
Myles Watson6bd442f2016-10-19 09:50:22 -0700742 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_BR_EDR);
743 BTIF_TRACE_DEBUG("%s: security flags (BR/EDR)=0x%02x", __func__, flags);
744 if (flags & BTM_SEC_FLAG_ENCRYPTED) rc |= ENCRYPTED_BREDR;
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800745
Myles Watson6bd442f2016-10-19 09:50:22 -0700746 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_LE);
747 BTIF_TRACE_DEBUG("%s: security flags (LE)=0x%02x", __func__, flags);
748 if (flags & BTM_SEC_FLAG_ENCRYPTED) rc |= ENCRYPTED_LE;
749 }
Andre Eisenbachdfb3b2f2015-02-05 20:00:45 -0800750
Myles Watson6bd442f2016-10-19 09:50:22 -0700751 return rc;
Andre Eisenbach249f6002014-06-18 12:20:37 -0700752}
753
754/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -0700755 *
756 * Function search_devices_copy_cb
757 *
758 * Description Deep copy callback for search devices event
759 *
760 * Returns void
761 *
762 ******************************************************************************/
763static void search_devices_copy_cb(uint16_t event, char* p_dest, char* p_src) {
764 tBTA_DM_SEARCH* p_dest_data = (tBTA_DM_SEARCH*)p_dest;
765 tBTA_DM_SEARCH* p_src_data = (tBTA_DM_SEARCH*)p_src;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800766
Myles Watson6bd442f2016-10-19 09:50:22 -0700767 if (!p_src) return;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800768
Myles Watson6bd442f2016-10-19 09:50:22 -0700769 BTIF_TRACE_DEBUG("%s: event=%s", __func__, dump_dm_search_event(event));
770 maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data));
771 switch (event) {
772 case BTA_DM_INQ_RES_EVT: {
773 if (p_src_data->inq_res.p_eir) {
774 p_dest_data->inq_res.p_eir =
775 (uint8_t*)(p_dest + sizeof(tBTA_DM_SEARCH));
776 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir,
777 HCI_EXT_INQ_RESPONSE_LEN);
778 }
779 } break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800780
Myles Watson6bd442f2016-10-19 09:50:22 -0700781 case BTA_DM_DISC_RES_EVT: {
782 if (p_src_data->disc_res.raw_data_size &&
783 p_src_data->disc_res.p_raw_data) {
784 p_dest_data->disc_res.p_raw_data =
785 (uint8_t*)(p_dest + sizeof(tBTA_DM_SEARCH));
786 memcpy(p_dest_data->disc_res.p_raw_data,
787 p_src_data->disc_res.p_raw_data,
788 p_src_data->disc_res.raw_data_size);
789 }
790 } break;
791 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800792}
793
Myles Watson6bd442f2016-10-19 09:50:22 -0700794static void search_services_copy_cb(uint16_t event, char* p_dest, char* p_src) {
795 tBTA_DM_SEARCH* p_dest_data = (tBTA_DM_SEARCH*)p_dest;
796 tBTA_DM_SEARCH* p_src_data = (tBTA_DM_SEARCH*)p_src;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800797
Myles Watson6bd442f2016-10-19 09:50:22 -0700798 if (!p_src) return;
799 maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data));
800 switch (event) {
801 case BTA_DM_DISC_RES_EVT: {
802 if (p_src_data->disc_res.result == BTA_SUCCESS) {
803 if (p_src_data->disc_res.num_uuids > 0) {
804 p_dest_data->disc_res.p_uuid_list =
805 (uint8_t*)(p_dest + sizeof(tBTA_DM_SEARCH));
806 memcpy(p_dest_data->disc_res.p_uuid_list,
807 p_src_data->disc_res.p_uuid_list,
808 p_src_data->disc_res.num_uuids * MAX_UUID_SIZE);
809 osi_free_and_reset((void**)&p_src_data->disc_res.p_uuid_list);
810 }
811 osi_free_and_reset((void**)&p_src_data->disc_res.p_raw_data);
812 }
813 } break;
814 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800815}
816/******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -0700817 *
818 * BTIF DM callback events
819 *
820 ****************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -0800821
822/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -0700823 *
824 * Function btif_dm_pin_req_evt
825 *
826 * Description Executes pin request event in btif context
827 *
828 * Returns void
829 *
830 ******************************************************************************/
831static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ* p_pin_req) {
832 bt_bdaddr_t bd_addr;
833 bt_bdname_t bd_name;
834 uint32_t cod;
835 bt_pin_code_t pin_code;
836 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800837
Myles Watson6bd442f2016-10-19 09:50:22 -0700838 /* Remote properties update */
839 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type)) {
840 dev_type = BT_DEVICE_TYPE_BREDR;
841 }
842 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
843 p_pin_req->dev_class,
844 (tBT_DEVICE_TYPE)dev_type);
845
846 bdcpy(bd_addr.address, p_pin_req->bd_addr);
847 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
848
849 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
850
851 cod = devclass2uint(p_pin_req->dev_class);
852
853 if (cod == 0) {
854 BTIF_TRACE_DEBUG("%s cod is 0, set as unclassified", __func__);
855 cod = COD_UNCLASSIFIED;
856 }
857
858 /* check for auto pair possiblity only if bond was initiated by local device
859 */
860 if (pairing_cb.is_local_initiated && (p_pin_req->min_16_digit == false)) {
861 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
862 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
863 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
864 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
865 check_cod(&bd_addr, COD_HID_POINTING)) {
866 /* Check if this device can be auto paired */
867 if (!interop_match_addr(INTEROP_DISABLE_AUTO_PAIRING, &bd_addr) &&
868 !interop_match_name(INTEROP_DISABLE_AUTO_PAIRING,
869 (const char*)bd_name.name) &&
870 (pairing_cb.autopair_attempts == 0)) {
871 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __func__);
872 pin_code.pin[0] = 0x30;
873 pin_code.pin[1] = 0x30;
874 pin_code.pin[2] = 0x30;
875 pin_code.pin[3] = 0x30;
876
877 pairing_cb.autopair_attempts++;
878 BTA_DmPinReply((uint8_t*)bd_addr.address, true, 4, pin_code.pin);
879 return;
880 }
881 } else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
882 check_cod(&bd_addr, COD_HID_COMBO)) {
883 if ((interop_match_addr(INTEROP_KEYBOARD_REQUIRES_FIXED_PIN, &bd_addr) ==
884 true) &&
885 (pairing_cb.autopair_attempts == 0)) {
886 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __func__);
887 pin_code.pin[0] = 0x30;
888 pin_code.pin[1] = 0x30;
889 pin_code.pin[2] = 0x30;
890 pin_code.pin[3] = 0x30;
891
892 pairing_cb.autopair_attempts++;
893 BTA_DmPinReply((uint8_t*)bd_addr.address, true, 4, pin_code.pin);
894 return;
895 }
Matthew Xie86f97ed2014-11-10 10:24:46 -0800896 }
Myles Watson6bd442f2016-10-19 09:50:22 -0700897 }
898 HAL_CBACK(bt_hal_cbacks, pin_request_cb, &bd_addr, &bd_name, cod,
899 p_pin_req->min_16_digit);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800900}
901
902/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -0700903 *
904 * Function btif_dm_ssp_cfm_req_evt
905 *
906 * Description Executes SSP confirm request event in btif context
907 *
908 * Returns void
909 *
910 ******************************************************************************/
911static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ* p_ssp_cfm_req) {
912 bt_bdaddr_t bd_addr;
913 bt_bdname_t bd_name;
914 uint32_t cod;
915 bool is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
916 int dev_type;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800917
Myles Watson6bd442f2016-10-19 09:50:22 -0700918 BTIF_TRACE_DEBUG("%s", __func__);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800919
Myles Watson6bd442f2016-10-19 09:50:22 -0700920 /* Remote properties update */
921 if (!btif_get_device_type(p_ssp_cfm_req->bd_addr, &dev_type)) {
922 dev_type = BT_DEVICE_TYPE_BREDR;
923 }
924 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
925 p_ssp_cfm_req->dev_class,
926 (tBT_DEVICE_TYPE)dev_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800927
Myles Watson6bd442f2016-10-19 09:50:22 -0700928 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
929 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800930
Myles Watson6bd442f2016-10-19 09:50:22 -0700931 /* Set the pairing_cb based on the local & remote authentication requirements
932 */
933 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800934
Myles Watson6bd442f2016-10-19 09:50:22 -0700935 /* if just_works and bonding bit is not set treat this as temporary */
936 if (p_ssp_cfm_req->just_works &&
937 !(p_ssp_cfm_req->loc_auth_req & BTM_AUTH_BONDS) &&
938 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
939 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
940 pairing_cb.bond_type = BOND_TYPE_TEMPORARY;
941 else
942 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800943
Myles Watson6bd442f2016-10-19 09:50:22 -0700944 btm_set_bond_type_dev(p_ssp_cfm_req->bd_addr, pairing_cb.bond_type);
Pavlin Radoslavov65f9c042015-07-16 17:40:28 -0700945
Myles Watson6bd442f2016-10-19 09:50:22 -0700946 pairing_cb.is_ssp = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800947
Myles Watson6bd442f2016-10-19 09:50:22 -0700948 /* If JustWorks auto-accept */
949 if (p_ssp_cfm_req->just_works) {
950 /* Pairing consent for JustWorks needed if:
951 * 1. Incoming (non-temporary) pairing is detected AND
952 * 2. local IO capabilities are DisplayYesNo AND
953 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
954 */
955 if (is_incoming && pairing_cb.bond_type != BOND_TYPE_TEMPORARY &&
956 ((p_ssp_cfm_req->loc_io_caps == HCI_IO_CAP_DISPLAY_YESNO) &&
957 (p_ssp_cfm_req->rmt_io_caps == HCI_IO_CAP_DISPLAY_ONLY ||
958 p_ssp_cfm_req->rmt_io_caps == HCI_IO_CAP_NO_IO))) {
959 BTIF_TRACE_EVENT(
960 "%s: User consent needed for incoming pairing request. loc_io_caps: "
961 "%d, rmt_io_caps: %d",
962 __func__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
963 } else {
964 BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __func__);
965 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, true, 0);
Sharvil Nanavati0b7fc582016-01-05 16:23:02 -0800966 return;
967 }
Myles Watson6bd442f2016-10-19 09:50:22 -0700968 }
Sharvil Nanavati0b7fc582016-01-05 16:23:02 -0800969
Myles Watson6bd442f2016-10-19 09:50:22 -0700970 cod = devclass2uint(p_ssp_cfm_req->dev_class);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800971
Myles Watson6bd442f2016-10-19 09:50:22 -0700972 if (cod == 0) {
973 LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__);
974 cod = COD_UNCLASSIFIED;
975 }
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -0800976
Myles Watson6bd442f2016-10-19 09:50:22 -0700977 pairing_cb.sdp_attempts = 0;
978 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
979 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT
980 : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
981 p_ssp_cfm_req->num_val);
982}
Ganesh Ganapathi Battae17bf002013-02-15 17:52:29 -0800983
Myles Watson6bd442f2016-10-19 09:50:22 -0700984static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF* p_ssp_key_notif) {
985 bt_bdaddr_t bd_addr;
986 bt_bdname_t bd_name;
987 uint32_t cod;
988 int dev_type;
Priti Agherac0edf9f2014-06-26 11:23:51 -0700989
Myles Watson6bd442f2016-10-19 09:50:22 -0700990 BTIF_TRACE_DEBUG("%s", __func__);
Priti Agherac0edf9f2014-06-26 11:23:51 -0700991
Myles Watson6bd442f2016-10-19 09:50:22 -0700992 /* Remote properties update */
993 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type)) {
994 dev_type = BT_DEVICE_TYPE_BREDR;
995 }
996 btif_update_remote_properties(
997 p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
998 p_ssp_key_notif->dev_class, (tBT_DEVICE_TYPE)dev_type);
Priti Agherac0edf9f2014-06-26 11:23:51 -0700999
Myles Watson6bd442f2016-10-19 09:50:22 -07001000 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
1001 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
Priti Agherac0edf9f2014-06-26 11:23:51 -07001002
Myles Watson6bd442f2016-10-19 09:50:22 -07001003 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1004 pairing_cb.is_ssp = true;
1005 cod = devclass2uint(p_ssp_key_notif->dev_class);
1006
1007 if (cod == 0) {
1008 LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__);
1009 cod = COD_UNCLASSIFIED;
1010 }
1011
1012 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
1013 BT_SSP_VARIANT_PASSKEY_NOTIFICATION, p_ssp_key_notif->passkey);
1014}
1015/*******************************************************************************
1016 *
1017 * Function btif_dm_auth_cmpl_evt
1018 *
1019 * Description Executes authentication complete event in btif context
1020 *
1021 * Returns void
1022 *
1023 ******************************************************************************/
1024static void btif_dm_auth_cmpl_evt(tBTA_DM_AUTH_CMPL* p_auth_cmpl) {
1025 /* Save link key, if not temporary */
1026 bt_bdaddr_t bd_addr;
1027 bt_status_t status = BT_STATUS_FAIL;
1028 bt_bond_state_t state = BT_BOND_STATE_NONE;
1029 bool skip_sdp = false;
1030
1031 BTIF_TRACE_DEBUG("%s: bond state=%d", __func__, pairing_cb.state);
1032
1033 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
1034 if ((p_auth_cmpl->success == true) && (p_auth_cmpl->key_present)) {
1035 if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) ||
1036 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
1037 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) ||
1038 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB_P_256) ||
1039 pairing_cb.bond_type == BOND_TYPE_PERSISTENT) {
1040 bt_status_t ret;
1041 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, bond_type=%d",
1042 __func__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1043 ret = btif_storage_add_bonded_device(&bd_addr, p_auth_cmpl->key,
1044 p_auth_cmpl->key_type,
1045 pairing_cb.pin_code_len);
1046 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
1047 } else {
1048 BTIF_TRACE_DEBUG(
1049 "%s: Temporary key. Not storing. key_type=0x%x, bond_type=%d",
1050 __func__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1051 if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY) {
1052 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
1053 __func__);
1054 btif_storage_remove_bonded_device(&bd_addr);
1055 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1056 return;
1057 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001058 }
Myles Watson6bd442f2016-10-19 09:50:22 -07001059 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001060
Myles Watson6bd442f2016-10-19 09:50:22 -07001061 // We could have received a new link key without going through the pairing
Nitin Arora6e8198a2016-04-13 15:53:03 -07001062 // flow. If so, we don't want to perform SDP or any other operations on the
1063 // authenticated device. Also, make sure that the link key is not derived from
1064 // secure LTK, because we will need to perform SDP in case of link key
1065 // derivation to allow bond state change notification for the BR/EDR transport
1066 // so that the subsequent BR/EDR connections to the remote can use the derived
1067 // link key.
1068 if ((bdcmp(p_auth_cmpl->bd_addr, pairing_cb.bd_addr) != 0) &&
1069 (!pairing_cb.ble.is_penc_key_rcvd)) {
Myles Watson6bd442f2016-10-19 09:50:22 -07001070 char address[32];
1071 bt_bdaddr_t bt_bdaddr;
Hemant Guptaaef7a672013-07-31 19:00:12 +05301072
Myles Watson6bd442f2016-10-19 09:50:22 -07001073 memcpy(bt_bdaddr.address, p_auth_cmpl->bd_addr, sizeof(bt_bdaddr.address));
1074 bdaddr_to_string(&bt_bdaddr, address, sizeof(address));
1075 LOG_INFO(LOG_TAG,
1076 "%s skipping SDP since we did not initiate pairing to %s.",
1077 __func__, address);
1078 return;
1079 }
Hemant Guptab4801442014-01-07 18:11:15 +05301080
Myles Watson6bd442f2016-10-19 09:50:22 -07001081 // Skip SDP for certain HID Devices
1082 if (p_auth_cmpl->success) {
Myles Watson6bd442f2016-10-19 09:50:22 -07001083 btif_storage_set_remote_addr_type(&bd_addr, p_auth_cmpl->addr_type);
Myles Watson6bd442f2016-10-19 09:50:22 -07001084 btif_update_remote_properties(p_auth_cmpl->bd_addr, p_auth_cmpl->bd_name,
1085 NULL, p_auth_cmpl->dev_type);
1086 pairing_cb.timeout_retries = 0;
1087 status = BT_STATUS_SUCCESS;
1088 state = BT_BOND_STATE_BONDED;
1089 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001090
Myles Watson6bd442f2016-10-19 09:50:22 -07001091 if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr)) {
1092 LOG_WARN(LOG_TAG, "%s:skip SDP", __func__);
1093 skip_sdp = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001094 }
Myles Watson6bd442f2016-10-19 09:50:22 -07001095 if (!pairing_cb.is_local_initiated && skip_sdp) {
1096 bond_state_changed(status, &bd_addr, state);
1097
1098 LOG_WARN(LOG_TAG, "%s: Incoming HID Connection", __func__);
1099 bt_property_t prop;
1100 bt_bdaddr_t bd_addr;
1101 bt_uuid_t uuid;
1102 char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE;
1103
1104 string_to_uuid(uuid_str, &uuid);
1105
1106 prop.type = BT_PROPERTY_UUIDS;
1107 prop.val = uuid.uu;
1108 prop.len = MAX_UUID_SIZE;
1109
1110 /* Send the event to the BTIF */
1111 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, BT_STATUS_SUCCESS,
1112 &bd_addr, 1, &prop);
1113 } else {
Myles Watson6bd442f2016-10-19 09:50:22 -07001114 bool is_crosskey = false;
1115 /* If bonded due to cross-key, save the static address too*/
1116 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
1117 (bdcmp(p_auth_cmpl->bd_addr, pairing_cb.bd_addr) != 0)) {
1118 BTIF_TRACE_DEBUG(
1119 "%s: bonding initiated due to cross key, adding static address",
1120 __func__);
1121 bdcpy(pairing_cb.static_bdaddr.address, p_auth_cmpl->bd_addr);
1122 is_crosskey = true;
1123 }
1124 if (!is_crosskey ||
1125 !(stack_config_get_interface()->get_pts_crosskey_sdp_disable())) {
Myles Watson6bd442f2016-10-19 09:50:22 -07001126 // Ensure inquiry is stopped before attempting service discovery
1127 btif_dm_cancel_discovery();
1128
1129 /* Trigger SDP on the device */
1130 pairing_cb.sdp_attempts = 1;
1131 btif_dm_get_remote_services(&bd_addr);
Myles Watson6bd442f2016-10-19 09:50:22 -07001132 }
Myles Watson6bd442f2016-10-19 09:50:22 -07001133 }
1134 // Do not call bond_state_changed_cb yet. Wait until remote service
1135 // discovery is complete
1136 } else {
1137 // Map the HCI fail reason to bt status
1138 switch (p_auth_cmpl->fail_reason) {
1139 case HCI_ERR_PAGE_TIMEOUT:
1140 if (interop_match_addr(INTEROP_AUTO_RETRY_PAIRING, &bd_addr) &&
1141 pairing_cb.timeout_retries) {
1142 BTIF_TRACE_WARNING("%s() - Pairing timeout; retrying (%d) ...",
1143 __func__, pairing_cb.timeout_retries);
1144 --pairing_cb.timeout_retries;
1145 btif_dm_cb_create_bond(&bd_addr, BTA_TRANSPORT_UNKNOWN);
1146 return;
1147 }
1148 /* Fall-through */
1149 case HCI_ERR_CONNECTION_TOUT:
1150 status = BT_STATUS_RMT_DEV_DOWN;
1151 break;
1152
1153 case HCI_ERR_PAIRING_NOT_ALLOWED:
1154 btif_storage_remove_bonded_device(&bd_addr);
1155 status = BT_STATUS_AUTH_REJECTED;
1156 break;
1157
1158 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
1159 status = BT_STATUS_AUTH_FAILURE;
1160 break;
1161
1162 /* map the auth failure codes, so we can retry pairing if necessary */
1163 case HCI_ERR_AUTH_FAILURE:
1164 case HCI_ERR_KEY_MISSING:
1165 btif_storage_remove_bonded_device(&bd_addr);
1166 case HCI_ERR_HOST_REJECT_SECURITY:
1167 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
1168 case HCI_ERR_UNIT_KEY_USED:
1169 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
1170 case HCI_ERR_INSUFFCIENT_SECURITY:
1171 case HCI_ERR_PEER_USER:
1172 case HCI_ERR_UNSPECIFIED:
1173 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d", __func__,
1174 p_auth_cmpl->fail_reason);
1175 if (pairing_cb.autopair_attempts == 1) {
1176 /* Create the Bond once again */
1177 BTIF_TRACE_WARNING("%s() auto pair failed. Reinitiate Bond",
1178 __func__);
1179 btif_dm_cb_create_bond(&bd_addr, BTA_TRANSPORT_UNKNOWN);
1180 return;
1181 } else {
1182 /* if autopair attempts are more than 1, or not attempted */
1183 status = BT_STATUS_AUTH_FAILURE;
1184 }
1185 break;
1186
1187 default:
1188 status = BT_STATUS_FAIL;
1189 }
1190 /* Special Handling for HID Devices */
1191 if (check_cod(&bd_addr, COD_HID_POINTING)) {
1192 /* Remove Device as bonded in nvram as authentication failed */
1193 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram",
1194 __func__);
1195 btif_storage_remove_bonded_device(&bd_addr);
1196 }
1197 bond_state_changed(status, &bd_addr, state);
1198 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001199}
1200
1201/******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07001202 *
1203 * Function btif_dm_search_devices_evt
1204 *
1205 * Description Executes search devices callback events in btif context
1206 *
1207 * Returns void
1208 *
1209 *****************************************************************************/
1210static void btif_dm_search_devices_evt(uint16_t event, char* p_param) {
1211 tBTA_DM_SEARCH* p_search_data;
1212 BTIF_TRACE_EVENT("%s event=%s", __func__, dump_dm_search_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001213
Myles Watson6bd442f2016-10-19 09:50:22 -07001214 switch (event) {
1215 case BTA_DM_DISC_RES_EVT: {
1216 p_search_data = (tBTA_DM_SEARCH*)p_param;
1217 /* Remote name update */
1218 if (strlen((const char*)p_search_data->disc_res.bd_name)) {
1219 bt_property_t properties[1];
1220 bt_bdaddr_t bdaddr;
1221 bt_status_t status;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001222
Myles Watson6bd442f2016-10-19 09:50:22 -07001223 properties[0].type = BT_PROPERTY_BDNAME;
1224 properties[0].val = p_search_data->disc_res.bd_name;
1225 properties[0].len = strlen((char*)p_search_data->disc_res.bd_name);
1226 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001227
Myles Watson6bd442f2016-10-19 09:50:22 -07001228 status =
1229 btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
1230 ASSERTC(status == BT_STATUS_SUCCESS,
1231 "failed to save remote device property", status);
1232 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, status, &bdaddr,
1233 1, properties);
1234 }
1235 /* TODO: Services? */
1236 } break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001237
Myles Watson6bd442f2016-10-19 09:50:22 -07001238 case BTA_DM_INQ_RES_EVT: {
1239 /* inquiry result */
1240 uint32_t cod;
1241 bt_bdname_t bdname;
1242 bt_bdaddr_t bdaddr;
1243 uint8_t remote_name_len;
1244 tBTA_SERVICE_MASK services = 0;
1245 bdstr_t bdstr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001246
Myles Watson6bd442f2016-10-19 09:50:22 -07001247 p_search_data = (tBTA_DM_SEARCH*)p_param;
1248 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001249
Myles Watson6bd442f2016-10-19 09:50:22 -07001250 BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __func__,
1251 bdaddr_to_string(&bdaddr, bdstr, sizeof(bdstr)),
Myles Watson6bd442f2016-10-19 09:50:22 -07001252 p_search_data->inq_res.device_type);
Myles Watson6bd442f2016-10-19 09:50:22 -07001253 bdname.name[0] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001254
Myles Watson6bd442f2016-10-19 09:50:22 -07001255 cod = devclass2uint(p_search_data->inq_res.dev_class);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001256
Myles Watson6bd442f2016-10-19 09:50:22 -07001257 if (cod == 0) {
1258 LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__);
1259 cod = COD_UNCLASSIFIED;
1260 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001261
Myles Watson6bd442f2016-10-19 09:50:22 -07001262 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1263 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001264
Myles Watson6bd442f2016-10-19 09:50:22 -07001265 /* Check EIR for remote name and services */
1266 if (p_search_data->inq_res.p_eir) {
1267 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
1268 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __func__,
1269 (uint32_t)services);
1270 /* TODO: Get the service list and check to see which uuids we got and
1271 * send it back to the client. */
1272 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001273
Myles Watson6bd442f2016-10-19 09:50:22 -07001274 {
1275 bt_property_t properties[5];
1276 bt_device_type_t dev_type;
1277 uint32_t num_properties = 0;
1278 bt_status_t status;
1279 int addr_type = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001280
Myles Watson6bd442f2016-10-19 09:50:22 -07001281 memset(properties, 0, sizeof(properties));
1282 /* BD_ADDR */
1283 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1284 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1285 num_properties++;
1286 /* BD_NAME */
1287 /* Don't send BDNAME if it is empty */
1288 if (bdname.name[0]) {
1289 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1290 BT_PROPERTY_BDNAME,
1291 strlen((char*)bdname.name), &bdname);
1292 num_properties++;
1293 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001294
Myles Watson6bd442f2016-10-19 09:50:22 -07001295 /* DEV_CLASS */
1296 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1297 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod),
1298 &cod);
1299 num_properties++;
Myles Watson90088882016-11-15 16:33:22 -08001300 /* DEV_TYPE */
Myles Watson6bd442f2016-10-19 09:50:22 -07001301 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
Nitin Aroraac728402015-06-26 18:09:37 -07001302
Myles Watson6bd442f2016-10-19 09:50:22 -07001303 /* Verify if the device is dual mode in NVRAM */
1304 int stored_device_type = 0;
1305 if (btif_get_device_type(bdaddr.address, &stored_device_type) &&
1306 ((stored_device_type != BT_DEVICE_TYPE_BREDR &&
1307 p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BREDR) ||
1308 (stored_device_type != BT_DEVICE_TYPE_BLE &&
1309 p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE))) {
1310 dev_type = (bt_device_type_t)BT_DEVICE_TYPE_DUMO;
1311 } else {
1312 dev_type = (bt_device_type_t)p_search_data->inq_res.device_type;
1313 }
Nitin Aroraac728402015-06-26 18:09:37 -07001314
Myles Watson6bd442f2016-10-19 09:50:22 -07001315 if (p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE)
1316 addr_type = p_search_data->inq_res.ble_addr_type;
Myles Watson6bd442f2016-10-19 09:50:22 -07001317 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1318 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type),
1319 &dev_type);
1320 num_properties++;
1321 /* RSSI */
1322 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1323 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1324 &(p_search_data->inq_res.rssi));
1325 num_properties++;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001326
Myles Watson6bd442f2016-10-19 09:50:22 -07001327 status =
1328 btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1329 ASSERTC(status == BT_STATUS_SUCCESS,
1330 "failed to save remote device (inquiry)", status);
Myles Watson6bd442f2016-10-19 09:50:22 -07001331 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
1332 ASSERTC(status == BT_STATUS_SUCCESS,
1333 "failed to save remote addr type (inquiry)", status);
Myles Watson6bd442f2016-10-19 09:50:22 -07001334 /* Callback to notify upper layer of device */
1335 HAL_CBACK(bt_hal_cbacks, device_found_cb, num_properties, properties);
1336 }
1337 } break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001338
Myles Watson6bd442f2016-10-19 09:50:22 -07001339 case BTA_DM_INQ_CMPL_EVT: {
Myles Watson6bd442f2016-10-19 09:50:22 -07001340 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1341 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1342 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param,
1343 NULL, bte_scan_filt_param_cfg_evt, 0);
Myles Watson6bd442f2016-10-19 09:50:22 -07001344 } break;
1345 case BTA_DM_DISC_CMPL_EVT: {
1346 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1347 BT_DISCOVERY_STOPPED);
1348 } break;
1349 case BTA_DM_SEARCH_CANCEL_CMPL_EVT: {
1350 /* if inquiry is not in progress and we get a cancel event, then
1351 * it means we are done with inquiry, but remote_name fetches are in
1352 * progress
1353 *
1354 * if inquiry is in progress, then we don't want to act on this
1355 * cancel_cmpl_evt
1356 * but instead wait for the cancel_cmpl_evt via the Busy Level
1357 *
1358 */
1359 if (btif_dm_inquiry_in_progress == false) {
Myles Watson6bd442f2016-10-19 09:50:22 -07001360 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1361 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1362 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0,
1363 &adv_filt_param, NULL,
1364 bte_scan_filt_param_cfg_evt, 0);
Myles Watson6bd442f2016-10-19 09:50:22 -07001365 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1366 BT_DISCOVERY_STOPPED);
1367 }
1368 } break;
1369 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001370}
1371
1372/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07001373 *
1374 * Function btif_dm_search_services_evt
1375 *
1376 * Description Executes search services event in btif context
1377 *
1378 * Returns void
1379 *
1380 ******************************************************************************/
1381static void btif_dm_search_services_evt(uint16_t event, char* p_param) {
1382 tBTA_DM_SEARCH* p_data = (tBTA_DM_SEARCH*)p_param;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001383
Myles Watson6bd442f2016-10-19 09:50:22 -07001384 BTIF_TRACE_EVENT("%s: event = %d", __func__, event);
1385 switch (event) {
1386 case BTA_DM_DISC_RES_EVT: {
1387 bt_property_t prop;
1388 uint32_t i = 0;
1389 bt_bdaddr_t bd_addr;
1390 bt_status_t ret;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001391
Myles Watson6bd442f2016-10-19 09:50:22 -07001392 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001393
Myles Watson6bd442f2016-10-19 09:50:22 -07001394 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __func__,
1395 p_data->disc_res.result, p_data->disc_res.services);
1396 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1397 (pairing_cb.state == BT_BOND_STATE_BONDING) &&
1398 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING)) {
1399 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting",
1400 __func__);
1401 pairing_cb.sdp_attempts++;
1402 btif_dm_get_remote_services(&bd_addr);
1403 return;
1404 }
1405 prop.type = BT_PROPERTY_UUIDS;
1406 prop.len = 0;
1407 if ((p_data->disc_res.result == BTA_SUCCESS) &&
1408 (p_data->disc_res.num_uuids > 0)) {
1409 prop.val = p_data->disc_res.p_uuid_list;
1410 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1411 for (i = 0; i < p_data->disc_res.num_uuids; i++) {
1412 char temp[256];
1413 uuid_to_string_legacy(
1414 (bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i * MAX_UUID_SIZE)),
1415 temp, sizeof(temp));
1416 LOG_INFO(LOG_TAG, "%s index:%d uuid:%s", __func__, i, temp);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001417 }
Myles Watson6bd442f2016-10-19 09:50:22 -07001418 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001419
Myles Watson6bd442f2016-10-19 09:50:22 -07001420 /* onUuidChanged requires getBondedDevices to be populated.
1421 ** bond_state_changed needs to be sent prior to remote_device_property
1422 */
1423 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1424 ((bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0) ||
1425 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr.address) ==
1426 0)) &&
1427 pairing_cb.sdp_attempts > 0) {
1428 BTIF_TRACE_DEBUG(
1429 "%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
1430 __func__);
1431 pairing_cb.sdp_attempts = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001432
Myles Watson6bd442f2016-10-19 09:50:22 -07001433 // If bonding occured due to cross-key pairing, send bonding callback
1434 // for static address now
1435 if (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr.address) ==
1436 0)
1437 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr,
1438 BT_BOND_STATE_BONDING);
1439
1440 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1441 }
1442
1443 if (p_data->disc_res.num_uuids != 0) {
1444 /* Also write this to the NVRAM */
1445 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1446 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed",
1447 ret);
1448 /* Send the event to the BTIF */
1449 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, BT_STATUS_SUCCESS,
1450 &bd_addr, 1, &prop);
1451 }
1452 } break;
1453
1454 case BTA_DM_DISC_CMPL_EVT:
1455 /* fixme */
1456 break;
1457
1458 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1459 /* no-op */
1460 break;
Jakub Pawlowski7df43242016-09-15 08:02:05 -07001461
Myles Watson6bd442f2016-10-19 09:50:22 -07001462 case BTA_DM_DISC_BLE_RES_EVT: {
1463 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __func__,
1464 p_data->disc_ble_res.service.uu.uuid16);
1465 bt_uuid_t uuid;
1466 int i = 0;
1467 int j = 15;
1468 int num_properties = 0;
1469 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID) {
1470 BTIF_TRACE_DEBUG("%s: Found HOGP UUID", __func__);
1471 bt_property_t prop[2];
1472 bt_bdaddr_t bd_addr;
1473 char temp[256];
1474 bt_status_t ret;
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001475
Myles Watson6bd442f2016-10-19 09:50:22 -07001476 bta_gatt_convert_uuid16_to_uuid128(
1477 uuid.uu, p_data->disc_ble_res.service.uu.uuid16);
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001478
Myles Watson6bd442f2016-10-19 09:50:22 -07001479 while (i < j) {
1480 unsigned char c = uuid.uu[j];
1481 uuid.uu[j] = uuid.uu[i];
1482 uuid.uu[i] = c;
1483 i++;
1484 j--;
Jakub Pawlowski713993d2016-04-21 13:16:45 -07001485 }
Myles Watson6bd442f2016-10-19 09:50:22 -07001486
1487 uuid_to_string_legacy(&uuid, temp, sizeof(temp));
1488 LOG_INFO(LOG_TAG, "%s uuid:%s", __func__, temp);
1489
1490 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1491 prop[0].type = BT_PROPERTY_UUIDS;
1492 prop[0].val = uuid.uu;
1493 prop[0].len = MAX_UUID_SIZE;
1494
1495 /* Also write this to the NVRAM */
1496 ret = btif_storage_set_remote_device_property(&bd_addr, &prop[0]);
1497 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed",
1498 ret);
1499 num_properties++;
1500
1501 /* Remote name update */
1502 if (strnlen((const char*)p_data->disc_ble_res.bd_name, BD_NAME_LEN)) {
1503 prop[1].type = BT_PROPERTY_BDNAME;
1504 prop[1].val = p_data->disc_ble_res.bd_name;
1505 prop[1].len =
1506 strnlen((char*)p_data->disc_ble_res.bd_name, BD_NAME_LEN);
1507
1508 ret = btif_storage_set_remote_device_property(&bd_addr, &prop[1]);
1509 ASSERTC(ret == BT_STATUS_SUCCESS,
1510 "failed to save remote device property", ret);
1511 num_properties++;
1512 }
1513
1514 /* Send the event to the BTIF */
1515 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, BT_STATUS_SUCCESS,
1516 &bd_addr, num_properties, prop);
1517 }
1518 } break;
Andre Eisenbach2e7fa682013-08-08 15:42:48 -07001519
Myles Watson6bd442f2016-10-19 09:50:22 -07001520 default: { ASSERTC(0, "unhandled search services event", event); } break;
1521 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001522}
1523
1524/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07001525 *
1526 * Function btif_dm_remote_service_record_evt
1527 *
1528 * Description Executes search service record event in btif context
1529 *
1530 * Returns void
1531 *
1532 ******************************************************************************/
1533static void btif_dm_remote_service_record_evt(uint16_t event, char* p_param) {
1534 tBTA_DM_SEARCH* p_data = (tBTA_DM_SEARCH*)p_param;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001535
Myles Watson6bd442f2016-10-19 09:50:22 -07001536 BTIF_TRACE_EVENT("%s: event = %d", __func__, event);
1537 switch (event) {
1538 case BTA_DM_DISC_RES_EVT: {
1539 bt_service_record_t rec;
1540 bt_property_t prop;
1541 bt_bdaddr_t bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001542
Myles Watson6bd442f2016-10-19 09:50:22 -07001543 memset(&rec, 0, sizeof(bt_service_record_t));
1544 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001545
Myles Watson6bd442f2016-10-19 09:50:22 -07001546 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __func__,
1547 p_data->disc_res.result, p_data->disc_res.services);
1548 prop.type = BT_PROPERTY_SERVICE_RECORD;
1549 prop.val = (void*)&rec;
1550 prop.len = sizeof(rec);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001551
Myles Watson6bd442f2016-10-19 09:50:22 -07001552 /* disc_res.result is overloaded with SCN. Cannot check result */
1553 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1554 /* TODO: Get the UUID as well */
1555 rec.channel = p_data->disc_res.result - 3;
1556 /* TODO: Need to get the service name using p_raw_data */
1557 rec.name[0] = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001558
Myles Watson6bd442f2016-10-19 09:50:22 -07001559 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, BT_STATUS_SUCCESS,
1560 &bd_addr, 1, &prop);
1561 } break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001562
Myles Watson6bd442f2016-10-19 09:50:22 -07001563 default: {
1564 ASSERTC(0, "unhandled remote service record event", event);
1565 } break;
1566 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001567}
1568
1569/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07001570 *
1571 * Function btif_dm_upstreams_cback
1572 *
1573 * Description Executes UPSTREAMS events in btif context
1574 *
1575 * Returns void
1576 *
1577 ******************************************************************************/
1578static void btif_dm_upstreams_evt(uint16_t event, char* p_param) {
1579 tBTA_DM_SEC* p_data = (tBTA_DM_SEC*)p_param;
1580 tBTA_SERVICE_MASK service_mask;
1581 uint32_t i;
1582 bt_bdaddr_t bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001583
Myles Watson6bd442f2016-10-19 09:50:22 -07001584 BTIF_TRACE_EVENT("%s: ev: %s", __func__, dump_dm_event(event));
The Android Open Source Project5738f832012-12-12 16:00:35 -08001585
Myles Watson6bd442f2016-10-19 09:50:22 -07001586 switch (event) {
1587 case BTA_DM_ENABLE_EVT: {
1588 BD_NAME bdname;
1589 bt_status_t status;
1590 bt_property_t prop;
1591 prop.type = BT_PROPERTY_BDNAME;
1592 prop.len = BD_NAME_LEN;
1593 prop.val = (void*)bdname;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001594
Myles Watson6bd442f2016-10-19 09:50:22 -07001595 status = btif_storage_get_adapter_property(&prop);
1596 if (status == BT_STATUS_SUCCESS) {
1597 /* A name exists in the storage. Make this the device name */
1598 BTA_DmSetDeviceName((char*)prop.val);
1599 } else {
1600 /* Storage does not have a name yet.
1601 * Use the default name and write it to the chip
1602 */
1603 BTA_DmSetDeviceName(btif_get_default_local_name());
1604 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001605
Myles Watson6bd442f2016-10-19 09:50:22 -07001606 /* Enable local privacy */
1607 BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED);
Andre Eisenbacha015a832014-09-11 14:09:40 -07001608
Myles Watson6bd442f2016-10-19 09:50:22 -07001609 /* for each of the enabled services in the mask, trigger the profile
1610 * enable */
1611 service_mask = btif_get_enabled_services_mask();
1612 for (i = 0; i <= BTA_MAX_SERVICE_ID; i++) {
1613 if (service_mask &
1614 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i))) {
1615 btif_in_execute_service_request(i, true);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001616 }
Myles Watson6bd442f2016-10-19 09:50:22 -07001617 }
1618 /* clear control blocks */
1619 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1620 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001621
Myles Watson6bd442f2016-10-19 09:50:22 -07001622 /* This function will also trigger the adapter_properties_cb
1623 ** and bonded_devices_info_cb
1624 */
1625 btif_storage_load_bonded_devices();
The Android Open Source Project5738f832012-12-12 16:00:35 -08001626
Myles Watson6bd442f2016-10-19 09:50:22 -07001627 btif_enable_bluetooth_evt(p_data->enable.status);
1628 } break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001629
Myles Watson6bd442f2016-10-19 09:50:22 -07001630 case BTA_DM_DISABLE_EVT:
1631 /* for each of the enabled services in the mask, trigger the profile
1632 * disable */
1633 service_mask = btif_get_enabled_services_mask();
1634 for (i = 0; i <= BTA_MAX_SERVICE_ID; i++) {
1635 if (service_mask &
1636 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i))) {
1637 btif_in_execute_service_request(i, false);
1638 }
1639 }
1640 btif_disable_bluetooth_evt();
1641 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001642
Myles Watson6bd442f2016-10-19 09:50:22 -07001643 case BTA_DM_PIN_REQ_EVT:
1644 btif_dm_pin_req_evt(&p_data->pin_req);
1645 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001646
Myles Watson6bd442f2016-10-19 09:50:22 -07001647 case BTA_DM_AUTH_CMPL_EVT:
1648 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1649 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001650
Myles Watson6bd442f2016-10-19 09:50:22 -07001651 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1652 if (pairing_cb.state == BT_BOND_STATE_BONDING) {
1653 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1654 btm_set_bond_type_dev(pairing_cb.bd_addr, BOND_TYPE_UNKNOWN);
1655 bond_state_changed((bt_status_t)p_data->bond_cancel_cmpl.result,
1656 &bd_addr, BT_BOND_STATE_NONE);
1657 }
1658 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001659
Myles Watson6bd442f2016-10-19 09:50:22 -07001660 case BTA_DM_SP_CFM_REQ_EVT:
1661 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1662 break;
1663 case BTA_DM_SP_KEY_NOTIF_EVT:
1664 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1665 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001666
Myles Watson6bd442f2016-10-19 09:50:22 -07001667 case BTA_DM_DEV_UNPAIRED_EVT:
1668 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1669 btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001670
Myles Watson6bd442f2016-10-19 09:50:22 -07001671/*special handling for HID devices */
1672#if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == true))
1673 btif_hh_remove_device(bd_addr);
1674#endif
1675 btif_storage_remove_bonded_device(&bd_addr);
1676 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1677 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001678
Myles Watson6bd442f2016-10-19 09:50:22 -07001679 case BTA_DM_BUSY_LEVEL_EVT: {
1680 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK) {
1681 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED) {
1682 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1683 BT_DISCOVERY_STARTED);
1684 btif_dm_inquiry_in_progress = true;
1685 } else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED) {
1686 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1687 BT_DISCOVERY_STOPPED);
1688 btif_dm_inquiry_in_progress = false;
1689 } else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE) {
1690 btif_dm_inquiry_in_progress = false;
1691 }
1692 }
1693 } break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001694
Myles Watson6bd442f2016-10-19 09:50:22 -07001695 case BTA_DM_LINK_UP_EVT:
1696 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
1697 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001698
Myles Watson6bd442f2016-10-19 09:50:22 -07001699 btif_update_remote_version_property(&bd_addr);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001700
Myles Watson6bd442f2016-10-19 09:50:22 -07001701 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1702 &bd_addr, BT_ACL_STATE_CONNECTED);
1703 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001704
Myles Watson6bd442f2016-10-19 09:50:22 -07001705 case BTA_DM_LINK_DOWN_EVT:
1706 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1707 btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);
Ayan Ghosh583b8172013-12-06 17:11:11 +05301708 btif_av_move_idle(bd_addr);
Myles Watson6bd442f2016-10-19 09:50:22 -07001709 BTIF_TRACE_DEBUG(
1710 "BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
1711 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1712 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1713 break;
1714
1715 case BTA_DM_HW_ERROR_EVT:
1716 BTIF_TRACE_ERROR("Received H/W Error. ");
1717 /* Flush storage data */
1718 btif_config_flush();
1719 usleep(100000); /* 100milliseconds */
1720 /* Killing the process to force a restart as part of fault tolerance */
1721 kill(getpid(), SIGKILL);
1722 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001723
Myles Watson6bd442f2016-10-19 09:50:22 -07001724 case BTA_DM_BLE_KEY_EVT:
1725 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT key_type=0x%02x ",
1726 p_data->ble_key.key_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001727
Myles Watson6bd442f2016-10-19 09:50:22 -07001728 /* If this pairing is by-product of local initiated GATT client Read or
1729 Write,
1730 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would
1731 not
1732 have setup properly. Setup pairing_cb and notify App about Bonding state
1733 now*/
1734 if (pairing_cb.state != BT_BOND_STATE_BONDING) {
1735 BTIF_TRACE_DEBUG(
1736 "Bond state not sent to App so far.Notify the app now");
1737 bond_state_changed(BT_STATUS_SUCCESS,
1738 (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1739 BT_BOND_STATE_BONDING);
1740 } else if (memcmp(pairing_cb.bd_addr, p_data->ble_key.bd_addr,
1741 BD_ADDR_LEN) != 0) {
1742 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",
1743 p_data->ble_key.key_type);
1744 break;
1745 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001746
Myles Watson6bd442f2016-10-19 09:50:22 -07001747 switch (p_data->ble_key.key_type) {
1748 case BTA_LE_KEY_PENC:
1749 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
1750 pairing_cb.ble.is_penc_key_rcvd = true;
1751 pairing_cb.ble.penc_key = p_data->ble_key.p_key_value->penc_key;
1752 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001753
Myles Watson6bd442f2016-10-19 09:50:22 -07001754 case BTA_LE_KEY_PID:
1755 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
1756 pairing_cb.ble.is_pid_key_rcvd = true;
1757 pairing_cb.ble.pid_key = p_data->ble_key.p_key_value->pid_key;
1758 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001759
Myles Watson6bd442f2016-10-19 09:50:22 -07001760 case BTA_LE_KEY_PCSRK:
1761 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
1762 pairing_cb.ble.is_pcsrk_key_rcvd = true;
1763 pairing_cb.ble.pcsrk_key = p_data->ble_key.p_key_value->pcsrk_key;
1764 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001765
Myles Watson6bd442f2016-10-19 09:50:22 -07001766 case BTA_LE_KEY_LENC:
1767 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
1768 pairing_cb.ble.is_lenc_key_rcvd = true;
1769 pairing_cb.ble.lenc_key = p_data->ble_key.p_key_value->lenc_key;
1770 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001771
Myles Watson6bd442f2016-10-19 09:50:22 -07001772 case BTA_LE_KEY_LCSRK:
1773 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
1774 pairing_cb.ble.is_lcsrk_key_rcvd = true;
1775 pairing_cb.ble.lcsrk_key = p_data->ble_key.p_key_value->lcsrk_key;
1776 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001777
Myles Watson6bd442f2016-10-19 09:50:22 -07001778 case BTA_LE_KEY_LID:
1779 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LID");
1780 pairing_cb.ble.is_lidk_key_rcvd = true;
1781 break;
Satya Calloji444a8da2015-03-06 10:38:22 -08001782
Myles Watson6bd442f2016-10-19 09:50:22 -07001783 default:
1784 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)",
1785 p_data->ble_key.key_type);
1786 break;
1787 }
1788 break;
1789 case BTA_DM_BLE_SEC_REQ_EVT:
1790 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
1791 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1792 break;
1793 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
1794 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
1795 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1796 break;
1797 case BTA_DM_BLE_PASSKEY_REQ_EVT:
1798 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
1799 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1800 break;
1801 case BTA_DM_BLE_NC_REQ_EVT:
1802 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
1803 btif_dm_ble_key_nc_req_evt(&p_data->key_notif);
1804 break;
1805 case BTA_DM_BLE_OOB_REQ_EVT:
1806 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
1807 btif_dm_ble_oob_req_evt(&p_data->rmt_oob);
1808 break;
1809 case BTA_DM_BLE_SC_OOB_REQ_EVT:
1810 BTIF_TRACE_DEBUG("BTA_DM_BLE_SC_OOB_REQ_EVT. ");
1811 btif_dm_ble_sc_oob_req_evt(&p_data->rmt_oob);
1812 break;
1813 case BTA_DM_BLE_LOCAL_IR_EVT:
1814 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
1815 ble_local_key_cb.is_id_keys_rcvd = true;
1816 memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0],
1817 sizeof(BT_OCTET16));
1818 memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0],
1819 sizeof(BT_OCTET16));
1820 memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0],
1821 sizeof(BT_OCTET16));
1822 btif_storage_add_ble_local_key((char*)&ble_local_key_cb.id_keys.irk[0],
1823 BTIF_DM_LE_LOCAL_KEY_IRK, BT_OCTET16_LEN);
1824 btif_storage_add_ble_local_key((char*)&ble_local_key_cb.id_keys.ir[0],
1825 BTIF_DM_LE_LOCAL_KEY_IR, BT_OCTET16_LEN);
1826 btif_storage_add_ble_local_key((char*)&ble_local_key_cb.id_keys.dhk[0],
1827 BTIF_DM_LE_LOCAL_KEY_DHK, BT_OCTET16_LEN);
1828 break;
1829 case BTA_DM_BLE_LOCAL_ER_EVT:
1830 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
1831 ble_local_key_cb.is_er_rcvd = true;
1832 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1833 btif_storage_add_ble_local_key((char*)&ble_local_key_cb.er[0],
1834 BTIF_DM_LE_LOCAL_KEY_ER, BT_OCTET16_LEN);
1835 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001836
Myles Watson6bd442f2016-10-19 09:50:22 -07001837 case BTA_DM_BLE_AUTH_CMPL_EVT:
1838 BTIF_TRACE_DEBUG("BTA_DM_BLE_AUTH_CMPL_EVT. ");
1839 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1840 break;
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001841
Myles Watson6bd442f2016-10-19 09:50:22 -07001842 case BTA_DM_LE_FEATURES_READ: {
1843 tBTM_BLE_VSC_CB cmn_vsc_cb;
1844 bt_local_le_features_t local_le_features;
1845 char buf[512];
1846 bt_property_t prop;
1847 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1848 prop.val = (void*)buf;
1849 prop.len = sizeof(buf);
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001850
Myles Watson6bd442f2016-10-19 09:50:22 -07001851 /* LE features are not stored in storage. Should be retrived from stack */
1852 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1853 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
Ganesh Ganapathi Batta9546abf2014-05-30 16:28:00 -07001854
Myles Watson6bd442f2016-10-19 09:50:22 -07001855 prop.len = sizeof(bt_local_le_features_t);
1856 if (cmn_vsc_cb.filter_support == 1)
1857 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1858 else
1859 local_le_features.max_adv_filter_supported = 0;
1860 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1861 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1862 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
1863 local_le_features.activity_energy_info_supported =
1864 cmn_vsc_cb.energy_support;
1865 local_le_features.scan_result_storage_size =
1866 cmn_vsc_cb.tot_scan_results_strg;
1867 local_le_features.version_supported = cmn_vsc_cb.version_supported;
1868 local_le_features.total_trackable_advertisers =
1869 cmn_vsc_cb.total_trackable_advertisers;
Andre Eisenbach979e3422015-06-10 21:04:38 -07001870
Myles Watson6bd442f2016-10-19 09:50:22 -07001871 local_le_features.extended_scan_support =
1872 cmn_vsc_cb.extended_scan_support > 0;
1873 local_le_features.debug_logging_supported =
1874 cmn_vsc_cb.debug_logging_supported > 0;
Andre Eisenbach979e3422015-06-10 21:04:38 -07001875
Myles Watson6bd442f2016-10-19 09:50:22 -07001876 memcpy(prop.val, &local_le_features, prop.len);
1877 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1,
1878 &prop);
1879 break;
1880 }
Satya Callojie5ba8842014-07-03 17:18:02 -07001881
Myles Watson6bd442f2016-10-19 09:50:22 -07001882 case BTA_DM_ENER_INFO_READ: {
1883 btif_activity_energy_info_cb_t* p_ener_data =
1884 (btif_activity_energy_info_cb_t*)p_param;
1885 bt_activity_energy_info energy_info;
1886 energy_info.status = p_ener_data->status;
1887 energy_info.ctrl_state = p_ener_data->ctrl_state;
1888 energy_info.rx_time = p_ener_data->rx_time;
1889 energy_info.tx_time = p_ener_data->tx_time;
1890 energy_info.idle_time = p_ener_data->idle_time;
1891 energy_info.energy_used = p_ener_data->energy_used;
Adam Lesinski0620f972015-12-02 22:15:08 -08001892
Myles Watson6bd442f2016-10-19 09:50:22 -07001893 bt_uid_traffic_t* data = uid_set_read_and_clear(uid_set);
1894 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info, data);
1895 osi_free(data);
1896 break;
1897 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001898
Myles Watson6bd442f2016-10-19 09:50:22 -07001899 case BTA_DM_AUTHORIZE_EVT:
1900 case BTA_DM_SIG_STRENGTH_EVT:
1901 case BTA_DM_SP_RMT_OOB_EVT:
1902 case BTA_DM_SP_KEYPRESS_EVT:
1903 case BTA_DM_ROLE_CHG_EVT:
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08001904
Myles Watson6bd442f2016-10-19 09:50:22 -07001905 default:
1906 BTIF_TRACE_WARNING("btif_dm_cback : unhandled event (%d)", event);
1907 break;
1908 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001909
Myles Watson6bd442f2016-10-19 09:50:22 -07001910 btif_dm_data_free(event, p_data);
Andre Eisenbach8a057242015-04-29 22:27:15 -07001911}
The Android Open Source Project5738f832012-12-12 16:00:35 -08001912
1913/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07001914 *
1915 * Function btif_dm_generic_evt
1916 *
1917 * Description Executes non-BTA upstream events in BTIF context
1918 *
1919 * Returns void
1920 *
1921 ******************************************************************************/
1922static void btif_dm_generic_evt(uint16_t event, char* p_param) {
1923 BTIF_TRACE_EVENT("%s: event=%d", __func__, event);
1924 switch (event) {
1925 case BTIF_DM_CB_DISCOVERY_STARTED: {
1926 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1927 BT_DISCOVERY_STARTED);
1928 } break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001929
Myles Watson6bd442f2016-10-19 09:50:22 -07001930 case BTIF_DM_CB_CREATE_BOND: {
1931 pairing_cb.timeout_retries = NUM_TIMEOUT_RETRIES;
1932 btif_dm_create_bond_cb_t* create_bond_cb =
1933 (btif_dm_create_bond_cb_t*)p_param;
1934 btif_dm_cb_create_bond(&create_bond_cb->bdaddr,
1935 create_bond_cb->transport);
1936 } break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001937
Myles Watson6bd442f2016-10-19 09:50:22 -07001938 case BTIF_DM_CB_REMOVE_BOND: {
1939 btif_dm_cb_remove_bond((bt_bdaddr_t*)p_param);
1940 } break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001941
Myles Watson6bd442f2016-10-19 09:50:22 -07001942 case BTIF_DM_CB_HID_REMOTE_NAME: {
1943 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME*)p_param);
1944 } break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08001945
Myles Watson6bd442f2016-10-19 09:50:22 -07001946 case BTIF_DM_CB_BOND_STATE_BONDING: {
1947 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_param,
1948 BT_BOND_STATE_BONDING);
1949 } break;
1950 case BTIF_DM_CB_LE_TX_TEST:
1951 case BTIF_DM_CB_LE_RX_TEST: {
1952 uint8_t status;
1953 STREAM_TO_UINT8(status, p_param);
1954 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1955 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
1956 } break;
1957 case BTIF_DM_CB_LE_TEST_END: {
1958 uint8_t status;
1959 uint16_t count = 0;
1960 STREAM_TO_UINT8(status, p_param);
1961 if (status == 0) STREAM_TO_UINT16(count, p_param);
1962 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1963 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
1964 } break;
1965 default: {
1966 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __func__, event);
1967 } break;
1968 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08001969}
1970
1971/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07001972 *
1973 * Function bte_dm_evt
1974 *
1975 * Description Switches context from BTE to BTIF for all DM events
1976 *
1977 * Returns void
1978 *
1979 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08001980
Myles Watson6bd442f2016-10-19 09:50:22 -07001981void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC* p_data) {
1982 /* switch context to btif task context (copy full union size for convenience)
1983 */
1984 bt_status_t status = btif_transfer_context(
1985 btif_dm_upstreams_evt, (uint16_t)event, (char*)p_data,
1986 sizeof(tBTA_DM_SEC), btif_dm_data_copy);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001987
Myles Watson6bd442f2016-10-19 09:50:22 -07001988 /* catch any failed context transfers */
1989 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
The Android Open Source Project5738f832012-12-12 16:00:35 -08001990}
1991
1992/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07001993 *
1994 * Function bte_search_devices_evt
1995 *
1996 * Description Switches context from BTE to BTIF for DM search events
1997 *
1998 * Returns void
1999 *
2000 ******************************************************************************/
2001static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event,
2002 tBTA_DM_SEARCH* p_data) {
2003 uint16_t param_len = 0;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002004
Myles Watson6bd442f2016-10-19 09:50:22 -07002005 if (p_data) param_len += sizeof(tBTA_DM_SEARCH);
2006 /* Allocate buffer to hold the pointers (deep copy). The pointers will point
2007 * to the end of the tBTA_DM_SEARCH */
2008 switch (event) {
2009 case BTA_DM_INQ_RES_EVT: {
2010 if (p_data->inq_res.p_eir) param_len += HCI_EXT_INQ_RESPONSE_LEN;
2011 } break;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002012
Myles Watson6bd442f2016-10-19 09:50:22 -07002013 case BTA_DM_DISC_RES_EVT: {
2014 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
2015 param_len += p_data->disc_res.raw_data_size;
2016 } break;
2017 }
2018 BTIF_TRACE_DEBUG("%s event=%s param_len=%d", __func__,
2019 dump_dm_search_event(event), param_len);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002020
Myles Watson6bd442f2016-10-19 09:50:22 -07002021 /* if remote name is available in EIR, set teh flag so that stack doesnt
2022 * trigger RNR */
2023 if (event == BTA_DM_INQ_RES_EVT)
2024 p_data->inq_res.remt_name_not_required =
2025 check_eir_remote_name(p_data, NULL, NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002026
Myles Watson6bd442f2016-10-19 09:50:22 -07002027 btif_transfer_context(
2028 btif_dm_search_devices_evt, (uint16_t)event, (char*)p_data, param_len,
2029 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002030}
2031
2032/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07002033 *
2034 * Function bte_dm_search_services_evt
2035 *
2036 * Description Switches context from BTE to BTIF for DM search services
2037 * event
2038 *
2039 * Returns void
2040 *
2041 ******************************************************************************/
2042static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event,
2043 tBTA_DM_SEARCH* p_data) {
2044 uint16_t param_len = 0;
2045 if (p_data) param_len += sizeof(tBTA_DM_SEARCH);
2046 switch (event) {
2047 case BTA_DM_DISC_RES_EVT: {
2048 if ((p_data->disc_res.result == BTA_SUCCESS) &&
2049 (p_data->disc_res.num_uuids > 0)) {
2050 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
2051 }
2052 } break;
2053 }
2054 /* TODO: The only other member that needs a deep copy is the p_raw_data. But
2055 * not sure
2056 * if raw_data is needed. */
2057 btif_transfer_context(
2058 btif_dm_search_services_evt, event, (char*)p_data, param_len,
2059 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002060}
2061
2062/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07002063 *
2064 * Function bte_dm_remote_service_record_evt
2065 *
2066 * Description Switches context from BTE to BTIF for DM search service
2067 * record event
2068 *
2069 * Returns void
2070 *
2071 ******************************************************************************/
2072static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event,
2073 tBTA_DM_SEARCH* p_data) {
2074 /* TODO: The only member that needs a deep copy is the p_raw_data. But not
2075 * sure yet if this is needed. */
2076 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data,
2077 sizeof(tBTA_DM_SEARCH), NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002078}
2079
Satya Callojie5ba8842014-07-03 17:18:02 -07002080/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07002081 *
2082 * Function bta_energy_info_cb
2083 *
2084 * Description Switches context from BTE to BTIF for DM energy info event
2085 *
2086 * Returns void
2087 *
2088 ******************************************************************************/
2089static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time,
2090 tBTA_DM_BLE_RX_TIME_MS rx_time,
2091 tBTA_DM_BLE_IDLE_TIME_MS idle_time,
2092 tBTA_DM_BLE_ENERGY_USED energy_used,
2093 tBTA_DM_CONTRL_STATE ctrl_state,
2094 tBTA_STATUS status) {
2095 BTIF_TRACE_DEBUG(
2096 "energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, "
2097 "idle_time=%ld,used=%ld",
2098 status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
Satya Callojie5ba8842014-07-03 17:18:02 -07002099
Myles Watson6bd442f2016-10-19 09:50:22 -07002100 btif_activity_energy_info_cb_t btif_cb;
2101 btif_cb.status = status;
2102 btif_cb.ctrl_state = ctrl_state;
2103 btif_cb.tx_time = (uint64_t)tx_time;
2104 btif_cb.rx_time = (uint64_t)rx_time;
2105 btif_cb.idle_time = (uint64_t)idle_time;
2106 btif_cb.energy_used = (uint64_t)energy_used;
2107 btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
2108 (char*)&btif_cb, sizeof(btif_activity_energy_info_cb_t),
2109 NULL);
Satya Callojie5ba8842014-07-03 17:18:02 -07002110}
2111
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002112/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07002113 *
2114 * Function bte_scan_filt_param_cfg_evt
2115 *
2116 * Description Scan filter param config event
2117 *
2118 * Returns void
2119 *
2120 ******************************************************************************/
Marie Janssenb7f64bc2016-06-22 12:52:19 -07002121static void bte_scan_filt_param_cfg_evt(uint8_t action_type,
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002122 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
Myles Watson6bd442f2016-10-19 09:50:22 -07002123 tBTA_DM_BLE_REF_VALUE ref_value,
2124 tBTA_STATUS status) {
2125 /* This event occurs on calling BTA_DmBleCfgFilterCondition internally,
2126 ** and that is why there is no HAL callback
2127 */
2128 if (BTA_SUCCESS != status) {
2129 BTIF_TRACE_ERROR("%s, %d", __func__, status);
2130 } else {
2131 BTIF_TRACE_DEBUG("%s", __func__);
2132 }
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002133}
2134
The Android Open Source Project5738f832012-12-12 16:00:35 -08002135/*****************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07002136 *
2137 * btif api functions (no context switch)
2138 *
2139 ****************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002140
2141/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07002142 *
2143 * Function btif_dm_start_discovery
2144 *
2145 * Description Start device discovery/inquiry
2146 *
2147 * Returns bt_status_t
2148 *
2149 ******************************************************************************/
2150bt_status_t btif_dm_start_discovery(void) {
2151 tBTA_DM_INQ inq_params;
2152 tBTA_SERVICE_MASK services = 0;
2153 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002154
Myles Watson6bd442f2016-10-19 09:50:22 -07002155 BTIF_TRACE_EVENT("%s", __func__);
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002156
Myles Watson6bd442f2016-10-19 09:50:22 -07002157 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
2158 /* Cleanup anything remaining on index 0 */
2159 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param,
2160 NULL, bte_scan_filt_param_cfg_evt, 0);
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002161
Myles Watson6bd442f2016-10-19 09:50:22 -07002162 /* Add an allow-all filter on index 0*/
2163 adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE;
2164 adv_filt_param.feat_seln = ALLOW_ALL_FILTER;
2165 adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;
2166 adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;
2167 adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE;
2168 adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE;
2169 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL,
2170 bte_scan_filt_param_cfg_evt, 0);
Satya Calloji6e2d9db2014-07-08 16:18:58 -07002171
Myles Watson6bd442f2016-10-19 09:50:22 -07002172 /* TODO: Do we need to handle multiple inquiries at the same time? */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002173
Myles Watson6bd442f2016-10-19 09:50:22 -07002174 /* Set inquiry params and call API */
2175 inq_params.mode = BTA_DM_GENERAL_INQUIRY | BTA_BLE_GENERAL_INQUIRY;
Marie Janssenb7f64bc2016-06-22 12:52:19 -07002176#if (BTA_HOST_INTERLEAVE_SEARCH == TRUE)
Myles Watson6bd442f2016-10-19 09:50:22 -07002177 inq_params.intl_duration[0] = BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
2178 inq_params.intl_duration[1] = BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
2179 inq_params.intl_duration[2] = BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
2180 inq_params.intl_duration[3] = BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
Matthew Xie7f3e4292013-09-30 12:44:10 -07002181#endif
Myles Watson6bd442f2016-10-19 09:50:22 -07002182 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002183
Myles Watson6bd442f2016-10-19 09:50:22 -07002184 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
2185 inq_params.report_dup = true;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002186
Myles Watson6bd442f2016-10-19 09:50:22 -07002187 inq_params.filter_type = BTA_DM_INQ_CLR;
2188 /* TODO: Filter device by BDA needs to be implemented here */
The Android Open Source Project5738f832012-12-12 16:00:35 -08002189
Myles Watson6bd442f2016-10-19 09:50:22 -07002190 /* Will be enabled to true once inquiry busy level has been received */
2191 btif_dm_inquiry_in_progress = false;
2192 /* find nearby devices */
2193 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002194
Myles Watson6bd442f2016-10-19 09:50:22 -07002195 return BT_STATUS_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002196}
2197
2198/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07002199 *
2200 * Function btif_dm_cancel_discovery
2201 *
2202 * Description Cancels search
2203 *
2204 * Returns bt_status_t
2205 *
2206 ******************************************************************************/
2207bt_status_t btif_dm_cancel_discovery(void) {
2208 BTIF_TRACE_EVENT("%s", __func__);
2209 BTA_DmSearchCancel();
2210 return BT_STATUS_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002211}
2212
2213/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07002214 *
2215 * Function btif_dm_create_bond
2216 *
2217 * Description Initiate bonding with the specified device
2218 *
2219 * Returns bt_status_t
2220 *
2221 ******************************************************************************/
2222bt_status_t btif_dm_create_bond(const bt_bdaddr_t* bd_addr, int transport) {
2223 btif_dm_create_bond_cb_t create_bond_cb;
2224 create_bond_cb.transport = transport;
2225 bdcpy(create_bond_cb.bdaddr.address, bd_addr->address);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002226
Myles Watson6bd442f2016-10-19 09:50:22 -07002227 bdstr_t bdstr;
2228 BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __func__,
2229 bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)), transport);
2230 if (pairing_cb.state != BT_BOND_STATE_NONE) return BT_STATUS_BUSY;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002231
Myles Watson6bd442f2016-10-19 09:50:22 -07002232 btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_CREATE_BOND,
2233 pairing_cb.state);
Ajay Panickerc138fe42016-02-05 16:50:20 -08002234
Myles Watson6bd442f2016-10-19 09:50:22 -07002235 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
2236 (char*)&create_bond_cb,
2237 sizeof(btif_dm_create_bond_cb_t), NULL);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002238
Myles Watson6bd442f2016-10-19 09:50:22 -07002239 return BT_STATUS_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002240}
2241
2242/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07002243 *
2244 * Function btif_dm_create_bond_out_of_band
2245 *
2246 * Description Initiate bonding with the specified device using out of band
Myles Watsonf4548162016-10-19 09:53:56 -07002247 * data
Myles Watson6bd442f2016-10-19 09:50:22 -07002248 *
2249 * Returns bt_status_t
2250 *
2251 ******************************************************************************/
2252bt_status_t btif_dm_create_bond_out_of_band(
2253 const bt_bdaddr_t* bd_addr, int transport,
2254 const bt_out_of_band_data_t* oob_data) {
2255 bdcpy(oob_cb.bdaddr, bd_addr->address);
2256 memcpy(&oob_cb.oob_data, oob_data, sizeof(bt_out_of_band_data_t));
Jakub Pawlowski1a5bb5f2015-12-01 12:14:22 -08002257
Jakub Pawlowski7921e8f2016-11-09 16:39:10 -08002258 uint8_t empty[] = {0, 0, 0, 0, 0, 0, 0};
2259 // If LE Bluetooth Device Address is provided, use provided address type
2260 // value.
2261 if (memcmp(oob_data->le_bt_dev_addr, empty, 7) != 0) {
2262 /* byte no 7 is address type in LE Bluetooth Address OOB data */
2263 uint8_t address_type = oob_data->le_bt_dev_addr[6];
2264 if (address_type == BLE_ADDR_PUBLIC || address_type == BLE_ADDR_RANDOM) {
2265 // bd_addr->address is already reversed, so use it instead of
2266 // oob_data->le_bt_dev_addr
Myles Watson90088882016-11-15 16:33:22 -08002267 BTM_SecAddBleDevice(bd_addr->address, NULL, BT_DEVICE_TYPE_BLE,
2268 address_type);
Jakub Pawlowski7921e8f2016-11-09 16:39:10 -08002269 }
2270 }
2271
Myles Watson6bd442f2016-10-19 09:50:22 -07002272 bdstr_t bdstr;
2273 BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __func__,
2274 bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)), transport);
2275 return btif_dm_create_bond(bd_addr, transport);
Jakub Pawlowski1a5bb5f2015-12-01 12:14:22 -08002276}
2277
2278/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07002279 *
2280 * Function btif_dm_cancel_bond
2281 *
2282 * Description Initiate bonding with the specified device
2283 *
2284 * Returns bt_status_t
2285 *
2286 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002287
Myles Watson6bd442f2016-10-19 09:50:22 -07002288bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t* bd_addr) {
2289 bdstr_t bdstr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002290
Myles Watson6bd442f2016-10-19 09:50:22 -07002291 BTIF_TRACE_EVENT("%s: bd_addr=%s", __func__,
2292 bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002293
Myles Watson6bd442f2016-10-19 09:50:22 -07002294 btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_CANCEL_BOND,
2295 pairing_cb.state);
Ajay Panickerc138fe42016-02-05 16:50:20 -08002296
Myles Watson6bd442f2016-10-19 09:50:22 -07002297 /* TODO:
2298 ** 1. Restore scan modes
2299 ** 2. special handling for HID devices
2300 */
2301 if (pairing_cb.state == BT_BOND_STATE_BONDING) {
Myles Watson6bd442f2016-10-19 09:50:22 -07002302 if (pairing_cb.is_ssp) {
2303 if (pairing_cb.is_le_only) {
2304 BTA_DmBleSecurityGrant((uint8_t*)bd_addr->address,
2305 BTA_DM_SEC_PAIR_NOT_SPT);
2306 } else {
2307 BTA_DmConfirm((uint8_t*)bd_addr->address, false);
2308 BTA_DmBondCancel((uint8_t*)bd_addr->address);
2309 btif_storage_remove_bonded_device((bt_bdaddr_t*)bd_addr);
2310 }
Satya Calloji7fb0da62015-01-29 20:40:17 -08002311 } else {
Myles Watson6bd442f2016-10-19 09:50:22 -07002312 if (pairing_cb.is_le_only) {
2313 BTA_DmBondCancel((uint8_t*)bd_addr->address);
2314 } else {
2315 BTA_DmPinReply((uint8_t*)bd_addr->address, false, 0, NULL);
2316 }
2317 /* Cancel bonding, in case it is in ACL connection setup state */
2318 BTA_DmBondCancel((uint8_t*)bd_addr->address);
Satya Calloji7fb0da62015-01-29 20:40:17 -08002319 }
Myles Watson6bd442f2016-10-19 09:50:22 -07002320 }
2321
2322 return BT_STATUS_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002323}
2324
2325/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07002326 *
2327 * Function btif_dm_hh_open_failed
2328 *
2329 * Description informs the upper layers if the HH have failed during
Myles Watsonf4548162016-10-19 09:53:56 -07002330 * bonding
Myles Watson6bd442f2016-10-19 09:50:22 -07002331 *
2332 * Returns none
2333 *
2334 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002335
Myles Watson6bd442f2016-10-19 09:50:22 -07002336void btif_dm_hh_open_failed(bt_bdaddr_t* bdaddr) {
2337 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2338 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0) {
2339 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2340 }
2341}
The Android Open Source Project5738f832012-12-12 16:00:35 -08002342
Myles Watson6bd442f2016-10-19 09:50:22 -07002343/*******************************************************************************
2344 *
2345 * Function btif_dm_remove_bond
2346 *
2347 * Description Removes bonding with the specified device
2348 *
2349 * Returns bt_status_t
2350 *
2351 ******************************************************************************/
The Android Open Source Project5738f832012-12-12 16:00:35 -08002352
Myles Watson6bd442f2016-10-19 09:50:22 -07002353bt_status_t btif_dm_remove_bond(const bt_bdaddr_t* bd_addr) {
2354 bdstr_t bdstr;
2355
2356 BTIF_TRACE_EVENT("%s: bd_addr=%s", __func__,
2357 bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)));
2358
2359 btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_REMOVE_BOND,
2360 pairing_cb.state);
2361
2362 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2363 (char*)bd_addr, sizeof(bt_bdaddr_t), NULL);
2364
2365 return BT_STATUS_SUCCESS;
2366}
2367
2368/*******************************************************************************
2369 *
2370 * Function btif_dm_pin_reply
2371 *
2372 * Description BT legacy pairing - PIN code reply
2373 *
2374 * Returns bt_status_t
2375 *
2376 ******************************************************************************/
2377
2378bt_status_t btif_dm_pin_reply(const bt_bdaddr_t* bd_addr, uint8_t accept,
2379 uint8_t pin_len, bt_pin_code_t* pin_code) {
2380 BTIF_TRACE_EVENT("%s: accept=%d", __func__, accept);
2381 if (pin_code == NULL || pin_len > PIN_CODE_LEN) return BT_STATUS_FAIL;
Myles Watson6bd442f2016-10-19 09:50:22 -07002382 if (pairing_cb.is_le_only) {
2383 int i;
2384 uint32_t passkey = 0;
2385 int multi[] = {100000, 10000, 1000, 100, 10, 1};
2386 BD_ADDR remote_bd_addr;
2387 bdcpy(remote_bd_addr, bd_addr->address);
2388 for (i = 0; i < 6; i++) {
2389 passkey += (multi[i] * (pin_code->pin[i] - '0'));
The Android Open Source Project5738f832012-12-12 16:00:35 -08002390 }
Myles Watson6bd442f2016-10-19 09:50:22 -07002391 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
2392 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2393
2394 } else {
2395 BTA_DmPinReply((uint8_t*)bd_addr->address, accept, pin_len, pin_code->pin);
2396 if (accept) pairing_cb.pin_code_len = pin_len;
2397 }
Myles Watson6bd442f2016-10-19 09:50:22 -07002398 return BT_STATUS_SUCCESS;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002399}
2400
2401/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07002402 *
2403 * Function btif_dm_ssp_reply
2404 *
2405 * Description BT SSP Reply - Just Works, Numeric Comparison & Passkey
Myles Watsonf4548162016-10-19 09:53:56 -07002406 * Entry
Myles Watson6bd442f2016-10-19 09:50:22 -07002407 *
2408 * Returns bt_status_t
2409 *
2410 ******************************************************************************/
2411bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t* bd_addr,
2412 bt_ssp_variant_t variant, uint8_t accept,
Myles Watsond35a6482016-10-27 08:52:16 -07002413 UNUSED_ATTR uint32_t passkey) {
Myles Watson6bd442f2016-10-19 09:50:22 -07002414 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY) {
2415 /* This is not implemented in the stack.
2416 * For devices with display, this is not needed
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002417 */
Myles Watson6bd442f2016-10-19 09:50:22 -07002418 BTIF_TRACE_WARNING("%s: Not implemented", __func__);
2419 return BT_STATUS_FAIL;
2420 }
2421 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
2422 BTIF_TRACE_EVENT("%s: accept=%d", __func__, accept);
Myles Watson6bd442f2016-10-19 09:50:22 -07002423 if (pairing_cb.is_le_only) {
2424 if (pairing_cb.is_le_nc) {
2425 BTA_DmBleConfirmReply((uint8_t*)bd_addr->address, accept);
2426 } else {
2427 if (accept)
2428 BTA_DmBleSecurityGrant((uint8_t*)bd_addr->address, BTA_DM_SEC_GRANTED);
2429 else
2430 BTA_DmBleSecurityGrant((uint8_t*)bd_addr->address,
2431 BTA_DM_SEC_PAIR_NOT_SPT);
2432 }
2433 } else {
2434 BTA_DmConfirm((uint8_t*)bd_addr->address, accept);
2435 }
Myles Watson6bd442f2016-10-19 09:50:22 -07002436 return BT_STATUS_SUCCESS;
2437}
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002438
Myles Watson6bd442f2016-10-19 09:50:22 -07002439/*******************************************************************************
2440 *
2441 * Function btif_dm_get_adapter_property
2442 *
2443 * Description Queries the BTA for the adapter property
2444 *
2445 * Returns bt_status_t
2446 *
2447 ******************************************************************************/
2448bt_status_t btif_dm_get_adapter_property(bt_property_t* prop) {
2449 BTIF_TRACE_EVENT("%s: type=0x%x", __func__, prop->type);
2450 switch (prop->type) {
2451 case BT_PROPERTY_BDNAME: {
2452 bt_bdname_t* bd_name = (bt_bdname_t*)prop->val;
2453 strncpy((char*)bd_name->name, (char*)btif_get_default_local_name(),
2454 sizeof(bd_name->name) - 1);
2455 bd_name->name[sizeof(bd_name->name) - 1] = 0;
2456 prop->len = strlen((char*)bd_name->name);
2457 } break;
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002458
Myles Watson6bd442f2016-10-19 09:50:22 -07002459 case BT_PROPERTY_ADAPTER_SCAN_MODE: {
2460 /* if the storage does not have it. Most likely app never set it. Default
2461 * is NONE */
2462 bt_scan_mode_t* mode = (bt_scan_mode_t*)prop->val;
2463 *mode = BT_SCAN_MODE_NONE;
2464 prop->len = sizeof(bt_scan_mode_t);
2465 } break;
2466
2467 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: {
2468 uint32_t* tmt = (uint32_t*)prop->val;
2469 *tmt = 120; /* default to 120s, if not found in NV */
2470 prop->len = sizeof(uint32_t);
2471 } break;
2472
2473 default:
2474 prop->len = 0;
2475 return BT_STATUS_FAIL;
2476 }
2477 return BT_STATUS_SUCCESS;
2478}
2479
2480/*******************************************************************************
2481 *
2482 * Function btif_dm_get_remote_services
2483 *
2484 * Description Start SDP to get remote services
2485 *
2486 * Returns bt_status_t
2487 *
2488 ******************************************************************************/
2489bt_status_t btif_dm_get_remote_services(bt_bdaddr_t* remote_addr) {
2490 bdstr_t bdstr;
2491
2492 BTIF_TRACE_EVENT("%s: remote_addr=%s", __func__,
2493 bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr)));
2494
2495 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2496 bte_dm_search_services_evt, true);
2497
2498 return BT_STATUS_SUCCESS;
2499}
2500
2501/*******************************************************************************
2502 *
2503 * Function btif_dm_get_remote_services_transport
2504 *
2505 * Description Start SDP to get remote services by transport
2506 *
2507 * Returns bt_status_t
2508 *
2509 ******************************************************************************/
2510bt_status_t btif_dm_get_remote_services_by_transport(bt_bdaddr_t* remote_addr,
2511 const int transport) {
2512 BTIF_TRACE_EVENT("%s", __func__);
2513
2514 /* Set the mask extension */
2515 tBTA_SERVICE_MASK_EXT mask_ext;
2516 mask_ext.num_uuid = 0;
2517 mask_ext.p_uuid = NULL;
2518 mask_ext.srvc_mask = BTA_ALL_SERVICE_MASK;
2519
2520 BTA_DmDiscoverByTransport(remote_addr->address, &mask_ext,
2521 bte_dm_search_services_evt, true, transport);
2522
2523 return BT_STATUS_SUCCESS;
2524}
2525
2526/*******************************************************************************
2527 *
2528 * Function btif_dm_get_remote_service_record
2529 *
2530 * Description Start SDP to get remote service record
2531 *
2532 *
2533 * Returns bt_status_t
2534 ******************************************************************************/
2535bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t* remote_addr,
2536 bt_uuid_t* uuid) {
2537 tSDP_UUID sdp_uuid;
2538 bdstr_t bdstr;
2539
2540 BTIF_TRACE_EVENT("%s: remote_addr=%s", __func__,
2541 bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr)));
2542
2543 sdp_uuid.len = MAX_UUID_SIZE;
2544 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2545
2546 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2547 bte_dm_remote_service_record_evt, true);
2548
2549 return BT_STATUS_SUCCESS;
2550}
2551
2552void btif_dm_execute_service_request(uint16_t event, char* p_param) {
2553 bool b_enable = false;
2554 bt_status_t status;
2555 if (event == BTIF_DM_ENABLE_SERVICE) {
2556 b_enable = true;
2557 }
2558 status =
2559 btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2560 if (status == BT_STATUS_SUCCESS) {
2561 bt_property_t property;
2562 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2563
2564 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2565 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2566 sizeof(local_uuids), local_uuids);
2567 btif_storage_get_adapter_property(&property);
2568 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1,
2569 &property);
2570 }
2571 return;
2572}
2573
Myles Watson90088882016-11-15 16:33:22 -08002574void btif_dm_proc_io_req(UNUSED_ATTR BD_ADDR bd_addr,
2575 UNUSED_ATTR tBTA_IO_CAP* p_io_cap,
2576 UNUSED_ATTR tBTA_OOB_DATA* p_oob_data,
2577 tBTA_AUTH_REQ* p_auth_req, bool is_orig) {
Myles Watson6bd442f2016-10-19 09:50:22 -07002578 uint8_t yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2579 /* if local initiated:
2580 ** 1. set DD + MITM
2581 ** if remote initiated:
2582 ** 1. Copy over the auth_req from peer's io_rsp
2583 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo
2584 *(iPhone)
2585 ** as a fallback set MITM+GB if peer had MITM set
2586 */
Myles Watson6bd442f2016-10-19 09:50:22 -07002587
2588 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __func__, *p_auth_req);
2589 if (pairing_cb.is_local_initiated) {
2590 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2591 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2592 } else if (!is_orig) {
2593 /* peer initiated paring. They probably know what they want.
2594 ** Copy the mitm from peer device.
2595 */
2596 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d", __func__,
2597 pairing_cb.auth_req);
2598 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2599
2600 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo,
2601 * force MITM */
2602 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO))
2603 *p_auth_req |= BTA_AUTH_SP_YES;
2604 } else if (yes_no_bit) {
2605 /* set the general bonding bit for stored device */
2606 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2607 }
2608 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __func__, *p_auth_req);
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002609}
2610
Myles Watsond35a6482016-10-27 08:52:16 -07002611void btif_dm_proc_io_rsp(UNUSED_ATTR BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
Myles Watson90088882016-11-15 16:33:22 -08002612 UNUSED_ATTR tBTA_OOB_DATA oob_data,
2613 tBTA_AUTH_REQ auth_req) {
Myles Watson6bd442f2016-10-19 09:50:22 -07002614 if (auth_req & BTA_AUTH_BONDS) {
2615 BTIF_TRACE_DEBUG("%s auth_req:%d", __func__, auth_req);
2616 pairing_cb.auth_req = auth_req;
2617 pairing_cb.io_cap = io_cap;
2618 }
Ganesh Ganapathi Battaa217ab92014-04-28 16:30:55 -07002619}
2620
Myles Watson6bd442f2016-10-19 09:50:22 -07002621void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA* p_has_oob_data) {
2622 if (is_empty_128bit(oob_cb.oob_data.c192)) {
2623 *p_has_oob_data = false;
2624 } else {
2625 *p_has_oob_data = true;
2626 }
2627 BTIF_TRACE_DEBUG("%s: *p_has_oob_data=%d", __func__, *p_has_oob_data);
Jakub Pawlowski1a5bb5f2015-12-01 12:14:22 -08002628}
2629
Myles Watson6bd442f2016-10-19 09:50:22 -07002630void btif_dm_set_oob_for_le_io_req(BD_ADDR bd_addr,
2631 tBTA_OOB_DATA* p_has_oob_data,
2632 tBTA_LE_AUTH_REQ* p_auth_req) {
2633 if (!is_empty_128bit(oob_cb.oob_data.le_sc_c) &&
2634 !is_empty_128bit(oob_cb.oob_data.le_sc_r)) {
2635 /* We have LE SC OOB data */
Jakub Pawlowski1a5bb5f2015-12-01 12:14:22 -08002636
Myles Watson6bd442f2016-10-19 09:50:22 -07002637 /* make sure OOB data is for this particular device */
2638 if (memcmp(bd_addr, oob_cb.bdaddr, BD_ADDR_LEN) == 0) {
2639 *p_auth_req = ((*p_auth_req) | BTM_LE_AUTH_REQ_SC_ONLY);
2640 *p_has_oob_data = true;
Jakub Pawlowskibd608542016-07-28 05:58:35 -07002641 } else {
Myles Watson6bd442f2016-10-19 09:50:22 -07002642 *p_has_oob_data = false;
2643 BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address",
2644 __func__);
Jakub Pawlowski1a5bb5f2015-12-01 12:14:22 -08002645 }
Myles Watson6bd442f2016-10-19 09:50:22 -07002646 } else if (!is_empty_128bit(oob_cb.oob_data.sm_tk)) {
2647 /* We have security manager TK */
2648
2649 /* make sure OOB data is for this particular device */
2650 if (memcmp(bd_addr, oob_cb.bdaddr, BD_ADDR_LEN) == 0) {
2651 // When using OOB with TK, SC Secure Connections bit must be disabled.
2652 tBTA_LE_AUTH_REQ mask = ~BTM_LE_AUTH_REQ_SC_ONLY;
2653 *p_auth_req = ((*p_auth_req) & mask);
2654
2655 *p_has_oob_data = true;
2656 } else {
2657 *p_has_oob_data = false;
2658 BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address",
2659 __func__);
2660 }
2661 } else {
2662 *p_has_oob_data = false;
2663 }
2664 BTIF_TRACE_DEBUG("%s *p_has_oob_data=%d", __func__, *p_has_oob_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002665}
The Android Open Source Project5738f832012-12-12 16:00:35 -08002666
2667#ifdef BTIF_DM_OOB_TEST
Myles Watson6bd442f2016-10-19 09:50:22 -07002668void btif_dm_load_local_oob(void) {
2669 char prop_oob[PROPERTY_VALUE_MAX];
2670 osi_property_get("service.brcm.bt.oob", prop_oob, "3");
2671 BTIF_TRACE_DEBUG("%s: prop_oob = %s", __func__, prop_oob);
2672 if (prop_oob[0] != '3') {
2673 if (is_empty_128bit(oob_cb.oob_data.c192)) {
2674 BTIF_TRACE_DEBUG("%s: read OOB, call BTA_DmLocalOob()", __func__);
2675 BTA_DmLocalOob();
The Android Open Source Project5738f832012-12-12 16:00:35 -08002676 }
Myles Watson6bd442f2016-10-19 09:50:22 -07002677 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002678}
2679
Myles Watson6bd442f2016-10-19 09:50:22 -07002680void btif_dm_proc_loc_oob(bool valid, BT_OCTET16 c, BT_OCTET16 r) {
2681 FILE* fp;
2682 const char* path_a = "/data/misc/bluedroid/LOCAL/a.key";
2683 const char* path_b = "/data/misc/bluedroid/LOCAL/b.key";
2684 const char* path = NULL;
2685 char prop_oob[PROPERTY_VALUE_MAX];
2686 BTIF_TRACE_DEBUG("%s: valid=%d", __func__, valid);
2687 if (is_empty_128bit(oob_cb.oob_data.c192) && valid) {
2688 BTIF_TRACE_DEBUG("save local OOB data in memory");
2689 memcpy(oob_cb.oob_data.c192, c, BT_OCTET16_LEN);
2690 memcpy(oob_cb.oob_data.r192, r, BT_OCTET16_LEN);
Jakub Pawlowski3eb4a482016-02-24 10:39:46 -08002691 osi_property_get("service.brcm.bt.oob", prop_oob, "3");
Jakub Pawlowski1a5bb5f2015-12-01 12:14:22 -08002692 BTIF_TRACE_DEBUG("%s: prop_oob = %s", __func__, prop_oob);
The Android Open Source Project5738f832012-12-12 16:00:35 -08002693 if (prop_oob[0] == '1')
Myles Watson6bd442f2016-10-19 09:50:22 -07002694 path = path_a;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002695 else if (prop_oob[0] == '2')
Myles Watson6bd442f2016-10-19 09:50:22 -07002696 path = path_b;
2697 if (path) {
2698 fp = fopen(path, "wb+");
2699 if (fp == NULL) {
2700 BTIF_TRACE_DEBUG("%s: failed to save local OOB data to %s", __func__,
2701 path);
2702 } else {
2703 BTIF_TRACE_DEBUG("%s: save local OOB data into file %s", __func__,
2704 path);
2705 fwrite(c, 1, BT_OCTET16_LEN, fp);
2706 fwrite(r, 1, BT_OCTET16_LEN, fp);
2707 fclose(fp);
2708 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08002709 }
Myles Watson6bd442f2016-10-19 09:50:22 -07002710 }
2711}
2712
2713/*******************************************************************************
2714 *
2715 * Function btif_dm_get_smp_config
2716 *
2717 * Description Retrieve the SMP pairing options from the bt_stack.conf
2718 * file. To provide specific pairing options for the host
2719 * add a node with label "SmpOptions" to the config file
2720 * and assign it a comma separated list of 5 values in the
2721 * format: auth, io, ikey, rkey, ksize, oob
2722 * eg: PTS_SmpOptions=0xD,0x4,0xf,0xf,0x10
2723 *
2724 * Parameters: tBTE_APPL_CFG*: pointer to struct defining pairing options
2725 *
2726 * Returns true if the options were successfully read, else false
2727 *
2728 ******************************************************************************/
2729bool btif_dm_get_smp_config(tBTE_APPL_CFG* p_cfg) {
2730 if (!stack_config_get_interface()->get_pts_smp_options()) {
2731 BTIF_TRACE_DEBUG("%s: SMP options not found in configuration", __func__);
2732 return false;
2733 }
2734
2735 char conf[64];
2736 const char* recv = stack_config_get_interface()->get_pts_smp_options();
2737 char* pch;
2738 char* endptr;
2739
2740 strncpy(conf, recv, 64);
2741 conf[63] = 0; // null terminate
2742
Marie Janssenf33b6f42016-11-22 15:01:42 -08002743 pch = strtok(conf, ",");
2744 if (pch != NULL)
Myles Watson6bd442f2016-10-19 09:50:22 -07002745 p_cfg->ble_auth_req = (uint8_t)strtoul(pch, &endptr, 16);
2746 else
2747 return false;
2748
Marie Janssenf33b6f42016-11-22 15:01:42 -08002749 pch = strtok(NULL, ",");
2750 if (pch != NULL)
Myles Watson6bd442f2016-10-19 09:50:22 -07002751 p_cfg->ble_io_cap = (uint8_t)strtoul(pch, &endptr, 16);
2752 else
2753 return false;
2754
Marie Janssenf33b6f42016-11-22 15:01:42 -08002755 pch = strtok(NULL, ",");
2756 if (pch != NULL)
Myles Watson6bd442f2016-10-19 09:50:22 -07002757 p_cfg->ble_init_key = (uint8_t)strtoul(pch, &endptr, 16);
2758 else
2759 return false;
2760
Marie Janssenf33b6f42016-11-22 15:01:42 -08002761 pch = strtok(NULL, ",");
2762 if (pch != NULL)
Myles Watson6bd442f2016-10-19 09:50:22 -07002763 p_cfg->ble_resp_key = (uint8_t)strtoul(pch, &endptr, 16);
2764 else
2765 return false;
2766
Marie Janssenf33b6f42016-11-22 15:01:42 -08002767 pch = strtok(NULL, ",");
2768 if (pch != NULL)
Myles Watson6bd442f2016-10-19 09:50:22 -07002769 p_cfg->ble_max_key_size = (uint8_t)strtoul(pch, &endptr, 16);
2770 else
2771 return false;
2772
2773 return true;
2774}
2775
2776bool btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r) {
2777 char t[128];
2778 FILE* fp;
2779 const char* path_a = "/data/misc/bluedroid/LOCAL/a.key";
2780 const char* path_b = "/data/misc/bluedroid/LOCAL/b.key";
2781 const char* path = NULL;
2782 char prop_oob[PROPERTY_VALUE_MAX];
2783 bool result = false;
2784 bt_bdaddr_t bt_bd_addr;
2785 bdcpy(oob_cb.bdaddr, bd_addr);
2786 osi_property_get("service.brcm.bt.oob", prop_oob, "3");
2787 BTIF_TRACE_DEBUG("%s: prop_oob = %s", __func__, prop_oob);
2788 if (prop_oob[0] == '1')
2789 path = path_b;
2790 else if (prop_oob[0] == '2')
2791 path = path_a;
2792 if (path) {
2793 fp = fopen(path, "rb");
2794 if (fp == NULL) {
2795 BTIF_TRACE_DEBUG("%s: failed to read OOB keys from %s", __func__, path);
2796 return false;
2797 } else {
2798 BTIF_TRACE_DEBUG("%s: read OOB data from %s", __func__, path);
2799 fread(p_c, 1, BT_OCTET16_LEN, fp);
2800 fread(p_r, 1, BT_OCTET16_LEN, fp);
2801 fclose(fp);
2802 }
2803 BTIF_TRACE_DEBUG("----%s: true", __func__);
2804 snprintf(t, sizeof(t), "%02x:%02x:%02x:%02x:%02x:%02x", oob_cb.bdaddr[0],
2805 oob_cb.bdaddr[1], oob_cb.bdaddr[2], oob_cb.bdaddr[3],
2806 oob_cb.bdaddr[4], oob_cb.bdaddr[5]);
2807 BTIF_TRACE_DEBUG("----%s: peer_bdaddr = %s", __func__, t);
2808 snprintf(t, sizeof(t),
2809 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x "
2810 "%02x %02x %02x",
2811 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
2812 p_c[8], p_c[9], p_c[10], p_c[11], p_c[12], p_c[13], p_c[14],
2813 p_c[15]);
2814 BTIF_TRACE_DEBUG("----%s: c = %s", __func__, t);
2815 snprintf(t, sizeof(t),
2816 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x "
2817 "%02x %02x %02x",
2818 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
2819 p_r[8], p_r[9], p_r[10], p_r[11], p_r[12], p_r[13], p_r[14],
2820 p_r[15]);
2821 BTIF_TRACE_DEBUG("----%s: r = %s", __func__, t);
2822 bdcpy(bt_bd_addr.address, bd_addr);
2823 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2824 (char*)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2825 result = true;
2826 }
2827 BTIF_TRACE_DEBUG("%s: result=%d", __func__, result);
2828 return result;
The Android Open Source Project5738f832012-12-12 16:00:35 -08002829}
2830#endif /* BTIF_DM_OOB_TEST */
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002831
Myles Watson6bd442f2016-10-19 09:50:22 -07002832static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF* p_ssp_key_notif) {
2833 bt_bdaddr_t bd_addr;
2834 bt_bdname_t bd_name;
2835 uint32_t cod;
2836 int dev_type;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002837
Myles Watson6bd442f2016-10-19 09:50:22 -07002838 BTIF_TRACE_DEBUG("%s", __func__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002839
Myles Watson6bd442f2016-10-19 09:50:22 -07002840 /* Remote name update */
2841 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type)) {
2842 dev_type = BT_DEVICE_TYPE_BLE;
2843 }
2844 btif_dm_update_ble_remote_properties(p_ssp_key_notif->bd_addr,
2845 p_ssp_key_notif->bd_name,
2846 (tBT_DEVICE_TYPE)dev_type);
2847 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2848 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002849
Myles Watson6bd442f2016-10-19 09:50:22 -07002850 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2851 pairing_cb.is_ssp = false;
2852 cod = COD_UNCLASSIFIED;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002853
Myles Watson6bd442f2016-10-19 09:50:22 -07002854 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
2855 BT_SSP_VARIANT_PASSKEY_NOTIFICATION, p_ssp_key_notif->passkey);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002856}
2857
2858/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07002859 *
2860 * Function btif_dm_ble_auth_cmpl_evt
2861 *
2862 * Description Executes authentication complete event in btif context
2863 *
2864 * Returns void
2865 *
2866 ******************************************************************************/
2867static void btif_dm_ble_auth_cmpl_evt(tBTA_DM_AUTH_CMPL* p_auth_cmpl) {
2868 /* Save link key, if not temporary */
2869 bt_bdaddr_t bd_addr;
2870 bt_status_t status = BT_STATUS_FAIL;
2871 bt_bond_state_t state = BT_BOND_STATE_NONE;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002872
Myles Watson6bd442f2016-10-19 09:50:22 -07002873 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
Jakub Pawlowski1a5bb5f2015-12-01 12:14:22 -08002874
Myles Watson6bd442f2016-10-19 09:50:22 -07002875 /* Clear OOB data */
2876 memset(&oob_cb, 0, sizeof(oob_cb));
Jakub Pawlowski1a5bb5f2015-12-01 12:14:22 -08002877
Myles Watson6bd442f2016-10-19 09:50:22 -07002878 if ((p_auth_cmpl->success == true) && (p_auth_cmpl->key_present)) {
2879 /* store keys */
2880 }
2881 if (p_auth_cmpl->success) {
2882 status = BT_STATUS_SUCCESS;
2883 state = BT_BOND_STATE_BONDED;
2884 int addr_type;
2885 bt_bdaddr_t bdaddr;
2886 bdcpy(bdaddr.address, p_auth_cmpl->bd_addr);
2887 if (btif_storage_get_remote_addr_type(&bdaddr, &addr_type) !=
2888 BT_STATUS_SUCCESS)
2889 btif_storage_set_remote_addr_type(&bdaddr, p_auth_cmpl->addr_type);
2890
2891 /* Test for temporary bonding */
2892 if (btm_get_bond_type_dev(p_auth_cmpl->bd_addr) == BOND_TYPE_TEMPORARY) {
2893 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
2894 __func__);
2895 btif_storage_remove_bonded_device(&bdaddr);
2896 state = BT_BOND_STATE_NONE;
2897 } else {
2898 btif_dm_save_ble_bonding_keys();
2899 BTA_GATTC_Refresh(bd_addr.address);
2900 btif_dm_get_remote_services_by_transport(&bd_addr, BTA_GATT_TRANSPORT_LE);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002901 }
Myles Watson6bd442f2016-10-19 09:50:22 -07002902 } else {
2903 /*Map the HCI fail reason to bt status */
2904 switch (p_auth_cmpl->fail_reason) {
2905 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
2906 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
2907 case BTA_DM_AUTH_SMP_UNKNOWN_ERR:
2908 case BTA_DM_AUTH_SMP_CONN_TOUT:
2909 btif_dm_remove_ble_bonding_keys();
2910 status = BT_STATUS_AUTH_FAILURE;
2911 break;
2912 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
2913 status = BT_STATUS_AUTH_REJECTED;
2914 break;
2915 default:
2916 btif_dm_remove_ble_bonding_keys();
2917 status = BT_STATUS_FAIL;
2918 break;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002919 }
Myles Watson6bd442f2016-10-19 09:50:22 -07002920 }
2921 bond_state_changed(status, &bd_addr, state);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002922}
2923
Myles Watson6bd442f2016-10-19 09:50:22 -07002924void btif_dm_load_ble_local_keys(void) {
2925 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002926
Myles Watson6bd442f2016-10-19 09:50:22 -07002927 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,
2928 (char*)&ble_local_key_cb.er[0],
2929 BT_OCTET16_LEN) == BT_STATUS_SUCCESS) {
2930 ble_local_key_cb.is_er_rcvd = true;
2931 BTIF_TRACE_DEBUG("%s BLE ER key loaded", __func__);
2932 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002933
Myles Watson6bd442f2016-10-19 09:50:22 -07002934 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,
2935 (char*)&ble_local_key_cb.id_keys.ir[0],
2936 BT_OCTET16_LEN) == BT_STATUS_SUCCESS) &&
2937 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK,
2938 (char*)&ble_local_key_cb.id_keys.irk[0],
2939 BT_OCTET16_LEN) == BT_STATUS_SUCCESS) &&
2940 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,
2941 (char*)&ble_local_key_cb.id_keys.dhk[0],
2942 BT_OCTET16_LEN) == BT_STATUS_SUCCESS)) {
2943 ble_local_key_cb.is_id_keys_rcvd = true;
2944 BTIF_TRACE_DEBUG("%s BLE ID keys loaded", __func__);
2945 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002946}
Myles Watson6bd442f2016-10-19 09:50:22 -07002947void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK* p_key_mask,
2948 BT_OCTET16 er,
2949 tBTA_BLE_LOCAL_ID_KEYS* p_id_keys) {
2950 if (ble_local_key_cb.is_er_rcvd) {
2951 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2952 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2953 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002954
Myles Watson6bd442f2016-10-19 09:50:22 -07002955 if (ble_local_key_cb.is_id_keys_rcvd) {
2956 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0],
2957 sizeof(BT_OCTET16));
2958 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0],
2959 sizeof(BT_OCTET16));
2960 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0],
2961 sizeof(BT_OCTET16));
2962 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
2963 }
2964 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x", __func__, *p_key_mask);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002965}
2966
Myles Watson6bd442f2016-10-19 09:50:22 -07002967void btif_dm_save_ble_bonding_keys(void) {
2968 bt_bdaddr_t bd_addr;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002969
Myles Watson6bd442f2016-10-19 09:50:22 -07002970 BTIF_TRACE_DEBUG("%s", __func__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002971
Myles Watson6bd442f2016-10-19 09:50:22 -07002972 bdcpy(bd_addr.address, pairing_cb.bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002973
Myles Watson6bd442f2016-10-19 09:50:22 -07002974 if (pairing_cb.ble.is_penc_key_rcvd) {
2975 btif_storage_add_ble_bonding_key(&bd_addr, (char*)&pairing_cb.ble.penc_key,
2976 BTIF_DM_LE_KEY_PENC,
2977 sizeof(tBTM_LE_PENC_KEYS));
2978 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002979
Myles Watson6bd442f2016-10-19 09:50:22 -07002980 if (pairing_cb.ble.is_pid_key_rcvd) {
2981 btif_storage_add_ble_bonding_key(&bd_addr, (char*)&pairing_cb.ble.pid_key,
2982 BTIF_DM_LE_KEY_PID,
2983 sizeof(tBTM_LE_PID_KEYS));
2984 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002985
Myles Watson6bd442f2016-10-19 09:50:22 -07002986 if (pairing_cb.ble.is_pcsrk_key_rcvd) {
2987 btif_storage_add_ble_bonding_key(&bd_addr, (char*)&pairing_cb.ble.pcsrk_key,
2988 BTIF_DM_LE_KEY_PCSRK,
2989 sizeof(tBTM_LE_PCSRK_KEYS));
2990 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002991
Myles Watson6bd442f2016-10-19 09:50:22 -07002992 if (pairing_cb.ble.is_lenc_key_rcvd) {
2993 btif_storage_add_ble_bonding_key(&bd_addr, (char*)&pairing_cb.ble.lenc_key,
2994 BTIF_DM_LE_KEY_LENC,
2995 sizeof(tBTM_LE_LENC_KEYS));
2996 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08002997
Myles Watson6bd442f2016-10-19 09:50:22 -07002998 if (pairing_cb.ble.is_lcsrk_key_rcvd) {
2999 btif_storage_add_ble_bonding_key(&bd_addr, (char*)&pairing_cb.ble.lcsrk_key,
3000 BTIF_DM_LE_KEY_LCSRK,
3001 sizeof(tBTM_LE_LCSRK_KEYS));
3002 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003003
Myles Watson6bd442f2016-10-19 09:50:22 -07003004 if (pairing_cb.ble.is_lidk_key_rcvd) {
3005 btif_storage_add_ble_bonding_key(&bd_addr, NULL, BTIF_DM_LE_KEY_LID, 0);
3006 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003007}
3008
Myles Watson6bd442f2016-10-19 09:50:22 -07003009void btif_dm_remove_ble_bonding_keys(void) {
3010 bt_bdaddr_t bd_addr;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003011
Myles Watson6bd442f2016-10-19 09:50:22 -07003012 BTIF_TRACE_DEBUG("%s", __func__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003013
Myles Watson6bd442f2016-10-19 09:50:22 -07003014 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3015 btif_storage_remove_ble_bonding_keys(&bd_addr);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003016}
3017
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003018/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07003019 *
3020 * Function btif_dm_ble_sec_req_evt
3021 *
3022 * Description Eprocess security request event in btif context
3023 *
3024 * Returns void
3025 *
3026 ******************************************************************************/
3027void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ* p_ble_req) {
3028 bt_bdaddr_t bd_addr;
3029 bt_bdname_t bd_name;
3030 uint32_t cod;
3031 int dev_type;
Matthew Xie86f97ed2014-11-10 10:24:46 -08003032
Myles Watson6bd442f2016-10-19 09:50:22 -07003033 BTIF_TRACE_DEBUG("%s", __func__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003034
Myles Watson6bd442f2016-10-19 09:50:22 -07003035 if (pairing_cb.state == BT_BOND_STATE_BONDING) {
3036 BTIF_TRACE_DEBUG("%s Discard security request", __func__);
3037 return;
3038 }
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003039
Myles Watson6bd442f2016-10-19 09:50:22 -07003040 /* Remote name update */
3041 if (!btif_get_device_type(p_ble_req->bd_addr, &dev_type)) {
3042 dev_type = BT_DEVICE_TYPE_BLE;
3043 }
3044 btif_dm_update_ble_remote_properties(p_ble_req->bd_addr, p_ble_req->bd_name,
3045 (tBT_DEVICE_TYPE)dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003046
Myles Watson6bd442f2016-10-19 09:50:22 -07003047 bdcpy(bd_addr.address, p_ble_req->bd_addr);
3048 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003049
Myles Watson6bd442f2016-10-19 09:50:22 -07003050 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003051
Myles Watson6bd442f2016-10-19 09:50:22 -07003052 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
3053 pairing_cb.is_le_only = true;
3054 pairing_cb.is_le_nc = false;
3055 pairing_cb.is_ssp = true;
3056 btm_set_bond_type_dev(p_ble_req->bd_addr, pairing_cb.bond_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003057
Myles Watson6bd442f2016-10-19 09:50:22 -07003058 cod = COD_UNCLASSIFIED;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003059
Myles Watson6bd442f2016-10-19 09:50:22 -07003060 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
3061 BT_SSP_VARIANT_CONSENT, 0);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003062}
3063
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003064/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07003065 *
3066 * Function btif_dm_ble_passkey_req_evt
3067 *
3068 * Description Executes pin request event in btif context
3069 *
3070 * Returns void
3071 *
3072 ******************************************************************************/
3073static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ* p_pin_req) {
3074 bt_bdaddr_t bd_addr;
3075 bt_bdname_t bd_name;
3076 uint32_t cod;
3077 int dev_type;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003078
Myles Watson6bd442f2016-10-19 09:50:22 -07003079 /* Remote name update */
3080 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type)) {
3081 dev_type = BT_DEVICE_TYPE_BLE;
3082 }
3083 btif_dm_update_ble_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
3084 (tBT_DEVICE_TYPE)dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003085
Myles Watson6bd442f2016-10-19 09:50:22 -07003086 bdcpy(bd_addr.address, p_pin_req->bd_addr);
3087 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003088
Myles Watson6bd442f2016-10-19 09:50:22 -07003089 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3090 pairing_cb.is_le_only = true;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003091
Myles Watson6bd442f2016-10-19 09:50:22 -07003092 cod = COD_UNCLASSIFIED;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003093
Myles Watson6bd442f2016-10-19 09:50:22 -07003094 HAL_CBACK(bt_hal_cbacks, pin_request_cb, &bd_addr, &bd_name, cod, false);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003095}
Myles Watson6bd442f2016-10-19 09:50:22 -07003096static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF* p_notif_req) {
3097 /* TODO implement key notification for numeric comparison */
3098 BTIF_TRACE_DEBUG("%s", __func__);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003099
Myles Watson6bd442f2016-10-19 09:50:22 -07003100 /* Remote name update */
3101 btif_update_remote_properties(p_notif_req->bd_addr, p_notif_req->bd_name,
3102 NULL, BT_DEVICE_TYPE_BLE);
Satya Calloji444a8da2015-03-06 10:38:22 -08003103
Myles Watson6bd442f2016-10-19 09:50:22 -07003104 bt_bdaddr_t bd_addr;
3105 bdcpy(bd_addr.address, p_notif_req->bd_addr);
Satya Calloji444a8da2015-03-06 10:38:22 -08003106
Myles Watson6bd442f2016-10-19 09:50:22 -07003107 bt_bdname_t bd_name;
3108 memcpy(bd_name.name, p_notif_req->bd_name, BD_NAME_LEN);
Satya Calloji444a8da2015-03-06 10:38:22 -08003109
Myles Watson6bd442f2016-10-19 09:50:22 -07003110 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3111 pairing_cb.is_ssp = false;
3112 pairing_cb.is_le_only = true;
3113 pairing_cb.is_le_nc = true;
Satya Calloji444a8da2015-03-06 10:38:22 -08003114
Myles Watson6bd442f2016-10-19 09:50:22 -07003115 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, COD_UNCLASSIFIED,
3116 BT_SSP_VARIANT_PASSKEY_CONFIRMATION, p_notif_req->passkey);
Satya Calloji444a8da2015-03-06 10:38:22 -08003117}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003118
Myles Watson6bd442f2016-10-19 09:50:22 -07003119static void btif_dm_ble_oob_req_evt(tBTA_DM_SP_RMT_OOB* req_oob_type) {
3120 BTIF_TRACE_DEBUG("%s", __func__);
Jakub Pawlowski1a5bb5f2015-12-01 12:14:22 -08003121
Myles Watson6bd442f2016-10-19 09:50:22 -07003122 bt_bdaddr_t bd_addr;
3123 bdcpy(bd_addr.address, req_oob_type->bd_addr);
3124 /* We already checked if OOB data is present in
3125 * btif_dm_set_oob_for_le_io_req, but check here again. If it's not present
3126 * do nothing, pairing will timeout.
3127 */
3128 if (is_empty_128bit(oob_cb.oob_data.sm_tk)) {
3129 return;
3130 }
Jakub Pawlowski1a5bb5f2015-12-01 12:14:22 -08003131
Myles Watson6bd442f2016-10-19 09:50:22 -07003132 /* make sure OOB data is for this particular device */
3133 if (memcmp(req_oob_type->bd_addr, oob_cb.bdaddr, BD_ADDR_LEN) != 0) {
3134 BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address",
3135 __func__);
3136 return;
3137 }
Jakub Pawlowski1a5bb5f2015-12-01 12:14:22 -08003138
Myles Watson6bd442f2016-10-19 09:50:22 -07003139 /* Remote name update */
3140 btif_update_remote_properties(req_oob_type->bd_addr, req_oob_type->bd_name,
3141 NULL, BT_DEVICE_TYPE_BLE);
Jakub Pawlowski1a5bb5f2015-12-01 12:14:22 -08003142
Myles Watson6bd442f2016-10-19 09:50:22 -07003143 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3144 pairing_cb.is_ssp = false;
3145 pairing_cb.is_le_only = true;
3146 pairing_cb.is_le_nc = false;
Jakub Pawlowski1a5bb5f2015-12-01 12:14:22 -08003147
Myles Watson6bd442f2016-10-19 09:50:22 -07003148 BTM_BleOobDataReply(req_oob_type->bd_addr, 0, 16, oob_cb.oob_data.sm_tk);
Jakub Pawlowski1a5bb5f2015-12-01 12:14:22 -08003149}
3150
Myles Watson6bd442f2016-10-19 09:50:22 -07003151static void btif_dm_ble_sc_oob_req_evt(tBTA_DM_SP_RMT_OOB* req_oob_type) {
3152 BTIF_TRACE_DEBUG("%s", __func__);
Jakub Pawlowskibd608542016-07-28 05:58:35 -07003153
Myles Watson6bd442f2016-10-19 09:50:22 -07003154 bt_bdaddr_t bd_addr;
3155 bdcpy(bd_addr.address, req_oob_type->bd_addr);
Jakub Pawlowskibd608542016-07-28 05:58:35 -07003156
Myles Watson6bd442f2016-10-19 09:50:22 -07003157 /* We already checked if OOB data is present in
3158 * btif_dm_set_oob_for_le_io_req, but check here again. If it's not present
3159 * do nothing, pairing will timeout.
3160 */
Jakub Pawlowskia638cc52016-11-10 15:56:46 -08003161 if (is_empty_128bit(oob_cb.oob_data.le_sc_c) &&
3162 is_empty_128bit(oob_cb.oob_data.le_sc_r)) {
3163 BTIF_TRACE_WARNING("%s: LE SC OOB data is empty", __func__);
Myles Watson6bd442f2016-10-19 09:50:22 -07003164 return;
3165 }
Jakub Pawlowskibd608542016-07-28 05:58:35 -07003166
Myles Watson6bd442f2016-10-19 09:50:22 -07003167 /* make sure OOB data is for this particular device */
3168 if (memcmp(req_oob_type->bd_addr, oob_cb.bdaddr, BD_ADDR_LEN) != 0) {
3169 BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address",
3170 __func__);
3171 return;
3172 }
Jakub Pawlowskibd608542016-07-28 05:58:35 -07003173
Myles Watson6bd442f2016-10-19 09:50:22 -07003174 /* Remote name update */
3175 btif_update_remote_properties(req_oob_type->bd_addr, req_oob_type->bd_name,
3176 NULL, BT_DEVICE_TYPE_BLE);
Jakub Pawlowskibd608542016-07-28 05:58:35 -07003177
Myles Watson6bd442f2016-10-19 09:50:22 -07003178 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3179 pairing_cb.is_ssp = false;
3180 pairing_cb.is_le_only =
3181 true; // TODO: we can derive classic pairing from this one
3182 pairing_cb.is_le_nc = false;
Jakub Pawlowskibd608542016-07-28 05:58:35 -07003183
Myles Watson6bd442f2016-10-19 09:50:22 -07003184 BTM_BleSecureConnectionOobDataReply(
3185 req_oob_type->bd_addr, oob_cb.oob_data.le_sc_c, oob_cb.oob_data.le_sc_r);
Jakub Pawlowskibd608542016-07-28 05:58:35 -07003186}
3187
Myles Watson6bd442f2016-10-19 09:50:22 -07003188void btif_dm_update_ble_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
3189 tBT_DEVICE_TYPE dev_type) {
3190 btif_update_remote_properties(bd_addr, bd_name, NULL, dev_type);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003191}
3192
Myles Watson6bd442f2016-10-19 09:50:22 -07003193static void btif_dm_ble_tx_test_cback(void* p) {
3194 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST, (char*)p, 1,
3195 NULL);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003196}
3197
Myles Watson6bd442f2016-10-19 09:50:22 -07003198static void btif_dm_ble_rx_test_cback(void* p) {
3199 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST, (char*)p, 1,
3200 NULL);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003201}
3202
Myles Watson6bd442f2016-10-19 09:50:22 -07003203static void btif_dm_ble_test_end_cback(void* p) {
3204 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END, (char*)p,
3205 3, NULL);
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003206}
3207/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07003208 *
3209 * Function btif_le_test_mode
3210 *
3211 * Description Sends a HCI BLE Test command to the Controller
3212 *
3213 * Returns BT_STATUS_SUCCESS on success
3214 *
3215 ******************************************************************************/
3216bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len) {
3217 switch (opcode) {
3218 case HCI_BLE_TRANSMITTER_TEST:
3219 if (len != 3) return BT_STATUS_PARM_INVALID;
3220 BTM_BleTransmitterTest(buf[0], buf[1], buf[2], btif_dm_ble_tx_test_cback);
3221 break;
3222 case HCI_BLE_RECEIVER_TEST:
3223 if (len != 1) return BT_STATUS_PARM_INVALID;
3224 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
3225 break;
3226 case HCI_BLE_TEST_END:
3227 BTM_BleTestEnd((tBTM_CMPL_CB*)btif_dm_ble_test_end_cback);
3228 break;
3229 default:
3230 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __func__,
3231 opcode);
3232 return BT_STATUS_UNSUPPORTED;
3233 }
3234 return BT_STATUS_SUCCESS;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -08003235}
The Android Open Source Project5738f832012-12-12 16:00:35 -08003236
Myles Watson6bd442f2016-10-19 09:50:22 -07003237void btif_dm_on_disable() {
3238 /* cancel any pending pairing requests */
3239 if (pairing_cb.state == BT_BOND_STATE_BONDING) {
3240 bt_bdaddr_t bd_addr;
The Android Open Source Project5738f832012-12-12 16:00:35 -08003241
Myles Watson6bd442f2016-10-19 09:50:22 -07003242 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __func__);
3243 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3244 btif_dm_cancel_bond(&bd_addr);
3245 }
The Android Open Source Project5738f832012-12-12 16:00:35 -08003246}
Matthew Xie1e5109b2012-11-09 18:26:26 -08003247
Satya Callojie5ba8842014-07-03 17:18:02 -07003248/*******************************************************************************
Myles Watson6bd442f2016-10-19 09:50:22 -07003249 *
3250 * Function btif_dm_read_energy_info
3251 *
3252 * Description Reads the energy info from controller
3253 *
3254 * Returns void
3255 *
3256 ******************************************************************************/
Myles Watson90088882016-11-15 16:33:22 -08003257void btif_dm_read_energy_info() { BTA_DmBleGetEnergyInfo(bta_energy_info_cb); }
Satya Callojie5ba8842014-07-03 17:18:02 -07003258
Matthew Xie1e5109b2012-11-09 18:26:26 -08003259static char* btif_get_default_local_name() {
Myles Watson6bd442f2016-10-19 09:50:22 -07003260 if (btif_default_local_name[0] == '\0') {
3261 int max_len = sizeof(btif_default_local_name) - 1;
3262 if (BTM_DEF_LOCAL_NAME[0] != '\0') {
3263 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
3264 } else {
3265 char prop_model[PROPERTY_VALUE_MAX];
3266 osi_property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
3267 strncpy(btif_default_local_name, prop_model, max_len);
Matthew Xie1e5109b2012-11-09 18:26:26 -08003268 }
Myles Watson6bd442f2016-10-19 09:50:22 -07003269 btif_default_local_name[max_len] = '\0';
3270 }
3271 return btif_default_local_name;
Matthew Xie1e5109b2012-11-09 18:26:26 -08003272}
Ajay Panickerc138fe42016-02-05 16:50:20 -08003273
Myles Watson6bd442f2016-10-19 09:50:22 -07003274static void btif_stats_add_bond_event(const bt_bdaddr_t* bd_addr,
Ajay Panickerc138fe42016-02-05 16:50:20 -08003275 bt_bond_function_t function,
3276 bt_bond_state_t state) {
Myles Watson6bd442f2016-10-19 09:50:22 -07003277 std::unique_lock<std::mutex> lock(bond_event_lock);
Ajay Panickerc138fe42016-02-05 16:50:20 -08003278
Myles Watson6bd442f2016-10-19 09:50:22 -07003279 btif_bond_event_t* event = &btif_dm_bond_events[btif_events_end_index];
3280 memcpy(&event->bd_addr, bd_addr, sizeof(bt_bdaddr_t));
3281 event->function = function;
3282 event->state = state;
3283 clock_gettime(CLOCK_REALTIME, &event->timestamp);
Ajay Panickerc138fe42016-02-05 16:50:20 -08003284
Myles Watson6bd442f2016-10-19 09:50:22 -07003285 btif_num_bond_events++;
3286 btif_events_end_index =
3287 (btif_events_end_index + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1);
3288 if (btif_events_end_index == btif_events_start_index) {
3289 btif_events_start_index =
3290 (btif_events_start_index + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1);
3291 }
Ajay Panickerc138fe42016-02-05 16:50:20 -08003292
Myles Watson6bd442f2016-10-19 09:50:22 -07003293 int type;
3294 btif_get_device_type(bd_addr->address, &type);
Ajay Panicker0ef9e062016-03-10 15:30:16 -08003295
Myles Watson6bd442f2016-10-19 09:50:22 -07003296 device_type_t device_type;
3297 switch (type) {
3298 case BT_DEVICE_TYPE_BREDR:
3299 device_type = DEVICE_TYPE_BREDR;
3300 break;
3301 case BT_DEVICE_TYPE_BLE:
3302 device_type = DEVICE_TYPE_LE;
3303 break;
3304 case BT_DEVICE_TYPE_DUMO:
3305 device_type = DEVICE_TYPE_DUMO;
3306 break;
3307 default:
3308 device_type = DEVICE_TYPE_UNKNOWN;
3309 break;
3310 }
Ajay Panicker0ef9e062016-03-10 15:30:16 -08003311
Myles Watson6bd442f2016-10-19 09:50:22 -07003312 uint32_t cod = get_cod(bd_addr);
3313 uint64_t ts =
3314 event->timestamp.tv_sec * 1000 + event->timestamp.tv_nsec / 1000000;
3315 metrics_pair_event(0, ts, cod, device_type);
Ajay Panickerc138fe42016-02-05 16:50:20 -08003316}
3317
3318void btif_debug_bond_event_dump(int fd) {
Myles Watson6bd442f2016-10-19 09:50:22 -07003319 std::unique_lock<std::mutex> lock(bond_event_lock);
3320 dprintf(fd, "\nBond Events: \n");
3321 dprintf(fd, " Total Number of events: %zu\n", btif_num_bond_events);
3322 if (btif_num_bond_events > 0)
3323 dprintf(fd,
3324 " Time BD_ADDR Function State\n");
Ajay Panickerc138fe42016-02-05 16:50:20 -08003325
Myles Watson6bd442f2016-10-19 09:50:22 -07003326 for (size_t i = btif_events_start_index; i != btif_events_end_index;
3327 i = (i + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1)) {
3328 btif_bond_event_t* event = &btif_dm_bond_events[i];
Ajay Panickerc138fe42016-02-05 16:50:20 -08003329
Myles Watson6bd442f2016-10-19 09:50:22 -07003330 char eventtime[20];
3331 char temptime[20];
3332 struct tm* tstamp = localtime(&event->timestamp.tv_sec);
3333 strftime(temptime, sizeof(temptime), "%H:%M:%S", tstamp);
3334 snprintf(eventtime, sizeof(eventtime), "%s.%03ld", temptime,
3335 event->timestamp.tv_nsec / 1000000);
Ajay Panickerc138fe42016-02-05 16:50:20 -08003336
Myles Watson6bd442f2016-10-19 09:50:22 -07003337 char bdaddr[18];
3338 bdaddr_to_string(&event->bd_addr, bdaddr, sizeof(bdaddr));
Ajay Panickerc138fe42016-02-05 16:50:20 -08003339
Myles Watson6bd442f2016-10-19 09:50:22 -07003340 const char* func_name;
3341 switch (event->function) {
3342 case BTIF_DM_FUNC_CREATE_BOND:
3343 func_name = "btif_dm_create_bond";
3344 break;
3345 case BTIF_DM_FUNC_REMOVE_BOND:
3346 func_name = "btif_dm_remove_bond";
3347 break;
3348 case BTIF_DM_FUNC_BOND_STATE_CHANGED:
3349 func_name = "bond_state_changed ";
3350 break;
3351 default:
3352 func_name = "Invalid value ";
3353 break;
Ajay Panickerc138fe42016-02-05 16:50:20 -08003354 }
Myles Watson6bd442f2016-10-19 09:50:22 -07003355
3356 const char* bond_state;
3357 switch (event->state) {
3358 case BT_BOND_STATE_NONE:
3359 bond_state = "BOND_STATE_NONE";
3360 break;
3361 case BT_BOND_STATE_BONDING:
3362 bond_state = "BOND_STATE_BONDING";
3363 break;
3364 case BT_BOND_STATE_BONDED:
3365 bond_state = "BOND_STATE_BONDED";
3366 break;
3367 default:
3368 bond_state = "Invalid bond state";
3369 break;
3370 }
3371 dprintf(fd, " %s %s %s %s\n", eventtime, bdaddr, func_name, bond_state);
3372 }
Ajay Panickerc138fe42016-02-05 16:50:20 -08003373}