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);
238 static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
239 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
240 tBTA_DM_BLE_REF_VALUE ref_value,
243 static char* btif_get_default_local_name();
245 static void btif_stats_add_bond_event(const bt_bdaddr_t *bd_addr,
246 bt_bond_function_t function,
247 bt_bond_state_t state);
249 /******************************************************************************
251 ******************************************************************************/
252 extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
253 extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
254 extern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
255 extern bt_status_t btif_av_sink_execute_service(BOOLEAN b_enable);
256 extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
257 extern bt_status_t btif_hf_client_execute_service(BOOLEAN b_enable);
258 extern bt_status_t btif_sdp_execute_service(BOOLEAN b_enable);
259 extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
260 extern void bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16);
262 /******************************************************************************
264 ******************************************************************************/
266 static bool is_empty_128bit(uint8_t *data)
268 static const uint8_t zero[16] = { 0 };
269 return !memcmp(zero, data, sizeof(zero));
272 static void btif_dm_data_copy(uint16_t event, char *dst, char *src)
274 tBTA_DM_SEC *dst_dm_sec = (tBTA_DM_SEC*)dst;
275 tBTA_DM_SEC *src_dm_sec = (tBTA_DM_SEC*)src;
281 maybe_non_aligned_memcpy(dst_dm_sec, src_dm_sec, sizeof(*src_dm_sec));
283 if (event == BTA_DM_BLE_KEY_EVT)
285 dst_dm_sec->ble_key.p_key_value = osi_malloc(sizeof(tBTM_LE_KEY_VALUE));
286 assert(src_dm_sec->ble_key.p_key_value);
287 memcpy(dst_dm_sec->ble_key.p_key_value, src_dm_sec->ble_key.p_key_value, sizeof(tBTM_LE_KEY_VALUE));
291 static void btif_dm_data_free(uint16_t event, tBTA_DM_SEC *dm_sec)
293 if (event == BTA_DM_BLE_KEY_EVT)
294 osi_free_and_reset((void **)&dm_sec->ble_key.p_key_value);
297 void btif_dm_init(uid_set_t* set)
300 pthread_mutex_init(&bond_event_lock, NULL);
303 void btif_dm_cleanup(void)
306 uid_set_destroy(uid_set);
309 pthread_mutex_destroy(&bond_event_lock);
312 bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
315 BTIF_TRACE_DEBUG("%s service_id: %d", __FUNCTION__, service_id);
316 /* Check the service_ID and invoke the profile's BT state changed API */
319 case BTA_HFP_SERVICE_ID:
320 case BTA_HSP_SERVICE_ID:
322 btif_hf_execute_service(b_enable);
324 case BTA_A2DP_SOURCE_SERVICE_ID:
326 btif_av_execute_service(b_enable);
328 case BTA_A2DP_SINK_SERVICE_ID:
330 btif_av_sink_execute_service(b_enable);
332 case BTA_HID_SERVICE_ID:
334 btif_hh_execute_service(b_enable);
336 case BTA_HFP_HS_SERVICE_ID:
338 btif_hf_client_execute_service(b_enable);
340 case BTA_SDP_SERVICE_ID:
342 btif_sdp_execute_service(b_enable);
345 BTIF_TRACE_ERROR("%s: Unknown service %d being %s",
346 __func__, service_id,
347 (b_enable) ? "enabled" : "disabled");
348 return BT_STATUS_FAIL;
350 return BT_STATUS_SUCCESS;
353 /*******************************************************************************
355 ** Function check_eir_remote_name
357 ** Description Check if remote name is in the EIR data
359 ** Returns TRUE if remote name found
360 ** Populate p_remote_name, if provided and remote name found
362 *******************************************************************************/
363 static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
364 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
366 UINT8 *p_eir_remote_name = NULL;
367 UINT8 remote_name_len = 0;
369 /* Check EIR for remote name and services */
370 if (p_search_data->inq_res.p_eir)
372 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
373 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
374 if (!p_eir_remote_name)
376 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
377 BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
380 if (p_eir_remote_name)
382 if (remote_name_len > BD_NAME_LEN)
383 remote_name_len = BD_NAME_LEN;
385 if (p_remote_name && p_remote_name_len)
387 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
388 *(p_remote_name + remote_name_len) = 0;
389 *p_remote_name_len = remote_name_len;
400 /*******************************************************************************
402 ** Function check_cached_remote_name
404 ** Description Check if remote name is in the NVRAM cache
406 ** Returns TRUE if remote name found
407 ** Populate p_remote_name, if provided and remote name found
409 *******************************************************************************/
410 static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
411 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
414 bt_bdaddr_t remote_bdaddr;
415 bt_property_t prop_name;
417 /* check if we already have it in our btif_storage cache */
418 bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
419 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
420 sizeof(bt_bdname_t), &bdname);
421 if (btif_storage_get_remote_device_property(
422 &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
424 if (p_remote_name && p_remote_name_len)
426 strcpy((char *)p_remote_name, (char *)bdname.name);
427 *p_remote_name_len = strlen((char *)p_remote_name);
435 static uint32_t get_cod(const bt_bdaddr_t *remote_bdaddr) {
437 bt_property_t prop_name;
439 /* check if we already have it in our btif_storage cache */
440 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
441 sizeof(uint32_t), &remote_cod);
442 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
444 LOG_INFO(LOG_TAG, "%s remote_cod = 0x%08x", __func__, remote_cod);
445 return remote_cod & COD_MASK;
451 BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
453 return get_cod(remote_bdaddr) == cod;
456 BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr)
458 return (get_cod(remote_bdaddr) & COD_HID_MASK) == COD_HID_MAJOR;
461 BOOLEAN check_hid_le(const bt_bdaddr_t *remote_bdaddr)
463 uint32_t remote_dev_type;
464 bt_property_t prop_name;
466 /* check if we already have it in our btif_storage cache */
467 BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE,
468 sizeof(uint32_t), &remote_dev_type);
469 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
470 &prop_name) == BT_STATUS_SUCCESS)
472 if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE)
475 bdaddr_to_string(remote_bdaddr, bdstr, sizeof(bdstr));
476 if(btif_config_exist(bdstr, "HidAppId"))
483 /*****************************************************************************
485 ** Function check_sdp_bl
487 ** Description Checks if a given device is blacklisted to skip sdp
489 ** Parameters skip_sdp_entry
491 ** Returns TRUE if the device is present in blacklist, else FALSE
493 *******************************************************************************/
494 BOOLEAN check_sdp_bl(const bt_bdaddr_t *remote_bdaddr)
496 UINT16 manufacturer = 0;
498 UINT16 lmp_subver = 0;
499 bt_property_t prop_name;
500 bt_remote_version_t info;
502 if (remote_bdaddr == NULL)
505 /* fetch additional info about remote device used in iop query */
506 BTM_ReadRemoteVersion(*(BD_ADDR*)remote_bdaddr, &lmp_ver,
507 &manufacturer, &lmp_subver);
509 /* if not available yet, try fetching from config database */
510 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO,
511 sizeof(bt_remote_version_t), &info);
513 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
514 &prop_name) != BT_STATUS_SUCCESS)
519 manufacturer = info.manufacturer;
521 for (unsigned int i = 0; i < ARRAY_SIZE(sdp_blacklist); i++)
523 if (manufacturer == sdp_blacklist[i].manufact_id)
529 static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
532 btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_BOND_STATE_CHANGED, state);
534 // Send bonding state only once - based on outgoing/incoming we may receive duplicates
535 if ((pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING))
537 // Cross key pairing so send callback for static address
538 if (!bdaddr_is_empty(&pairing_cb.static_bdaddr))
540 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
545 if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
546 state = BT_BOND_STATE_NONE;
548 BTIF_TRACE_DEBUG("%s: state=%d, prev_state=%d, sdp_attempts = %d", __func__,
549 state, pairing_cb.state, pairing_cb.sdp_attempts);
551 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
553 if (state == BT_BOND_STATE_BONDING)
555 pairing_cb.state = state;
556 bdcpy(pairing_cb.bd_addr, bd_addr->address);
558 if (!pairing_cb.sdp_attempts)
559 memset(&pairing_cb, 0, sizeof(pairing_cb));
561 BTIF_TRACE_DEBUG("%s: BR-EDR service discovery active", __func__);
565 /* store remote version in bt config to always have access
567 static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
569 bt_property_t property;
571 UINT16 lmp_subver = 0;
573 tBTM_STATUS btm_status;
574 bt_remote_version_t info;
578 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
579 &mfct_set, &lmp_subver);
581 LOG_DEBUG(LOG_TAG, "remote version info [%s]: %x, %x, %x", bdaddr_to_string(p_bd, bdstr, sizeof(bdstr)),
582 lmp_ver, mfct_set, lmp_subver);
584 if (btm_status == BTM_SUCCESS)
586 // Always update cache to ensure we have availability whenever BTM API is not populated
587 info.manufacturer = mfct_set;
588 info.sub_ver = lmp_subver;
589 info.version = lmp_ver;
590 BTIF_STORAGE_FILL_PROPERTY(&property,
591 BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
593 status = btif_storage_set_remote_device_property(p_bd, &property);
594 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
598 static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
599 DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
601 int num_properties = 0;
602 bt_property_t properties[3];
606 bt_device_type_t dev_type;
608 memset(properties, 0, sizeof(properties));
609 bdcpy(bdaddr.address, bd_addr);
612 if (strlen((const char *) bd_name))
614 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
615 BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
616 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
617 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
621 /* class of device */
622 cod = devclass2uint(dev_class);
623 BTIF_TRACE_DEBUG("%s cod is 0x%06x", __func__, cod);
625 /* Try to retrieve cod from storage */
626 BTIF_TRACE_DEBUG("%s cod is 0, checking cod from storage", __func__);
627 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
628 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
629 status = btif_storage_get_remote_device_property(&bdaddr, &properties[num_properties]);
630 BTIF_TRACE_DEBUG("%s cod retrieved from storage is 0x%06x", __func__, cod);
632 BTIF_TRACE_DEBUG("%s cod is again 0, set as unclassified", __func__);
633 cod = COD_UNCLASSIFIED;
637 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
638 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
639 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
640 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
644 bt_property_t prop_name;
645 uint8_t remote_dev_type;
646 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_TYPE_OF_DEVICE,
647 sizeof(uint8_t), &remote_dev_type);
648 if (btif_storage_get_remote_device_property(&bdaddr, &prop_name) == BT_STATUS_SUCCESS)
649 dev_type = remote_dev_type | device_type;
651 dev_type = device_type;
653 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
654 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
655 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
656 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
659 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
660 status, &bdaddr, num_properties, properties);
663 /*******************************************************************************
665 ** Function btif_dm_cb_hid_remote_name
667 ** Description Remote name callback for HID device. Called in btif context
668 ** Special handling for HID devices
672 *******************************************************************************/
673 static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
675 BTIF_TRACE_DEBUG("%s: status=%d pairing_cb.state=%d", __FUNCTION__, p_remote_name->status, pairing_cb.state);
676 if (pairing_cb.state == BT_BOND_STATE_BONDING)
678 bt_bdaddr_t remote_bd;
680 bdcpy(remote_bd.address, pairing_cb.bd_addr);
682 if (p_remote_name->status == BTM_SUCCESS)
684 bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
687 bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
691 /*******************************************************************************
693 ** Function btif_dm_cb_create_bond
695 ** Description Create bond initiated from the BTIF thread context
696 ** Special handling for HID devices
700 *******************************************************************************/
701 static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport)
703 BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
704 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
706 #if BLE_INCLUDED == TRUE
710 bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr));
711 if (transport == BT_TRANSPORT_LE)
713 if (!btif_config_get_int((char const *)&bdstr,"DevType", &device_type))
715 btif_config_set_int(bdstr, "DevType", BT_DEVICE_TYPE_BLE);
717 if (btif_storage_get_remote_addr_type(bd_addr, &addr_type) != BT_STATUS_SUCCESS)
719 btif_storage_set_remote_addr_type(bd_addr, BLE_ADDR_PUBLIC);
722 if((btif_config_get_int((char const *)&bdstr,"DevType", &device_type) &&
723 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
724 (device_type & BT_DEVICE_TYPE_BLE) == BT_DEVICE_TYPE_BLE) || (transport == BT_TRANSPORT_LE))
726 BTA_DmAddBleDevice(bd_addr->address, addr_type, device_type);
730 #if BLE_INCLUDED == TRUE
731 if(is_hid && (device_type & BT_DEVICE_TYPE_BLE) == 0)
737 status = btif_hh_connect(bd_addr);
738 if(status != BT_STATUS_SUCCESS)
739 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
743 BTA_DmBondByTransport((UINT8 *)bd_addr->address, transport);
745 /* Track originator of bond creation */
746 pairing_cb.is_local_initiated = TRUE;
750 /*******************************************************************************
752 ** Function btif_dm_cb_remove_bond
754 ** Description remove bond initiated from the BTIF thread context
755 ** Special handling for HID devices
759 *******************************************************************************/
760 void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
762 /*special handling for HID devices */
763 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there
764 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
765 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
766 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
769 BTIF_TRACE_DEBUG("%s: Removing HH device", __func__);
770 BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
774 /*******************************************************************************
776 ** Function btif_dm_get_connection_state
778 ** Description Returns whether the remote device is currently connected
779 ** and whether encryption is active for the connection
781 ** Returns 0 if not connected; 1 if connected and > 1 if connection is
784 *******************************************************************************/
785 uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr)
787 uint8_t *bda = (uint8_t*)bd_addr->address;
788 uint16_t rc = BTA_DmGetConnectionState(bda);
794 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_BR_EDR);
795 BTIF_TRACE_DEBUG("%s: security flags (BR/EDR)=0x%02x", __FUNCTION__, flags);
796 if (flags & BTM_SEC_FLAG_ENCRYPTED)
797 rc |= ENCRYPTED_BREDR;
799 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_LE);
800 BTIF_TRACE_DEBUG("%s: security flags (LE)=0x%02x", __FUNCTION__, flags);
801 if (flags & BTM_SEC_FLAG_ENCRYPTED)
808 /*******************************************************************************
810 ** Function search_devices_copy_cb
812 ** Description Deep copy callback for search devices event
816 *******************************************************************************/
817 static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
819 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
820 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
825 BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
826 maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data));
829 case BTA_DM_INQ_RES_EVT:
831 if (p_src_data->inq_res.p_eir)
833 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
834 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
839 case BTA_DM_DISC_RES_EVT:
841 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
843 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
844 memcpy(p_dest_data->disc_res.p_raw_data,
845 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
852 static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
854 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
855 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
859 maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data));
862 case BTA_DM_DISC_RES_EVT:
864 if (p_src_data->disc_res.result == BTA_SUCCESS)
866 if (p_src_data->disc_res.num_uuids > 0)
868 p_dest_data->disc_res.p_uuid_list =
869 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
870 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
871 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
872 osi_free_and_reset((void **)&p_src_data->disc_res.p_uuid_list);
874 osi_free_and_reset((void **)&p_src_data->disc_res.p_raw_data);
879 /******************************************************************************
881 ** BTIF DM callback events
883 *****************************************************************************/
885 /*******************************************************************************
887 ** Function btif_dm_pin_req_evt
889 ** Description Executes pin request event in btif context
893 *******************************************************************************/
894 static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
899 bt_pin_code_t pin_code;
902 /* Remote properties update */
903 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
905 dev_type = BT_DEVICE_TYPE_BREDR;
907 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
908 p_pin_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
910 bdcpy(bd_addr.address, p_pin_req->bd_addr);
911 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
913 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
915 cod = devclass2uint(p_pin_req->dev_class);
918 BTIF_TRACE_DEBUG("%s cod is 0, set as unclassified", __func__);
919 cod = COD_UNCLASSIFIED;
922 /* check for auto pair possiblity only if bond was initiated by local device */
923 if (pairing_cb.is_local_initiated && (p_pin_req->min_16_digit == FALSE))
925 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
926 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
927 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
928 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
929 check_cod(&bd_addr, COD_HID_POINTING))
931 /* Check if this device can be auto paired */
932 if (!interop_match_addr(INTEROP_DISABLE_AUTO_PAIRING, &bd_addr) &&
933 !interop_match_name(INTEROP_DISABLE_AUTO_PAIRING, (const char *)bd_name.name) &&
934 (pairing_cb.autopair_attempts == 0))
936 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
937 pin_code.pin[0] = 0x30;
938 pin_code.pin[1] = 0x30;
939 pin_code.pin[2] = 0x30;
940 pin_code.pin[3] = 0x30;
942 pairing_cb.autopair_attempts++;
943 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
947 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
948 check_cod(&bd_addr, COD_HID_COMBO))
950 if ((interop_match_addr(INTEROP_KEYBOARD_REQUIRES_FIXED_PIN, &bd_addr) == TRUE) &&
951 (pairing_cb.autopair_attempts == 0))
953 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
954 pin_code.pin[0] = 0x30;
955 pin_code.pin[1] = 0x30;
956 pin_code.pin[2] = 0x30;
957 pin_code.pin[3] = 0x30;
959 pairing_cb.autopair_attempts++;
960 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
965 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
966 &bd_addr, &bd_name, cod, p_pin_req->min_16_digit);
969 /*******************************************************************************
971 ** Function btif_dm_ssp_cfm_req_evt
973 ** Description Executes SSP confirm request event in btif context
977 *******************************************************************************/
978 static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
983 BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
986 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
988 /* Remote properties update */
989 if (!btif_get_device_type(p_ssp_cfm_req->bd_addr, &dev_type))
991 dev_type = BT_DEVICE_TYPE_BREDR;
993 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
994 p_ssp_cfm_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
996 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
997 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
999 /* Set the pairing_cb based on the local & remote authentication requirements */
1000 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1002 /* if just_works and bonding bit is not set treat this as temporary */
1003 if (p_ssp_cfm_req->just_works && !(p_ssp_cfm_req->loc_auth_req & BTM_AUTH_BONDS) &&
1004 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
1005 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
1006 pairing_cb.bond_type = BOND_TYPE_TEMPORARY;
1008 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
1010 btm_set_bond_type_dev(p_ssp_cfm_req->bd_addr, pairing_cb.bond_type);
1012 pairing_cb.is_ssp = TRUE;
1014 /* If JustWorks auto-accept */
1015 if (p_ssp_cfm_req->just_works)
1017 /* Pairing consent for JustWorks needed if:
1018 * 1. Incoming (non-temporary) pairing is detected AND
1019 * 2. local IO capabilities are DisplayYesNo AND
1020 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
1022 if (is_incoming && pairing_cb.bond_type != BOND_TYPE_TEMPORARY &&
1023 ((p_ssp_cfm_req->loc_io_caps == HCI_IO_CAP_DISPLAY_YESNO) &&
1024 (p_ssp_cfm_req->rmt_io_caps == HCI_IO_CAP_DISPLAY_ONLY ||
1025 p_ssp_cfm_req->rmt_io_caps == HCI_IO_CAP_NO_IO)))
1027 BTIF_TRACE_EVENT("%s: User consent needed for incoming pairing request. loc_io_caps: %d, rmt_io_caps: %d",
1028 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
1032 BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __FUNCTION__);
1033 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
1038 cod = devclass2uint(p_ssp_cfm_req->dev_class);
1041 LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__);
1042 cod = COD_UNCLASSIFIED;
1045 pairing_cb.sdp_attempts = 0;
1046 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
1047 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
1048 p_ssp_cfm_req->num_val);
1051 static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
1053 bt_bdaddr_t bd_addr;
1054 bt_bdname_t bd_name;
1058 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
1060 /* Remote properties update */
1061 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
1063 dev_type = BT_DEVICE_TYPE_BREDR;
1065 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
1066 p_ssp_key_notif->dev_class, (tBT_DEVICE_TYPE) dev_type);
1068 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
1069 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
1071 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1072 pairing_cb.is_ssp = TRUE;
1073 cod = devclass2uint(p_ssp_key_notif->dev_class);
1076 LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__);
1077 cod = COD_UNCLASSIFIED;
1080 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
1081 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
1082 p_ssp_key_notif->passkey);
1084 /*******************************************************************************
1086 ** Function btif_dm_auth_cmpl_evt
1088 ** Description Executes authentication complete event in btif context
1092 *******************************************************************************/
1093 static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
1095 /* Save link key, if not temporary */
1096 bt_bdaddr_t bd_addr;
1097 bt_status_t status = BT_STATUS_FAIL;
1098 bt_bond_state_t state = BT_BOND_STATE_NONE;
1099 BOOLEAN skip_sdp = FALSE;
1101 BTIF_TRACE_DEBUG("%s: bond state=%d", __func__, pairing_cb.state);
1103 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
1104 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
1106 if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) ||
1107 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
1108 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) ||
1109 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB_P_256) ||
1110 pairing_cb.bond_type == BOND_TYPE_PERSISTENT)
1113 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, bond_type=%d",
1114 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1115 ret = btif_storage_add_bonded_device(&bd_addr,
1116 p_auth_cmpl->key, p_auth_cmpl->key_type,
1117 pairing_cb.pin_code_len);
1118 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
1122 BTIF_TRACE_DEBUG("%s: Temporary key. Not storing. key_type=0x%x, bond_type=%d",
1123 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1124 if(pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
1126 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
1128 btif_storage_remove_bonded_device(&bd_addr);
1129 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1135 // We could have received a new link key without going through the pairing flow.
1136 // If so, we don't want to perform SDP or any other operations on the authenticated
1138 if (bdcmp(p_auth_cmpl->bd_addr, pairing_cb.bd_addr) != 0) {
1140 bt_bdaddr_t bt_bdaddr;
1142 memcpy(bt_bdaddr.address, p_auth_cmpl->bd_addr,
1143 sizeof(bt_bdaddr.address));
1144 bdaddr_to_string(&bt_bdaddr, address, sizeof(address));
1145 LOG_INFO(LOG_TAG, "%s skipping SDP since we did not initiate pairing to %s.", __func__, address);
1149 // Skip SDP for certain HID Devices
1150 if (p_auth_cmpl->success)
1152 #if BLE_INCLUDED == TRUE
1153 btif_storage_set_remote_addr_type(&bd_addr, p_auth_cmpl->addr_type);
1155 btif_update_remote_properties(p_auth_cmpl->bd_addr,
1156 p_auth_cmpl->bd_name, NULL, p_auth_cmpl->dev_type);
1157 pairing_cb.timeout_retries = 0;
1158 status = BT_STATUS_SUCCESS;
1159 state = BT_BOND_STATE_BONDED;
1160 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
1162 if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr))
1164 LOG_WARN(LOG_TAG, "%s:skip SDP", __FUNCTION__);
1167 if(!pairing_cb.is_local_initiated && skip_sdp)
1169 bond_state_changed(status, &bd_addr, state);
1171 LOG_WARN(LOG_TAG, "%s: Incoming HID Connection",__FUNCTION__);
1173 bt_bdaddr_t bd_addr;
1175 char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE;
1177 string_to_uuid(uuid_str, &uuid);
1179 prop.type = BT_PROPERTY_UUIDS;
1181 prop.len = MAX_UUID_SIZE;
1183 /* Send the event to the BTIF */
1184 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1185 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1189 #if BLE_INCLUDED == TRUE
1190 BOOLEAN is_crosskey = FALSE;
1191 /* If bonded due to cross-key, save the static address too*/
1192 if(pairing_cb.state == BT_BOND_STATE_BONDING &&
1193 (bdcmp(p_auth_cmpl->bd_addr, pairing_cb.bd_addr) != 0))
1195 BTIF_TRACE_DEBUG("%s: bonding initiated due to cross key, adding static address",
1197 bdcpy(pairing_cb.static_bdaddr.address, p_auth_cmpl->bd_addr);
1200 if (!is_crosskey || !(stack_config_get_interface()->get_pts_crosskey_sdp_disable()))
1204 // Ensure inquiry is stopped before attempting service discovery
1205 btif_dm_cancel_discovery();
1207 /* Trigger SDP on the device */
1208 pairing_cb.sdp_attempts = 1;
1209 btif_dm_get_remote_services(&bd_addr);
1210 #if BLE_INCLUDED == TRUE
1214 // Do not call bond_state_changed_cb yet. Wait until remote service discovery is complete
1218 // Map the HCI fail reason to bt status
1219 switch(p_auth_cmpl->fail_reason)
1221 case HCI_ERR_PAGE_TIMEOUT:
1222 if (interop_match_addr(INTEROP_AUTO_RETRY_PAIRING, &bd_addr)
1223 && pairing_cb.timeout_retries)
1225 BTIF_TRACE_WARNING("%s() - Pairing timeout; retrying (%d) ...", __FUNCTION__, pairing_cb.timeout_retries);
1226 --pairing_cb.timeout_retries;
1227 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
1231 case HCI_ERR_CONNECTION_TOUT:
1232 status = BT_STATUS_RMT_DEV_DOWN;
1235 case HCI_ERR_PAIRING_NOT_ALLOWED:
1236 status = BT_STATUS_AUTH_REJECTED;
1239 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
1240 status = BT_STATUS_AUTH_FAILURE;
1243 /* map the auth failure codes, so we can retry pairing if necessary */
1244 case HCI_ERR_AUTH_FAILURE:
1245 case HCI_ERR_KEY_MISSING:
1246 btif_storage_remove_bonded_device(&bd_addr);
1247 case HCI_ERR_HOST_REJECT_SECURITY:
1248 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
1249 case HCI_ERR_UNIT_KEY_USED:
1250 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
1251 case HCI_ERR_INSUFFCIENT_SECURITY:
1252 case HCI_ERR_PEER_USER:
1253 case HCI_ERR_UNSPECIFIED:
1254 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d",
1255 __FUNCTION__, p_auth_cmpl->fail_reason);
1256 if (pairing_cb.autopair_attempts == 1)
1258 /* Create the Bond once again */
1259 BTIF_TRACE_WARNING("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
1260 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
1265 /* if autopair attempts are more than 1, or not attempted */
1266 status = BT_STATUS_AUTH_FAILURE;
1271 status = BT_STATUS_FAIL;
1273 /* Special Handling for HID Devices */
1274 if (check_cod(&bd_addr, COD_HID_POINTING)) {
1275 /* Remove Device as bonded in nvram as authentication failed */
1276 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __FUNCTION__);
1277 btif_storage_remove_bonded_device(&bd_addr);
1279 bond_state_changed(status, &bd_addr, state);
1283 /******************************************************************************
1285 ** Function btif_dm_search_devices_evt
1287 ** Description Executes search devices callback events in btif context
1291 ******************************************************************************/
1292 static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
1294 tBTA_DM_SEARCH *p_search_data;
1295 BTIF_TRACE_EVENT("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
1299 case BTA_DM_DISC_RES_EVT:
1301 p_search_data = (tBTA_DM_SEARCH *)p_param;
1302 /* Remote name update */
1303 if (strlen((const char *) p_search_data->disc_res.bd_name))
1305 bt_property_t properties[1];
1309 properties[0].type = BT_PROPERTY_BDNAME;
1310 properties[0].val = p_search_data->disc_res.bd_name;
1311 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
1312 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
1314 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
1315 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
1316 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1317 status, &bdaddr, 1, properties);
1319 /* TODO: Services? */
1323 case BTA_DM_INQ_RES_EVT:
1325 /* inquiry result */
1329 UINT8 remote_name_len;
1330 tBTA_SERVICE_MASK services = 0;
1333 p_search_data = (tBTA_DM_SEARCH *)p_param;
1334 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
1336 BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __FUNCTION__, bdaddr_to_string(&bdaddr, bdstr, sizeof(bdstr)),
1337 #if (BLE_INCLUDED == TRUE)
1338 p_search_data->inq_res.device_type);
1340 BT_DEVICE_TYPE_BREDR);
1344 cod = devclass2uint (p_search_data->inq_res.dev_class);
1347 LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__);
1348 cod = COD_UNCLASSIFIED;
1351 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1352 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1354 /* Check EIR for remote name and services */
1355 if (p_search_data->inq_res.p_eir)
1357 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
1358 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
1359 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */
1363 bt_property_t properties[5];
1364 bt_device_type_t dev_type;
1365 uint32_t num_properties = 0;
1369 memset(properties, 0, sizeof(properties));
1371 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1372 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1375 /* Don't send BDNAME if it is empty */
1378 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1380 strlen((char *)bdname.name), &bdname);
1385 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1386 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1389 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1390 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1392 /* Verify if the device is dual mode in NVRAM */
1393 int stored_device_type = 0;
1394 if (btif_get_device_type(bdaddr.address, &stored_device_type) &&
1395 ((stored_device_type != BT_DEVICE_TYPE_BREDR &&
1396 p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BREDR) ||
1397 (stored_device_type != BT_DEVICE_TYPE_BLE &&
1398 p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE))) {
1399 dev_type = BT_DEVICE_TYPE_DUMO;
1401 dev_type = p_search_data->inq_res.device_type;
1404 if (p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE)
1405 addr_type = p_search_data->inq_res.ble_addr_type;
1407 dev_type = BT_DEVICE_TYPE_BREDR;
1409 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1410 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1413 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1414 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1415 &(p_search_data->inq_res.rssi));
1418 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1419 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
1420 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1421 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
1422 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1424 /* Callback to notify upper layer of device */
1425 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1426 num_properties, properties);
1431 case BTA_DM_INQ_CMPL_EVT:
1433 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1434 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1435 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1436 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1437 bte_scan_filt_param_cfg_evt, 0);
1441 case BTA_DM_DISC_CMPL_EVT:
1443 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1446 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1448 /* if inquiry is not in progress and we get a cancel event, then
1449 * it means we are done with inquiry, but remote_name fetches are in
1452 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt
1453 * but instead wait for the cancel_cmpl_evt via the Busy Level
1456 if (btif_dm_inquiry_in_progress == FALSE)
1458 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1459 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1460 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1461 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1462 bte_scan_filt_param_cfg_evt, 0);
1464 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1471 /*******************************************************************************
1473 ** Function btif_dm_search_services_evt
1475 ** Description Executes search services event in btif context
1479 *******************************************************************************/
1480 static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1482 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1484 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
1487 case BTA_DM_DISC_RES_EVT:
1491 bt_bdaddr_t bd_addr;
1494 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1496 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
1497 p_data->disc_res.result, p_data->disc_res.services);
1498 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1499 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1500 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1502 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__);
1503 pairing_cb.sdp_attempts++;
1504 btif_dm_get_remote_services(&bd_addr);
1507 prop.type = BT_PROPERTY_UUIDS;
1509 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1511 prop.val = p_data->disc_res.p_uuid_list;
1512 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1513 for (i=0; i < p_data->disc_res.num_uuids; i++)
1516 uuid_to_string_legacy((bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i*MAX_UUID_SIZE)), temp);
1517 LOG_INFO(LOG_TAG, "%s index:%d uuid:%s", __func__, i, temp);
1521 /* onUuidChanged requires getBondedDevices to be populated.
1522 ** bond_state_changed needs to be sent prior to remote_device_property
1524 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1525 ((bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0) ||
1526 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr.address) == 0)) &&
1527 pairing_cb.sdp_attempts > 0)
1529 BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
1531 pairing_cb.sdp_attempts = 0;
1533 // If bonding occured due to cross-key pairing, send bonding callback
1534 // for static address now
1535 if (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr.address) == 0)
1536 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1538 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1541 if (p_data->disc_res.num_uuids != 0)
1543 /* Also write this to the NVRAM */
1544 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1545 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1546 /* Send the event to the BTIF */
1547 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1548 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1553 case BTA_DM_DISC_CMPL_EVT:
1557 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1561 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1562 case BTA_DM_DISC_BLE_RES_EVT:
1563 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__,
1564 p_data->disc_ble_res.service.uu.uuid16);
1568 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1570 BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__);
1572 bt_bdaddr_t bd_addr;
1576 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1580 unsigned char c = uuid.uu[j];
1581 uuid.uu[j] = uuid.uu[i];
1587 uuid_to_string_legacy(&uuid, temp);
1588 LOG_INFO(LOG_TAG, "%s uuid:%s", __func__, temp);
1590 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1591 prop.type = BT_PROPERTY_UUIDS;
1593 prop.len = MAX_UUID_SIZE;
1595 /* Also write this to the NVRAM */
1596 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1597 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1599 /* Send the event to the BTIF */
1600 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1601 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1605 #endif /* BLE_INCLUDED */
1609 ASSERTC(0, "unhandled search services event", event);
1615 /*******************************************************************************
1617 ** Function btif_dm_remote_service_record_evt
1619 ** Description Executes search service record event in btif context
1623 *******************************************************************************/
1624 static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1626 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1628 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
1631 case BTA_DM_DISC_RES_EVT:
1633 bt_service_record_t rec;
1635 bt_bdaddr_t bd_addr;
1637 memset(&rec, 0, sizeof(bt_service_record_t));
1638 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1640 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
1641 p_data->disc_res.result, p_data->disc_res.services);
1642 prop.type = BT_PROPERTY_SERVICE_RECORD;
1643 prop.val = (void*)&rec;
1644 prop.len = sizeof(rec);
1646 /* disc_res.result is overloaded with SCN. Cannot check result */
1647 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1648 /* TODO: Get the UUID as well */
1649 rec.channel = p_data->disc_res.result - 3;
1650 /* TODO: Need to get the service name using p_raw_data */
1653 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1654 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1660 ASSERTC(0, "unhandled remote service record event", event);
1666 /*******************************************************************************
1668 ** Function btif_dm_upstreams_cback
1670 ** Description Executes UPSTREAMS events in btif context
1674 *******************************************************************************/
1675 static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1677 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1678 tBTA_SERVICE_MASK service_mask;
1680 bt_bdaddr_t bd_addr;
1682 BTIF_TRACE_EVENT("btif_dm_upstreams_cback ev: %s", dump_dm_event(event));
1686 case BTA_DM_ENABLE_EVT:
1691 prop.type = BT_PROPERTY_BDNAME;
1692 prop.len = BD_NAME_LEN;
1693 prop.val = (void*)bdname;
1695 status = btif_storage_get_adapter_property(&prop);
1696 if (status == BT_STATUS_SUCCESS)
1698 /* A name exists in the storage. Make this the device name */
1699 BTA_DmSetDeviceName((char*)prop.val);
1703 /* Storage does not have a name yet.
1704 * Use the default name and write it to the chip
1706 BTA_DmSetDeviceName(btif_get_default_local_name());
1709 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1710 /* Enable local privacy */
1711 BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED);
1714 /* for each of the enabled services in the mask, trigger the profile
1716 service_mask = btif_get_enabled_services_mask();
1717 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1720 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1722 btif_in_execute_service_request(i, TRUE);
1725 /* clear control blocks */
1726 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1727 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
1729 /* This function will also trigger the adapter_properties_cb
1730 ** and bonded_devices_info_cb
1732 btif_storage_load_bonded_devices();
1734 btif_enable_bluetooth_evt(p_data->enable.status);
1738 case BTA_DM_DISABLE_EVT:
1739 /* for each of the enabled services in the mask, trigger the profile
1741 service_mask = btif_get_enabled_services_mask();
1742 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1745 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1747 btif_in_execute_service_request(i, FALSE);
1750 btif_disable_bluetooth_evt();
1753 case BTA_DM_PIN_REQ_EVT:
1754 btif_dm_pin_req_evt(&p_data->pin_req);
1757 case BTA_DM_AUTH_CMPL_EVT:
1758 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1761 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1762 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1764 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1765 btm_set_bond_type_dev(pairing_cb.bd_addr, BOND_TYPE_UNKNOWN);
1766 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1770 case BTA_DM_SP_CFM_REQ_EVT:
1771 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1773 case BTA_DM_SP_KEY_NOTIF_EVT:
1774 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1777 case BTA_DM_DEV_UNPAIRED_EVT:
1778 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1779 btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);
1781 /*special handling for HID devices */
1782 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
1783 btif_hh_remove_device(bd_addr);
1785 btif_storage_remove_bonded_device(&bd_addr);
1786 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1789 case BTA_DM_BUSY_LEVEL_EVT:
1792 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
1794 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
1796 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1797 BT_DISCOVERY_STARTED);
1798 btif_dm_inquiry_in_progress = TRUE;
1800 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
1802 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1803 BT_DISCOVERY_STOPPED);
1804 btif_dm_inquiry_in_progress = FALSE;
1806 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
1808 btif_dm_inquiry_in_progress = FALSE;
1813 case BTA_DM_LINK_UP_EVT:
1814 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
1815 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
1817 btif_update_remote_version_property(&bd_addr);
1819 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1820 &bd_addr, BT_ACL_STATE_CONNECTED);
1823 case BTA_DM_LINK_DOWN_EVT:
1824 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1825 btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);
1826 BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
1827 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1828 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1831 case BTA_DM_HW_ERROR_EVT:
1832 BTIF_TRACE_ERROR("Received H/W Error. ");
1833 /* Flush storage data */
1834 btif_config_flush();
1835 usleep(100000); /* 100milliseconds */
1836 /* Killing the process to force a restart as part of fault tolerance */
1837 kill(getpid(), SIGKILL);
1840 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1841 case BTA_DM_BLE_KEY_EVT:
1842 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT key_type=0x%02x ", p_data->ble_key.key_type);
1844 /* If this pairing is by-product of local initiated GATT client Read or Write,
1845 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1846 have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1847 if (pairing_cb.state != BT_BOND_STATE_BONDING)
1849 BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now");
1850 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1851 BT_BOND_STATE_BONDING);
1853 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1855 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
1859 switch (p_data->ble_key.key_type)
1861 case BTA_LE_KEY_PENC:
1862 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
1863 pairing_cb.ble.is_penc_key_rcvd = TRUE;
1864 pairing_cb.ble.penc_key = p_data->ble_key.p_key_value->penc_key;
1867 case BTA_LE_KEY_PID:
1868 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
1869 pairing_cb.ble.is_pid_key_rcvd = TRUE;
1870 pairing_cb.ble.pid_key = p_data->ble_key.p_key_value->pid_key;
1873 case BTA_LE_KEY_PCSRK:
1874 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
1875 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1876 pairing_cb.ble.pcsrk_key = p_data->ble_key.p_key_value->pcsrk_key;
1879 case BTA_LE_KEY_LENC:
1880 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
1881 pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1882 pairing_cb.ble.lenc_key = p_data->ble_key.p_key_value->lenc_key;
1885 case BTA_LE_KEY_LCSRK:
1886 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
1887 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1888 pairing_cb.ble.lcsrk_key = p_data->ble_key.p_key_value->lcsrk_key;
1891 case BTA_LE_KEY_LID:
1892 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LID");
1893 pairing_cb.ble.is_lidk_key_rcvd = TRUE;
1897 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
1901 case BTA_DM_BLE_SEC_REQ_EVT:
1902 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
1903 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1905 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
1906 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
1907 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1909 case BTA_DM_BLE_PASSKEY_REQ_EVT:
1910 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
1911 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1913 case BTA_DM_BLE_NC_REQ_EVT:
1914 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
1915 btif_dm_ble_key_nc_req_evt(&p_data->key_notif);
1917 case BTA_DM_BLE_OOB_REQ_EVT:
1918 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
1919 btif_dm_ble_oob_req_evt(&p_data->rmt_oob);
1921 case BTA_DM_BLE_LOCAL_IR_EVT:
1922 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
1923 ble_local_key_cb.is_id_keys_rcvd = TRUE;
1924 memcpy(&ble_local_key_cb.id_keys.irk[0],
1925 &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1926 memcpy(&ble_local_key_cb.id_keys.ir[0],
1927 &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1928 memcpy(&ble_local_key_cb.id_keys.dhk[0],
1929 &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1930 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1931 BTIF_DM_LE_LOCAL_KEY_IRK,
1933 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1934 BTIF_DM_LE_LOCAL_KEY_IR,
1936 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1937 BTIF_DM_LE_LOCAL_KEY_DHK,
1940 case BTA_DM_BLE_LOCAL_ER_EVT:
1941 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
1942 ble_local_key_cb.is_er_rcvd = TRUE;
1943 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1944 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1945 BTIF_DM_LE_LOCAL_KEY_ER,
1949 case BTA_DM_BLE_AUTH_CMPL_EVT:
1950 BTIF_TRACE_DEBUG("BTA_DM_BLE_AUTH_CMPL_EVT. ");
1951 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1954 case BTA_DM_LE_FEATURES_READ:
1956 tBTM_BLE_VSC_CB cmn_vsc_cb;
1957 bt_local_le_features_t local_le_features;
1960 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1961 prop.val = (void*)buf;
1962 prop.len = sizeof(buf);
1964 /* LE features are not stored in storage. Should be retrived from stack */
1965 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1966 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1968 prop.len = sizeof (bt_local_le_features_t);
1969 if (cmn_vsc_cb.filter_support == 1)
1970 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1972 local_le_features.max_adv_filter_supported = 0;
1973 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1974 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1975 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
1976 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
1977 local_le_features.scan_result_storage_size = cmn_vsc_cb.tot_scan_results_strg;
1978 local_le_features.version_supported = cmn_vsc_cb.version_supported;
1979 local_le_features.total_trackable_advertisers =
1980 cmn_vsc_cb.total_trackable_advertisers;
1982 local_le_features.extended_scan_support = cmn_vsc_cb.extended_scan_support > 0;
1983 local_le_features.debug_logging_supported = cmn_vsc_cb.debug_logging_supported > 0;
1985 memcpy(prop.val, &local_le_features, prop.len);
1986 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
1990 case BTA_DM_ENER_INFO_READ:
1992 btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param;
1993 bt_activity_energy_info energy_info;
1994 energy_info.status = p_ener_data->status;
1995 energy_info.ctrl_state = p_ener_data->ctrl_state;
1996 energy_info.rx_time = p_ener_data->rx_time;
1997 energy_info.tx_time = p_ener_data->tx_time;
1998 energy_info.idle_time = p_ener_data->idle_time;
1999 energy_info.energy_used = p_ener_data->energy_used;
2001 bt_uid_traffic_t* data = uid_set_read_and_clear(uid_set);
2002 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info, data);
2008 case BTA_DM_AUTHORIZE_EVT:
2009 case BTA_DM_SIG_STRENGTH_EVT:
2010 case BTA_DM_SP_RMT_OOB_EVT:
2011 case BTA_DM_SP_KEYPRESS_EVT:
2012 case BTA_DM_ROLE_CHG_EVT:
2015 BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event );
2019 btif_dm_data_free(event, p_data);
2022 /*******************************************************************************
2024 ** Function btif_dm_generic_evt
2026 ** Description Executes non-BTA upstream events in BTIF context
2030 *******************************************************************************/
2031 static void btif_dm_generic_evt(UINT16 event, char* p_param)
2033 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
2036 case BTIF_DM_CB_DISCOVERY_STARTED:
2038 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
2042 case BTIF_DM_CB_CREATE_BOND:
2044 pairing_cb.timeout_retries = NUM_TIMEOUT_RETRIES;
2045 btif_dm_create_bond_cb_t *create_bond_cb = (btif_dm_create_bond_cb_t*)p_param;
2046 btif_dm_cb_create_bond(&create_bond_cb->bdaddr, create_bond_cb->transport);
2050 case BTIF_DM_CB_REMOVE_BOND:
2052 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
2056 case BTIF_DM_CB_HID_REMOTE_NAME:
2058 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
2062 case BTIF_DM_CB_BOND_STATE_BONDING:
2064 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
2067 case BTIF_DM_CB_LE_TX_TEST:
2068 case BTIF_DM_CB_LE_RX_TEST:
2071 STREAM_TO_UINT8(status, p_param);
2072 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2073 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
2076 case BTIF_DM_CB_LE_TEST_END:
2080 STREAM_TO_UINT8(status, p_param);
2082 STREAM_TO_UINT16(count, p_param);
2083 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2084 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
2089 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
2095 /*******************************************************************************
2097 ** Function bte_dm_evt
2099 ** Description Switches context from BTE to BTIF for all DM events
2103 *******************************************************************************/
2105 void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
2107 /* switch context to btif task context (copy full union size for convenience) */
2108 bt_status_t status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event,
2109 (void*)p_data, sizeof(tBTA_DM_SEC), btif_dm_data_copy);
2111 /* catch any failed context transfers */
2112 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
2115 /*******************************************************************************
2117 ** Function bte_search_devices_evt
2119 ** Description Switches context from BTE to BTIF for DM search events
2123 *******************************************************************************/
2124 static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2126 UINT16 param_len = 0;
2129 param_len += sizeof(tBTA_DM_SEARCH);
2130 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
2133 case BTA_DM_INQ_RES_EVT:
2135 if (p_data->inq_res.p_eir)
2136 param_len += HCI_EXT_INQ_RESPONSE_LEN;
2140 case BTA_DM_DISC_RES_EVT:
2142 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
2143 param_len += p_data->disc_res.raw_data_size;
2147 BTIF_TRACE_DEBUG("%s event=%s param_len=%d", __FUNCTION__, dump_dm_search_event(event), param_len);
2149 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
2150 if (event == BTA_DM_INQ_RES_EVT)
2151 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
2153 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
2154 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
2157 /*******************************************************************************
2159 ** Function bte_dm_search_services_evt
2161 ** Description Switches context from BTE to BTIF for DM search services
2166 *******************************************************************************/
2167 static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2169 UINT16 param_len = 0;
2171 param_len += sizeof(tBTA_DM_SEARCH);
2174 case BTA_DM_DISC_RES_EVT:
2176 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
2177 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
2181 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
2182 * if raw_data is needed. */
2183 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
2184 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
2187 /*******************************************************************************
2189 ** Function bte_dm_remote_service_record_evt
2191 ** Description Switches context from BTE to BTIF for DM search service
2196 *******************************************************************************/
2197 static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2199 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
2200 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
2203 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2204 /*******************************************************************************
2206 ** Function bta_energy_info_cb
2208 ** Description Switches context from BTE to BTIF for DM energy info event
2212 *******************************************************************************/
2213 static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time,
2214 tBTA_DM_BLE_IDLE_TIME_MS idle_time,
2215 tBTA_DM_BLE_ENERGY_USED energy_used,
2216 tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status)
2218 BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld",
2219 status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
2221 btif_activity_energy_info_cb_t btif_cb;
2222 btif_cb.status = status;
2223 btif_cb.ctrl_state = ctrl_state;
2224 btif_cb.tx_time = (uint64_t) tx_time;
2225 btif_cb.rx_time = (uint64_t) rx_time;
2226 btif_cb.idle_time =(uint64_t) idle_time;
2227 btif_cb.energy_used =(uint64_t) energy_used;
2228 btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
2229 (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL);
2233 /*******************************************************************************
2235 ** Function bte_scan_filt_param_cfg_evt
2237 ** Description Scan filter param config event
2241 *******************************************************************************/
2242 static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
2243 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
2244 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
2246 /* This event occurs on calling BTA_DmBleCfgFilterCondition internally,
2247 ** and that is why there is no HAL callback
2249 if(BTA_SUCCESS != status)
2251 BTIF_TRACE_ERROR("%s, %d", __FUNCTION__, status);
2255 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2259 /*****************************************************************************
2261 ** btif api functions (no context switch)
2263 *****************************************************************************/
2265 /*******************************************************************************
2267 ** Function btif_dm_start_discovery
2269 ** Description Start device discovery/inquiry
2271 ** Returns bt_status_t
2273 *******************************************************************************/
2274 bt_status_t btif_dm_start_discovery(void)
2276 tBTA_DM_INQ inq_params;
2277 tBTA_SERVICE_MASK services = 0;
2278 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
2280 BTIF_TRACE_EVENT("%s", __FUNCTION__);
2282 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2283 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
2284 /* Cleanup anything remaining on index 0 */
2285 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
2286 bte_scan_filt_param_cfg_evt, 0);
2288 /* Add an allow-all filter on index 0*/
2289 adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE;
2290 adv_filt_param.feat_seln = ALLOW_ALL_FILTER;
2291 adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;
2292 adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;
2293 adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE;
2294 adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE;
2295 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL,
2296 bte_scan_filt_param_cfg_evt, 0);
2298 /* TODO: Do we need to handle multiple inquiries at the same time? */
2300 /* Set inquiry params and call API */
2301 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
2302 #if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
2303 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
2304 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
2305 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
2306 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
2309 inq_params.mode = BTA_DM_GENERAL_INQUIRY;
2311 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
2313 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
2314 inq_params.report_dup = TRUE;
2316 inq_params.filter_type = BTA_DM_INQ_CLR;
2317 /* TODO: Filter device by BDA needs to be implemented here */
2319 /* Will be enabled to TRUE once inquiry busy level has been received */
2320 btif_dm_inquiry_in_progress = FALSE;
2321 /* find nearby devices */
2322 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
2324 return BT_STATUS_SUCCESS;
2327 /*******************************************************************************
2329 ** Function btif_dm_cancel_discovery
2331 ** Description Cancels search
2333 ** Returns bt_status_t
2335 *******************************************************************************/
2336 bt_status_t btif_dm_cancel_discovery(void)
2338 BTIF_TRACE_EVENT("%s", __FUNCTION__);
2339 BTA_DmSearchCancel();
2340 return BT_STATUS_SUCCESS;
2343 /*******************************************************************************
2345 ** Function btif_dm_create_bond
2347 ** Description Initiate bonding with the specified device
2349 ** Returns bt_status_t
2351 *******************************************************************************/
2352 bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr, int transport)
2354 btif_dm_create_bond_cb_t create_bond_cb;
2355 create_bond_cb.transport = transport;
2356 bdcpy(create_bond_cb.bdaddr.address, bd_addr->address);
2359 BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)), transport);
2360 if (pairing_cb.state != BT_BOND_STATE_NONE)
2361 return BT_STATUS_BUSY;
2363 btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_CREATE_BOND, pairing_cb.state);
2365 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
2366 (char *)&create_bond_cb, sizeof(btif_dm_create_bond_cb_t), NULL);
2368 return BT_STATUS_SUCCESS;
2371 /*******************************************************************************
2373 ** Function btif_dm_create_bond_out_of_band
2375 ** Description Initiate bonding with the specified device using out of band data
2377 ** Returns bt_status_t
2379 *******************************************************************************/
2380 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)
2382 bdcpy(oob_cb.bdaddr, bd_addr->address);
2383 memcpy(&oob_cb.oob_data, oob_data, sizeof(bt_out_of_band_data_t));
2386 BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)), transport);
2387 return btif_dm_create_bond(bd_addr, transport);
2390 /*******************************************************************************
2392 ** Function btif_dm_cancel_bond
2394 ** Description Initiate bonding with the specified device
2396 ** Returns bt_status_t
2398 *******************************************************************************/
2400 bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
2404 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)));
2406 btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_CANCEL_BOND, pairing_cb.state);
2409 ** 1. Restore scan modes
2410 ** 2. special handling for HID devices
2412 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2415 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2417 if (pairing_cb.is_ssp)
2419 if (pairing_cb.is_le_only)
2421 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2425 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2426 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2427 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2432 if (pairing_cb.is_le_only)
2434 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2438 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2440 /* Cancel bonding, in case it is in ACL connection setup state */
2441 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2445 if (pairing_cb.is_ssp)
2447 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2451 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2453 /* Cancel bonding, in case it is in ACL connection setup state */
2454 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2455 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2459 return BT_STATUS_SUCCESS;
2462 /*******************************************************************************
2464 ** Function btif_dm_hh_open_failed
2466 ** Description informs the upper layers if the HH have failed during bonding
2470 *******************************************************************************/
2472 void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
2474 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2475 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2477 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2481 /*******************************************************************************
2483 ** Function btif_dm_remove_bond
2485 ** Description Removes bonding with the specified device
2487 ** Returns bt_status_t
2489 *******************************************************************************/
2491 bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
2495 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)));
2497 btif_stats_add_bond_event(bd_addr, BTIF_DM_FUNC_REMOVE_BOND, pairing_cb.state);
2499 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2500 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2502 return BT_STATUS_SUCCESS;
2505 /*******************************************************************************
2507 ** Function btif_dm_pin_reply
2509 ** Description BT legacy pairing - PIN code reply
2511 ** Returns bt_status_t
2513 *******************************************************************************/
2515 bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2516 uint8_t pin_len, bt_pin_code_t *pin_code)
2518 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
2519 if (pin_code == NULL || pin_len > PIN_CODE_LEN)
2520 return BT_STATUS_FAIL;
2521 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2523 if (pairing_cb.is_le_only)
2527 int multi[] = {100000, 10000, 1000, 100, 10,1};
2528 BD_ADDR remote_bd_addr;
2529 bdcpy(remote_bd_addr, bd_addr->address);
2530 for (i = 0; i < 6; i++)
2532 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2534 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
2535 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2540 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2542 pairing_cb.pin_code_len = pin_len;
2545 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2548 pairing_cb.pin_code_len = pin_len;
2550 return BT_STATUS_SUCCESS;
2553 /*******************************************************************************
2555 ** Function btif_dm_ssp_reply
2557 ** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2559 ** Returns bt_status_t
2561 *******************************************************************************/
2562 bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2563 bt_ssp_variant_t variant, uint8_t accept,
2568 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2570 /* This is not implemented in the stack.
2571 * For devices with display, this is not needed
2573 BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__);
2574 return BT_STATUS_FAIL;
2576 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
2577 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
2578 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2579 if (pairing_cb.is_le_only)
2581 if(pairing_cb.is_le_nc)
2583 BTA_DmBleConfirmReply((UINT8 *)bd_addr->address,accept);
2586 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2588 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2591 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2594 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2596 return BT_STATUS_SUCCESS;
2599 /*******************************************************************************
2601 ** Function btif_dm_get_adapter_property
2603 ** Description Queries the BTA for the adapter property
2605 ** Returns bt_status_t
2607 *******************************************************************************/
2608 bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2610 BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type);
2613 case BT_PROPERTY_BDNAME:
2615 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
2616 strncpy((char *)bd_name->name, (char *)btif_get_default_local_name(),
2617 sizeof(bd_name->name) - 1);
2618 bd_name->name[sizeof(bd_name->name) - 1] = 0;
2619 prop->len = strlen((char *)bd_name->name);
2623 case BT_PROPERTY_ADAPTER_SCAN_MODE:
2625 /* if the storage does not have it. Most likely app never set it. Default is NONE */
2626 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2627 *mode = BT_SCAN_MODE_NONE;
2628 prop->len = sizeof(bt_scan_mode_t);
2632 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2634 uint32_t *tmt = (uint32_t*)prop->val;
2635 *tmt = 120; /* default to 120s, if not found in NV */
2636 prop->len = sizeof(uint32_t);
2642 return BT_STATUS_FAIL;
2644 return BT_STATUS_SUCCESS;
2647 /*******************************************************************************
2649 ** Function btif_dm_get_remote_services
2651 ** Description Start SDP to get remote services
2653 ** Returns bt_status_t
2655 *******************************************************************************/
2656 bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2660 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr)));
2662 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2663 bte_dm_search_services_evt, TRUE);
2665 return BT_STATUS_SUCCESS;
2668 /*******************************************************************************
2670 ** Function btif_dm_get_remote_services_transport
2672 ** Description Start SDP to get remote services by transport
2674 ** Returns bt_status_t
2676 *******************************************************************************/
2677 bt_status_t btif_dm_get_remote_services_by_transport(bt_bdaddr_t *remote_addr, const int transport)
2679 BTIF_TRACE_EVENT("%s", __func__);
2681 /* Set the mask extension */
2682 tBTA_SERVICE_MASK_EXT mask_ext;
2683 mask_ext.num_uuid = 0;
2684 mask_ext.p_uuid = NULL;
2685 mask_ext.srvc_mask = BTA_ALL_SERVICE_MASK;
2687 BTA_DmDiscoverByTransport(remote_addr->address, &mask_ext,
2688 bte_dm_search_services_evt, TRUE, transport);
2690 return BT_STATUS_SUCCESS;
2693 /*******************************************************************************
2695 ** Function btif_dm_get_remote_service_record
2697 ** Description Start SDP to get remote service record
2700 ** Returns bt_status_t
2701 *******************************************************************************/
2702 bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2708 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr)));
2710 sdp_uuid.len = MAX_UUID_SIZE;
2711 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2713 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2714 bte_dm_remote_service_record_evt, TRUE);
2716 return BT_STATUS_SUCCESS;
2719 void btif_dm_execute_service_request(UINT16 event, char *p_param)
2721 BOOLEAN b_enable = FALSE;
2723 if (event == BTIF_DM_ENABLE_SERVICE)
2727 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2728 if (status == BT_STATUS_SUCCESS)
2730 bt_property_t property;
2731 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2733 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2734 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2735 sizeof(local_uuids), local_uuids);
2736 btif_storage_get_adapter_property(&property);
2737 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2738 BT_STATUS_SUCCESS, 1, &property);
2743 void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2744 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2746 UINT8 yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2747 /* if local initiated:
2749 ** if remote initiated:
2750 ** 1. Copy over the auth_req from peer's io_rsp
2751 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2752 ** as a fallback set MITM+GB if peer had MITM set
2756 UNUSED (p_oob_data);
2758 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
2759 if(pairing_cb.is_local_initiated)
2761 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2762 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2766 /* peer initiated paring. They probably know what they want.
2767 ** Copy the mitm from peer device.
2769 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
2770 __FUNCTION__, pairing_cb.auth_req);
2771 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2773 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2774 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2775 *p_auth_req |= BTA_AUTH_SP_YES;
2777 else if (yes_no_bit)
2779 /* set the general bonding bit for stored device */
2780 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2782 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
2785 void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2786 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2791 if(auth_req & BTA_AUTH_BONDS)
2793 BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
2794 pairing_cb.auth_req = auth_req;
2795 pairing_cb.io_cap = io_cap;
2799 void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_has_oob_data)
2801 if (is_empty_128bit(oob_cb.oob_data.c192))
2803 *p_has_oob_data = FALSE;
2807 *p_has_oob_data = TRUE;
2809 BTIF_TRACE_DEBUG("%s: *p_has_oob_data=%d", __func__, *p_has_oob_data);
2812 void btif_dm_set_oob_for_le_io_req(BD_ADDR bd_addr, tBTA_OOB_DATA *p_has_oob_data,
2813 tBTA_LE_AUTH_REQ *p_auth_req)
2816 /* We currently support only Security Manager TK as OOB data for LE transport.
2817 If it's not present mark no OOB data.
2819 if (!is_empty_128bit(oob_cb.oob_data.sm_tk))
2821 /* make sure OOB data is for this particular device */
2822 if (memcmp(bd_addr, oob_cb.bdaddr, BD_ADDR_LEN) == 0) {
2823 // When using OOB with TK, SC Secure Connections bit must be disabled.
2824 tBTA_LE_AUTH_REQ mask = ~BTM_LE_AUTH_REQ_SC_ONLY;
2825 *p_auth_req = ((*p_auth_req) & mask);
2827 *p_has_oob_data = TRUE;
2831 *p_has_oob_data = FALSE;
2832 BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address",
2838 *p_has_oob_data = FALSE;
2840 BTIF_TRACE_DEBUG("%s *p_has_oob_data=%d", __func__, *p_has_oob_data);
2843 #ifdef BTIF_DM_OOB_TEST
2844 void btif_dm_load_local_oob(void)
2846 char prop_oob[PROPERTY_VALUE_MAX];
2847 osi_property_get("service.brcm.bt.oob", prop_oob, "3");
2848 BTIF_TRACE_DEBUG("%s: prop_oob = %s", __func__, prop_oob);
2849 if (prop_oob[0] != '3')
2851 if (is_empty_128bit(oob_cb.oob_data.c192))
2853 BTIF_TRACE_DEBUG("%s: read OOB, call BTA_DmLocalOob()", __func__);
2859 void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2862 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2863 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2865 char prop_oob[PROPERTY_VALUE_MAX];
2866 BTIF_TRACE_DEBUG("%s: valid=%d", __func__, valid);
2867 if (is_empty_128bit(oob_cb.oob_data.c192) && valid)
2869 BTIF_TRACE_DEBUG("save local OOB data in memory");
2870 memcpy(oob_cb.oob_data.c192, c, BT_OCTET16_LEN);
2871 memcpy(oob_cb.oob_data.r192, r, BT_OCTET16_LEN);
2872 osi_property_get("service.brcm.bt.oob", prop_oob, "3");
2873 BTIF_TRACE_DEBUG("%s: prop_oob = %s", __func__, prop_oob);
2874 if (prop_oob[0] == '1')
2876 else if (prop_oob[0] == '2')
2880 fp = fopen(path, "wb+");
2883 BTIF_TRACE_DEBUG("%s: failed to save local OOB data to %s", __func__, path);
2887 BTIF_TRACE_DEBUG("%s: save local OOB data into file %s", __func__, path);
2888 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2889 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2896 /*******************************************************************************
2898 ** Function btif_dm_get_smp_config
2900 ** Description Retrieve the SMP pairing options from the bt_stack.conf
2901 ** file. To provide specific pairing options for the host
2902 ** add a node with label "SmpOptions" to the config file
2903 ** and assign it a comma separated list of 5 values in the
2904 ** format: auth, io, ikey, rkey, ksize, oob
2905 ** eg: PTS_SmpOptions=0xD,0x4,0xf,0xf,0x10
2907 ** Parameters: tBTE_APPL_CFG*: pointer to struct defining pairing options
2909 ** Returns TRUE if the options were successfully read, else FALSE
2911 *******************************************************************************/
2912 BOOLEAN btif_dm_get_smp_config(tBTE_APPL_CFG* p_cfg) {
2914 if(!stack_config_get_interface()->get_pts_smp_options()) {
2915 BTIF_TRACE_DEBUG ("%s: SMP options not found in configuration", __func__);
2920 const char* recv = stack_config_get_interface()->get_pts_smp_options();
2924 strncpy(conf, recv, 64);
2925 conf[63] = 0; // null terminate
2927 if ((pch = strtok(conf, ",")) != NULL)
2928 p_cfg->ble_auth_req = (UINT8) strtoul(pch, &endptr, 16);
2932 if ((pch = strtok(NULL, ",")) != NULL)
2933 p_cfg->ble_io_cap = (UINT8) strtoul(pch, &endptr, 16);
2937 if ((pch = strtok(NULL, ",")) != NULL)
2938 p_cfg->ble_init_key = (UINT8) strtoul(pch, &endptr, 16);
2942 if ((pch = strtok(NULL, ",")) != NULL)
2943 p_cfg->ble_resp_key = (UINT8) strtoul(pch, &endptr, 16);
2947 if ((pch = strtok(NULL, ",")) != NULL)
2948 p_cfg->ble_max_key_size = (UINT8) strtoul(pch, &endptr, 16);
2955 BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2959 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2960 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2962 char prop_oob[PROPERTY_VALUE_MAX];
2963 BOOLEAN result = FALSE;
2964 bt_bdaddr_t bt_bd_addr;
2965 bdcpy(oob_cb.bdaddr, bd_addr);
2966 osi_property_get("service.brcm.bt.oob", prop_oob, "3");
2967 BTIF_TRACE_DEBUG("%s: prop_oob = %s", __func__, prop_oob);
2968 if (prop_oob[0] == '1')
2970 else if (prop_oob[0] == '2')
2974 fp = fopen(path, "rb");
2977 BTIF_TRACE_DEBUG("%s: failed to read OOB keys from %s", __func__, path);
2982 BTIF_TRACE_DEBUG("%s: read OOB data from %s", __func__, path);
2983 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2984 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2987 BTIF_TRACE_DEBUG("----%s: TRUE", __func__);
2988 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2989 oob_cb.bdaddr[0], oob_cb.bdaddr[1], oob_cb.bdaddr[2],
2990 oob_cb.bdaddr[3], oob_cb.bdaddr[4], oob_cb.bdaddr[5]);
2991 BTIF_TRACE_DEBUG("----%s: peer_bdaddr = %s", __func__, t);
2992 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2993 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
2994 p_c[8], p_c[9], p_c[10], p_c[11], p_c[12], p_c[13], p_c[14], p_c[15]);
2995 BTIF_TRACE_DEBUG("----%s: c = %s", __func__, t);
2996 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2997 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
2998 p_r[8], p_r[9], p_r[10], p_r[11], p_r[12], p_r[13], p_r[14], p_r[15]);
2999 BTIF_TRACE_DEBUG("----%s: r = %s", __func__, t);
3000 bdcpy(bt_bd_addr.address, bd_addr);
3001 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
3002 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
3005 BTIF_TRACE_DEBUG("%s: result=%d", __func__, result);
3008 #endif /* BTIF_DM_OOB_TEST */
3009 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
3011 static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
3013 bt_bdaddr_t bd_addr;
3014 bt_bdname_t bd_name;
3018 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
3020 /* Remote name update */
3021 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
3023 dev_type = BT_DEVICE_TYPE_BLE;
3025 btif_dm_update_ble_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
3026 (tBT_DEVICE_TYPE) dev_type);
3027 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
3028 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
3030 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3031 pairing_cb.is_ssp = FALSE;
3032 cod = COD_UNCLASSIFIED;
3034 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
3035 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
3036 p_ssp_key_notif->passkey);
3039 /*******************************************************************************
3041 ** Function btif_dm_ble_auth_cmpl_evt
3043 ** Description Executes authentication complete event in btif context
3047 *******************************************************************************/
3048 static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
3050 /* Save link key, if not temporary */
3051 bt_bdaddr_t bd_addr;
3052 bt_status_t status = BT_STATUS_FAIL;
3053 bt_bond_state_t state = BT_BOND_STATE_NONE;
3055 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
3057 /* Clear OOB data */
3058 memset(&oob_cb, 0, sizeof(oob_cb));
3060 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
3064 if (p_auth_cmpl->success)
3066 status = BT_STATUS_SUCCESS;
3067 state = BT_BOND_STATE_BONDED;
3070 bdcpy(bdaddr.address, p_auth_cmpl->bd_addr);
3071 if (btif_storage_get_remote_addr_type(&bdaddr, &addr_type) != BT_STATUS_SUCCESS)
3072 btif_storage_set_remote_addr_type(&bdaddr, p_auth_cmpl->addr_type);
3074 /* Test for temporary bonding */
3075 if (btm_get_bond_type_dev(p_auth_cmpl->bd_addr) == BOND_TYPE_TEMPORARY) {
3076 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
3078 btif_storage_remove_bonded_device(&bdaddr);
3079 state = BT_BOND_STATE_NONE;
3081 btif_dm_save_ble_bonding_keys();
3082 BTA_GATTC_Refresh(bd_addr.address);
3083 btif_dm_get_remote_services_by_transport(&bd_addr, BTA_GATT_TRANSPORT_LE);
3088 /*Map the HCI fail reason to bt status */
3089 switch (p_auth_cmpl->fail_reason)
3091 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
3092 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
3093 case BTA_DM_AUTH_SMP_UNKNOWN_ERR:
3094 case BTA_DM_AUTH_SMP_CONN_TOUT:
3095 btif_dm_remove_ble_bonding_keys();
3096 status = BT_STATUS_AUTH_FAILURE;
3098 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
3099 status = BT_STATUS_AUTH_REJECTED;
3102 btif_dm_remove_ble_bonding_keys();
3103 status = BT_STATUS_FAIL;
3107 bond_state_changed(status, &bd_addr, state);
3110 void btif_dm_load_ble_local_keys(void)
3112 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
3114 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
3115 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
3117 ble_local_key_cb.is_er_rcvd = TRUE;
3118 BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
3121 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
3122 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
3123 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
3124 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
3125 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
3126 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
3128 ble_local_key_cb.is_id_keys_rcvd = TRUE;
3129 BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
3133 void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
3134 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
3136 if (ble_local_key_cb.is_er_rcvd )
3138 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
3139 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
3142 if (ble_local_key_cb.is_id_keys_rcvd)
3144 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
3145 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
3146 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
3147 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
3149 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
3152 void btif_dm_save_ble_bonding_keys(void)
3155 bt_bdaddr_t bd_addr;
3157 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
3159 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3161 if (pairing_cb.ble.is_penc_key_rcvd)
3163 btif_storage_add_ble_bonding_key(&bd_addr,
3164 (char *) &pairing_cb.ble.penc_key,
3165 BTIF_DM_LE_KEY_PENC,
3166 sizeof(tBTM_LE_PENC_KEYS));
3169 if (pairing_cb.ble.is_pid_key_rcvd)
3171 btif_storage_add_ble_bonding_key(&bd_addr,
3172 (char *) &pairing_cb.ble.pid_key,
3174 sizeof(tBTM_LE_PID_KEYS));
3177 if (pairing_cb.ble.is_pcsrk_key_rcvd)
3179 btif_storage_add_ble_bonding_key(&bd_addr,
3180 (char *) &pairing_cb.ble.pcsrk_key,
3181 BTIF_DM_LE_KEY_PCSRK,
3182 sizeof(tBTM_LE_PCSRK_KEYS));
3185 if (pairing_cb.ble.is_lenc_key_rcvd)
3187 btif_storage_add_ble_bonding_key(&bd_addr,
3188 (char *) &pairing_cb.ble.lenc_key,
3189 BTIF_DM_LE_KEY_LENC,
3190 sizeof(tBTM_LE_LENC_KEYS));
3193 if (pairing_cb.ble.is_lcsrk_key_rcvd)
3195 btif_storage_add_ble_bonding_key(&bd_addr,
3196 (char *) &pairing_cb.ble.lcsrk_key,
3197 BTIF_DM_LE_KEY_LCSRK,
3198 sizeof(tBTM_LE_LCSRK_KEYS));
3201 if (pairing_cb.ble.is_lidk_key_rcvd)
3203 btif_storage_add_ble_bonding_key(&bd_addr,
3211 void btif_dm_remove_ble_bonding_keys(void)
3213 bt_bdaddr_t bd_addr;
3215 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
3217 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3218 btif_storage_remove_ble_bonding_keys(&bd_addr);
3221 /*******************************************************************************
3223 ** Function btif_dm_ble_sec_req_evt
3225 ** Description Eprocess security request event in btif context
3229 *******************************************************************************/
3230 void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
3232 bt_bdaddr_t bd_addr;
3233 bt_bdname_t bd_name;
3237 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
3239 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3241 BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
3245 /* Remote name update */
3246 if (!btif_get_device_type(p_ble_req->bd_addr, &dev_type))
3248 dev_type = BT_DEVICE_TYPE_BLE;
3250 btif_dm_update_ble_remote_properties(p_ble_req->bd_addr, p_ble_req->bd_name,
3251 (tBT_DEVICE_TYPE) dev_type);
3253 bdcpy(bd_addr.address, p_ble_req->bd_addr);
3254 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
3256 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3258 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
3259 pairing_cb.is_le_only = TRUE;
3260 pairing_cb.is_le_nc = FALSE;
3261 pairing_cb.is_ssp = TRUE;
3262 btm_set_bond_type_dev(p_ble_req->bd_addr, pairing_cb.bond_type);
3264 cod = COD_UNCLASSIFIED;
3266 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
3267 BT_SSP_VARIANT_CONSENT, 0);
3270 /*******************************************************************************
3272 ** Function btif_dm_ble_passkey_req_evt
3274 ** Description Executes pin request event in btif context
3278 *******************************************************************************/
3279 static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
3281 bt_bdaddr_t bd_addr;
3282 bt_bdname_t bd_name;
3286 /* Remote name update */
3287 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
3289 dev_type = BT_DEVICE_TYPE_BLE;
3291 btif_dm_update_ble_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,
3292 (tBT_DEVICE_TYPE) dev_type);
3294 bdcpy(bd_addr.address, p_pin_req->bd_addr);
3295 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
3297 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3298 pairing_cb.is_le_only = TRUE;
3300 cod = COD_UNCLASSIFIED;
3302 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
3303 &bd_addr, &bd_name, cod, FALSE);
3305 static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF *p_notif_req)
3307 /* TODO implement key notification for numeric comparison */
3308 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
3310 /* Remote name update */
3311 btif_update_remote_properties(p_notif_req->bd_addr , p_notif_req->bd_name,
3312 NULL, BT_DEVICE_TYPE_BLE);
3314 bt_bdaddr_t bd_addr;
3315 bdcpy(bd_addr.address, p_notif_req->bd_addr);
3317 bt_bdname_t bd_name;
3318 memcpy(bd_name.name, p_notif_req->bd_name, BD_NAME_LEN);
3320 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3321 pairing_cb.is_ssp = FALSE;
3322 pairing_cb.is_le_only = TRUE;
3323 pairing_cb.is_le_nc = TRUE;
3325 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
3326 COD_UNCLASSIFIED, BT_SSP_VARIANT_PASSKEY_CONFIRMATION,
3327 p_notif_req->passkey);
3330 static void btif_dm_ble_oob_req_evt(tBTA_DM_SP_RMT_OOB *req_oob_type)
3332 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
3334 bt_bdaddr_t bd_addr;
3335 bdcpy(bd_addr.address, req_oob_type->bd_addr);
3337 /* We currently support only Security Manager TK as OOB data. We already
3338 * checked if it's present in btif_dm_set_oob_for_le_io_req, but check here
3339 * again. If it's not present do nothing, pairing will timeout.
3341 if (is_empty_128bit(oob_cb.oob_data.sm_tk)) {
3345 /* make sure OOB data is for this particular device */
3346 if (memcmp(req_oob_type->bd_addr, oob_cb.bdaddr, BD_ADDR_LEN) != 0) {
3347 BTIF_TRACE_WARNING("%s: remote address didn't match OOB data address", __func__);
3351 /* Remote name update */
3352 btif_update_remote_properties(req_oob_type->bd_addr , req_oob_type->bd_name,
3353 NULL, BT_DEVICE_TYPE_BLE);
3355 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3356 pairing_cb.is_ssp = FALSE;
3357 pairing_cb.is_le_only = TRUE;
3358 pairing_cb.is_le_nc = FALSE;
3360 BTM_BleOobDataReply(req_oob_type->bd_addr, 0, 16, oob_cb.oob_data.sm_tk);
3363 void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
3364 tBT_DEVICE_TYPE dev_type)
3366 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
3369 static void btif_dm_ble_tx_test_cback(void *p)
3371 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
3372 (char *)p, 1, NULL);
3375 static void btif_dm_ble_rx_test_cback(void *p)
3377 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
3378 (char *)p, 1, NULL);
3381 static void btif_dm_ble_test_end_cback(void *p)
3383 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
3384 (char *)p, 3, NULL);
3386 /*******************************************************************************
3388 ** Function btif_le_test_mode
3390 ** Description Sends a HCI BLE Test command to the Controller
3392 ** Returns BT_STATUS_SUCCESS on success
3394 *******************************************************************************/
3395 bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
3398 case HCI_BLE_TRANSMITTER_TEST:
3399 if (len != 3) return BT_STATUS_PARM_INVALID;
3400 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
3402 case HCI_BLE_RECEIVER_TEST:
3403 if (len != 1) return BT_STATUS_PARM_INVALID;
3404 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
3406 case HCI_BLE_TEST_END:
3407 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
3410 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
3411 return BT_STATUS_UNSUPPORTED;
3413 return BT_STATUS_SUCCESS;
3417 void btif_dm_on_disable()
3419 /* cancel any pending pairing requests */
3420 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3422 bt_bdaddr_t bd_addr;
3424 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
3425 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3426 btif_dm_cancel_bond(&bd_addr);
3430 /*******************************************************************************
3432 ** Function btif_dm_read_energy_info
3434 ** Description Reads the energy info from controller
3438 *******************************************************************************/
3439 void btif_dm_read_energy_info()
3441 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
3442 BTA_DmBleGetEnergyInfo(bta_energy_info_cb);
3446 static char* btif_get_default_local_name() {
3447 if (btif_default_local_name[0] == '\0')
3449 int max_len = sizeof(btif_default_local_name) - 1;
3450 if (BTM_DEF_LOCAL_NAME[0] != '\0')
3452 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
3456 char prop_model[PROPERTY_VALUE_MAX];
3457 osi_property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
3458 strncpy(btif_default_local_name, prop_model, max_len);
3460 btif_default_local_name[max_len] = '\0';
3462 return btif_default_local_name;
3465 static void btif_stats_add_bond_event(const bt_bdaddr_t *bd_addr,
3466 bt_bond_function_t function,
3467 bt_bond_state_t state) {
3468 pthread_mutex_lock(&bond_event_lock);
3470 btif_bond_event_t* event = &btif_dm_bond_events[btif_events_end_index];
3471 memcpy(&event->bd_addr, bd_addr, sizeof(bt_bdaddr_t));
3472 event->function = function;
3473 event->state = state;
3474 clock_gettime(CLOCK_REALTIME, &event->timestamp);
3476 btif_num_bond_events++;
3477 btif_events_end_index = (btif_events_end_index + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1);
3478 if (btif_events_end_index == btif_events_start_index) {
3479 btif_events_start_index = (btif_events_start_index + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1);
3483 btif_get_device_type(bd_addr->address, &type);
3485 device_type_t device_type;
3487 case BT_DEVICE_TYPE_BREDR:
3488 device_type = DEVICE_TYPE_BREDR;
3490 case BT_DEVICE_TYPE_BLE:
3491 device_type = DEVICE_TYPE_LE;
3493 case BT_DEVICE_TYPE_DUMO:
3494 device_type = DEVICE_TYPE_DUMO;
3497 device_type = DEVICE_TYPE_UNKNOWN;
3501 uint32_t cod = get_cod(bd_addr);
3502 uint64_t ts = event->timestamp.tv_sec * 1000 +
3503 event->timestamp.tv_nsec / 1000000;
3504 metrics_pair_event(0, ts, cod, device_type);
3506 pthread_mutex_unlock(&bond_event_lock);
3509 void btif_debug_bond_event_dump(int fd) {
3510 pthread_mutex_lock(&bond_event_lock);
3511 dprintf(fd, "\nBond Events: \n");
3512 dprintf(fd, " Total Number of events: %zu\n", btif_num_bond_events);
3513 if (btif_num_bond_events > 0)
3514 dprintf(fd, " Time BD_ADDR Function State\n");
3516 for (size_t i = btif_events_start_index; i != btif_events_end_index;
3517 i = (i + 1) % (MAX_BTIF_BOND_EVENT_ENTRIES + 1)) {
3518 btif_bond_event_t* event = &btif_dm_bond_events[i];
3522 struct tm *tstamp = localtime(&event->timestamp.tv_sec);
3523 strftime(temptime, sizeof(temptime), "%H:%M:%S", tstamp);
3524 snprintf(eventtime, sizeof(eventtime), "%s.%03ld", temptime,
3525 event->timestamp.tv_nsec / 1000000);
3528 bdaddr_to_string(&event->bd_addr, bdaddr, sizeof(bdaddr));
3531 switch (event->function) {
3532 case BTIF_DM_FUNC_CREATE_BOND:
3533 func_name = "btif_dm_create_bond";
3535 case BTIF_DM_FUNC_REMOVE_BOND:
3536 func_name = "btif_dm_remove_bond";
3538 case BTIF_DM_FUNC_BOND_STATE_CHANGED:
3539 func_name = "bond_state_changed ";
3542 func_name = "Invalid value ";
3547 switch (event->state) {
3548 case BT_BOND_STATE_NONE:
3549 bond_state = "BOND_STATE_NONE";
3551 case BT_BOND_STATE_BONDING:
3552 bond_state = "BOND_STATE_BONDING";
3554 case BT_BOND_STATE_BONDED:
3555 bond_state = "BOND_STATE_BONDED";
3558 bond_state = "Invalid bond state";
3561 dprintf(fd, " %s %s %s %s\n", eventtime, bdaddr, func_name, bond_state);
3563 pthread_mutex_unlock(&bond_event_lock);