1 /******************************************************************************
2 *
3 * Copyright (C) 2009-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /*******************************************************************************
20 *
21 * Filename: btif_dm.c
22 *
23 * Description: Contains Device Management (DM) related functionality
24 *
25 *
26 ******************************************************************************/
27
28 #define LOG_TAG "bt_btif_dm"
29
30 #include "btif_dm.h"
31
32 #include <base/bind.h>
33 #include <base/logging.h>
34 #include <signal.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/types.h>
39 #include <time.h>
40 #include <unistd.h>
41
42 #include <mutex>
43
44 #include <hardware/bluetooth.h>
45
46 #include "advertise_data_parser.h"
47 #include "bt_common.h"
48 #include "bta_closure_api.h"
49 #include "bta_gatt_api.h"
50 #include "btif_api.h"
51 #include "btif_config.h"
52 #include "btif_dm.h"
53 #include "btif_hd.h"
54 #include "btif_hh.h"
55 #include "btif_sdp.h"
56 #include "btif_storage.h"
57 #include "btif_util.h"
58 #include "btu.h"
59 #include "device/include/controller.h"
60 #include "device/include/interop.h"
61 #include "include/stack_config.h"
62 #include "osi/include/allocator.h"
63 #include "osi/include/log.h"
64 #include "osi/include/metrics.h"
65 #include "osi/include/osi.h"
66 #include "osi/include/properties.h"
67 #include "stack/btm/btm_int.h"
68 #include "stack_config.h"
69
70 /******************************************************************************
71 * Constants & Macros
72 *****************************************************************************/
73
74 #define COD_MASK 0x07FF
75
76 #define COD_UNCLASSIFIED ((0x1F) << 8)
77 #define COD_HID_KEYBOARD 0x0540
78 #define COD_HID_POINTING 0x0580
79 #define COD_HID_COMBO 0x05C0
80 #define COD_HID_MAJOR 0x0500
81 #define COD_HID_MASK 0x0700
82 #define COD_AV_HEADSETS 0x0404
83 #define COD_AV_HANDSFREE 0x0408
84 #define COD_AV_HEADPHONES 0x0418
85 #define COD_AV_PORTABLE_AUDIO 0x041C
86 #define COD_AV_HIFI_AUDIO 0x0428
87
88 #define BTIF_DM_DEFAULT_INQ_MAX_RESULTS 0
89 #define BTIF_DM_DEFAULT_INQ_MAX_DURATION 10
90 #define BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING 2
91
92 #define NUM_TIMEOUT_RETRIES 5
93
94 #define PROPERTY_PRODUCT_MODEL "ro.product.model"
95 #define DEFAULT_LOCAL_NAME_MAX 31
96 #if (DEFAULT_LOCAL_NAME_MAX > BTM_MAX_LOC_BD_NAME_LEN)
97 #error "default btif local name size exceeds stack supported length"
98 #endif
99
100 #if (BTA_HOST_INTERLEAVE_SEARCH == TRUE)
101 #define BTIF_DM_INTERLEAVE_DURATION_BR_ONE 2
102 #define BTIF_DM_INTERLEAVE_DURATION_LE_ONE 2
103 #define BTIF_DM_INTERLEAVE_DURATION_BR_TWO 3
104 #define BTIF_DM_INTERLEAVE_DURATION_LE_TWO 4
105 #endif
106
107 #define ENCRYPTED_BREDR 2
108 #define ENCRYPTED_LE 4
109
110 typedef struct {
111 bt_bond_state_t state;
112 RawAddress static_bdaddr;
113 RawAddress bd_addr;
114 tBTM_BOND_TYPE bond_type;
115 uint8_t pin_code_len;
116 uint8_t is_ssp;
117 uint8_t auth_req;
118 uint8_t io_cap;
119 uint8_t autopair_attempts;
120 uint8_t timeout_retries;
121 uint8_t is_local_initiated;
122 uint8_t sdp_attempts;
123 bool is_le_only;
124 bool is_le_nc; /* LE Numeric comparison */
125 btif_dm_ble_cb_t ble;
126 } btif_dm_pairing_cb_t;
127
128 typedef struct {
129 uint8_t ir[BT_OCTET16_LEN];
130 uint8_t irk[BT_OCTET16_LEN];
131 uint8_t dhk[BT_OCTET16_LEN];
132 } btif_dm_local_key_id_t;
133
134 typedef struct {
135 bool is_er_rcvd;
136 uint8_t er[BT_OCTET16_LEN];
137 bool is_id_keys_rcvd;
138 btif_dm_local_key_id_t id_keys; /* ID kyes */
139
140 } btif_dm_local_key_cb_t;
141
142 typedef struct {
143 RawAddress bd_addr;
144 BD_NAME bd_name;
145 } btif_dm_remote_name_t;
146
147 /* this structure holds optional OOB data for remote device */
148 typedef struct {
149 RawAddress bdaddr; /* peer bdaddr */
150 bt_out_of_band_data_t oob_data;
151 } btif_dm_oob_cb_t;
152
153 typedef struct {
154 RawAddress bdaddr;
155 uint8_t transport; /* 0=Unknown, 1=BR/EDR, 2=LE */
156 } btif_dm_create_bond_cb_t;
157
158 typedef struct {
159 uint8_t status;
160 uint8_t ctrl_state;
161 uint64_t tx_time;
162 uint64_t rx_time;
163 uint64_t idle_time;
164 uint64_t energy_used;
165 } btif_activity_energy_info_cb_t;
166
167 typedef struct { unsigned int manufact_id; } skip_sdp_entry_t;
168
169 typedef enum {
170 BTIF_DM_FUNC_CREATE_BOND,
171 BTIF_DM_FUNC_CANCEL_BOND,
172 BTIF_DM_FUNC_REMOVE_BOND,
173 BTIF_DM_FUNC_BOND_STATE_CHANGED,
174 } bt_bond_function_t;
175
176 typedef struct {
177 RawAddress bd_addr;
178 bt_bond_function_t function;
179 bt_bond_state_t state;
180 struct timespec timestamp;
181 } btif_bond_event_t;
182
183 #define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id))
184
185 #define UUID_HUMAN_INTERFACE_DEVICE "00001124-0000-1000-8000-00805f9b34fb"
186 #define UUID_EMPTY "00000000-0000-0000-0000-000000000000"
187
188 #define MAX_BTIF_BOND_EVENT_ENTRIES 15
189
190 static skip_sdp_entry_t sdp_blacklist[] = {{76}}; // Apple Mouse and Keyboard
191
192 /* This flag will be true if HCI_Inquiry is in progress */
193 static bool btif_dm_inquiry_in_progress = false;
194
195 /*******************************************************************************
196 * Static variables
197 ******************************************************************************/
198 static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX + 1] = {'\0'};
199 static uid_set_t* uid_set = NULL;
200
201 /* A circular array to keep track of the most recent bond events */
202 static btif_bond_event_t btif_dm_bond_events[MAX_BTIF_BOND_EVENT_ENTRIES + 1];
203
204 static std::mutex bond_event_lock;
205
206 /* |btif_num_bond_events| keeps track of the total number of events and can be
207 greater than |MAX_BTIF_BOND_EVENT_ENTRIES| */
208 static size_t btif_num_bond_events = 0;
209 static size_t btif_events_start_index = 0;
210 static size_t btif_events_end_index = 0;
211
212 /******************************************************************************
213 * Static functions
214 *****************************************************************************/
215 static btif_dm_pairing_cb_t pairing_cb;
216 static btif_dm_oob_cb_t oob_cb;
217 static void btif_dm_generic_evt(uint16_t event, char* p_param);
218 static void btif_dm_cb_create_bond(const RawAddress& bd_addr,
219 tBTA_TRANSPORT transport);
220 static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME* p_remote_name);
221 static void btif_update_remote_properties(const RawAddress& bd_addr,
222 BD_NAME bd_name, DEV_CLASS dev_class,
223 tBT_DEVICE_TYPE dev_type);
224 static btif_dm_local_key_cb_t ble_local_key_cb;
225 static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF* p_ssp_key_notif);
226 static void btif_dm_ble_auth_cmpl_evt(tBTA_DM_AUTH_CMPL* p_auth_cmpl);
227 static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ* p_pin_req);
228 static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF* p_notif_req);
229 static void btif_dm_ble_oob_req_evt(tBTA_DM_SP_RMT_OOB* req_oob_type);
230 static void btif_dm_ble_sc_oob_req_evt(tBTA_DM_SP_RMT_OOB* req_oob_type);
231
232 static void bte_scan_filt_param_cfg_evt(uint8_t action_type, uint8_t avbl_space,
233 uint8_t ref_value, uint8_t status);
234
235 static char* btif_get_default_local_name();
236
237 static void btif_stats_add_bond_event(const RawAddress& bd_addr,
238 bt_bond_function_t function,
239 bt_bond_state_t state);
240
241 /******************************************************************************
242 * Externs
243 *****************************************************************************/
244 extern bt_status_t btif_hf_execute_service(bool b_enable);
245 extern bt_status_t btif_av_execute_service(bool b_enable);
246 extern bt_status_t btif_av_sink_execute_service(bool b_enable);
247 extern bt_status_t btif_hh_execute_service(bool b_enable);
248 extern bt_status_t btif_hf_client_execute_service(bool b_enable);
249 extern bt_status_t btif_sdp_execute_service(bool b_enable);
250 extern int btif_hh_connect(const RawAddress* bd_addr);
251 extern void bta_gatt_convert_uuid16_to_uuid128(uint8_t uuid_128[LEN_UUID_128],
252 uint16_t uuid_16);
253 extern void btif_av_move_idle(RawAddress bd_addr);
254 extern bt_status_t btif_hd_execute_service(bool b_enable);
255
256 /******************************************************************************
257 * Functions
258 *****************************************************************************/
259
is_empty_128bit(uint8_t * data)260 static bool is_empty_128bit(uint8_t* data) {
261 static const uint8_t zero[16] = {0};
262 return !memcmp(zero, data, sizeof(zero));
263 }
264
is_bonding_or_sdp()265 static bool is_bonding_or_sdp() {
266 return pairing_cb.state == BT_BOND_STATE_BONDING ||
267 (pairing_cb.state == BT_BOND_STATE_BONDED && pairing_cb.sdp_attempts);
268 }
269
btif_dm_data_copy(uint16_t event,char * dst,char * src)270 static void btif_dm_data_copy(uint16_t event, char* dst, char* src) {
271 tBTA_DM_SEC* dst_dm_sec = (tBTA_DM_SEC*)dst;
272 tBTA_DM_SEC* src_dm_sec = (tBTA_DM_SEC*)src;
273
274 if (!src_dm_sec) return;
275
276 CHECK(dst_dm_sec);
277 maybe_non_aligned_memcpy(dst_dm_sec, src_dm_sec, sizeof(*src_dm_sec));
278
279 if (event == BTA_DM_BLE_KEY_EVT) {
280 dst_dm_sec->ble_key.p_key_value =
281 (tBTM_LE_KEY_VALUE*)osi_malloc(sizeof(tBTM_LE_KEY_VALUE));
282 CHECK(src_dm_sec->ble_key.p_key_value);
283 memcpy(dst_dm_sec->ble_key.p_key_value, src_dm_sec->ble_key.p_key_value,
284 sizeof(tBTM_LE_KEY_VALUE));
285 }
286 }
287
btif_dm_data_free(uint16_t event,tBTA_DM_SEC * dm_sec)288 static void btif_dm_data_free(uint16_t event, tBTA_DM_SEC* dm_sec) {
289 if (event == BTA_DM_BLE_KEY_EVT)
290 osi_free_and_reset((void**)&dm_sec->ble_key.p_key_value);
291 }
292
btif_dm_init(uid_set_t * set)293 void btif_dm_init(uid_set_t* set) { uid_set = set; }
294
btif_dm_cleanup(void)295 void btif_dm_cleanup(void) {
296 if (uid_set) {
297 uid_set_destroy(uid_set);
298 uid_set = NULL;
299 }
300 }
301
btif_in_execute_service_request(tBTA_SERVICE_ID service_id,bool b_enable)302 bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
303 bool b_enable) {
304 BTIF_TRACE_DEBUG("%s service_id: %d", __func__, service_id);
305 /* Check the service_ID and invoke the profile's BT state changed API */
306 switch (service_id) {
307 case BTA_HFP_SERVICE_ID:
308 case BTA_HSP_SERVICE_ID: {
309 btif_hf_execute_service(b_enable);
310 } break;
311 case BTA_A2DP_SOURCE_SERVICE_ID: {
312 btif_av_execute_service(b_enable);
313 } break;
314 case BTA_A2DP_SINK_SERVICE_ID: {
315 btif_av_sink_execute_service(b_enable);
316 } break;
317 case BTA_HID_SERVICE_ID: {
318 btif_hh_execute_service(b_enable);
319 } break;
320 case BTA_HFP_HS_SERVICE_ID: {
321 btif_hf_client_execute_service(b_enable);
322 } break;
323 case BTA_SDP_SERVICE_ID: {
324 btif_sdp_execute_service(b_enable);
325 } break;
326 case BTA_HIDD_SERVICE_ID: {
327 btif_hd_execute_service(b_enable);
328 } break;
329 default:
330 BTIF_TRACE_ERROR("%s: Unknown service %d being %s", __func__, service_id,
331 (b_enable) ? "enabled" : "disabled");
332 return BT_STATUS_FAIL;
333 }
334 return BT_STATUS_SUCCESS;
335 }
336
337 /*******************************************************************************
338 *
339 * Function check_eir_remote_name
340 *
341 * Description Check if remote name is in the EIR data
342 *
343 * Returns true if remote name found
344 * Populate p_remote_name, if provided and remote name found
345 *
346 ******************************************************************************/
check_eir_remote_name(tBTA_DM_SEARCH * p_search_data,uint8_t * p_remote_name,uint8_t * p_remote_name_len)347 static bool check_eir_remote_name(tBTA_DM_SEARCH* p_search_data,
348 uint8_t* p_remote_name,
349 uint8_t* p_remote_name_len) {
350 const uint8_t* p_eir_remote_name = NULL;
351 uint8_t remote_name_len = 0;
352
353 /* Check EIR for remote name and services */
354 if (p_search_data->inq_res.p_eir) {
355 p_eir_remote_name = AdvertiseDataParser::GetFieldByType(
356 p_search_data->inq_res.p_eir, p_search_data->inq_res.eir_len,
357 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
358 if (!p_eir_remote_name) {
359 p_eir_remote_name = AdvertiseDataParser::GetFieldByType(
360 p_search_data->inq_res.p_eir, p_search_data->inq_res.eir_len,
361 BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
362 }
363
364 if (p_eir_remote_name) {
365 if (remote_name_len > BD_NAME_LEN) remote_name_len = BD_NAME_LEN;
366
367 if (p_remote_name && p_remote_name_len) {
368 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
369 *(p_remote_name + remote_name_len) = 0;
370 *p_remote_name_len = remote_name_len;
371 }
372
373 return true;
374 }
375 }
376
377 return false;
378 }
379
380 /*******************************************************************************
381 *
382 * Function check_cached_remote_name
383 *
384 * Description Check if remote name is in the NVRAM cache
385 *
386 * Returns true if remote name found
387 * Populate p_remote_name, if provided and remote name found
388 *
389 ******************************************************************************/
check_cached_remote_name(tBTA_DM_SEARCH * p_search_data,uint8_t * p_remote_name,uint8_t * p_remote_name_len)390 static bool check_cached_remote_name(tBTA_DM_SEARCH* p_search_data,
391 uint8_t* p_remote_name,
392 uint8_t* p_remote_name_len) {
393 bt_bdname_t bdname;
394 bt_property_t prop_name;
395
396 /* check if we already have it in our btif_storage cache */
397
398 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
399 sizeof(bt_bdname_t), &bdname);
400 if (btif_storage_get_remote_device_property(
401 &p_search_data->inq_res.bd_addr, &prop_name) == BT_STATUS_SUCCESS) {
402 if (p_remote_name && p_remote_name_len) {
403 strcpy((char*)p_remote_name, (char*)bdname.name);
404 *p_remote_name_len = strlen((char*)p_remote_name);
405 }
406 return true;
407 }
408
409 return false;
410 }
411
get_cod(const RawAddress * remote_bdaddr)412 static uint32_t get_cod(const RawAddress* remote_bdaddr) {
413 uint32_t remote_cod;
414 bt_property_t prop_name;
415
416 /* check if we already have it in our btif_storage cache */
417 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
418 sizeof(uint32_t), &remote_cod);
419 if (btif_storage_get_remote_device_property(
420 (RawAddress*)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS) {
421 LOG_INFO(LOG_TAG, "%s remote_cod = 0x%08x", __func__, remote_cod);
422 return remote_cod & COD_MASK;
423 }
424
425 return 0;
426 }
427
check_cod(const RawAddress * remote_bdaddr,uint32_t cod)428 bool check_cod(const RawAddress* remote_bdaddr, uint32_t cod) {
429 return get_cod(remote_bdaddr) == cod;
430 }
431
check_cod_hid(const RawAddress * remote_bdaddr)432 bool check_cod_hid(const RawAddress* remote_bdaddr) {
433 return (get_cod(remote_bdaddr) & COD_HID_MASK) == COD_HID_MAJOR;
434 }
435
check_hid_le(const RawAddress * remote_bdaddr)436 bool check_hid_le(const RawAddress* remote_bdaddr) {
437 uint32_t remote_dev_type;
438 bt_property_t prop_name;
439
440 /* check if we already have it in our btif_storage cache */
441 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_TYPE_OF_DEVICE,
442 sizeof(uint32_t), &remote_dev_type);
443 if (btif_storage_get_remote_device_property(
444 (RawAddress*)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS) {
445 if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE) {
446 if (btif_config_exist(remote_bdaddr->ToString().c_str(), "HidAppId"))
447 return true;
448 }
449 }
450 return false;
451 }
452
453 /*****************************************************************************
454 *
455 * Function check_sdp_bl
456 *
457 * Description Checks if a given device is blacklisted to skip sdp
458 *
459 * Parameters skip_sdp_entry
460 *
461 * Returns true if the device is present in blacklist, else false
462 *
463 ******************************************************************************/
check_sdp_bl(const RawAddress * remote_bdaddr)464 bool check_sdp_bl(const RawAddress* remote_bdaddr) {
465 uint16_t manufacturer = 0;
466 uint8_t lmp_ver = 0;
467 uint16_t lmp_subver = 0;
468 bt_property_t prop_name;
469 bt_remote_version_t info;
470
471 if (remote_bdaddr == NULL) return false;
472
473 /* fetch additional info about remote device used in iop query */
474 BTM_ReadRemoteVersion(*remote_bdaddr, &lmp_ver, &manufacturer, &lmp_subver);
475
476 /* if not available yet, try fetching from config database */
477 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO,
478 sizeof(bt_remote_version_t), &info);
479
480 if (btif_storage_get_remote_device_property(remote_bdaddr, &prop_name) !=
481 BT_STATUS_SUCCESS) {
482 return false;
483 }
484 manufacturer = info.manufacturer;
485
486 for (unsigned int i = 0; i < ARRAY_SIZE(sdp_blacklist); i++) {
487 if (manufacturer == sdp_blacklist[i].manufact_id) return true;
488 }
489 return false;
490 }
491
bond_state_changed(bt_status_t status,const RawAddress & bd_addr,bt_bond_state_t state)492 static void bond_state_changed(bt_status_t status, const RawAddress& bd_addr,
493 bt_bond_state_t state) {
494 btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_BOND_STATE_CHANGED, state);
495
496 if ((pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING)) {
497 // Cross key pairing so send callback for static address
498 if (!pairing_cb.static_bdaddr.IsEmpty()) {
499 auto tmp = bd_addr;
500 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, &tmp, state);
501 }
502 return;
503 }
504
505 if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY) state = BT_BOND_STATE_NONE;
506
507 BTIF_TRACE_DEBUG("%s: state=%d, prev_state=%d, sdp_attempts = %d", __func__,
508 state, pairing_cb.state, pairing_cb.sdp_attempts);
509
510 auto tmp = bd_addr;
511 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, &tmp, state);
512
513 if (state == BT_BOND_STATE_BONDING ||
514 (state == BT_BOND_STATE_BONDED && pairing_cb.sdp_attempts > 0)) {
515 // Save state for the device is bonding or SDP.
516 pairing_cb.state = state;
517 pairing_cb.bd_addr = bd_addr;
518 } else {
519 pairing_cb = {};
520 }
521 }
522
523 /* store remote version in bt config to always have access
524 to it post pairing*/
btif_update_remote_version_property(RawAddress * p_bd)525 static void btif_update_remote_version_property(RawAddress* p_bd) {
526 bt_property_t property;
527 uint8_t lmp_ver = 0;
528 uint16_t lmp_subver = 0;
529 uint16_t mfct_set = 0;
530 tBTM_STATUS btm_status;
531 bt_remote_version_t info;
532 bt_status_t status;
533
534 btm_status = BTM_ReadRemoteVersion(*p_bd, &lmp_ver, &mfct_set, &lmp_subver);
535
536 LOG_DEBUG(LOG_TAG, "remote version info [%s]: %x, %x, %x",
537 p_bd->ToString().c_str(), lmp_ver, mfct_set, lmp_subver);
538
539 if (btm_status == BTM_SUCCESS) {
540 // Always update cache to ensure we have availability whenever BTM API is
541 // not populated
542 info.manufacturer = mfct_set;
543 info.sub_ver = lmp_subver;
544 info.version = lmp_ver;
545 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_REMOTE_VERSION_INFO,
546 sizeof(bt_remote_version_t), &info);
547 status = btif_storage_set_remote_device_property(p_bd, &property);
548 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version",
549 status);
550 }
551 }
552
btif_update_remote_properties(const RawAddress & bdaddr,BD_NAME bd_name,DEV_CLASS dev_class,tBT_DEVICE_TYPE device_type)553 static void btif_update_remote_properties(const RawAddress& bdaddr,
554 BD_NAME bd_name, DEV_CLASS dev_class,
555 tBT_DEVICE_TYPE device_type) {
556 int num_properties = 0;
557 bt_property_t properties[3];
558 bt_status_t status;
559 uint32_t cod;
560 bt_device_type_t dev_type;
561
562 memset(properties, 0, sizeof(properties));
563
564 /* remote name */
565 if (strlen((const char*)bd_name)) {
566 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], BT_PROPERTY_BDNAME,
567 strlen((char*)bd_name), bd_name);
568 status = btif_storage_set_remote_device_property(
569 &bdaddr, &properties[num_properties]);
570 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name",
571 status);
572 num_properties++;
573 }
574
575 /* class of device */
576 cod = devclass2uint(dev_class);
577 BTIF_TRACE_DEBUG("%s cod is 0x%06x", __func__, cod);
578 if (cod == 0) {
579 /* Try to retrieve cod from storage */
580 BTIF_TRACE_DEBUG("%s cod is 0, checking cod from storage", __func__);
581 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
582 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
583 status = btif_storage_get_remote_device_property(
584 &bdaddr, &properties[num_properties]);
585 BTIF_TRACE_DEBUG("%s cod retrieved from storage is 0x%06x", __func__, cod);
586 if (cod == 0) {
587 BTIF_TRACE_DEBUG("%s cod is again 0, set as unclassified", __func__);
588 cod = COD_UNCLASSIFIED;
589 }
590 }
591
592 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
593 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
594 status = btif_storage_set_remote_device_property(&bdaddr,
595 &properties[num_properties]);
596 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class",
597 status);
598 num_properties++;
599
600 /* device type */
601 bt_property_t prop_name;
602 uint8_t remote_dev_type;
603 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_TYPE_OF_DEVICE,
604 sizeof(uint8_t), &remote_dev_type);
605 if (btif_storage_get_remote_device_property(&bdaddr, &prop_name) ==
606 BT_STATUS_SUCCESS)
607 dev_type = (bt_device_type_t)(remote_dev_type | device_type);
608 else
609 dev_type = (bt_device_type_t)device_type;
610
611 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
612 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type),
613 &dev_type);
614 status = btif_storage_set_remote_device_property(&bdaddr,
615 &properties[num_properties]);
616 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type",
617 status);
618 num_properties++;
619
620 auto tmp = bdaddr;
621 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, status, &tmp,
622 num_properties, properties);
623 }
624
625 /*******************************************************************************
626 *
627 * Function btif_dm_cb_hid_remote_name
628 *
629 * Description Remote name callback for HID device. Called in btif context
630 * Special handling for HID devices
631 *
632 * Returns void
633 *
634 ******************************************************************************/
btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME * p_remote_name)635 static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME* p_remote_name) {
636 BTIF_TRACE_DEBUG("%s: status=%d pairing_cb.state=%d", __func__,
637 p_remote_name->status, pairing_cb.state);
638 if (pairing_cb.state == BT_BOND_STATE_BONDING) {
639 if (p_remote_name->status == BTM_SUCCESS) {
640 bond_state_changed(BT_STATUS_SUCCESS, pairing_cb.bd_addr,
641 BT_BOND_STATE_BONDED);
642 } else
643 bond_state_changed(BT_STATUS_FAIL, pairing_cb.bd_addr,
644 BT_BOND_STATE_NONE);
645 }
646 }
647
648 /*******************************************************************************
649 *
650 * Function btif_dm_cb_create_bond
651 *
652 * Description Create bond initiated from the BTIF thread context
653 * Special handling for HID devices
654 *
655 * Returns void
656 *
657 ******************************************************************************/
btif_dm_cb_create_bond(const RawAddress & bd_addr,tBTA_TRANSPORT transport)658 static void btif_dm_cb_create_bond(const RawAddress& bd_addr,
659 tBTA_TRANSPORT transport) {
660 bool is_hid = check_cod(&bd_addr, COD_HID_POINTING);
661 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
662
663 int device_type;
664 int addr_type;
665 std::string addrstr = bd_addr.ToString();
666 const char* bdstr = addrstr.c_str();
667 if (transport == BT_TRANSPORT_LE) {
668 if (!btif_config_get_int(bdstr, "DevType", &device_type)) {
669 btif_config_set_int(bdstr, "DevType", BT_DEVICE_TYPE_BLE);
670 }
671 if (btif_storage_get_remote_addr_type(&bd_addr, &addr_type) !=
672 BT_STATUS_SUCCESS) {
673 // Try to read address type. OOB pairing might have set it earlier, but
674 // didn't store it, it defaults to BLE_ADDR_PUBLIC
675 uint8_t tmp_dev_type;
676 uint8_t tmp_addr_type;
677 BTM_ReadDevInfo(bd_addr, &tmp_dev_type, &tmp_addr_type);
678 addr_type = tmp_addr_type;
679
680 btif_storage_set_remote_addr_type(&bd_addr, addr_type);
681 }
682 }
683 if ((btif_config_get_int(bdstr, "DevType", &device_type) &&
684 (btif_storage_get_remote_addr_type(&bd_addr, &addr_type) ==
685 BT_STATUS_SUCCESS) &&
686 (device_type & BT_DEVICE_TYPE_BLE) == BT_DEVICE_TYPE_BLE) ||
687 (transport == BT_TRANSPORT_LE)) {
688 BTA_DmAddBleDevice(bd_addr, addr_type, device_type);
689 }
690
691 if (is_hid && (device_type & BT_DEVICE_TYPE_BLE) == 0) {
692 bt_status_t status;
693 status = (bt_status_t)btif_hh_connect(&bd_addr);
694 if (status != BT_STATUS_SUCCESS)
695 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
696 } else {
697 BTA_DmBondByTransport(bd_addr, transport);
698 }
699 /* Track originator of bond creation */
700 pairing_cb.is_local_initiated = true;
701 }
702
703 /*******************************************************************************
704 *
705 * Function btif_dm_cb_remove_bond
706 *
707 * Description remove bond initiated from the BTIF thread context
708 * Special handling for HID devices
709 *
710 * Returns void
711 *
712 ******************************************************************************/
btif_dm_cb_remove_bond(const RawAddress * bd_addr)713 void btif_dm_cb_remove_bond(const RawAddress* bd_addr) {
714 /*special handling for HID devices */
715 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if
716 there
717 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
718 #if (BTA_HH_INCLUDED == TRUE)
719 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
720 #endif
721 {
722 BTIF_TRACE_DEBUG("%s: Removing HH device", __func__);
723 BTA_DmRemoveDevice(*bd_addr);
724 }
725 }
726
727 /*******************************************************************************
728 *
729 * Function btif_dm_get_connection_state
730 *
731 * Description Returns whether the remote device is currently connected
732 * and whether encryption is active for the connection
733 *
734 * Returns 0 if not connected; 1 if connected and > 1 if connection is
735 * encrypted
736 *
737 ******************************************************************************/
btif_dm_get_connection_state(const RawAddress * bd_addr)738 uint16_t btif_dm_get_connection_state(const RawAddress* bd_addr) {
739 uint16_t rc = BTA_DmGetConnectionState(*bd_addr);
740
741 if (rc != 0) {
742 uint8_t flags = 0;
743
744 BTM_GetSecurityFlagsByTransport(*bd_addr, &flags, BT_TRANSPORT_BR_EDR);
745 BTIF_TRACE_DEBUG("%s: security flags (BR/EDR)=0x%02x", __func__, flags);
746 if (flags & BTM_SEC_FLAG_ENCRYPTED) rc |= ENCRYPTED_BREDR;
747
748 BTM_GetSecurityFlagsByTransport(*bd_addr, &flags, BT_TRANSPORT_LE);
749 BTIF_TRACE_DEBUG("%s: security flags (LE)=0x%02x", __func__, flags);
750 if (flags & BTM_SEC_FLAG_ENCRYPTED) rc |= ENCRYPTED_LE;
751 }
752
753 return rc;
754 }
755
756 /*******************************************************************************
757 *
758 * Function search_devices_copy_cb
759 *
760 * Description Deep copy callback for search devices event
761 *
762 * Returns void
763 *
764 ******************************************************************************/
search_devices_copy_cb(uint16_t event,char * p_dest,char * p_src)765 static void search_devices_copy_cb(uint16_t event, char* p_dest, char* p_src) {
766 tBTA_DM_SEARCH* p_dest_data = (tBTA_DM_SEARCH*)p_dest;
767 tBTA_DM_SEARCH* p_src_data = (tBTA_DM_SEARCH*)p_src;
768
769 if (!p_src) return;
770
771 BTIF_TRACE_DEBUG("%s: event=%s", __func__, dump_dm_search_event(event));
772 maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data));
773 switch (event) {
774 case BTA_DM_INQ_RES_EVT: {
775 if (p_src_data->inq_res.p_eir) {
776 p_dest_data->inq_res.p_eir =
777 (uint8_t*)(p_dest + sizeof(tBTA_DM_SEARCH));
778 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir,
779 p_src_data->inq_res.eir_len);
780 p_dest_data->inq_res.eir_len = p_src_data->inq_res.eir_len;
781 }
782 } break;
783
784 case BTA_DM_DISC_RES_EVT: {
785 if (p_src_data->disc_res.raw_data_size &&
786 p_src_data->disc_res.p_raw_data) {
787 p_dest_data->disc_res.p_raw_data =
788 (uint8_t*)(p_dest + sizeof(tBTA_DM_SEARCH));
789 memcpy(p_dest_data->disc_res.p_raw_data,
790 p_src_data->disc_res.p_raw_data,
791 p_src_data->disc_res.raw_data_size);
792 }
793 } break;
794 }
795 }
796
search_services_copy_cb(uint16_t event,char * p_dest,char * p_src)797 static void search_services_copy_cb(uint16_t event, char* p_dest, char* p_src) {
798 tBTA_DM_SEARCH* p_dest_data = (tBTA_DM_SEARCH*)p_dest;
799 tBTA_DM_SEARCH* p_src_data = (tBTA_DM_SEARCH*)p_src;
800
801 if (!p_src) return;
802 maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data));
803 switch (event) {
804 case BTA_DM_DISC_RES_EVT: {
805 if (p_src_data->disc_res.result == BTA_SUCCESS) {
806 if (p_src_data->disc_res.num_uuids > 0) {
807 p_dest_data->disc_res.p_uuid_list =
808 (uint8_t*)(p_dest + sizeof(tBTA_DM_SEARCH));
809 memcpy(p_dest_data->disc_res.p_uuid_list,
810 p_src_data->disc_res.p_uuid_list,
811 p_src_data->disc_res.num_uuids * MAX_UUID_SIZE);
812 osi_free_and_reset((void**)&p_src_data->disc_res.p_uuid_list);
813 }
814 osi_free_and_reset((void**)&p_src_data->disc_res.p_raw_data);
815 }
816 } break;
817 }
818 }
819 /******************************************************************************
820 *
821 * BTIF DM callback events
822 *
823 ****************************************************************************/
824
825 /*******************************************************************************
826 *
827 * Function btif_dm_pin_req_evt
828 *
829 * Description Executes pin request event in btif context
830 *
831 * Returns void
832 *
833 ******************************************************************************/
btif_dm_pin_req_evt(tBTA_DM_PIN_REQ * p_pin_req)834 static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ* p_pin_req) {
835 bt_bdname_t bd_name;
836 uint32_t cod;
837 bt_pin_code_t pin_code;
838 int dev_type;
839
840 /* Remote properties update */
841 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type)) {
842 dev_type = BT_DEVICE_TYPE_BREDR;
843 }
844 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
845 p_pin_req->dev_class,
846 (tBT_DEVICE_TYPE)dev_type);
847
848 const RawAddress& bd_addr = p_pin_req->bd_addr;
849 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
850
851 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
852
853 cod = devclass2uint(p_pin_req->dev_class);
854
855 if (cod == 0) {
856 BTIF_TRACE_DEBUG("%s cod is 0, set as unclassified", __func__);
857 cod = COD_UNCLASSIFIED;
858 }
859
860 /* check for auto pair possiblity only if bond was initiated by local device
861 */
862 if (pairing_cb.is_local_initiated && (p_pin_req->min_16_digit == false)) {
863 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
864 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
865 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
866 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
867 check_cod(&bd_addr, COD_HID_POINTING)) {
868 /* Check if this device can be auto paired */
869 if (!interop_match_addr(INTEROP_DISABLE_AUTO_PAIRING, &bd_addr) &&
870 !interop_match_name(INTEROP_DISABLE_AUTO_PAIRING,
871 (const char*)bd_name.name) &&
872 (pairing_cb.autopair_attempts == 0)) {
873 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __func__);
874 pin_code.pin[0] = 0x30;
875 pin_code.pin[1] = 0x30;
876 pin_code.pin[2] = 0x30;
877 pin_code.pin[3] = 0x30;
878
879 pairing_cb.autopair_attempts++;
880 BTA_DmPinReply(bd_addr, true, 4, pin_code.pin);
881 return;
882 }
883 } else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
884 check_cod(&bd_addr, COD_HID_COMBO)) {
885 if ((interop_match_addr(INTEROP_KEYBOARD_REQUIRES_FIXED_PIN, &bd_addr) ==
886 true) &&
887 (pairing_cb.autopair_attempts == 0)) {
888 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __func__);
889 pin_code.pin[0] = 0x30;
890 pin_code.pin[1] = 0x30;
891 pin_code.pin[2] = 0x30;
892 pin_code.pin[3] = 0x30;
893
894 pairing_cb.autopair_attempts++;
895 BTA_DmPinReply(bd_addr, true, 4, pin_code.pin);
896 return;
897 }
898 }
899 }
900 // TODO: make cback accept const and get rid of tmp!
901 auto tmp = bd_addr;
902 HAL_CBACK(bt_hal_cbacks, pin_request_cb, &tmp, &bd_name, cod,
903 p_pin_req->min_16_digit);
904 }
905
906 /*******************************************************************************
907 *
908 * Function btif_dm_ssp_cfm_req_evt
909 *
910 * Description Executes SSP confirm request event in btif context
911 *
912 * Returns void
913 *
914 ******************************************************************************/
btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ * p_ssp_cfm_req)915 static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ* p_ssp_cfm_req) {
916 bt_bdname_t bd_name;
917 uint32_t cod;
918 bool is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
919 int dev_type;
920
921 BTIF_TRACE_DEBUG("%s", __func__);
922
923 /* Remote properties update */
924 if (!btif_get_device_type(p_ssp_cfm_req->bd_addr, &dev_type)) {
925 dev_type = BT_DEVICE_TYPE_BREDR;
926 }
927 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
928 p_ssp_cfm_req->dev_class,
929 (tBT_DEVICE_TYPE)dev_type);
930
931 RawAddress bd_addr = p_ssp_cfm_req->bd_addr;
932 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
933
934 /* Set the pairing_cb based on the local & remote authentication requirements
935 */
936 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
937
938 /* if just_works and bonding bit is not set treat this as temporary */
939 if (p_ssp_cfm_req->just_works &&
940 !(p_ssp_cfm_req->loc_auth_req & BTM_AUTH_BONDS) &&
941 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
942 !(check_cod((RawAddress*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
943 pairing_cb.bond_type = BOND_TYPE_TEMPORARY;
944 else
945 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
946
947 btm_set_bond_type_dev(p_ssp_cfm_req->bd_addr, pairing_cb.bond_type);
948
949 pairing_cb.is_ssp = true;
950
951 /* If JustWorks auto-accept */
952 if (p_ssp_cfm_req->just_works) {
953 /* Pairing consent for JustWorks NOT needed if:
954 * 1. Incoming temporary pairing is detected
955 */
956 if (is_incoming && pairing_cb.bond_type == BOND_TYPE_TEMPORARY) {
957 BTIF_TRACE_EVENT(
958 "%s: Auto-accept JustWorks pairing for temporary incoming", __func__);
959 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, true, 0);
960 return;
961 }
962 }
963
964 cod = devclass2uint(p_ssp_cfm_req->dev_class);
965
966 if (cod == 0) {
967 LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__);
968 cod = COD_UNCLASSIFIED;
969 }
970
971 pairing_cb.sdp_attempts = 0;
972 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
973 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT
974 : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
975 p_ssp_cfm_req->num_val);
976 }
977
btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF * p_ssp_key_notif)978 static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF* p_ssp_key_notif) {
979 bt_bdname_t bd_name;
980 uint32_t cod;
981 int dev_type;
982
983 BTIF_TRACE_DEBUG("%s", __func__);
984
985 /* Remote properties update */
986 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type)) {
987 dev_type = BT_DEVICE_TYPE_BREDR;
988 }
989 btif_update_remote_properties(
990 p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
991 p_ssp_key_notif->dev_class, (tBT_DEVICE_TYPE)dev_type);
992
993 RawAddress bd_addr = p_ssp_key_notif->bd_addr;
994 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
995
996 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
997 pairing_cb.is_ssp = true;
998 cod = devclass2uint(p_ssp_key_notif->dev_class);
999
1000 if (cod == 0) {
1001 LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__);
1002 cod = COD_UNCLASSIFIED;
1003 }
1004
1005 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
1006 BT_SSP_VARIANT_PASSKEY_NOTIFICATION, p_ssp_key_notif->passkey);
1007 }
1008 /*******************************************************************************
1009 *
1010 * Function btif_dm_auth_cmpl_evt
1011 *
1012 * Description Executes authentication complete event in btif context
1013 *
1014 * Returns void
1015 *
1016 ******************************************************************************/
btif_dm_auth_cmpl_evt(tBTA_DM_AUTH_CMPL * p_auth_cmpl)1017 static void btif_dm_auth_cmpl_evt(tBTA_DM_AUTH_CMPL* p_auth_cmpl) {
1018 /* Save link key, if not temporary */
1019 bt_status_t status = BT_STATUS_FAIL;
1020 bt_bond_state_t state = BT_BOND_STATE_NONE;
1021 bool skip_sdp = false;
1022
1023 BTIF_TRACE_DEBUG("%s: bond state=%d", __func__, pairing_cb.state);
1024
1025 RawAddress bd_addr = p_auth_cmpl->bd_addr;
1026 if ((p_auth_cmpl->success == true) && (p_auth_cmpl->key_present)) {
1027 if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) ||
1028 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
1029 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) ||
1030 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB_P_256) ||
1031 pairing_cb.bond_type == BOND_TYPE_PERSISTENT) {
1032 bt_status_t ret;
1033 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, bond_type=%d",
1034 __func__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1035 ret = btif_storage_add_bonded_device(&bd_addr, p_auth_cmpl->key,
1036 p_auth_cmpl->key_type,
1037 pairing_cb.pin_code_len);
1038 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
1039 } else {
1040 BTIF_TRACE_DEBUG(
1041 "%s: Temporary key. Not storing. key_type=0x%x, bond_type=%d",
1042 __func__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1043 if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY) {
1044 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
1045 __func__);
1046 btif_storage_remove_bonded_device(&bd_addr);
1047 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_NONE);
1048 return;
1049 }
1050 }
1051 }
1052
1053 // We could have received a new link key without going through the pairing
1054 // flow. If so, we don't want to perform SDP or any other operations on the
1055 // authenticated device. Also, make sure that the link key is not derived from
1056 // secure LTK, because we will need to perform SDP in case of link key
1057 // derivation to allow bond state change notification for the BR/EDR transport
1058 // so that the subsequent BR/EDR connections to the remote can use the derived
1059 // link key.
1060 if (p_auth_cmpl->bd_addr != pairing_cb.bd_addr &&
1061 (!pairing_cb.ble.is_penc_key_rcvd)) {
1062 LOG(INFO) << __func__
1063 << " skipping SDP since we did not initiate pairing to "
1064 << p_auth_cmpl->bd_addr;
1065 return;
1066 }
1067
1068 // Skip SDP for certain HID Devices
1069 if (p_auth_cmpl->success) {
1070 btif_storage_set_remote_addr_type(&bd_addr, p_auth_cmpl->addr_type);
1071 btif_update_remote_properties(p_auth_cmpl->bd_addr, p_auth_cmpl->bd_name,
1072 NULL, p_auth_cmpl->dev_type);
1073 pairing_cb.timeout_retries = 0;
1074 status = BT_STATUS_SUCCESS;
1075 state = BT_BOND_STATE_BONDED;
1076 bd_addr = p_auth_cmpl->bd_addr;
1077
1078 if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr)) {
1079 LOG_WARN(LOG_TAG, "%s:skip SDP", __func__);
1080 skip_sdp = true;
1081 }
1082 if (!pairing_cb.is_local_initiated && skip_sdp) {
1083 bond_state_changed(status, bd_addr, state);
1084
1085 LOG_WARN(LOG_TAG, "%s: Incoming HID Connection", __func__);
1086 bt_property_t prop;
1087 RawAddress bd_addr;
1088 bt_uuid_t uuid;
1089 char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE;
1090
1091 string_to_uuid(uuid_str, &uuid);
1092
1093 prop.type = BT_PROPERTY_UUIDS;
1094 prop.val = uuid.uu;
1095 prop.len = MAX_UUID_SIZE;
1096
1097 /* Send the event to the BTIF */
1098 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, BT_STATUS_SUCCESS,
1099 &bd_addr, 1, &prop);
1100 } else {
1101 bool is_crosskey = false;
1102 /* If bonded due to cross-key, save the static address too*/
1103 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
1104 p_auth_cmpl->bd_addr != pairing_cb.bd_addr) {
1105 BTIF_TRACE_DEBUG(
1106 "%s: bonding initiated due to cross key, adding static address",
1107 __func__);
1108 pairing_cb.static_bdaddr = bd_addr;
1109 is_crosskey = true;
1110 }
1111 if (!is_crosskey ||
1112 !(stack_config_get_interface()->get_pts_crosskey_sdp_disable())) {
1113 // Ensure inquiry is stopped before attempting service discovery
1114 btif_dm_cancel_discovery();
1115
1116 /* Trigger SDP on the device */
1117 pairing_cb.sdp_attempts = 1;
1118
1119 if (is_crosskey) {
1120 // If bonding occurred due to cross-key pairing, send bonding callback
1121 // for static address now
1122 LOG_INFO(LOG_TAG,
1123 "%s: send bonding state update for static address %s",
1124 __func__, bd_addr.ToString().c_str());
1125 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
1126 }
1127 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDED);
1128
1129 btif_dm_get_remote_services(bd_addr);
1130 }
1131 }
1132 // Do not call bond_state_changed_cb yet. Wait until remote service
1133 // discovery is complete
1134 } else {
1135 // Map the HCI fail reason to bt status
1136 switch (p_auth_cmpl->fail_reason) {
1137 case HCI_ERR_PAGE_TIMEOUT:
1138 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
1139 if (interop_match_addr(INTEROP_AUTO_RETRY_PAIRING, &bd_addr) &&
1140 pairing_cb.timeout_retries) {
1141 BTIF_TRACE_WARNING("%s() - Pairing timeout; retrying (%d) ...",
1142 __func__, pairing_cb.timeout_retries);
1143 --pairing_cb.timeout_retries;
1144 btif_dm_cb_create_bond(bd_addr, BTA_TRANSPORT_UNKNOWN);
1145 return;
1146 }
1147 /* Fall-through */
1148 case HCI_ERR_CONNECTION_TOUT:
1149 status = BT_STATUS_RMT_DEV_DOWN;
1150 break;
1151
1152 case HCI_ERR_PAIRING_NOT_ALLOWED:
1153 btif_storage_remove_bonded_device(&bd_addr);
1154 status = BT_STATUS_AUTH_REJECTED;
1155 break;
1156
1157 /* map the auth failure codes, so we can retry pairing if necessary */
1158 case HCI_ERR_AUTH_FAILURE:
1159 case HCI_ERR_KEY_MISSING:
1160 btif_storage_remove_bonded_device(&bd_addr);
1161 case HCI_ERR_HOST_REJECT_SECURITY:
1162 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
1163 case HCI_ERR_UNIT_KEY_USED:
1164 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
1165 case HCI_ERR_INSUFFCIENT_SECURITY:
1166 case HCI_ERR_PEER_USER:
1167 case HCI_ERR_UNSPECIFIED:
1168 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d", __func__,
1169 p_auth_cmpl->fail_reason);
1170 if (pairing_cb.autopair_attempts == 1) {
1171 /* Create the Bond once again */
1172 BTIF_TRACE_WARNING("%s() auto pair failed. Reinitiate Bond",
1173 __func__);
1174 btif_dm_cb_create_bond(bd_addr, BTA_TRANSPORT_UNKNOWN);
1175 return;
1176 } else {
1177 /* if autopair attempts are more than 1, or not attempted */
1178 status = BT_STATUS_AUTH_FAILURE;
1179 }
1180 break;
1181
1182 default:
1183 status = BT_STATUS_FAIL;
1184 }
1185 /* Special Handling for HID Devices */
1186 if (check_cod(&bd_addr, COD_HID_POINTING)) {
1187 /* Remove Device as bonded in nvram as authentication failed */
1188 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram",
1189 __func__);
1190 btif_storage_remove_bonded_device(&bd_addr);
1191 }
1192 bond_state_changed(status, bd_addr, state);
1193 }
1194 }
1195
1196 /******************************************************************************
1197 *
1198 * Function btif_dm_search_devices_evt
1199 *
1200 * Description Executes search devices callback events in btif context
1201 *
1202 * Returns void
1203 *
1204 *****************************************************************************/
btif_dm_search_devices_evt(uint16_t event,char * p_param)1205 static void btif_dm_search_devices_evt(uint16_t event, char* p_param) {
1206 tBTA_DM_SEARCH* p_search_data;
1207 BTIF_TRACE_EVENT("%s event=%s", __func__, dump_dm_search_event(event));
1208
1209 switch (event) {
1210 case BTA_DM_DISC_RES_EVT: {
1211 p_search_data = (tBTA_DM_SEARCH*)p_param;
1212 /* Remote name update */
1213 if (strlen((const char*)p_search_data->disc_res.bd_name)) {
1214 bt_property_t properties[1];
1215 bt_status_t status;
1216
1217 properties[0].type = BT_PROPERTY_BDNAME;
1218 properties[0].val = p_search_data->disc_res.bd_name;
1219 properties[0].len = strlen((char*)p_search_data->disc_res.bd_name);
1220 RawAddress& bdaddr = p_search_data->disc_res.bd_addr;
1221
1222 status =
1223 btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
1224 ASSERTC(status == BT_STATUS_SUCCESS,
1225 "failed to save remote device property", status);
1226 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, status, &bdaddr,
1227 1, properties);
1228 }
1229 /* TODO: Services? */
1230 } break;
1231
1232 case BTA_DM_INQ_RES_EVT: {
1233 /* inquiry result */
1234 bt_bdname_t bdname;
1235 uint8_t remote_name_len;
1236 tBTA_SERVICE_MASK services = 0;
1237
1238 p_search_data = (tBTA_DM_SEARCH*)p_param;
1239 RawAddress& bdaddr = p_search_data->inq_res.bd_addr;
1240
1241 BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __func__,
1242 bdaddr.ToString().c_str(),
1243 p_search_data->inq_res.device_type);
1244 bdname.name[0] = 0;
1245
1246 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1247 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1248
1249 /* Check EIR for remote name and services */
1250 if (p_search_data->inq_res.p_eir) {
1251 BTA_GetEirService(p_search_data->inq_res.p_eir,
1252 p_search_data->inq_res.eir_len, &services);
1253 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __func__,
1254 (uint32_t)services);
1255 /* TODO: Get the service list and check to see which uuids we got and
1256 * send it back to the client. */
1257 }
1258
1259 {
1260 bt_property_t properties[5];
1261 bt_device_type_t dev_type;
1262 uint32_t num_properties = 0;
1263 bt_status_t status;
1264 int addr_type = 0;
1265
1266 memset(properties, 0, sizeof(properties));
1267 /* RawAddress */
1268 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1269 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1270 num_properties++;
1271 /* BD_NAME */
1272 /* Don't send BDNAME if it is empty */
1273 if (bdname.name[0]) {
1274 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1275 BT_PROPERTY_BDNAME,
1276 strlen((char*)bdname.name), &bdname);
1277 num_properties++;
1278 }
1279
1280 /* DEV_CLASS */
1281 uint32_t cod = devclass2uint(p_search_data->inq_res.dev_class);
1282 BTIF_TRACE_DEBUG("%s cod is 0x%06x", __func__, cod);
1283 if (cod != 0) {
1284 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1285 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod),
1286 &cod);
1287 num_properties++;
1288 }
1289
1290 /* DEV_TYPE */
1291 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1292
1293 /* Verify if the device is dual mode in NVRAM */
1294 int stored_device_type = 0;
1295 if (btif_get_device_type(bdaddr, &stored_device_type) &&
1296 ((stored_device_type != BT_DEVICE_TYPE_BREDR &&
1297 p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BREDR) ||
1298 (stored_device_type != BT_DEVICE_TYPE_BLE &&
1299 p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE))) {
1300 dev_type = (bt_device_type_t)BT_DEVICE_TYPE_DUMO;
1301 } else {
1302 dev_type = (bt_device_type_t)p_search_data->inq_res.device_type;
1303 }
1304
1305 if (p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE)
1306 addr_type = p_search_data->inq_res.ble_addr_type;
1307 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1308 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type),
1309 &dev_type);
1310 num_properties++;
1311 /* RSSI */
1312 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1313 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1314 &(p_search_data->inq_res.rssi));
1315 num_properties++;
1316
1317 status =
1318 btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1319 ASSERTC(status == BT_STATUS_SUCCESS,
1320 "failed to save remote device (inquiry)", status);
1321 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
1322 ASSERTC(status == BT_STATUS_SUCCESS,
1323 "failed to save remote addr type (inquiry)", status);
1324 /* Callback to notify upper layer of device */
1325 HAL_CBACK(bt_hal_cbacks, device_found_cb, num_properties, properties);
1326 }
1327 } break;
1328
1329 case BTA_DM_INQ_CMPL_EVT: {
1330 do_in_bta_thread(
1331 FROM_HERE,
1332 base::Bind(&BTM_BleAdvFilterParamSetup, BTM_BLE_SCAN_COND_DELETE, 0,
1333 nullptr, base::Bind(&bte_scan_filt_param_cfg_evt, 0)));
1334 } break;
1335 case BTA_DM_DISC_CMPL_EVT: {
1336 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1337 BT_DISCOVERY_STOPPED);
1338 } break;
1339 case BTA_DM_SEARCH_CANCEL_CMPL_EVT: {
1340 /* if inquiry is not in progress and we get a cancel event, then
1341 * it means we are done with inquiry, but remote_name fetches are in
1342 * progress
1343 *
1344 * if inquiry is in progress, then we don't want to act on this
1345 * cancel_cmpl_evt
1346 * but instead wait for the cancel_cmpl_evt via the Busy Level
1347 *
1348 */
1349 if (btif_dm_inquiry_in_progress == false) {
1350 btgatt_filt_param_setup_t adv_filt_param;
1351 memset(&adv_filt_param, 0, sizeof(btgatt_filt_param_setup_t));
1352 do_in_bta_thread(
1353 FROM_HERE,
1354 base::Bind(&BTM_BleAdvFilterParamSetup, BTM_BLE_SCAN_COND_DELETE, 0,
1355 nullptr, base::Bind(&bte_scan_filt_param_cfg_evt, 0)));
1356 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1357 BT_DISCOVERY_STOPPED);
1358 }
1359 } break;
1360 }
1361 }
1362
1363 /*******************************************************************************
1364 *
1365 * Function btif_dm_search_services_evt
1366 *
1367 * Description Executes search services event in btif context
1368 *
1369 * Returns void
1370 *
1371 ******************************************************************************/
btif_dm_search_services_evt(uint16_t event,char * p_param)1372 static void btif_dm_search_services_evt(uint16_t event, char* p_param) {
1373 tBTA_DM_SEARCH* p_data = (tBTA_DM_SEARCH*)p_param;
1374
1375 BTIF_TRACE_EVENT("%s: event = %d", __func__, event);
1376 switch (event) {
1377 case BTA_DM_DISC_RES_EVT: {
1378 bt_property_t prop;
1379 uint32_t i = 0;
1380 bt_status_t ret;
1381
1382 RawAddress& bd_addr = p_data->disc_res.bd_addr;
1383
1384 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __func__,
1385 p_data->disc_res.result, p_data->disc_res.services);
1386 if (p_data->disc_res.result != BTA_SUCCESS &&
1387 pairing_cb.state == BT_BOND_STATE_BONDED &&
1388 pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING) {
1389 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting",
1390 __func__);
1391 pairing_cb.sdp_attempts++;
1392 btif_dm_get_remote_services(bd_addr);
1393 return;
1394 }
1395 prop.type = BT_PROPERTY_UUIDS;
1396 prop.len = 0;
1397 if ((p_data->disc_res.result == BTA_SUCCESS) &&
1398 (p_data->disc_res.num_uuids > 0)) {
1399 prop.val = p_data->disc_res.p_uuid_list;
1400 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1401 for (i = 0; i < p_data->disc_res.num_uuids; i++) {
1402 char temp[256];
1403 uuid_to_string_legacy(
1404 (bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i * MAX_UUID_SIZE)),
1405 temp, sizeof(temp));
1406 LOG_INFO(LOG_TAG, "%s index:%d uuid:%s", __func__, i, temp);
1407 }
1408 }
1409
1410 /* onUuidChanged requires getBondedDevices to be populated.
1411 ** bond_state_changed needs to be sent prior to remote_device_property
1412 */
1413 if ((pairing_cb.state == BT_BOND_STATE_BONDED && pairing_cb.sdp_attempts) &&
1414 (p_data->disc_res.bd_addr == pairing_cb.bd_addr ||
1415 p_data->disc_res.bd_addr == pairing_cb.static_bdaddr)) {
1416 LOG_INFO(LOG_TAG, "%s: SDP search done for %s", __func__,
1417 bd_addr.ToString().c_str());
1418 pairing_cb.sdp_attempts = 0;
1419
1420 // Both SDP and bonding are done, clear pairing control block
1421 pairing_cb = {};
1422
1423 // Send one empty UUID to Java to unblock pairing intent when SDP failed
1424 // or no UUID is discovered
1425 if (p_data->disc_res.result != BTA_SUCCESS ||
1426 p_data->disc_res.num_uuids == 0) {
1427 LOG_INFO(LOG_TAG,
1428 "%s: SDP failed, send empty UUID to unblock bonding %s",
1429 __func__, bd_addr.ToString().c_str());
1430 bt_property_t prop;
1431 bt_uuid_t uuid = {};
1432 char uuid_str[128] = UUID_EMPTY;
1433
1434 string_to_uuid(uuid_str, &uuid);
1435
1436 prop.type = BT_PROPERTY_UUIDS;
1437 prop.val = uuid.uu;
1438 prop.len = MAX_UUID_SIZE;
1439
1440 /* Send the event to the BTIF */
1441 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1442 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1443 break;
1444 }
1445 }
1446
1447 if (p_data->disc_res.num_uuids != 0) {
1448 /* Also write this to the NVRAM */
1449 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1450 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed",
1451 ret);
1452 /* Send the event to the BTIF */
1453 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, BT_STATUS_SUCCESS,
1454 &bd_addr, 1, &prop);
1455 }
1456 } break;
1457
1458 case BTA_DM_DISC_CMPL_EVT:
1459 /* fixme */
1460 break;
1461
1462 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1463 /* no-op */
1464 break;
1465
1466 case BTA_DM_DISC_BLE_RES_EVT: {
1467 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __func__,
1468 p_data->disc_ble_res.service.uu.uuid16);
1469 bt_uuid_t uuid;
1470 int i = 0;
1471 int j = 15;
1472 int num_properties = 0;
1473 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID) {
1474 BTIF_TRACE_DEBUG("%s: Found HOGP UUID", __func__);
1475 bt_property_t prop[2];
1476 char temp[256];
1477 bt_status_t ret;
1478
1479 bta_gatt_convert_uuid16_to_uuid128(
1480 uuid.uu, p_data->disc_ble_res.service.uu.uuid16);
1481
1482 while (i < j) {
1483 unsigned char c = uuid.uu[j];
1484 uuid.uu[j] = uuid.uu[i];
1485 uuid.uu[i] = c;
1486 i++;
1487 j--;
1488 }
1489
1490 uuid_to_string_legacy(&uuid, temp, sizeof(temp));
1491 LOG_INFO(LOG_TAG, "%s uuid:%s", __func__, temp);
1492
1493 RawAddress& bd_addr = p_data->disc_ble_res.bd_addr;
1494 prop[0].type = BT_PROPERTY_UUIDS;
1495 prop[0].val = uuid.uu;
1496 prop[0].len = MAX_UUID_SIZE;
1497
1498 /* Also write this to the NVRAM */
1499 ret = btif_storage_set_remote_device_property(&bd_addr, &prop[0]);
1500 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed",
1501 ret);
1502 num_properties++;
1503
1504 /* Remote name update */
1505 if (strnlen((const char*)p_data->disc_ble_res.bd_name, BD_NAME_LEN)) {
1506 prop[1].type = BT_PROPERTY_BDNAME;
1507 prop[1].val = p_data->disc_ble_res.bd_name;
1508 prop[1].len =
1509 strnlen((char*)p_data->disc_ble_res.bd_name, BD_NAME_LEN);
1510
1511 ret = btif_storage_set_remote_device_property(&bd_addr, &prop[1]);
1512 ASSERTC(ret == BT_STATUS_SUCCESS,
1513 "failed to save remote device property", ret);
1514 num_properties++;
1515 }
1516
1517 /* Send the event to the BTIF */
1518 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, BT_STATUS_SUCCESS,
1519 &bd_addr, num_properties, prop);
1520 }
1521 } break;
1522
1523 default: { ASSERTC(0, "unhandled search services event", event); } break;
1524 }
1525 }
1526
1527 /*******************************************************************************
1528 *
1529 * Function btif_dm_remote_service_record_evt
1530 *
1531 * Description Executes search service record event in btif context
1532 *
1533 * Returns void
1534 *
1535 ******************************************************************************/
btif_dm_remote_service_record_evt(uint16_t event,char * p_param)1536 static void btif_dm_remote_service_record_evt(uint16_t event, char* p_param) {
1537 tBTA_DM_SEARCH* p_data = (tBTA_DM_SEARCH*)p_param;
1538
1539 BTIF_TRACE_EVENT("%s: event = %d", __func__, event);
1540 switch (event) {
1541 case BTA_DM_DISC_RES_EVT: {
1542 bt_service_record_t rec;
1543 bt_property_t prop;
1544
1545 memset(&rec, 0, sizeof(bt_service_record_t));
1546 RawAddress& bd_addr = p_data->disc_res.bd_addr;
1547
1548 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __func__,
1549 p_data->disc_res.result, p_data->disc_res.services);
1550 prop.type = BT_PROPERTY_SERVICE_RECORD;
1551 prop.val = (void*)&rec;
1552 prop.len = sizeof(rec);
1553
1554 /* disc_res.result is overloaded with SCN. Cannot check result */
1555 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1556 /* TODO: Get the UUID as well */
1557 rec.channel = p_data->disc_res.result - 3;
1558 /* TODO: Need to get the service name using p_raw_data */
1559 rec.name[0] = 0;
1560
1561 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb, BT_STATUS_SUCCESS,
1562 &bd_addr, 1, &prop);
1563 } break;
1564
1565 default: {
1566 ASSERTC(0, "unhandled remote service record event", event);
1567 } break;
1568 }
1569 }
1570
1571 /*******************************************************************************
1572 *
1573 * Function btif_dm_upstreams_cback
1574 *
1575 * Description Executes UPSTREAMS events in btif context
1576 *
1577 * Returns void
1578 *
1579 ******************************************************************************/
btif_dm_upstreams_evt(uint16_t event,char * p_param)1580 static void btif_dm_upstreams_evt(uint16_t event, char* p_param) {
1581 tBTA_DM_SEC* p_data = (tBTA_DM_SEC*)p_param;
1582 tBTA_SERVICE_MASK service_mask;
1583 uint32_t i;
1584 RawAddress bd_addr;
1585
1586 BTIF_TRACE_EVENT("%s: ev: %s", __func__, dump_dm_event(event));
1587
1588 switch (event) {
1589 case BTA_DM_ENABLE_EVT: {
1590 BD_NAME bdname;
1591 bt_status_t status;
1592 bt_property_t prop;
1593 prop.type = BT_PROPERTY_BDNAME;
1594 prop.len = BD_NAME_LEN;
1595 prop.val = (void*)bdname;
1596
1597 status = btif_storage_get_adapter_property(&prop);
1598 if (status == BT_STATUS_SUCCESS) {
1599 /* A name exists in the storage. Make this the device name */
1600 BTA_DmSetDeviceName((char*)prop.val);
1601 } else {
1602 /* Storage does not have a name yet.
1603 * Use the default name and write it to the chip
1604 */
1605 BTA_DmSetDeviceName(btif_get_default_local_name());
1606 }
1607
1608 /* Enable local privacy */
1609 BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED);
1610
1611 /* for each of the enabled services in the mask, trigger the profile
1612 * enable */
1613 service_mask = btif_get_enabled_services_mask();
1614 for (i = 0; i <= BTA_MAX_SERVICE_ID; i++) {
1615 if (service_mask &
1616 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i))) {
1617 btif_in_execute_service_request(i, true);
1618 }
1619 }
1620 /* clear control blocks */
1621 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1622 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
1623
1624 /* This function will also trigger the adapter_properties_cb
1625 ** and bonded_devices_info_cb
1626 */
1627 btif_storage_load_bonded_devices();
1628
1629 btif_enable_bluetooth_evt(p_data->enable.status);
1630 } break;
1631
1632 case BTA_DM_DISABLE_EVT:
1633 /* for each of the enabled services in the mask, trigger the profile
1634 * disable */
1635 service_mask = btif_get_enabled_services_mask();
1636 for (i = 0; i <= BTA_MAX_SERVICE_ID; i++) {
1637 if (service_mask &
1638 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i))) {
1639 btif_in_execute_service_request(i, false);
1640 }
1641 }
1642 btif_disable_bluetooth_evt();
1643 break;
1644
1645 case BTA_DM_PIN_REQ_EVT:
1646 btif_dm_pin_req_evt(&p_data->pin_req);
1647 break;
1648
1649 case BTA_DM_AUTH_CMPL_EVT:
1650 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1651 break;
1652
1653 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1654 if (is_bonding_or_sdp()) {
1655 bd_addr = pairing_cb.bd_addr;
1656 btm_set_bond_type_dev(pairing_cb.bd_addr, BOND_TYPE_UNKNOWN);
1657 bond_state_changed((bt_status_t)p_data->bond_cancel_cmpl.result,
1658 bd_addr, BT_BOND_STATE_NONE);
1659 }
1660 break;
1661
1662 case BTA_DM_SP_CFM_REQ_EVT:
1663 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1664 break;
1665 case BTA_DM_SP_KEY_NOTIF_EVT:
1666 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1667 break;
1668
1669 case BTA_DM_DEV_UNPAIRED_EVT:
1670 bd_addr = p_data->link_down.bd_addr;
1671 btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);
1672
1673 /*special handling for HID devices */
1674 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == true))
1675 btif_hh_remove_device(bd_addr);
1676 #endif
1677 #if (defined(BTA_HD_INCLUDED) && (BTA_HD_INCLUDED == TRUE))
1678 btif_hd_remove_device(bd_addr);
1679 #endif
1680 btif_storage_remove_bonded_device(&bd_addr);
1681 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_NONE);
1682 break;
1683
1684 case BTA_DM_BUSY_LEVEL_EVT: {
1685 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK) {
1686 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED) {
1687 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1688 BT_DISCOVERY_STARTED);
1689 btif_dm_inquiry_in_progress = true;
1690 } else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED) {
1691 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1692 BT_DISCOVERY_STOPPED);
1693 btif_dm_inquiry_in_progress = false;
1694 } else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE) {
1695 btif_dm_inquiry_in_progress = false;
1696 }
1697 }
1698 } break;
1699
1700 case BTA_DM_LINK_UP_EVT:
1701 bd_addr = p_data->link_up.bd_addr;
1702 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
1703
1704 btif_update_remote_version_property(&bd_addr);
1705
1706 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1707 &bd_addr, BT_ACL_STATE_CONNECTED);
1708 break;
1709
1710 case BTA_DM_LINK_DOWN_EVT:
1711 bd_addr = p_data->link_down.bd_addr;
1712 btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);
1713 btif_av_move_idle(bd_addr);
1714 BTIF_TRACE_DEBUG(
1715 "BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
1716 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1717 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1718 break;
1719
1720 case BTA_DM_HW_ERROR_EVT:
1721 BTIF_TRACE_ERROR("Received H/W Error. ");
1722 /* Flush storage data */
1723 btif_config_flush();
1724 usleep(100000); /* 100milliseconds */
1725 /* Killing the process to force a restart as part of fault tolerance */
1726 kill(getpid(), SIGKILL);
1727 break;
1728
1729 case BTA_DM_BLE_KEY_EVT:
1730 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT key_type=0x%02x ",
1731 p_data->ble_key.key_type);
1732
1733 /* If this pairing is by-product of local initiated GATT client Read or
1734 Write,
1735 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would
1736 not
1737 have setup properly. Setup pairing_cb and notify App about Bonding state
1738 now*/
1739 if (pairing_cb.state != BT_BOND_STATE_BONDING) {
1740 BTIF_TRACE_DEBUG(
1741 "Bond state not sent to App so far.Notify the app now");
1742 bond_state_changed(BT_STATUS_SUCCESS, p_data->ble_key.bd_addr,
1743 BT_BOND_STATE_BONDING);
1744 } else if (pairing_cb.bd_addr != p_data->ble_key.bd_addr) {
1745 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",
1746 p_data->ble_key.key_type);
1747 break;
1748 }
1749
1750 switch (p_data->ble_key.key_type) {
1751 case BTA_LE_KEY_PENC:
1752 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
1753 pairing_cb.ble.is_penc_key_rcvd = true;
1754 pairing_cb.ble.penc_key = p_data->ble_key.p_key_value->penc_key;
1755 break;
1756
1757 case BTA_LE_KEY_PID:
1758 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
1759 pairing_cb.ble.is_pid_key_rcvd = true;
1760 pairing_cb.ble.pid_key = p_data->ble_key.p_key_value->pid_key;
1761 break;
1762
1763 case BTA_LE_KEY_PCSRK:
1764 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
1765 pairing_cb.ble.is_pcsrk_key_rcvd = true;
1766 pairing_cb.ble.pcsrk_key = p_data->ble_key.p_key_value->pcsrk_key;
1767 break;
1768
1769 case BTA_LE_KEY_LENC:
1770 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
1771 pairing_cb.ble.is_lenc_key_rcvd = true;
1772 pairing_cb.ble.lenc_key = p_data->ble_key.p_key_value->lenc_key;
1773 break;
1774
1775 case BTA_LE_KEY_LCSRK:
1776 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
1777 pairing_cb.ble.is_lcsrk_key_rcvd = true;
1778 pairing_cb.ble.lcsrk_key = p_data->ble_key.p_key_value->lcsrk_key;
1779 break;
1780
1781 case BTA_LE_KEY_LID:
1782 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LID");
1783 pairing_cb.ble.is_lidk_key_rcvd = true;
1784 break;
1785
1786 default:
1787 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)",
1788 p_data->ble_key.key_type);
1789 break;
1790 }
1791 break;
1792 case BTA_DM_BLE_SEC_REQ_EVT:
1793 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
1794 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1795 break;
1796 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
1797 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
1798 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1799 break;
1800 case BTA_DM_BLE_PASSKEY_REQ_EVT:
1801 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
1802 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1803 break;
1804 case BTA_DM_BLE_NC_REQ_EVT:
1805 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
1806 btif_dm_ble_key_nc_req_evt(&p_data->key_notif);
1807 break;
1808 case BTA_DM_BLE_OOB_REQ_EVT:
1809 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
1810 btif_dm_ble_oob_req_evt(&p_data->rmt_oob);
1811 break;
1812 case BTA_DM_BLE_SC_OOB_REQ_EVT:
1813 BTIF_TRACE_DEBUG("BTA_DM_BLE_SC_OOB_REQ_EVT. ");
1814 btif_dm_ble_sc_oob_req_evt(&p_data->rmt_oob);
1815 break;
1816 case BTA_DM_BLE_LOCAL_IR_EVT:
1817 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
1818 ble_local_key_cb.is_id_keys_rcvd = true;
1819 memcpy(&ble_local_key_cb.id_keys.irk[0], &p_data->ble_id_keys.irk[0],
1820 sizeof(BT_OCTET16));
1821 memcpy(&ble_local_key_cb.id_keys.ir[0], &p_data->ble_id_keys.ir[0],
1822 sizeof(BT_OCTET16));
1823 memcpy(&ble_local_key_cb.id_keys.dhk[0], &p_data->ble_id_keys.dhk[0],
1824 sizeof(BT_OCTET16));
1825 btif_storage_add_ble_local_key((char*)&ble_local_key_cb.id_keys.irk[0],
1826 BTIF_DM_LE_LOCAL_KEY_IRK, BT_OCTET16_LEN);
1827 btif_storage_add_ble_local_key((char*)&ble_local_key_cb.id_keys.ir[0],
1828 BTIF_DM_LE_LOCAL_KEY_IR, BT_OCTET16_LEN);
1829 btif_storage_add_ble_local_key((char*)&ble_local_key_cb.id_keys.dhk[0],
1830 BTIF_DM_LE_LOCAL_KEY_DHK, BT_OCTET16_LEN);
1831 break;
1832 case BTA_DM_BLE_LOCAL_ER_EVT:
1833 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
1834 ble_local_key_cb.is_er_rcvd = true;
1835 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1836 btif_storage_add_ble_local_key((char*)&ble_local_key_cb.er[0],
1837 BTIF_DM_LE_LOCAL_KEY_ER, BT_OCTET16_LEN);
1838 break;
1839
1840 case BTA_DM_BLE_AUTH_CMPL_EVT:
1841 BTIF_TRACE_DEBUG("BTA_DM_BLE_AUTH_CMPL_EVT. ");
1842 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1843 break;
1844
1845 case BTA_DM_LE_FEATURES_READ: {
1846 tBTM_BLE_VSC_CB cmn_vsc_cb;
1847 bt_local_le_features_t local_le_features;
1848 char buf[512];
1849 bt_property_t prop;
1850 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1851 prop.val = (void*)buf;
1852 prop.len = sizeof(buf);
1853
1854 /* LE features are not stored in storage. Should be retrived from stack */
1855 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1856 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1857
1858 prop.len = sizeof(bt_local_le_features_t);
1859 if (cmn_vsc_cb.filter_support == 1)
1860 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1861 else
1862 local_le_features.max_adv_filter_supported = 0;
1863 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1864 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1865 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
1866 local_le_features.activity_energy_info_supported =
1867 cmn_vsc_cb.energy_support;
1868 local_le_features.scan_result_storage_size =
1869 cmn_vsc_cb.tot_scan_results_strg;
1870 local_le_features.version_supported = cmn_vsc_cb.version_supported;
1871 local_le_features.total_trackable_advertisers =
1872 cmn_vsc_cb.total_trackable_advertisers;
1873
1874 local_le_features.extended_scan_support =
1875 cmn_vsc_cb.extended_scan_support > 0;
1876 local_le_features.debug_logging_supported =
1877 cmn_vsc_cb.debug_logging_supported > 0;
1878
1879 const controller_t* controller = controller_get_interface();
1880
1881 local_le_features.le_2m_phy_supported = controller->supports_ble_2m_phy();
1882 local_le_features.le_coded_phy_supported =
1883 controller->supports_ble_coded_phy();
1884 local_le_features.le_extended_advertising_supported =
1885 controller->supports_ble_extended_advertising();
1886 local_le_features.le_periodic_advertising_supported =
1887 controller->supports_ble_periodic_advertising();
1888 local_le_features.le_maximum_advertising_data_length =
1889 controller->get_ble_maxium_advertising_data_length();
1890
1891 memcpy(prop.val, &local_le_features, prop.len);
1892 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1,
1893 &prop);
1894 break;
1895 }
1896
1897 case BTA_DM_ENER_INFO_READ: {
1898 btif_activity_energy_info_cb_t* p_ener_data =
1899 (btif_activity_energy_info_cb_t*)p_param;
1900 bt_activity_energy_info energy_info;
1901 energy_info.status = p_ener_data->status;
1902 energy_info.ctrl_state = p_ener_data->ctrl_state;
1903 energy_info.rx_time = p_ener_data->rx_time;
1904 energy_info.tx_time = p_ener_data->tx_time;
1905 energy_info.idle_time = p_ener_data->idle_time;
1906 energy_info.energy_used = p_ener_data->energy_used;
1907
1908 bt_uid_traffic_t* data = uid_set_read_and_clear(uid_set);
1909 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info, data);
1910 osi_free(data);
1911 break;
1912 }
1913
1914 case BTA_DM_AUTHORIZE_EVT:
1915 case BTA_DM_SIG_STRENGTH_EVT:
1916 case BTA_DM_SP_RMT_OOB_EVT:
1917 case BTA_DM_SP_KEYPRESS_EVT:
1918 case BTA_DM_ROLE_CHG_EVT:
1919
1920 default:
1921 BTIF_TRACE_WARNING("btif_dm_cback : unhandled event (%d)", event);
1922 break;
1923 }
1924
1925 btif_dm_data_free(event, p_data);
1926 }
1927
1928 /*******************************************************************************
1929 *
1930 * Function btif_dm_generic_evt
1931 *
1932 * Description Executes non-BTA upstream events in BTIF context
1933 *
1934 * Returns void
1935 *
1936 ******************************************************************************/
btif_dm_generic_evt(uint16_t event,char * p_param)1937 static void btif_dm_generic_evt(uint16_t event, char* p_param) {
1938 BTIF_TRACE_EVENT("%s: event=%d", __func__, event);
1939 switch (event) {
1940 case BTIF_DM_CB_DISCOVERY_STARTED: {
1941 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1942 BT_DISCOVERY_STARTED);
1943 } break;
1944
1945 case BTIF_DM_CB_CREATE_BOND: {
1946 pairing_cb.timeout_retries = NUM_TIMEOUT_RETRIES;
1947 btif_dm_create_bond_cb_t* create_bond_cb =
1948 (btif_dm_create_bond_cb_t*)p_param;
1949 btif_dm_cb_create_bond(create_bond_cb->bdaddr, create_bond_cb->transport);
1950 } break;
1951
1952 case BTIF_DM_CB_REMOVE_BOND: {
1953 btif_dm_cb_remove_bond((RawAddress*)p_param);
1954 } break;
1955
1956 case BTIF_DM_CB_HID_REMOTE_NAME: {
1957 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME*)p_param);
1958 } break;
1959
1960 case BTIF_DM_CB_BOND_STATE_BONDING: {
1961 bond_state_changed(BT_STATUS_SUCCESS, *((RawAddress*)p_param),
1962 BT_BOND_STATE_BONDING);
1963 } break;
1964 case BTIF_DM_CB_LE_TX_TEST:
1965 case BTIF_DM_CB_LE_RX_TEST: {
1966 uint8_t status;
1967 STREAM_TO_UINT8(status, p_param);
1968 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1969 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
1970 } break;
1971 case BTIF_DM_CB_LE_TEST_END: {
1972 uint8_t status;
1973 uint16_t count = 0;
1974 STREAM_TO_UINT8(status, p_param);
1975 if (status == 0) STREAM_TO_UINT16(count, p_param);
1976 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
1977 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
1978 } break;
1979 default: {
1980 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __func__, event);
1981 } break;
1982 }
1983 }
1984
1985 /*******************************************************************************
1986 *
1987 * Function bte_dm_evt
1988 *
1989 * Description Switches context from BTE to BTIF for all DM events
1990 *
1991 * Returns void
1992 *
1993 ******************************************************************************/
1994
bte_dm_evt(tBTA_DM_SEC_EVT event,tBTA_DM_SEC * p_data)1995 void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC* p_data) {
1996 /* switch context to btif task context (copy full union size for convenience)
1997 */
1998 bt_status_t status = btif_transfer_context(
1999 btif_dm_upstreams_evt, (uint16_t)event, (char*)p_data,
2000 sizeof(tBTA_DM_SEC), btif_dm_data_copy);
2001
2002 /* catch any failed context transfers */
2003 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
2004 }
2005
2006 /*******************************************************************************
2007 *
2008 * Function bte_search_devices_evt
2009 *
2010 * Description Switches context from BTE to BTIF for DM search events
2011 *
2012 * Returns void
2013 *
2014 ******************************************************************************/
bte_search_devices_evt(tBTA_DM_SEARCH_EVT event,tBTA_DM_SEARCH * p_data)2015 static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event,
2016 tBTA_DM_SEARCH* p_data) {
2017 uint16_t param_len = 0;
2018
2019 if (p_data) param_len += sizeof(tBTA_DM_SEARCH);
2020 /* Allocate buffer to hold the pointers (deep copy). The pointers will point
2021 * to the end of the tBTA_DM_SEARCH */
2022 switch (event) {
2023 case BTA_DM_INQ_RES_EVT: {
2024 if (p_data->inq_res.p_eir) param_len += p_data->inq_res.eir_len;
2025 } break;
2026
2027 case BTA_DM_DISC_RES_EVT: {
2028 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
2029 param_len += p_data->disc_res.raw_data_size;
2030 } break;
2031 }
2032 BTIF_TRACE_DEBUG("%s event=%s param_len=%d", __func__,
2033 dump_dm_search_event(event), param_len);
2034
2035 /* if remote name is available in EIR, set teh flag so that stack doesnt
2036 * trigger RNR */
2037 if (event == BTA_DM_INQ_RES_EVT)
2038 p_data->inq_res.remt_name_not_required =
2039 check_eir_remote_name(p_data, NULL, NULL);
2040
2041 btif_transfer_context(
2042 btif_dm_search_devices_evt, (uint16_t)event, (char*)p_data, param_len,
2043 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
2044 }
2045
2046 /*******************************************************************************
2047 *
2048 * Function bte_dm_search_services_evt
2049 *
2050 * Description Switches context from BTE to BTIF for DM search services
2051 * event
2052 *
2053 * Returns void
2054 *
2055 ******************************************************************************/
bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event,tBTA_DM_SEARCH * p_data)2056 static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event,
2057 tBTA_DM_SEARCH* p_data) {
2058 uint16_t param_len = 0;
2059 if (p_data) param_len += sizeof(tBTA_DM_SEARCH);
2060 switch (event) {
2061 case BTA_DM_DISC_RES_EVT: {
2062 if ((p_data->disc_res.result == BTA_SUCCESS) &&
2063 (p_data->disc_res.num_uuids > 0)) {
2064 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
2065 }
2066 } break;
2067 }
2068 /* TODO: The only other member that needs a deep copy is the p_raw_data. But
2069 * not sure
2070 * if raw_data is needed. */
2071 btif_transfer_context(
2072 btif_dm_search_services_evt, event, (char*)p_data, param_len,
2073 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
2074 }
2075
2076 /*******************************************************************************
2077 *
2078 * Function bte_dm_remote_service_record_evt
2079 *
2080 * Description Switches context from BTE to BTIF for DM search service
2081 * record event
2082 *
2083 * Returns void
2084 *
2085 ******************************************************************************/
bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event,tBTA_DM_SEARCH * p_data)2086 static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event,
2087 tBTA_DM_SEARCH* p_data) {
2088 /* TODO: The only member that needs a deep copy is the p_raw_data. But not
2089 * sure yet if this is needed. */
2090 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data,
2091 sizeof(tBTA_DM_SEARCH), NULL);
2092 }
2093
2094 /*******************************************************************************
2095 *
2096 * Function bta_energy_info_cb
2097 *
2098 * Description Switches context from BTE to BTIF for DM energy info event
2099 *
2100 * Returns void
2101 *
2102 ******************************************************************************/
bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time,tBTA_DM_BLE_RX_TIME_MS rx_time,tBTA_DM_BLE_IDLE_TIME_MS idle_time,tBTA_DM_BLE_ENERGY_USED energy_used,tBTA_DM_CONTRL_STATE ctrl_state,tBTA_STATUS status)2103 static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time,
2104 tBTA_DM_BLE_RX_TIME_MS rx_time,
2105 tBTA_DM_BLE_IDLE_TIME_MS idle_time,
2106 tBTA_DM_BLE_ENERGY_USED energy_used,
2107 tBTA_DM_CONTRL_STATE ctrl_state,
2108 tBTA_STATUS status) {
2109 BTIF_TRACE_DEBUG(
2110 "energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, "
2111 "idle_time=%ld,used=%ld",
2112 status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
2113
2114 btif_activity_energy_info_cb_t btif_cb;
2115 btif_cb.status = status;
2116 btif_cb.ctrl_state = ctrl_state;
2117 btif_cb.tx_time = (uint64_t)tx_time;
2118 btif_cb.rx_time = (uint64_t)rx_time;
2119 btif_cb.idle_time = (uint64_t)idle_time;
2120 btif_cb.energy_used = (uint64_t)energy_used;
2121 btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
2122 (char*)&btif_cb, sizeof(btif_activity_energy_info_cb_t),
2123 NULL);
2124 }
2125
2126 /* Scan filter param config event */
bte_scan_filt_param_cfg_evt(uint8_t ref_value,uint8_t avbl_space,uint8_t action_type,uint8_t status)2127 static void bte_scan_filt_param_cfg_evt(uint8_t ref_value, uint8_t avbl_space,
2128 uint8_t action_type, uint8_t status) {
2129 /* This event occurs on calling BTA_DmBleCfgFilterCondition internally,
2130 ** and that is why there is no HAL callback
2131 */
2132 if (BTA_SUCCESS != status) {
2133 BTIF_TRACE_ERROR("%s, %d", __func__, status);
2134 } else {
2135 BTIF_TRACE_DEBUG("%s", __func__);
2136 }
2137 }
2138
2139 /*****************************************************************************
2140 *
2141 * btif api functions (no context switch)
2142 *
2143 ****************************************************************************/
2144
2145 /*******************************************************************************
2146 *
2147 * Function btif_dm_start_discovery
2148 *
2149 * Description Start device discovery/inquiry
2150 *
2151 * Returns bt_status_t
2152 *
2153 ******************************************************************************/
btif_dm_start_discovery(void)2154 bt_status_t btif_dm_start_discovery(void) {
2155 tBTA_DM_INQ inq_params;
2156 tBTA_SERVICE_MASK services = 0;
2157
2158 BTIF_TRACE_EVENT("%s", __func__);
2159
2160 /* Cleanup anything remaining on index 0 */
2161 do_in_bta_thread(
2162 FROM_HERE,
2163 base::Bind(&BTM_BleAdvFilterParamSetup, BTM_BLE_SCAN_COND_DELETE, 0,
2164 nullptr, base::Bind(&bte_scan_filt_param_cfg_evt, 0)));
2165
2166 auto adv_filt_param = std::make_unique<btgatt_filt_param_setup_t>();
2167 /* Add an allow-all filter on index 0*/
2168 adv_filt_param->dely_mode = IMMEDIATE_DELY_MODE;
2169 adv_filt_param->feat_seln = ALLOW_ALL_FILTER;
2170 adv_filt_param->filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;
2171 adv_filt_param->list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;
2172 adv_filt_param->rssi_low_thres = LOWEST_RSSI_VALUE;
2173 adv_filt_param->rssi_high_thres = LOWEST_RSSI_VALUE;
2174 do_in_bta_thread(
2175 FROM_HERE, base::Bind(&BTM_BleAdvFilterParamSetup, BTM_BLE_SCAN_COND_ADD,
2176 0, base::Passed(&adv_filt_param),
2177 base::Bind(&bte_scan_filt_param_cfg_evt, 0)));
2178
2179 /* TODO: Do we need to handle multiple inquiries at the same time? */
2180
2181 /* Set inquiry params and call API */
2182 inq_params.mode = BTA_DM_GENERAL_INQUIRY | BTA_BLE_GENERAL_INQUIRY;
2183 #if (BTA_HOST_INTERLEAVE_SEARCH == TRUE)
2184 inq_params.intl_duration[0] = BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
2185 inq_params.intl_duration[1] = BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
2186 inq_params.intl_duration[2] = BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
2187 inq_params.intl_duration[3] = BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
2188 #endif
2189 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
2190
2191 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
2192 inq_params.report_dup = true;
2193
2194 inq_params.filter_type = BTA_DM_INQ_CLR;
2195 /* TODO: Filter device by BDA needs to be implemented here */
2196
2197 /* Will be enabled to true once inquiry busy level has been received */
2198 btif_dm_inquiry_in_progress = false;
2199 /* find nearby devices */
2200 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
2201
2202 return BT_STATUS_SUCCESS;
2203 }
2204
2205 /*******************************************************************************
2206 *
2207 * Function btif_dm_cancel_discovery
2208 *
2209 * Description Cancels search
2210 *
2211 * Returns bt_status_t
2212 *
2213 ******************************************************************************/
btif_dm_cancel_discovery(void)2214 bt_status_t btif_dm_cancel_discovery(void) {
2215 BTIF_TRACE_EVENT("%s", __func__);
2216 BTA_DmSearchCancel();
2217 return BT_STATUS_SUCCESS;
2218 }
2219
2220 /*******************************************************************************
2221 *
2222 * Function btif_dm_create_bond
2223 *
2224 * Description Initiate bonding with the specified device
2225 *
2226 * Returns bt_status_t
2227 *
2228 ******************************************************************************/
btif_dm_create_bond(const RawAddress * bd_addr,int transport)2229 bt_status_t btif_dm_create_bond(const RawAddress* bd_addr, int transport) {
2230 btif_dm_create_bond_cb_t create_bond_cb;
2231 create_bond_cb.transport = transport;
2232 create_bond_cb.bdaddr = *bd_addr;
2233
2234 BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __func__,
2235 bd_addr->ToString().c_str(), transport);
2236 if (pairing_cb.state != BT_BOND_STATE_NONE) return BT_STATUS_BUSY;
2237
2238 btif_stats_add_bond_event(*bd_addr, BTIF_DM_FUNC_CREATE_BOND,
2239 pairing_cb.state);
2240
2241 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
2242 (char*)&create_bond_cb,
2243 sizeof(btif_dm_create_bond_cb_t), NULL);
2244
2245 return BT_STATUS_SUCCESS;
2246 }
2247
2248 /*******************************************************************************
2249 *
2250 * Function btif_dm_create_bond_out_of_band
2251 *
2252 * Description Initiate bonding with the specified device using out of band
2253 * data
2254 *
2255 * Returns bt_status_t
2256 *
2257 ******************************************************************************/
btif_dm_create_bond_out_of_band(const RawAddress * bd_addr,int transport,const bt_out_of_band_data_t * oob_data)2258 bt_status_t btif_dm_create_bond_out_of_band(
2259 const RawAddress* bd_addr, int transport,
2260 const bt_out_of_band_data_t* oob_data) {
2261 oob_cb.bdaddr = *bd_addr;
2262 memcpy(&oob_cb.oob_data, oob_data, sizeof(bt_out_of_band_data_t));
2263
2264 uint8_t empty[] = {0, 0, 0, 0, 0, 0, 0};
2265 // If LE Bluetooth Device Address is provided, use provided address type
2266 // value.
2267 if (memcmp(oob_data->le_bt_dev_addr, empty, 7) != 0) {
2268 /* byte no 7 is address type in LE Bluetooth Address OOB data */
2269 uint8_t address_type = oob_data->le_bt_dev_addr[6];
2270 if (address_type == BLE_ADDR_PUBLIC || address_type == BLE_ADDR_RANDOM) {
2271 // bd_addr->address is already reversed, so use it instead of
2272 // oob_data->le_bt_dev_addr
2273 BTM_SecAddBleDevice(*bd_addr, NULL, BT_DEVICE_TYPE_BLE, address_type);
2274 }
2275 }
2276
2277 BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __func__,
2278 bd_addr->ToString().c_str(), transport);
2279 return btif_dm_create_bond(bd_addr, transport);
2280 }
2281
2282 /*******************************************************************************
2283 *
2284 * Function btif_dm_cancel_bond
2285 *
2286 * Description Initiate bonding with the specified device
2287 *
2288 * Returns bt_status_t
2289 *
2290 ******************************************************************************/
2291
btif_dm_cancel_bond(const RawAddress * bd_addr)2292 bt_status_t btif_dm_cancel_bond(const RawAddress* bd_addr) {
2293 BTIF_TRACE_EVENT("%s: bd_addr=%s", __func__, bd_addr->ToString().c_str());
2294
2295 btif_stats_add_bond_event(*bd_addr, BTIF_DM_FUNC_CANCEL_BOND,
2296 pairing_cb.state);
2297
2298 /* TODO:
2299 ** 1. Restore scan modes
2300 ** 2. special handling for HID devices
2301 */
2302 if (is_bonding_or_sdp()) {
2303 if (pairing_cb.is_ssp) {
2304 if (pairing_cb.is_le_only) {
2305 BTA_DmBleSecurityGrant(*bd_addr, BTA_DM_SEC_PAIR_NOT_SPT);
2306 } else {
2307 BTA_DmConfirm(*bd_addr, false);
2308 BTA_DmBondCancel(*bd_addr);
2309 btif_storage_remove_bonded_device(bd_addr);
2310 }
2311 } else {
2312 if (pairing_cb.is_le_only) {
2313 BTA_DmBondCancel(*bd_addr);
2314 } else {
2315 BTA_DmPinReply(*bd_addr, false, 0, NULL);
2316 }
2317 /* Cancel bonding, in case it is in ACL connection setup state */
2318 BTA_DmBondCancel(*bd_addr);
2319 }
2320 }
2321
2322 return BT_STATUS_SUCCESS;
2323 }
2324
2325 /*******************************************************************************
2326 *
2327 * Function btif_dm_hh_open_failed
2328 *
2329 * Description informs the upper layers if the HH have failed during
2330 * bonding
2331 *
2332 * Returns none
2333 *
2334 ******************************************************************************/
2335
btif_dm_hh_open_failed(RawAddress * bdaddr)2336 void btif_dm_hh_open_failed(RawAddress* bdaddr) {
2337 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2338 *bdaddr == pairing_cb.bd_addr) {
2339 bond_state_changed(BT_STATUS_FAIL, *bdaddr, BT_BOND_STATE_NONE);
2340 }
2341 }
2342
2343 /*******************************************************************************
2344 *
2345 * Function btif_dm_remove_bond
2346 *
2347 * Description Removes bonding with the specified device
2348 *
2349 * Returns bt_status_t
2350 *
2351 ******************************************************************************/
2352
btif_dm_remove_bond(const RawAddress * bd_addr)2353 bt_status_t btif_dm_remove_bond(const RawAddress* bd_addr) {
2354 BTIF_TRACE_EVENT("%s: bd_addr=%s", __func__, bd_addr->ToString().c_str());
2355
2356 btif_stats_add_bond_event(*bd_addr, BTIF_DM_FUNC_REMOVE_BOND,
2357 pairing_cb.state);
2358
2359 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2360 (char*)bd_addr, sizeof(RawAddress), NULL);
2361
2362 return BT_STATUS_SUCCESS;
2363 }
2364
2365 /*******************************************************************************
2366 *
2367 * Function btif_dm_pin_reply
2368 *
2369 * Description BT legacy pairing - PIN code reply
2370 *
2371 * Returns bt_status_t
2372 *
2373 ******************************************************************************/
2374
btif_dm_pin_reply(const RawAddress * bd_addr,uint8_t accept,uint8_t pin_len,bt_pin_code_t * pin_code)2375 bt_status_t btif_dm_pin_reply(const RawAddress* bd_addr, uint8_t accept,
2376 uint8_t pin_len, bt_pin_code_t* pin_code) {
2377 BTIF_TRACE_EVENT("%s: accept=%d", __func__, accept);
2378 if (pin_code == NULL || pin_len > PIN_CODE_LEN) return BT_STATUS_FAIL;
2379 if (pairing_cb.is_le_only) {
2380 int i;
2381 uint32_t passkey = 0;
2382 int multi[] = {100000, 10000, 1000, 100, 10, 1};
2383 for (i = 0; i < 6; i++) {
2384 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2385 }
2386 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
2387 BTA_DmBlePasskeyReply(*bd_addr, accept, passkey);
2388
2389 } else {
2390 BTA_DmPinReply(*bd_addr, accept, pin_len, pin_code->pin);
2391 if (accept) pairing_cb.pin_code_len = pin_len;
2392 }
2393 return BT_STATUS_SUCCESS;
2394 }
2395
2396 /*******************************************************************************
2397 *
2398 * Function btif_dm_ssp_reply
2399 *
2400 * Description BT SSP Reply - Just Works, Numeric Comparison & Passkey
2401 * Entry
2402 *
2403 * Returns bt_status_t
2404 *
2405 ******************************************************************************/
btif_dm_ssp_reply(const RawAddress * bd_addr,bt_ssp_variant_t variant,uint8_t accept,UNUSED_ATTR uint32_t passkey)2406 bt_status_t btif_dm_ssp_reply(const RawAddress* bd_addr,
2407 bt_ssp_variant_t variant, uint8_t accept,
2408 UNUSED_ATTR uint32_t passkey) {
2409 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY) {
2410 /* This is not implemented in the stack.
2411 * For devices with display, this is not needed
2412 */
2413 BTIF_TRACE_WARNING("%s: Not implemented", __func__);
2414 return BT_STATUS_FAIL;
2415 }
2416 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
2417 BTIF_TRACE_EVENT("%s: accept=%d", __func__, accept);
2418 if (pairing_cb.is_le_only) {
2419 if (pairing_cb.is_le_nc) {
2420 BTA_DmBleConfirmReply(*bd_addr, accept);
2421 } else {
2422 if (accept)
2423 BTA_DmBleSecurityGrant(*bd_addr, BTA_DM_SEC_GRANTED);
2424 else
2425 BTA_DmBleSecurityGrant(*bd_addr, BTA_DM_SEC_PAIR_NOT_SPT);
2426 }
2427 } else {
2428 BTA_DmConfirm(*bd_addr, accept);
2429 }
2430 return BT_STATUS_SUCCESS;
2431 }
2432
2433 /*******************************************************************************
2434 *
2435 * Function btif_dm_get_adapter_property
2436 *
2437 * Description Queries the BTA for the adapter property
2438 *
2439 * Returns bt_status_t
2440 *
2441 ******************************************************************************/
btif_dm_get_adapter_property(bt_property_t * prop)2442 bt_status_t btif_dm_get_adapter_property(bt_property_t* prop) {
2443 BTIF_TRACE_EVENT("%s: type=0x%x", __func__, prop->type);
2444 switch (prop->type) {
2445 case BT_PROPERTY_BDNAME: {
2446 bt_bdname_t* bd_name = (bt_bdname_t*)prop->val;
2447 strncpy((char*)bd_name->name, (char*)btif_get_default_local_name(),
2448 sizeof(bd_name->name) - 1);
2449 bd_name->name[sizeof(bd_name->name) - 1] = 0;
2450 prop->len = strlen((char*)bd_name->name);
2451 } break;
2452
2453 case BT_PROPERTY_ADAPTER_SCAN_MODE: {
2454 /* if the storage does not have it. Most likely app never set it. Default
2455 * is NONE */
2456 bt_scan_mode_t* mode = (bt_scan_mode_t*)prop->val;
2457 *mode = BT_SCAN_MODE_NONE;
2458 prop->len = sizeof(bt_scan_mode_t);
2459 } break;
2460
2461 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: {
2462 uint32_t* tmt = (uint32_t*)prop->val;
2463 *tmt = 120; /* default to 120s, if not found in NV */
2464 prop->len = sizeof(uint32_t);
2465 } break;
2466
2467 default:
2468 prop->len = 0;
2469 return BT_STATUS_FAIL;
2470 }
2471 return BT_STATUS_SUCCESS;
2472 }
2473
2474 /*******************************************************************************
2475 *
2476 * Function btif_dm_get_remote_services
2477 *
2478 * Description Start SDP to get remote services
2479 *
2480 * Returns bt_status_t
2481 *
2482 ******************************************************************************/
btif_dm_get_remote_services(const RawAddress & remote_addr)2483 bt_status_t btif_dm_get_remote_services(const RawAddress& remote_addr) {
2484 BTIF_TRACE_EVENT("%s: bd_addr=%s", __func__, remote_addr.ToString().c_str());
2485
2486 BTA_DmDiscover(remote_addr, BTA_ALL_SERVICE_MASK, bte_dm_search_services_evt,
2487 true);
2488
2489 return BT_STATUS_SUCCESS;
2490 }
2491
2492 /*******************************************************************************
2493 *
2494 * Function btif_dm_get_remote_services_by_transport
2495 *
2496 * Description Start SDP to get remote services by transport
2497 *
2498 * Returns bt_status_t
2499 *
2500 ******************************************************************************/
btif_dm_get_remote_services_by_transport(RawAddress * remote_addr,const int transport)2501 bt_status_t btif_dm_get_remote_services_by_transport(RawAddress* remote_addr,
2502 const int transport) {
2503 BTIF_TRACE_EVENT("%s", __func__);
2504
2505 /* Set the mask extension */
2506 tBTA_SERVICE_MASK_EXT mask_ext;
2507 mask_ext.num_uuid = 0;
2508 mask_ext.p_uuid = NULL;
2509 mask_ext.srvc_mask = BTA_ALL_SERVICE_MASK;
2510
2511 BTA_DmDiscoverByTransport(*remote_addr, &mask_ext, bte_dm_search_services_evt,
2512 true, transport);
2513
2514 return BT_STATUS_SUCCESS;
2515 }
2516
2517 /*******************************************************************************
2518 *
2519 * Function btif_dm_get_remote_service_record
2520 *
2521 * Description Start SDP to get remote service record
2522 *
2523 *
2524 * Returns bt_status_t
2525 ******************************************************************************/
btif_dm_get_remote_service_record(RawAddress * remote_addr,bt_uuid_t * uuid)2526 bt_status_t btif_dm_get_remote_service_record(RawAddress* remote_addr,
2527 bt_uuid_t* uuid) {
2528 BTIF_TRACE_EVENT("%s: bd_addr=%s", __func__, remote_addr->ToString().c_str());
2529
2530 tSDP_UUID sdp_uuid;
2531 sdp_uuid.len = MAX_UUID_SIZE;
2532 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2533
2534 BTA_DmDiscoverUUID(*remote_addr, &sdp_uuid, bte_dm_remote_service_record_evt,
2535 true);
2536
2537 return BT_STATUS_SUCCESS;
2538 }
2539
btif_dm_execute_service_request(uint16_t event,char * p_param)2540 void btif_dm_execute_service_request(uint16_t event, char* p_param) {
2541 bool b_enable = false;
2542 bt_status_t status;
2543 if (event == BTIF_DM_ENABLE_SERVICE) {
2544 b_enable = true;
2545 }
2546 status =
2547 btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2548 if (status == BT_STATUS_SUCCESS) {
2549 bt_property_t property;
2550 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2551
2552 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2553 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2554 sizeof(local_uuids), local_uuids);
2555 btif_storage_get_adapter_property(&property);
2556 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1,
2557 &property);
2558 }
2559 return;
2560 }
2561
btif_dm_proc_io_req(UNUSED_ATTR const RawAddress & bd_addr,UNUSED_ATTR tBTA_IO_CAP * p_io_cap,UNUSED_ATTR tBTA_OOB_DATA * p_oob_data,tBTA_AUTH_REQ * p_auth_req,bool is_orig)2562 void btif_dm_proc_io_req(UNUSED_ATTR const RawAddress& bd_addr,
2563 UNUSED_ATTR tBTA_IO_CAP* p_io_cap,
2564 UNUSED_ATTR tBTA_OOB_DATA* p_oob_data,
2565 tBTA_AUTH_REQ* p_auth_req, bool is_orig) {
2566 uint8_t yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2567 /* if local initiated:
2568 ** 1. set DD + MITM
2569 ** if remote initiated:
2570 ** 1. Copy over the auth_req from peer's io_rsp
2571 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo
2572 *(iPhone)
2573 ** as a fallback set MITM+GB if peer had MITM set
2574 */
2575
2576 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __func__, *p_auth_req);
2577 if (pairing_cb.is_local_initiated) {
2578 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2579 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2580 } else if (!is_orig) {
2581 /* peer initiated paring. They probably know what they want.
2582 ** Copy the mitm from peer device.
2583 */
2584 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d", __func__,
2585 pairing_cb.auth_req);
2586 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2587
2588 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo,
2589 * force MITM */
2590 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO))
2591 *p_auth_req |= BTA_AUTH_SP_YES;
2592 } else if (yes_no_bit) {
2593 /* set the general bonding bit for stored device */
2594 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2595 }
2596 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __func__, *p_auth_req);
2597 }
2598
btif_dm_proc_io_rsp(UNUSED_ATTR const RawAddress & bd_addr,tBTA_IO_CAP io_cap,UNUSED_ATTR tBTA_OOB_DATA oob_data,tBTA_AUTH_REQ auth_req)2599 void btif_dm_proc_io_rsp(UNUSED_ATTR const RawAddress& bd_addr,
2600 tBTA_IO_CAP io_cap, UNUSED_ATTR tBTA_OOB_DATA oob_data,
2601 tBTA_AUTH_REQ auth_req) {
2602 if (auth_req & BTA_AUTH_BONDS) {
2603 BTIF_TRACE_DEBUG("%s auth_req:%d", __func__, auth_req);
2604 pairing_cb.auth_req = auth_req;
2605 pairing_cb.io_cap = io_cap;
2606 }
2607 }
2608
btif_dm_set_oob_for_io_req(tBTA_OOB_DATA * p_has_oob_data)2609 void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA* p_has_oob_data) {
2610 if (is_empty_128bit(oob_cb.oob_data.c192)) {
2611 *p_has_oob_data = false;
2612 } else {
2613 *p_has_oob_data = true;
2614 }
2615 BTIF_TRACE_DEBUG("%s: *p_has_oob_data=%d", __func__, *p_has_oob_data);
2616 }
2617
btif_dm_set_oob_for_le_io_req(const RawAddress & bd_addr,tBTA_OOB_DATA * p_has_oob_data,tBTA_LE_AUTH_REQ * p_auth_req)2618 void btif_dm_set_oob_for_le_io_req(const RawAddress& bd_addr,
2619 tBTA_OOB_DATA* p_has_oob_data,
2620 tBTA_LE_AUTH_REQ* p_auth_req) {
2621 if (!is_empty_128bit(oob_cb.oob_data.le_sc_c) &&
2622 !is_empty_128bit(oob_cb.oob_data.le_sc_r)) {
2623 /* We have LE SC OOB data */
2624
2625 /* make sure OOB data is for this particular device */
2626 if (bd_addr == oob_cb.bdaddr) {
2627 *p_auth_req = ((*p_auth_req) | BTM_LE_AUTH_REQ_SC_ONLY);
2628 *p_has_oob_data = true;
2629 } else {
2630 *p_has_oob_data = false;
2631 BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address",
2632 __func__);
2633 }
2634 } else if (!is_empty_128bit(oob_cb.oob_data.sm_tk)) {
2635 /* We have security manager TK */
2636
2637 /* make sure OOB data is for this particular device */
2638 if (bd_addr == oob_cb.bdaddr) {
2639 // When using OOB with TK, SC Secure Connections bit must be disabled.
2640 tBTA_LE_AUTH_REQ mask = ~BTM_LE_AUTH_REQ_SC_ONLY;
2641 *p_auth_req = ((*p_auth_req) & mask);
2642
2643 *p_has_oob_data = true;
2644 } else {
2645 *p_has_oob_data = false;
2646 BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address",
2647 __func__);
2648 }
2649 } else {
2650 *p_has_oob_data = false;
2651 }
2652 BTIF_TRACE_DEBUG("%s *p_has_oob_data=%d", __func__, *p_has_oob_data);
2653 }
2654
2655 #ifdef BTIF_DM_OOB_TEST
btif_dm_load_local_oob(void)2656 void btif_dm_load_local_oob(void) {
2657 char prop_oob[PROPERTY_VALUE_MAX];
2658 osi_property_get("service.brcm.bt.oob", prop_oob, "3");
2659 BTIF_TRACE_DEBUG("%s: prop_oob = %s", __func__, prop_oob);
2660 if (prop_oob[0] != '3') {
2661 if (is_empty_128bit(oob_cb.oob_data.c192)) {
2662 BTIF_TRACE_DEBUG("%s: read OOB, call BTA_DmLocalOob()", __func__);
2663 BTA_DmLocalOob();
2664 }
2665 }
2666 }
2667
btif_dm_proc_loc_oob(bool valid,BT_OCTET16 c,BT_OCTET16 r)2668 void btif_dm_proc_loc_oob(bool valid, BT_OCTET16 c, BT_OCTET16 r) {
2669 FILE* fp;
2670 const char* path_a = "/data/misc/bluedroid/LOCAL/a.key";
2671 const char* path_b = "/data/misc/bluedroid/LOCAL/b.key";
2672 const char* path = NULL;
2673 char prop_oob[PROPERTY_VALUE_MAX];
2674 BTIF_TRACE_DEBUG("%s: valid=%d", __func__, valid);
2675 if (is_empty_128bit(oob_cb.oob_data.c192) && valid) {
2676 BTIF_TRACE_DEBUG("save local OOB data in memory");
2677 memcpy(oob_cb.oob_data.c192, c, BT_OCTET16_LEN);
2678 memcpy(oob_cb.oob_data.r192, r, BT_OCTET16_LEN);
2679 osi_property_get("service.brcm.bt.oob", prop_oob, "3");
2680 BTIF_TRACE_DEBUG("%s: prop_oob = %s", __func__, prop_oob);
2681 if (prop_oob[0] == '1')
2682 path = path_a;
2683 else if (prop_oob[0] == '2')
2684 path = path_b;
2685 if (path) {
2686 fp = fopen(path, "wb+");
2687 if (fp == NULL) {
2688 BTIF_TRACE_DEBUG("%s: failed to save local OOB data to %s", __func__,
2689 path);
2690 } else {
2691 BTIF_TRACE_DEBUG("%s: save local OOB data into file %s", __func__,
2692 path);
2693 fwrite(c, 1, BT_OCTET16_LEN, fp);
2694 fwrite(r, 1, BT_OCTET16_LEN, fp);
2695 fclose(fp);
2696 }
2697 }
2698 }
2699 }
2700
2701 /*******************************************************************************
2702 *
2703 * Function btif_dm_get_smp_config
2704 *
2705 * Description Retrieve the SMP pairing options from the bt_stack.conf
2706 * file. To provide specific pairing options for the host
2707 * add a node with label "SmpOptions" to the config file
2708 * and assign it a comma separated list of 5 values in the
2709 * format: auth, io, ikey, rkey, ksize, oob
2710 * eg: PTS_SmpOptions=0xD,0x4,0xf,0xf,0x10
2711 *
2712 * Parameters: tBTE_APPL_CFG*: pointer to struct defining pairing options
2713 *
2714 * Returns true if the options were successfully read, else false
2715 *
2716 ******************************************************************************/
btif_dm_get_smp_config(tBTE_APPL_CFG * p_cfg)2717 bool btif_dm_get_smp_config(tBTE_APPL_CFG* p_cfg) {
2718 if (!stack_config_get_interface()->get_pts_smp_options()) {
2719 BTIF_TRACE_DEBUG("%s: SMP options not found in configuration", __func__);
2720 return false;
2721 }
2722
2723 char conf[64];
2724 const char* recv = stack_config_get_interface()->get_pts_smp_options();
2725 char* pch;
2726 char* endptr;
2727
2728 strncpy(conf, recv, 64);
2729 conf[63] = 0; // null terminate
2730
2731 pch = strtok(conf, ",");
2732 if (pch != NULL)
2733 p_cfg->ble_auth_req = (uint8_t)strtoul(pch, &endptr, 16);
2734 else
2735 return false;
2736
2737 pch = strtok(NULL, ",");
2738 if (pch != NULL)
2739 p_cfg->ble_io_cap = (uint8_t)strtoul(pch, &endptr, 16);
2740 else
2741 return false;
2742
2743 pch = strtok(NULL, ",");
2744 if (pch != NULL)
2745 p_cfg->ble_init_key = (uint8_t)strtoul(pch, &endptr, 16);
2746 else
2747 return false;
2748
2749 pch = strtok(NULL, ",");
2750 if (pch != NULL)
2751 p_cfg->ble_resp_key = (uint8_t)strtoul(pch, &endptr, 16);
2752 else
2753 return false;
2754
2755 pch = strtok(NULL, ",");
2756 if (pch != NULL)
2757 p_cfg->ble_max_key_size = (uint8_t)strtoul(pch, &endptr, 16);
2758 else
2759 return false;
2760
2761 return true;
2762 }
2763
btif_dm_proc_rmt_oob(const RawAddress & bd_addr,BT_OCTET16 p_c,BT_OCTET16 p_r)2764 bool btif_dm_proc_rmt_oob(const RawAddress& bd_addr, BT_OCTET16 p_c,
2765 BT_OCTET16 p_r) {
2766 const char* path_a = "/data/misc/bluedroid/LOCAL/a.key";
2767 const char* path_b = "/data/misc/bluedroid/LOCAL/b.key";
2768 const char* path = NULL;
2769 char prop_oob[PROPERTY_VALUE_MAX];
2770 osi_property_get("service.brcm.bt.oob", prop_oob, "3");
2771 BTIF_TRACE_DEBUG("%s: prop_oob = %s", __func__, prop_oob);
2772 if (prop_oob[0] == '1')
2773 path = path_b;
2774 else if (prop_oob[0] == '2')
2775 path = path_a;
2776 if (!path) {
2777 BTIF_TRACE_DEBUG("%s: can't open path!", __func__);
2778 return false;
2779 }
2780
2781 FILE* fp = fopen(path, "rb");
2782 if (fp == NULL) {
2783 BTIF_TRACE_DEBUG("%s: failed to read OOB keys from %s", __func__, path);
2784 return false;
2785 }
2786
2787 BTIF_TRACE_DEBUG("%s: read OOB data from %s", __func__, path);
2788 fread(p_c, 1, BT_OCTET16_LEN, fp);
2789 fread(p_r, 1, BT_OCTET16_LEN, fp);
2790 fclose(fp);
2791
2792 RawAddress bt_bd_addr = bd_addr;
2793 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2794 (char*)&bt_bd_addr, sizeof(RawAddress), NULL);
2795 return true;
2796 }
2797 #endif /* BTIF_DM_OOB_TEST */
2798
btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF * p_ssp_key_notif)2799 static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF* p_ssp_key_notif) {
2800 RawAddress bd_addr;
2801 bt_bdname_t bd_name;
2802 uint32_t cod;
2803 int dev_type;
2804
2805 BTIF_TRACE_DEBUG("%s", __func__);
2806
2807 /* Remote name update */
2808 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type)) {
2809 dev_type = BT_DEVICE_TYPE_BLE;
2810 }
2811 btif_dm_update_ble_remote_properties(p_ssp_key_notif->bd_addr,
2812 p_ssp_key_notif->bd_name,
2813 (tBT_DEVICE_TYPE)dev_type);
2814 bd_addr = p_ssp_key_notif->bd_addr;
2815 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2816
2817 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
2818 pairing_cb.is_ssp = false;
2819 cod = COD_UNCLASSIFIED;
2820
2821 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
2822 BT_SSP_VARIANT_PASSKEY_NOTIFICATION, p_ssp_key_notif->passkey);
2823 }
2824
2825 /*******************************************************************************
2826 *
2827 * Function btif_dm_ble_auth_cmpl_evt
2828 *
2829 * Description Executes authentication complete event in btif context
2830 *
2831 * Returns void
2832 *
2833 ******************************************************************************/
btif_dm_ble_auth_cmpl_evt(tBTA_DM_AUTH_CMPL * p_auth_cmpl)2834 static void btif_dm_ble_auth_cmpl_evt(tBTA_DM_AUTH_CMPL* p_auth_cmpl) {
2835 /* Save link key, if not temporary */
2836 bt_status_t status = BT_STATUS_FAIL;
2837 bt_bond_state_t state = BT_BOND_STATE_NONE;
2838
2839 RawAddress bd_addr = p_auth_cmpl->bd_addr;
2840
2841 /* Clear OOB data */
2842 memset(&oob_cb, 0, sizeof(oob_cb));
2843
2844 if ((p_auth_cmpl->success == true) && (p_auth_cmpl->key_present)) {
2845 /* store keys */
2846 }
2847 if (p_auth_cmpl->success) {
2848 status = BT_STATUS_SUCCESS;
2849 state = BT_BOND_STATE_BONDED;
2850 int addr_type;
2851 RawAddress bdaddr = p_auth_cmpl->bd_addr;
2852 if (btif_storage_get_remote_addr_type(&bdaddr, &addr_type) !=
2853 BT_STATUS_SUCCESS)
2854 btif_storage_set_remote_addr_type(&bdaddr, p_auth_cmpl->addr_type);
2855
2856 /* Test for temporary bonding */
2857 if (btm_get_bond_type_dev(p_auth_cmpl->bd_addr) == BOND_TYPE_TEMPORARY) {
2858 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
2859 __func__);
2860 btif_storage_remove_bonded_device(&bdaddr);
2861 state = BT_BOND_STATE_NONE;
2862 } else {
2863 btif_dm_save_ble_bonding_keys(bdaddr);
2864 BTA_GATTC_Refresh(bd_addr);
2865 btif_dm_get_remote_services_by_transport(&bd_addr, BTA_GATT_TRANSPORT_LE);
2866 }
2867 } else {
2868 /*Map the HCI fail reason to bt status */
2869 switch (p_auth_cmpl->fail_reason) {
2870 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
2871 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
2872 case BTA_DM_AUTH_SMP_UNKNOWN_ERR:
2873 case BTA_DM_AUTH_SMP_CONN_TOUT:
2874 btif_dm_remove_ble_bonding_keys();
2875 status = BT_STATUS_AUTH_FAILURE;
2876 break;
2877 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
2878 status = BT_STATUS_AUTH_REJECTED;
2879 break;
2880 default:
2881 btif_dm_remove_ble_bonding_keys();
2882 status = BT_STATUS_FAIL;
2883 break;
2884 }
2885 }
2886 if (state == BT_BOND_STATE_BONDED && bd_addr != pairing_cb.static_bdaddr) {
2887 // Report RPA bonding state to Java in crosskey paring
2888 bond_state_changed(status, bd_addr, BT_BOND_STATE_BONDING);
2889 }
2890 bond_state_changed(status, bd_addr, state);
2891 }
2892
btif_dm_load_ble_local_keys(void)2893 void btif_dm_load_ble_local_keys(void) {
2894 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2895
2896 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,
2897 (char*)&ble_local_key_cb.er[0],
2898 BT_OCTET16_LEN) == BT_STATUS_SUCCESS) {
2899 ble_local_key_cb.is_er_rcvd = true;
2900 BTIF_TRACE_DEBUG("%s BLE ER key loaded", __func__);
2901 }
2902
2903 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,
2904 (char*)&ble_local_key_cb.id_keys.ir[0],
2905 BT_OCTET16_LEN) == BT_STATUS_SUCCESS) &&
2906 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK,
2907 (char*)&ble_local_key_cb.id_keys.irk[0],
2908 BT_OCTET16_LEN) == BT_STATUS_SUCCESS) &&
2909 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,
2910 (char*)&ble_local_key_cb.id_keys.dhk[0],
2911 BT_OCTET16_LEN) == BT_STATUS_SUCCESS)) {
2912 ble_local_key_cb.is_id_keys_rcvd = true;
2913 BTIF_TRACE_DEBUG("%s BLE ID keys loaded", __func__);
2914 }
2915 }
btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK * p_key_mask,BT_OCTET16 er,tBTA_BLE_LOCAL_ID_KEYS * p_id_keys)2916 void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK* p_key_mask,
2917 BT_OCTET16 er,
2918 tBTA_BLE_LOCAL_ID_KEYS* p_id_keys) {
2919 if (ble_local_key_cb.is_er_rcvd) {
2920 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2921 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2922 }
2923
2924 if (ble_local_key_cb.is_id_keys_rcvd) {
2925 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0],
2926 sizeof(BT_OCTET16));
2927 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0],
2928 sizeof(BT_OCTET16));
2929 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0],
2930 sizeof(BT_OCTET16));
2931 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
2932 }
2933 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x", __func__, *p_key_mask);
2934 }
2935
btif_dm_save_ble_bonding_keys(RawAddress & bd_addr)2936 void btif_dm_save_ble_bonding_keys(RawAddress& bd_addr) {
2937 BTIF_TRACE_DEBUG("%s", __func__);
2938
2939 if (pairing_cb.ble.is_penc_key_rcvd) {
2940 btif_storage_add_ble_bonding_key(&bd_addr, (char*)&pairing_cb.ble.penc_key,
2941 BTIF_DM_LE_KEY_PENC,
2942 sizeof(tBTM_LE_PENC_KEYS));
2943 }
2944
2945 if (pairing_cb.ble.is_pid_key_rcvd) {
2946 btif_storage_add_ble_bonding_key(&bd_addr, (char*)&pairing_cb.ble.pid_key,
2947 BTIF_DM_LE_KEY_PID,
2948 sizeof(tBTM_LE_PID_KEYS));
2949 }
2950
2951 if (pairing_cb.ble.is_pcsrk_key_rcvd) {
2952 btif_storage_add_ble_bonding_key(&bd_addr, (char*)&pairing_cb.ble.pcsrk_key,
2953 BTIF_DM_LE_KEY_PCSRK,
2954 sizeof(tBTM_LE_PCSRK_KEYS));
2955 }
2956
2957 if (pairing_cb.ble.is_lenc_key_rcvd) {
2958 btif_storage_add_ble_bonding_key(&bd_addr, (char*)&pairing_cb.ble.lenc_key,
2959 BTIF_DM_LE_KEY_LENC,
2960 sizeof(tBTM_LE_LENC_KEYS));
2961 }
2962
2963 if (pairing_cb.ble.is_lcsrk_key_rcvd) {
2964 btif_storage_add_ble_bonding_key(&bd_addr, (char*)&pairing_cb.ble.lcsrk_key,
2965 BTIF_DM_LE_KEY_LCSRK,
2966 sizeof(tBTM_LE_LCSRK_KEYS));
2967 }
2968
2969 if (pairing_cb.ble.is_lidk_key_rcvd) {
2970 btif_storage_add_ble_bonding_key(&bd_addr, NULL, BTIF_DM_LE_KEY_LID, 0);
2971 }
2972 }
2973
btif_dm_remove_ble_bonding_keys(void)2974 void btif_dm_remove_ble_bonding_keys(void) {
2975 BTIF_TRACE_DEBUG("%s", __func__);
2976
2977 RawAddress bd_addr = pairing_cb.bd_addr;
2978 btif_storage_remove_ble_bonding_keys(&bd_addr);
2979 }
2980
2981 /*******************************************************************************
2982 *
2983 * Function btif_dm_ble_sec_req_evt
2984 *
2985 * Description Eprocess security request event in btif context
2986 *
2987 * Returns void
2988 *
2989 ******************************************************************************/
btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ * p_ble_req)2990 void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ* p_ble_req) {
2991 bt_bdname_t bd_name;
2992 uint32_t cod;
2993 int dev_type;
2994
2995 BTIF_TRACE_DEBUG("%s", __func__);
2996
2997 if (pairing_cb.state == BT_BOND_STATE_BONDING) {
2998 BTIF_TRACE_DEBUG("%s Discard security request", __func__);
2999 return;
3000 }
3001
3002 /* Remote name update */
3003 if (!btif_get_device_type(p_ble_req->bd_addr, &dev_type)) {
3004 dev_type = BT_DEVICE_TYPE_BLE;
3005 }
3006 btif_dm_update_ble_remote_properties(p_ble_req->bd_addr, p_ble_req->bd_name,
3007 (tBT_DEVICE_TYPE)dev_type);
3008
3009 RawAddress bd_addr = p_ble_req->bd_addr;
3010 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
3011
3012 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
3013
3014 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
3015 pairing_cb.is_le_only = true;
3016 pairing_cb.is_le_nc = false;
3017 pairing_cb.is_ssp = true;
3018 btm_set_bond_type_dev(p_ble_req->bd_addr, pairing_cb.bond_type);
3019
3020 cod = COD_UNCLASSIFIED;
3021
3022 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
3023 BT_SSP_VARIANT_CONSENT, 0);
3024 }
3025
3026 /*******************************************************************************
3027 *
3028 * Function btif_dm_ble_passkey_req_evt
3029 *
3030 * Description Executes pin request event in btif context
3031 *
3032 * Returns void
3033 *
3034 ******************************************************************************/
btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ * p_pin_req)3035 static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ* p_pin_req) {
3036 bt_bdname_t bd_name;
3037 uint32_t cod;
3038 int dev_type;
3039
3040 /* Remote name update */
3041 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type)) {
3042 dev_type = BT_DEVICE_TYPE_BLE;
3043 }
3044 btif_dm_update_ble_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
3045 (tBT_DEVICE_TYPE)dev_type);
3046
3047 RawAddress bd_addr = p_pin_req->bd_addr;
3048 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
3049
3050 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
3051 pairing_cb.is_le_only = true;
3052
3053 cod = COD_UNCLASSIFIED;
3054
3055 HAL_CBACK(bt_hal_cbacks, pin_request_cb, &bd_addr, &bd_name, cod, false);
3056 }
btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF * p_notif_req)3057 static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF* p_notif_req) {
3058 /* TODO implement key notification for numeric comparison */
3059 BTIF_TRACE_DEBUG("%s", __func__);
3060
3061 /* Remote name update */
3062 btif_update_remote_properties(p_notif_req->bd_addr, p_notif_req->bd_name,
3063 NULL, BT_DEVICE_TYPE_BLE);
3064
3065 RawAddress bd_addr = p_notif_req->bd_addr;
3066
3067 bt_bdname_t bd_name;
3068 memcpy(bd_name.name, p_notif_req->bd_name, BD_NAME_LEN);
3069
3070 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
3071 pairing_cb.is_ssp = false;
3072 pairing_cb.is_le_only = true;
3073 pairing_cb.is_le_nc = true;
3074
3075 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, COD_UNCLASSIFIED,
3076 BT_SSP_VARIANT_PASSKEY_CONFIRMATION, p_notif_req->passkey);
3077 }
3078
btif_dm_ble_oob_req_evt(tBTA_DM_SP_RMT_OOB * req_oob_type)3079 static void btif_dm_ble_oob_req_evt(tBTA_DM_SP_RMT_OOB* req_oob_type) {
3080 BTIF_TRACE_DEBUG("%s", __func__);
3081
3082 RawAddress bd_addr = req_oob_type->bd_addr;
3083 /* We already checked if OOB data is present in
3084 * btif_dm_set_oob_for_le_io_req, but check here again. If it's not present
3085 * do nothing, pairing will timeout.
3086 */
3087 if (is_empty_128bit(oob_cb.oob_data.sm_tk)) {
3088 return;
3089 }
3090
3091 /* make sure OOB data is for this particular device */
3092 if (req_oob_type->bd_addr != oob_cb.bdaddr) {
3093 BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address",
3094 __func__);
3095 return;
3096 }
3097
3098 /* Remote name update */
3099 btif_update_remote_properties(req_oob_type->bd_addr, req_oob_type->bd_name,
3100 NULL, BT_DEVICE_TYPE_BLE);
3101
3102 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
3103 pairing_cb.is_ssp = false;
3104 pairing_cb.is_le_only = true;
3105 pairing_cb.is_le_nc = false;
3106
3107 BTM_BleOobDataReply(req_oob_type->bd_addr, 0, 16, oob_cb.oob_data.sm_tk);
3108 }
3109
btif_dm_ble_sc_oob_req_evt(tBTA_DM_SP_RMT_OOB * req_oob_type)3110 static void btif_dm_ble_sc_oob_req_evt(tBTA_DM_SP_RMT_OOB* req_oob_type) {
3111 BTIF_TRACE_DEBUG("%s", __func__);
3112
3113 RawAddress bd_addr = req_oob_type->bd_addr;
3114
3115 /* We already checked if OOB data is present in
3116 * btif_dm_set_oob_for_le_io_req, but check here again. If it's not present
3117 * do nothing, pairing will timeout.
3118 */
3119 if (is_empty_128bit(oob_cb.oob_data.le_sc_c) &&
3120 is_empty_128bit(oob_cb.oob_data.le_sc_r)) {
3121 BTIF_TRACE_WARNING("%s: LE SC OOB data is empty", __func__);
3122 return;
3123 }
3124
3125 /* make sure OOB data is for this particular device */
3126 if (req_oob_type->bd_addr != oob_cb.bdaddr) {
3127 BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address",
3128 __func__);
3129 return;
3130 }
3131
3132 /* Remote name update */
3133 btif_update_remote_properties(req_oob_type->bd_addr, req_oob_type->bd_name,
3134 NULL, BT_DEVICE_TYPE_BLE);
3135
3136 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
3137 pairing_cb.is_ssp = false;
3138 pairing_cb.is_le_only =
3139 true; // TODO: we can derive classic pairing from this one
3140 pairing_cb.is_le_nc = false;
3141
3142 BTM_BleSecureConnectionOobDataReply(
3143 req_oob_type->bd_addr, oob_cb.oob_data.le_sc_c, oob_cb.oob_data.le_sc_r);
3144 }
3145
btif_dm_update_ble_remote_properties(const RawAddress & bd_addr,BD_NAME bd_name,tBT_DEVICE_TYPE dev_type)3146 void btif_dm_update_ble_remote_properties(const RawAddress& bd_addr,
3147 BD_NAME bd_name,
3148 tBT_DEVICE_TYPE dev_type) {
3149 btif_update_remote_properties(bd_addr, bd_name, NULL, dev_type);
3150 }
3151
btif_dm_ble_tx_test_cback(void * p)3152 static void btif_dm_ble_tx_test_cback(void* p) {
3153 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST, (char*)p, 1,
3154 NULL);
3155 }
3156
btif_dm_ble_rx_test_cback(void * p)3157 static void btif_dm_ble_rx_test_cback(void* p) {
3158 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST, (char*)p, 1,
3159 NULL);
3160 }
3161
btif_dm_ble_test_end_cback(void * p)3162 static void btif_dm_ble_test_end_cback(void* p) {
3163 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END, (char*)p,
3164 3, NULL);
3165 }
3166 /*******************************************************************************
3167 *
3168 * Function btif_le_test_mode
3169 *
3170 * Description Sends a HCI BLE Test command to the Controller
3171 *
3172 * Returns BT_STATUS_SUCCESS on success
3173 *
3174 ******************************************************************************/
btif_le_test_mode(uint16_t opcode,uint8_t * buf,uint8_t len)3175 bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len) {
3176 switch (opcode) {
3177 case HCI_BLE_TRANSMITTER_TEST:
3178 if (len != 3) return BT_STATUS_PARM_INVALID;
3179 BTM_BleTransmitterTest(buf[0], buf[1], buf[2], btif_dm_ble_tx_test_cback);
3180 break;
3181 case HCI_BLE_RECEIVER_TEST:
3182 if (len != 1) return BT_STATUS_PARM_INVALID;
3183 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
3184 break;
3185 case HCI_BLE_TEST_END:
3186 BTM_BleTestEnd((tBTM_CMPL_CB*)btif_dm_ble_test_end_cback);
3187 break;
3188 default:
3189 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __func__,
3190 opcode);
3191 return BT_STATUS_UNSUPPORTED;
3192 }
3193 return BT_STATUS_SUCCESS;
3194 }
3195
btif_dm_on_disable()3196 void btif_dm_on_disable() {
3197 /* cancel any pending pairing requests */
3198 if (is_bonding_or_sdp()) {
3199 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __func__);
3200 btif_dm_cancel_bond(&pairing_cb.bd_addr);
3201 }
3202 }
3203
3204 /*******************************************************************************
3205 *
3206 * Function btif_dm_read_energy_info
3207 *
3208 * Description Reads the energy info from controller
3209 *
3210 * Returns void
3211 *
3212 ******************************************************************************/
btif_dm_read_energy_info()3213 void btif_dm_read_energy_info() { BTA_DmBleGetEnergyInfo(bta_energy_info_cb); }
3214
btif_get_default_local_name()3215 static char* btif_get_default_local_name() {
3216 if (btif_default_local_name[0] == '\0') {
3217 int max_len = sizeof(btif_default_local_name) - 1;
3218 if (BTM_DEF_LOCAL_NAME[0] != '\0') {
3219 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
3220 } else {
3221 char prop_model[PROPERTY_VALUE_MAX];
3222 osi_property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
3223 strncpy(btif_default_local_name, prop_model, max_len);
3224 }
3225 btif_default_local_name[max_len] = '\0';
3226 }
3227 return btif_default_local_name;
3228 }
3229
btif_stats_add_bond_event(const RawAddress & bd_addr,bt_bond_function_t function,bt_bond_state_t state)3230 static void btif_stats_add_bond_event(const RawAddress& bd_addr,
3231 bt_bond_function_t function,
3232 bt_bond_state_t state) {
3233 std::unique_lock<std::mutex> lock(bond_event_lock);
3234
3235 btif_bond_event_t* event = &btif_dm_bond_events[btif_events_end_index];
3236 event->bd_addr = bd_addr;
3237 event->function = function;
3238 event->state = state;
3239 clock_gettime(CLOCK_REALTIME, &event->timestamp);
3240
3241 btif_num_bond_events++;
3242 btif_events_end_index =
3243 (btif_events_end_index + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1);
3244 if (btif_events_end_index == btif_events_start_index) {
3245 btif_events_start_index =
3246 (btif_events_start_index + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1);
3247 }
3248
3249 int type;
3250 btif_get_device_type(bd_addr, &type);
3251
3252 system_bt_osi::device_type_t device_type;
3253 switch (type) {
3254 case BT_DEVICE_TYPE_BREDR:
3255 device_type = system_bt_osi::DEVICE_TYPE_BREDR;
3256 break;
3257 case BT_DEVICE_TYPE_BLE:
3258 device_type = system_bt_osi::DEVICE_TYPE_LE;
3259 break;
3260 case BT_DEVICE_TYPE_DUMO:
3261 device_type = system_bt_osi::DEVICE_TYPE_DUMO;
3262 break;
3263 default:
3264 device_type = system_bt_osi::DEVICE_TYPE_UNKNOWN;
3265 break;
3266 }
3267
3268 uint32_t cod = get_cod(&bd_addr);
3269 uint64_t ts =
3270 event->timestamp.tv_sec * 1000 + event->timestamp.tv_nsec / 1000000;
3271 system_bt_osi::BluetoothMetricsLogger::GetInstance()->LogPairEvent(
3272 0, ts, cod, device_type);
3273 }
3274
btif_debug_bond_event_dump(int fd)3275 void btif_debug_bond_event_dump(int fd) {
3276 std::unique_lock<std::mutex> lock(bond_event_lock);
3277 dprintf(fd, "\nBond Events: \n");
3278 dprintf(fd, " Total Number of events: %zu\n", btif_num_bond_events);
3279 if (btif_num_bond_events > 0)
3280 dprintf(fd,
3281 " Time address Function State\n");
3282
3283 for (size_t i = btif_events_start_index; i != btif_events_end_index;
3284 i = (i + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1)) {
3285 btif_bond_event_t* event = &btif_dm_bond_events[i];
3286
3287 char eventtime[20];
3288 char temptime[20];
3289 struct tm* tstamp = localtime(&event->timestamp.tv_sec);
3290 strftime(temptime, sizeof(temptime), "%H:%M:%S", tstamp);
3291 snprintf(eventtime, sizeof(eventtime), "%s.%03ld", temptime,
3292 event->timestamp.tv_nsec / 1000000);
3293
3294 const char* func_name;
3295 switch (event->function) {
3296 case BTIF_DM_FUNC_CREATE_BOND:
3297 func_name = "btif_dm_create_bond";
3298 break;
3299 case BTIF_DM_FUNC_REMOVE_BOND:
3300 func_name = "btif_dm_remove_bond";
3301 break;
3302 case BTIF_DM_FUNC_BOND_STATE_CHANGED:
3303 func_name = "bond_state_changed ";
3304 break;
3305 default:
3306 func_name = "Invalid value ";
3307 break;
3308 }
3309
3310 const char* bond_state;
3311 switch (event->state) {
3312 case BT_BOND_STATE_NONE:
3313 bond_state = "BOND_STATE_NONE";
3314 break;
3315 case BT_BOND_STATE_BONDING:
3316 bond_state = "BOND_STATE_BONDING";
3317 break;
3318 case BT_BOND_STATE_BONDED:
3319 bond_state = "BOND_STATE_BONDED";
3320 break;
3321 default:
3322 bond_state = "Invalid bond state";
3323 break;
3324 }
3325
3326 dprintf(fd, " %s %s %s %s\n", eventtime,
3327 event->bd_addr.ToString().c_str(), func_name, bond_state);
3328 }
3329 }
3330