1 /******************************************************************************
3 * Copyright (C) 2009-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /************************************************************************************
23 * Description: Contains Device Management (DM) related functionality
26 ***********************************************************************************/
28 #define LOG_TAG "bt_btif_dm"
38 #include <sys/types.h>
42 #include <hardware/bluetooth.h>
45 #include "bta_gatt_api.h"
47 #include "btif_config.h"
50 #include "btif_storage.h"
51 #include "btif_util.h"
53 #include "bt_common.h"
54 #include "bta_gatt_api.h"
55 #include "device/include/interop.h"
56 #include "include/stack_config.h"
57 #include "osi/include/allocator.h"
58 #include "osi/include/log.h"
59 #include "osi/include/metrics.h"
60 #include "osi/include/properties.h"
61 #include "stack_config.h"
62 #include "stack/btm/btm_int.h"
64 /******************************************************************************
66 ******************************************************************************/
68 #define COD_MASK 0x07FF
70 #define COD_UNCLASSIFIED ((0x1F) << 8)
71 #define COD_HID_KEYBOARD 0x0540
72 #define COD_HID_POINTING 0x0580
73 #define COD_HID_COMBO 0x05C0
74 #define COD_HID_MAJOR 0x0500
75 #define COD_HID_MASK 0x0700
76 #define COD_AV_HEADSETS 0x0404
77 #define COD_AV_HANDSFREE 0x0408
78 #define COD_AV_HEADPHONES 0x0418
79 #define COD_AV_PORTABLE_AUDIO 0x041C
80 #define COD_AV_HIFI_AUDIO 0x0428
82 #define BTIF_DM_DEFAULT_INQ_MAX_RESULTS 0
83 #define BTIF_DM_DEFAULT_INQ_MAX_DURATION 10
84 #define BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING 2
86 #define NUM_TIMEOUT_RETRIES 5
88 #define PROPERTY_PRODUCT_MODEL "ro.product.model"
89 #define DEFAULT_LOCAL_NAME_MAX 31
90 #if (DEFAULT_LOCAL_NAME_MAX > BTM_MAX_LOC_BD_NAME_LEN)
91 #error "default btif local name size exceeds stack supported length"
94 #if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
95 #define BTIF_DM_INTERLEAVE_DURATION_BR_ONE 2
96 #define BTIF_DM_INTERLEAVE_DURATION_LE_ONE 2
97 #define BTIF_DM_INTERLEAVE_DURATION_BR_TWO 3
98 #define BTIF_DM_INTERLEAVE_DURATION_LE_TWO 4
101 #define ENCRYPTED_BREDR 2
102 #define ENCRYPTED_LE 4
106 bt_bond_state_t state;
107 bt_bdaddr_t static_bdaddr;
109 tBTM_BOND_TYPE bond_type;
114 UINT8 autopair_attempts;
115 UINT8 timeout_retries;
116 UINT8 is_local_initiated;
118 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
120 BOOLEAN is_le_nc; /* LE Numeric comparison */
121 btif_dm_ble_cb_t ble;
123 } btif_dm_pairing_cb_t;
127 UINT8 ir[BT_OCTET16_LEN];
128 UINT8 irk[BT_OCTET16_LEN];
129 UINT8 dhk[BT_OCTET16_LEN];
130 }btif_dm_local_key_id_t;
135 UINT8 er[BT_OCTET16_LEN];
136 BOOLEAN is_id_keys_rcvd;
137 btif_dm_local_key_id_t id_keys; /* ID kyes */
139 }btif_dm_local_key_cb_t;
145 } btif_dm_remote_name_t;
147 /* this structure holds optional OOB data for remote device */
150 BD_ADDR bdaddr; /* peer bdaddr */
151 bt_out_of_band_data_t oob_data;
157 UINT8 transport; /* 0=Unknown, 1=BR/EDR, 2=LE */
158 } btif_dm_create_bond_cb_t;
167 uint64_t energy_used;
168 } btif_activity_energy_info_cb_t;
172 unsigned int manufact_id;
177 BTIF_DM_FUNC_CREATE_BOND,
178 BTIF_DM_FUNC_CANCEL_BOND,
179 BTIF_DM_FUNC_REMOVE_BOND,
180 BTIF_DM_FUNC_BOND_STATE_CHANGED,
181 } bt_bond_function_t;
186 bt_bond_function_t function;
187 bt_bond_state_t state;
188 struct timespec timestamp;
191 #define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id))
193 #define UUID_HUMAN_INTERFACE_DEVICE "00001124-0000-1000-8000-00805f9b34fb"
195 #define MAX_BTIF_BOND_EVENT_ENTRIES 15
197 static skip_sdp_entry_t sdp_blacklist[] = {{76}}; //Apple Mouse and Keyboard
199 /* This flag will be true if HCI_Inquiry is in progress */
200 static BOOLEAN btif_dm_inquiry_in_progress = FALSE;
202 /************************************************************************************
204 ************************************************************************************/
205 static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'};
206 static uid_set_t* uid_set = NULL;
208 /* A circular array to keep track of the most recent bond events */
209 static btif_bond_event_t btif_dm_bond_events[MAX_BTIF_BOND_EVENT_ENTRIES + 1];
211 static pthread_mutex_t bond_event_lock;
213 /* |btif_num_bond_events| keeps track of the total number of events and can be
214 greater than |MAX_BTIF_BOND_EVENT_ENTRIES| */
215 static size_t btif_num_bond_events = 0;
216 static size_t btif_events_start_index = 0;
217 static size_t btif_events_end_index = 0;
219 /******************************************************************************
221 ******************************************************************************/
222 static btif_dm_pairing_cb_t pairing_cb;
223 static btif_dm_oob_cb_t oob_cb;
224 static void btif_dm_generic_evt(UINT16 event, char* p_param);
225 static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport);
226 static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name);
227 static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
228 DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type);
229 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
230 static btif_dm_local_key_cb_t ble_local_key_cb;
231 static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif);
232 static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl);
233 static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req);
234 static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF *p_notif_req) ;
235 static void btif_dm_ble_oob_req_evt(tBTA_DM_SP_RMT_OOB *req_oob_type);
236 static void btif_dm_ble_sc_oob_req_evt(tBTA_DM_SP_RMT_OOB *req_oob_type);
239 static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
240 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
241 tBTA_DM_BLE_REF_VALUE ref_value,
244 static char* btif_get_default_local_name();
246 static void btif_stats_add_bond_event(const bt_bdaddr_t *bd_addr,
247 bt_bond_function_t function,
248 bt_bond_state_t state);
250 /******************************************************************************
252 ******************************************************************************/
253 extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
254 extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
255 extern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
256 extern bt_status_t btif_av_sink_execute_service(BOOLEAN b_enable);
257 extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
258 extern bt_status_t btif_hf_client_execute_service(BOOLEAN b_enable);
259 extern bt_status_t btif_sdp_execute_service(BOOLEAN b_enable);
260 extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
261 extern void bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16);
263 /******************************************************************************
265 ******************************************************************************/
267 static bool is_empty_128bit(uint8_t *data)
269 static const uint8_t zero[16] = { 0 };
270 return !memcmp(zero, data, sizeof(zero));
273 static void btif_dm_data_copy(uint16_t event, char *dst, char *src)
275 tBTA_DM_SEC *dst_dm_sec = (tBTA_DM_SEC*)dst;
276 tBTA_DM_SEC *src_dm_sec = (tBTA_DM_SEC*)src;
282 maybe_non_aligned_memcpy(dst_dm_sec, src_dm_sec, sizeof(*src_dm_sec));
284 if (event == BTA_DM_BLE_KEY_EVT)
286 dst_dm_sec->ble_key.p_key_value = osi_malloc(sizeof(tBTM_LE_KEY_VALUE));
287 assert(src_dm_sec->ble_key.p_key_value);
288 memcpy(dst_dm_sec->ble_key.p_key_value, src_dm_sec->ble_key.p_key_value, sizeof(tBTM_LE_KEY_VALUE));
292 static void btif_dm_data_free(uint16_t event, tBTA_DM_SEC *dm_sec)
294 if (event == BTA_DM_BLE_KEY_EVT)
295 osi_free_and_reset((void **)&dm_sec->ble_key.p_key_value);
298 void btif_dm_init(uid_set_t* set)
301 pthread_mutex_init(&bond_event_lock, NULL);
304 void btif_dm_cleanup(void)
307 uid_set_destroy(uid_set);
310 pthread_mutex_destroy(&bond_event_lock);
313 bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
316 BTIF_TRACE_DEBUG("%s service_id: %d", __FUNCTION__, service_id);
317 /* Check the service_ID and invoke the profile's BT state changed API */
320 case BTA_HFP_SERVICE_ID:
321 case BTA_HSP_SERVICE_ID:
323 btif_hf_execute_service(b_enable);
325 case BTA_A2DP_SOURCE_SERVICE_ID:
327 btif_av_execute_service(b_enable);
329 case BTA_A2DP_SINK_SERVICE_ID:
331 btif_av_sink_execute_service(b_enable);
333 case BTA_HID_SERVICE_ID:
335 btif_hh_execute_service(b_enable);
337 case BTA_HFP_HS_SERVICE_ID:
339 btif_hf_client_execute_service(b_enable);
341 case BTA_SDP_SERVICE_ID:
343 btif_sdp_execute_service(b_enable);
346 BTIF_TRACE_ERROR("%s: Unknown service %d being %s",
347 __func__, service_id,
348 (b_enable) ? "enabled" : "disabled");
349 return BT_STATUS_FAIL;
351 return BT_STATUS_SUCCESS;
354 /*******************************************************************************
356 ** Function check_eir_remote_name
358 ** Description Check if remote name is in the EIR data
360 ** Returns TRUE if remote name found
361 ** Populate p_remote_name, if provided and remote name found
363 *******************************************************************************/
364 static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
365 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
367 UINT8 *p_eir_remote_name = NULL;
368 UINT8 remote_name_len = 0;
370 /* Check EIR for remote name and services */
371 if (p_search_data->inq_res.p_eir)
373 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
374 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
375 if (!p_eir_remote_name)
377 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
378 BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
381 if (p_eir_remote_name)
383 if (remote_name_len > BD_NAME_LEN)
384 remote_name_len = BD_NAME_LEN;
386 if (p_remote_name && p_remote_name_len)
388 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
389 *(p_remote_name + remote_name_len) = 0;
390 *p_remote_name_len = remote_name_len;
401 /*******************************************************************************
403 ** Function check_cached_remote_name
405 ** Description Check if remote name is in the NVRAM cache
407 ** Returns TRUE if remote name found
408 ** Populate p_remote_name, if provided and remote name found
410 *******************************************************************************/
411 static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
412 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
415 bt_bdaddr_t remote_bdaddr;
416 bt_property_t prop_name;
418 /* check if we already have it in our btif_storage cache */
419 bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
420 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
421 sizeof(bt_bdname_t), &bdname);
422 if (btif_storage_get_remote_device_property(
423 &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
425 if (p_remote_name && p_remote_name_len)
427 strcpy((char *)p_remote_name, (char *)bdname.name);
428 *p_remote_name_len = strlen((char *)p_remote_name);
436 static uint32_t get_cod(const bt_bdaddr_t *remote_bdaddr) {
438 bt_property_t prop_name;
440 /* check if we already have it in our btif_storage cache */
441 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
442 sizeof(uint32_t), &remote_cod);
443 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
445 LOG_INFO(LOG_TAG, "%s remote_cod = 0x%08x", __func__, remote_cod);
446 return remote_cod & COD_MASK;
452 BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
454 return get_cod(remote_bdaddr) == cod;
457 BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr)
459 return (get_cod(remote_bdaddr) & COD_HID_MASK) == COD_HID_MAJOR;
462 BOOLEAN check_hid_le(const bt_bdaddr_t *remote_bdaddr)
464 uint32_t remote_dev_type;
465 bt_property_t prop_name;
467 /* check if we already have it in our btif_storage cache */
468 BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE,
469 sizeof(uint32_t), &remote_dev_type);
470 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
471 &prop_name) == BT_STATUS_SUCCESS)
473 if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE)
476 bdaddr_to_string(remote_bdaddr, bdstr, sizeof(bdstr));
477 if(btif_config_exist(bdstr, "HidAppId"))
484 /*****************************************************************************
486 ** Function check_sdp_bl
488 ** Description Checks if a given device is blacklisted to skip sdp
490 ** Parameters skip_sdp_entry
492 ** Returns TRUE if the device is present in blacklist, else FALSE
494 *******************************************************************************/
495 BOOLEAN check_sdp_bl(const bt_bdaddr_t *remote_bdaddr)
497 UINT16 manufacturer = 0;
499 UINT16 lmp_subver = 0;
500 bt_property_t prop_name;
501 bt_remote_version_t info;
503 if (remote_bdaddr == NULL)
506 /* fetch additional info about remote device used in iop query */
507 BTM_ReadRemoteVersion(*(BD_ADDR*)remote_bdaddr, &lmp_ver,
508 &manufacturer, &lmp_subver);
510 /* if not available yet, try fetching from config database */
511 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO,
512 sizeof(bt_remote_version_t), &info);
514 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
515 &prop_name) != BT_STATUS_SUCCESS)
520 manufacturer = info.manufacturer;
522 for (unsigned int i = 0; i < ARRAY_SIZE(sdp_blacklist); i++)
524 if (manufacturer == sdp_blacklist[i].manufact_id)
530 static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
533 btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_BOND_STATE_CHANGED, state);
535 // Send bonding state only once - based on outgoing/incoming we may receive duplicates
536 if ((pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING))
538 // Cross key pairing so send callback for static address
539 if (!bdaddr_is_empty(&pairing_cb.static_bdaddr))
541 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
546 if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
547 state = BT_BOND_STATE_NONE;
549 BTIF_TRACE_DEBUG("%s: state=%d, prev_state=%d, sdp_attempts = %d", __func__,
550 state, pairing_cb.state, pairing_cb.sdp_attempts);
552 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
554 if (state == BT_BOND_STATE_BONDING)
556 pairing_cb.state = state;
557 bdcpy(pairing_cb.bd_addr, bd_addr->address);
559 if (!pairing_cb.sdp_attempts)
560 memset(&pairing_cb, 0, sizeof(pairing_cb));
562 BTIF_TRACE_DEBUG("%s: BR-EDR service discovery active", __func__);
566 /* store remote version in bt config to always have access
568 static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
570 bt_property_t property;
572 UINT16 lmp_subver = 0;
574 tBTM_STATUS btm_status;
575 bt_remote_version_t info;
579 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
580 &mfct_set, &lmp_subver);
582 LOG_DEBUG(LOG_TAG, "remote version info [%s]: %x, %x, %x", bdaddr_to_string(p_bd, bdstr, sizeof(bdstr)),
583 lmp_ver, mfct_set, lmp_subver);
585 if (btm_status == BTM_SUCCESS)
587 // Always update cache to ensure we have availability whenever BTM API is not populated
588 info.manufacturer = mfct_set;
589 info.sub_ver = lmp_subver;
590 info.version = lmp_ver;
591 BTIF_STORAGE_FILL_PROPERTY(&property,
592 BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
594 status = btif_storage_set_remote_device_property(p_bd, &property);
595 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
599 static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
600 DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
602 int num_properties = 0;
603 bt_property_t properties[3];
607 bt_device_type_t dev_type;
609 memset(properties, 0, sizeof(properties));
610 bdcpy(bdaddr.address, bd_addr);
613 if (strlen((const char *) bd_name))
615 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
616 BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
617 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
618 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
622 /* class of device */
623 cod = devclass2uint(dev_class);
624 BTIF_TRACE_DEBUG("%s cod is 0x%06x", __func__, cod);
626 /* Try to retrieve cod from storage */
627 BTIF_TRACE_DEBUG("%s cod is 0, checking cod from storage", __func__);
628 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
629 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
630 status = btif_storage_get_remote_device_property(&bdaddr, &properties[num_properties]);
631 BTIF_TRACE_DEBUG("%s cod retrieved from storage is 0x%06x", __func__, cod);
633 BTIF_TRACE_DEBUG("%s cod is again 0, set as unclassified", __func__);
634 cod = COD_UNCLASSIFIED;
638 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
639 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
640 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
641 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
645 bt_property_t prop_name;
646 uint8_t remote_dev_type;
647 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_TYPE_OF_DEVICE,
648 sizeof(uint8_t), &remote_dev_type);
649 if (btif_storage_get_remote_device_property(&bdaddr, &prop_name) == BT_STATUS_SUCCESS)
650 dev_type = remote_dev_type | device_type;
652 dev_type = device_type;
654 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
655 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
656 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
657 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
660 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
661 status, &bdaddr, num_properties, properties);
664 /*******************************************************************************
666 ** Function btif_dm_cb_hid_remote_name
668 ** Description Remote name callback for HID device. Called in btif context
669 ** Special handling for HID devices
673 *******************************************************************************/
674 static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
676 BTIF_TRACE_DEBUG("%s: status=%d pairing_cb.state=%d", __FUNCTION__, p_remote_name->status, pairing_cb.state);
677 if (pairing_cb.state == BT_BOND_STATE_BONDING)
679 bt_bdaddr_t remote_bd;
681 bdcpy(remote_bd.address, pairing_cb.bd_addr);
683 if (p_remote_name->status == BTM_SUCCESS)
685 bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
688 bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
692 /*******************************************************************************
694 ** Function btif_dm_cb_create_bond
696 ** Description Create bond initiated from the BTIF thread context
697 ** Special handling for HID devices
701 *******************************************************************************/
702 static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport)
704 BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
705 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
707 #if BLE_INCLUDED == TRUE
711 bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr));
712 if (transport == BT_TRANSPORT_LE)
714 if (!btif_config_get_int((char const *)&bdstr,"DevType", &device_type))
716 btif_config_set_int(bdstr, "DevType", BT_DEVICE_TYPE_BLE);
718 if (btif_storage_get_remote_addr_type(bd_addr, &addr_type) != BT_STATUS_SUCCESS)
721 // Try to read address type. OOB pairing might have set it earlier, but
722 // didn't store it, it defaults to BLE_ADDR_PUBLIC
723 uint8_t tmp_dev_type;
724 uint8_t tmp_addr_type;
725 BTM_ReadDevInfo(bd_addr->address, &tmp_dev_type, &tmp_addr_type);
726 addr_type = tmp_addr_type;
728 btif_storage_set_remote_addr_type(bd_addr, addr_type);
731 if((btif_config_get_int((char const *)&bdstr,"DevType", &device_type) &&
732 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
733 (device_type & BT_DEVICE_TYPE_BLE) == BT_DEVICE_TYPE_BLE) || (transport == BT_TRANSPORT_LE))
735 BTA_DmAddBleDevice(bd_addr->address, addr_type, device_type);
739 #if BLE_INCLUDED == TRUE
740 if(is_hid && (device_type & BT_DEVICE_TYPE_BLE) == 0)
746 status = btif_hh_connect(bd_addr);
747 if(status != BT_STATUS_SUCCESS)
748 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
752 BTA_DmBondByTransport((UINT8 *)bd_addr->address, transport);
754 /* Track originator of bond creation */
755 pairing_cb.is_local_initiated = TRUE;
759 /*******************************************************************************
761 ** Function btif_dm_cb_remove_bond
763 ** Description remove bond initiated from the BTIF thread context
764 ** Special handling for HID devices
768 *******************************************************************************/
769 void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
771 /*special handling for HID devices */
772 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there
773 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
774 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
775 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
778 BTIF_TRACE_DEBUG("%s: Removing HH device", __func__);
779 BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
783 /*******************************************************************************
785 ** Function btif_dm_get_connection_state
787 ** Description Returns whether the remote device is currently connected
788 ** and whether encryption is active for the connection
790 ** Returns 0 if not connected; 1 if connected and > 1 if connection is
793 *******************************************************************************/
794 uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr)
796 uint8_t *bda = (uint8_t*)bd_addr->address;
797 uint16_t rc = BTA_DmGetConnectionState(bda);
803 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_BR_EDR);
804 BTIF_TRACE_DEBUG("%s: security flags (BR/EDR)=0x%02x", __FUNCTION__, flags);
805 if (flags & BTM_SEC_FLAG_ENCRYPTED)
806 rc |= ENCRYPTED_BREDR;
808 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_LE);
809 BTIF_TRACE_DEBUG("%s: security flags (LE)=0x%02x", __FUNCTION__, flags);
810 if (flags & BTM_SEC_FLAG_ENCRYPTED)
817 /*******************************************************************************
819 ** Function search_devices_copy_cb
821 ** Description Deep copy callback for search devices event
825 *******************************************************************************/
826 static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
828 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
829 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
834 BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
835 maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data));
838 case BTA_DM_INQ_RES_EVT:
840 if (p_src_data->inq_res.p_eir)
842 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
843 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
848 case BTA_DM_DISC_RES_EVT:
850 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
852 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
853 memcpy(p_dest_data->disc_res.p_raw_data,
854 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
861 static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
863 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
864 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
868 maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data));
871 case BTA_DM_DISC_RES_EVT:
873 if (p_src_data->disc_res.result == BTA_SUCCESS)
875 if (p_src_data->disc_res.num_uuids > 0)
877 p_dest_data->disc_res.p_uuid_list =
878 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
879 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
880 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
881 osi_free_and_reset((void **)&p_src_data->disc_res.p_uuid_list);
883 osi_free_and_reset((void **)&p_src_data->disc_res.p_raw_data);
888 /******************************************************************************
890 ** BTIF DM callback events
892 *****************************************************************************/
894 /*******************************************************************************
896 ** Function btif_dm_pin_req_evt
898 ** Description Executes pin request event in btif context
902 *******************************************************************************/
903 static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
908 bt_pin_code_t pin_code;
911 /* Remote properties update */
912 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
914 dev_type = BT_DEVICE_TYPE_BREDR;
916 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
917 p_pin_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
919 bdcpy(bd_addr.address, p_pin_req->bd_addr);
920 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
922 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
924 cod = devclass2uint(p_pin_req->dev_class);
927 BTIF_TRACE_DEBUG("%s cod is 0, set as unclassified", __func__);
928 cod = COD_UNCLASSIFIED;
931 /* check for auto pair possiblity only if bond was initiated by local device */
932 if (pairing_cb.is_local_initiated && (p_pin_req->min_16_digit == FALSE))
934 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
935 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
936 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
937 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
938 check_cod(&bd_addr, COD_HID_POINTING))
940 /* Check if this device can be auto paired */
941 if (!interop_match_addr(INTEROP_DISABLE_AUTO_PAIRING, &bd_addr) &&
942 !interop_match_name(INTEROP_DISABLE_AUTO_PAIRING, (const char *)bd_name.name) &&
943 (pairing_cb.autopair_attempts == 0))
945 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
946 pin_code.pin[0] = 0x30;
947 pin_code.pin[1] = 0x30;
948 pin_code.pin[2] = 0x30;
949 pin_code.pin[3] = 0x30;
951 pairing_cb.autopair_attempts++;
952 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
956 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
957 check_cod(&bd_addr, COD_HID_COMBO))
959 if ((interop_match_addr(INTEROP_KEYBOARD_REQUIRES_FIXED_PIN, &bd_addr) == TRUE) &&
960 (pairing_cb.autopair_attempts == 0))
962 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
963 pin_code.pin[0] = 0x30;
964 pin_code.pin[1] = 0x30;
965 pin_code.pin[2] = 0x30;
966 pin_code.pin[3] = 0x30;
968 pairing_cb.autopair_attempts++;
969 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
974 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
975 &bd_addr, &bd_name, cod, p_pin_req->min_16_digit);
978 /*******************************************************************************
980 ** Function btif_dm_ssp_cfm_req_evt
982 ** Description Executes SSP confirm request event in btif context
986 *******************************************************************************/
987 static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
994 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
996 /* Remote properties update */
997 if (!btif_get_device_type(p_ssp_cfm_req->bd_addr, &dev_type))
999 dev_type = BT_DEVICE_TYPE_BREDR;
1001 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
1002 p_ssp_cfm_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
1004 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
1005 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
1007 /* Set the pairing_cb based on the local & remote authentication requirements */
1008 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1010 /* if just_works and bonding bit is not set treat this as temporary */
1011 if (p_ssp_cfm_req->just_works && !(p_ssp_cfm_req->loc_auth_req & BTM_AUTH_BONDS) &&
1012 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
1013 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
1014 pairing_cb.bond_type = BOND_TYPE_TEMPORARY;
1016 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
1018 btm_set_bond_type_dev(p_ssp_cfm_req->bd_addr, pairing_cb.bond_type);
1020 pairing_cb.is_ssp = TRUE;
1022 cod = devclass2uint(p_ssp_cfm_req->dev_class);
1025 LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__);
1026 cod = COD_UNCLASSIFIED;
1029 pairing_cb.sdp_attempts = 0;
1030 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
1031 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
1032 p_ssp_cfm_req->num_val);
1035 static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
1037 bt_bdaddr_t bd_addr;
1038 bt_bdname_t bd_name;
1042 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
1044 /* Remote properties update */
1045 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
1047 dev_type = BT_DEVICE_TYPE_BREDR;
1049 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
1050 p_ssp_key_notif->dev_class, (tBT_DEVICE_TYPE) dev_type);
1052 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
1053 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
1055 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1056 pairing_cb.is_ssp = TRUE;
1057 cod = devclass2uint(p_ssp_key_notif->dev_class);
1060 LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__);
1061 cod = COD_UNCLASSIFIED;
1064 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
1065 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
1066 p_ssp_key_notif->passkey);
1068 /*******************************************************************************
1070 ** Function btif_dm_auth_cmpl_evt
1072 ** Description Executes authentication complete event in btif context
1076 *******************************************************************************/
1077 static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
1079 /* Save link key, if not temporary */
1080 bt_bdaddr_t bd_addr;
1081 bt_status_t status = BT_STATUS_FAIL;
1082 bt_bond_state_t state = BT_BOND_STATE_NONE;
1083 BOOLEAN skip_sdp = FALSE;
1085 BTIF_TRACE_DEBUG("%s: bond state=%d", __func__, pairing_cb.state);
1087 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
1088 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
1090 if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) ||
1091 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
1092 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) ||
1093 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB_P_256) ||
1094 pairing_cb.bond_type == BOND_TYPE_PERSISTENT)
1097 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, bond_type=%d",
1098 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1099 ret = btif_storage_add_bonded_device(&bd_addr,
1100 p_auth_cmpl->key, p_auth_cmpl->key_type,
1101 pairing_cb.pin_code_len);
1102 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
1106 BTIF_TRACE_DEBUG("%s: Temporary key. Not storing. key_type=0x%x, bond_type=%d",
1107 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1108 if(pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
1110 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
1112 btif_storage_remove_bonded_device(&bd_addr);
1113 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1119 // We could have received a new link key without going through the pairing flow.
1120 // If so, we don't want to perform SDP or any other operations on the authenticated
1122 if (bdcmp(p_auth_cmpl->bd_addr, pairing_cb.bd_addr) != 0) {
1124 bt_bdaddr_t bt_bdaddr;
1126 memcpy(bt_bdaddr.address, p_auth_cmpl->bd_addr,
1127 sizeof(bt_bdaddr.address));
1128 bdaddr_to_string(&bt_bdaddr, address, sizeof(address));
1129 LOG_INFO(LOG_TAG, "%s skipping SDP since we did not initiate pairing to %s.", __func__, address);
1133 // Skip SDP for certain HID Devices
1134 if (p_auth_cmpl->success)
1136 #if BLE_INCLUDED == TRUE
1137 btif_storage_set_remote_addr_type(&bd_addr, p_auth_cmpl->addr_type);
1139 btif_update_remote_properties(p_auth_cmpl->bd_addr,
1140 p_auth_cmpl->bd_name, NULL, p_auth_cmpl->dev_type);
1141 pairing_cb.timeout_retries = 0;
1142 status = BT_STATUS_SUCCESS;
1143 state = BT_BOND_STATE_BONDED;
1144 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
1146 if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr))
1148 LOG_WARN(LOG_TAG, "%s:skip SDP", __FUNCTION__);
1151 if(!pairing_cb.is_local_initiated && skip_sdp)
1153 bond_state_changed(status, &bd_addr, state);
1155 LOG_WARN(LOG_TAG, "%s: Incoming HID Connection",__FUNCTION__);
1157 bt_bdaddr_t bd_addr;
1159 char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE;
1161 string_to_uuid(uuid_str, &uuid);
1163 prop.type = BT_PROPERTY_UUIDS;
1165 prop.len = MAX_UUID_SIZE;
1167 /* Send the event to the BTIF */
1168 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1169 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1173 #if BLE_INCLUDED == TRUE
1174 BOOLEAN is_crosskey = FALSE;
1175 /* If bonded due to cross-key, save the static address too*/
1176 if(pairing_cb.state == BT_BOND_STATE_BONDING &&
1177 (bdcmp(p_auth_cmpl->bd_addr, pairing_cb.bd_addr) != 0))
1179 BTIF_TRACE_DEBUG("%s: bonding initiated due to cross key, adding static address",
1181 bdcpy(pairing_cb.static_bdaddr.address, p_auth_cmpl->bd_addr);
1184 if (!is_crosskey || !(stack_config_get_interface()->get_pts_crosskey_sdp_disable()))
1188 // Ensure inquiry is stopped before attempting service discovery
1189 btif_dm_cancel_discovery();
1191 /* Trigger SDP on the device */
1192 pairing_cb.sdp_attempts = 1;
1193 btif_dm_get_remote_services(&bd_addr);
1194 #if BLE_INCLUDED == TRUE
1198 // Do not call bond_state_changed_cb yet. Wait until remote service discovery is complete
1202 // Map the HCI fail reason to bt status
1203 switch(p_auth_cmpl->fail_reason)
1205 case HCI_ERR_PAGE_TIMEOUT:
1206 if (interop_match_addr(INTEROP_AUTO_RETRY_PAIRING, &bd_addr)
1207 && pairing_cb.timeout_retries)
1209 BTIF_TRACE_WARNING("%s() - Pairing timeout; retrying (%d) ...", __FUNCTION__, pairing_cb.timeout_retries);
1210 --pairing_cb.timeout_retries;
1211 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
1215 case HCI_ERR_CONNECTION_TOUT:
1216 status = BT_STATUS_RMT_DEV_DOWN;
1219 case HCI_ERR_PAIRING_NOT_ALLOWED:
1220 status = BT_STATUS_AUTH_REJECTED;
1223 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
1224 status = BT_STATUS_AUTH_FAILURE;
1227 /* map the auth failure codes, so we can retry pairing if necessary */
1228 case HCI_ERR_AUTH_FAILURE:
1229 case HCI_ERR_KEY_MISSING:
1230 btif_storage_remove_bonded_device(&bd_addr);
1231 case HCI_ERR_HOST_REJECT_SECURITY:
1232 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
1233 case HCI_ERR_UNIT_KEY_USED:
1234 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
1235 case HCI_ERR_INSUFFCIENT_SECURITY:
1236 case HCI_ERR_PEER_USER:
1237 case HCI_ERR_UNSPECIFIED:
1238 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d",
1239 __FUNCTION__, p_auth_cmpl->fail_reason);
1240 if (pairing_cb.autopair_attempts == 1)
1242 /* Create the Bond once again */
1243 BTIF_TRACE_WARNING("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
1244 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
1249 /* if autopair attempts are more than 1, or not attempted */
1250 status = BT_STATUS_AUTH_FAILURE;
1255 status = BT_STATUS_FAIL;
1257 /* Special Handling for HID Devices */
1258 if (check_cod(&bd_addr, COD_HID_POINTING)) {
1259 /* Remove Device as bonded in nvram as authentication failed */
1260 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __FUNCTION__);
1261 btif_storage_remove_bonded_device(&bd_addr);
1263 bond_state_changed(status, &bd_addr, state);
1267 /******************************************************************************
1269 ** Function btif_dm_search_devices_evt
1271 ** Description Executes search devices callback events in btif context
1275 ******************************************************************************/
1276 static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
1278 tBTA_DM_SEARCH *p_search_data;
1279 BTIF_TRACE_EVENT("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
1283 case BTA_DM_DISC_RES_EVT:
1285 p_search_data = (tBTA_DM_SEARCH *)p_param;
1286 /* Remote name update */
1287 if (strlen((const char *) p_search_data->disc_res.bd_name))
1289 bt_property_t properties[1];
1293 properties[0].type = BT_PROPERTY_BDNAME;
1294 properties[0].val = p_search_data->disc_res.bd_name;
1295 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
1296 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
1298 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
1299 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
1300 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1301 status, &bdaddr, 1, properties);
1303 /* TODO: Services? */
1307 case BTA_DM_INQ_RES_EVT:
1309 /* inquiry result */
1313 UINT8 remote_name_len;
1314 tBTA_SERVICE_MASK services = 0;
1317 p_search_data = (tBTA_DM_SEARCH *)p_param;
1318 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
1320 BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __FUNCTION__, bdaddr_to_string(&bdaddr, bdstr, sizeof(bdstr)),
1321 #if (BLE_INCLUDED == TRUE)
1322 p_search_data->inq_res.device_type);
1324 BT_DEVICE_TYPE_BREDR);
1328 cod = devclass2uint (p_search_data->inq_res.dev_class);
1331 LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__);
1332 cod = COD_UNCLASSIFIED;
1335 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1336 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1338 /* Check EIR for remote name and services */
1339 if (p_search_data->inq_res.p_eir)
1341 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
1342 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
1343 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */
1347 bt_property_t properties[5];
1348 bt_device_type_t dev_type;
1349 uint32_t num_properties = 0;
1353 memset(properties, 0, sizeof(properties));
1355 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1356 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1359 /* Don't send BDNAME if it is empty */
1362 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1364 strlen((char *)bdname.name), &bdname);
1369 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1370 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1373 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1374 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1376 /* Verify if the device is dual mode in NVRAM */
1377 int stored_device_type = 0;
1378 if (btif_get_device_type(bdaddr.address, &stored_device_type) &&
1379 ((stored_device_type != BT_DEVICE_TYPE_BREDR &&
1380 p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BREDR) ||
1381 (stored_device_type != BT_DEVICE_TYPE_BLE &&
1382 p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE))) {
1383 dev_type = BT_DEVICE_TYPE_DUMO;
1385 dev_type = p_search_data->inq_res.device_type;
1388 if (p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE)
1389 addr_type = p_search_data->inq_res.ble_addr_type;
1391 dev_type = BT_DEVICE_TYPE_BREDR;
1393 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1394 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1397 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1398 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1399 &(p_search_data->inq_res.rssi));
1402 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1403 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
1404 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1405 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
1406 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1408 /* Callback to notify upper layer of device */
1409 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1410 num_properties, properties);
1415 case BTA_DM_INQ_CMPL_EVT:
1417 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1418 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1419 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1420 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1421 bte_scan_filt_param_cfg_evt, 0);
1425 case BTA_DM_DISC_CMPL_EVT:
1427 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1430 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1432 /* if inquiry is not in progress and we get a cancel event, then
1433 * it means we are done with inquiry, but remote_name fetches are in
1436 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt
1437 * but instead wait for the cancel_cmpl_evt via the Busy Level
1440 if (btif_dm_inquiry_in_progress == FALSE)
1442 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1443 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1444 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1445 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1446 bte_scan_filt_param_cfg_evt, 0);
1448 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1455 /*******************************************************************************
1457 ** Function btif_dm_search_services_evt
1459 ** Description Executes search services event in btif context
1463 *******************************************************************************/
1464 static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1466 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1468 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
1471 case BTA_DM_DISC_RES_EVT:
1475 bt_bdaddr_t bd_addr;
1478 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1480 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
1481 p_data->disc_res.result, p_data->disc_res.services);
1482 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1483 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1484 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1486 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__);
1487 pairing_cb.sdp_attempts++;
1488 btif_dm_get_remote_services(&bd_addr);
1491 prop.type = BT_PROPERTY_UUIDS;
1493 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1495 prop.val = p_data->disc_res.p_uuid_list;
1496 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1497 for (i=0; i < p_data->disc_res.num_uuids; i++)
1500 uuid_to_string_legacy((bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i*MAX_UUID_SIZE)), temp);
1501 LOG_INFO(LOG_TAG, "%s index:%d uuid:%s", __func__, i, temp);
1505 /* onUuidChanged requires getBondedDevices to be populated.
1506 ** bond_state_changed needs to be sent prior to remote_device_property
1508 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1509 ((bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0) ||
1510 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr.address) == 0)) &&
1511 pairing_cb.sdp_attempts > 0)
1513 BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
1515 pairing_cb.sdp_attempts = 0;
1517 // If bonding occured due to cross-key pairing, send bonding callback
1518 // for static address now
1519 if (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr.address) == 0)
1520 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1522 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1525 if (p_data->disc_res.num_uuids != 0)
1527 /* Also write this to the NVRAM */
1528 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1529 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1530 /* Send the event to the BTIF */
1531 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1532 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1537 case BTA_DM_DISC_CMPL_EVT:
1541 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1545 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1546 case BTA_DM_DISC_BLE_RES_EVT:
1547 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__,
1548 p_data->disc_ble_res.service.uu.uuid16);
1552 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1554 BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__);
1556 bt_bdaddr_t bd_addr;
1560 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1564 unsigned char c = uuid.uu[j];
1565 uuid.uu[j] = uuid.uu[i];
1571 uuid_to_string_legacy(&uuid, temp);
1572 LOG_INFO(LOG_TAG, "%s uuid:%s", __func__, temp);
1574 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1575 prop.type = BT_PROPERTY_UUIDS;
1577 prop.len = MAX_UUID_SIZE;
1579 /* Also write this to the NVRAM */
1580 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1581 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1583 /* Send the event to the BTIF */
1584 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1585 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1589 #endif /* BLE_INCLUDED */
1593 ASSERTC(0, "unhandled search services event", event);
1599 /*******************************************************************************
1601 ** Function btif_dm_remote_service_record_evt
1603 ** Description Executes search service record event in btif context
1607 *******************************************************************************/
1608 static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1610 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1612 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
1615 case BTA_DM_DISC_RES_EVT:
1617 bt_service_record_t rec;
1619 bt_bdaddr_t bd_addr;
1621 memset(&rec, 0, sizeof(bt_service_record_t));
1622 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1624 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
1625 p_data->disc_res.result, p_data->disc_res.services);
1626 prop.type = BT_PROPERTY_SERVICE_RECORD;
1627 prop.val = (void*)&rec;
1628 prop.len = sizeof(rec);
1630 /* disc_res.result is overloaded with SCN. Cannot check result */
1631 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1632 /* TODO: Get the UUID as well */
1633 rec.channel = p_data->disc_res.result - 3;
1634 /* TODO: Need to get the service name using p_raw_data */
1637 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1638 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1644 ASSERTC(0, "unhandled remote service record event", event);
1650 /*******************************************************************************
1652 ** Function btif_dm_upstreams_cback
1654 ** Description Executes UPSTREAMS events in btif context
1658 *******************************************************************************/
1659 static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1661 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1662 tBTA_SERVICE_MASK service_mask;
1664 bt_bdaddr_t bd_addr;
1666 BTIF_TRACE_EVENT("%s: ev: %s", __func__, dump_dm_event(event));
1670 case BTA_DM_ENABLE_EVT:
1675 prop.type = BT_PROPERTY_BDNAME;
1676 prop.len = BD_NAME_LEN;
1677 prop.val = (void*)bdname;
1679 status = btif_storage_get_adapter_property(&prop);
1680 if (status == BT_STATUS_SUCCESS)
1682 /* A name exists in the storage. Make this the device name */
1683 BTA_DmSetDeviceName((char*)prop.val);
1687 /* Storage does not have a name yet.
1688 * Use the default name and write it to the chip
1690 BTA_DmSetDeviceName(btif_get_default_local_name());
1693 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1694 /* Enable local privacy */
1695 BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED);
1698 /* for each of the enabled services in the mask, trigger the profile
1700 service_mask = btif_get_enabled_services_mask();
1701 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1704 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1706 btif_in_execute_service_request(i, TRUE);
1709 /* clear control blocks */
1710 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1711 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
1713 /* This function will also trigger the adapter_properties_cb
1714 ** and bonded_devices_info_cb
1716 btif_storage_load_bonded_devices();
1718 btif_enable_bluetooth_evt(p_data->enable.status);
1722 case BTA_DM_DISABLE_EVT:
1723 /* for each of the enabled services in the mask, trigger the profile
1725 service_mask = btif_get_enabled_services_mask();
1726 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1729 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1731 btif_in_execute_service_request(i, FALSE);
1734 btif_disable_bluetooth_evt();
1737 case BTA_DM_PIN_REQ_EVT:
1738 btif_dm_pin_req_evt(&p_data->pin_req);
1741 case BTA_DM_AUTH_CMPL_EVT:
1742 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1745 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1746 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1748 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1749 btm_set_bond_type_dev(pairing_cb.bd_addr, BOND_TYPE_UNKNOWN);
1750 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1754 case BTA_DM_SP_CFM_REQ_EVT:
1755 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1757 case BTA_DM_SP_KEY_NOTIF_EVT:
1758 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1761 case BTA_DM_DEV_UNPAIRED_EVT:
1762 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1763 btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);
1765 /*special handling for HID devices */
1766 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
1767 btif_hh_remove_device(bd_addr);
1769 btif_storage_remove_bonded_device(&bd_addr);
1770 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1773 case BTA_DM_BUSY_LEVEL_EVT:
1776 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
1778 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
1780 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1781 BT_DISCOVERY_STARTED);
1782 btif_dm_inquiry_in_progress = TRUE;
1784 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
1786 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1787 BT_DISCOVERY_STOPPED);
1788 btif_dm_inquiry_in_progress = FALSE;
1790 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
1792 btif_dm_inquiry_in_progress = FALSE;
1797 case BTA_DM_LINK_UP_EVT:
1798 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
1799 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
1801 btif_update_remote_version_property(&bd_addr);
1803 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1804 &bd_addr, BT_ACL_STATE_CONNECTED);
1807 case BTA_DM_LINK_DOWN_EVT:
1808 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1809 btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);
1810 BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
1811 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1812 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1815 case BTA_DM_HW_ERROR_EVT:
1816 BTIF_TRACE_ERROR("Received H/W Error. ");
1817 /* Flush storage data */
1818 btif_config_flush();
1819 usleep(100000); /* 100milliseconds */
1820 /* Killing the process to force a restart as part of fault tolerance */
1821 kill(getpid(), SIGKILL);
1824 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1825 case BTA_DM_BLE_KEY_EVT:
1826 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT key_type=0x%02x ", p_data->ble_key.key_type);
1828 /* If this pairing is by-product of local initiated GATT client Read or Write,
1829 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1830 have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1831 if (pairing_cb.state != BT_BOND_STATE_BONDING)
1833 BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now");
1834 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1835 BT_BOND_STATE_BONDING);
1837 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1839 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
1843 switch (p_data->ble_key.key_type)
1845 case BTA_LE_KEY_PENC:
1846 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
1847 pairing_cb.ble.is_penc_key_rcvd = TRUE;
1848 pairing_cb.ble.penc_key = p_data->ble_key.p_key_value->penc_key;
1851 case BTA_LE_KEY_PID:
1852 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
1853 pairing_cb.ble.is_pid_key_rcvd = TRUE;
1854 pairing_cb.ble.pid_key = p_data->ble_key.p_key_value->pid_key;
1857 case BTA_LE_KEY_PCSRK:
1858 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
1859 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1860 pairing_cb.ble.pcsrk_key = p_data->ble_key.p_key_value->pcsrk_key;
1863 case BTA_LE_KEY_LENC:
1864 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
1865 pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1866 pairing_cb.ble.lenc_key = p_data->ble_key.p_key_value->lenc_key;
1869 case BTA_LE_KEY_LCSRK:
1870 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
1871 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1872 pairing_cb.ble.lcsrk_key = p_data->ble_key.p_key_value->lcsrk_key;
1875 case BTA_LE_KEY_LID:
1876 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LID");
1877 pairing_cb.ble.is_lidk_key_rcvd = TRUE;
1881 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
1885 case BTA_DM_BLE_SEC_REQ_EVT:
1886 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
1887 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1889 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
1890 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
1891 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1893 case BTA_DM_BLE_PASSKEY_REQ_EVT:
1894 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
1895 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1897 case BTA_DM_BLE_NC_REQ_EVT:
1898 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
1899 btif_dm_ble_key_nc_req_evt(&p_data->key_notif);
1901 case BTA_DM_BLE_OOB_REQ_EVT:
1902 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
1903 btif_dm_ble_oob_req_evt(&p_data->rmt_oob);
1905 case BTA_DM_BLE_SC_OOB_REQ_EVT:
1906 BTIF_TRACE_DEBUG("BTA_DM_BLE_SC_OOB_REQ_EVT. ");
1907 btif_dm_ble_sc_oob_req_evt(&p_data->rmt_oob);
1909 case BTA_DM_BLE_LOCAL_IR_EVT:
1910 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
1911 ble_local_key_cb.is_id_keys_rcvd = TRUE;
1912 memcpy(&ble_local_key_cb.id_keys.irk[0],
1913 &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1914 memcpy(&ble_local_key_cb.id_keys.ir[0],
1915 &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1916 memcpy(&ble_local_key_cb.id_keys.dhk[0],
1917 &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1918 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1919 BTIF_DM_LE_LOCAL_KEY_IRK,
1921 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1922 BTIF_DM_LE_LOCAL_KEY_IR,
1924 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1925 BTIF_DM_LE_LOCAL_KEY_DHK,
1928 case BTA_DM_BLE_LOCAL_ER_EVT:
1929 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
1930 ble_local_key_cb.is_er_rcvd = TRUE;
1931 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1932 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1933 BTIF_DM_LE_LOCAL_KEY_ER,
1937 case BTA_DM_BLE_AUTH_CMPL_EVT:
1938 BTIF_TRACE_DEBUG("BTA_DM_BLE_AUTH_CMPL_EVT. ");
1939 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1942 case BTA_DM_LE_FEATURES_READ:
1944 tBTM_BLE_VSC_CB cmn_vsc_cb;
1945 bt_local_le_features_t local_le_features;
1948 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1949 prop.val = (void*)buf;
1950 prop.len = sizeof(buf);
1952 /* LE features are not stored in storage. Should be retrived from stack */
1953 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1954 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1956 prop.len = sizeof (bt_local_le_features_t);
1957 if (cmn_vsc_cb.filter_support == 1)
1958 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1960 local_le_features.max_adv_filter_supported = 0;
1961 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1962 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1963 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
1964 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
1965 local_le_features.scan_result_storage_size = cmn_vsc_cb.tot_scan_results_strg;
1966 local_le_features.version_supported = cmn_vsc_cb.version_supported;
1967 local_le_features.total_trackable_advertisers =
1968 cmn_vsc_cb.total_trackable_advertisers;
1970 local_le_features.extended_scan_support = cmn_vsc_cb.extended_scan_support > 0;
1971 local_le_features.debug_logging_supported = cmn_vsc_cb.debug_logging_supported > 0;
1973 memcpy(prop.val, &local_le_features, prop.len);
1974 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
1978 case BTA_DM_ENER_INFO_READ:
1980 btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param;
1981 bt_activity_energy_info energy_info;
1982 energy_info.status = p_ener_data->status;
1983 energy_info.ctrl_state = p_ener_data->ctrl_state;
1984 energy_info.rx_time = p_ener_data->rx_time;
1985 energy_info.tx_time = p_ener_data->tx_time;
1986 energy_info.idle_time = p_ener_data->idle_time;
1987 energy_info.energy_used = p_ener_data->energy_used;
1989 bt_uid_traffic_t* data = uid_set_read_and_clear(uid_set);
1990 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info, data);
1996 case BTA_DM_AUTHORIZE_EVT:
1997 case BTA_DM_SIG_STRENGTH_EVT:
1998 case BTA_DM_SP_RMT_OOB_EVT:
1999 case BTA_DM_SP_KEYPRESS_EVT:
2000 case BTA_DM_ROLE_CHG_EVT:
2003 BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event );
2007 btif_dm_data_free(event, p_data);
2010 /*******************************************************************************
2012 ** Function btif_dm_generic_evt
2014 ** Description Executes non-BTA upstream events in BTIF context
2018 *******************************************************************************/
2019 static void btif_dm_generic_evt(UINT16 event, char* p_param)
2021 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
2024 case BTIF_DM_CB_DISCOVERY_STARTED:
2026 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
2030 case BTIF_DM_CB_CREATE_BOND:
2032 pairing_cb.timeout_retries = NUM_TIMEOUT_RETRIES;
2033 btif_dm_create_bond_cb_t *create_bond_cb = (btif_dm_create_bond_cb_t*)p_param;
2034 btif_dm_cb_create_bond(&create_bond_cb->bdaddr, create_bond_cb->transport);
2038 case BTIF_DM_CB_REMOVE_BOND:
2040 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
2044 case BTIF_DM_CB_HID_REMOTE_NAME:
2046 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
2050 case BTIF_DM_CB_BOND_STATE_BONDING:
2052 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
2055 case BTIF_DM_CB_LE_TX_TEST:
2056 case BTIF_DM_CB_LE_RX_TEST:
2059 STREAM_TO_UINT8(status, p_param);
2060 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2061 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
2064 case BTIF_DM_CB_LE_TEST_END:
2068 STREAM_TO_UINT8(status, p_param);
2070 STREAM_TO_UINT16(count, p_param);
2071 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2072 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
2077 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
2083 /*******************************************************************************
2085 ** Function bte_dm_evt
2087 ** Description Switches context from BTE to BTIF for all DM events
2091 *******************************************************************************/
2093 void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
2095 /* switch context to btif task context (copy full union size for convenience) */
2096 bt_status_t status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event,
2097 (void*)p_data, sizeof(tBTA_DM_SEC), btif_dm_data_copy);
2099 /* catch any failed context transfers */
2100 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
2103 /*******************************************************************************
2105 ** Function bte_search_devices_evt
2107 ** Description Switches context from BTE to BTIF for DM search events
2111 *******************************************************************************/
2112 static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2114 UINT16 param_len = 0;
2117 param_len += sizeof(tBTA_DM_SEARCH);
2118 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
2121 case BTA_DM_INQ_RES_EVT:
2123 if (p_data->inq_res.p_eir)
2124 param_len += HCI_EXT_INQ_RESPONSE_LEN;
2128 case BTA_DM_DISC_RES_EVT:
2130 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
2131 param_len += p_data->disc_res.raw_data_size;
2135 BTIF_TRACE_DEBUG("%s event=%s param_len=%d", __FUNCTION__, dump_dm_search_event(event), param_len);
2137 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
2138 if (event == BTA_DM_INQ_RES_EVT)
2139 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
2141 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
2142 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
2145 /*******************************************************************************
2147 ** Function bte_dm_search_services_evt
2149 ** Description Switches context from BTE to BTIF for DM search services
2154 *******************************************************************************/
2155 static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2157 UINT16 param_len = 0;
2159 param_len += sizeof(tBTA_DM_SEARCH);
2162 case BTA_DM_DISC_RES_EVT:
2164 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
2165 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
2169 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
2170 * if raw_data is needed. */
2171 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
2172 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
2175 /*******************************************************************************
2177 ** Function bte_dm_remote_service_record_evt
2179 ** Description Switches context from BTE to BTIF for DM search service
2184 *******************************************************************************/
2185 static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2187 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
2188 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
2191 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2192 /*******************************************************************************
2194 ** Function bta_energy_info_cb
2196 ** Description Switches context from BTE to BTIF for DM energy info event
2200 *******************************************************************************/
2201 static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time,
2202 tBTA_DM_BLE_IDLE_TIME_MS idle_time,
2203 tBTA_DM_BLE_ENERGY_USED energy_used,
2204 tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status)
2206 BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld",
2207 status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
2209 btif_activity_energy_info_cb_t btif_cb;
2210 btif_cb.status = status;
2211 btif_cb.ctrl_state = ctrl_state;
2212 btif_cb.tx_time = (uint64_t) tx_time;
2213 btif_cb.rx_time = (uint64_t) rx_time;
2214 btif_cb.idle_time =(uint64_t) idle_time;
2215 btif_cb.energy_used =(uint64_t) energy_used;
2216 btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
2217 (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL);
2221 /*******************************************************************************
2223 ** Function bte_scan_filt_param_cfg_evt
2225 ** Description Scan filter param config event
2229 *******************************************************************************/
2230 static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
2231 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
2232 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
2234 /* This event occurs on calling BTA_DmBleCfgFilterCondition internally,
2235 ** and that is why there is no HAL callback
2237 if(BTA_SUCCESS != status)
2239 BTIF_TRACE_ERROR("%s, %d", __FUNCTION__, status);
2243 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2247 /*****************************************************************************
2249 ** btif api functions (no context switch)
2251 *****************************************************************************/
2253 /*******************************************************************************
2255 ** Function btif_dm_start_discovery
2257 ** Description Start device discovery/inquiry
2259 ** Returns bt_status_t
2261 *******************************************************************************/
2262 bt_status_t btif_dm_start_discovery(void)
2264 tBTA_DM_INQ inq_params;
2265 tBTA_SERVICE_MASK services = 0;
2266 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
2268 BTIF_TRACE_EVENT("%s", __FUNCTION__);
2270 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2271 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
2272 /* Cleanup anything remaining on index 0 */
2273 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
2274 bte_scan_filt_param_cfg_evt, 0);
2276 /* Add an allow-all filter on index 0*/
2277 adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE;
2278 adv_filt_param.feat_seln = ALLOW_ALL_FILTER;
2279 adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;
2280 adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;
2281 adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE;
2282 adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE;
2283 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL,
2284 bte_scan_filt_param_cfg_evt, 0);
2286 /* TODO: Do we need to handle multiple inquiries at the same time? */
2288 /* Set inquiry params and call API */
2289 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
2290 #if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
2291 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
2292 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
2293 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
2294 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
2297 inq_params.mode = BTA_DM_GENERAL_INQUIRY;
2299 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
2301 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
2302 inq_params.report_dup = TRUE;
2304 inq_params.filter_type = BTA_DM_INQ_CLR;
2305 /* TODO: Filter device by BDA needs to be implemented here */
2307 /* Will be enabled to TRUE once inquiry busy level has been received */
2308 btif_dm_inquiry_in_progress = FALSE;
2309 /* find nearby devices */
2310 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
2312 return BT_STATUS_SUCCESS;
2315 /*******************************************************************************
2317 ** Function btif_dm_cancel_discovery
2319 ** Description Cancels search
2321 ** Returns bt_status_t
2323 *******************************************************************************/
2324 bt_status_t btif_dm_cancel_discovery(void)
2326 BTIF_TRACE_EVENT("%s", __FUNCTION__);
2327 BTA_DmSearchCancel();
2328 return BT_STATUS_SUCCESS;
2331 /*******************************************************************************
2333 ** Function btif_dm_create_bond
2335 ** Description Initiate bonding with the specified device
2337 ** Returns bt_status_t
2339 *******************************************************************************/
2340 bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr, int transport)
2342 btif_dm_create_bond_cb_t create_bond_cb;
2343 create_bond_cb.transport = transport;
2344 bdcpy(create_bond_cb.bdaddr.address, bd_addr->address);
2347 BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)), transport);
2348 if (pairing_cb.state != BT_BOND_STATE_NONE)
2349 return BT_STATUS_BUSY;
2351 btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_CREATE_BOND, pairing_cb.state);
2353 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
2354 (char *)&create_bond_cb, sizeof(btif_dm_create_bond_cb_t), NULL);
2356 return BT_STATUS_SUCCESS;
2359 /*******************************************************************************
2361 ** Function btif_dm_create_bond_out_of_band
2363 ** Description Initiate bonding with the specified device using out of band data
2365 ** Returns bt_status_t
2367 *******************************************************************************/
2368 bt_status_t btif_dm_create_bond_out_of_band(const bt_bdaddr_t *bd_addr, int transport, const bt_out_of_band_data_t *oob_data)
2370 bdcpy(oob_cb.bdaddr, bd_addr->address);
2371 memcpy(&oob_cb.oob_data, oob_data, sizeof(bt_out_of_band_data_t));
2373 uint8_t empty[] = {0, 0, 0, 0, 0, 0, 0};
2374 // If LE Bluetooth Device Address is provided, use provided address type
2376 if (memcmp(oob_data->le_bt_dev_addr, empty, 7) != 0) {
2377 /* byte no 7 is address type in LE Bluetooth Address OOB data */
2378 uint8_t address_type = oob_data->le_bt_dev_addr[6];
2379 if (address_type == BLE_ADDR_PUBLIC || address_type == BLE_ADDR_RANDOM) {
2380 // bd_addr->address is already reversed, so use it instead of
2381 // oob_data->le_bt_dev_addr
2382 BTM_SecAddBleDevice(bd_addr->address, NULL, BT_DEVICE_TYPE_BLE, address_type);
2387 BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)), transport);
2388 return btif_dm_create_bond(bd_addr, transport);
2391 /*******************************************************************************
2393 ** Function btif_dm_cancel_bond
2395 ** Description Initiate bonding with the specified device
2397 ** Returns bt_status_t
2399 *******************************************************************************/
2401 bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
2405 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)));
2407 btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_CANCEL_BOND, pairing_cb.state);
2410 ** 1. Restore scan modes
2411 ** 2. special handling for HID devices
2413 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2416 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2418 if (pairing_cb.is_ssp)
2420 if (pairing_cb.is_le_only)
2422 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2426 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2427 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2428 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2433 if (pairing_cb.is_le_only)
2435 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2439 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2441 /* Cancel bonding, in case it is in ACL connection setup state */
2442 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2446 if (pairing_cb.is_ssp)
2448 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2452 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2454 /* Cancel bonding, in case it is in ACL connection setup state */
2455 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2456 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2460 return BT_STATUS_SUCCESS;
2463 /*******************************************************************************
2465 ** Function btif_dm_hh_open_failed
2467 ** Description informs the upper layers if the HH have failed during bonding
2471 *******************************************************************************/
2473 void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
2475 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2476 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2478 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2482 /*******************************************************************************
2484 ** Function btif_dm_remove_bond
2486 ** Description Removes bonding with the specified device
2488 ** Returns bt_status_t
2490 *******************************************************************************/
2492 bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
2496 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)));
2498 btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_REMOVE_BOND, pairing_cb.state);
2500 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2501 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2503 return BT_STATUS_SUCCESS;
2506 /*******************************************************************************
2508 ** Function btif_dm_pin_reply
2510 ** Description BT legacy pairing - PIN code reply
2512 ** Returns bt_status_t
2514 *******************************************************************************/
2516 bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2517 uint8_t pin_len, bt_pin_code_t *pin_code)
2519 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
2520 if (pin_code == NULL || pin_len > PIN_CODE_LEN)
2521 return BT_STATUS_FAIL;
2522 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2524 if (pairing_cb.is_le_only)
2528 int multi[] = {100000, 10000, 1000, 100, 10,1};
2529 BD_ADDR remote_bd_addr;
2530 bdcpy(remote_bd_addr, bd_addr->address);
2531 for (i = 0; i < 6; i++)
2533 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2535 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
2536 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2541 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2543 pairing_cb.pin_code_len = pin_len;
2546 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2549 pairing_cb.pin_code_len = pin_len;
2551 return BT_STATUS_SUCCESS;
2554 /*******************************************************************************
2556 ** Function btif_dm_ssp_reply
2558 ** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2560 ** Returns bt_status_t
2562 *******************************************************************************/
2563 bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2564 bt_ssp_variant_t variant, uint8_t accept,
2569 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2571 /* This is not implemented in the stack.
2572 * For devices with display, this is not needed
2574 BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__);
2575 return BT_STATUS_FAIL;
2577 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
2578 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
2579 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2580 if (pairing_cb.is_le_only)
2582 if(pairing_cb.is_le_nc)
2584 BTA_DmBleConfirmReply((UINT8 *)bd_addr->address,accept);
2587 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2589 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2592 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2595 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2597 return BT_STATUS_SUCCESS;
2600 /*******************************************************************************
2602 ** Function btif_dm_get_adapter_property
2604 ** Description Queries the BTA for the adapter property
2606 ** Returns bt_status_t
2608 *******************************************************************************/
2609 bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2611 BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type);
2614 case BT_PROPERTY_BDNAME:
2616 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
2617 strncpy((char *)bd_name->name, (char *)btif_get_default_local_name(),
2618 sizeof(bd_name->name) - 1);
2619 bd_name->name[sizeof(bd_name->name) - 1] = 0;
2620 prop->len = strlen((char *)bd_name->name);
2624 case BT_PROPERTY_ADAPTER_SCAN_MODE:
2626 /* if the storage does not have it. Most likely app never set it. Default is NONE */
2627 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2628 *mode = BT_SCAN_MODE_NONE;
2629 prop->len = sizeof(bt_scan_mode_t);
2633 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2635 uint32_t *tmt = (uint32_t*)prop->val;
2636 *tmt = 120; /* default to 120s, if not found in NV */
2637 prop->len = sizeof(uint32_t);
2643 return BT_STATUS_FAIL;
2645 return BT_STATUS_SUCCESS;
2648 /*******************************************************************************
2650 ** Function btif_dm_get_remote_services
2652 ** Description Start SDP to get remote services
2654 ** Returns bt_status_t
2656 *******************************************************************************/
2657 bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2661 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr)));
2663 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2664 bte_dm_search_services_evt, TRUE);
2666 return BT_STATUS_SUCCESS;
2669 /*******************************************************************************
2671 ** Function btif_dm_get_remote_services_transport
2673 ** Description Start SDP to get remote services by transport
2675 ** Returns bt_status_t
2677 *******************************************************************************/
2678 bt_status_t btif_dm_get_remote_services_by_transport(bt_bdaddr_t *remote_addr, const int transport)
2680 BTIF_TRACE_EVENT("%s", __func__);
2682 /* Set the mask extension */
2683 tBTA_SERVICE_MASK_EXT mask_ext;
2684 mask_ext.num_uuid = 0;
2685 mask_ext.p_uuid = NULL;
2686 mask_ext.srvc_mask = BTA_ALL_SERVICE_MASK;
2688 BTA_DmDiscoverByTransport(remote_addr->address, &mask_ext,
2689 bte_dm_search_services_evt, TRUE, transport);
2691 return BT_STATUS_SUCCESS;
2694 /*******************************************************************************
2696 ** Function btif_dm_get_remote_service_record
2698 ** Description Start SDP to get remote service record
2701 ** Returns bt_status_t
2702 *******************************************************************************/
2703 bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2709 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr)));
2711 sdp_uuid.len = MAX_UUID_SIZE;
2712 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2714 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2715 bte_dm_remote_service_record_evt, TRUE);
2717 return BT_STATUS_SUCCESS;
2720 void btif_dm_execute_service_request(UINT16 event, char *p_param)
2722 BOOLEAN b_enable = FALSE;
2724 if (event == BTIF_DM_ENABLE_SERVICE)
2728 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2729 if (status == BT_STATUS_SUCCESS)
2731 bt_property_t property;
2732 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2734 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2735 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2736 sizeof(local_uuids), local_uuids);
2737 btif_storage_get_adapter_property(&property);
2738 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2739 BT_STATUS_SUCCESS, 1, &property);
2744 void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2745 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2747 UINT8 yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2748 /* if local initiated:
2750 ** if remote initiated:
2751 ** 1. Copy over the auth_req from peer's io_rsp
2752 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2753 ** as a fallback set MITM+GB if peer had MITM set
2757 UNUSED (p_oob_data);
2759 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
2760 if(pairing_cb.is_local_initiated)
2762 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2763 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2767 /* peer initiated paring. They probably know what they want.
2768 ** Copy the mitm from peer device.
2770 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
2771 __FUNCTION__, pairing_cb.auth_req);
2772 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2774 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2775 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2776 *p_auth_req |= BTA_AUTH_SP_YES;
2778 else if (yes_no_bit)
2780 /* set the general bonding bit for stored device */
2781 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2783 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
2786 void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2787 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2792 if(auth_req & BTA_AUTH_BONDS)
2794 BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
2795 pairing_cb.auth_req = auth_req;
2796 pairing_cb.io_cap = io_cap;
2800 void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_has_oob_data)
2802 if (is_empty_128bit(oob_cb.oob_data.c192))
2804 *p_has_oob_data = FALSE;
2808 *p_has_oob_data = TRUE;
2810 BTIF_TRACE_DEBUG("%s: *p_has_oob_data=%d", __func__, *p_has_oob_data);
2813 void btif_dm_set_oob_for_le_io_req(BD_ADDR bd_addr, tBTA_OOB_DATA *p_has_oob_data,
2814 tBTA_LE_AUTH_REQ *p_auth_req)
2817 if (!is_empty_128bit(oob_cb.oob_data.le_sc_c) &&
2818 !is_empty_128bit(oob_cb.oob_data.le_sc_r)) {
2819 /* We have LE SC OOB data */
2821 /* make sure OOB data is for this particular device */
2822 if (memcmp(bd_addr, oob_cb.bdaddr, BD_ADDR_LEN) == 0) {
2823 *p_auth_req = ((*p_auth_req) | BTM_LE_AUTH_REQ_SC_ONLY);
2824 *p_has_oob_data = true;
2826 *p_has_oob_data = false;
2827 BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address",
2830 } else if (!is_empty_128bit(oob_cb.oob_data.sm_tk)) {
2831 /* We have security manager TK */
2833 /* make sure OOB data is for this particular device */
2834 if (memcmp(bd_addr, oob_cb.bdaddr, BD_ADDR_LEN) == 0) {
2835 // When using OOB with TK, SC Secure Connections bit must be disabled.
2836 tBTA_LE_AUTH_REQ mask = ~BTM_LE_AUTH_REQ_SC_ONLY;
2837 *p_auth_req = ((*p_auth_req) & mask);
2839 *p_has_oob_data = true;
2841 *p_has_oob_data = false;
2842 BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address",
2846 *p_has_oob_data = false;
2848 BTIF_TRACE_DEBUG("%s *p_has_oob_data=%d", __func__, *p_has_oob_data);
2851 #ifdef BTIF_DM_OOB_TEST
2852 void btif_dm_load_local_oob(void)
2854 char prop_oob[PROPERTY_VALUE_MAX];
2855 osi_property_get("service.brcm.bt.oob", prop_oob, "3");
2856 BTIF_TRACE_DEBUG("%s: prop_oob = %s", __func__, prop_oob);
2857 if (prop_oob[0] != '3')
2859 if (is_empty_128bit(oob_cb.oob_data.c192))
2861 BTIF_TRACE_DEBUG("%s: read OOB, call BTA_DmLocalOob()", __func__);
2867 void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2870 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2871 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2873 char prop_oob[PROPERTY_VALUE_MAX];
2874 BTIF_TRACE_DEBUG("%s: valid=%d", __func__, valid);
2875 if (is_empty_128bit(oob_cb.oob_data.c192) && valid)
2877 BTIF_TRACE_DEBUG("save local OOB data in memory");
2878 memcpy(oob_cb.oob_data.c192, c, BT_OCTET16_LEN);
2879 memcpy(oob_cb.oob_data.r192, r, BT_OCTET16_LEN);
2880 osi_property_get("service.brcm.bt.oob", prop_oob, "3");
2881 BTIF_TRACE_DEBUG("%s: prop_oob = %s", __func__, prop_oob);
2882 if (prop_oob[0] == '1')
2884 else if (prop_oob[0] == '2')
2888 fp = fopen(path, "wb+");
2891 BTIF_TRACE_DEBUG("%s: failed to save local OOB data to %s", __func__, path);
2895 BTIF_TRACE_DEBUG("%s: save local OOB data into file %s", __func__, path);
2896 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2897 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2904 /*******************************************************************************
2906 ** Function btif_dm_get_smp_config
2908 ** Description Retrieve the SMP pairing options from the bt_stack.conf
2909 ** file. To provide specific pairing options for the host
2910 ** add a node with label "SmpOptions" to the config file
2911 ** and assign it a comma separated list of 5 values in the
2912 ** format: auth, io, ikey, rkey, ksize, oob
2913 ** eg: PTS_SmpOptions=0xD,0x4,0xf,0xf,0x10
2915 ** Parameters: tBTE_APPL_CFG*: pointer to struct defining pairing options
2917 ** Returns TRUE if the options were successfully read, else FALSE
2919 *******************************************************************************/
2920 BOOLEAN btif_dm_get_smp_config(tBTE_APPL_CFG* p_cfg) {
2922 if(!stack_config_get_interface()->get_pts_smp_options()) {
2923 BTIF_TRACE_DEBUG ("%s: SMP options not found in configuration", __func__);
2928 const char* recv = stack_config_get_interface()->get_pts_smp_options();
2932 strncpy(conf, recv, 64);
2933 conf[63] = 0; // null terminate
2935 if ((pch = strtok(conf, ",")) != NULL)
2936 p_cfg->ble_auth_req = (UINT8) strtoul(pch, &endptr, 16);
2940 if ((pch = strtok(NULL, ",")) != NULL)
2941 p_cfg->ble_io_cap = (UINT8) strtoul(pch, &endptr, 16);
2945 if ((pch = strtok(NULL, ",")) != NULL)
2946 p_cfg->ble_init_key = (UINT8) strtoul(pch, &endptr, 16);
2950 if ((pch = strtok(NULL, ",")) != NULL)
2951 p_cfg->ble_resp_key = (UINT8) strtoul(pch, &endptr, 16);
2955 if ((pch = strtok(NULL, ",")) != NULL)
2956 p_cfg->ble_max_key_size = (UINT8) strtoul(pch, &endptr, 16);
2963 BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2967 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2968 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2970 char prop_oob[PROPERTY_VALUE_MAX];
2971 BOOLEAN result = FALSE;
2972 bt_bdaddr_t bt_bd_addr;
2973 bdcpy(oob_cb.bdaddr, bd_addr);
2974 osi_property_get("service.brcm.bt.oob", prop_oob, "3");
2975 BTIF_TRACE_DEBUG("%s: prop_oob = %s", __func__, prop_oob);
2976 if (prop_oob[0] == '1')
2978 else if (prop_oob[0] == '2')
2982 fp = fopen(path, "rb");
2985 BTIF_TRACE_DEBUG("%s: failed to read OOB keys from %s", __func__, path);
2990 BTIF_TRACE_DEBUG("%s: read OOB data from %s", __func__, path);
2991 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2992 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2995 BTIF_TRACE_DEBUG("----%s: TRUE", __func__);
2996 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2997 oob_cb.bdaddr[0], oob_cb.bdaddr[1], oob_cb.bdaddr[2],
2998 oob_cb.bdaddr[3], oob_cb.bdaddr[4], oob_cb.bdaddr[5]);
2999 BTIF_TRACE_DEBUG("----%s: peer_bdaddr = %s", __func__, t);
3000 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
3001 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
3002 p_c[8], p_c[9], p_c[10], p_c[11], p_c[12], p_c[13], p_c[14], p_c[15]);
3003 BTIF_TRACE_DEBUG("----%s: c = %s", __func__, t);
3004 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
3005 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
3006 p_r[8], p_r[9], p_r[10], p_r[11], p_r[12], p_r[13], p_r[14], p_r[15]);
3007 BTIF_TRACE_DEBUG("----%s: r = %s", __func__, t);
3008 bdcpy(bt_bd_addr.address, bd_addr);
3009 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
3010 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
3013 BTIF_TRACE_DEBUG("%s: result=%d", __func__, result);
3016 #endif /* BTIF_DM_OOB_TEST */
3017 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
3019 static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
3021 bt_bdaddr_t bd_addr;
3022 bt_bdname_t bd_name;
3026 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
3028 /* Remote name update */
3029 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
3031 dev_type = BT_DEVICE_TYPE_BLE;
3033 btif_dm_update_ble_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
3034 (tBT_DEVICE_TYPE) dev_type);
3035 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
3036 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
3038 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3039 pairing_cb.is_ssp = FALSE;
3040 cod = COD_UNCLASSIFIED;
3042 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
3043 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
3044 p_ssp_key_notif->passkey);
3047 /*******************************************************************************
3049 ** Function btif_dm_ble_auth_cmpl_evt
3051 ** Description Executes authentication complete event in btif context
3055 *******************************************************************************/
3056 static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
3058 /* Save link key, if not temporary */
3059 bt_bdaddr_t bd_addr;
3060 bt_status_t status = BT_STATUS_FAIL;
3061 bt_bond_state_t state = BT_BOND_STATE_NONE;
3063 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
3065 /* Clear OOB data */
3066 memset(&oob_cb, 0, sizeof(oob_cb));
3068 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
3072 if (p_auth_cmpl->success)
3074 status = BT_STATUS_SUCCESS;
3075 state = BT_BOND_STATE_BONDED;
3078 bdcpy(bdaddr.address, p_auth_cmpl->bd_addr);
3079 if (btif_storage_get_remote_addr_type(&bdaddr, &addr_type) != BT_STATUS_SUCCESS)
3080 btif_storage_set_remote_addr_type(&bdaddr, p_auth_cmpl->addr_type);
3082 /* Test for temporary bonding */
3083 if (btm_get_bond_type_dev(p_auth_cmpl->bd_addr) == BOND_TYPE_TEMPORARY) {
3084 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
3086 btif_storage_remove_bonded_device(&bdaddr);
3087 state = BT_BOND_STATE_NONE;
3089 btif_dm_save_ble_bonding_keys();
3090 BTA_GATTC_Refresh(bd_addr.address);
3091 btif_dm_get_remote_services_by_transport(&bd_addr, BTA_GATT_TRANSPORT_LE);
3096 /*Map the HCI fail reason to bt status */
3097 switch (p_auth_cmpl->fail_reason)
3099 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
3100 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
3101 case BTA_DM_AUTH_SMP_UNKNOWN_ERR:
3102 case BTA_DM_AUTH_SMP_CONN_TOUT:
3103 btif_dm_remove_ble_bonding_keys();
3104 status = BT_STATUS_AUTH_FAILURE;
3106 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
3107 status = BT_STATUS_AUTH_REJECTED;
3110 btif_dm_remove_ble_bonding_keys();
3111 status = BT_STATUS_FAIL;
3115 bond_state_changed(status, &bd_addr, state);
3118 void btif_dm_load_ble_local_keys(void)
3120 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
3122 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
3123 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
3125 ble_local_key_cb.is_er_rcvd = TRUE;
3126 BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
3129 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
3130 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
3131 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
3132 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
3133 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
3134 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
3136 ble_local_key_cb.is_id_keys_rcvd = TRUE;
3137 BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
3141 void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
3142 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
3144 if (ble_local_key_cb.is_er_rcvd )
3146 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
3147 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
3150 if (ble_local_key_cb.is_id_keys_rcvd)
3152 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
3153 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
3154 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
3155 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
3157 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
3160 void btif_dm_save_ble_bonding_keys(void)
3163 bt_bdaddr_t bd_addr;
3165 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
3167 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3169 if (pairing_cb.ble.is_penc_key_rcvd)
3171 btif_storage_add_ble_bonding_key(&bd_addr,
3172 (char *) &pairing_cb.ble.penc_key,
3173 BTIF_DM_LE_KEY_PENC,
3174 sizeof(tBTM_LE_PENC_KEYS));
3177 if (pairing_cb.ble.is_pid_key_rcvd)
3179 btif_storage_add_ble_bonding_key(&bd_addr,
3180 (char *) &pairing_cb.ble.pid_key,
3182 sizeof(tBTM_LE_PID_KEYS));
3185 if (pairing_cb.ble.is_pcsrk_key_rcvd)
3187 btif_storage_add_ble_bonding_key(&bd_addr,
3188 (char *) &pairing_cb.ble.pcsrk_key,
3189 BTIF_DM_LE_KEY_PCSRK,
3190 sizeof(tBTM_LE_PCSRK_KEYS));
3193 if (pairing_cb.ble.is_lenc_key_rcvd)
3195 btif_storage_add_ble_bonding_key(&bd_addr,
3196 (char *) &pairing_cb.ble.lenc_key,
3197 BTIF_DM_LE_KEY_LENC,
3198 sizeof(tBTM_LE_LENC_KEYS));
3201 if (pairing_cb.ble.is_lcsrk_key_rcvd)
3203 btif_storage_add_ble_bonding_key(&bd_addr,
3204 (char *) &pairing_cb.ble.lcsrk_key,
3205 BTIF_DM_LE_KEY_LCSRK,
3206 sizeof(tBTM_LE_LCSRK_KEYS));
3209 if (pairing_cb.ble.is_lidk_key_rcvd)
3211 btif_storage_add_ble_bonding_key(&bd_addr,
3219 void btif_dm_remove_ble_bonding_keys(void)
3221 bt_bdaddr_t bd_addr;
3223 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
3225 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3226 btif_storage_remove_ble_bonding_keys(&bd_addr);
3229 /*******************************************************************************
3231 ** Function btif_dm_ble_sec_req_evt
3233 ** Description Eprocess security request event in btif context
3237 *******************************************************************************/
3238 void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
3240 bt_bdaddr_t bd_addr;
3241 bt_bdname_t bd_name;
3245 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
3247 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3249 BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
3253 /* Remote name update */
3254 if (!btif_get_device_type(p_ble_req->bd_addr, &dev_type))
3256 dev_type = BT_DEVICE_TYPE_BLE;
3258 btif_dm_update_ble_remote_properties(p_ble_req->bd_addr, p_ble_req->bd_name,
3259 (tBT_DEVICE_TYPE) dev_type);
3261 bdcpy(bd_addr.address, p_ble_req->bd_addr);
3262 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
3264 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3266 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
3267 pairing_cb.is_le_only = TRUE;
3268 pairing_cb.is_le_nc = FALSE;
3269 pairing_cb.is_ssp = TRUE;
3270 btm_set_bond_type_dev(p_ble_req->bd_addr, pairing_cb.bond_type);
3272 cod = COD_UNCLASSIFIED;
3274 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
3275 BT_SSP_VARIANT_CONSENT, 0);
3278 /*******************************************************************************
3280 ** Function btif_dm_ble_passkey_req_evt
3282 ** Description Executes pin request event in btif context
3286 *******************************************************************************/
3287 static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
3289 bt_bdaddr_t bd_addr;
3290 bt_bdname_t bd_name;
3294 /* Remote name update */
3295 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
3297 dev_type = BT_DEVICE_TYPE_BLE;
3299 btif_dm_update_ble_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,
3300 (tBT_DEVICE_TYPE) dev_type);
3302 bdcpy(bd_addr.address, p_pin_req->bd_addr);
3303 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
3305 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3306 pairing_cb.is_le_only = TRUE;
3308 cod = COD_UNCLASSIFIED;
3310 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
3311 &bd_addr, &bd_name, cod, FALSE);
3313 static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF *p_notif_req)
3315 /* TODO implement key notification for numeric comparison */
3316 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
3318 /* Remote name update */
3319 btif_update_remote_properties(p_notif_req->bd_addr , p_notif_req->bd_name,
3320 NULL, BT_DEVICE_TYPE_BLE);
3322 bt_bdaddr_t bd_addr;
3323 bdcpy(bd_addr.address, p_notif_req->bd_addr);
3325 bt_bdname_t bd_name;
3326 memcpy(bd_name.name, p_notif_req->bd_name, BD_NAME_LEN);
3328 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3329 pairing_cb.is_ssp = FALSE;
3330 pairing_cb.is_le_only = TRUE;
3331 pairing_cb.is_le_nc = TRUE;
3333 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
3334 COD_UNCLASSIFIED, BT_SSP_VARIANT_PASSKEY_CONFIRMATION,
3335 p_notif_req->passkey);
3338 static void btif_dm_ble_oob_req_evt(tBTA_DM_SP_RMT_OOB *req_oob_type)
3340 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
3342 bt_bdaddr_t bd_addr;
3343 bdcpy(bd_addr.address, req_oob_type->bd_addr);
3344 /* We already checked if OOB data is present in
3345 * btif_dm_set_oob_for_le_io_req, but check here again. If it's not present
3346 * do nothing, pairing will timeout.
3348 if (is_empty_128bit(oob_cb.oob_data.sm_tk)) {
3352 /* make sure OOB data is for this particular device */
3353 if (memcmp(req_oob_type->bd_addr, oob_cb.bdaddr, BD_ADDR_LEN) != 0) {
3354 BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address", __func__);
3358 /* Remote name update */
3359 btif_update_remote_properties(req_oob_type->bd_addr , req_oob_type->bd_name,
3360 NULL, BT_DEVICE_TYPE_BLE);
3362 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3363 pairing_cb.is_ssp = FALSE;
3364 pairing_cb.is_le_only = TRUE;
3365 pairing_cb.is_le_nc = FALSE;
3367 BTM_BleOobDataReply(req_oob_type->bd_addr, 0, 16, oob_cb.oob_data.sm_tk);
3371 static void btif_dm_ble_sc_oob_req_evt(tBTA_DM_SP_RMT_OOB *req_oob_type)
3373 BTIF_TRACE_DEBUG("%s", __func__);
3375 bt_bdaddr_t bd_addr;
3376 bdcpy(bd_addr.address, req_oob_type->bd_addr);
3378 /* We already checked if OOB data is present in
3379 * btif_dm_set_oob_for_le_io_req, but check here again. If it's not present
3380 * do nothing, pairing will timeout.
3382 if (is_empty_128bit(oob_cb.oob_data.le_sc_c) &&
3383 is_empty_128bit(oob_cb.oob_data.le_sc_r)) {
3384 BTIF_TRACE_WARNING("%s: LE SC OOB data is empty", __func__);
3388 /* make sure OOB data is for this particular device */
3389 if (memcmp(req_oob_type->bd_addr, oob_cb.bdaddr, BD_ADDR_LEN) != 0) {
3390 BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address", __func__);
3394 /* Remote name update */
3395 btif_update_remote_properties(req_oob_type->bd_addr , req_oob_type->bd_name,
3396 NULL, BT_DEVICE_TYPE_BLE);
3398 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3399 pairing_cb.is_ssp = false;
3400 pairing_cb.is_le_only = true; //TODO: we can derive classic pairing from this one
3401 pairing_cb.is_le_nc = false;
3403 BTM_BleSecureConnectionOobDataReply(req_oob_type->bd_addr,
3404 oob_cb.oob_data.le_sc_c,
3405 oob_cb.oob_data.le_sc_r);
3408 void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
3409 tBT_DEVICE_TYPE dev_type)
3411 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
3414 static void btif_dm_ble_tx_test_cback(void *p)
3416 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
3417 (char *)p, 1, NULL);
3420 static void btif_dm_ble_rx_test_cback(void *p)
3422 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
3423 (char *)p, 1, NULL);
3426 static void btif_dm_ble_test_end_cback(void *p)
3428 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
3429 (char *)p, 3, NULL);
3431 /*******************************************************************************
3433 ** Function btif_le_test_mode
3435 ** Description Sends a HCI BLE Test command to the Controller
3437 ** Returns BT_STATUS_SUCCESS on success
3439 *******************************************************************************/
3440 bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
3443 case HCI_BLE_TRANSMITTER_TEST:
3444 if (len != 3) return BT_STATUS_PARM_INVALID;
3445 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
3447 case HCI_BLE_RECEIVER_TEST:
3448 if (len != 1) return BT_STATUS_PARM_INVALID;
3449 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
3451 case HCI_BLE_TEST_END:
3452 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
3455 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
3456 return BT_STATUS_UNSUPPORTED;
3458 return BT_STATUS_SUCCESS;
3462 void btif_dm_on_disable()
3464 /* cancel any pending pairing requests */
3465 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3467 bt_bdaddr_t bd_addr;
3469 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
3470 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3471 btif_dm_cancel_bond(&bd_addr);
3475 /*******************************************************************************
3477 ** Function btif_dm_read_energy_info
3479 ** Description Reads the energy info from controller
3483 *******************************************************************************/
3484 void btif_dm_read_energy_info()
3486 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
3487 BTA_DmBleGetEnergyInfo(bta_energy_info_cb);
3491 static char* btif_get_default_local_name() {
3492 if (btif_default_local_name[0] == '\0')
3494 int max_len = sizeof(btif_default_local_name) - 1;
3495 if (BTM_DEF_LOCAL_NAME[0] != '\0')
3497 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
3501 char prop_model[PROPERTY_VALUE_MAX];
3502 osi_property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
3503 strncpy(btif_default_local_name, prop_model, max_len);
3505 btif_default_local_name[max_len] = '\0';
3507 return btif_default_local_name;
3510 static void btif_stats_add_bond_event(const bt_bdaddr_t *bd_addr,
3511 bt_bond_function_t function,
3512 bt_bond_state_t state) {
3513 pthread_mutex_lock(&bond_event_lock);
3515 btif_bond_event_t* event = &btif_dm_bond_events[btif_events_end_index];
3516 memcpy(&event->bd_addr, bd_addr, sizeof(bt_bdaddr_t));
3517 event->function = function;
3518 event->state = state;
3519 clock_gettime(CLOCK_REALTIME, &event->timestamp);
3521 btif_num_bond_events++;
3522 btif_events_end_index = (btif_events_end_index + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1);
3523 if (btif_events_end_index == btif_events_start_index) {
3524 btif_events_start_index = (btif_events_start_index + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1);
3528 btif_get_device_type(bd_addr->address, &type);
3530 device_type_t device_type;
3532 case BT_DEVICE_TYPE_BREDR:
3533 device_type = DEVICE_TYPE_BREDR;
3535 case BT_DEVICE_TYPE_BLE:
3536 device_type = DEVICE_TYPE_LE;
3538 case BT_DEVICE_TYPE_DUMO:
3539 device_type = DEVICE_TYPE_DUMO;
3542 device_type = DEVICE_TYPE_UNKNOWN;
3546 uint32_t cod = get_cod(bd_addr);
3547 uint64_t ts = event->timestamp.tv_sec * 1000 +
3548 event->timestamp.tv_nsec / 1000000;
3549 metrics_log_pair_event(0, ts, cod, device_type);
3551 pthread_mutex_unlock(&bond_event_lock);
3554 void btif_debug_bond_event_dump(int fd) {
3555 pthread_mutex_lock(&bond_event_lock);
3556 dprintf(fd, "\nBond Events: \n");
3557 dprintf(fd, " Total Number of events: %zu\n", btif_num_bond_events);
3558 if (btif_num_bond_events > 0)
3559 dprintf(fd, " Time BD_ADDR Function State\n");
3561 for (size_t i = btif_events_start_index; i != btif_events_end_index;
3562 i = (i + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1)) {
3563 btif_bond_event_t* event = &btif_dm_bond_events[i];
3567 struct tm *tstamp = localtime(&event->timestamp.tv_sec);
3568 strftime(temptime, sizeof(temptime), "%H:%M:%S", tstamp);
3569 snprintf(eventtime, sizeof(eventtime), "%s.%03ld", temptime,
3570 event->timestamp.tv_nsec / 1000000);
3573 bdaddr_to_string(&event->bd_addr, bdaddr, sizeof(bdaddr));
3576 switch (event->function) {
3577 case BTIF_DM_FUNC_CREATE_BOND:
3578 func_name = "btif_dm_create_bond";
3580 case BTIF_DM_FUNC_REMOVE_BOND:
3581 func_name = "btif_dm_remove_bond";
3583 case BTIF_DM_FUNC_BOND_STATE_CHANGED:
3584 func_name = "bond_state_changed ";
3587 func_name = "Invalid value ";
3592 switch (event->state) {
3593 case BT_BOND_STATE_NONE:
3594 bond_state = "BOND_STATE_NONE";
3596 case BT_BOND_STATE_BONDING:
3597 bond_state = "BOND_STATE_BONDING";
3599 case BT_BOND_STATE_BONDED:
3600 bond_state = "BOND_STATE_BONDED";
3603 bond_state = "Invalid bond state";
3606 dprintf(fd, " %s %s %s %s\n", eventtime, bdaddr, func_name, bond_state);
3608 pthread_mutex_unlock(&bond_event_lock);