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"
35 #include <sys/types.h>
38 #include <hardware/bluetooth.h>
40 #include <cutils/properties.h>
43 #include "btcore/include/bdaddr.h"
46 #include "btif_util.h"
48 #include "btif_storage.h"
50 #include "btif_config.h"
52 #include "bta_gatt_api.h"
53 #include "device/include/interop.h"
54 #include "include/stack_config.h"
55 #include "osi/include/log.h"
56 #include "osi/include/allocator.h"
57 #include "stack/btm/btm_int.h"
59 /******************************************************************************
61 ******************************************************************************/
63 #define COD_UNCLASSIFIED ((0x1F) << 8)
64 #define COD_HID_KEYBOARD 0x0540
65 #define COD_HID_POINTING 0x0580
66 #define COD_HID_COMBO 0x05C0
67 #define COD_HID_MAJOR 0x0500
68 #define COD_AV_HEADSETS 0x0404
69 #define COD_AV_HANDSFREE 0x0408
70 #define COD_AV_HEADPHONES 0x0418
71 #define COD_AV_PORTABLE_AUDIO 0x041C
72 #define COD_AV_HIFI_AUDIO 0x0428
75 #define BTIF_DM_DEFAULT_INQ_MAX_RESULTS 0
76 #define BTIF_DM_DEFAULT_INQ_MAX_DURATION 10
77 #define BTIF_DM_MAX_SDP_ATTEMPTS_AFTER_PAIRING 2
79 #define NUM_TIMEOUT_RETRIES 5
81 #define PROPERTY_PRODUCT_MODEL "ro.product.model"
82 #define DEFAULT_LOCAL_NAME_MAX 31
83 #if (DEFAULT_LOCAL_NAME_MAX > BTM_MAX_LOC_BD_NAME_LEN)
84 #error "default btif local name size exceeds stack supported length"
87 #if (defined(BTA_HOST_INTERLEAVE_SEARCH) && BTA_HOST_INTERLEAVE_SEARCH == TRUE)
88 #define BTIF_DM_INTERLEAVE_DURATION_BR_ONE 2
89 #define BTIF_DM_INTERLEAVE_DURATION_LE_ONE 2
90 #define BTIF_DM_INTERLEAVE_DURATION_BR_TWO 3
91 #define BTIF_DM_INTERLEAVE_DURATION_LE_TWO 4
94 #define MAX_SDP_BL_ENTRIES 3
96 #define ENCRYPTED_BREDR 2
97 #define ENCRYPTED_LE 4
101 bt_bond_state_t state;
102 bt_bdaddr_t static_bdaddr;
104 tBTM_BOND_TYPE bond_type;
109 UINT8 autopair_attempts;
110 UINT8 timeout_retries;
111 UINT8 is_local_initiated;
113 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
115 BOOLEAN is_le_nc; /* LE Numeric comparison */
116 btif_dm_ble_cb_t ble;
118 } btif_dm_pairing_cb_t;
123 UINT8 ir[BT_OCTET16_LEN];
124 UINT8 irk[BT_OCTET16_LEN];
125 UINT8 dhk[BT_OCTET16_LEN];
126 }btif_dm_local_key_id_t;
131 UINT8 er[BT_OCTET16_LEN];
132 BOOLEAN is_id_keys_rcvd;
133 btif_dm_local_key_id_t id_keys; /* ID kyes */
135 }btif_dm_local_key_cb_t;
141 } btif_dm_remote_name_t;
147 BD_ADDR oob_bdaddr; /* peer bdaddr*/
153 UINT8 transport; /* 0=Unknown, 1=BR/EDR, 2=LE */
154 } btif_dm_create_bond_cb_t;
163 uint64_t energy_used;
164 } btif_activity_energy_info_cb_t;
168 unsigned int manufact_id;
171 #define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id))
173 #define MAX_SDP_BL_ENTRIES 3
174 #define UUID_HUMAN_INTERFACE_DEVICE "00001124-0000-1000-8000-00805f9b34fb"
176 static skip_sdp_entry_t sdp_blacklist[] = {{76}}; //Apple Mouse and Keyboard
178 /* This flag will be true if HCI_Inquiry is in progress */
179 static BOOLEAN btif_dm_inquiry_in_progress = FALSE;
183 /************************************************************************************
185 ************************************************************************************/
186 static char btif_default_local_name[DEFAULT_LOCAL_NAME_MAX+1] = {'\0'};
188 /******************************************************************************
190 ******************************************************************************/
191 static btif_dm_pairing_cb_t pairing_cb;
192 static btif_dm_oob_cb_t oob_cb;
193 static void btif_dm_generic_evt(UINT16 event, char* p_param);
194 static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport);
195 static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name);
196 static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
197 DEV_CLASS dev_class, tBT_DEVICE_TYPE dev_type);
198 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
199 static btif_dm_local_key_cb_t ble_local_key_cb;
200 static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif);
201 static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl);
202 static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req);
203 static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF *p_notif_req) ;
206 static void bte_scan_filt_param_cfg_evt(UINT8 action_type,
207 tBTA_DM_BLE_PF_AVBL_SPACE avbl_space,
208 tBTA_DM_BLE_REF_VALUE ref_value, tBTA_STATUS status);
210 static char* btif_get_default_local_name();
211 /******************************************************************************
213 ******************************************************************************/
214 extern UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID];
215 extern bt_status_t btif_hf_execute_service(BOOLEAN b_enable);
216 extern bt_status_t btif_av_execute_service(BOOLEAN b_enable);
217 extern bt_status_t btif_av_sink_execute_service(BOOLEAN b_enable);
218 extern bt_status_t btif_hh_execute_service(BOOLEAN b_enable);
219 extern bt_status_t btif_hf_client_execute_service(BOOLEAN b_enable);
220 extern bt_status_t btif_sdp_execute_service(BOOLEAN b_enable);
221 extern int btif_hh_connect(bt_bdaddr_t *bd_addr);
222 extern void bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16);
225 /******************************************************************************
227 ******************************************************************************/
229 static void btif_dm_data_copy(uint16_t event, char *dst, char *src)
231 tBTA_DM_SEC *dst_dm_sec = (tBTA_DM_SEC*)dst;
232 tBTA_DM_SEC *src_dm_sec = (tBTA_DM_SEC*)src;
238 memcpy(dst_dm_sec, src_dm_sec, sizeof(tBTA_DM_SEC));
240 if (event == BTA_DM_BLE_KEY_EVT)
242 dst_dm_sec->ble_key.p_key_value = osi_malloc(sizeof(tBTM_LE_KEY_VALUE));
243 assert(src_dm_sec->ble_key.p_key_value);
244 assert(dst_dm_sec->ble_key.p_key_value);
245 memcpy(dst_dm_sec->ble_key.p_key_value, src_dm_sec->ble_key.p_key_value, sizeof(tBTM_LE_KEY_VALUE));
249 static void btif_dm_data_free(uint16_t event, tBTA_DM_SEC *dm_sec)
251 if (event == BTA_DM_BLE_KEY_EVT)
252 osi_free(dm_sec->ble_key.p_key_value);
255 bt_status_t btif_in_execute_service_request(tBTA_SERVICE_ID service_id,
258 BTIF_TRACE_DEBUG("%s service_id: %d", __FUNCTION__, service_id);
259 /* Check the service_ID and invoke the profile's BT state changed API */
262 case BTA_HFP_SERVICE_ID:
263 case BTA_HSP_SERVICE_ID:
265 btif_hf_execute_service(b_enable);
267 case BTA_A2DP_SOURCE_SERVICE_ID:
269 btif_av_execute_service(b_enable);
271 case BTA_A2DP_SINK_SERVICE_ID:
273 btif_av_sink_execute_service(b_enable);
275 case BTA_HID_SERVICE_ID:
277 btif_hh_execute_service(b_enable);
279 case BTA_HFP_HS_SERVICE_ID:
281 btif_hf_client_execute_service(b_enable);
283 case BTA_SDP_SERVICE_ID:
285 btif_sdp_execute_service(b_enable);
288 BTIF_TRACE_ERROR("%s: Unknown service being enabled", __FUNCTION__);
289 return BT_STATUS_FAIL;
291 return BT_STATUS_SUCCESS;
294 /*******************************************************************************
296 ** Function check_eir_remote_name
298 ** Description Check if remote name is in the EIR data
300 ** Returns TRUE if remote name found
301 ** Populate p_remote_name, if provided and remote name found
303 *******************************************************************************/
304 static BOOLEAN check_eir_remote_name(tBTA_DM_SEARCH *p_search_data,
305 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
307 UINT8 *p_eir_remote_name = NULL;
308 UINT8 remote_name_len = 0;
310 /* Check EIR for remote name and services */
311 if (p_search_data->inq_res.p_eir)
313 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
314 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len);
315 if (!p_eir_remote_name)
317 p_eir_remote_name = BTM_CheckEirData(p_search_data->inq_res.p_eir,
318 BTM_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len);
321 if (p_eir_remote_name)
323 if (remote_name_len > BD_NAME_LEN)
324 remote_name_len = BD_NAME_LEN;
326 if (p_remote_name && p_remote_name_len)
328 memcpy(p_remote_name, p_eir_remote_name, remote_name_len);
329 *(p_remote_name + remote_name_len) = 0;
330 *p_remote_name_len = remote_name_len;
341 /*******************************************************************************
343 ** Function check_cached_remote_name
345 ** Description Check if remote name is in the NVRAM cache
347 ** Returns TRUE if remote name found
348 ** Populate p_remote_name, if provided and remote name found
350 *******************************************************************************/
351 static BOOLEAN check_cached_remote_name(tBTA_DM_SEARCH *p_search_data,
352 UINT8 *p_remote_name, UINT8 *p_remote_name_len)
355 bt_bdaddr_t remote_bdaddr;
356 bt_property_t prop_name;
358 /* check if we already have it in our btif_storage cache */
359 bdcpy(remote_bdaddr.address, p_search_data->inq_res.bd_addr);
360 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_BDNAME,
361 sizeof(bt_bdname_t), &bdname);
362 if (btif_storage_get_remote_device_property(
363 &remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
365 if (p_remote_name && p_remote_name_len)
367 strcpy((char *)p_remote_name, (char *)bdname.name);
368 *p_remote_name_len = strlen((char *)p_remote_name);
376 BOOLEAN check_cod(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
379 bt_property_t prop_name;
381 /* check if we already have it in our btif_storage cache */
382 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
383 sizeof(uint32_t), &remote_cod);
384 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr, &prop_name) == BT_STATUS_SUCCESS)
386 LOG_INFO("%s remote_cod = 0x%08x cod = 0x%08x", __func__, remote_cod, cod);
387 if ((remote_cod & 0x7ff) == cod)
394 BOOLEAN check_cod_hid(const bt_bdaddr_t *remote_bdaddr, uint32_t cod)
397 bt_property_t prop_name;
399 /* check if we already have it in our btif_storage cache */
400 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE,
401 sizeof(uint32_t), &remote_cod);
402 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
403 &prop_name) == BT_STATUS_SUCCESS)
405 BTIF_TRACE_DEBUG("%s: remote_cod = 0x%06x", __FUNCTION__, remote_cod);
406 if ((remote_cod & 0x700) == cod)
412 BOOLEAN check_hid_le(const bt_bdaddr_t *remote_bdaddr)
414 uint32_t remote_dev_type;
415 bt_property_t prop_name;
417 /* check if we already have it in our btif_storage cache */
418 BTIF_STORAGE_FILL_PROPERTY(&prop_name,BT_PROPERTY_TYPE_OF_DEVICE,
419 sizeof(uint32_t), &remote_dev_type);
420 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
421 &prop_name) == BT_STATUS_SUCCESS)
423 if (remote_dev_type == BT_DEVICE_DEVTYPE_BLE)
426 bdaddr_to_string(remote_bdaddr, bdstr, sizeof(bdstr));
427 if(btif_config_exist(bdstr, "HidAppId"))
434 /*****************************************************************************
436 ** Function check_sdp_bl
438 ** Description Checks if a given device is blacklisted to skip sdp
440 ** Parameters skip_sdp_entry
442 ** Returns TRUE if the device is present in blacklist, else FALSE
444 *******************************************************************************/
445 BOOLEAN check_sdp_bl(const bt_bdaddr_t *remote_bdaddr)
447 UINT16 manufacturer = 0;
449 UINT16 lmp_subver = 0;
450 bt_property_t prop_name;
451 bt_remote_version_t info;
454 if (remote_bdaddr == NULL)
457 /* fetch additional info about remote device used in iop query */
458 BTM_ReadRemoteVersion(*(BD_ADDR*)remote_bdaddr, &lmp_ver,
459 &manufacturer, &lmp_subver);
463 /* if not available yet, try fetching from config database */
464 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_REMOTE_VERSION_INFO,
465 sizeof(bt_remote_version_t), &info);
467 if (btif_storage_get_remote_device_property((bt_bdaddr_t *)remote_bdaddr,
468 &prop_name) != BT_STATUS_SUCCESS)
473 manufacturer = info.manufacturer;
475 for (int i = 0; i < MAX_SDP_BL_ENTRIES; i++)
477 if (manufacturer == sdp_blacklist[i].manufact_id)
484 static void bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr, bt_bond_state_t state)
486 // Send bonding state only once - based on outgoing/incoming we may receive duplicates
487 if ((pairing_cb.state == state) && (state == BT_BOND_STATE_BONDING))
489 // Cross key pairing so send callback for static address
490 if (!bdaddr_is_empty(&pairing_cb.static_bdaddr))
492 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
497 if (pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
498 state = BT_BOND_STATE_NONE;
500 BTIF_TRACE_DEBUG("%s: state=%d, prev_state=%d, sdp_attempts = %d", __func__,
501 state, pairing_cb.state, pairing_cb.sdp_attempts);
503 HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, status, bd_addr, state);
505 if (state == BT_BOND_STATE_BONDING)
507 pairing_cb.state = state;
508 bdcpy(pairing_cb.bd_addr, bd_addr->address);
510 if (!pairing_cb.sdp_attempts)
511 memset(&pairing_cb, 0, sizeof(pairing_cb));
513 BTIF_TRACE_DEBUG("%s: BR-EDR service discovery active", __func__);
517 /* store remote version in bt config to always have access
519 static void btif_update_remote_version_property(bt_bdaddr_t *p_bd)
521 bt_property_t property;
523 UINT16 lmp_subver = 0;
525 tBTM_STATUS btm_status;
526 bt_remote_version_t info;
530 btm_status = BTM_ReadRemoteVersion(*(BD_ADDR*)p_bd, &lmp_ver,
531 &mfct_set, &lmp_subver);
533 LOG_DEBUG("remote version info [%s]: %x, %x, %x", bdaddr_to_string(p_bd, bdstr, sizeof(bdstr)),
534 lmp_ver, mfct_set, lmp_subver);
536 if (btm_status == BTM_SUCCESS)
538 // Always update cache to ensure we have availability whenever BTM API is not populated
539 info.manufacturer = mfct_set;
540 info.sub_ver = lmp_subver;
541 info.version = lmp_ver;
542 BTIF_STORAGE_FILL_PROPERTY(&property,
543 BT_PROPERTY_REMOTE_VERSION_INFO, sizeof(bt_remote_version_t),
545 status = btif_storage_set_remote_device_property(p_bd, &property);
546 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote version", status);
551 static void btif_update_remote_properties(BD_ADDR bd_addr, BD_NAME bd_name,
552 DEV_CLASS dev_class, tBT_DEVICE_TYPE device_type)
554 int num_properties = 0;
555 bt_property_t properties[3];
559 bt_device_type_t dev_type;
561 memset(properties, 0, sizeof(properties));
562 bdcpy(bdaddr.address, bd_addr);
565 if (strlen((const char *) bd_name))
567 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
568 BT_PROPERTY_BDNAME, strlen((char *)bd_name), bd_name);
569 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
570 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device name", status);
574 /* class of device */
575 cod = devclass2uint(dev_class);
576 BTIF_TRACE_DEBUG("%s cod is 0x%06x", __func__, cod);
578 /* Try to retrieve cod from storage */
579 BTIF_TRACE_DEBUG("%s cod is 0, checking cod from storage", __func__);
580 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
581 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
582 status = btif_storage_get_remote_device_property(&bdaddr, &properties[num_properties]);
583 BTIF_TRACE_DEBUG("%s cod retrieved from storage is 0x%06x", __func__, cod);
585 BTIF_TRACE_DEBUG("%s cod is again 0, set as unclassified", __func__);
586 cod = COD_UNCLASSIFIED;
590 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
591 BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
592 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
593 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device class", status);
597 bt_property_t prop_name;
598 uint8_t remote_dev_type;
599 BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_TYPE_OF_DEVICE,
600 sizeof(uint8_t), &remote_dev_type);
601 if (btif_storage_get_remote_device_property(&bdaddr, &prop_name) == BT_STATUS_SUCCESS)
602 dev_type = remote_dev_type | device_type;
604 dev_type = device_type;
606 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties],
607 BT_PROPERTY_TYPE_OF_DEVICE, sizeof(dev_type), &dev_type);
608 status = btif_storage_set_remote_device_property(&bdaddr, &properties[num_properties]);
609 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device type", status);
612 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
613 status, &bdaddr, num_properties, properties);
616 /*******************************************************************************
618 ** Function btif_dm_cb_hid_remote_name
620 ** Description Remote name callback for HID device. Called in btif context
621 ** Special handling for HID devices
625 *******************************************************************************/
626 static void btif_dm_cb_hid_remote_name(tBTM_REMOTE_DEV_NAME *p_remote_name)
628 BTIF_TRACE_DEBUG("%s: status=%d pairing_cb.state=%d", __FUNCTION__, p_remote_name->status, pairing_cb.state);
629 if (pairing_cb.state == BT_BOND_STATE_BONDING)
631 bt_bdaddr_t remote_bd;
633 bdcpy(remote_bd.address, pairing_cb.bd_addr);
635 if (p_remote_name->status == BTM_SUCCESS)
637 bond_state_changed(BT_STATUS_SUCCESS, &remote_bd, BT_BOND_STATE_BONDED);
640 bond_state_changed(BT_STATUS_FAIL, &remote_bd, BT_BOND_STATE_NONE);
644 /*******************************************************************************
646 ** Function btif_dm_cb_create_bond
648 ** Description Create bond initiated from the BTIF thread context
649 ** Special handling for HID devices
653 *******************************************************************************/
654 static void btif_dm_cb_create_bond(bt_bdaddr_t *bd_addr, tBTA_TRANSPORT transport)
656 BOOLEAN is_hid = check_cod(bd_addr, COD_HID_POINTING);
657 bond_state_changed(BT_STATUS_SUCCESS, bd_addr, BT_BOND_STATE_BONDING);
659 #if BLE_INCLUDED == TRUE
663 bdaddr_to_string(bd_addr, bdstr, sizeof(bdstr));
664 if (transport == BT_TRANSPORT_LE)
666 if (!btif_config_get_int((char const *)&bdstr,"DevType", &device_type))
668 btif_config_set_int(bdstr, "DevType", BT_DEVICE_TYPE_BLE);
670 if (btif_storage_get_remote_addr_type(bd_addr, &addr_type) != BT_STATUS_SUCCESS)
672 btif_storage_set_remote_addr_type(bd_addr, BLE_ADDR_PUBLIC);
675 if((btif_config_get_int((char const *)&bdstr,"DevType", &device_type) &&
676 (btif_storage_get_remote_addr_type(bd_addr, &addr_type) == BT_STATUS_SUCCESS) &&
677 (device_type & BT_DEVICE_TYPE_BLE) == BT_DEVICE_TYPE_BLE) || (transport == BT_TRANSPORT_LE))
679 BTA_DmAddBleDevice(bd_addr->address, addr_type, device_type);
683 #if BLE_INCLUDED == TRUE
684 if(is_hid && (device_type & BT_DEVICE_TYPE_BLE) == 0)
690 status = btif_hh_connect(bd_addr);
691 if(status != BT_STATUS_SUCCESS)
692 bond_state_changed(status, bd_addr, BT_BOND_STATE_NONE);
696 BTA_DmBondByTransport((UINT8 *)bd_addr->address, transport);
698 /* Track originator of bond creation */
699 pairing_cb.is_local_initiated = TRUE;
703 /*******************************************************************************
705 ** Function btif_dm_cb_remove_bond
707 ** Description remove bond initiated from the BTIF thread context
708 ** Special handling for HID devices
712 *******************************************************************************/
713 void btif_dm_cb_remove_bond(bt_bdaddr_t *bd_addr)
715 /*special handling for HID devices */
716 /* VUP needs to be sent if its a HID Device. The HID HOST module will check if there
717 is a valid hid connection with this bd_addr. If yes VUP will be issued.*/
718 #if (defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE))
719 if (btif_hh_virtual_unplug(bd_addr) != BT_STATUS_SUCCESS)
722 BTIF_TRACE_DEBUG("%s: Removing HH device", __func__);
723 BTA_DmRemoveDevice((UINT8 *)bd_addr->address);
727 /*******************************************************************************
729 ** Function btif_dm_get_connection_state
731 ** Description Returns whether the remote device is currently connected
732 ** and whether encryption is active for the connection
734 ** Returns 0 if not connected; 1 if connected and > 1 if connection is
737 *******************************************************************************/
738 uint16_t btif_dm_get_connection_state(const bt_bdaddr_t *bd_addr)
740 uint8_t *bda = (uint8_t*)bd_addr->address;
741 uint16_t rc = BTA_DmGetConnectionState(bda);
747 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_BR_EDR);
748 BTIF_TRACE_DEBUG("%s: security flags (BR/EDR)=0x%02x", __FUNCTION__, flags);
749 if (flags & BTM_SEC_FLAG_ENCRYPTED)
750 rc |= ENCRYPTED_BREDR;
752 BTM_GetSecurityFlagsByTransport(bda, &flags, BT_TRANSPORT_LE);
753 BTIF_TRACE_DEBUG("%s: security flags (LE)=0x%02x", __FUNCTION__, flags);
754 if (flags & BTM_SEC_FLAG_ENCRYPTED)
761 /*******************************************************************************
763 ** Function search_devices_copy_cb
765 ** Description Deep copy callback for search devices event
769 *******************************************************************************/
770 static void search_devices_copy_cb(UINT16 event, char *p_dest, char *p_src)
772 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
773 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
778 BTIF_TRACE_DEBUG("%s: event=%s", __FUNCTION__, dump_dm_search_event(event));
779 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
782 case BTA_DM_INQ_RES_EVT:
784 if (p_src_data->inq_res.p_eir)
786 p_dest_data->inq_res.p_eir = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
787 memcpy(p_dest_data->inq_res.p_eir, p_src_data->inq_res.p_eir, HCI_EXT_INQ_RESPONSE_LEN);
792 case BTA_DM_DISC_RES_EVT:
794 if (p_src_data->disc_res.raw_data_size && p_src_data->disc_res.p_raw_data)
796 p_dest_data->disc_res.p_raw_data = (UINT8 *)(p_dest + sizeof(tBTA_DM_SEARCH));
797 memcpy(p_dest_data->disc_res.p_raw_data,
798 p_src_data->disc_res.p_raw_data, p_src_data->disc_res.raw_data_size);
805 static void search_services_copy_cb(UINT16 event, char *p_dest, char *p_src)
807 tBTA_DM_SEARCH *p_dest_data = (tBTA_DM_SEARCH *) p_dest;
808 tBTA_DM_SEARCH *p_src_data = (tBTA_DM_SEARCH *) p_src;
812 memcpy(p_dest_data, p_src_data, sizeof(tBTA_DM_SEARCH));
815 case BTA_DM_DISC_RES_EVT:
817 if (p_src_data->disc_res.result == BTA_SUCCESS)
819 if (p_src_data->disc_res.num_uuids > 0)
821 p_dest_data->disc_res.p_uuid_list =
822 (UINT8*)(p_dest + sizeof(tBTA_DM_SEARCH));
823 memcpy(p_dest_data->disc_res.p_uuid_list, p_src_data->disc_res.p_uuid_list,
824 p_src_data->disc_res.num_uuids*MAX_UUID_SIZE);
825 GKI_freebuf(p_src_data->disc_res.p_uuid_list);
827 if (p_src_data->disc_res.p_raw_data != NULL)
829 GKI_freebuf(p_src_data->disc_res.p_raw_data);
835 /******************************************************************************
837 ** BTIF DM callback events
839 *****************************************************************************/
841 /*******************************************************************************
843 ** Function btif_dm_pin_req_evt
845 ** Description Executes pin request event in btif context
849 *******************************************************************************/
850 static void btif_dm_pin_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
855 bt_pin_code_t pin_code;
858 /* Remote properties update */
859 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
861 dev_type = BT_DEVICE_TYPE_BREDR;
863 btif_update_remote_properties(p_pin_req->bd_addr, p_pin_req->bd_name,
864 p_pin_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
866 bdcpy(bd_addr.address, p_pin_req->bd_addr);
867 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
869 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
871 cod = devclass2uint(p_pin_req->dev_class);
874 BTIF_TRACE_DEBUG("%s cod is 0, set as unclassified", __func__);
875 cod = COD_UNCLASSIFIED;
878 /* check for auto pair possiblity only if bond was initiated by local device */
879 if (pairing_cb.is_local_initiated && (p_pin_req->min_16_digit == FALSE))
881 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
882 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
883 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
884 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
885 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
886 check_cod(&bd_addr, COD_HID_POINTING))
888 BTIF_TRACE_DEBUG("%s()cod matches for auto pair", __FUNCTION__);
889 /* Check if this device can be auto paired */
890 if ((btif_storage_is_device_autopair_blacklisted(&bd_addr) == FALSE) &&
891 (pairing_cb.autopair_attempts == 0))
893 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
894 pin_code.pin[0] = 0x30;
895 pin_code.pin[1] = 0x30;
896 pin_code.pin[2] = 0x30;
897 pin_code.pin[3] = 0x30;
899 pairing_cb.autopair_attempts++;
900 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
904 else if (check_cod(&bd_addr, COD_HID_KEYBOARD) ||
905 check_cod(&bd_addr, COD_HID_COMBO))
907 if(( btif_storage_is_fixed_pin_zeros_keyboard (&bd_addr) == TRUE) &&
908 (pairing_cb.autopair_attempts == 0))
910 BTIF_TRACE_DEBUG("%s() Attempting auto pair", __FUNCTION__);
911 pin_code.pin[0] = 0x30;
912 pin_code.pin[1] = 0x30;
913 pin_code.pin[2] = 0x30;
914 pin_code.pin[3] = 0x30;
916 pairing_cb.autopair_attempts++;
917 BTA_DmPinReply( (UINT8*)bd_addr.address, TRUE, 4, pin_code.pin);
922 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
923 &bd_addr, &bd_name, cod, p_pin_req->min_16_digit);
926 /*******************************************************************************
928 ** Function btif_dm_ssp_cfm_req_evt
930 ** Description Executes SSP confirm request event in btif context
934 *******************************************************************************/
935 static void btif_dm_ssp_cfm_req_evt(tBTA_DM_SP_CFM_REQ *p_ssp_cfm_req)
940 BOOLEAN is_incoming = !(pairing_cb.state == BT_BOND_STATE_BONDING);
943 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
945 /* Remote properties update */
946 if (!btif_get_device_type(p_ssp_cfm_req->bd_addr, &dev_type))
948 dev_type = BT_DEVICE_TYPE_BREDR;
950 btif_update_remote_properties(p_ssp_cfm_req->bd_addr, p_ssp_cfm_req->bd_name,
951 p_ssp_cfm_req->dev_class, (tBT_DEVICE_TYPE) dev_type);
953 bdcpy(bd_addr.address, p_ssp_cfm_req->bd_addr);
954 memcpy(bd_name.name, p_ssp_cfm_req->bd_name, BD_NAME_LEN);
956 /* Set the pairing_cb based on the local & remote authentication requirements */
957 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
959 /* if just_works and bonding bit is not set treat this as temporary */
960 if (p_ssp_cfm_req->just_works && !(p_ssp_cfm_req->loc_auth_req & BTM_AUTH_BONDS) &&
961 !(p_ssp_cfm_req->rmt_auth_req & BTM_AUTH_BONDS) &&
962 !(check_cod((bt_bdaddr_t*)&p_ssp_cfm_req->bd_addr, COD_HID_POINTING)))
963 pairing_cb.bond_type = BOND_TYPE_TEMPORARY;
965 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
967 btm_set_bond_type_dev(p_ssp_cfm_req->bd_addr, pairing_cb.bond_type);
969 pairing_cb.is_ssp = TRUE;
971 /* If JustWorks auto-accept */
972 if (p_ssp_cfm_req->just_works)
974 /* Pairing consent for JustWorks needed if:
975 * 1. Incoming (non-temporary) pairing is detected AND
976 * 2. local IO capabilities are DisplayYesNo AND
977 * 3. remote IO capabiltiies are DisplayOnly or NoInputNoOutput;
979 if (is_incoming && pairing_cb.bond_type != BOND_TYPE_TEMPORARY &&
980 ((p_ssp_cfm_req->loc_io_caps == HCI_IO_CAP_DISPLAY_YESNO) &&
981 (p_ssp_cfm_req->rmt_io_caps == HCI_IO_CAP_DISPLAY_ONLY ||
982 p_ssp_cfm_req->rmt_io_caps == HCI_IO_CAP_NO_IO)))
984 BTIF_TRACE_EVENT("%s: User consent needed for incoming pairing request. loc_io_caps: %d, rmt_io_caps: %d",
985 __FUNCTION__, p_ssp_cfm_req->loc_io_caps, p_ssp_cfm_req->rmt_io_caps);
989 BTIF_TRACE_EVENT("%s: Auto-accept JustWorks pairing", __FUNCTION__);
990 btif_dm_ssp_reply(&bd_addr, BT_SSP_VARIANT_CONSENT, TRUE, 0);
995 cod = devclass2uint(p_ssp_cfm_req->dev_class);
998 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
999 cod = COD_UNCLASSIFIED;
1002 pairing_cb.sdp_attempts = 0;
1003 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
1004 (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT : BT_SSP_VARIANT_PASSKEY_CONFIRMATION),
1005 p_ssp_cfm_req->num_val);
1008 static void btif_dm_ssp_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
1010 bt_bdaddr_t bd_addr;
1011 bt_bdname_t bd_name;
1015 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
1017 /* Remote properties update */
1018 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
1020 dev_type = BT_DEVICE_TYPE_BREDR;
1022 btif_update_remote_properties(p_ssp_key_notif->bd_addr, p_ssp_key_notif->bd_name,
1023 p_ssp_key_notif->dev_class, (tBT_DEVICE_TYPE) dev_type);
1025 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
1026 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
1028 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
1029 pairing_cb.is_ssp = TRUE;
1030 cod = devclass2uint(p_ssp_key_notif->dev_class);
1033 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
1034 cod = COD_UNCLASSIFIED;
1037 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
1038 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
1039 p_ssp_key_notif->passkey);
1041 /*******************************************************************************
1043 ** Function btif_dm_auth_cmpl_evt
1045 ** Description Executes authentication complete event in btif context
1049 *******************************************************************************/
1050 static void btif_dm_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
1052 /* Save link key, if not temporary */
1053 bt_bdaddr_t bd_addr;
1054 bt_status_t status = BT_STATUS_FAIL;
1055 bt_bond_state_t state = BT_BOND_STATE_NONE;
1056 BOOLEAN skip_sdp = FALSE;
1058 BTIF_TRACE_DEBUG("%s: bond state=%d", __func__, pairing_cb.state);
1060 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
1061 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
1063 if ((p_auth_cmpl->key_type < HCI_LKEY_TYPE_DEBUG_COMB) ||
1064 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB) ||
1065 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_CHANGED_COMB) ||
1066 (p_auth_cmpl->key_type == HCI_LKEY_TYPE_AUTH_COMB_P_256) ||
1067 pairing_cb.bond_type == BOND_TYPE_PERSISTENT)
1070 BTIF_TRACE_DEBUG("%s: Storing link key. key_type=0x%x, bond_type=%d",
1071 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1072 ret = btif_storage_add_bonded_device(&bd_addr,
1073 p_auth_cmpl->key, p_auth_cmpl->key_type,
1074 pairing_cb.pin_code_len);
1075 ASSERTC(ret == BT_STATUS_SUCCESS, "storing link key failed", ret);
1079 BTIF_TRACE_DEBUG("%s: Temporary key. Not storing. key_type=0x%x, bond_type=%d",
1080 __FUNCTION__, p_auth_cmpl->key_type, pairing_cb.bond_type);
1081 if(pairing_cb.bond_type == BOND_TYPE_TEMPORARY)
1083 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
1085 btif_storage_remove_bonded_device(&bd_addr);
1086 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_NONE);
1092 // Skip SDP for certain HID Devices
1093 if (p_auth_cmpl->success)
1095 #if BLE_INCLUDED == TRUE
1096 btif_storage_set_remote_addr_type(&bd_addr, p_auth_cmpl->addr_type);
1098 btif_update_remote_properties(p_auth_cmpl->bd_addr,
1099 p_auth_cmpl->bd_name, NULL, p_auth_cmpl->dev_type);
1100 pairing_cb.timeout_retries = 0;
1101 status = BT_STATUS_SUCCESS;
1102 state = BT_BOND_STATE_BONDED;
1103 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
1105 if (check_sdp_bl(&bd_addr) && check_cod_hid(&bd_addr, COD_HID_MAJOR))
1107 LOG_WARN("%s:skip SDP", __FUNCTION__);
1110 if(!pairing_cb.is_local_initiated && skip_sdp)
1112 bond_state_changed(status, &bd_addr, state);
1114 LOG_WARN("%s: Incoming HID Connection",__FUNCTION__);
1116 bt_bdaddr_t bd_addr;
1118 char uuid_str[128] = UUID_HUMAN_INTERFACE_DEVICE;
1120 string_to_uuid(uuid_str, &uuid);
1122 prop.type = BT_PROPERTY_UUIDS;
1124 prop.len = MAX_UUID_SIZE;
1126 /* Send the event to the BTIF */
1127 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1128 BT_STATUS_SUCCESS, &bd_addr, 1, &prop);
1132 /* Trigger SDP on the device */
1133 pairing_cb.sdp_attempts = 1;;
1135 #if BLE_INCLUDED == TRUE
1136 /* If bonded due to cross-key, save the static address too*/
1137 if(pairing_cb.state == BT_BOND_STATE_BONDING &&
1138 (bdcmp(p_auth_cmpl->bd_addr, pairing_cb.bd_addr) != 0))
1140 BTIF_TRACE_DEBUG("%s: bonding initiated due to cross key, adding static address",
1142 bdcpy(pairing_cb.static_bdaddr.address, p_auth_cmpl->bd_addr);
1146 if(btif_dm_inquiry_in_progress)
1147 btif_dm_cancel_discovery();
1149 btif_dm_get_remote_services(&bd_addr);
1151 // Do not call bond_state_changed_cb yet. Wait until remote service discovery is complete
1155 // Map the HCI fail reason to bt status
1156 switch(p_auth_cmpl->fail_reason)
1158 case HCI_ERR_PAGE_TIMEOUT:
1159 if (interop_match(INTEROP_AUTO_RETRY_PAIRING, &bd_addr)
1160 && pairing_cb.timeout_retries)
1162 BTIF_TRACE_WARNING("%s() - Pairing timeout; retrying (%d) ...", __FUNCTION__, pairing_cb.timeout_retries);
1163 --pairing_cb.timeout_retries;
1164 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
1168 case HCI_ERR_CONNECTION_TOUT:
1169 status = BT_STATUS_RMT_DEV_DOWN;
1172 case HCI_ERR_PAIRING_NOT_ALLOWED:
1173 status = BT_STATUS_AUTH_REJECTED;
1176 case HCI_ERR_LMP_RESPONSE_TIMEOUT:
1177 status = BT_STATUS_AUTH_FAILURE;
1180 /* map the auth failure codes, so we can retry pairing if necessary */
1181 case HCI_ERR_AUTH_FAILURE:
1182 case HCI_ERR_KEY_MISSING:
1183 btif_storage_remove_bonded_device(&bd_addr);
1184 case HCI_ERR_HOST_REJECT_SECURITY:
1185 case HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE:
1186 case HCI_ERR_UNIT_KEY_USED:
1187 case HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED:
1188 case HCI_ERR_INSUFFCIENT_SECURITY:
1189 case HCI_ERR_PEER_USER:
1190 case HCI_ERR_UNSPECIFIED:
1191 BTIF_TRACE_DEBUG(" %s() Authentication fail reason %d",
1192 __FUNCTION__, p_auth_cmpl->fail_reason);
1193 if (pairing_cb.autopair_attempts == 1)
1195 BTIF_TRACE_DEBUG("%s(): Adding device to blacklist ", __FUNCTION__);
1197 /* Add the device to dynamic black list only if this device belongs to Audio/pointing dev class */
1198 if (check_cod(&bd_addr, COD_AV_HEADSETS) ||
1199 check_cod(&bd_addr, COD_AV_HANDSFREE) ||
1200 check_cod(&bd_addr, COD_AV_HEADPHONES) ||
1201 check_cod(&bd_addr, COD_AV_PORTABLE_AUDIO) ||
1202 check_cod(&bd_addr, COD_AV_HIFI_AUDIO) ||
1203 check_cod(&bd_addr, COD_HID_POINTING))
1205 btif_storage_add_device_to_autopair_blacklist (&bd_addr);
1207 pairing_cb.autopair_attempts++;
1209 /* Create the Bond once again */
1210 BTIF_TRACE_DEBUG("%s() auto pair failed. Reinitiate Bond", __FUNCTION__);
1211 btif_dm_cb_create_bond (&bd_addr, BTA_TRANSPORT_UNKNOWN);
1216 /* if autopair attempts are more than 1, or not attempted */
1217 status = BT_STATUS_AUTH_FAILURE;
1222 status = BT_STATUS_FAIL;
1224 /* Special Handling for HID Devices */
1225 if (check_cod(&bd_addr, COD_HID_POINTING)) {
1226 /* Remove Device as bonded in nvram as authentication failed */
1227 BTIF_TRACE_DEBUG("%s(): removing hid pointing device from nvram", __FUNCTION__);
1228 btif_storage_remove_bonded_device(&bd_addr);
1230 bond_state_changed(status, &bd_addr, state);
1234 /******************************************************************************
1236 ** Function btif_dm_search_devices_evt
1238 ** Description Executes search devices callback events in btif context
1242 ******************************************************************************/
1243 static void btif_dm_search_devices_evt (UINT16 event, char *p_param)
1245 tBTA_DM_SEARCH *p_search_data;
1246 BTIF_TRACE_EVENT("%s event=%s", __FUNCTION__, dump_dm_search_event(event));
1250 case BTA_DM_DISC_RES_EVT:
1252 p_search_data = (tBTA_DM_SEARCH *)p_param;
1253 /* Remote name update */
1254 if (strlen((const char *) p_search_data->disc_res.bd_name))
1256 bt_property_t properties[1];
1260 properties[0].type = BT_PROPERTY_BDNAME;
1261 properties[0].val = p_search_data->disc_res.bd_name;
1262 properties[0].len = strlen((char *)p_search_data->disc_res.bd_name);
1263 bdcpy(bdaddr.address, p_search_data->disc_res.bd_addr);
1265 status = btif_storage_set_remote_device_property(&bdaddr, &properties[0]);
1266 ASSERTC(status == BT_STATUS_SUCCESS, "failed to save remote device property", status);
1267 HAL_CBACK(bt_hal_cbacks, remote_device_properties_cb,
1268 status, &bdaddr, 1, properties);
1270 /* TODO: Services? */
1274 case BTA_DM_INQ_RES_EVT:
1276 /* inquiry result */
1280 UINT8 remote_name_len;
1281 tBTA_SERVICE_MASK services = 0;
1284 p_search_data = (tBTA_DM_SEARCH *)p_param;
1285 bdcpy(bdaddr.address, p_search_data->inq_res.bd_addr);
1287 BTIF_TRACE_DEBUG("%s() %s device_type = 0x%x\n", __FUNCTION__, bdaddr_to_string(&bdaddr, bdstr, sizeof(bdstr)),
1288 #if (BLE_INCLUDED == TRUE)
1289 p_search_data->inq_res.device_type);
1291 BT_DEVICE_TYPE_BREDR);
1295 cod = devclass2uint (p_search_data->inq_res.dev_class);
1298 LOG_DEBUG("%s cod is 0, set as unclassified", __func__);
1299 cod = COD_UNCLASSIFIED;
1302 if (!check_eir_remote_name(p_search_data, bdname.name, &remote_name_len))
1303 check_cached_remote_name(p_search_data, bdname.name, &remote_name_len);
1305 /* Check EIR for remote name and services */
1306 if (p_search_data->inq_res.p_eir)
1308 BTA_GetEirService(p_search_data->inq_res.p_eir, &services);
1309 BTIF_TRACE_DEBUG("%s()EIR BTA services = %08X", __FUNCTION__, (UINT32)services);
1310 /* 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("%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("%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 || pin_len > PIN_CODE_LEN)
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);
2679 BTIF_TRACE_DEBUG("+%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
2680 if(pairing_cb.is_local_initiated)
2682 /* if initing/responding to a dedicated bonding, use dedicate bonding bit */
2683 *p_auth_req = BTA_AUTH_DD_BOND | BTA_AUTH_SP_YES;
2687 /* peer initiated paring. They probably know what they want.
2688 ** Copy the mitm from peer device.
2690 BTIF_TRACE_DEBUG("%s: setting p_auth_req to peer's: %d",
2691 __FUNCTION__, pairing_cb.auth_req);
2692 *p_auth_req = (pairing_cb.auth_req & BTA_AUTH_BONDS);
2694 /* copy over the MITM bit as well. In addition if the peer has DisplayYesNo, force MITM */
2695 if ((yes_no_bit) || (pairing_cb.io_cap & BTM_IO_CAP_IO) )
2696 *p_auth_req |= BTA_AUTH_SP_YES;
2698 else if (yes_no_bit)
2700 /* set the general bonding bit for stored device */
2701 *p_auth_req = BTA_AUTH_GEN_BOND | yes_no_bit;
2703 BTIF_TRACE_DEBUG("-%s: p_auth_req=%d", __FUNCTION__, *p_auth_req);
2706 void btif_dm_proc_io_rsp(BD_ADDR bd_addr, tBTA_IO_CAP io_cap,
2707 tBTA_OOB_DATA oob_data, tBTA_AUTH_REQ auth_req)
2712 if(auth_req & BTA_AUTH_BONDS)
2714 BTIF_TRACE_DEBUG("%s auth_req:%d", __FUNCTION__, auth_req);
2715 pairing_cb.auth_req = auth_req;
2716 pairing_cb.io_cap = io_cap;
2720 #if (BTM_OOB_INCLUDED == TRUE)
2721 void btif_dm_set_oob_for_io_req(tBTA_OOB_DATA *p_oob_data)
2723 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2724 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2726 *p_oob_data = FALSE;
2732 BTIF_TRACE_DEBUG("btif_dm_set_oob_for_io_req *p_oob_data=%d", *p_oob_data);
2734 #endif /* BTM_OOB_INCLUDED */
2736 #ifdef BTIF_DM_OOB_TEST
2737 void btif_dm_load_local_oob(void)
2739 char prop_oob[PROPERTY_VALUE_MAX];
2740 property_get("service.brcm.bt.oob", prop_oob, "3");
2741 BTIF_TRACE_DEBUG("btif_dm_load_local_oob prop_oob = %s",prop_oob);
2742 if (prop_oob[0] != '3')
2744 #if (BTM_OOB_INCLUDED == TRUE)
2745 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2746 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 )
2748 BTIF_TRACE_DEBUG("btif_dm_load_local_oob: read OOB, call BTA_DmLocalOob()");
2752 BTIF_TRACE_ERROR("BTM_OOB_INCLUDED is FALSE!!(btif_dm_load_local_oob)");
2757 void btif_dm_proc_loc_oob(BOOLEAN valid, BT_OCTET16 c, BT_OCTET16 r)
2760 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2761 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2763 char prop_oob[PROPERTY_VALUE_MAX];
2764 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: valid=%d", valid);
2765 if (oob_cb.sp_c[0] == 0 && oob_cb.sp_c[1] == 0 &&
2766 oob_cb.sp_c[2] == 0 && oob_cb.sp_c[3] == 0 &&
2769 BTIF_TRACE_DEBUG("save local OOB data in memory");
2770 memcpy(oob_cb.sp_c, c, BT_OCTET16_LEN);
2771 memcpy(oob_cb.sp_r, r, BT_OCTET16_LEN);
2772 property_get("service.brcm.bt.oob", prop_oob, "3");
2773 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob prop_oob = %s",prop_oob);
2774 if (prop_oob[0] == '1')
2776 else if (prop_oob[0] == '2')
2780 fp = fopen(path, "wb+");
2783 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: failed to save local OOB data to %s", path);
2787 BTIF_TRACE_DEBUG("btif_dm_proc_loc_oob: save local OOB data into file %s",path);
2788 fwrite (c , 1 , BT_OCTET16_LEN , fp );
2789 fwrite (r , 1 , BT_OCTET16_LEN , fp );
2795 BOOLEAN btif_dm_proc_rmt_oob(BD_ADDR bd_addr, BT_OCTET16 p_c, BT_OCTET16 p_r)
2799 char *path_a = "/data/misc/bluedroid/LOCAL/a.key";
2800 char *path_b = "/data/misc/bluedroid/LOCAL/b.key";
2802 char prop_oob[PROPERTY_VALUE_MAX];
2803 BOOLEAN result = FALSE;
2804 bt_bdaddr_t bt_bd_addr;
2805 bdcpy(oob_cb.oob_bdaddr, bd_addr);
2806 property_get("service.brcm.bt.oob", prop_oob, "3");
2807 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob prop_oob = %s",prop_oob);
2808 if (prop_oob[0] == '1')
2810 else if (prop_oob[0] == '2')
2814 fp = fopen(path, "rb");
2817 BTIF_TRACE_DEBUG("btapp_dm_rmt_oob_reply: failed to read OOB keys from %s",path);
2822 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob: read OOB data from %s",path);
2823 fread (p_c , 1 , BT_OCTET16_LEN , fp );
2824 fread (p_r , 1 , BT_OCTET16_LEN , fp );
2827 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: TRUE");
2828 sprintf(t, "%02x:%02x:%02x:%02x:%02x:%02x",
2829 oob_cb.oob_bdaddr[0], oob_cb.oob_bdaddr[1], oob_cb.oob_bdaddr[2],
2830 oob_cb.oob_bdaddr[3], oob_cb.oob_bdaddr[4], oob_cb.oob_bdaddr[5]);
2831 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: peer_bdaddr = %s", t);
2832 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2833 p_c[0], p_c[1], p_c[2], p_c[3], p_c[4], p_c[5], p_c[6], p_c[7],
2834 p_c[8], p_c[9], p_c[10], p_c[11], p_c[12], p_c[13], p_c[14], p_c[15]);
2835 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: c = %s",t);
2836 sprintf(t, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
2837 p_r[0], p_r[1], p_r[2], p_r[3], p_r[4], p_r[5], p_r[6], p_r[7],
2838 p_r[8], p_r[9], p_r[10], p_r[11], p_r[12], p_r[13], p_r[14], p_r[15]);
2839 BTIF_TRACE_DEBUG("----btif_dm_proc_rmt_oob: r = %s",t);
2840 bdcpy(bt_bd_addr.address, bd_addr);
2841 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_BOND_STATE_BONDING,
2842 (char *)&bt_bd_addr, sizeof(bt_bdaddr_t), NULL);
2845 BTIF_TRACE_DEBUG("btif_dm_proc_rmt_oob result=%d",result);
2848 #endif /* BTIF_DM_OOB_TEST */
2849 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
2851 static void btif_dm_ble_key_notif_evt(tBTA_DM_SP_KEY_NOTIF *p_ssp_key_notif)
2853 bt_bdaddr_t bd_addr;
2854 bt_bdname_t bd_name;
2858 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
2860 /* Remote name update */
2861 if (!btif_get_device_type(p_ssp_key_notif->bd_addr, &dev_type))
2863 dev_type = BT_DEVICE_TYPE_BLE;
2865 btif_dm_update_ble_remote_properties(p_ssp_key_notif->bd_addr , p_ssp_key_notif->bd_name,
2866 (tBT_DEVICE_TYPE) dev_type);
2867 bdcpy(bd_addr.address, p_ssp_key_notif->bd_addr);
2868 memcpy(bd_name.name, p_ssp_key_notif->bd_name, BD_NAME_LEN);
2870 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
2871 pairing_cb.is_ssp = FALSE;
2872 cod = COD_UNCLASSIFIED;
2874 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
2875 cod, BT_SSP_VARIANT_PASSKEY_NOTIFICATION,
2876 p_ssp_key_notif->passkey);
2879 /*******************************************************************************
2881 ** Function btif_dm_ble_auth_cmpl_evt
2883 ** Description Executes authentication complete event in btif context
2887 *******************************************************************************/
2888 static void btif_dm_ble_auth_cmpl_evt (tBTA_DM_AUTH_CMPL *p_auth_cmpl)
2890 /* Save link key, if not temporary */
2891 bt_bdaddr_t bd_addr;
2892 bt_status_t status = BT_STATUS_FAIL;
2893 bt_bond_state_t state = BT_BOND_STATE_NONE;
2895 bdcpy(bd_addr.address, p_auth_cmpl->bd_addr);
2896 if ( (p_auth_cmpl->success == TRUE) && (p_auth_cmpl->key_present) )
2900 if (p_auth_cmpl->success)
2902 status = BT_STATUS_SUCCESS;
2903 state = BT_BOND_STATE_BONDED;
2906 bdcpy(bdaddr.address, p_auth_cmpl->bd_addr);
2907 if (btif_storage_get_remote_addr_type(&bdaddr, &addr_type) != BT_STATUS_SUCCESS)
2908 btif_storage_set_remote_addr_type(&bdaddr, p_auth_cmpl->addr_type);
2910 /* Test for temporary bonding */
2911 if (btm_get_bond_type_dev(p_auth_cmpl->bd_addr) == BOND_TYPE_TEMPORARY) {
2912 BTIF_TRACE_DEBUG("%s: sending BT_BOND_STATE_NONE for Temp pairing",
2914 btif_storage_remove_bonded_device(&bdaddr);
2915 state = BT_BOND_STATE_NONE;
2917 btif_dm_save_ble_bonding_keys();
2918 BTA_GATTC_Refresh(bd_addr.address);
2919 btif_dm_get_remote_services_by_transport(&bd_addr, BTA_GATT_TRANSPORT_LE);
2924 /*Map the HCI fail reason to bt status */
2925 switch (p_auth_cmpl->fail_reason)
2927 case BTA_DM_AUTH_SMP_PAIR_AUTH_FAIL:
2928 case BTA_DM_AUTH_SMP_CONFIRM_VALUE_FAIL:
2929 btif_dm_remove_ble_bonding_keys();
2930 status = BT_STATUS_AUTH_FAILURE;
2932 case BTA_DM_AUTH_SMP_PAIR_NOT_SUPPORT:
2933 status = BT_STATUS_AUTH_REJECTED;
2936 btif_dm_remove_ble_bonding_keys();
2937 status = BT_STATUS_FAIL;
2941 bond_state_changed(status, &bd_addr, state);
2946 void btif_dm_load_ble_local_keys(void)
2948 memset(&ble_local_key_cb, 0, sizeof(btif_dm_local_key_cb_t));
2950 if (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_ER,(char*)&ble_local_key_cb.er[0],
2951 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)
2953 ble_local_key_cb.is_er_rcvd = TRUE;
2954 BTIF_TRACE_DEBUG("%s BLE ER key loaded",__FUNCTION__ );
2957 if ((btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IR,(char*)&ble_local_key_cb.id_keys.ir[0],
2958 BT_OCTET16_LEN)== BT_STATUS_SUCCESS )&&
2959 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_IRK, (char*)&ble_local_key_cb.id_keys.irk[0],
2960 BT_OCTET16_LEN)== BT_STATUS_SUCCESS)&&
2961 (btif_storage_get_ble_local_key(BTIF_DM_LE_LOCAL_KEY_DHK,(char*)&ble_local_key_cb.id_keys.dhk[0],
2962 BT_OCTET16_LEN)== BT_STATUS_SUCCESS))
2964 ble_local_key_cb.is_id_keys_rcvd = TRUE;
2965 BTIF_TRACE_DEBUG("%s BLE ID keys loaded",__FUNCTION__ );
2969 void btif_dm_get_ble_local_keys(tBTA_DM_BLE_LOCAL_KEY_MASK *p_key_mask, BT_OCTET16 er,
2970 tBTA_BLE_LOCAL_ID_KEYS *p_id_keys)
2972 if (ble_local_key_cb.is_er_rcvd )
2974 memcpy(&er[0], &ble_local_key_cb.er[0], sizeof(BT_OCTET16));
2975 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ER;
2978 if (ble_local_key_cb.is_id_keys_rcvd)
2980 memcpy(&p_id_keys->ir[0], &ble_local_key_cb.id_keys.ir[0], sizeof(BT_OCTET16));
2981 memcpy(&p_id_keys->irk[0], &ble_local_key_cb.id_keys.irk[0], sizeof(BT_OCTET16));
2982 memcpy(&p_id_keys->dhk[0], &ble_local_key_cb.id_keys.dhk[0], sizeof(BT_OCTET16));
2983 *p_key_mask |= BTA_BLE_LOCAL_KEY_TYPE_ID;
2985 BTIF_TRACE_DEBUG("%s *p_key_mask=0x%02x",__FUNCTION__, *p_key_mask);
2988 void btif_dm_save_ble_bonding_keys(void)
2991 bt_bdaddr_t bd_addr;
2993 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
2995 bdcpy(bd_addr.address, pairing_cb.bd_addr);
2997 if (pairing_cb.ble.is_penc_key_rcvd)
2999 btif_storage_add_ble_bonding_key(&bd_addr,
3000 (char *) &pairing_cb.ble.penc_key,
3001 BTIF_DM_LE_KEY_PENC,
3002 sizeof(tBTM_LE_PENC_KEYS));
3005 if (pairing_cb.ble.is_pid_key_rcvd)
3007 btif_storage_add_ble_bonding_key(&bd_addr,
3008 (char *) &pairing_cb.ble.pid_key,
3010 sizeof(tBTM_LE_PID_KEYS));
3014 if (pairing_cb.ble.is_pcsrk_key_rcvd)
3016 btif_storage_add_ble_bonding_key(&bd_addr,
3017 (char *) &pairing_cb.ble.pcsrk_key,
3018 BTIF_DM_LE_KEY_PCSRK,
3019 sizeof(tBTM_LE_PCSRK_KEYS));
3023 if (pairing_cb.ble.is_lenc_key_rcvd)
3025 btif_storage_add_ble_bonding_key(&bd_addr,
3026 (char *) &pairing_cb.ble.lenc_key,
3027 BTIF_DM_LE_KEY_LENC,
3028 sizeof(tBTM_LE_LENC_KEYS));
3031 if (pairing_cb.ble.is_lcsrk_key_rcvd)
3033 btif_storage_add_ble_bonding_key(&bd_addr,
3034 (char *) &pairing_cb.ble.lcsrk_key,
3035 BTIF_DM_LE_KEY_LCSRK,
3036 sizeof(tBTM_LE_LCSRK_KEYS));
3039 if (pairing_cb.ble.is_lidk_key_rcvd)
3041 btif_storage_add_ble_bonding_key(&bd_addr,
3050 void btif_dm_remove_ble_bonding_keys(void)
3052 bt_bdaddr_t bd_addr;
3054 BTIF_TRACE_DEBUG("%s",__FUNCTION__ );
3056 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3057 btif_storage_remove_ble_bonding_keys(&bd_addr);
3061 /*******************************************************************************
3063 ** Function btif_dm_ble_sec_req_evt
3065 ** Description Eprocess security request event in btif context
3069 *******************************************************************************/
3070 void btif_dm_ble_sec_req_evt(tBTA_DM_BLE_SEC_REQ *p_ble_req)
3072 bt_bdaddr_t bd_addr;
3073 bt_bdname_t bd_name;
3077 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
3079 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3081 BTIF_TRACE_DEBUG("%s Discard security request", __FUNCTION__);
3085 /* Remote name update */
3086 if (!btif_get_device_type(p_ble_req->bd_addr, &dev_type))
3088 dev_type = BT_DEVICE_TYPE_BLE;
3090 btif_dm_update_ble_remote_properties(p_ble_req->bd_addr, p_ble_req->bd_name,
3091 (tBT_DEVICE_TYPE) dev_type);
3093 bdcpy(bd_addr.address, p_ble_req->bd_addr);
3094 memcpy(bd_name.name, p_ble_req->bd_name, BD_NAME_LEN);
3096 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3098 pairing_cb.bond_type = BOND_TYPE_PERSISTENT;
3099 pairing_cb.is_le_only = TRUE;
3100 pairing_cb.is_le_nc = FALSE;
3101 pairing_cb.is_ssp = TRUE;
3102 btm_set_bond_type_dev(p_ble_req->bd_addr, pairing_cb.bond_type);
3104 cod = COD_UNCLASSIFIED;
3106 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, cod,
3107 BT_SSP_VARIANT_CONSENT, 0);
3112 /*******************************************************************************
3114 ** Function btif_dm_ble_passkey_req_evt
3116 ** Description Executes pin request event in btif context
3120 *******************************************************************************/
3121 static void btif_dm_ble_passkey_req_evt(tBTA_DM_PIN_REQ *p_pin_req)
3123 bt_bdaddr_t bd_addr;
3124 bt_bdname_t bd_name;
3128 /* Remote name update */
3129 if (!btif_get_device_type(p_pin_req->bd_addr, &dev_type))
3131 dev_type = BT_DEVICE_TYPE_BLE;
3133 btif_dm_update_ble_remote_properties(p_pin_req->bd_addr,p_pin_req->bd_name,
3134 (tBT_DEVICE_TYPE) dev_type);
3136 bdcpy(bd_addr.address, p_pin_req->bd_addr);
3137 memcpy(bd_name.name, p_pin_req->bd_name, BD_NAME_LEN);
3139 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3140 pairing_cb.is_le_only = TRUE;
3142 cod = COD_UNCLASSIFIED;
3144 HAL_CBACK(bt_hal_cbacks, pin_request_cb,
3145 &bd_addr, &bd_name, cod, FALSE);
3147 static void btif_dm_ble_key_nc_req_evt(tBTA_DM_SP_KEY_NOTIF *p_notif_req)
3149 /* TODO implement key notification for numeric comparison */
3150 BTIF_TRACE_DEBUG("%s", __FUNCTION__);
3152 /* Remote name update */
3153 btif_update_remote_properties(p_notif_req->bd_addr , p_notif_req->bd_name,
3154 NULL, BT_DEVICE_TYPE_BLE);
3156 bt_bdaddr_t bd_addr;
3157 bdcpy(bd_addr.address, p_notif_req->bd_addr);
3159 bt_bdname_t bd_name;
3160 memcpy(bd_name.name, p_notif_req->bd_name, BD_NAME_LEN);
3162 bond_state_changed(BT_STATUS_SUCCESS, &bd_addr, BT_BOND_STATE_BONDING);
3163 pairing_cb.is_ssp = FALSE;
3164 pairing_cb.is_le_only = TRUE;
3165 pairing_cb.is_le_nc = TRUE;
3167 HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name,
3168 COD_UNCLASSIFIED, BT_SSP_VARIANT_PASSKEY_CONFIRMATION,
3169 p_notif_req->passkey);
3172 void btif_dm_update_ble_remote_properties( BD_ADDR bd_addr, BD_NAME bd_name,
3173 tBT_DEVICE_TYPE dev_type)
3175 btif_update_remote_properties(bd_addr,bd_name,NULL,dev_type);
3178 static void btif_dm_ble_tx_test_cback(void *p)
3180 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TX_TEST,
3181 (char *)p, 1, NULL);
3184 static void btif_dm_ble_rx_test_cback(void *p)
3186 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_RX_TEST,
3187 (char *)p, 1, NULL);
3190 static void btif_dm_ble_test_end_cback(void *p)
3192 btif_transfer_context(btif_dm_generic_evt, BTIF_DM_CB_LE_TEST_END,
3193 (char *)p, 3, NULL);
3195 /*******************************************************************************
3197 ** Function btif_le_test_mode
3199 ** Description Sends a HCI BLE Test command to the Controller
3201 ** Returns BT_STATUS_SUCCESS on success
3203 *******************************************************************************/
3204 bt_status_t btif_le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
3207 case HCI_BLE_TRANSMITTER_TEST:
3208 if (len != 3) return BT_STATUS_PARM_INVALID;
3209 BTM_BleTransmitterTest(buf[0],buf[1],buf[2], btif_dm_ble_tx_test_cback);
3211 case HCI_BLE_RECEIVER_TEST:
3212 if (len != 1) return BT_STATUS_PARM_INVALID;
3213 BTM_BleReceiverTest(buf[0], btif_dm_ble_rx_test_cback);
3215 case HCI_BLE_TEST_END:
3216 BTM_BleTestEnd((tBTM_CMPL_CB*) btif_dm_ble_test_end_cback);
3219 BTIF_TRACE_ERROR("%s: Unknown LE Test Mode Command 0x%x", __FUNCTION__, opcode);
3220 return BT_STATUS_UNSUPPORTED;
3222 return BT_STATUS_SUCCESS;
3226 void btif_dm_on_disable()
3228 /* cancel any pending pairing requests */
3229 if (pairing_cb.state == BT_BOND_STATE_BONDING)
3231 bt_bdaddr_t bd_addr;
3233 BTIF_TRACE_DEBUG("%s: Cancel pending pairing request", __FUNCTION__);
3234 bdcpy(bd_addr.address, pairing_cb.bd_addr);
3235 btif_dm_cancel_bond(&bd_addr);
3239 /*******************************************************************************
3241 ** Function btif_dm_read_energy_info
3243 ** Description Reads the energy info from controller
3247 *******************************************************************************/
3248 void btif_dm_read_energy_info()
3250 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
3251 BTA_DmBleGetEnergyInfo(bta_energy_info_cb);
3255 static char* btif_get_default_local_name() {
3256 if (btif_default_local_name[0] == '\0')
3258 int max_len = sizeof(btif_default_local_name) - 1;
3259 if (BTM_DEF_LOCAL_NAME[0] != '\0')
3261 strncpy(btif_default_local_name, BTM_DEF_LOCAL_NAME, max_len);
3265 char prop_model[PROPERTY_VALUE_MAX];
3266 property_get(PROPERTY_PRODUCT_MODEL, prop_model, "");
3267 strncpy(btif_default_local_name, prop_model, max_len);
3269 btif_default_local_name[max_len] = '\0';
3271 return btif_default_local_name;