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"
37 #include <sys/types.h>
40 #include <hardware/bluetooth.h>
43 * TODO(armansito): cutils/properties.h is only being used to pull-in runtime
44 * settings on Android. Remove this conditional include once we have a generic
45 * way to obtain system properties.
47 #if !defined(OS_GENERIC)
48 #include <cutils/properties.h>
49 #endif /* !defined(OS_GENERIC) */
52 #include "bta_gatt_api.h"
54 #include "btif_config.h"
57 #include "btif_storage.h"
58 #include "btif_util.h"
60 #include "bt_common.h"
61 #include "bta_gatt_api.h"
62 #include "device/include/interop.h"
63 #include "include/stack_config.h"
64 #include "osi/include/log.h"
65 #include "osi/include/allocator.h"
66 #include "osi/include/log.h"
67 #include "stack_config.h"
68 #include "stack/btm/btm_int.h"
70 /******************************************************************************
72 ******************************************************************************/
74 #define COD_UNCLASSIFIED ((0x1F) << 8)
75 #define COD_HID_KEYBOARD 0x0540
76 #define COD_HID_POINTING 0x0580
77 #define COD_HID_COMBO 0x05C0
78 #define COD_HID_MAJOR 0x0500
79 #define COD_AV_HEADSETS 0x0404
80 #define COD_AV_HANDSFREE 0x0408
81 #define COD_AV_HEADPHONES 0x0418
82 #define COD_AV_PORTABLE_AUDIO 0x041C
83 #define COD_AV_HIFI_AUDIO 0x0428
85 #define BTIF_DM_DEFAULT_INQ_MAX_RESULTS 0
86 #define BTIF_DM_DEFAULT_INQ_MAX_DURATION 10
87 #define BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING 2
89 #define NUM_TIMEOUT_RETRIES 5
91 #define PROPERTY_PRODUCT_MODEL "ro.product.model"
92 #define DEFAULT_LOCAL_NAME_MAX 31
93 #if (DEFAULT_LOCAL_NAME_MAX > BTM_MAX_LOC_BD_NAME_LEN)
94 #error "default btif local name size exceeds stack supported length"
97 #if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
98 #define BTIF_DM_INTERLEAVE_DURATION_BR_ONE 2
99 #define BTIF_DM_INTERLEAVE_DURATION_LE_ONE 2
100 #define BTIF_DM_INTERLEAVE_DURATION_BR_TWO 3
101 #define BTIF_DM_INTERLEAVE_DURATION_LE_TWO 4
104 #define MAX_SDP_BL_ENTRIES 3
106 #define ENCRYPTED_BREDR 2
107 #define ENCRYPTED_LE 4
111 bt_bond_state_t state;
112 bt_bdaddr_t static_bdaddr;
114 tBTM_BOND_TYPE bond_type;
119 UINT8 autopair_attempts;
120 UINT8 timeout_retries;
121 UINT8 is_local_initiated;
123 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
125 BOOLEAN is_le_nc; /* LE Numeric comparison */
126 btif_dm_ble_cb_t ble;
128 } btif_dm_pairing_cb_t;
132 UINT8 ir[BT_OCTET16_LEN];
133 UINT8 irk[BT_OCTET16_LEN];
134 UINT8 dhk[BT_OCTET16_LEN];
135 }btif_dm_local_key_id_t;
140 UINT8 er[BT_OCTET16_LEN];
141 BOOLEAN is_id_keys_rcvd;
142 btif_dm_local_key_id_t id_keys; /* ID kyes */
144 }btif_dm_local_key_cb_t;
150 } btif_dm_remote_name_t;
156 BD_ADDR oob_bdaddr; /* peer bdaddr*/
162 UINT8 transport; /* 0=Unknown, 1=BR/EDR, 2=LE */
163 } btif_dm_create_bond_cb_t;
172 uint64_t energy_used;
173 } btif_activity_energy_info_cb_t;
177 unsigned int manufact_id;
180 #define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id))
182 #define MAX_SDP_BL_ENTRIES 3
183 #define UUID_HUMAN_INTERFACE_DEVICE "00001124-0000-1000-8000-00805f9b34fb"
185 static skip_sdp_entry_t sdp_blacklist[] = {{76}}; //Apple Mouse and Keyboard
187 /* This flag will be true if HCI_Inquiry is in progress */
188 static BOOLEAN btif_dm_inquiry_in_progress = FALSE;
190 /************************************************************************************
192 ************************************************************************************/
193 static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'};
195 /******************************************************************************
197 ******************************************************************************/
198 static btif_dm_pairing_cb_t pairing_cb;
199 static btif_dm_oob_cb_t oob_cb;
200 static void btif_dm_generic_evt(UINT16 event, char* p_param);
201 static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport);
202 static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name);
203 static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
204 DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type);
205 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
206 static btif_dm_local_key_cb_t ble_local_key_cb;
207 static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif);
208 static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl);
209 static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req);
210 static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF *p_notif_req) ;
213 static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
214 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
215 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status);
217 static char* btif_get_default_local_name();
218 /******************************************************************************
220 ******************************************************************************/
221 extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
222 extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
223 extern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
224 extern bt_status_t btif_av_sink_execute_service(BOOLEAN b_enable);
225 extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
226 extern bt_status_t btif_hf_client_execute_service(BOOLEAN b_enable);
227 extern bt_status_t btif_sdp_execute_service(BOOLEAN b_enable);
228 extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
229 extern void bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16);
231 /******************************************************************************
233 ******************************************************************************/
235 static void btif_dm_data_copy(uint16_t event, char *dst, char *src)
237 tBTA_DM_SEC *dst_dm_sec = (tBTA_DM_SEC*)dst;
238 tBTA_DM_SEC *src_dm_sec = (tBTA_DM_SEC*)src;
244 maybe_non_aligned_memcpy(dst_dm_sec, src_dm_sec, sizeof(*src_dm_sec));
246 if (event == BTA_DM_BLE_KEY_EVT)
248 dst_dm_sec->ble_key.p_key_value = osi_malloc(sizeof(tBTM_LE_KEY_VALUE));
249 assert(src_dm_sec->ble_key.p_key_value);
250 assert(dst_dm_sec->ble_key.p_key_value);
251 memcpy(dst_dm_sec->ble_key.p_key_value, src_dm_sec->ble_key.p_key_value, sizeof(tBTM_LE_KEY_VALUE));
255 static void btif_dm_data_free(uint16_t event, tBTA_DM_SEC *dm_sec)
257 if (event == BTA_DM_BLE_KEY_EVT)
258 osi_free(dm_sec->ble_key.p_key_value);
261 bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
264 BTIF_TRACE_DEBUG("%s service_id: %d", __FUNCTION__, service_id);
265 /* Check the service_ID and invoke the profile's BT state changed API */
268 case BTA_HFP_SERVICE_ID:
269 case BTA_HSP_SERVICE_ID:
271 btif_hf_execute_service(b_enable);
273 case BTA_A2DP_SOURCE_SERVICE_ID:
275 btif_av_execute_service(b_enable);
277 case BTA_A2DP_SINK_SERVICE_ID:
279 btif_av_sink_execute_service(b_enable);
281 case BTA_HID_SERVICE_ID:
283 btif_hh_execute_service(b_enable);
285 case BTA_HFP_HS_SERVICE_ID:
287 btif_hf_client_execute_service(b_enable);
289 case BTA_SDP_SERVICE_ID:
291 btif_sdp_execute_service(b_enable);
294 BTIF_TRACE_ERROR("%s: Unknown service being enabled", __FUNCTION__);
295 return BT_STATUS_FAIL;
297 return BT_STATUS_SUCCESS;
300 /*******************************************************************************
302 ** Function check_eir_remote_name
304 ** Description Check if remote name is in the EIR data
306 ** Returns TRUE if remote name found
307 ** Populate p_remote_name, if provided and remote name found
309 *******************************************************************************/
310 static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
311 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
313 UINT8 *p_eir_remote_name = NULL;
314 UINT8 remote_name_len = 0;
316 /* Check EIR for remote name and services */
317 if (p_search_data->inq_res.p_eir)
319 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
320 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
321 if (!p_eir_remote_name)
323 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
324 BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
327 if (p_eir_remote_name)
329 if (remote_name_len > BD_NAME_LEN)
330 remote_name_len = BD_NAME_LEN;
332 if (p_remote_name && p_remote_name_len)
334 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
335 *(p_remote_name + remote_name_len) = 0;
336 *p_remote_name_len = remote_name_len;
347 /*******************************************************************************
349 ** Function check_cached_remote_name
351 ** Description Check if remote name is in the NVRAM cache
353 ** Returns TRUE if remote name found
354 ** Populate p_remote_name, if provided and remote name found
356 *******************************************************************************/
357 static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
358 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
361 bt_bdaddr_t remote_bdaddr;
362 bt_property_t prop_name;
364 /* check if we already have it in our btif_storage cache */
365 bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
366 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
367 sizeof(bt_bdname_t), &bdname);
368 if (btif_storage_get_remote_device_property(
369 &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
371 if (p_remote_name && p_remote_name_len)
373 strcpy((char *)p_remote_name, (char *)bdname.name);
374 *p_remote_name_len = strlen((char *)p_remote_name);
382 BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
385 bt_property_t prop_name;
387 /* check if we already have it in our btif_storage cache */
388 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
389 sizeof(uint32_t), &remote_cod);
390 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
392 LOG_INFO(LOG_TAG, "%s remote_cod = 0x%08x cod = 0x%08x", __func__, remote_cod, cod);
393 if ((remote_cod & 0x7ff) == cod)
400 BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
403 bt_property_t prop_name;
405 /* check if we already have it in our btif_storage cache */
406 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
407 sizeof(uint32_t), &remote_cod);
408 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
409 &prop_name) == BT_STATUS_SUCCESS)
411 BTIF_TRACE_DEBUG("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
412 if ((remote_cod & 0x700) == cod)
418 BOOLEAN check_hid_le(const bt_bdaddr_t *remote_bdaddr)
420 uint32_t remote_dev_type;
421 bt_property_t prop_name;
423 /* check if we already have it in our btif_storage cache */
424 BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE,
425 sizeof(uint32_t), &remote_dev_type);
426 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
427 &prop_name) == BT_STATUS_SUCCESS)
429 if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE)
432 bdaddr_to_string(remote_bdaddr, bdstr, sizeof(bdstr));
433 if(btif_config_exist(bdstr, "HidAppId"))
440 /*****************************************************************************
442 ** Function check_sdp_bl
444 ** Description Checks if a given device is blacklisted to skip sdp
446 ** Parameters skip_sdp_entry
448 ** Returns TRUE if the device is present in blacklist, else FALSE
450 *******************************************************************************/
451 BOOLEAN check_sdp_bl(const bt_bdaddr_t *remote_bdaddr)
453 UINT16 manufacturer = 0;
455 UINT16 lmp_subver = 0;
456 bt_property_t prop_name;
457 bt_remote_version_t info;
459 if (remote_bdaddr == NULL)
462 /* fetch additional info about remote device used in iop query */
463 BTM_ReadRemoteVersion(*(BD_ADDR*)remote_bdaddr, &lmp_ver,
464 &manufacturer, &lmp_subver);
466 /* if not available yet, try fetching from config database */
467 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO,
468 sizeof(bt_remote_version_t), &info);
470 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
471 &prop_name) != BT_STATUS_SUCCESS)
476 manufacturer = info.manufacturer;
478 for (int i = 0; i < MAX_SDP_BL_ENTRIES; i++)
480 if (manufacturer == sdp_blacklist[i].manufact_id)
486 static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
488 // Send bonding state only once - based on outgoing/incoming we may receive duplicates
489 if ((pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING))
491 // Cross key pairing so send callback for static address
492 if (!bdaddr_is_empty(&pairing_cb.static_bdaddr))
494 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
499 if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
500 state = BT_BOND_STATE_NONE;
502 BTIF_TRACE_DEBUG("%s: state=%d, prev_state=%d, sdp_attempts = %d", __func__,
503 state, pairing_cb.state, pairing_cb.sdp_attempts);
505 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
507 if (state == BT_BOND_STATE_BONDING)
509 pairing_cb.state = state;
510 bdcpy(pairing_cb.bd_addr, bd_addr->address);
512 if (!pairing_cb.sdp_attempts)
513 memset(&pairing_cb, 0, sizeof(pairing_cb));
515 BTIF_TRACE_DEBUG("%s: BR-EDR service discovery active", __func__);
519 /* store remote version in bt config to always have access
521 static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
523 bt_property_t property;
525 UINT16 lmp_subver = 0;
527 tBTM_STATUS btm_status;
528 bt_remote_version_t info;
532 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
533 &mfct_set, &lmp_subver);
535 LOG_DEBUG(LOG_TAG, "remote version info [%s]: %x, %x, %x", bdaddr_to_string(p_bd, bdstr, sizeof(bdstr)),
536 lmp_ver, mfct_set, lmp_subver);
538 if (btm_status == BTM_SUCCESS)
540 // Always update cache to ensure we have availability whenever BTM API is not populated
541 info.manufacturer = mfct_set;
542 info.sub_ver = lmp_subver;
543 info.version = lmp_ver;
544 BTIF_STORAGE_FILL_PROPERTY(&property,
545 BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
547 status = btif_storage_set_remote_device_property(p_bd, &property);
548 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
552 static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
553 DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
555 int num_properties = 0;
556 bt_property_t properties[3];
560 bt_device_type_t dev_type;
562 memset(properties, 0, sizeof(properties));
563 bdcpy(bdaddr.address, bd_addr);
566 if (strlen((const char *) bd_name))
568 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
569 BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
570 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
571 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
575 /* class of device */
576 cod = devclass2uint(dev_class);
577 BTIF_TRACE_DEBUG("%s cod is 0x%06x", __func__, cod);
579 /* Try to retrieve cod from storage */
580 BTIF_TRACE_DEBUG("%s cod is 0, checking cod from storage", __func__);
581 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
582 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
583 status = btif_storage_get_remote_device_property(&bdaddr, &properties[num_properties]);
584 BTIF_TRACE_DEBUG("%s cod retrieved from storage is 0x%06x", __func__, cod);
586 BTIF_TRACE_DEBUG("%s cod is again 0, set as unclassified", __func__);
587 cod = COD_UNCLASSIFIED;
591 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
592 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
593 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
594 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
598 bt_property_t prop_name;
599 uint8_t remote_dev_type;
600 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_TYPE_OF_DEVICE,
601 sizeof(uint8_t), &remote_dev_type);
602 if (btif_storage_get_remote_device_property(&bdaddr, &prop_name) == BT_STATUS_SUCCESS)
603 dev_type = remote_dev_type | device_type;
605 dev_type = device_type;
607 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
608 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
609 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
610 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
613 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
614 status, &bdaddr, num_properties, properties);
617 /*******************************************************************************
619 ** Function btif_dm_cb_hid_remote_name
621 ** Description Remote name callback for HID device. Called in btif context
622 ** Special handling for HID devices
626 *******************************************************************************/
627 static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
629 BTIF_TRACE_DEBUG("%s: status=%d pairing_cb.state=%d", __FUNCTION__, p_remote_name->status, pairing_cb.state);
630 if (pairing_cb.state == BT_BOND_STATE_BONDING)
632 bt_bdaddr_t remote_bd;
634 bdcpy(remote_bd.address, pairing_cb.bd_addr);
636 if (p_remote_name->status == BTM_SUCCESS)
638 bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
641 bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
645 /*******************************************************************************
647 ** Function btif_dm_cb_create_bond
649 ** Description Create bond initiated from the BTIF thread context
650 ** Special handling for HID devices
654 *******************************************************************************/
655 static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport)
657 BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
658 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
660 #if BLE_INCLUDED == TRUE
664 bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr));
665 if (transport == BT_TRANSPORT_LE)
667 if (!btif_config_get_int((char const *)&bdstr,"DevType", &device_type))
669 btif_config_set_int(bdstr, "DevType", BT_DEVICE_TYPE_BLE);
671 if (btif_storage_get_remote_addr_type(bd_addr, &addr_type) != BT_STATUS_SUCCESS)
673 btif_storage_set_remote_addr_type(bd_addr, BLE_ADDR_PUBLIC);
676 if((btif_config_get_int((char const *)&bdstr,"DevType", &device_type) &&
677 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
678 (device_type & BT_DEVICE_TYPE_BLE) == BT_DEVICE_TYPE_BLE) || (transport == BT_TRANSPORT_LE))
680 BTA_DmAddBleDevice(bd_addr->address, addr_type, device_type);
684 #if BLE_INCLUDED == TRUE
685 if(is_hid && (device_type & BT_DEVICE_TYPE_BLE) == 0)
691 status = btif_hh_connect(bd_addr);
692 if(status != BT_STATUS_SUCCESS)
693 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
697 BTA_DmBondByTransport((UINT8 *)bd_addr->address, transport);
699 /* Track originator of bond creation */
700 pairing_cb.is_local_initiated = TRUE;
704 /*******************************************************************************
706 ** Function btif_dm_cb_remove_bond
708 ** Description remove bond initiated from the BTIF thread context
709 ** Special handling for HID devices
713 *******************************************************************************/
714 void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
716 /*special handling for HID devices */
717 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there
718 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
719 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
720 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
723 BTIF_TRACE_DEBUG("%s: Removing HH device", __func__);
724 BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
728 /*******************************************************************************
730 ** Function btif_dm_get_connection_state
732 ** Description Returns whether the remote device is currently connected
733 ** and whether encryption is active for the connection
735 ** Returns 0 if not connected; 1 if connected and > 1 if connection is
738 *******************************************************************************/
739 uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr)
741 uint8_t *bda = (uint8_t*)bd_addr->address;
742 uint16_t rc = BTA_DmGetConnectionState(bda);
748 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_BR_EDR);
749 BTIF_TRACE_DEBUG("%s: security flags (BR/EDR)=0x%02x", __FUNCTION__, flags);
750 if (flags & BTM_SEC_FLAG_ENCRYPTED)
751 rc |= ENCRYPTED_BREDR;
753 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_LE);
754 BTIF_TRACE_DEBUG("%s: security flags (LE)=0x%02x", __FUNCTION__, flags);
755 if (flags & BTM_SEC_FLAG_ENCRYPTED)
762 /*******************************************************************************
764 ** Function search_devices_copy_cb
766 ** Description Deep copy callback for search devices event
770 *******************************************************************************/
771 static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
773 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
774 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
779 BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
780 maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data));
783 case BTA_DM_INQ_RES_EVT:
785 if (p_src_data->inq_res.p_eir)
787 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
788 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
793 case BTA_DM_DISC_RES_EVT:
795 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
797 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
798 memcpy(p_dest_data->disc_res.p_raw_data,
799 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
806 static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
808 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
809 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
813 maybe_non_aligned_memcpy(p_dest_data, p_src_data, sizeof(*p_src_data));
816 case BTA_DM_DISC_RES_EVT:
818 if (p_src_data->disc_res.result == BTA_SUCCESS)
820 if (p_src_data->disc_res.num_uuids > 0)
822 p_dest_data->disc_res.p_uuid_list =
823 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
824 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
825 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
826 osi_freebuf(p_src_data->disc_res.p_uuid_list);
828 if (p_src_data->disc_res.p_raw_data != NULL)
830 osi_freebuf(p_src_data->disc_res.p_raw_data);
836 /******************************************************************************
838 ** BTIF DM callback events
840 *****************************************************************************/
842 /*******************************************************************************
844 ** Function btif_dm_pin_req_evt
846 ** Description Executes pin request event in btif context
850 *******************************************************************************/
851 static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
856 bt_pin_code_t pin_code;
859 /* Remote properties update */
860 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
862 dev_type = BT_DEVICE_TYPE_BREDR;
864 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
865 p_pin_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
867 bdcpy(bd_addr.address, p_pin_req->bd_addr);
868 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
870 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
872 cod = devclass2uint(p_pin_req->dev_class);
875 BTIF_TRACE_DEBUG("%s cod is 0, set as unclassified", __func__);
876 cod = COD_UNCLASSIFIED;
879 /* check for auto pair possiblity only if bond was initiated by local device */
880 if (pairing_cb.is_local_initiated && (p_pin_req->min_16_digit == FALSE))
882 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
883 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
884 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
885 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
886 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
887 check_cod(&bd_addr, COD_HID_POINTING))
889 BTIF_TRACE_DEBUG("%s()cod matches for auto pair", __FUNCTION__);
890 /* Check if this device can be auto paired */
891 if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) &&
892 (pairing_cb.autopair_attempts == 0))
894 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
895 pin_code.pin[0] = 0x30;
896 pin_code.pin[1] = 0x30;
897 pin_code.pin[2] = 0x30;
898 pin_code.pin[3] = 0x30;
900 pairing_cb.autopair_attempts++;
901 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
905 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
906 check_cod(&bd_addr, COD_HID_COMBO))
908 if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) &&
909 (pairing_cb.autopair_attempts == 0))
911 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
912 pin_code.pin[0] = 0x30;
913 pin_code.pin[1] = 0x30;
914 pin_code.pin[2] = 0x30;
915 pin_code.pin[3] = 0x30;
917 pairing_cb.autopair_attempts++;
918 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
923 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
924 &bd_addr, &bd_name, cod, p_pin_req->min_16_digit);
927 /*******************************************************************************
929 ** Function btif_dm_ssp_cfm_req_evt
931 ** Description Executes SSP confirm request event in btif context
935 *******************************************************************************/
936 static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
941 BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
944 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
946 /* Remote properties update */
947 if (!btif_get_device_type(p_ssp_cfm_req->bd_addr, &dev_type))
949 dev_type = BT_DEVICE_TYPE_BREDR;
951 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
952 p_ssp_cfm_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
954 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
955 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
957 /* Set the pairing_cb based on the local & remote authentication requirements */
958 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
960 /* if just_works and bonding bit is not set treat this as temporary */
961 if (p_ssp_cfm_req->just_works && !(p_ssp_cfm_req->loc_auth_req & BTM_AUTH_BONDS) &&
962 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
963 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
964 pairing_cb.bond_type = BOND_TYPE_TEMPORARY;
966 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
968 btm_set_bond_type_dev(p_ssp_cfm_req->bd_addr, pairing_cb.bond_type);
970 pairing_cb.is_ssp = TRUE;
972 /* If JustWorks auto-accept */
973 if (p_ssp_cfm_req->just_works)
975 /* Pairing consent for JustWorks needed if:
976 * 1. Incoming (non-temporary) pairing is detected AND
977 * 2. local IO capabilities are DisplayYesNo AND
978 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
980 if (is_incoming && pairing_cb.bond_type != BOND_TYPE_TEMPORARY &&
981 ((p_ssp_cfm_req->loc_io_caps == HCI_IO_CAP_DISPLAY_YESNO) &&
982 (p_ssp_cfm_req->rmt_io_caps == HCI_IO_CAP_DISPLAY_ONLY ||
983 p_ssp_cfm_req->rmt_io_caps == HCI_IO_CAP_NO_IO)))
985 BTIF_TRACE_EVENT("%s: User consent needed for incoming pairing request. loc_io_caps: %d, rmt_io_caps: %d",
986 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
990 BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __FUNCTION__);
991 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
996 cod = devclass2uint(p_ssp_cfm_req->dev_class);
999 LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__);
1000 cod = COD_UNCLASSIFIED;
1003 pairing_cb.sdp_attempts = 0;
1004 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
1005 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
1006 p_ssp_cfm_req->num_val);
1009 static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
1011 bt_bdaddr_t bd_addr;
1012 bt_bdname_t bd_name;
1016 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
1018 /* Remote properties update */
1019 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
1021 dev_type = BT_DEVICE_TYPE_BREDR;
1023 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
1024 p_ssp_key_notif->dev_class, (tBT_DEVICE_TYPE) dev_type);
1026 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
1027 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
1029 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1030 pairing_cb.is_ssp = TRUE;
1031 cod = devclass2uint(p_ssp_key_notif->dev_class);
1034 LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__);
1035 cod = COD_UNCLASSIFIED;
1038 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
1039 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
1040 p_ssp_key_notif->passkey);
1042 /*******************************************************************************
1044 ** Function btif_dm_auth_cmpl_evt
1046 ** Description Executes authentication complete event in btif context
1050 *******************************************************************************/
1051 static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
1053 /* Save link key, if not temporary */
1054 bt_bdaddr_t bd_addr;
1055 bt_status_t status = BT_STATUS_FAIL;
1056 bt_bond_state_t state = BT_BOND_STATE_NONE;
1057 BOOLEAN skip_sdp = FALSE;
1059 BTIF_TRACE_DEBUG("%s: bond state=%d", __func__, pairing_cb.state);
1061 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
1062 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
1064 if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) ||
1065 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
1066 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) ||
1067 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB_P_256) ||
1068 pairing_cb.bond_type == BOND_TYPE_PERSISTENT)
1071 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, bond_type=%d",
1072 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1073 ret = btif_storage_add_bonded_device(&bd_addr,
1074 p_auth_cmpl->key, p_auth_cmpl->key_type,
1075 pairing_cb.pin_code_len);
1076 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
1080 BTIF_TRACE_DEBUG("%s: Temporary key. Not storing. key_type=0x%x, bond_type=%d",
1081 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1082 if(pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
1084 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
1086 btif_storage_remove_bonded_device(&bd_addr);
1087 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1093 // Skip SDP for certain HID Devices
1094 if (p_auth_cmpl->success)
1096 #if BLE_INCLUDED == TRUE
1097 btif_storage_set_remote_addr_type(&bd_addr, p_auth_cmpl->addr_type);
1099 btif_update_remote_properties(p_auth_cmpl->bd_addr,
1100 p_auth_cmpl->bd_name, NULL, p_auth_cmpl->dev_type);
1101 pairing_cb.timeout_retries = 0;
1102 status = BT_STATUS_SUCCESS;
1103 state = BT_BOND_STATE_BONDED;
1104 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
1106 if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr, COD_HID_MAJOR))
1108 LOG_WARN(LOG_TAG, "%s:skip SDP", __FUNCTION__);
1111 if(!pairing_cb.is_local_initiated && skip_sdp)
1113 bond_state_changed(status, &bd_addr, state);
1115 LOG_WARN(LOG_TAG, "%s: Incoming HID Connection",__FUNCTION__);
1117 bt_bdaddr_t bd_addr;
1119 char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE;
1121 string_to_uuid(uuid_str, &uuid);
1123 prop.type = BT_PROPERTY_UUIDS;
1125 prop.len = MAX_UUID_SIZE;
1127 /* Send the event to the BTIF */
1128 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1129 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1133 /* Trigger SDP on the device */
1134 pairing_cb.sdp_attempts = 1;;
1136 #if BLE_INCLUDED == TRUE
1137 /* If bonded due to cross-key, save the static address too*/
1138 if(pairing_cb.state == BT_BOND_STATE_BONDING &&
1139 (bdcmp(p_auth_cmpl->bd_addr, pairing_cb.bd_addr) != 0))
1141 BTIF_TRACE_DEBUG("%s: bonding initiated due to cross key, adding static address",
1143 bdcpy(pairing_cb.static_bdaddr.address, p_auth_cmpl->bd_addr);
1147 if(btif_dm_inquiry_in_progress)
1148 btif_dm_cancel_discovery();
1150 btif_dm_get_remote_services(&bd_addr);
1152 // Do not call bond_state_changed_cb yet. Wait until remote service discovery is complete
1156 // Map the HCI fail reason to bt status
1157 switch(p_auth_cmpl->fail_reason)
1159 case HCI_ERR_PAGE_TIMEOUT:
1160 if (interop_match(INTEROP_AUTO_RETRY_PAIRING, &bd_addr)
1161 && pairing_cb.timeout_retries)
1163 BTIF_TRACE_WARNING("%s() - Pairing timeout; retrying (%d) ...", __FUNCTION__, pairing_cb.timeout_retries);
1164 --pairing_cb.timeout_retries;
1165 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
1169 case HCI_ERR_CONNECTION_TOUT:
1170 status = BT_STATUS_RMT_DEV_DOWN;
1173 case HCI_ERR_PAIRING_NOT_ALLOWED:
1174 status = BT_STATUS_AUTH_REJECTED;
1177 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
1178 status = BT_STATUS_AUTH_FAILURE;
1181 /* map the auth failure codes, so we can retry pairing if necessary */
1182 case HCI_ERR_AUTH_FAILURE:
1183 case HCI_ERR_KEY_MISSING:
1184 btif_storage_remove_bonded_device(&bd_addr);
1185 case HCI_ERR_HOST_REJECT_SECURITY:
1186 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
1187 case HCI_ERR_UNIT_KEY_USED:
1188 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
1189 case HCI_ERR_INSUFFCIENT_SECURITY:
1190 case HCI_ERR_PEER_USER:
1191 case HCI_ERR_UNSPECIFIED:
1192 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d",
1193 __FUNCTION__, p_auth_cmpl->fail_reason);
1194 if (pairing_cb.autopair_attempts == 1)
1196 BTIF_TRACE_DEBUG("%s(): Adding device to blacklist ", __FUNCTION__);
1198 /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class */
1199 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
1200 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
1201 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
1202 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
1203 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
1204 check_cod(&bd_addr, COD_HID_POINTING))
1206 btif_storage_add_device_to_autopair_blacklist (&bd_addr);
1208 pairing_cb.autopair_attempts++;
1210 /* Create the Bond once again */
1211 BTIF_TRACE_DEBUG("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
1212 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
1217 /* if autopair attempts are more than 1, or not attempted */
1218 status = BT_STATUS_AUTH_FAILURE;
1223 status = BT_STATUS_FAIL;
1225 /* Special Handling for HID Devices */
1226 if (check_cod(&bd_addr, COD_HID_POINTING)) {
1227 /* Remove Device as bonded in nvram as authentication failed */
1228 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __FUNCTION__);
1229 btif_storage_remove_bonded_device(&bd_addr);
1231 bond_state_changed(status, &bd_addr, state);
1235 /******************************************************************************
1237 ** Function btif_dm_search_devices_evt
1239 ** Description Executes search devices callback events in btif context
1243 ******************************************************************************/
1244 static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
1246 tBTA_DM_SEARCH *p_search_data;
1247 BTIF_TRACE_EVENT("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
1251 case BTA_DM_DISC_RES_EVT:
1253 p_search_data = (tBTA_DM_SEARCH *)p_param;
1254 /* Remote name update */
1255 if (strlen((const char *) p_search_data->disc_res.bd_name))
1257 bt_property_t properties[1];
1261 properties[0].type = BT_PROPERTY_BDNAME;
1262 properties[0].val = p_search_data->disc_res.bd_name;
1263 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
1264 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
1266 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
1267 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
1268 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1269 status, &bdaddr, 1, properties);
1271 /* TODO: Services? */
1275 case BTA_DM_INQ_RES_EVT:
1277 /* inquiry result */
1281 UINT8 remote_name_len;
1282 tBTA_SERVICE_MASK services = 0;
1285 p_search_data = (tBTA_DM_SEARCH *)p_param;
1286 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
1288 BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __FUNCTION__, bdaddr_to_string(&bdaddr, bdstr, sizeof(bdstr)),
1289 #if (BLE_INCLUDED == TRUE)
1290 p_search_data->inq_res.device_type);
1292 BT_DEVICE_TYPE_BREDR);
1296 cod = devclass2uint (p_search_data->inq_res.dev_class);
1299 LOG_DEBUG(LOG_TAG, "%s cod is 0, set as unclassified", __func__);
1300 cod = COD_UNCLASSIFIED;
1303 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1304 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1306 /* Check EIR for remote name and services */
1307 if (p_search_data->inq_res.p_eir)
1309 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
1310 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
1311 /* TODO: Get the service list and check to see which uuids we got and send it back to the client. */
1315 bt_property_t properties[5];
1316 bt_device_type_t dev_type;
1317 uint32_t num_properties = 0;
1321 memset(properties, 0, sizeof(properties));
1323 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1324 BT_PROPERTY_BDADDR, sizeof(bdaddr), &bdaddr);
1327 /* Don't send BDNAME if it is empty */
1330 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1332 strlen((char *)bdname.name), &bdname);
1337 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1338 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
1341 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1342 /* FixMe: Assumption is that bluetooth.h and BTE enums match */
1344 /* Verify if the device is dual mode in NVRAM */
1345 int stored_device_type = 0;
1346 if (btif_get_device_type(bdaddr.address, &stored_device_type) &&
1347 ((stored_device_type == BT_DEVICE_TYPE_BLE &&
1348 p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BREDR) ||
1349 (stored_device_type == BT_DEVICE_TYPE_BREDR &&
1350 p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE))) {
1351 dev_type = BT_DEVICE_TYPE_DUMO;
1353 dev_type = p_search_data->inq_res.device_type;
1356 if (p_search_data->inq_res.device_type == BT_DEVICE_TYPE_BLE)
1357 addr_type = p_search_data->inq_res.ble_addr_type;
1359 dev_type = BT_DEVICE_TYPE_BREDR;
1361 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1362 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
1365 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
1366 BT_PROPERTY_REMOTE_RSSI, sizeof(int8_t),
1367 &(p_search_data->inq_res.rssi));
1370 status = btif_storage_add_remote_device(&bdaddr, num_properties, properties);
1371 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device (inquiry)", status);
1372 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1373 status = btif_storage_set_remote_addr_type(&bdaddr, addr_type);
1374 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote addr type (inquiry)", status);
1376 /* Callback to notify upper layer of device */
1377 HAL_CBACK(bt_hal_cbacks, device_found_cb,
1378 num_properties, properties);
1383 case BTA_DM_INQ_CMPL_EVT:
1385 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1386 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1387 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1388 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1389 bte_scan_filt_param_cfg_evt, 0);
1393 case BTA_DM_DISC_CMPL_EVT:
1395 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1398 case BTA_DM_SEARCH_CANCEL_CMPL_EVT:
1400 /* if inquiry is not in progress and we get a cancel event, then
1401 * it means we are done with inquiry, but remote_name fetches are in
1404 * if inquiry is in progress, then we don't want to act on this cancel_cmpl_evt
1405 * but instead wait for the cancel_cmpl_evt via the Busy Level
1408 if (btif_dm_inquiry_in_progress == FALSE)
1410 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1411 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
1412 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
1413 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
1414 bte_scan_filt_param_cfg_evt, 0);
1416 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STOPPED);
1423 /*******************************************************************************
1425 ** Function btif_dm_search_services_evt
1427 ** Description Executes search services event in btif context
1431 *******************************************************************************/
1432 static void btif_dm_search_services_evt(UINT16 event, char *p_param)
1434 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1436 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
1439 case BTA_DM_DISC_RES_EVT:
1443 bt_bdaddr_t bd_addr;
1446 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1448 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
1449 p_data->disc_res.result, p_data->disc_res.services);
1450 if ((p_data->disc_res.result != BTA_SUCCESS) &&
1451 (pairing_cb.state == BT_BOND_STATE_BONDING ) &&
1452 (pairing_cb.sdp_attempts < BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING))
1454 BTIF_TRACE_WARNING("%s:SDP failed after bonding re-attempting", __FUNCTION__);
1455 pairing_cb.sdp_attempts++;
1456 btif_dm_get_remote_services(&bd_addr);
1459 prop.type = BT_PROPERTY_UUIDS;
1461 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0))
1463 prop.val = p_data->disc_res.p_uuid_list;
1464 prop.len = p_data->disc_res.num_uuids * MAX_UUID_SIZE;
1465 for (i=0; i < p_data->disc_res.num_uuids; i++)
1468 uuid_to_string_legacy((bt_uuid_t*)(p_data->disc_res.p_uuid_list + (i*MAX_UUID_SIZE)), temp);
1469 LOG_INFO(LOG_TAG, "%s index:%d uuid:%s", __func__, i, temp);
1473 /* onUuidChanged requires getBondedDevices to be populated.
1474 ** bond_state_changed needs to be sent prior to remote_device_property
1476 if ((pairing_cb.state == BT_BOND_STATE_BONDING) &&
1477 ((bdcmp(p_data->disc_res.bd_addr, pairing_cb.bd_addr) == 0) ||
1478 (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr.address) == 0)) &&
1479 pairing_cb.sdp_attempts > 0)
1481 BTIF_TRACE_DEBUG("%s Remote Service SDP done. Call bond_state_changed_cb BONDED",
1483 pairing_cb.sdp_attempts = 0;
1485 // If bonding occured due to cross-key pairing, send bonding callback
1486 // for static address now
1487 if (bdcmp(p_data->disc_res.bd_addr, pairing_cb.static_bdaddr.address) == 0)
1488 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1490 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDED);
1493 if (p_data->disc_res.num_uuids != 0)
1495 /* Also write this to the NVRAM */
1496 ret = btif_storage_set_remote_device_property(&bd_addr, &prop);
1497 ASSERTC(ret == BT_STATUS_SUCCESS, "storing remote services failed", ret);
1498 /* Send the event to the BTIF */
1499 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1500 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1505 case BTA_DM_DISC_CMPL_EVT:
1509 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1510 case BTA_DM_DISC_BLE_RES_EVT:
1511 BTIF_TRACE_DEBUG("%s:, services 0x%x)", __FUNCTION__,
1512 p_data->disc_ble_res.service.uu.uuid16);
1516 if (p_data->disc_ble_res.service.uu.uuid16 == UUID_SERVCLASS_LE_HID)
1518 BTIF_TRACE_DEBUG("%s: Found HOGP UUID",__FUNCTION__);
1520 bt_bdaddr_t bd_addr;
1524 bta_gatt_convert_uuid16_to_uuid128(uuid.uu,p_data->disc_ble_res.service.uu.uuid16);
1528 unsigned char c = uuid.uu[j];
1529 uuid.uu[j] = uuid.uu[i];
1535 uuid_to_string_legacy(&uuid, temp);
1536 LOG_INFO(LOG_TAG, "%s uuid:%s", __func__, temp);
1538 bdcpy(bd_addr.address, p_data->disc_ble_res.bd_addr);
1539 prop.type = BT_PROPERTY_UUIDS;
1541 prop.len = MAX_UUID_SIZE;
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);
1547 /* Send the event to the BTIF */
1548 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1549 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1553 #endif /* BLE_INCLUDED */
1557 ASSERTC(0, "unhandled search services event", event);
1563 /*******************************************************************************
1565 ** Function btif_dm_remote_service_record_evt
1567 ** Description Executes search service record event in btif context
1571 *******************************************************************************/
1572 static void btif_dm_remote_service_record_evt(UINT16 event, char *p_param)
1574 tBTA_DM_SEARCH *p_data = (tBTA_DM_SEARCH*)p_param;
1576 BTIF_TRACE_EVENT("%s: event = %d", __FUNCTION__, event);
1579 case BTA_DM_DISC_RES_EVT:
1581 bt_service_record_t rec;
1583 bt_bdaddr_t bd_addr;
1585 memset(&rec, 0, sizeof(bt_service_record_t));
1586 bdcpy(bd_addr.address, p_data->disc_res.bd_addr);
1588 BTIF_TRACE_DEBUG("%s:(result=0x%x, services 0x%x)", __FUNCTION__,
1589 p_data->disc_res.result, p_data->disc_res.services);
1590 prop.type = BT_PROPERTY_SERVICE_RECORD;
1591 prop.val = (void*)&rec;
1592 prop.len = sizeof(rec);
1594 /* disc_res.result is overloaded with SCN. Cannot check result */
1595 p_data->disc_res.services &= ~BTA_USER_SERVICE_MASK;
1596 /* TODO: Get the UUID as well */
1597 rec.channel = p_data->disc_res.result - 3;
1598 /* TODO: Need to get the service name using p_raw_data */
1601 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1602 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1608 ASSERTC(0, "unhandled remote service record event", event);
1614 /*******************************************************************************
1616 ** Function btif_dm_upstreams_cback
1618 ** Description Executes UPSTREAMS events in btif context
1622 *******************************************************************************/
1623 static void btif_dm_upstreams_evt(UINT16 event, char* p_param)
1625 tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param;
1626 tBTA_SERVICE_MASK service_mask;
1628 bt_bdaddr_t bd_addr;
1630 BTIF_TRACE_EVENT("btif_dm_upstreams_cback ev: %s", dump_dm_event(event));
1634 case BTA_DM_ENABLE_EVT:
1639 prop.type = BT_PROPERTY_BDNAME;
1640 prop.len = BD_NAME_LEN;
1641 prop.val = (void*)bdname;
1643 status = btif_storage_get_adapter_property(&prop);
1644 if (status == BT_STATUS_SUCCESS)
1646 /* A name exists in the storage. Make this the device name */
1647 BTA_DmSetDeviceName((char*)prop.val);
1651 /* Storage does not have a name yet.
1652 * Use the default name and write it to the chip
1654 BTA_DmSetDeviceName(btif_get_default_local_name());
1657 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1658 /* Enable local privacy */
1659 BTA_DmBleConfigLocalPrivacy(BLE_LOCAL_PRIVACY_ENABLED);
1662 /* for each of the enabled services in the mask, trigger the profile
1664 service_mask = btif_get_enabled_services_mask();
1665 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1668 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1670 btif_in_execute_service_request(i, TRUE);
1673 /* clear control blocks */
1674 memset(&pairing_cb, 0, sizeof(btif_dm_pairing_cb_t));
1675 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
1677 /* This function will also trigger the adapter_properties_cb
1678 ** and bonded_devices_info_cb
1680 btif_storage_load_bonded_devices();
1682 btif_storage_load_autopair_device_list();
1684 btif_enable_bluetooth_evt(p_data->enable.status);
1688 case BTA_DM_DISABLE_EVT:
1689 /* for each of the enabled services in the mask, trigger the profile
1691 service_mask = btif_get_enabled_services_mask();
1692 for (i=0; i <= BTA_MAX_SERVICE_ID; i++)
1695 (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(i)))
1697 btif_in_execute_service_request(i, FALSE);
1700 btif_disable_bluetooth_evt();
1703 case BTA_DM_PIN_REQ_EVT:
1704 btif_dm_pin_req_evt(&p_data->pin_req);
1707 case BTA_DM_AUTH_CMPL_EVT:
1708 btif_dm_auth_cmpl_evt(&p_data->auth_cmpl);
1711 case BTA_DM_BOND_CANCEL_CMPL_EVT:
1712 if (pairing_cb.state == BT_BOND_STATE_BONDING)
1714 bdcpy(bd_addr.address, pairing_cb.bd_addr);
1715 btm_set_bond_type_dev(pairing_cb.bd_addr, BOND_TYPE_UNKNOWN);
1716 bond_state_changed(p_data->bond_cancel_cmpl.result, &bd_addr, BT_BOND_STATE_NONE);
1720 case BTA_DM_SP_CFM_REQ_EVT:
1721 btif_dm_ssp_cfm_req_evt(&p_data->cfm_req);
1723 case BTA_DM_SP_KEY_NOTIF_EVT:
1724 btif_dm_ssp_key_notif_evt(&p_data->key_notif);
1727 case BTA_DM_DEV_UNPAIRED_EVT:
1728 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1729 btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);
1731 /*special handling for HID devices */
1732 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
1733 btif_hh_remove_device(bd_addr);
1735 btif_storage_remove_bonded_device(&bd_addr);
1736 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1739 case BTA_DM_BUSY_LEVEL_EVT:
1742 if (p_data->busy_level.level_flags & BTM_BL_INQUIRY_PAGING_MASK)
1744 if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_STARTED)
1746 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1747 BT_DISCOVERY_STARTED);
1748 btif_dm_inquiry_in_progress = TRUE;
1750 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_CANCELLED)
1752 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb,
1753 BT_DISCOVERY_STOPPED);
1754 btif_dm_inquiry_in_progress = FALSE;
1756 else if (p_data->busy_level.level_flags == BTM_BL_INQUIRY_COMPLETE)
1758 btif_dm_inquiry_in_progress = FALSE;
1763 case BTA_DM_LINK_UP_EVT:
1764 bdcpy(bd_addr.address, p_data->link_up.bd_addr);
1765 BTIF_TRACE_DEBUG("BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED");
1767 btif_update_remote_version_property(&bd_addr);
1769 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1770 &bd_addr, BT_ACL_STATE_CONNECTED);
1773 case BTA_DM_LINK_DOWN_EVT:
1774 bdcpy(bd_addr.address, p_data->link_down.bd_addr);
1775 btm_set_bond_type_dev(p_data->link_down.bd_addr, BOND_TYPE_UNKNOWN);
1776 BTIF_TRACE_DEBUG("BTA_DM_LINK_DOWN_EVT. Sending BT_ACL_STATE_DISCONNECTED");
1777 HAL_CBACK(bt_hal_cbacks, acl_state_changed_cb, BT_STATUS_SUCCESS,
1778 &bd_addr, BT_ACL_STATE_DISCONNECTED);
1781 case BTA_DM_HW_ERROR_EVT:
1782 BTIF_TRACE_ERROR("Received H/W Error. ");
1783 /* Flush storage data */
1784 btif_config_flush();
1785 usleep(100000); /* 100milliseconds */
1786 /* Killing the process to force a restart as part of fault tolerance */
1787 kill(getpid(), SIGKILL);
1790 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
1791 case BTA_DM_BLE_KEY_EVT:
1792 BTIF_TRACE_DEBUG("BTA_DM_BLE_KEY_EVT key_type=0x%02x ", p_data->ble_key.key_type);
1794 /* If this pairing is by-product of local initiated GATT client Read or Write,
1795 BTA would not have sent BTA_DM_BLE_SEC_REQ_EVT event and Bond state would not
1796 have setup properly. Setup pairing_cb and notify App about Bonding state now*/
1797 if (pairing_cb.state != BT_BOND_STATE_BONDING)
1799 BTIF_TRACE_DEBUG("Bond state not sent to App so far.Notify the app now");
1800 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t*)p_data->ble_key.bd_addr,
1801 BT_BOND_STATE_BONDING);
1803 else if (memcmp (pairing_cb.bd_addr, p_data->ble_key.bd_addr, BD_ADDR_LEN)!=0)
1805 BTIF_TRACE_ERROR("BD mismatch discard BLE key_type=%d ",p_data->ble_key.key_type);
1809 switch (p_data->ble_key.key_type)
1811 case BTA_LE_KEY_PENC:
1812 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PENC");
1813 pairing_cb.ble.is_penc_key_rcvd = TRUE;
1814 pairing_cb.ble.penc_key = p_data->ble_key.p_key_value->penc_key;
1817 case BTA_LE_KEY_PID:
1818 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PID");
1819 pairing_cb.ble.is_pid_key_rcvd = TRUE;
1820 pairing_cb.ble.pid_key = p_data->ble_key.p_key_value->pid_key;
1823 case BTA_LE_KEY_PCSRK:
1824 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_PCSRK");
1825 pairing_cb.ble.is_pcsrk_key_rcvd = TRUE;
1826 pairing_cb.ble.pcsrk_key = p_data->ble_key.p_key_value->pcsrk_key;
1829 case BTA_LE_KEY_LENC:
1830 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LENC");
1831 pairing_cb.ble.is_lenc_key_rcvd = TRUE;
1832 pairing_cb.ble.lenc_key = p_data->ble_key.p_key_value->lenc_key;
1835 case BTA_LE_KEY_LCSRK:
1836 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LCSRK");
1837 pairing_cb.ble.is_lcsrk_key_rcvd = TRUE;
1838 pairing_cb.ble.lcsrk_key = p_data->ble_key.p_key_value->lcsrk_key;
1841 case BTA_LE_KEY_LID:
1842 BTIF_TRACE_DEBUG("Rcv BTA_LE_KEY_LID");
1843 pairing_cb.ble.is_lidk_key_rcvd = TRUE;
1847 BTIF_TRACE_ERROR("unknown BLE key type (0x%02x)", p_data->ble_key.key_type);
1851 case BTA_DM_BLE_SEC_REQ_EVT:
1852 BTIF_TRACE_DEBUG("BTA_DM_BLE_SEC_REQ_EVT. ");
1853 btif_dm_ble_sec_req_evt(&p_data->ble_req);
1855 case BTA_DM_BLE_PASSKEY_NOTIF_EVT:
1856 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_NOTIF_EVT. ");
1857 btif_dm_ble_key_notif_evt(&p_data->key_notif);
1859 case BTA_DM_BLE_PASSKEY_REQ_EVT:
1860 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
1861 btif_dm_ble_passkey_req_evt(&p_data->pin_req);
1863 case BTA_DM_BLE_NC_REQ_EVT:
1864 BTIF_TRACE_DEBUG("BTA_DM_BLE_PASSKEY_REQ_EVT. ");
1865 btif_dm_ble_key_nc_req_evt(&p_data->key_notif);
1867 case BTA_DM_BLE_OOB_REQ_EVT:
1868 BTIF_TRACE_DEBUG("BTA_DM_BLE_OOB_REQ_EVT. ");
1870 case BTA_DM_BLE_LOCAL_IR_EVT:
1871 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_IR_EVT. ");
1872 ble_local_key_cb.is_id_keys_rcvd = TRUE;
1873 memcpy(&ble_local_key_cb.id_keys.irk[0],
1874 &p_data->ble_id_keys.irk[0], sizeof(BT_OCTET16));
1875 memcpy(&ble_local_key_cb.id_keys.ir[0],
1876 &p_data->ble_id_keys.ir[0], sizeof(BT_OCTET16));
1877 memcpy(&ble_local_key_cb.id_keys.dhk[0],
1878 &p_data->ble_id_keys.dhk[0], sizeof(BT_OCTET16));
1879 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.irk[0],
1880 BTIF_DM_LE_LOCAL_KEY_IRK,
1882 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.ir[0],
1883 BTIF_DM_LE_LOCAL_KEY_IR,
1885 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.id_keys.dhk[0],
1886 BTIF_DM_LE_LOCAL_KEY_DHK,
1889 case BTA_DM_BLE_LOCAL_ER_EVT:
1890 BTIF_TRACE_DEBUG("BTA_DM_BLE_LOCAL_ER_EVT. ");
1891 ble_local_key_cb.is_er_rcvd = TRUE;
1892 memcpy(&ble_local_key_cb.er[0], &p_data->ble_er[0], sizeof(BT_OCTET16));
1893 btif_storage_add_ble_local_key( (char *)&ble_local_key_cb.er[0],
1894 BTIF_DM_LE_LOCAL_KEY_ER,
1898 case BTA_DM_BLE_AUTH_CMPL_EVT:
1899 BTIF_TRACE_DEBUG("BTA_DM_BLE_AUTH_CMPL_EVT. ");
1900 btif_dm_ble_auth_cmpl_evt(&p_data->auth_cmpl);
1903 case BTA_DM_LE_FEATURES_READ:
1905 tBTM_BLE_VSC_CB cmn_vsc_cb;
1906 bt_local_le_features_t local_le_features;
1909 prop.type = BT_PROPERTY_LOCAL_LE_FEATURES;
1910 prop.val = (void*)buf;
1911 prop.len = sizeof(buf);
1913 /* LE features are not stored in storage. Should be retrived from stack */
1914 BTM_BleGetVendorCapabilities(&cmn_vsc_cb);
1915 local_le_features.local_privacy_enabled = BTM_BleLocalPrivacyEnabled();
1917 prop.len = sizeof (bt_local_le_features_t);
1918 if (cmn_vsc_cb.filter_support == 1)
1919 local_le_features.max_adv_filter_supported = cmn_vsc_cb.max_filter;
1921 local_le_features.max_adv_filter_supported = 0;
1922 local_le_features.max_adv_instance = cmn_vsc_cb.adv_inst_max;
1923 local_le_features.max_irk_list_size = cmn_vsc_cb.max_irk_list_sz;
1924 local_le_features.rpa_offload_supported = cmn_vsc_cb.rpa_offloading;
1925 local_le_features.activity_energy_info_supported = cmn_vsc_cb.energy_support;
1926 local_le_features.scan_result_storage_size = cmn_vsc_cb.tot_scan_results_strg;
1927 local_le_features.version_supported = cmn_vsc_cb.version_supported;
1928 local_le_features.total_trackable_advertisers =
1929 cmn_vsc_cb.total_trackable_advertisers;
1931 local_le_features.extended_scan_support = cmn_vsc_cb.extended_scan_support > 0;
1932 local_le_features.debug_logging_supported = cmn_vsc_cb.debug_logging_supported > 0;
1934 memcpy(prop.val, &local_le_features, prop.len);
1935 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb, BT_STATUS_SUCCESS, 1, &prop);
1939 case BTA_DM_ENER_INFO_READ:
1941 btif_activity_energy_info_cb_t *p_ener_data = (btif_activity_energy_info_cb_t*) p_param;
1942 bt_activity_energy_info energy_info;
1943 energy_info.status = p_ener_data->status;
1944 energy_info.ctrl_state = p_ener_data->ctrl_state;
1945 energy_info.rx_time = p_ener_data->rx_time;
1946 energy_info.tx_time = p_ener_data->tx_time;
1947 energy_info.idle_time = p_ener_data->idle_time;
1948 energy_info.energy_used = p_ener_data->energy_used;
1949 HAL_CBACK(bt_hal_cbacks, energy_info_cb, &energy_info);
1954 case BTA_DM_AUTHORIZE_EVT:
1955 case BTA_DM_SIG_STRENGTH_EVT:
1956 case BTA_DM_SP_RMT_OOB_EVT:
1957 case BTA_DM_SP_KEYPRESS_EVT:
1958 case BTA_DM_ROLE_CHG_EVT:
1961 BTIF_TRACE_WARNING( "btif_dm_cback : unhandled event (%d)", event );
1965 btif_dm_data_free(event, p_data);
1968 /*******************************************************************************
1970 ** Function btif_dm_generic_evt
1972 ** Description Executes non-BTA upstream events in BTIF context
1976 *******************************************************************************/
1977 static void btif_dm_generic_evt(UINT16 event, char* p_param)
1979 BTIF_TRACE_EVENT("%s: event=%d", __FUNCTION__, event);
1982 case BTIF_DM_CB_DISCOVERY_STARTED:
1984 HAL_CBACK(bt_hal_cbacks, discovery_state_changed_cb, BT_DISCOVERY_STARTED);
1988 case BTIF_DM_CB_CREATE_BOND:
1990 pairing_cb.timeout_retries = NUM_TIMEOUT_RETRIES;
1991 btif_dm_create_bond_cb_t *create_bond_cb = (btif_dm_create_bond_cb_t*)p_param;
1992 btif_dm_cb_create_bond(&create_bond_cb->bdaddr, create_bond_cb->transport);
1996 case BTIF_DM_CB_REMOVE_BOND:
1998 btif_dm_cb_remove_bond((bt_bdaddr_t *)p_param);
2002 case BTIF_DM_CB_HID_REMOTE_NAME:
2004 btif_dm_cb_hid_remote_name((tBTM_REMOTE_DEV_NAME *)p_param);
2008 case BTIF_DM_CB_BOND_STATE_BONDING:
2010 bond_state_changed(BT_STATUS_SUCCESS, (bt_bdaddr_t *)p_param, BT_BOND_STATE_BONDING);
2013 case BTIF_DM_CB_LE_TX_TEST:
2014 case BTIF_DM_CB_LE_RX_TEST:
2017 STREAM_TO_UINT8(status, p_param);
2018 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2019 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, 0);
2022 case BTIF_DM_CB_LE_TEST_END:
2026 STREAM_TO_UINT8(status, p_param);
2028 STREAM_TO_UINT16(count, p_param);
2029 HAL_CBACK(bt_hal_cbacks, le_test_mode_cb,
2030 (status == 0) ? BT_STATUS_SUCCESS : BT_STATUS_FAIL, count);
2035 BTIF_TRACE_WARNING("%s : Unknown event 0x%x", __FUNCTION__, event);
2041 /*******************************************************************************
2043 ** Function bte_dm_evt
2045 ** Description Switches context from BTE to BTIF for all DM events
2049 *******************************************************************************/
2051 void bte_dm_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC *p_data)
2053 /* switch context to btif task context (copy full union size for convenience) */
2054 bt_status_t status = btif_transfer_context(btif_dm_upstreams_evt, (uint16_t)event,
2055 (void*)p_data, sizeof(tBTA_DM_SEC), btif_dm_data_copy);
2057 /* catch any failed context transfers */
2058 ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
2061 /*******************************************************************************
2063 ** Function bte_search_devices_evt
2065 ** Description Switches context from BTE to BTIF for DM search events
2069 *******************************************************************************/
2070 static void bte_search_devices_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2072 UINT16 param_len = 0;
2075 param_len += sizeof(tBTA_DM_SEARCH);
2076 /* Allocate buffer to hold the pointers (deep copy). The pointers will point to the end of the tBTA_DM_SEARCH */
2079 case BTA_DM_INQ_RES_EVT:
2081 if (p_data->inq_res.p_eir)
2082 param_len += HCI_EXT_INQ_RESPONSE_LEN;
2086 case BTA_DM_DISC_RES_EVT:
2088 if (p_data->disc_res.raw_data_size && p_data->disc_res.p_raw_data)
2089 param_len += p_data->disc_res.raw_data_size;
2093 BTIF_TRACE_DEBUG("%s event=%s param_len=%d", __FUNCTION__, dump_dm_search_event(event), param_len);
2095 /* if remote name is available in EIR, set teh flag so that stack doesnt trigger RNR */
2096 if (event == BTA_DM_INQ_RES_EVT)
2097 p_data->inq_res.remt_name_not_required = check_eir_remote_name(p_data, NULL, NULL);
2099 btif_transfer_context (btif_dm_search_devices_evt , (UINT16) event, (void *)p_data, param_len,
2100 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_devices_copy_cb : NULL);
2103 /*******************************************************************************
2105 ** Function bte_dm_search_services_evt
2107 ** Description Switches context from BTE to BTIF for DM search services
2112 *******************************************************************************/
2113 static void bte_dm_search_services_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2115 UINT16 param_len = 0;
2117 param_len += sizeof(tBTA_DM_SEARCH);
2120 case BTA_DM_DISC_RES_EVT:
2122 if ((p_data->disc_res.result == BTA_SUCCESS) && (p_data->disc_res.num_uuids > 0)) {
2123 param_len += (p_data->disc_res.num_uuids * MAX_UUID_SIZE);
2127 /* TODO: The only other member that needs a deep copy is the p_raw_data. But not sure
2128 * if raw_data is needed. */
2129 btif_transfer_context(btif_dm_search_services_evt, event, (char*)p_data, param_len,
2130 (param_len > sizeof(tBTA_DM_SEARCH)) ? search_services_copy_cb : NULL);
2133 /*******************************************************************************
2135 ** Function bte_dm_remote_service_record_evt
2137 ** Description Switches context from BTE to BTIF for DM search service
2142 *******************************************************************************/
2143 static void bte_dm_remote_service_record_evt(tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data)
2145 /* TODO: The only member that needs a deep copy is the p_raw_data. But not sure yet if this is needed. */
2146 btif_transfer_context(btif_dm_remote_service_record_evt, event, (char*)p_data, sizeof(tBTA_DM_SEARCH), NULL);
2149 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2150 /*******************************************************************************
2152 ** Function bta_energy_info_cb
2154 ** Description Switches context from BTE to BTIF for DM energy info event
2158 *******************************************************************************/
2159 static void bta_energy_info_cb(tBTA_DM_BLE_TX_TIME_MS tx_time, tBTA_DM_BLE_RX_TIME_MS rx_time,
2160 tBTA_DM_BLE_IDLE_TIME_MS idle_time,
2161 tBTA_DM_BLE_ENERGY_USED energy_used,
2162 tBTA_DM_CONTRL_STATE ctrl_state, tBTA_STATUS status)
2164 BTIF_TRACE_DEBUG("energy_info_cb-Status:%d,state=%d,tx_t=%ld, rx_t=%ld, idle_time=%ld,used=%ld",
2165 status, ctrl_state, tx_time, rx_time, idle_time, energy_used);
2167 btif_activity_energy_info_cb_t btif_cb;
2168 btif_cb.status = status;
2169 btif_cb.ctrl_state = ctrl_state;
2170 btif_cb.tx_time = (uint64_t) tx_time;
2171 btif_cb.rx_time = (uint64_t) rx_time;
2172 btif_cb.idle_time =(uint64_t) idle_time;
2173 btif_cb.energy_used =(uint64_t) energy_used;
2174 btif_transfer_context(btif_dm_upstreams_evt, BTA_DM_ENER_INFO_READ,
2175 (char*) &btif_cb, sizeof(btif_activity_energy_info_cb_t), NULL);
2179 /*******************************************************************************
2181 ** Function bte_scan_filt_param_cfg_evt
2183 ** Description Scan filter param config event
2187 *******************************************************************************/
2188 static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
2189 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
2190 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status)
2192 /* This event occurs on calling BTA_DmBleCfgFilterCondition internally,
2193 ** and that is why there is no HAL callback
2195 if(BTA_SUCCESS != status)
2197 BTIF_TRACE_ERROR("%s, %d", __FUNCTION__, status);
2201 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2205 /*****************************************************************************
2207 ** btif api functions (no context switch)
2209 *****************************************************************************/
2211 /*******************************************************************************
2213 ** Function btif_dm_start_discovery
2215 ** Description Start device discovery/inquiry
2217 ** Returns bt_status_t
2219 *******************************************************************************/
2220 bt_status_t btif_dm_start_discovery(void)
2222 tBTA_DM_INQ inq_params;
2223 tBTA_SERVICE_MASK services = 0;
2224 tBTA_DM_BLE_PF_FILT_PARAMS adv_filt_param;
2226 BTIF_TRACE_EVENT("%s", __FUNCTION__);
2228 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2229 memset(&adv_filt_param, 0, sizeof(tBTA_DM_BLE_PF_FILT_PARAMS));
2230 /* Cleanup anything remaining on index 0 */
2231 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_DELETE, 0, &adv_filt_param, NULL,
2232 bte_scan_filt_param_cfg_evt, 0);
2234 /* Add an allow-all filter on index 0*/
2235 adv_filt_param.dely_mode = IMMEDIATE_DELY_MODE;
2236 adv_filt_param.feat_seln = ALLOW_ALL_FILTER;
2237 adv_filt_param.filt_logic_type = BTA_DM_BLE_PF_FILT_LOGIC_OR;
2238 adv_filt_param.list_logic_type = BTA_DM_BLE_PF_LIST_LOGIC_OR;
2239 adv_filt_param.rssi_low_thres = LOWEST_RSSI_VALUE;
2240 adv_filt_param.rssi_high_thres = LOWEST_RSSI_VALUE;
2241 BTA_DmBleScanFilterSetup(BTA_DM_BLE_SCAN_COND_ADD, 0, &adv_filt_param, NULL,
2242 bte_scan_filt_param_cfg_evt, 0);
2244 /* TODO: Do we need to handle multiple inquiries at the same time? */
2246 /* Set inquiry params and call API */
2247 inq_params.mode = BTA_DM_GENERAL_INQUIRY|BTA_BLE_GENERAL_INQUIRY;
2248 #if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
2249 inq_params.intl_duration[0]= BTIF_DM_INTERLEAVE_DURATION_BR_ONE;
2250 inq_params.intl_duration[1]= BTIF_DM_INTERLEAVE_DURATION_LE_ONE;
2251 inq_params.intl_duration[2]= BTIF_DM_INTERLEAVE_DURATION_BR_TWO;
2252 inq_params.intl_duration[3]= BTIF_DM_INTERLEAVE_DURATION_LE_TWO;
2255 inq_params.mode = BTA_DM_GENERAL_INQUIRY;
2257 inq_params.duration = BTIF_DM_DEFAULT_INQ_MAX_DURATION;
2259 inq_params.max_resps = BTIF_DM_DEFAULT_INQ_MAX_RESULTS;
2260 inq_params.report_dup = TRUE;
2262 inq_params.filter_type = BTA_DM_INQ_CLR;
2263 /* TODO: Filter device by BDA needs to be implemented here */
2265 /* Will be enabled to TRUE once inquiry busy level has been received */
2266 btif_dm_inquiry_in_progress = FALSE;
2267 /* find nearby devices */
2268 BTA_DmSearch(&inq_params, services, bte_search_devices_evt);
2270 return BT_STATUS_SUCCESS;
2273 /*******************************************************************************
2275 ** Function btif_dm_cancel_discovery
2277 ** Description Cancels search
2279 ** Returns bt_status_t
2281 *******************************************************************************/
2282 bt_status_t btif_dm_cancel_discovery(void)
2284 BTIF_TRACE_EVENT("%s", __FUNCTION__);
2285 BTA_DmSearchCancel();
2286 return BT_STATUS_SUCCESS;
2289 /*******************************************************************************
2291 ** Function btif_dm_create_bond
2293 ** Description Initiate bonding with the specified device
2295 ** Returns bt_status_t
2297 *******************************************************************************/
2298 bt_status_t btif_dm_create_bond(const bt_bdaddr_t *bd_addr, int transport)
2300 btif_dm_create_bond_cb_t create_bond_cb;
2301 create_bond_cb.transport = transport;
2302 bdcpy(create_bond_cb.bdaddr.address, bd_addr->address);
2305 BTIF_TRACE_EVENT("%s: bd_addr=%s, transport=%d", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)), transport);
2306 if (pairing_cb.state != BT_BOND_STATE_NONE)
2307 return BT_STATUS_BUSY;
2309 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_CREATE_BOND,
2310 (char *)&create_bond_cb, sizeof(btif_dm_create_bond_cb_t), NULL);
2312 return BT_STATUS_SUCCESS;
2315 /*******************************************************************************
2317 ** Function btif_dm_cancel_bond
2319 ** Description Initiate bonding with the specified device
2321 ** Returns bt_status_t
2323 *******************************************************************************/
2325 bt_status_t btif_dm_cancel_bond(const bt_bdaddr_t *bd_addr)
2329 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)));
2332 ** 1. Restore scan modes
2333 ** 2. special handling for HID devices
2335 if (pairing_cb.state == BT_BOND_STATE_BONDING)
2338 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2340 if (pairing_cb.is_ssp)
2342 if (pairing_cb.is_le_only)
2344 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2348 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2349 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2350 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2355 if (pairing_cb.is_le_only)
2357 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2361 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2363 /* Cancel bonding, in case it is in ACL connection setup state */
2364 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2368 if (pairing_cb.is_ssp)
2370 BTA_DmConfirm( (UINT8 *)bd_addr->address, FALSE);
2374 BTA_DmPinReply( (UINT8 *)bd_addr->address, FALSE, 0, NULL);
2376 /* Cancel bonding, in case it is in ACL connection setup state */
2377 BTA_DmBondCancel ((UINT8 *)bd_addr->address);
2378 btif_storage_remove_bonded_device((bt_bdaddr_t *)bd_addr);
2382 return BT_STATUS_SUCCESS;
2385 /*******************************************************************************
2387 ** Function btif_dm_hh_open_failed
2389 ** Description informs the upper layers if the HH have failed during bonding
2393 *******************************************************************************/
2395 void btif_dm_hh_open_failed(bt_bdaddr_t *bdaddr)
2397 if (pairing_cb.state == BT_BOND_STATE_BONDING &&
2398 bdcmp(bdaddr->address, pairing_cb.bd_addr) == 0)
2400 bond_state_changed(BT_STATUS_FAIL, bdaddr, BT_BOND_STATE_NONE);
2404 /*******************************************************************************
2406 ** Function btif_dm_remove_bond
2408 ** Description Removes bonding with the specified device
2410 ** Returns bt_status_t
2412 *******************************************************************************/
2414 bt_status_t btif_dm_remove_bond(const bt_bdaddr_t *bd_addr)
2418 BTIF_TRACE_EVENT("%s: bd_addr=%s", __FUNCTION__, bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr)));
2419 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_REMOVE_BOND,
2420 (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
2422 return BT_STATUS_SUCCESS;
2425 /*******************************************************************************
2427 ** Function btif_dm_pin_reply
2429 ** Description BT legacy pairing - PIN code reply
2431 ** Returns bt_status_t
2433 *******************************************************************************/
2435 bt_status_t btif_dm_pin_reply( const bt_bdaddr_t *bd_addr, uint8_t accept,
2436 uint8_t pin_len, bt_pin_code_t *pin_code)
2438 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
2439 if (pin_code == NULL)
2440 return BT_STATUS_FAIL;
2441 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2443 if (pairing_cb.is_le_only)
2447 int multi[] = {100000, 10000, 1000, 100, 10,1};
2448 BD_ADDR remote_bd_addr;
2449 bdcpy(remote_bd_addr, bd_addr->address);
2450 for (i = 0; i < 6; i++)
2452 passkey += (multi[i] * (pin_code->pin[i] - '0'));
2454 BTIF_TRACE_DEBUG("btif_dm_pin_reply: passkey: %d", passkey);
2455 BTA_DmBlePasskeyReply(remote_bd_addr, accept, passkey);
2460 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2462 pairing_cb.pin_code_len = pin_len;
2465 BTA_DmPinReply( (UINT8 *)bd_addr->address, accept, pin_len, pin_code->pin);
2468 pairing_cb.pin_code_len = pin_len;
2470 return BT_STATUS_SUCCESS;
2473 /*******************************************************************************
2475 ** Function btif_dm_ssp_reply
2477 ** Description BT SSP Reply - Just Works, Numeric Comparison & Passkey Entry
2479 ** Returns bt_status_t
2481 *******************************************************************************/
2482 bt_status_t btif_dm_ssp_reply(const bt_bdaddr_t *bd_addr,
2483 bt_ssp_variant_t variant, uint8_t accept,
2488 if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY)
2490 /* This is not implemented in the stack.
2491 * For devices with display, this is not needed
2493 BTIF_TRACE_WARNING("%s: Not implemented", __FUNCTION__);
2494 return BT_STATUS_FAIL;
2496 /* BT_SSP_VARIANT_CONSENT & BT_SSP_VARIANT_PASSKEY_CONFIRMATION supported */
2497 BTIF_TRACE_EVENT("%s: accept=%d", __FUNCTION__, accept);
2498 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2499 if (pairing_cb.is_le_only)
2501 if(pairing_cb.is_le_nc)
2503 BTA_DmBleConfirmReply((UINT8 *)bd_addr->address,accept);
2506 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_GRANTED);
2508 BTA_DmBleSecurityGrant((UINT8 *)bd_addr->address,BTA_DM_SEC_PAIR_NOT_SPT);
2511 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2514 BTA_DmConfirm( (UINT8 *)bd_addr->address, accept);
2516 return BT_STATUS_SUCCESS;
2519 /*******************************************************************************
2521 ** Function btif_dm_get_adapter_property
2523 ** Description Queries the BTA for the adapter property
2525 ** Returns bt_status_t
2527 *******************************************************************************/
2528 bt_status_t btif_dm_get_adapter_property(bt_property_t *prop)
2530 BTIF_TRACE_EVENT("%s: type=0x%x", __FUNCTION__, prop->type);
2533 case BT_PROPERTY_BDNAME:
2535 bt_bdname_t *bd_name = (bt_bdname_t*)prop->val;
2536 strncpy((char *)bd_name->name, (char *)btif_get_default_local_name(),
2537 sizeof(bd_name->name) - 1);
2538 bd_name->name[sizeof(bd_name->name) - 1] = 0;
2539 prop->len = strlen((char *)bd_name->name);
2543 case BT_PROPERTY_ADAPTER_SCAN_MODE:
2545 /* if the storage does not have it. Most likely app never set it. Default is NONE */
2546 bt_scan_mode_t *mode = (bt_scan_mode_t*)prop->val;
2547 *mode = BT_SCAN_MODE_NONE;
2548 prop->len = sizeof(bt_scan_mode_t);
2552 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT:
2554 uint32_t *tmt = (uint32_t*)prop->val;
2555 *tmt = 120; /* default to 120s, if not found in NV */
2556 prop->len = sizeof(uint32_t);
2562 return BT_STATUS_FAIL;
2564 return BT_STATUS_SUCCESS;
2567 /*******************************************************************************
2569 ** Function btif_dm_get_remote_services
2571 ** Description Start SDP to get remote services
2573 ** Returns bt_status_t
2575 *******************************************************************************/
2576 bt_status_t btif_dm_get_remote_services(bt_bdaddr_t *remote_addr)
2580 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr)));
2582 BTA_DmDiscover(remote_addr->address, BTA_ALL_SERVICE_MASK,
2583 bte_dm_search_services_evt, TRUE);
2585 return BT_STATUS_SUCCESS;
2588 /*******************************************************************************
2590 ** Function btif_dm_get_remote_services_transport
2592 ** Description Start SDP to get remote services by transport
2594 ** Returns bt_status_t
2596 *******************************************************************************/
2597 bt_status_t btif_dm_get_remote_services_by_transport(bt_bdaddr_t *remote_addr, const int transport)
2599 BTIF_TRACE_EVENT("%s", __func__);
2601 /* Set the mask extension */
2602 tBTA_SERVICE_MASK_EXT mask_ext;
2603 mask_ext.num_uuid = 0;
2604 mask_ext.p_uuid = NULL;
2605 mask_ext.srvc_mask = BTA_ALL_SERVICE_MASK;
2607 BTA_DmDiscoverByTransport(remote_addr->address, &mask_ext,
2608 bte_dm_search_services_evt, TRUE, transport);
2610 return BT_STATUS_SUCCESS;
2613 /*******************************************************************************
2615 ** Function btif_dm_get_remote_service_record
2617 ** Description Start SDP to get remote service record
2620 ** Returns bt_status_t
2621 *******************************************************************************/
2622 bt_status_t btif_dm_get_remote_service_record(bt_bdaddr_t *remote_addr,
2628 BTIF_TRACE_EVENT("%s: remote_addr=%s", __FUNCTION__, bdaddr_to_string(remote_addr, bdstr, sizeof(bdstr)));
2630 sdp_uuid.len = MAX_UUID_SIZE;
2631 memcpy(sdp_uuid.uu.uuid128, uuid->uu, MAX_UUID_SIZE);
2633 BTA_DmDiscoverUUID(remote_addr->address, &sdp_uuid,
2634 bte_dm_remote_service_record_evt, TRUE);
2636 return BT_STATUS_SUCCESS;
2639 void btif_dm_execute_service_request(UINT16 event, char *p_param)
2641 BOOLEAN b_enable = FALSE;
2643 if (event == BTIF_DM_ENABLE_SERVICE)
2647 status = btif_in_execute_service_request(*((tBTA_SERVICE_ID*)p_param), b_enable);
2648 if (status == BT_STATUS_SUCCESS)
2650 bt_property_t property;
2651 bt_uuid_t local_uuids[BT_MAX_NUM_UUIDS];
2653 /* Now send the UUID_PROPERTY_CHANGED event to the upper layer */
2654 BTIF_STORAGE_FILL_PROPERTY(&property, BT_PROPERTY_UUIDS,
2655 sizeof(local_uuids), local_uuids);
2656 btif_storage_get_adapter_property(&property);
2657 HAL_CBACK(bt_hal_cbacks, adapter_properties_cb,
2658 BT_STATUS_SUCCESS, 1, &property);
2663 void btif_dm_proc_io_req(BD_ADDR bd_addr, tBTA_IO_CAP *p_io_cap, tBTA_OOB_DATA *p_oob_data,
2664 tBTA_AUTH_REQ *p_auth_req, BOOLEAN is_orig)
2666 UINT8 yes_no_bit = BTA_AUTH_SP_YES & *p_auth_req;
2667 /* if local initiated:
2669 ** if remote initiated:
2670 ** 1. Copy over the auth_req from peer's io_rsp
2671 ** 2. Set the MITM if peer has it set or if peer has DisplayYesNo (iPhone)
2672 ** as a fallback set MITM+GB if peer had MITM set
2676 UNUSED (p_oob_data);
2678 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
2679 if(pairing_cb.is_local_initiated)
2681 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2682 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2686 /* peer initiated paring. They probably know what they want.
2687 ** Copy the mitm from peer device.
2689 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
2690 __FUNCTION__, pairing_cb.auth_req);
2691 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2693 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2694 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2695 *p_auth_req |= BTA_AUTH_SP_YES;
2697 else if (yes_no_bit)
2699 /* set the general bonding bit for stored device */
2700 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2702 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
2705 void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2706 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2711 if(auth_req & BTA_AUTH_BONDS)
2713 BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
2714 pairing_cb.auth_req = auth_req;
2715 pairing_cb.io_cap = io_cap;
2719 void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data)
2721 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2722 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2724 *p_oob_data = FALSE;
2730 BTIF_TRACE_DEBUG("btif_dm_set_oob_for_io_req *p_oob_data=%d", *p_oob_data);
2733 #ifdef BTIF_DM_OOB_TEST
2734 void btif_dm_load_local_oob(void)
2737 * TODO(armansito): On OSs other than Android, the sys/properties.h system
2738 * does not exist. Remove this conditional include once we have a generic way
2739 * to obtain system properties.
2741 #if !defined(OS_GENERIC)
2742 char prop_oob[PROPERTY_VALUE_MAX];
2743 property_get("service.brcm.bt.oob", prop_oob, "3");
2744 BTIF_TRACE_DEBUG("btif_dm_load_local_oob prop_oob = %s",prop_oob);
2745 if (prop_oob[0] != '3')
2747 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2748 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2750 BTIF_TRACE_DEBUG("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
2754 #endif /* !defined(OS_GENERIC) */
2757 void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2760 * TODO(armansito): On OSs other than Android, the sys/properties.h system
2761 * does not exist. Remove this conditional include once we have a generic way
2762 * to obtain system properties.
2764 #if !defined(OS_GENERIC)
2766 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2767 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2769 char prop_oob[PROPERTY_VALUE_MAX];
2770 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: valid=%d", valid);
2771 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2772 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2775 BTIF_TRACE_DEBUG("save local OOB data in memory");
2776 memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2777 memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2778 property_get("service.brcm.bt.oob", prop_oob, "3");
2779 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
2780 if (prop_oob[0] == '1')
2782 else if (prop_oob[0] == '2')
2786 fp = fopen(path, "wb+");
2789 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: failed to save local OOB data to %s", path);
2793 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: save local OOB data into file %s",path);
2794 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2795 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2800 #endif /* !defined(OS_GENERIC) */
2803 BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2806 * TODO(armansito): On OSs other than Android, the sys/properties.h system
2807 * does not exist. Remove this conditional include once we have a generic way
2808 * to obtain system properties.
2810 #if !defined(OS_GENERIC)
2813 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2814 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2816 char prop_oob[PROPERTY_VALUE_MAX];
2817 BOOLEAN result = FALSE;
2818 bt_bdaddr_t bt_bd_addr;
2819 bdcpy(oob_cb.oob_bdaddr, bd_addr);
2820 property_get("service.brcm.bt.oob", prop_oob, "3");
2821 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
2822 if (prop_oob[0] == '1')
2824 else if (prop_oob[0] == '2')
2828 fp = fopen(path, "rb");
2831 BTIF_TRACE_DEBUG("btapp_dm_rmt_oob_reply: failed to read OOB keys from %s",path);
2836 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob: read OOB data from %s",path);
2837 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2838 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2841 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: TRUE");
2842 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2843 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2844 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
2845 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
2846 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2847 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
2848 p_c[8], p_c[9], p_c[10], p_c[11], p_c[12], p_c[13], p_c[14], p_c[15]);
2849 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: c = %s",t);
2850 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2851 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
2852 p_r[8], p_r[9], p_r[10], p_r[11], p_r[12], p_r[13], p_r[14], p_r[15]);
2853 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: r = %s",t);
2854 bdcpy(bt_bd_addr.address, bd_addr);
2855 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2856 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2859 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob result=%d",result);
2861 #else /* defined(OS_GENERIC) */
2863 #endif /* !defined(OS_GENERIC) */
2865 #endif /* BTIF_DM_OOB_TEST */
2866 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2868 static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2870 bt_bdaddr_t bd_addr;
2871 bt_bdname_t bd_name;
2875 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2877 /* Remote name update */
2878 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
2880 dev_type = BT_DEVICE_TYPE_BLE;
2882 btif_dm_update_ble_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2883 (tBT_DEVICE_TYPE) dev_type);
2884 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2885 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2887 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2888 pairing_cb.is_ssp = FALSE;
2889 cod = COD_UNCLASSIFIED;
2891 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2892 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2893 p_ssp_key_notif->passkey);
2896 /*******************************************************************************
2898 ** Function btif_dm_ble_auth_cmpl_evt
2900 ** Description Executes authentication complete event in btif context
2904 *******************************************************************************/
2905 static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2907 /* Save link key, if not temporary */
2908 bt_bdaddr_t bd_addr;
2909 bt_status_t status = BT_STATUS_FAIL;
2910 bt_bond_state_t state = BT_BOND_STATE_NONE;
2912 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2913 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2917 if (p_auth_cmpl->success)
2919 status = BT_STATUS_SUCCESS;
2920 state = BT_BOND_STATE_BONDED;
2923 bdcpy(bdaddr.address, p_auth_cmpl->bd_addr);
2924 if (btif_storage_get_remote_addr_type(&bdaddr, &addr_type) != BT_STATUS_SUCCESS)
2925 btif_storage_set_remote_addr_type(&bdaddr, p_auth_cmpl->addr_type);
2927 /* Test for temporary bonding */
2928 if (btm_get_bond_type_dev(p_auth_cmpl->bd_addr) == BOND_TYPE_TEMPORARY) {
2929 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
2931 btif_storage_remove_bonded_device(&bdaddr);
2932 state = BT_BOND_STATE_NONE;
2934 btif_dm_save_ble_bonding_keys();
2935 BTA_GATTC_Refresh(bd_addr.address);
2936 btif_dm_get_remote_services_by_transport(&bd_addr, BTA_GATT_TRANSPORT_LE);
2941 /*Map the HCI fail reason to bt status */
2942 switch (p_auth_cmpl->fail_reason)
2944 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
2945 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
2946 btif_dm_remove_ble_bonding_keys();
2947 status = BT_STATUS_AUTH_FAILURE;
2949 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
2950 status = BT_STATUS_AUTH_REJECTED;
2953 btif_dm_remove_ble_bonding_keys();
2954 status = BT_STATUS_FAIL;
2958 bond_state_changed(status, &bd_addr, state);
2961 void btif_dm_load_ble_local_keys(void)
2963 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2965 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2966 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2968 ble_local_key_cb.is_er_rcvd = TRUE;
2969 BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
2972 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
2973 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
2974 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
2975 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
2976 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
2977 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
2979 ble_local_key_cb.is_id_keys_rcvd = TRUE;
2980 BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
2984 void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
2985 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
2987 if (ble_local_key_cb.is_er_rcvd )
2989 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2990 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2993 if (ble_local_key_cb.is_id_keys_rcvd)
2995 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
2996 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
2997 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
2998 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
3000 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
3003 void btif_dm_save_ble_bonding_keys(void)
3006 bt_bdaddr_t bd_addr;
3008 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
3010 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3012 if (pairing_cb.ble.is_penc_key_rcvd)
3014 btif_storage_add_ble_bonding_key(&bd_addr,
3015 (char *) &pairing_cb.ble.penc_key,
3016 BTIF_DM_LE_KEY_PENC,
3017 sizeof(tBTM_LE_PENC_KEYS));
3020 if (pairing_cb.ble.is_pid_key_rcvd)
3022 btif_storage_add_ble_bonding_key(&bd_addr,
3023 (char *) &pairing_cb.ble.pid_key,
3025 sizeof(tBTM_LE_PID_KEYS));
3028 if (pairing_cb.ble.is_pcsrk_key_rcvd)
3030 btif_storage_add_ble_bonding_key(&bd_addr,
3031 (char *) &pairing_cb.ble.pcsrk_key,
3032 BTIF_DM_LE_KEY_PCSRK,
3033 sizeof(tBTM_LE_PCSRK_KEYS));
3036 if (pairing_cb.ble.is_lenc_key_rcvd)
3038 btif_storage_add_ble_bonding_key(&bd_addr,
3039 (char *) &pairing_cb.ble.lenc_key,
3040 BTIF_DM_LE_KEY_LENC,
3041 sizeof(tBTM_LE_LENC_KEYS));
3044 if (pairing_cb.ble.is_lcsrk_key_rcvd)
3046 btif_storage_add_ble_bonding_key(&bd_addr,
3047 (char *) &pairing_cb.ble.lcsrk_key,
3048 BTIF_DM_LE_KEY_LCSRK,
3049 sizeof(tBTM_LE_LCSRK_KEYS));
3052 if (pairing_cb.ble.is_lidk_key_rcvd)
3054 btif_storage_add_ble_bonding_key(&bd_addr,
3062 void btif_dm_remove_ble_bonding_keys(void)
3064 bt_bdaddr_t bd_addr;
3066 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
3068 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3069 btif_storage_remove_ble_bonding_keys(&bd_addr);
3072 /*******************************************************************************
3074 ** Function btif_dm_ble_sec_req_evt
3076 ** Description Eprocess security request event in btif context
3080 *******************************************************************************/
3081 void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
3083 bt_bdaddr_t bd_addr;
3084 bt_bdname_t bd_name;
3088 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
3090 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3092 BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
3096 /* Remote name update */
3097 if (!btif_get_device_type(p_ble_req->bd_addr, &dev_type))
3099 dev_type = BT_DEVICE_TYPE_BLE;
3101 btif_dm_update_ble_remote_properties(p_ble_req->bd_addr, p_ble_req->bd_name,
3102 (tBT_DEVICE_TYPE) dev_type);
3104 bdcpy(bd_addr.address, p_ble_req->bd_addr);
3105 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
3107 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3109 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
3110 pairing_cb.is_le_only = TRUE;
3111 pairing_cb.is_le_nc = FALSE;
3112 pairing_cb.is_ssp = TRUE;
3113 btm_set_bond_type_dev(p_ble_req->bd_addr, pairing_cb.bond_type);
3115 cod = COD_UNCLASSIFIED;
3117 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
3118 BT_SSP_VARIANT_CONSENT, 0);
3121 /*******************************************************************************
3123 ** Function btif_dm_ble_passkey_req_evt
3125 ** Description Executes pin request event in btif context
3129 *******************************************************************************/
3130 static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
3132 bt_bdaddr_t bd_addr;
3133 bt_bdname_t bd_name;
3137 /* Remote name update */
3138 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
3140 dev_type = BT_DEVICE_TYPE_BLE;
3142 btif_dm_update_ble_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,
3143 (tBT_DEVICE_TYPE) dev_type);
3145 bdcpy(bd_addr.address, p_pin_req->bd_addr);
3146 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
3148 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3149 pairing_cb.is_le_only = TRUE;
3151 cod = COD_UNCLASSIFIED;
3153 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
3154 &bd_addr, &bd_name, cod, FALSE);
3156 static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF *p_notif_req)
3158 /* TODO implement key notification for numeric comparison */
3159 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
3161 /* Remote name update */
3162 btif_update_remote_properties(p_notif_req->bd_addr , p_notif_req->bd_name,
3163 NULL, BT_DEVICE_TYPE_BLE);
3165 bt_bdaddr_t bd_addr;
3166 bdcpy(bd_addr.address, p_notif_req->bd_addr);
3168 bt_bdname_t bd_name;
3169 memcpy(bd_name.name, p_notif_req->bd_name, BD_NAME_LEN);
3171 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3172 pairing_cb.is_ssp = FALSE;
3173 pairing_cb.is_le_only = TRUE;
3174 pairing_cb.is_le_nc = TRUE;
3176 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
3177 COD_UNCLASSIFIED, BT_SSP_VARIANT_PASSKEY_CONFIRMATION,
3178 p_notif_req->passkey);
3181 void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
3182 tBT_DEVICE_TYPE dev_type)
3184 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
3187 static void btif_dm_ble_tx_test_cback(void *p)
3189 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
3190 (char *)p, 1, NULL);
3193 static void btif_dm_ble_rx_test_cback(void *p)
3195 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
3196 (char *)p, 1, NULL);
3199 static void btif_dm_ble_test_end_cback(void *p)
3201 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
3202 (char *)p, 3, NULL);
3204 /*******************************************************************************
3206 ** Function btif_le_test_mode
3208 ** Description Sends a HCI BLE Test command to the Controller
3210 ** Returns BT_STATUS_SUCCESS on success
3212 *******************************************************************************/
3213 bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
3216 case HCI_BLE_TRANSMITTER_TEST:
3217 if (len != 3) return BT_STATUS_PARM_INVALID;
3218 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
3220 case HCI_BLE_RECEIVER_TEST:
3221 if (len != 1) return BT_STATUS_PARM_INVALID;
3222 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
3224 case HCI_BLE_TEST_END:
3225 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
3228 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
3229 return BT_STATUS_UNSUPPORTED;
3231 return BT_STATUS_SUCCESS;
3235 void btif_dm_on_disable()
3237 /* cancel any pending pairing requests */
3238 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3240 bt_bdaddr_t bd_addr;
3242 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
3243 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3244 btif_dm_cancel_bond(&bd_addr);
3248 /*******************************************************************************
3250 ** Function btif_dm_read_energy_info
3252 ** Description Reads the energy info from controller
3256 *******************************************************************************/
3257 void btif_dm_read_energy_info()
3259 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
3260 BTA_DmBleGetEnergyInfo(bta_energy_info_cb);
3264 static char* btif_get_default_local_name() {
3266 * TODO(armansito): On OSs other than Android, the sys/properties.h system
3267 * does not exist. Remove this conditional include once we have a generic way
3268 * to obtain system properties.
3270 #if !defined(OS_GENERIC)
3271 if (btif_default_local_name[0] == '\0')
3273 int max_len = sizeof(btif_default_local_name) - 1;
3274 if (BTM_DEF_LOCAL_NAME[0] != '\0')
3276 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
3280 char prop_model[PROPERTY_VALUE_MAX];
3281 property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
3282 strncpy(btif_default_local_name, prop_model, max_len);
3284 btif_default_local_name[max_len] = '\0';
3286 #endif /* !defined(OS_GENERIC) */
3287 return btif_default_local_name;