diff options
author | Andre Eisenbach <andre@broadcom.com> | 2012-02-22 13:18:21 -0800 |
---|---|---|
committer | Matthew Xie <mattx@google.com> | 2012-07-14 11:19:11 -0700 |
commit | e448862a47c08eb23185aaed574b39264f5005fc (patch) | |
tree | 2bc6246e3091315e77224fd798ea2fe8074ef972 /bta/dm | |
parent | a2ca4b83ab8bbbfd8d5f6693e927ed4b82094624 (diff) | |
download | external_bluetooth_bluedroid-e448862a47c08eb23185aaed574b39264f5005fc.zip external_bluetooth_bluedroid-e448862a47c08eb23185aaed574b39264f5005fc.tar.gz external_bluetooth_bluedroid-e448862a47c08eb23185aaed574b39264f5005fc.tar.bz2 |
Initial Bluedroid stack commit
Diffstat (limited to 'bta/dm')
-rw-r--r-- | bta/dm/bta_dm_act.c | 4678 | ||||
-rw-r--r-- | bta/dm/bta_dm_api.c | 1602 | ||||
-rw-r--r-- | bta/dm/bta_dm_cfg.c | 424 | ||||
-rw-r--r-- | bta/dm/bta_dm_ci.c | 106 | ||||
-rw-r--r-- | bta/dm/bta_dm_int.h | 956 | ||||
-rw-r--r-- | bta/dm/bta_dm_main.c | 331 | ||||
-rw-r--r-- | bta/dm/bta_dm_pm.c | 980 | ||||
-rw-r--r-- | bta/dm/bta_dm_sco.c | 684 |
8 files changed, 9761 insertions, 0 deletions
diff --git a/bta/dm/bta_dm_act.c b/bta/dm/bta_dm_act.c new file mode 100644 index 0000000..e21dc3e --- /dev/null +++ b/bta/dm/bta_dm_act.c @@ -0,0 +1,4678 @@ +/***************************************************************************** +** +** Name: bta_dm_act.c +** +** Description: This file contains the action functions for device manager +** state machine. +** +** Copyright (c) 2003-2011, Broadcom Corp., All Rights Reserved. +** Broadcom Bluetooth Core. Proprietary and confidential. +** +*****************************************************************************/ + +#include "bt_types.h" +#include "gki.h" +#include "bd.h" +#include "bta_sys.h" +#include "bta_api.h" +#include "bta_dm_int.h" +#include "bta_dm_co.h" +#include "btm_api.h" +#include "btm_int.h" +#include "btu.h" +#include "sdp_api.h" +#include "l2c_api.h" +#include "wbt_api.h" +#include "utl.h" +#include <string.h> + +static void bta_dm_inq_results_cb (tBTM_INQ_RESULTS *p_inq, UINT8 *p_eir); +static void bta_dm_inq_cmpl_cb (void * p_result); +static void bta_dm_service_search_remname_cback (BD_ADDR bd_addr, DEV_CLASS dc, BD_NAME bd_name); +static void bta_dm_remname_cback (tBTM_REMOTE_DEV_NAME *p_remote_name); +static void bta_dm_find_services ( BD_ADDR bd_addr); +static void bta_dm_discover_next_device(void); +static void bta_dm_sdp_callback (UINT16 sdp_status); +static UINT8 bta_dm_authorize_cback (BD_ADDR bd_addr, DEV_CLASS dev_class, BD_NAME bd_name, UINT8 *service_name, UINT8 service_id, BOOLEAN is_originator); +static UINT8 bta_dm_pin_cback (BD_ADDR bd_addr, DEV_CLASS dev_class, BD_NAME bd_name); +static UINT8 bta_dm_link_key_request_cback (BD_ADDR bd_addr, LINK_KEY key); +static UINT8 bta_dm_new_link_key_cback(BD_ADDR bd_addr, DEV_CLASS dev_class, BD_NAME bd_name, LINK_KEY key, UINT8 key_type); +static UINT8 bta_dm_authentication_complete_cback(BD_ADDR bd_addr, DEV_CLASS dev_class,BD_NAME bd_name, int result); +static void bta_dm_local_name_cback(BD_ADDR bd_addr); +static BOOLEAN bta_dm_check_av(UINT16 event); +#if (BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE) +static void bta_dm_bl_change_cback (tBTM_BL_EVENT_DATA *p_data); +#else +static void bta_dm_acl_change_cback (BD_ADDR p_bda, DEV_CLASS p_dc, BD_NAME p_bdn, BD_FEATURES features, BOOLEAN is_new); +#endif +static void bta_dm_policy_cback(tBTA_SYS_CONN_STATUS status, UINT8 id, UINT8 app_id, BD_ADDR peer_addr); + +/* Extended Inquiry Response */ +static UINT8 bta_dm_sp_cback (tBTM_SP_EVT event, tBTM_SP_EVT_DATA *p_data); + +#if (BTM_EIR_SERVER_INCLUDED == TRUE) +static void bta_dm_set_eir (char *local_name); +#endif /* BTM_EIR_SERVER_INCLUDED */ + +#if (BTM_EIR_CLIENT_INCLUDED == TRUE) +static void bta_dm_eir_search_services( tBTM_INQ_RESULTS *p_result, + tBTA_SERVICE_MASK *p_services_to_search, + tBTA_SERVICE_MASK *p_services_found); +#endif /* BTM_EIR_CLIENT_INCLUDED */ + +static void bta_dm_rssi_cback (tBTM_RSSI_RESULTS *p_result); +static void bta_dm_signal_strength_timer_cback (TIMER_LIST_ENT *p_tle); +static void bta_dm_link_quality_cback (tBTM_LINK_QUALITY_RESULTS *p_result); +static void bta_dm_search_timer_cback (TIMER_LIST_ENT *p_tle); +static void bta_dm_disable_timer_cback (TIMER_LIST_ENT *p_tle); +static void bta_dm_disable_conn_down_timer_cback (TIMER_LIST_ENT *p_tle); +static void bta_dm_rm_cback(tBTA_SYS_CONN_STATUS status, UINT8 id, UINT8 app_id, BD_ADDR peer_addr); +static void bta_dm_adjust_roles(void); +static char *bta_dm_get_remname(void); +static void bta_dm_bond_cancel_complete_cback(tBTM_STATUS result); + +static BOOLEAN bta_dm_read_remote_device_name (BD_ADDR bd_addr); +static void bta_dm_discover_device(BD_ADDR remote_bd_addr); + +static void bta_dm_sys_hw_cback( tBTA_SYS_HW_EVT status ); + +#if ((defined BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) + #if ((defined SMP_INCLUDED) && (SMP_INCLUDED == TRUE)) +static UINT8 bta_dm_ble_smp_cback (tBTM_LE_EVT event, BD_ADDR bda, tBTM_LE_EVT_DATA *p_data); + #endif +static void bta_dm_ble_id_key_cback (UINT8 key_type, tBTM_BLE_LOCAL_KEYS *p_key); + #if ((defined BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)) +static void btm_dm_start_gatt_discovery ( BD_ADDR bd_addr); +static void bta_dm_cancel_gatt_discovery(BD_ADDR bd_addr); +static void bta_dm_gattc_callback(tBTA_GATTC_EVT event, tBTA_GATTC *p_data); + #endif +#endif + +const UINT16 bta_service_id_to_uuid_lkup_tbl [BTA_MAX_SERVICE_ID] = +{ + UUID_SERVCLASS_PNP_INFORMATION, /* Reserved */ + UUID_SERVCLASS_SERIAL_PORT, /* BTA_SPP_SERVICE_ID */ + UUID_SERVCLASS_DIALUP_NETWORKING, /* BTA_DUN_SERVICE_ID */ + UUID_SERVCLASS_FAX, /* BTA_FAX_SERVICE_ID */ + UUID_SERVCLASS_LAN_ACCESS_USING_PPP, /* BTA_LAP_SERVICE_ID */ + UUID_SERVCLASS_HEADSET, /* BTA_HSP_SERVICE_ID */ + UUID_SERVCLASS_HF_HANDSFREE, /* BTA_HFP_SERVICE_ID */ + UUID_SERVCLASS_OBEX_OBJECT_PUSH, /* BTA_OPP_SERVICE_ID */ + UUID_SERVCLASS_OBEX_FILE_TRANSFER, /* BTA_FTP_SERVICE_ID */ + UUID_SERVCLASS_CORDLESS_TELEPHONY, /* BTA_CTP_SERVICE_ID */ + UUID_SERVCLASS_INTERCOM, /* BTA_ICP_SERVICE_ID */ + UUID_SERVCLASS_IRMC_SYNC, /* BTA_SYNC_SERVICE_ID */ + UUID_SERVCLASS_DIRECT_PRINTING, /* BTA_BPP_SERVICE_ID */ + UUID_SERVCLASS_IMAGING_RESPONDER, /* BTA_BIP_SERVICE_ID */ + UUID_SERVCLASS_PANU, /* BTA_PANU_SERVICE_ID */ + UUID_SERVCLASS_NAP, /* BTA_NAP_SERVICE_ID */ + UUID_SERVCLASS_GN, /* BTA_GN_SERVICE_ID */ + UUID_SERVCLASS_SAP, /* BTA_SAP_SERVICE_ID */ + UUID_SERVCLASS_AUDIO_SINK, /* BTA_A2DP_SERVICE_ID */ + UUID_SERVCLASS_AV_REMOTE_CONTROL, /* BTA_AVRCP_SERVICE_ID */ + UUID_SERVCLASS_HUMAN_INTERFACE, /* BTA_HID_SERVICE_ID */ + UUID_SERVCLASS_VIDEO_SINK, /* BTA_VDP_SERVICE_ID */ + UUID_SERVCLASS_PBAP_PSE, /* BTA_PBAP_SERVICE_ID */ + UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY, /* BTA_HSP_HS_SERVICE_ID */ + UUID_SERVCLASS_AG_HANDSFREE, /* BTA_HFP_HS_SERVICE_ID */ + UUID_SERVCLASS_MESSAGE_ACCESS, /* BTA_MAP_SERVICE_ID */ + UUID_SERVCLASS_MESSAGE_NOTIFICATION, /* BTA_MN_SERVICE_ID */ + UUID_SERVCLASS_HDP_PROFILE, /* BTA_HDP_SERVICE_ID */ + UUID_SERVCLASS_PBAP_PCE /* BTA_PCE_SERVICE_ID */ +#if BLE_INCLUDED && BTA_GATT_INCLUDED + ,UUID_PROTOCOL_ATT /* BTA_GATT_SERVICE_ID */ +#endif +}; + +/* + * NOTE : The number of element in bta_service_id_to_btm_srv_id_lkup_tbl should be matching with + * the value BTA_MAX_SERVICE_ID in bta_api.h + * + * i.e., If you add new Service ID for BTA, the correct security ID of the new service + * from Security service definitions (btm_api.h) should be added to this lookup table. + */ +const UINT32 bta_service_id_to_btm_srv_id_lkup_tbl [BTA_MAX_SERVICE_ID] = +{ + 0, /* Reserved */ + BTM_SEC_SERVICE_SERIAL_PORT, /* BTA_SPP_SERVICE_ID */ + BTM_SEC_SERVICE_DUN, /* BTA_DUN_SERVICE_ID */ + BTM_SEC_SERVICE_FAX, /* BTA_FAX_SERVICE_ID */ + BTM_SEC_SERVICE_LAN_ACCESS, /* BTA_LAP_SERVICE_ID */ + BTM_SEC_SERVICE_HEADSET_AG, /* BTA_HSP_SERVICE_ID */ + BTM_SEC_SERVICE_AG_HANDSFREE, /* BTA_HFP_SERVICE_ID */ + BTM_SEC_SERVICE_OBEX, /* BTA_OPP_SERVICE_ID */ + BTM_SEC_SERVICE_OBEX_FTP, /* BTA_FTP_SERVICE_ID */ + BTM_SEC_SERVICE_CORDLESS, /* BTA_CTP_SERVICE_ID */ + BTM_SEC_SERVICE_INTERCOM, /* BTA_ICP_SERVICE_ID */ + BTM_SEC_SERVICE_IRMC_SYNC, /* BTA_SYNC_SERVICE_ID */ + BTM_SEC_SERVICE_BPP_JOB, /* BTA_BPP_SERVICE_ID */ + BTM_SEC_SERVICE_BIP, /* BTA_BIP_SERVICE_ID */ + BTM_SEC_SERVICE_BNEP_PANU, /* BTA_PANU_SERVICE_ID */ + BTM_SEC_SERVICE_BNEP_NAP, /* BTA_NAP_SERVICE_ID */ + BTM_SEC_SERVICE_BNEP_GN, /* BTA_GN_SERVICE_ID */ + BTM_SEC_SERVICE_SAP, /* BTA_SAP_SERVICE_ID */ + BTM_SEC_SERVICE_AVDTP, /* BTA_A2DP_SERVICE_ID */ + BTM_SEC_SERVICE_AVCTP, /* BTA_AVRCP_SERVICE_ID */ + BTM_SEC_SERVICE_HID_SEC_CTRL, /* BTA_HID_SERVICE_ID */ + BTM_SEC_SERVICE_AVDTP, /* BTA_VDP_SERVICE_ID */ + BTM_SEC_SERVICE_PBAP, /* BTA_PBAP_SERVICE_ID */ + BTM_SEC_SERVICE_HEADSET, /* BTA_HSP_HS_SERVICE_ID */ + BTM_SEC_SERVICE_HF_HANDSFREE, /* BTA_HFP_HS_SERVICE_ID */ + BTM_SEC_SERVICE_MAP, /* BTA_MAP_SERVICE_ID */ + BTM_SEC_SERVICE_MAP, /* BTA_MN_SERVICE_ID */ + BTM_SEC_SERVICE_HDP_SNK, /* BTA_HDP_SERVICE_ID */ + BTM_SEC_SERVICE_PBAP /* BTA_PCE_SERVICE_ID */ +#if BLE_INCLUDED && BTA_GATT_INCLUDED + ,BTM_SEC_SERVICE_ATT /* BTA_GATT_SERVICE_ID */ +#endif +}; + +/* bta security callback */ +const tBTM_APPL_INFO bta_security = +{ + &bta_dm_authorize_cback, + &bta_dm_pin_cback, + &bta_dm_new_link_key_cback, + &bta_dm_link_key_request_cback, + &bta_dm_authentication_complete_cback, + NULL, + &bta_dm_bond_cancel_complete_cback, +#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE) + &bta_dm_sp_cback +#else + NULL +#endif +#if BLE_INCLUDED == TRUE +#if SMP_INCLUDED == TRUE + ,&bta_dm_ble_smp_cback +#endif + ,&bta_dm_ble_id_key_cback +#endif + +}; + +#define MAX_DISC_RAW_DATA_BUF (4096) +UINT8 g_disc_raw_data_buf[MAX_DISC_RAW_DATA_BUF]; + +/******************************************************************************* +** +** Function bta_dm_app_ready_timer_cback +** +** Description allow sending EIR to controller +** +** +** Returns void +** +*******************************************************************************/ +#if ( BTM_EIR_SERVER_INCLUDED == TRUE )&&(BTA_EIR_CANNED_UUID_LIST != TRUE) +static void bta_dm_app_ready_timer_cback (TIMER_LIST_ENT *p_tle) +{ + bta_dm_set_eir (NULL); +} +#else +#define bta_dm_app_ready_timer_cback (x) +#endif + +/******************************************************************************* +** +** Function bta_dm_enable +** +** Description Initialises the BT device manager +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_enable(tBTA_DM_MSG *p_data) +{ + tBTA_SYS_HW_MSG *sys_enable_event; + tBTA_DM_ENABLE enable_event; + + + /* if already in use, return an error */ + if( bta_dm_cb.is_bta_dm_active == TRUE ) + { + APPL_TRACE_WARNING0("bta_dm_enable - device already started by another application"); + memset(&enable_event, 0, sizeof ( tBTA_DM_ENABLE )); + enable_event.status = BTA_FAILURE; + if( p_data->enable.p_sec_cback != NULL ) + p_data->enable.p_sec_cback (BTA_DM_ENABLE_EVT, (tBTA_DM_SEC *)&enable_event ); + return; + } + + + /* first, register our callback to SYS HW manager */ + bta_sys_hw_register( BTA_SYS_HW_BLUETOOTH, bta_dm_sys_hw_cback ); + + /* make sure security callback is saved - if no callback, do not erase the previous one, + it could be an error recovery mechanism */ + if( p_data->enable.p_sec_cback != NULL ) + bta_dm_cb.p_sec_cback = p_data->enable.p_sec_cback; + /* notify BTA DM is now active */ + bta_dm_cb.is_bta_dm_active = TRUE; + + /* send a message to BTA SYS */ + if ((sys_enable_event = (tBTA_SYS_HW_MSG *) GKI_getbuf(sizeof(tBTA_SYS_HW_MSG))) != NULL) + { + sys_enable_event->hdr.event = BTA_SYS_API_ENABLE_EVT; + sys_enable_event->hw_module = BTA_SYS_HW_BLUETOOTH; + + bta_sys_sendmsg(sys_enable_event); + + } + + + +} + + + +/******************************************************************************* +** +** Function bta_dm_sys_hw_cback +** +** Description callback register to SYS to get HW status updates +** +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_sys_hw_cback( tBTA_SYS_HW_EVT status ) +{ + DEV_CLASS dev_class; + tBTA_DM_SEC_CBACK *temp_cback; +#if BLE_INCLUDED == TRUE + UINT8 key_mask = 0; + BT_OCTET16 er; + tBTA_BLE_LOCAL_ID_KEYS id_key; + tBT_UUID app_uuid = {LEN_UUID_128,{0}}; +#endif + APPL_TRACE_DEBUG1(" bta_dm_sys_hw_cback with event: %i" , status ); + + if( status == BTA_SYS_HW_OFF_EVT ) + { + if( bta_dm_cb.p_sec_cback != NULL ) + bta_dm_cb.p_sec_cback(BTA_DM_DISABLE_EVT, NULL); + + /* reinitialize the control block */ + memset(&bta_dm_cb, 0, sizeof(bta_dm_cb)); + + /* unregister from SYS */ + bta_sys_hw_unregister( BTA_SYS_HW_BLUETOOTH ); + /* notify BTA DM is now unactive */ + bta_dm_cb.is_bta_dm_active = FALSE; + } + else + if( status == BTA_SYS_HW_ON_EVT ) + { + /* make sure we unregister, so that we don't motified again if another module starts */ + bta_sys_hw_unregister( BTA_SYS_HW_BLUETOOTH); + + /* save security callback */ + temp_cback = bta_dm_cb.p_sec_cback; + /* make sure the control block is properly initialized */ + memset(&bta_dm_cb, 0, sizeof(bta_dm_cb)); + /* and retrieve the callback */ + bta_dm_cb.p_sec_cback=temp_cback; + bta_dm_cb.is_bta_dm_active = TRUE; + + /* hw is ready, go on with BTA DM initialization */ + memset(&bta_dm_search_cb, 0x00, sizeof(bta_dm_search_cb)); + memset(&bta_dm_conn_srvcs, 0x00, sizeof(bta_dm_conn_srvcs)); + memset(&bta_dm_di_cb, 0, sizeof(tBTA_DM_DI_CB)); + + memcpy(dev_class, bta_dm_cfg.dev_class, sizeof(dev_class)); + BTM_SetDeviceClass (dev_class); + +#if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE) + /* load BLE local information: ID keys, ER if available */ + bta_dm_co_ble_load_local_keys(&key_mask, er, &id_key); + + if (key_mask & BTA_BLE_LOCAL_KEY_TYPE_ER) + { + BTM_BleLoadLocalKeys(BTA_BLE_LOCAL_KEY_TYPE_ER, (tBTM_BLE_LOCAL_KEYS *)&er); + } + if (key_mask & BTA_BLE_LOCAL_KEY_TYPE_ID) + { + BTM_BleLoadLocalKeys(BTA_BLE_LOCAL_KEY_TYPE_ID, (tBTM_BLE_LOCAL_KEYS *)&id_key); + } +#if ((defined BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)) + memset (&app_uuid.uu.uuid128, 0x87, LEN_UUID_128); + BTA_GATTC_AppRegister(&app_uuid, bta_dm_gattc_callback); +#endif +#endif + + BTM_SecRegister((tBTM_APPL_INFO*)&bta_security); + BTM_SetDefaultLinkSuperTout(bta_dm_cfg.link_timeout); + BTM_WritePageTimeout(bta_dm_cfg.page_timeout); + bta_dm_cb.cur_policy = bta_dm_cfg.policy_settings; + BTM_SetDefaultLinkPolicy(bta_dm_cb.cur_policy); +#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE) + BTM_RegBusyLevelNotif (bta_dm_bl_change_cback, NULL, BTM_BL_UPDATE_MASK|BTM_BL_ROLE_CHG_MASK); +#else + BTM_AclRegisterForChanges(bta_dm_acl_change_cback); +#endif + /* Earlier, we used to invoke BTM_ReadLocalAddr which was just copying the bd_addr + from the control block and invoking the callback which was sending the DM_ENABLE_EVT. + But then we have a few HCI commands being invoked above which were still in progress + when the ENABLE_EVT was sent. So modified this to fetch the local name which forces + the DM_ENABLE_EVT to be sent only after all the init steps are complete */ + BTM_ReadLocalDeviceNameFromController((tBTM_CMPL_CB *)bta_dm_local_name_cback); + + bta_sys_rm_register((tBTA_SYS_CONN_CBACK*)bta_dm_rm_cback); + + /* initialize bluetooth low power manager */ + bta_dm_init_pm(); + + bta_sys_policy_register((tBTA_SYS_CONN_CBACK*)bta_dm_policy_cback); + + + // BLUEDROID REMOVE ?? +#if 0 +#if 1 + /* Create broadcom primary DI record */ + if(WBT_ExtCreateRecord()) + { +#if ( BTM_EIR_SERVER_INCLUDED == TRUE )&&( BTA_EIR_CANNED_UUID_LIST != TRUE ) + /* while app_ready_timer is running, BTA DM doesn't send EIR to controller */ + bta_dm_cb.app_ready_timer.p_cback = (TIMER_CBACK*)&bta_dm_app_ready_timer_cback; + bta_sys_start_timer(&bta_dm_cb.app_ready_timer, 0, 100); + + bta_sys_add_uuid(UUID_SERVCLASS_PNP_INFORMATION); +#endif + bta_dm_di_cb.di_handle[bta_dm_di_cb.di_num] = 0; /* primary DI record */ + bta_dm_di_cb.di_num ++; + } +#else /* Eventually implement pin code */ + if (WBT_ExtCreateRecord()) + WBT_ExtAddPinCode(); +#endif +#endif + } + else + APPL_TRACE_DEBUG0(" --- ignored event"); + +} + + +/******************************************************************************* +** +** Function bta_dm_disable +** +** Description Disables the BT device manager +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_disable (tBTA_DM_MSG *p_data) +{ + /* Set l2cap idle timeout to 0 (so BTE immediately disconnects ACL link after last channel is closed) */ + L2CA_SetIdleTimeoutByBdAddr((UINT8 *)BT_BD_ANY, 0); + + /* disable all active subsystems */ + bta_sys_disable(BTA_SYS_HW_BLUETOOTH); + + BTM_SetDiscoverability(BTM_NON_DISCOVERABLE, 0, 0); + BTM_SetConnectability(BTM_NON_CONNECTABLE, 0, 0); + + bta_dm_disable_pm(); + + bta_dm_cb.disabling = TRUE; + + bta_dm_search_cancel(NULL); + + if(BTM_GetNumAclLinks()==0) + { + bta_dm_cb.disable_timer.p_cback = (TIMER_CBACK*)&bta_dm_disable_conn_down_timer_cback; + bta_sys_start_timer(&bta_dm_cb.disable_timer, 0, 1000); + } + else + { + bta_dm_cb.disable_timer.p_cback = (TIMER_CBACK*)&bta_dm_disable_timer_cback; + bta_sys_start_timer(&bta_dm_cb.disable_timer, 0, 5000); + } + +} + +/******************************************************************************* +** +** Function bta_dm_disable_timer_cback +** +** Description Called if the disable timer expires +** Used to close ACL connections which are still active +** +** +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_disable_timer_cback (TIMER_LIST_ENT *p_tle) +{ + + UINT8 i; + + APPL_TRACE_EVENT0(" bta_dm_disable_timer_cback "); + + if(BTM_GetNumAclLinks()) + { + for(i=0; i<bta_dm_cb.device_list.count; i++) + { + btm_remove_acl(bta_dm_cb.device_list.peer_device[i].peer_bdaddr); + + } + + } + else + { + bta_dm_cb.disabling = FALSE; + + bta_sys_remove_uuid(UUID_SERVCLASS_PNP_INFORMATION); + bta_dm_cb.p_sec_cback(BTA_DM_DISABLE_EVT, NULL); + } +} + + + + +/******************************************************************************* +** +** Function bta_dm_set_dev_name +** +** Description Sets local device name +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_set_dev_name (tBTA_DM_MSG *p_data) +{ + + BTM_SetLocalDeviceName((char*)p_data->set_name.name); +#if (BTM_EIR_SERVER_INCLUDED == TRUE) + bta_dm_set_eir ((char*)p_data->set_name.name); +#endif +} + +/******************************************************************************* +** +** Function bta_dm_set_visibility +** +** Description Sets discoverability, connectability and pairability +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_set_visibility (tBTA_DM_MSG *p_data) +{ + + + /* set modes for Discoverability and connectability if not ignore */ + if (p_data->set_visibility.disc_mode != BTA_DM_IGNORE) + BTM_SetDiscoverability((UINT8)p_data->set_visibility.disc_mode, + bta_dm_cb.inquiry_scan_window, + bta_dm_cb.inquiry_scan_interval); + + if (p_data->set_visibility.conn_mode != BTA_DM_IGNORE) + BTM_SetConnectability((UINT8)p_data->set_visibility.conn_mode, + bta_dm_cb.page_scan_window, + bta_dm_cb.page_scan_interval); + + /* Send False or True if not ignore */ + if (p_data->set_visibility.pair_mode != BTA_DM_IGNORE ) + { + + if (p_data->set_visibility.pair_mode == BTA_DM_NON_PAIRABLE) + bta_dm_cb.disable_pair_mode = TRUE; + else + bta_dm_cb.disable_pair_mode = FALSE; + + } + + /* Send False or True if not ignore */ + if (p_data->set_visibility.conn_paired_only != BTA_DM_IGNORE) + { + + if (p_data->set_visibility.conn_paired_only == BTA_DM_CONN_ALL) + bta_dm_cb.conn_paired_only = FALSE; + else + bta_dm_cb.conn_paired_only = TRUE; + + } + + /* Change mode if either mode is not ignore */ + if (p_data->set_visibility.pair_mode != BTA_DM_IGNORE || p_data->set_visibility.conn_paired_only != BTA_DM_IGNORE) + BTM_SetPairableMode((BOOLEAN)(!(bta_dm_cb.disable_pair_mode)),bta_dm_cb.conn_paired_only); + +} + + +/******************************************************************************* +** +** Function bta_dm_set_afhchannels +** +** Description This function sets the AFH first and +** last disable channel, so channels within +** that range are disabled. +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_set_afhchannels (tBTA_DM_MSG *p_data) +{ + BTM_SetAfhChannels(p_data->set_afhchannels.first,p_data->set_afhchannels.last); + +} + + +/******************************************************************************* +** +** Function bta_dm_vendor_spec_command +** +** Description Send a vendor specific command to the controller +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_vendor_spec_command (tBTA_DM_MSG *p_data) +{ + tBTM_STATUS status; + + status = BTM_VendorSpecificCommand(p_data->vendor_command.opcode,p_data->vendor_command.param_len,p_data->vendor_command.p_param_buf, p_data->vendor_command.p_cback); + +} + + +/******************************************************************************* +** +** Function bta_dm_tx_inqpower +** +** Description write inquiry tx power. +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_tx_inqpower(tBTA_DM_MSG *p_data) +{ + if (BTM_WriteInquiryTxPower (p_data->tx_inq_pwr.tx_power) == BTM_ILLEGAL_VALUE) + { + APPL_TRACE_ERROR1("Invalid Inquiry Tx Power: %d", p_data->tx_inq_pwr.tx_power); + } + return; +} + +/******************************************************************************* +** +** Function bta_dm_add_device +** +** Description This function adds a Link Key to an security database entry. +** It is normally called during host startup to restore all required information +** stored in the NVRAM. +**** +*******************************************************************************/ +void bta_dm_add_device (tBTA_DM_MSG *p_data) +{ + tBTA_DM_API_ADD_DEVICE *p_dev = &p_data->add_dev; + UINT8 *p_dc = NULL; + UINT8 *p_lc = NULL; + UINT32 trusted_services_mask[BTM_SEC_SERVICE_ARRAY_SIZE]; + UINT8 index = 0; + UINT8 btm_mask_index = 0; + + memset (trusted_services_mask, 0, sizeof(trusted_services_mask)); + + /* If not all zeros, the device class has been specified */ + if (p_dev->dc_known) + p_dc = (UINT8 *)p_dev->dc; + + if (p_dev->link_key_known) + p_lc = (UINT8 *)p_dev->link_key; + + if (p_dev->is_trusted) + { + /* covert BTA service mask to BTM mask */ + while (p_dev->tm && (index < BTA_MAX_SERVICE_ID)) + { + if (p_dev->tm & (UINT32)(1<<index)) + { + + btm_mask_index = bta_service_id_to_btm_srv_id_lkup_tbl[index] / BTM_SEC_ARRAY_BITS; + trusted_services_mask[btm_mask_index] |= (UINT32)(1 << (bta_service_id_to_btm_srv_id_lkup_tbl[index] - (UINT32)(btm_mask_index * 32))); + + p_dev->tm &= (UINT32)(~(1<<index)); + + } + index++; + } + } + + if (!BTM_SecAddDevice (p_dev->bd_addr, p_dc, p_dev->bd_name, p_dev->features, + trusted_services_mask, p_lc, p_dev->key_type, p_dev->io_cap)) + { + APPL_TRACE_ERROR2 ("BTA_DM: Error adding device %08x%04x", + (p_dev->bd_addr[0]<<24)+(p_dev->bd_addr[1]<<16)+(p_dev->bd_addr[2]<<8)+p_dev->bd_addr[3], + (p_dev->bd_addr[4]<<8)+p_dev->bd_addr[5]); + } +} + +/******************************************************************************* +** +** Function bta_dm_bond +** +** Description Bonds with peer device +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_bond (tBTA_DM_MSG *p_data) +{ + tBTM_STATUS status; + tBTA_DM_SEC sec_event; + char *p_name; + + status = BTM_SecBond ( p_data->bond.bd_addr, 0, NULL, 0 ); + + if (bta_dm_cb.p_sec_cback && (status != BTM_CMD_STARTED)) + { + + p_name = BTM_SecReadDevName(p_data->bond.bd_addr); + if (!p_name) + p_name = ""; + + memset(&sec_event, 0, sizeof(tBTA_DM_SEC)); + bdcpy(sec_event.auth_cmpl.bd_addr, p_data->bond.bd_addr); + memcpy(sec_event.auth_cmpl.bd_name, p_name, (BD_NAME_LEN-1)); + sec_event.auth_cmpl.bd_name[BD_NAME_LEN-1] = 0; + +/* taken care of by memset [above] + sec_event.auth_cmpl.key_present = FALSE; + sec_event.auth_cmpl.success = FALSE; +*/ + sec_event.auth_cmpl.fail_reason = HCI_ERR_ILLEGAL_COMMAND; + if (status == BTM_SUCCESS) + sec_event.auth_cmpl.success = TRUE; + + bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event); + } + +} + +/******************************************************************************* +** +** Function bta_dm_bond_cancel +** +** Description Cancels bonding with a peer device +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_bond_cancel (tBTA_DM_MSG *p_data) +{ + tBTM_STATUS status; + tBTA_DM_SEC sec_event; + + APPL_TRACE_EVENT0(" bta_dm_bond_cancel "); + status = BTM_SecBondCancel ( p_data->bond_cancel.bd_addr ); + + if (bta_dm_cb.p_sec_cback && (status != BTM_CMD_STARTED && status != BTM_SUCCESS)) + { + sec_event.bond_cancel_cmpl.result = BTA_FAILURE; + + bta_dm_cb.p_sec_cback(BTA_DM_BOND_CANCEL_CMPL_EVT, &sec_event); + } + +} + +/******************************************************************************* +** +** Function bta_dm_pin_reply +** +** Description Send the pin_reply to a request from BTM +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_pin_reply (tBTA_DM_MSG *p_data) +{ + UINT32 trusted_mask[BTM_SEC_SERVICE_ARRAY_SIZE]; + UINT32 * current_trusted_mask; + + current_trusted_mask = BTM_ReadTrustedMask(p_data->pin_reply.bd_addr); + + if(current_trusted_mask) + { + memcpy(trusted_mask, current_trusted_mask, sizeof(trusted_mask)); + } + else + { + memset(trusted_mask, 0, sizeof(trusted_mask)); + } + + if(p_data->pin_reply.accept) + { + + BTM_PINCodeReply(p_data->pin_reply.bd_addr, BTM_SUCCESS, p_data->pin_reply.pin_len, p_data->pin_reply.p_pin, trusted_mask ); + } + else + { + BTM_PINCodeReply(p_data->pin_reply.bd_addr, BTM_NOT_AUTHORIZED, 0, NULL, trusted_mask ); + } + +} + +/******************************************************************************* +** +** Function bta_dm_link_policy +** +** Description remove/set link policy mask. +** wake the link, is sniff/park is removed +** +** Returns void +** +*******************************************************************************/ +void bta_dm_link_policy (tBTA_DM_MSG *p_data) +{ + tBTA_DM_PEER_DEVICE *p_dev; + + p_dev = bta_dm_find_peer_device(p_data->link_policy.bd_addr); + if(!p_dev) + return; + + APPL_TRACE_DEBUG2(" bta_dm_link_policy set:%d, policy:0x%x", + p_data->link_policy.set, p_data->link_policy.policy_mask); + if(p_data->link_policy.set) + { + /* restore the default link policy */ + p_dev->link_policy |= p_data->link_policy.policy_mask; + BTM_SetLinkPolicy(p_dev->peer_bdaddr, &(p_dev->link_policy)); + } + else + { + /* clear the policy from the default link policy */ + p_dev->link_policy &= (~p_data->link_policy.policy_mask); + BTM_SetLinkPolicy(p_dev->peer_bdaddr, &(p_dev->link_policy)); + + if(p_data->link_policy.policy_mask & (HCI_ENABLE_SNIFF_MODE | HCI_ENABLE_PARK_MODE)) + { + /* if clearing sniff/park, wake the link */ + bta_dm_pm_active(p_dev->peer_bdaddr); + } + } +} + +/******************************************************************************* +** +** Function bta_dm_policy_cback +** +** Description process the link policy changes +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_policy_cback(tBTA_SYS_CONN_STATUS status, UINT8 id, UINT8 app_id, BD_ADDR peer_addr) +{ + tBTA_DM_PEER_DEVICE *p_dev = NULL; + UINT16 policy = app_id; + UINT32 mask = (UINT32)(1 << id); + + if(peer_addr) + p_dev = bta_dm_find_peer_device(peer_addr); + + APPL_TRACE_DEBUG2(" bta_dm_policy_cback cmd:%d, policy:0x%x", + status, policy); + switch(status) + { + case BTA_SYS_PLCY_SET: + if(!p_dev) + return; + /* restore the default link policy */ + p_dev->link_policy |= policy; + BTM_SetLinkPolicy(p_dev->peer_bdaddr, &(p_dev->link_policy)); + break; + + case BTA_SYS_PLCY_CLR: + if(!p_dev) + return; + /* clear the policy from the default link policy */ + p_dev->link_policy &= (~policy); + BTM_SetLinkPolicy(p_dev->peer_bdaddr, &(p_dev->link_policy)); + + if(policy & (HCI_ENABLE_SNIFF_MODE | HCI_ENABLE_PARK_MODE)) + { + /* if clearing sniff/park, wake the link */ + bta_dm_pm_active(p_dev->peer_bdaddr); + } + break; + + case BTA_SYS_PLCY_DEF_SET: + /* want to restore/set the role switch policy */ + bta_dm_cb.role_policy_mask &= ~mask; + if(0 == bta_dm_cb.role_policy_mask) + { + /* if nobody wants to insist on the role */ + bta_dm_cb.cur_policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH; + BTM_SetDefaultLinkPolicy(bta_dm_cb.cur_policy); + } + break; + + case BTA_SYS_PLCY_DEF_CLR: + /* want to remove the role switch policy */ + bta_dm_cb.role_policy_mask |= mask; + bta_dm_cb.cur_policy &= ~HCI_ENABLE_MASTER_SLAVE_SWITCH; + BTM_SetDefaultLinkPolicy(bta_dm_cb.cur_policy); + break; + } +} + + +/******************************************************************************* +** +** Function bta_dm_auth_reply +** +** Description Send the authorization reply to a request from BTM +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_auth_reply (tBTA_DM_MSG *p_data) +{ + + UINT32 trusted_mask[BTM_SEC_SERVICE_ARRAY_SIZE]; + UINT8 btm_mask_index = 0; + UINT32 * current_trusted_mask; + + current_trusted_mask = BTM_ReadTrustedMask(p_data->auth_reply.bd_addr); + + if(current_trusted_mask) + { + memcpy(trusted_mask, current_trusted_mask, sizeof(trusted_mask)); + } + else + { + memset(trusted_mask, 0, sizeof(trusted_mask)); + } + + if(p_data->auth_reply.response != BTA_DM_NOT_AUTH) + { + if(p_data->auth_reply.response == BTA_DM_AUTH_PERM) + { + if(p_data->auth_reply.service < BTA_MAX_SERVICE_ID) + { + /* convert BTA service id to BTM mask */ + btm_mask_index = bta_service_id_to_btm_srv_id_lkup_tbl[p_data->auth_reply.service] / 32; + trusted_mask[btm_mask_index] |= (UINT32)(1 << (bta_service_id_to_btm_srv_id_lkup_tbl[p_data->auth_reply.service] - (UINT32)(btm_mask_index * 32))); + + } + } + BTM_DeviceAuthorized (p_data->auth_reply.bd_addr, BTM_SUCCESS,trusted_mask); + } + else + { + BTM_DeviceAuthorized (p_data->auth_reply.bd_addr, BTM_NOT_AUTHORIZED,trusted_mask); + } + +} + +/******************************************************************************* +** +** Function bta_dm_confirm +** +** Description Send the user confirm request reply in response to a +** request from BTM +** +** Returns void +** +*******************************************************************************/ +void bta_dm_confirm(tBTA_DM_MSG *p_data) +{ + tBTM_STATUS res = BTM_NOT_AUTHORIZED; + + if(p_data->confirm.accept == TRUE) + res = BTM_SUCCESS; + BTM_ConfirmReqReply(res, p_data->confirm.bd_addr); +} + +/******************************************************************************* +** +** Function bta_dm_passkey_cancel +** +** Description Send the passkey cancel from SP initiator by sending a negative +** passkey request replyreply. +** Returns void +** +*******************************************************************************/ +#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE) +void bta_dm_passkey_cancel(tBTA_DM_MSG *p_data) +{ + BTM_PasskeyReqReply(BTM_NOT_AUTHORIZED, p_data->passkey_cancel.bd_addr, 0); +} +#endif + +/******************************************************************************* +** +** Function bta_dm_loc_oob +** +** Description Retrieve the OOB data from the local LM +** +** Returns void +** +*******************************************************************************/ +#if (BTM_OOB_INCLUDED == TRUE) +void bta_dm_loc_oob(tBTA_DM_MSG *p_data) +{ + BTM_ReadLocalOobData(); +} + +/******************************************************************************* +** +** Function bta_dm_ci_io_req_act +** +** Description respond to the IO capabilities request from BTM +** +** Returns void +** +*******************************************************************************/ +void bta_dm_ci_io_req_act(tBTA_DM_MSG *p_data) +{ + tBTM_AUTH_REQ auth_req = BTM_AUTH_AP_NO; + if(p_data->ci_io_req.auth_req) + auth_req = BTM_AUTH_AP_YES; + BTM_IoCapRsp(p_data->ci_io_req.bd_addr, p_data->ci_io_req.io_cap, + p_data->ci_io_req.oob_data, auth_req); +} + +/******************************************************************************* +** +** Function bta_dm_ci_rmt_oob_act +** +** Description respond to the OOB data request for the remote device from BTM +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_ci_rmt_oob_act(tBTA_DM_MSG *p_data) +{ + tBTM_STATUS res = BTM_NOT_AUTHORIZED; + + if(p_data->ci_rmt_oob.accept == TRUE) + res = BTM_SUCCESS; + BTM_RemoteOobDataReply(res, p_data->ci_rmt_oob.bd_addr, + p_data->ci_rmt_oob.c, p_data->ci_rmt_oob.r ); +} +#endif /* BTM_OOB_INCLUDED */ + +/******************************************************************************* +** +** Function bta_dm_search_start +** +** Description Starts an inquiry +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_search_start (tBTA_DM_MSG *p_data) +{ + tBTM_INQUIRY_CMPL result; + +#if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE + UINT16 len = (UINT16)(sizeof(tBT_UUID) * p_data->search.num_uuid); +#endif + + APPL_TRACE_DEBUG1("bta_dm_search_start avoid_scatter=%d", bta_dm_cfg.avoid_scatter); + if (bta_dm_cfg.avoid_scatter && + (p_data->search.rs_res == BTA_DM_RS_NONE) && bta_dm_check_av(BTA_DM_API_SEARCH_EVT)) + { + memcpy(&bta_dm_cb.search_msg, &p_data->search, sizeof(tBTA_DM_API_SEARCH)); + return; + } + + BTM_ClearInqDb(NULL); + /* save search params */ + bta_dm_search_cb.p_search_cback = p_data->search.p_cback; + bta_dm_search_cb.services = p_data->search.services; + +#if (BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE) + utl_freebuf((void **)&bta_dm_search_cb.p_srvc_uuid); + + if ((bta_dm_search_cb.num_uuid = p_data->search.num_uuid) != 0 && + p_data->search.p_uuid != NULL) + { + if ((bta_dm_search_cb.p_srvc_uuid = (tBT_UUID *)GKI_getbuf(len)) == NULL) + { + APPL_TRACE_ERROR0("bta_dm_search_start no resources"); + + result.status = BTA_FAILURE; + result.num_resp = 0; + bta_dm_inq_cmpl_cb ((void *)&result); + return; + } +// bta_dm_search_cb.p_srvc_uuid = (tBT_UUID *)GKI_getbuf(len); + memcpy(bta_dm_search_cb.p_srvc_uuid, p_data->search.p_uuid, len); + } + + if (p_data->search.inq_params.mode & BTM_LIMITED_INQUIRY) + p_data->search.inq_params.mode |= BTM_BLE_LIMITED_INQUIRY; + else + p_data->search.inq_params.mode |= BTM_BLE_GENERAL_INQUIRY; +#endif + result.status = BTM_StartInquiry( (tBTM_INQ_PARMS*)&p_data->search.inq_params, + bta_dm_inq_results_cb, + (tBTM_CMPL_CB*) bta_dm_inq_cmpl_cb); + + APPL_TRACE_EVENT1("bta_dm_search_start status=%d", result.status); + if (result.status != BTM_CMD_STARTED) + { + result.num_resp = 0; + bta_dm_inq_cmpl_cb ((void *)&result); + } + +} + +/******************************************************************************* +** +** Function bta_dm_search_cancel +** +** Description Cancels an ongoing search for devices +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_search_cancel (tBTA_DM_MSG *p_data) +{ + + tBTA_DM_MSG * p_msg; + + if(BTM_IsInquiryActive()) + { + BTM_CancelInquiry(); + bta_dm_search_cancel_notify(NULL); + + if ((p_msg = (tBTA_DM_MSG *) GKI_getbuf(sizeof(tBTA_DM_MSG))) != NULL) + { + p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT; + p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT; + bta_sys_sendmsg(p_msg); + + } + } + /* If no Service Search going on then issue cancel remote name in case it is active */ + else if (!bta_dm_search_cb.name_discover_done) + { + BTM_CancelRemoteDeviceName(); + } +#if ((BLE_INCLUDED == TRUE) && (defined BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)) + if (bta_dm_search_cb.gatt_disc_active) + { + bta_dm_cancel_gatt_discovery(bta_dm_search_cb.peer_bdaddr); + } +#endif +} + +/******************************************************************************* +** +** Function bta_dm_discover +** +** Description Discovers services on a remote device +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_discover (tBTA_DM_MSG *p_data) +{ +#if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE + UINT16 len = (UINT16)(sizeof(tBT_UUID) * p_data->discover.num_uuid); +#endif + APPL_TRACE_EVENT2("bta_dm_discover services_to_search=0x%04X, sdp_search=%d", + p_data->discover.services, p_data->discover.sdp_search); + + /* save the search condition */ + bta_dm_search_cb.services = p_data->discover.services; + +#if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE + utl_freebuf((void **)&bta_dm_search_cb.p_srvc_uuid); + if ((bta_dm_search_cb.num_uuid = p_data->discover.num_uuid) != 0 && + p_data->discover.p_uuid != NULL) + { + if ((bta_dm_search_cb.p_srvc_uuid = (tBT_UUID *)GKI_getbuf(len)) == NULL) + { + p_data->discover.p_cback(BTA_DM_DISC_CMPL_EVT, NULL); + return; + } + memcpy(bta_dm_search_cb.p_srvc_uuid, p_data->discover.p_uuid, len); + } + bta_dm_search_cb.uuid_to_search = bta_dm_search_cb.num_uuid; +#endif + + bta_dm_search_cb.p_search_cback = p_data->discover.p_cback; + bta_dm_search_cb.sdp_search = p_data->discover.sdp_search; + bta_dm_search_cb.services_to_search = bta_dm_search_cb.services; + bta_dm_search_cb.service_index = 0; + bta_dm_search_cb.services_found = 0; + bta_dm_search_cb.peer_name[0] = 0; + bta_dm_search_cb.sdp_search = p_data->discover.sdp_search; + bta_dm_search_cb.p_btm_inq_info = BTM_InqDbRead (p_data->discover.bd_addr); + + bta_dm_search_cb.name_discover_done = FALSE; + memcpy(&bta_dm_search_cb.uuid, &p_data->discover.uuid, sizeof(tSDP_UUID)); + bta_dm_discover_device(p_data->discover.bd_addr); +} + +/******************************************************************************* +** +** Function bta_dm_di_disc_cmpl +** +** Description Sends event to application when DI discovery complete +** +** Returns void +** +*******************************************************************************/ +void bta_dm_di_disc_cmpl(tBTA_DM_MSG *p_data) +{ + tBTA_DM_DI_DISC_CMPL di_disc; + + memset(&di_disc, 0, sizeof(tBTA_DM_DI_DISC_CMPL)); + bdcpy(di_disc.bd_addr, bta_dm_search_cb.peer_bdaddr); + + if((p_data->hdr.offset == SDP_SUCCESS) + || (p_data->hdr.offset == SDP_DB_FULL)) + { + di_disc.num_record = SDP_GetNumDiRecords(bta_dm_di_cb.p_di_db); + } + else + di_disc.result = BTA_FAILURE; + + bta_dm_di_cb.p_di_db = NULL; + bta_dm_search_cb.p_search_cback(BTA_DM_DI_DISC_CMPL_EVT, (tBTA_DM_SEARCH *) &di_disc); +} + +/******************************************************************************* +** +** Function bta_dm_di_disc_callback +** +** Description This function queries a remote device for DI information. +** +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_di_disc_callback(UINT16 result) +{ + tBTA_DM_MSG * p_msg; + + if ((p_msg = (tBTA_DM_MSG *) GKI_getbuf(sizeof(tBTA_DM_MSG))) != NULL) + { + p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT; + p_msg->hdr.layer_specific = BTA_DM_API_DI_DISCOVER_EVT; + p_msg->hdr.offset = result; + bta_sys_sendmsg(p_msg); + } +} + +/******************************************************************************* +** +** Function bta_dm_di_disc +** +** Description This function queries a remote device for DI information. +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_di_disc (tBTA_DM_MSG *p_data) +{ + UINT16 result = BTA_FAILURE; + tBTA_DM_MSG *p_msg; + + bta_dm_search_cb.p_search_cback = p_data->di_disc.p_cback; + bdcpy(bta_dm_search_cb.peer_bdaddr, p_data->di_disc.bd_addr); + bta_dm_di_cb.p_di_db = p_data->di_disc.p_sdp_db; + + if((bta_dm_search_cb.p_sdp_db = (tSDP_DISCOVERY_DB *)GKI_getbuf(BTA_DM_SDP_DB_SIZE)) != NULL) + { + if ( SDP_DiDiscover(bta_dm_search_cb.peer_bdaddr, p_data->di_disc.p_sdp_db, + p_data->di_disc.len, bta_dm_di_disc_callback) == SDP_SUCCESS) + { + result = BTA_SUCCESS; + } + } + else + { + APPL_TRACE_ERROR0("No buffer to start DI discovery"); + } + + if ( result == BTA_FAILURE && + (p_msg = (tBTA_DM_MSG *) GKI_getbuf(sizeof(tBTA_DM_MSG))) != NULL) + { + p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT; + p_msg->hdr.layer_specific = BTA_DM_API_DI_DISCOVER_EVT; + p_data->hdr.offset = result; + bta_sys_sendmsg(p_msg); + } +} + +/******************************************************************************* +** +** Function bta_dm_read_remote_device_name +** +** Description Initiate to get remote device name +** +** Returns TRUE if started to get remote name +** +*******************************************************************************/ +static BOOLEAN bta_dm_read_remote_device_name (BD_ADDR bd_addr) +{ + tBTM_STATUS btm_status; + + APPL_TRACE_DEBUG0("bta_dm_read_remote_device_name"); + + bdcpy(bta_dm_search_cb.peer_bdaddr, bd_addr); + bta_dm_search_cb.peer_name[0] = 0; + + btm_status = BTM_ReadRemoteDeviceName (bta_dm_search_cb.peer_bdaddr, + (tBTM_CMPL_CB *) bta_dm_remname_cback); + + if ( btm_status == BTM_CMD_STARTED ) + { + APPL_TRACE_DEBUG0("bta_dm_read_remote_device_name: BTM_ReadRemoteDeviceName is started"); + + return (TRUE); + } + else if ( btm_status == BTM_BUSY ) + { + APPL_TRACE_DEBUG0("bta_dm_read_remote_device_name: BTM_ReadRemoteDeviceName is busy"); + + /* Remote name discovery is on going now so BTM cannot notify through "bta_dm_remname_cback" */ + /* adding callback to get notified that current reading remore name done */ + BTM_SecAddRmtNameNotifyCallback(&bta_dm_service_search_remname_cback); + + return (TRUE); + } + else + { + APPL_TRACE_WARNING1("bta_dm_read_remote_device_name: BTM_ReadRemoteDeviceName returns 0x%02X", btm_status); + + return (FALSE); + } +} + +/******************************************************************************* +** +** Function bta_dm_inq_cmpl +** +** Description Process the inquiry complete event from BTM +** +** Returns void +** +*******************************************************************************/ +void bta_dm_inq_cmpl (tBTA_DM_MSG *p_data) +{ + tBTA_DM_MSG * p_msg; + tBTA_DM_SEARCH data; + + APPL_TRACE_DEBUG0("bta_dm_inq_cmpl"); + + data.inq_cmpl.num_resps = p_data->inq_cmpl.num; + bta_dm_search_cb.p_search_cback(BTA_DM_INQ_CMPL_EVT, &data); + + if((bta_dm_search_cb.p_btm_inq_info = BTM_InqDbFirst()) != NULL) + { + /* start name and service discovery from the first device on inquiry result */ + bta_dm_search_cb.name_discover_done = FALSE; + bta_dm_search_cb.peer_name[0] = 0; + bta_dm_discover_device(bta_dm_search_cb.p_btm_inq_info->results.remote_bd_addr); + } + else + { + /* no devices, search complete */ + bta_dm_search_cb.services = 0; + + if ((p_msg = (tBTA_DM_MSG *) GKI_getbuf(sizeof(tBTA_DM_MSG))) != NULL) + { + p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT; + p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT; + bta_sys_sendmsg(p_msg); + } + } + } + +/******************************************************************************* +** +** Function bta_dm_rmt_name +** +** Description Process the remote name result from BTM +** +** Returns void +** +*******************************************************************************/ +void bta_dm_rmt_name (tBTA_DM_MSG *p_data) +{ + APPL_TRACE_DEBUG0("bta_dm_rmt_name"); + + if( p_data->rem_name.result.disc_res.bd_name[0] && bta_dm_search_cb.p_btm_inq_info) + { + bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name = TRUE; + } + + bta_dm_discover_device(bta_dm_search_cb.peer_bdaddr); +} + +/******************************************************************************* +** +** Function bta_dm_disc_rmt_name +** +** Description Process the remote name result from BTM when application +** wants to find the name for a bdaddr +** +** Returns void +** +*******************************************************************************/ +void bta_dm_disc_rmt_name (tBTA_DM_MSG *p_data) +{ + tBTM_INQ_INFO *p_btm_inq_info; + + APPL_TRACE_DEBUG0("bta_dm_disc_rmt_name"); + + p_btm_inq_info = BTM_InqDbRead (p_data->rem_name.result.disc_res.bd_addr); + if( p_btm_inq_info ) + { + if( p_data->rem_name.result.disc_res.bd_name[0] ) + { + p_btm_inq_info->appl_knows_rem_name = TRUE; + } + } + + bta_dm_discover_device(p_data->rem_name.result.disc_res.bd_addr); +} + +/******************************************************************************* +** +** Function bta_dm_sdp_result +** +** Description Process the discovery result from sdp +** +** Returns void +** +*******************************************************************************/ +void bta_dm_sdp_result (tBTA_DM_MSG *p_data) +{ + + tSDP_DISC_REC *p_sdp_rec = NULL; + tBTA_DM_MSG *p_msg; + BOOLEAN service_found = FALSE; + BOOLEAN scn_found = FALSE; + UINT16 service = 0xFFFF; + tSDP_PROTOCOL_ELEM pe; + +#if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE + tBT_UUID *p_uuid = bta_dm_search_cb.p_srvc_uuid; + tBTA_DM_SEARCH result; + tBT_UUID service_uuid; +#endif + + if((p_data->sdp_event.sdp_result == SDP_SUCCESS) + || (p_data->sdp_event.sdp_result == SDP_NO_RECS_MATCH) + || (p_data->sdp_event.sdp_result == SDP_DB_FULL)) + { + APPL_TRACE_DEBUG1("sdp_result::0x%x", p_data->sdp_event.sdp_result); + do + { + + service_found = FALSE; + p_sdp_rec = NULL; + if( bta_dm_search_cb.service_index == (BTA_USER_SERVICE_ID+1) ) + { + p_sdp_rec = SDP_FindServiceUUIDInDb(bta_dm_search_cb.p_sdp_db, &bta_dm_search_cb.uuid, p_sdp_rec); + + if (p_sdp_rec && SDP_FindProtocolListElemInRec(p_sdp_rec, UUID_PROTOCOL_RFCOMM, &pe)) + { + bta_dm_search_cb.peer_scn = (UINT8) pe.params[0]; + scn_found = TRUE; + } + } + else + { + service = bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index-1]; + p_sdp_rec = SDP_FindServiceInDb(bta_dm_search_cb.p_sdp_db, service, p_sdp_rec); + } + +#if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE + /* finished with BR/EDR services, now we check the result for GATT based service UUID */ + if (bta_dm_search_cb.service_index == BTA_MAX_SERVICE_ID) + { + if (bta_dm_search_cb.uuid_to_search != 0 && p_uuid != NULL) + { + p_uuid += (bta_dm_search_cb.num_uuid - bta_dm_search_cb.uuid_to_search); + /* only support 16 bits UUID for now */ + service = p_uuid->uu.uuid16; + + } + /* all GATT based services */ + do + { + /* find a service record, report it */ + p_sdp_rec = SDP_FindServiceInDb(bta_dm_search_cb.p_sdp_db, + 0, p_sdp_rec); + if (p_sdp_rec) + { + if (SDP_FindServiceUUIDInRec(p_sdp_rec, &service_uuid)) + { + /* send result back to app now, one by one */ + bdcpy (result.disc_ble_res.bd_addr, bta_dm_search_cb.peer_bdaddr); + BCM_STRNCPY_S((char*)result.disc_ble_res.bd_name, sizeof(BD_NAME), bta_dm_get_remname(), (BD_NAME_LEN-1)); + result.disc_ble_res.service.len = service_uuid.len; + result.disc_ble_res.service.uu.uuid16 = service_uuid.uu.uuid16; + + bta_dm_search_cb.p_search_cback(BTA_DM_DISC_BLE_RES_EVT, &result); + } + } + + if (bta_dm_search_cb.uuid_to_search > 0) + break; + + } while (p_sdp_rec); + } + else +#endif + { + /* SDP_DB_FULL means some records with the + required attributes were received */ + if((p_data->sdp_event.sdp_result == SDP_DB_FULL) || + (p_sdp_rec != NULL)) + { + /* If Plug and Play service record, check to see if Broadcom stack */ + if (service == UUID_SERVCLASS_PNP_INFORMATION) + { + if (p_sdp_rec) + { + if (SDP_FindAttributeInRec (p_sdp_rec, ATTR_ID_EXT_BRCM_VERSION)) + { + service_found = TRUE; + } + } + } + else + { + service_found = TRUE; + } + + if (service_found) + { + bta_dm_search_cb.services_found |= + (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(bta_dm_search_cb.service_index-1)); + } + } + } + + if(bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK && + bta_dm_search_cb.services_to_search == 0) + { +#if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE + if ( bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID && + bta_dm_search_cb.uuid_to_search > 0) + bta_dm_search_cb.uuid_to_search --; + + if (bta_dm_search_cb.uuid_to_search == 0 || + bta_dm_search_cb.service_index != BTA_BLE_SERVICE_ID) +#endif + bta_dm_search_cb.service_index++; + } + else /* regular one service per search or PNP search */ + break; + + } + while(bta_dm_search_cb.service_index <= BTA_MAX_SERVICE_ID); + +// GKI_freebuf(bta_dm_search_cb.p_sdp_db); +// bta_dm_search_cb.p_sdp_db = NULL; + APPL_TRACE_DEBUG1("bta_dm_sdp_result services_found = %04x", bta_dm_search_cb.services_found); + + /* if there are more services to search for */ + if(bta_dm_search_cb.services_to_search) + { + /* Free up the p_sdp_db before checking the next one */ + bta_dm_free_sdp_db(NULL); + bta_dm_find_services(bta_dm_search_cb.peer_bdaddr); + } + else + { + /* callbacks */ + /* start next bd_addr if necessary */ + + BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback); + + + if ((p_msg = (tBTA_DM_MSG *) GKI_getbuf(sizeof(tBTA_DM_MSG))) != NULL) + { + p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT; + p_msg->disc_result.result.disc_res.result = BTA_SUCCESS; + p_msg->disc_result.result.disc_res.p_raw_data = NULL; + p_msg->disc_result.result.disc_res.raw_data_size = 0; + + //copy the raw_data to the discovery result structure + // + APPL_TRACE_DEBUG2("bta_dm_sdp_result (raw_data used = 0x%x raw_data_ptr = 0x%x)\r\n",bta_dm_search_cb.p_sdp_db->raw_used, bta_dm_search_cb.p_sdp_db->raw_data); + + if ( bta_dm_search_cb.p_sdp_db != NULL && bta_dm_search_cb.p_sdp_db->raw_used != 0 && + bta_dm_search_cb.p_sdp_db->raw_data != NULL) { + + p_msg->disc_result.result.disc_res.p_raw_data = GKI_getbuf(bta_dm_search_cb.p_sdp_db->raw_used); + if ( NULL != p_msg->disc_result.result.disc_res.p_raw_data ) { + memcpy( p_msg->disc_result.result.disc_res.p_raw_data, + bta_dm_search_cb.p_sdp_db->raw_data, + bta_dm_search_cb.p_sdp_db->raw_used ); + + p_msg->disc_result.result.disc_res.raw_data_size = + bta_dm_search_cb.p_sdp_db->raw_used; + + } else { + APPL_TRACE_DEBUG1("bta_dm_sdp_result GKI Alloc failed to allocate %d bytes !!\r\n",bta_dm_search_cb.p_sdp_db->raw_used); + printf("bta_dm_sdp_result GKI Alloc failed to allocate %d bytes !!\r\n",bta_dm_search_cb.p_sdp_db->raw_used); + } + + bta_dm_search_cb.p_sdp_db->raw_data = NULL; //no need to free this - it is a global assigned. + bta_dm_search_cb.p_sdp_db->raw_used = 0; + bta_dm_search_cb.p_sdp_db->raw_size = 0; + } + else { + APPL_TRACE_DEBUG0("bta_dm_sdp_result raw data size is 0 or raw_data is null!!\r\n"); + } + /* Done with p_sdp_db. Free it */ + bta_dm_free_sdp_db(NULL); + p_msg->disc_result.result.disc_res.services = bta_dm_search_cb.services_found; + + //Piggy back the SCN over result field + if( scn_found ) + { + p_msg->disc_result.result.disc_res.result = (3 + bta_dm_search_cb.peer_scn); + p_msg->disc_result.result.disc_res.services |= BTA_USER_SERVICE_MASK; + + APPL_TRACE_EVENT1(" Piggy back the SCN over result field SCN=%d", bta_dm_search_cb.peer_scn); + + } + bdcpy (p_msg->disc_result.result.disc_res.bd_addr, bta_dm_search_cb.peer_bdaddr); + BCM_STRNCPY_S((char*)p_msg->disc_result.result.disc_res.bd_name, sizeof(BD_NAME), + bta_dm_get_remname(), (BD_NAME_LEN-1)); + + /* make sure the string is null terminated */ + p_msg->disc_result.result.disc_res.bd_name[BD_NAME_LEN-1] = 0; + + bta_sys_sendmsg(p_msg); + } + + } + + } + else + { + /* conn failed. No need for timer */ + if(p_data->sdp_event.sdp_result == SDP_CONN_FAILED || p_data->sdp_event.sdp_result == SDP_CONN_REJECTED + || p_data->sdp_event.sdp_result == SDP_SECURITY_ERR) + bta_dm_search_cb.wait_disc = FALSE; + + /* not able to connect go to next device */ + GKI_freebuf(bta_dm_search_cb.p_sdp_db); + bta_dm_search_cb.p_sdp_db = NULL; + + BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback); + + if ((p_msg = (tBTA_DM_MSG *) GKI_getbuf(sizeof(tBTA_DM_MSG))) != NULL) + { + p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT; + p_msg->disc_result.result.disc_res.result = BTA_FAILURE; + p_msg->disc_result.result.disc_res.services = bta_dm_search_cb.services_found; + bdcpy (p_msg->disc_result.result.disc_res.bd_addr, bta_dm_search_cb.peer_bdaddr); + BCM_STRNCPY_S((char*)p_msg->disc_result.result.disc_res.bd_name, sizeof(BD_NAME), + bta_dm_get_remname(), (BD_NAME_LEN-1)); + + /* make sure the string is null terminated */ + p_msg->disc_result.result.disc_res.bd_name[BD_NAME_LEN-1] = 0; + + bta_sys_sendmsg(p_msg); + } + } +} + +/******************************************************************************* +** +** Function bta_dm_search_cmpl +** +** Description Sends event to application +** +** Returns void +** +*******************************************************************************/ +void bta_dm_search_cmpl (tBTA_DM_MSG *p_data) +{ + APPL_TRACE_DEBUG0("bta_dm_search_cmpl"); + +#if (BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE) + utl_freebuf((void **)&bta_dm_search_cb.p_srvc_uuid); +#endif + + if (p_data->hdr.layer_specific == BTA_DM_API_DI_DISCOVER_EVT) + bta_dm_di_disc_cmpl(p_data); + else + bta_dm_search_cb.p_search_cback(BTA_DM_DISC_CMPL_EVT, NULL); +} + +/******************************************************************************* +** +** Function bta_dm_disc_result +** +** Description Service discovery result when discovering services on a device +** +** Returns void +** +*******************************************************************************/ +void bta_dm_disc_result (tBTA_DM_MSG *p_data) +{ + tBTA_DM_MSG * p_msg; + + APPL_TRACE_DEBUG0("bta_dm_disc_result"); + +#if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE + /* if any BR/EDR service discovery has been done, report the event */ + if ((bta_dm_search_cb.services & ((BTA_ALL_SERVICE_MASK | BTA_USER_SERVICE_MASK ) & ~BTA_BLE_SERVICE_MASK))) +#endif + bta_dm_search_cb.p_search_cback(BTA_DM_DISC_RES_EVT, &p_data->disc_result.result); + + /* send a message to change state */ + if ((p_msg = (tBTA_DM_MSG *) GKI_getbuf(sizeof(tBTA_DM_MSG))) != NULL) + { + p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT; + p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT; + bta_sys_sendmsg(p_msg); + } +} + +/******************************************************************************* +** +** Function bta_dm_search_result +** +** Description Service discovery result while searching for devices +** +** Returns void +** +*******************************************************************************/ +void bta_dm_search_result (tBTA_DM_MSG *p_data) +{ + APPL_TRACE_DEBUG2("bta_dm_search_result searching:0x%04x, result:0x%04x", + bta_dm_search_cb.services, + p_data->disc_result.result.disc_res.services); + + /* call back if application wants name discovery or found services that application is searching */ + if (( !bta_dm_search_cb.services ) + ||(( bta_dm_search_cb.services ) && ( p_data->disc_result.result.disc_res.services ))) + { + bta_dm_search_cb.p_search_cback(BTA_DM_DISC_RES_EVT, &p_data->disc_result.result); + } + + /* if searching did not initiate to create link */ + if(!bta_dm_search_cb.wait_disc ) + { +#if ( BTM_EIR_CLIENT_INCLUDED == TRUE ) + /* if service searching is done with EIR, don't search next device */ + if( bta_dm_search_cb.p_btm_inq_info ) +#endif + bta_dm_discover_next_device(); + } + else + { + /* wait until link is disconnected or timeout */ + bta_dm_search_cb.sdp_results = TRUE; + bta_dm_search_cb.search_timer.p_cback = (TIMER_CBACK*)&bta_dm_search_timer_cback; + bta_sys_start_timer(&bta_dm_search_cb.search_timer, 0, 1000*(L2CAP_LINK_INACTIVITY_TOUT+1) ); + } + +} + +/******************************************************************************* +** +** Function bta_dm_search_timer_cback +** +** Description Called when ACL disconnect time is over +** +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_search_timer_cback (TIMER_LIST_ENT *p_tle) +{ + + APPL_TRACE_EVENT0(" bta_dm_search_timer_cback "); + bta_dm_search_cb.wait_disc = FALSE; + + /* proceed with next device */ + bta_dm_discover_next_device(); + +} + + +/******************************************************************************* +** +** Function bta_dm_free_sdp_db +** +** Description Frees SDP data base +** +** Returns void +** +*******************************************************************************/ +void bta_dm_free_sdp_db (tBTA_DM_MSG *p_data) +{ + if(bta_dm_search_cb.p_sdp_db) + { + GKI_freebuf(bta_dm_search_cb.p_sdp_db); + bta_dm_search_cb.p_sdp_db = NULL; + } + +} + +/******************************************************************************* +** +** Function bta_dm_queue_search +** +** Description Queues search command while search is being cancelled +** +** Returns void +** +*******************************************************************************/ +void bta_dm_queue_search (tBTA_DM_MSG *p_data) +{ + + bta_dm_search_cb.p_search_queue = (tBTA_DM_MSG *)GKI_getbuf(sizeof(tBTA_DM_API_SEARCH)); + memcpy(bta_dm_search_cb.p_search_queue, p_data, sizeof(tBTA_DM_API_SEARCH)); + +} + +/******************************************************************************* +** +** Function bta_dm_queue_disc +** +** Description Queues discovery command while search is being cancelled +** +** Returns void +** +*******************************************************************************/ +void bta_dm_queue_disc (tBTA_DM_MSG *p_data) +{ + + bta_dm_search_cb.p_search_queue = (tBTA_DM_MSG *)GKI_getbuf(sizeof(tBTA_DM_API_DISCOVER)); + memcpy(bta_dm_search_cb.p_search_queue, p_data, sizeof(tBTA_DM_API_DISCOVER)); + +} + +/******************************************************************************* +** +** Function bta_dm_search_clear_queue +** +** Description Clears the queue if API search cancel is called +** +** Returns void +** +*******************************************************************************/ +void bta_dm_search_clear_queue (tBTA_DM_MSG *p_data) +{ + + if(bta_dm_search_cb.p_search_queue) + { + GKI_freebuf(bta_dm_search_cb.p_search_queue); + bta_dm_search_cb.p_search_queue = NULL; + } + + +} + +/******************************************************************************* +** +** Function bta_dm_search_cancel_cmpl +** +** Description Search cancel is complete +** +** Returns void +** +*******************************************************************************/ +void bta_dm_search_cancel_cmpl (tBTA_DM_MSG *p_data) +{ + + if(bta_dm_search_cb.p_search_queue) + { + bta_sys_sendmsg(bta_dm_search_cb.p_search_queue); + bta_dm_search_cb.p_search_queue = NULL; + } + +} + +/******************************************************************************* +** +** Function bta_dm_search_cancel_transac_cmpl +** +** Description Current Service Discovery or remote name procedure is +** completed after search cancellation +** +** Returns void +** +*******************************************************************************/ +void bta_dm_search_cancel_transac_cmpl(tBTA_DM_MSG *p_data) +{ + + if(bta_dm_search_cb.p_sdp_db) + { + GKI_freebuf(bta_dm_search_cb.p_sdp_db); + bta_dm_search_cb.p_sdp_db = NULL; + } + + bta_dm_search_cancel_notify(NULL); +} + + +/******************************************************************************* +** +** Function bta_dm_search_cancel_notify +** +** Description Notify application that search has been cancelled +** +** Returns void +** +*******************************************************************************/ +void bta_dm_search_cancel_notify (tBTA_DM_MSG *p_data) +{ + if (bta_dm_search_cb.p_search_cback) + { + bta_dm_search_cb.p_search_cback(BTA_DM_SEARCH_CANCEL_CMPL_EVT, NULL); + } + if (!bta_dm_search_cb.name_discover_done) + { + BTM_CancelRemoteDeviceName(); + } +#if ((BLE_INCLUDED == TRUE) && (defined BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)) + if (bta_dm_search_cb.gatt_disc_active) + { + bta_dm_cancel_gatt_discovery(bta_dm_search_cb.peer_bdaddr); + } +#endif + +} + +/******************************************************************************* +** +** Function bta_dm_find_services +** +** Description Starts discovery on a device +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_find_services ( BD_ADDR bd_addr) +{ + + tSDP_UUID uuid; + UINT16 attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST, ATTR_ID_EXT_BRCM_VERSION}; + UINT16 num_attrs = 1; + tBTA_DM_MSG *p_msg; + + memset (&uuid, 0, sizeof(tSDP_UUID)); + + while(bta_dm_search_cb.service_index < BTA_MAX_SERVICE_ID) + { + if( bta_dm_search_cb.services_to_search + & (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(bta_dm_search_cb.service_index))) + { + if((bta_dm_search_cb.p_sdp_db = (tSDP_DISCOVERY_DB *)GKI_getbuf(BTA_DM_SDP_DB_SIZE)) != NULL) + { + APPL_TRACE_DEBUG1("bta_dm_search_cb.services = %04x***********", bta_dm_search_cb.services); + /* try to search all services by search based on L2CAP UUID */ + if(bta_dm_search_cb.services == BTA_ALL_SERVICE_MASK ) + { + APPL_TRACE_ERROR1("services_to_search = %08x",bta_dm_search_cb.services_to_search); + if (bta_dm_search_cb.services_to_search & BTA_RES_SERVICE_MASK) + { + uuid.uu.uuid16 = bta_service_id_to_uuid_lkup_tbl[0]; + bta_dm_search_cb.services_to_search &= ~BTA_RES_SERVICE_MASK; + } + else + { + uuid.uu.uuid16 = UUID_PROTOCOL_L2CAP; + bta_dm_search_cb.services_to_search = 0; + } + } + else + { +#if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE + /* for LE only profile */ + if (bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID) + { + if (bta_dm_search_cb.uuid_to_search > 0 && bta_dm_search_cb.p_srvc_uuid) + { + memcpy(&uuid, + (const void *)(bta_dm_search_cb.p_srvc_uuid + \ + bta_dm_search_cb.num_uuid - bta_dm_search_cb.uuid_to_search), + sizeof(tBT_UUID)); + + bta_dm_search_cb.uuid_to_search -- ; + } + else + uuid.uu.uuid16 = bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index]; + + /* last one? clear the BLE service bit if all discovery has been done */ + if (bta_dm_search_cb.uuid_to_search == 0) + bta_dm_search_cb.services_to_search &= + (tBTA_SERVICE_MASK)(~(BTA_SERVICE_ID_TO_SERVICE_MASK(bta_dm_search_cb.service_index))); + + } + else +#endif + { + /* remove the service from services to be searched */ + bta_dm_search_cb.services_to_search &= + (tBTA_SERVICE_MASK)(~(BTA_SERVICE_ID_TO_SERVICE_MASK(bta_dm_search_cb.service_index))); + uuid.uu.uuid16 = bta_service_id_to_uuid_lkup_tbl[bta_dm_search_cb.service_index]; + } + } + + if (uuid.len == 0) + uuid.len = LEN_UUID_16; + +#if 0 + if (uuid.uu.uuid16 == UUID_SERVCLASS_PNP_INFORMATION) + { + num_attrs = 2; + } +#endif + + if (bta_dm_search_cb.service_index == BTA_USER_SERVICE_ID) + { + memcpy(&uuid, &bta_dm_search_cb.uuid, sizeof(tSDP_UUID)); + } + + + APPL_TRACE_ERROR1("****************search UUID = %04x***********", uuid.uu.uuid16); + //SDP_InitDiscoveryDb (bta_dm_search_cb.p_sdp_db, BTA_DM_SDP_DB_SIZE, 1, &uuid, num_attrs, attr_list); + SDP_InitDiscoveryDb (bta_dm_search_cb.p_sdp_db, BTA_DM_SDP_DB_SIZE, 1, &uuid, 0, NULL); + + + memset(g_disc_raw_data_buf, 0, sizeof(g_disc_raw_data_buf)); + bta_dm_search_cb.p_sdp_db->raw_data = g_disc_raw_data_buf; + + bta_dm_search_cb.p_sdp_db->raw_size = MAX_DISC_RAW_DATA_BUF; + + if (!SDP_ServiceSearchAttributeRequest (bd_addr, bta_dm_search_cb.p_sdp_db, &bta_dm_sdp_callback)) + { + /* if discovery not successful with this device + proceed to next one */ + GKI_freebuf(bta_dm_search_cb.p_sdp_db); + bta_dm_search_cb.p_sdp_db = NULL; + bta_dm_search_cb.service_index = BTA_MAX_SERVICE_ID; + + } + else + { +#if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE + if ((bta_dm_search_cb.service_index == BTA_BLE_SERVICE_ID && + bta_dm_search_cb.uuid_to_search == 0) || + bta_dm_search_cb.service_index != BTA_BLE_SERVICE_ID) +#endif + bta_dm_search_cb.service_index++; + return; + } + } + else + { + APPL_TRACE_ERROR0("#### Failed to allocate SDP DB buffer! ####"); + } + } + + bta_dm_search_cb.service_index++; + } + + /* no more services to be discovered */ + if(bta_dm_search_cb.service_index >= BTA_MAX_SERVICE_ID) + { + if ((p_msg = (tBTA_DM_MSG *) GKI_getbuf(sizeof(tBTA_DM_MSG))) != NULL) + { + p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT; + p_msg->disc_result.result.disc_res.services = bta_dm_search_cb.services_found; + bdcpy (p_msg->disc_result.result.disc_res.bd_addr, bta_dm_search_cb.peer_bdaddr); + BCM_STRNCPY_S((char*)p_msg->disc_result.result.disc_res.bd_name, sizeof(BD_NAME), + bta_dm_get_remname(), (BD_NAME_LEN-1)); + + /* make sure the string is terminated */ + p_msg->disc_result.result.disc_res.bd_name[BD_NAME_LEN-1] = 0; + + bta_sys_sendmsg(p_msg); + } + } +} + +/******************************************************************************* +** +** Function bta_dm_discover_next_device +** +** Description Starts discovery on the next device in Inquiry data base +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_discover_next_device(void) +{ + + tBTA_DM_MSG * p_msg; + + APPL_TRACE_DEBUG0("bta_dm_discover_next_device"); + + /* searching next device on inquiry result */ + if((bta_dm_search_cb.p_btm_inq_info = BTM_InqDbNext(bta_dm_search_cb.p_btm_inq_info)) != NULL) + { + bta_dm_search_cb.name_discover_done = FALSE; + bta_dm_search_cb.peer_name[0] = 0; + bta_dm_discover_device(bta_dm_search_cb.p_btm_inq_info->results.remote_bd_addr); + } + else + { + /* no devices, search complete */ + bta_dm_search_cb.services = 0; + + if ((p_msg = (tBTA_DM_MSG *) GKI_getbuf(sizeof(tBTA_DM_MSG))) != NULL) + { + p_msg->hdr.event = BTA_DM_SEARCH_CMPL_EVT; + p_msg->hdr.layer_specific = BTA_DM_API_DISCOVER_EVT; + bta_sys_sendmsg(p_msg); + } + } +} + +/******************************************************************************* +** +** Function bta_dm_discover_device +** +** Description Starts name and service discovery on the device +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_discover_device(BD_ADDR remote_bd_addr) +{ + tBTA_DM_MSG * p_msg; + +#if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE + tBT_DEVICE_TYPE dev_type; + tBLE_ADDR_TYPE addr_type; +#endif + + APPL_TRACE_DEBUG6("bta_dm_discover_device, BDA:0x%02X%02X%02X%02X%02X%02X", + remote_bd_addr[0],remote_bd_addr[1], + remote_bd_addr[2],remote_bd_addr[3], + remote_bd_addr[4],remote_bd_addr[5]); + + bdcpy(bta_dm_search_cb.peer_bdaddr, remote_bd_addr); + + APPL_TRACE_DEBUG2("bta_dm_discover_device name_discover_done = %d p_btm_inq_info 0x%x ", + bta_dm_search_cb.name_discover_done, + bta_dm_search_cb.p_btm_inq_info + ); + if ( bta_dm_search_cb.p_btm_inq_info ) { + + APPL_TRACE_DEBUG1("bta_dm_discover_device appl_knows_rem_name %d", + bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name + ); + } + + /* if name discovery is not done and application needs remote name */ + if ((!bta_dm_search_cb.name_discover_done) + && (( bta_dm_search_cb.p_btm_inq_info == NULL ) + ||(bta_dm_search_cb.p_btm_inq_info && (!bta_dm_search_cb.p_btm_inq_info->appl_knows_rem_name)))) + { + if( bta_dm_read_remote_device_name(bta_dm_search_cb.peer_bdaddr) == TRUE ) + { + return; + } + else + { + /* starting name discovery failed */ + bta_dm_search_cb.name_discover_done = TRUE; + } + } + + /* if application wants to discover service */ + if ( bta_dm_search_cb.services ) + { + /* initialize variables */ + bta_dm_search_cb.service_index = 0; + bta_dm_search_cb.services_found = 0; + bta_dm_search_cb.services_to_search = bta_dm_search_cb.services; +#if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE + bta_dm_search_cb.uuid_to_search = bta_dm_search_cb.num_uuid; +#endif +#if ( BTM_EIR_CLIENT_INCLUDED == TRUE ) + if ((bta_dm_search_cb.p_btm_inq_info != NULL) && + bta_dm_search_cb.services != BTA_USER_SERVICE_MASK + &&(bta_dm_search_cb.sdp_search == FALSE)) + { + /* check if EIR provides the information of supported services */ + bta_dm_eir_search_services( &bta_dm_search_cb.p_btm_inq_info->results, + &bta_dm_search_cb.services_to_search, + &bta_dm_search_cb.services_found ); + } + + /* if seaching with EIR is not completed */ + if(bta_dm_search_cb.services_to_search) +#endif + { + /* check whether connection already exists to the device + if connection exists, we don't have to wait for ACL + link to go down to start search on next device */ + if(BTM_IsAclConnectionUp(bta_dm_search_cb.peer_bdaddr)) + bta_dm_search_cb.wait_disc = FALSE; + else + bta_dm_search_cb.wait_disc = TRUE; + +#if (BLE_INCLUDED == TRUE && (defined BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)) + if ( bta_dm_search_cb.p_btm_inq_info ) + { + APPL_TRACE_DEBUG3("bta_dm_discover_device p_btm_inq_info 0x%x results.device_type 0x%x services_to_search 0x%x", + bta_dm_search_cb.p_btm_inq_info, + bta_dm_search_cb.p_btm_inq_info->results.device_type, + bta_dm_search_cb.services_to_search + ); + } + BTM_ReadDevInfo(remote_bd_addr, &dev_type, &addr_type); + + if (dev_type == BT_DEVICE_TYPE_BLE) + /* + if ( bta_dm_search_cb.p_btm_inq_info != NULL && + bta_dm_search_cb.p_btm_inq_info->results.device_type == BT_DEVICE_TYPE_BLE && + (bta_dm_search_cb.services_to_search & BTA_BLE_SERVICE_MASK))*/ + { + if (bta_dm_search_cb.services_to_search & BTA_BLE_SERVICE_MASK) + { + //set the raw data buffer here + memset(g_disc_raw_data_buf, 0, sizeof(g_disc_raw_data_buf)); + bta_dm_search_cb.p_ble_rawdata = g_disc_raw_data_buf; + + bta_dm_search_cb.ble_raw_size = MAX_DISC_RAW_DATA_BUF; + bta_dm_search_cb.ble_raw_used = 0; + + /* start GATT for service discovery */ + btm_dm_start_gatt_discovery(bta_dm_search_cb.peer_bdaddr); + return; + } + } + else +#endif + { + bta_dm_search_cb.sdp_results = FALSE; + bta_dm_find_services(bta_dm_search_cb.peer_bdaddr); + + return; + } + } + } + + /* name discovery and service discovery are done for this device */ + if ((p_msg = (tBTA_DM_MSG *) GKI_getbuf(sizeof(tBTA_DM_MSG))) != NULL) + { + p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT; + /* initialize the data structure - includes p_raw_data and raw_data_size */ + memset(&(p_msg->disc_result.result), 0, sizeof(tBTA_DM_DISC_RES)); + p_msg->disc_result.result.disc_res.result = BTA_SUCCESS; + p_msg->disc_result.result.disc_res.services = bta_dm_search_cb.services_found; + bdcpy (p_msg->disc_result.result.disc_res.bd_addr, bta_dm_search_cb.peer_bdaddr); + BCM_STRNCPY_S((char*)p_msg->disc_result.result.disc_res.bd_name, sizeof(BD_NAME), + (char*)bta_dm_search_cb.peer_name, (BD_NAME_LEN-1)); + + /* make sure the string is terminated */ + p_msg->disc_result.result.disc_res.bd_name[BD_NAME_LEN-1] = 0; + + bta_sys_sendmsg(p_msg); + } +} + +/******************************************************************************* +** +** Function bta_dm_sdp_callback +** +** Description Callback from sdp with discovery status +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_sdp_callback (UINT16 sdp_status) +{ + + tBTA_DM_SDP_RESULT * p_msg; + + if ((p_msg = (tBTA_DM_SDP_RESULT *) GKI_getbuf(sizeof(tBTA_DM_SDP_RESULT))) != NULL) + { + p_msg->hdr.event = BTA_DM_SDP_RESULT_EVT; + p_msg->sdp_result = sdp_status; + bta_sys_sendmsg(p_msg); + + } +} + +/******************************************************************************* +** +** Function bta_dm_inq_results_cb +** +** Description Inquiry results callback from BTM +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_inq_results_cb (tBTM_INQ_RESULTS *p_inq, UINT8 *p_eir) +{ + + tBTA_DM_SEARCH result; + tBTM_INQ_INFO *p_inq_info; + UINT16 service_class; + + bdcpy(result.inq_res.bd_addr, p_inq->remote_bd_addr); + memcpy(result.inq_res.dev_class, p_inq->dev_class, DEV_CLASS_LEN); + BTM_COD_SERVICE_CLASS(service_class, p_inq->dev_class); + result.inq_res.is_limited = (service_class & BTM_COD_SERVICE_LMTD_DISCOVER)?TRUE:FALSE; + result.inq_res.rssi = p_inq->rssi; + +#if (BLE_INCLUDED == TRUE) + result.inq_res.ble_addr_type = p_inq->ble_addr_type; + result.inq_res.inq_result_type = p_inq->inq_result_type; + result.inq_res.device_type = p_inq->device_type; + +#endif + + /* application will parse EIR to find out remote device name */ + result.inq_res.p_eir = p_eir; + + if((p_inq_info = BTM_InqDbRead(p_inq->remote_bd_addr)) != NULL) + { + /* initialize remt_name_not_required to FALSE so that we get the name by default */ + result.inq_res.remt_name_not_required = FALSE; + + } + + if(bta_dm_search_cb.p_search_cback) + bta_dm_search_cb.p_search_cback(BTA_DM_INQ_RES_EVT, &result); + + if(p_inq_info) + { + /* application indicates if it knows the remote name, inside the callback + copy that to the inquiry data base*/ + if(result.inq_res.remt_name_not_required) + p_inq_info->appl_knows_rem_name = TRUE; + + } + + +} + + +/******************************************************************************* +** +** Function bta_dm_inq_cmpl_cb +** +** Description Inquiry complete callback from BTM +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_inq_cmpl_cb (void * p_result) +{ + + tBTA_DM_MSG * p_msg; + + APPL_TRACE_DEBUG0("bta_dm_inq_cmpl_cb"); + if ((p_msg = (tBTA_DM_MSG *) GKI_getbuf(sizeof(tBTA_DM_MSG))) != NULL) + { + p_msg->inq_cmpl.hdr.event = BTA_DM_INQUIRY_CMPL_EVT; + p_msg->inq_cmpl.num = ((tBTM_INQUIRY_CMPL *)p_result)->num_resp; + bta_sys_sendmsg(p_msg); + + } + + +} + +/******************************************************************************* +** +** Function bta_dm_service_search_remname_cback +** +** Description Remote name call back from BTM during service discovery +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_service_search_remname_cback (BD_ADDR bd_addr, DEV_CLASS dc, BD_NAME bd_name) +{ + tBTM_REMOTE_DEV_NAME rem_name; + tBTM_STATUS btm_status; + + APPL_TRACE_DEBUG1("bta_dm_service_search_remname_cback name=<%s>", bd_name); + + /* if this is what we are looking for */ + if (!bdcmp( bta_dm_search_cb.peer_bdaddr, bd_addr)) + { + rem_name.length = strlen((char*)bd_name); + if (rem_name.length > (BD_NAME_LEN-1)) + { + rem_name.length = (BD_NAME_LEN-1); + rem_name.remote_bd_name[(BD_NAME_LEN-1)] = 0; + } + BCM_STRNCPY_S((char*)rem_name.remote_bd_name, sizeof(BD_NAME), (char*)bd_name, (BD_NAME_LEN-1)); + rem_name.status = BTM_SUCCESS; + + bta_dm_remname_cback(&rem_name); + } + else + { + /* get name of device */ + btm_status = BTM_ReadRemoteDeviceName (bta_dm_search_cb.peer_bdaddr, + (tBTM_CMPL_CB *) bta_dm_remname_cback); + if ( btm_status == BTM_BUSY ) + { + /* wait for next chance(notification of remote name discovery done) */ + APPL_TRACE_DEBUG0("bta_dm_service_search_remname_cback: BTM_ReadRemoteDeviceName is busy"); + } + else if ( btm_status != BTM_CMD_STARTED ) + { + /* if failed to start getting remote name then continue */ + APPL_TRACE_WARNING1("bta_dm_service_search_remname_cback: BTM_ReadRemoteDeviceName returns 0x%02X", btm_status); + + rem_name.length = 0; + rem_name.remote_bd_name[0] = 0; + rem_name.status = btm_status; + bta_dm_remname_cback(&rem_name); + } + } +} + + +/******************************************************************************* +** +** Function bta_dm_remname_cback +** +** Description Remote name complete call back from BTM +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_remname_cback (tBTM_REMOTE_DEV_NAME *p_remote_name) +{ + tBTA_DM_REM_NAME * p_msg; + + APPL_TRACE_DEBUG1("bta_dm_remname_cback name=<%s>", p_remote_name->remote_bd_name); + + /* remote name discovery is done but it could be failed */ + bta_dm_search_cb.name_discover_done = TRUE; + BCM_STRNCPY_S((char*)bta_dm_search_cb.peer_name, sizeof(BD_NAME), (char*)p_remote_name->remote_bd_name, (BD_NAME_LEN-1)); + bta_dm_search_cb.peer_name[BD_NAME_LEN-1]=0; + + BTM_SecDeleteRmtNameNotifyCallback(&bta_dm_service_search_remname_cback); + + if ((p_msg = (tBTA_DM_REM_NAME *) GKI_getbuf(sizeof(tBTA_DM_REM_NAME))) != NULL) + { + bdcpy (p_msg->result.disc_res.bd_addr, bta_dm_search_cb.peer_bdaddr); + BCM_STRNCPY_S((char*)p_msg->result.disc_res.bd_name, sizeof(BD_NAME), (char*)p_remote_name->remote_bd_name, (BD_NAME_LEN-1)); + + /* make sure the string is null terminated */ + p_msg->result.disc_res.bd_name[BD_NAME_LEN-1] = 0; + + p_msg->hdr.event = BTA_DM_REMT_NAME_EVT; + bta_sys_sendmsg(p_msg); + + } +} + +/******************************************************************************* +** +** Function bta_dm_authorize_cback +** +** Description cback requesting authorization +** +** Returns void +** +*******************************************************************************/ +static UINT8 bta_dm_authorize_cback (BD_ADDR bd_addr, DEV_CLASS dev_class, BD_NAME bd_name, + UINT8 *service_name, UINT8 service_id, BOOLEAN is_originator) +{ + tBTA_DM_SEC sec_event; + UINT8 index = 1; + + bdcpy(sec_event.authorize.bd_addr, bd_addr); + memcpy(sec_event.authorize.dev_class, dev_class, DEV_CLASS_LEN); + + BCM_STRNCPY_S((char*)sec_event.authorize.bd_name, sizeof(BD_NAME), (char*)bd_name, (BD_NAME_LEN-1)); + + /* make sure the string is null terminated */ + sec_event.authorize.bd_name[BD_NAME_LEN-1] = 0; + +#if ( defined(BTA_JV_INCLUDED) && BTA_JV_INCLUDED == TRUE ) + sec_event.authorize.service = service_id; +#endif + + while(index < BTA_MAX_SERVICE_ID) + { + /* get the BTA service id corresponding to BTM id */ + if(bta_service_id_to_btm_srv_id_lkup_tbl[index] == service_id) + { + sec_event.authorize.service = index; + break; + } + index++; + } + + + /* if supported service callback otherwise not authorized */ + if(bta_dm_cb.p_sec_cback && (index < BTA_MAX_SERVICE_ID +#if ( defined(BTA_JV_INCLUDED) && BTA_JV_INCLUDED == TRUE ) + /* pass through JV service ID */ + || (service_id >= BTA_FIRST_JV_SERVICE_ID && service_id <= BTA_LAST_JV_SERVICE_ID) +#endif + )) + { + bta_dm_cb.p_sec_cback(BTA_DM_AUTHORIZE_EVT, &sec_event); + return BTM_CMD_STARTED; + } + else + { + return BTM_NOT_AUTHORIZED; + } +} + + + + + +/******************************************************************************* +** +** Function bta_dm_pinname_cback +** +** Description Callback requesting pin_key +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_pinname_cback (void *p_data) +{ + tBTM_REMOTE_DEV_NAME *p_result = (tBTM_REMOTE_DEV_NAME *)p_data; + tBTA_DM_SEC sec_event; + UINT32 bytes_to_copy; + tBTA_DM_SEC_EVT event = bta_dm_cb.pin_evt; + + if (BTA_DM_SP_CFM_REQ_EVT == event) + { + /* Retrieved saved device class and bd_addr */ + bdcpy(sec_event.cfm_req.bd_addr, bta_dm_cb.pin_bd_addr); + BTA_COPY_DEVICE_CLASS(sec_event.cfm_req.dev_class, bta_dm_cb.pin_dev_class); + + if (p_result && p_result->status == BTM_SUCCESS) + { + bytes_to_copy = (p_result->length < (BD_NAME_LEN-1)) + ? p_result->length : (BD_NAME_LEN-1); + memcpy(sec_event.cfm_req.bd_name, p_result->remote_bd_name, bytes_to_copy); + sec_event.pin_req.bd_name[BD_NAME_LEN-1] = 0; + } + else /* No name found */ + sec_event.cfm_req.bd_name[0] = 0; + + sec_event.key_notif.passkey = bta_dm_cb.num_val; /* get PIN code numeric number */ + + /* 1 additional event data fields for this event */ + sec_event.cfm_req.just_works = bta_dm_cb.just_works; + } + else + { + /* Retrieved saved device class and bd_addr */ + bdcpy(sec_event.pin_req.bd_addr, bta_dm_cb.pin_bd_addr); + BTA_COPY_DEVICE_CLASS(sec_event.pin_req.dev_class, bta_dm_cb.pin_dev_class); + + if (p_result && p_result->status == BTM_SUCCESS) + { + bytes_to_copy = (p_result->length < (BD_NAME_LEN-1)) + ? p_result->length : (BD_NAME_LEN-1); + memcpy(sec_event.pin_req.bd_name, p_result->remote_bd_name, bytes_to_copy); + sec_event.pin_req.bd_name[BD_NAME_LEN-1] = 0; + } + else /* No name found */ + sec_event.pin_req.bd_name[0] = 0; + + event = bta_dm_cb.pin_evt; + sec_event.key_notif.passkey = bta_dm_cb.num_val; /* get PIN code numeric number */ + } + + if( bta_dm_cb.p_sec_cback ) + bta_dm_cb.p_sec_cback(event, &sec_event); +} + + + +/******************************************************************************* +** +** Function bta_dm_pin_cback +** +** Description Callback requesting pin_key +** +** Returns void +** +*******************************************************************************/ +static UINT8 bta_dm_pin_cback (BD_ADDR bd_addr, DEV_CLASS dev_class, BD_NAME bd_name) +{ + tBTA_DM_SEC sec_event; + + if (!bta_dm_cb.p_sec_cback) + return BTM_NOT_AUTHORIZED; + + /* If the device name is not known, save bdaddr and devclass and initiate a name request */ + if (bd_name[0] == 0) + { + bta_dm_cb.pin_evt = BTA_DM_PIN_REQ_EVT; + bdcpy(bta_dm_cb.pin_bd_addr, bd_addr); + BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class, dev_class); + if ((BTM_ReadRemoteDeviceName(bd_addr, bta_dm_pinname_cback)) == BTM_CMD_STARTED) + return BTM_CMD_STARTED; + + APPL_TRACE_WARNING0(" bta_dm_pin_cback() -> Failed to start Remote Name Request "); + } + + bdcpy(sec_event.pin_req.bd_addr, bd_addr); + BTA_COPY_DEVICE_CLASS(sec_event.pin_req.dev_class, dev_class); + BCM_STRNCPY_S((char*)sec_event.pin_req.bd_name, sizeof(BD_NAME), (char*)bd_name, (BD_NAME_LEN-1)); + sec_event.pin_req.bd_name[BD_NAME_LEN-1] = 0; + + bta_dm_cb.p_sec_cback(BTA_DM_PIN_REQ_EVT, &sec_event); + return BTM_CMD_STARTED; +} + + + +/******************************************************************************* +** +** Function bta_dm_link_key_request_cback +** +** Description Callback requesting linkkey +** +** Returns void +** +*******************************************************************************/ +static UINT8 bta_dm_link_key_request_cback (BD_ADDR bd_addr, LINK_KEY key) +{ + /* Application passes all link key to + BTM during initialization using add_device + API. If BTM doesn't have the link key in it's + data base, that's because application doesn't + it */ + + return BTM_NOT_AUTHORIZED; +} + + + + + +/******************************************************************************* +** +** Function bta_dm_new_link_key_cback +** +** Description Callback from BTM to notify new link key +** +** Returns void +** +*******************************************************************************/ +static UINT8 bta_dm_new_link_key_cback(BD_ADDR bd_addr, DEV_CLASS dev_class, + BD_NAME bd_name, LINK_KEY key, UINT8 key_type) +{ + tBTA_DM_SEC sec_event; + tBTA_DM_AUTH_CMPL *p_auth_cmpl; + UINT8 event; + + memset (&sec_event, 0, sizeof(tBTA_DM_SEC)); + + /* Not AMP Key type */ + if (key_type != HCI_LKEY_TYPE_AMP_WIFI && key_type != HCI_LKEY_TYPE_AMP_UWB) + { + event = BTA_DM_AUTH_CMPL_EVT; + p_auth_cmpl = &sec_event.auth_cmpl; + + bdcpy(p_auth_cmpl->bd_addr, bd_addr); + + memcpy(p_auth_cmpl->bd_name, bd_name, (BD_NAME_LEN-1)); + p_auth_cmpl->bd_name[BD_NAME_LEN-1] = 0; + + p_auth_cmpl->key_present = TRUE; + p_auth_cmpl->key_type = key_type; + p_auth_cmpl->success = TRUE; + + memcpy(p_auth_cmpl->key, key, LINK_KEY_LEN); + sec_event.auth_cmpl.fail_reason = HCI_SUCCESS; + + if(bta_dm_cb.p_sec_cback) + { + bta_dm_cb.p_sec_cback(event, &sec_event); + } + } + else + { + APPL_TRACE_WARNING0(" bta_dm_new_link_key_cback() Received AMP Key?? "); + } + + return BTM_CMD_STARTED; +} + + +/******************************************************************************* +** +** Function bta_dm_authentication_complete_cback +** +** Description Authentication complete callback from BTM +** +** Returns void +** +*******************************************************************************/ +static UINT8 bta_dm_authentication_complete_cback(BD_ADDR bd_addr, DEV_CLASS dev_class,BD_NAME bd_name, int result) +{ + + tBTA_DM_SEC sec_event; + + if(result != BTM_SUCCESS) + { + memset(&sec_event, 0, sizeof(tBTA_DM_SEC)); + bdcpy(sec_event.auth_cmpl.bd_addr, bd_addr); + + memcpy(sec_event.auth_cmpl.bd_name, bd_name, (BD_NAME_LEN-1)); + sec_event.auth_cmpl.bd_name[BD_NAME_LEN-1] = 0; + +/* taken care of by memset [above] + sec_event.auth_cmpl.key_present = FALSE; + sec_event.auth_cmpl.success = FALSE; +*/ + sec_event.auth_cmpl.fail_reason = (UINT8)result; + if(bta_dm_cb.p_sec_cback) + { + bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event); + } + } + + return BTM_SUCCESS; +} + +/******************************************************************************* +** +** Function bta_dm_sp_cback +** +** Description simple pairing callback from BTM +** +** Returns void +** +*******************************************************************************/ +static UINT8 bta_dm_sp_cback (tBTM_SP_EVT event, tBTM_SP_EVT_DATA *p_data) +{ + tBTM_STATUS status = BTM_CMD_STARTED; + tBTA_DM_SEC sec_event; + tBTA_DM_SEC_EVT pin_evt = BTA_DM_SP_KEY_NOTIF_EVT; + + APPL_TRACE_EVENT1("bta_dm_sp_cback: %d", event); + if (!bta_dm_cb.p_sec_cback) + return BTM_NOT_AUTHORIZED; + + /* TODO_SP */ + switch(event) + { + case BTM_SP_IO_REQ_EVT: +#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE) + /* translate auth_req */ + bta_dm_co_io_req(p_data->io_req.bd_addr, &p_data->io_req.io_cap, + &p_data->io_req.oob_data, &p_data->io_req.auth_req, p_data->io_req.is_orig); +#endif +#if BTM_OOB_INCLUDED == FALSE + status = BTM_SUCCESS; +#endif + + APPL_TRACE_EVENT2("io mitm: %d oob_data:%d", p_data->io_req.auth_req, p_data->io_req.oob_data); + break; + case BTM_SP_IO_RSP_EVT: +#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE) + bta_dm_co_io_rsp(p_data->io_rsp.bd_addr, p_data->io_rsp.io_cap, + p_data->io_rsp.oob_data, p_data->io_rsp.auth_req ); +#endif + break; + + case BTM_SP_CFM_REQ_EVT: + pin_evt = BTA_DM_SP_CFM_REQ_EVT; + bta_dm_cb.just_works = sec_event.cfm_req.just_works = p_data->cfm_req.just_works; + sec_event.cfm_req.loc_auth_req = p_data->cfm_req.loc_auth_req; + sec_event.cfm_req.rmt_auth_req = p_data->cfm_req.rmt_auth_req; + /* continue to next case */ +#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE) + /* Passkey entry mode, mobile device with output capability is very + unlikely to receive key request, so skip this event */ + /*case BTM_SP_KEY_REQ_EVT: */ + case BTM_SP_KEY_NOTIF_EVT: +#endif + bta_dm_cb.num_val = sec_event.key_notif.passkey = p_data->key_notif.passkey; + /* If the device name is not known, save bdaddr and devclass and initiate a name request */ + if (p_data->key_notif.bd_name[0] == 0) + { + bta_dm_cb.pin_evt = pin_evt; + bdcpy(bta_dm_cb.pin_bd_addr, p_data->key_notif.bd_addr); + BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class, p_data->key_notif.dev_class); + if ((BTM_ReadRemoteDeviceName(p_data->key_notif.bd_addr, bta_dm_pinname_cback)) == BTM_CMD_STARTED) + return BTM_CMD_STARTED; + + APPL_TRACE_WARNING0(" bta_dm_sp_cback() -> Failed to start Remote Name Request "); + } + bdcpy(sec_event.key_notif.bd_addr, p_data->key_notif.bd_addr); + BTA_COPY_DEVICE_CLASS(sec_event.key_notif.dev_class, p_data->key_notif.dev_class); + BCM_STRNCPY_S((char*)sec_event.key_notif.bd_name, sizeof(BD_NAME), (char*)p_data->key_notif.bd_name, (BD_NAME_LEN-1)); + sec_event.key_notif.bd_name[BD_NAME_LEN-1] = 0; + + bta_dm_cb.p_sec_cback(pin_evt, &sec_event); + + break; + +#if BTM_OOB_INCLUDED == TRUE + case BTM_SP_LOC_OOB_EVT: + bta_dm_co_loc_oob((BOOLEAN)(p_data->loc_oob.status == BTM_SUCCESS), + p_data->loc_oob.c, p_data->loc_oob.r); + break; + + case BTM_SP_RMT_OOB_EVT: + /* If the device name is not known, save bdaddr and devclass and initiate a name request */ + if (p_data->rmt_oob.bd_name[0] == 0) + { + bta_dm_cb.pin_evt = BTA_DM_SP_RMT_OOB_EVT; + bdcpy(bta_dm_cb.pin_bd_addr, p_data->rmt_oob.bd_addr); + BTA_COPY_DEVICE_CLASS(bta_dm_cb.pin_dev_class, p_data->rmt_oob.dev_class); + if ((BTM_ReadRemoteDeviceName(p_data->rmt_oob.bd_addr, bta_dm_pinname_cback)) == BTM_CMD_STARTED) + return BTM_CMD_STARTED; + + APPL_TRACE_WARNING0(" bta_dm_sp_cback() -> Failed to start Remote Name Request "); + } + bdcpy(sec_event.rmt_oob.bd_addr, p_data->rmt_oob.bd_addr); + BTA_COPY_DEVICE_CLASS(sec_event.rmt_oob.dev_class, p_data->rmt_oob.dev_class); + BCM_STRNCPY_S((char*)sec_event.rmt_oob.bd_name, sizeof(BD_NAME), (char*)p_data->rmt_oob.bd_name, (BD_NAME_LEN-1)); + sec_event.rmt_oob.bd_name[BD_NAME_LEN-1] = 0; + + bta_dm_cb.p_sec_cback(BTA_DM_SP_RMT_OOB_EVT, &sec_event); + + bta_dm_co_rmt_oob(p_data->rmt_oob.bd_addr); + break; +#endif + case BTM_SP_COMPLT_EVT: + /* do not report this event - handled by link_key_callback or auth_complete_callback */ + break; + + case BTM_SP_KEYPRESS_EVT: + memcpy(&sec_event.key_press, &p_data->key_press, sizeof(tBTM_SP_KEYPRESS)); + bta_dm_cb.p_sec_cback(BTA_DM_SP_KEYPRESS_EVT, &sec_event); + break; + + case BTM_SP_UPGRADE_EVT: + bta_dm_co_lk_upgrade(p_data->upgrade.bd_addr, &p_data->upgrade.upgrade ); + break; + + default: + status = BTM_NOT_AUTHORIZED; + break; + } + APPL_TRACE_EVENT1("dm status: %d", status); + return status; +} + +/******************************************************************************* +** +** Function bta_dm_local_name_cback +** +** Description Callback from btm after local name is read +** +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_local_name_cback(UINT8 *p_name) +{ + tBTA_DM_ENABLE enable_event; + + BTM_GetLocalDeviceAddr(enable_event.bd_addr); + enable_event.status = BTA_SUCCESS; + + if(bta_dm_cb.p_sec_cback) + bta_dm_cb.p_sec_cback(BTA_DM_ENABLE_EVT, (tBTA_DM_SEC *)&enable_event); +} + +/******************************************************************************* +** +** Function bta_dm_signal_strength +** +** Description Callback from btm after local bdaddr is read +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_signal_strength(tBTA_DM_MSG *p_data) +{ + + if(p_data->sig_strength.start) + { + bta_dm_cb.signal_strength_mask = p_data->sig_strength.mask; + bta_dm_cb.signal_strength_period = p_data->sig_strength.period; + bta_dm_signal_strength_timer_cback(NULL); + } + else + { + bta_sys_stop_timer(&bta_dm_cb.signal_strength_timer); + } + +} +/******************************************************************************* +** +** Function bta_dm_signal_strength_timer_cback +** +** Description Periodic timer callback to read signal strength +** +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_signal_strength_timer_cback (TIMER_LIST_ENT *p_tle) +{ + + UINT8 i; + + if(bta_dm_cb.signal_strength_mask & BTA_SIG_STRENGTH_RSSI_MASK) + { + for(i=0; i<bta_dm_cb.device_list.count; i++) + { + BTM_ReadRSSI (bta_dm_cb.device_list.peer_device[i].peer_bdaddr, (tBTM_CMPL_CB *)bta_dm_rssi_cback); + + } + } + if(bta_dm_cb.signal_strength_mask & BTA_SIG_STRENGTH_LINK_QUALITY_MASK) + { + + for(i=0; i<bta_dm_cb.device_list.count; i++) + { + BTM_ReadLinkQuality (bta_dm_cb.device_list.peer_device[i].peer_bdaddr, (tBTM_CMPL_CB *)bta_dm_link_quality_cback); + } + + } + + if(bta_dm_cb.signal_strength_period) + { + bta_dm_cb.signal_strength_timer.p_cback = (TIMER_CBACK*)&bta_dm_signal_strength_timer_cback; + bta_sys_start_timer(&bta_dm_cb.signal_strength_timer, 0, (UINT32)1000*bta_dm_cb.signal_strength_period); + } +} + + +#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE) +/******************************************************************************* +** +** Function bta_dm_bl_change_cback +** +** Description Callback from btm when acl connection goes up or down +** +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_bl_change_cback (tBTM_BL_EVENT_DATA *p_data) +{ + + tBTA_DM_ACL_CHANGE * p_msg; + + if ((p_msg = (tBTA_DM_ACL_CHANGE *) GKI_getbuf(sizeof(tBTA_DM_ACL_CHANGE))) != NULL) + { + p_msg->event = p_data->event; + p_msg->is_new = FALSE; + + switch(p_msg->event) + { + case BTM_BL_CONN_EVT: + p_msg->is_new = TRUE; + bdcpy(p_msg->bd_addr, p_data->conn.p_bda); + break; + case BTM_BL_DISCN_EVT: + bdcpy(p_msg->bd_addr, p_data->discn.p_bda); + break; + case BTM_BL_UPDATE_EVT: + p_msg->busy_level = p_data->update.busy_level; + break; + case BTM_BL_ROLE_CHG_EVT: + p_msg->new_role = p_data->role_chg.new_role; + p_msg->hci_status = p_data->role_chg.hci_status; + bdcpy(p_msg->bd_addr, p_data->role_chg.p_bda); + break; + case BTM_BL_COLLISION_EVT: + bdcpy(p_msg->bd_addr, p_data->conn.p_bda); + break;; + } + + p_msg->hdr.event = BTA_DM_ACL_CHANGE_EVT; + bta_sys_sendmsg(p_msg); + + } + +} +#else + +/******************************************************************************* +** +** Function bta_dm_acl_change_cback +** +** Description Callback from btm when acl connection goes up or down +** +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_acl_change_cback (BD_ADDR p_bda, DEV_CLASS p_dc, BD_NAME p_bdn, + BD_FEATURES features, BOOLEAN is_new) +{ + + tBTA_DM_ACL_CHANGE * p_msg; + + if ((p_msg = (tBTA_DM_ACL_CHANGE *) GKI_getbuf(sizeof(tBTA_DM_ACL_CHANGE))) != NULL) + { + bdcpy (p_msg->bd_addr, p_bda); + p_msg->is_new = is_new; + + /* This is collision case */ + if (features != NULL) + { + if ((features[0] == 0xFF) && !is_new) + p_msg->event = BTM_BL_COLLISION_EVT; + } + + p_msg->hdr.event = BTA_DM_ACL_CHANGE_EVT; + bta_sys_sendmsg(p_msg); + + } + +} +#endif +/******************************************************************************* +** +** Function bta_dm_rs_cback +** +** Description Receives the role switch complete event +** +** Returns +** +*******************************************************************************/ +static void bta_dm_rs_cback (tBTM_ROLE_SWITCH_CMPL *p1) +{ + APPL_TRACE_WARNING1("bta_dm_rs_cback:%d", bta_dm_cb.rs_event); + if(bta_dm_cb.rs_event == BTA_DM_API_SEARCH_EVT) + { + bta_dm_cb.search_msg.rs_res = BTA_DM_RS_OK; /* do not care about the result for now */ + bta_dm_cb.rs_event = 0; + bta_dm_search_start((tBTA_DM_MSG *)&bta_dm_cb.search_msg); + } +} + +/******************************************************************************* +** +** Function bta_dm_check_av +** +** Description This function checks if AV is active +** if yes, make sure the AV link is master +** +** Returns BOOLEAN - TRUE, if switch is in progress +** +*******************************************************************************/ +static BOOLEAN bta_dm_check_av(UINT16 event) +{ + BOOLEAN switching = FALSE; + UINT8 i; + tBTA_DM_PEER_DEVICE *p_dev; + + APPL_TRACE_WARNING1("bta_dm_check_av:%d", bta_dm_cb.cur_av_count); + if(bta_dm_cb.cur_av_count) + { + for(i=0; i<bta_dm_cb.device_list.count; i++) + { + p_dev = &bta_dm_cb.device_list.peer_device[i]; + APPL_TRACE_WARNING3("[%d]: state:%d, info:x%x", i, p_dev->conn_state, p_dev->info); + if((p_dev->conn_state == BTA_DM_CONNECTED) && (p_dev->info & BTA_DM_DI_AV_ACTIVE)) + { + /* make master and take away the role switch policy */ + if(BTM_CMD_STARTED == BTM_SwitchRole (p_dev->peer_bdaddr, HCI_ROLE_MASTER, (tBTM_CMPL_CB *)bta_dm_rs_cback)) + { + /* the role switch command is actually sent */ + bta_dm_cb.rs_event = event; + switching = TRUE; + } + /* else either already master or can not switch for some reasons */ + bta_dm_policy_cback(BTA_SYS_PLCY_CLR, 0, HCI_ENABLE_MASTER_SLAVE_SWITCH, p_dev->peer_bdaddr); + break; + } + } + } + return switching; +} + +/******************************************************************************* +** +** Function bta_dm_acl_change +** +** Description Process BTA_DM_ACL_CHANGE_EVT +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_acl_change(tBTA_DM_MSG *p_data) +{ + + UINT8 i; + UINT8 *p; + tBTA_DM_SEC conn; + BOOLEAN is_new = p_data->acl_change.is_new; + BD_ADDR_PTR p_bda = p_data->acl_change.bd_addr; + BOOLEAN need_policy_change = FALSE; + +#if (defined(BTM_BUSY_LEVEL_CHANGE_INCLUDED) && BTM_BUSY_LEVEL_CHANGE_INCLUDED == TRUE) + tBTA_DM_PEER_DEVICE *p_dev; + + switch(p_data->acl_change.event) + { + case BTM_BL_UPDATE_EVT: /* busy level update */ + if( bta_dm_cb.p_sec_cback ) + bta_dm_cb.p_sec_cback(BTA_DM_BUSY_LEVEL_EVT, (tBTA_DM_SEC *)&p_data->acl_change.busy_level); + return; + + case BTM_BL_ROLE_CHG_EVT: /* role change event */ + p_dev = bta_dm_find_peer_device(p_bda); + if(p_dev) + { + APPL_TRACE_DEBUG3("bta_dm_acl_change role chg info:x%x new_role:%d dev count:%d", + p_dev->info, p_data->acl_change.new_role, bta_dm_cb.device_list.count); + if(p_dev->info & BTA_DM_DI_AV_ACTIVE) + { + /* there's AV activity on this link */ + if(p_data->acl_change.new_role == HCI_ROLE_SLAVE && bta_dm_cb.device_list.count > 1 + && p_data->acl_change.hci_status == HCI_SUCCESS) + { + /* more than one connections and the AV connection is role switched to slave + * switch it back to master and remove the switch policy */ + BTM_SwitchRole(p_bda, BTM_ROLE_MASTER, NULL); + need_policy_change = TRUE; + } + else if (bta_dm_cfg.avoid_scatter && (p_data->acl_change.new_role == HCI_ROLE_MASTER)) + { + /* if the link updated to be master include AV activities, remove the switch policy */ + need_policy_change = TRUE; + } + + if(need_policy_change) + { + bta_dm_policy_cback(BTA_SYS_PLCY_CLR, 0, HCI_ENABLE_MASTER_SLAVE_SWITCH, p_dev->peer_bdaddr); + } + } + else + { + /* there's AV no activity on this link and role switch happened + * check if AV is active + * if so, make sure the AV link is master */ + bta_dm_check_av(0); + } + bta_sys_notify_role_chg(p_data->acl_change.bd_addr, p_data->acl_change.new_role, p_data->acl_change.hci_status); + bdcpy(conn.role_chg.bd_addr, p_bda); + conn.role_chg.new_role = (UINT8) p_data->acl_change.new_role; + if( bta_dm_cb.p_sec_cback ) + bta_dm_cb.p_sec_cback(BTA_DM_ROLE_CHG_EVT, (tBTA_DM_SEC *)&conn); + } + return; + } +#endif + + /* Collision report from Stack: Notify profiles */ + if (p_data->acl_change.event == BTM_BL_COLLISION_EVT) + { + bta_sys_notify_collision (p_bda); + return; + } + + if(is_new) + { + for(i=0; i<bta_dm_cb.device_list.count; i++) + { + if(!bdcmp( bta_dm_cb.device_list.peer_device[i].peer_bdaddr, p_bda)) + break; + + } + + if(i == bta_dm_cb.device_list.count) + { + bdcpy(bta_dm_cb.device_list.peer_device[bta_dm_cb.device_list.count].peer_bdaddr, p_bda); + bta_dm_cb.device_list.peer_device[bta_dm_cb.device_list.count].link_policy = bta_dm_cb.cur_policy; + bta_dm_cb.device_list.count++; + } + + bta_dm_cb.device_list.peer_device[i].conn_state = BTA_DM_CONNECTED; + bta_dm_cb.device_list.peer_device[i].pref_role = BTA_ANY_ROLE; + bdcpy(conn.link_up.bd_addr, p_bda); + bta_dm_cb.device_list.peer_device[i].info = BTA_DM_DI_NONE; + if( ((NULL != (p = BTM_ReadLocalFeatures ())) && HCI_SNIFF_SUB_RATE_SUPPORTED(p)) && + ((NULL != (p = BTM_ReadRemoteFeatures (p_bda))) && HCI_SNIFF_SUB_RATE_SUPPORTED(p)) ) + { + /* both local and remote devices support SSR */ + bta_dm_cb.device_list.peer_device[i].info = BTA_DM_DI_USE_SSR; + } + APPL_TRACE_WARNING1("info:x%x", bta_dm_cb.device_list.peer_device[i].info); + if( bta_dm_cb.p_sec_cback ) + bta_dm_cb.p_sec_cback(BTA_DM_LINK_UP_EVT, (tBTA_DM_SEC *)&conn); + + } + else + { + for(i=0; i<bta_dm_cb.device_list.count; i++) + { + if(bdcmp( bta_dm_cb.device_list.peer_device[i].peer_bdaddr, p_bda)) + continue; + + for(; i<bta_dm_cb.device_list.count ; i++) + { + + memcpy(&bta_dm_cb.device_list.peer_device[i], &bta_dm_cb.device_list.peer_device[i+1], sizeof(bta_dm_cb.device_list.peer_device[i])); + + } + + break; + + } + if(bta_dm_cb.device_list.count) + bta_dm_cb.device_list.count--; + + if(bta_dm_search_cb.wait_disc && !bdcmp(bta_dm_search_cb.peer_bdaddr, p_bda)) + { + bta_dm_search_cb.wait_disc = FALSE; + + if(bta_dm_search_cb.sdp_results) + { + APPL_TRACE_EVENT0(" timer stopped "); + bta_sys_stop_timer(&bta_dm_search_cb.search_timer); + bta_dm_discover_next_device(); + } + + } + + if(bta_dm_cb.disabling) + { + + if(!BTM_GetNumAclLinks()) + { + bta_sys_stop_timer(&bta_dm_cb.disable_timer); + bta_dm_cb.disable_timer.p_cback = (TIMER_CBACK*)&bta_dm_disable_conn_down_timer_cback; + /* start a timer to make sure that the profiles + get the disconnect event */ + bta_sys_start_timer(&bta_dm_cb.disable_timer, 0, 1000); + } + + } + + bdcpy(conn.link_down.bd_addr, p_bda); + conn.link_down.status = (UINT8) btm_get_acl_disc_reason_code(); + if( bta_dm_cb.p_sec_cback ) + bta_dm_cb.p_sec_cback(BTA_DM_LINK_DOWN_EVT, (tBTA_DM_SEC *)&conn); + } + + bta_dm_adjust_roles(); +} + +/******************************************************************************* +** +** Function bta_dm_disable_conn_down_timer_cback +** +** Description Sends disable event to application +** +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_disable_conn_down_timer_cback (TIMER_LIST_ENT *p_tle) +{ + tBTA_SYS_HW_MSG *sys_enable_event; + + /* register our callback to SYS HW manager */ + bta_sys_hw_register( BTA_SYS_HW_BLUETOOTH, bta_dm_sys_hw_cback ); + + /* send a message to BTA SYS */ + if ((sys_enable_event = (tBTA_SYS_HW_MSG *) GKI_getbuf(sizeof(tBTA_SYS_HW_MSG))) != NULL) + { + sys_enable_event->hdr.event = BTA_SYS_API_DISABLE_EVT; + sys_enable_event->hw_module = BTA_SYS_HW_BLUETOOTH; + bta_sys_sendmsg(sys_enable_event); + } + + bta_dm_cb.disabling = FALSE; + +} + +/******************************************************************************* +** +** Function bta_dm_rssi_cback +** +** Description Callback from btm with rssi values +** +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_rssi_cback (tBTM_RSSI_RESULTS *p_result) +{ + + tBTA_DM_SIG_STRENGTH result; + + if(p_result->status == BTM_SUCCESS) + { + + bdcpy(result.bd_addr, p_result->rem_bda); + result.mask = BTA_SIG_STRENGTH_RSSI_MASK; + result.rssi_value = p_result->rssi; + if( bta_dm_cb.p_sec_cback!= NULL ) + bta_dm_cb.p_sec_cback(BTA_DM_SIG_STRENGTH_EVT, (tBTA_DM_SEC *)&result); + + } +} + +/******************************************************************************* +** +** Function bta_dm_link_quality_cback +** +** Description Callback from btm with link quality value +** +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_link_quality_cback (tBTM_LINK_QUALITY_RESULTS *p_result) +{ + + tBTA_DM_SIG_STRENGTH result; + + if(p_result->status == BTM_SUCCESS) + { + + bdcpy(result.bd_addr, p_result->rem_bda); + result.mask = BTA_SIG_STRENGTH_LINK_QUALITY_MASK; + result.link_quality_value = p_result->link_quality; + if( bta_dm_cb.p_sec_cback!= NULL ) + bta_dm_cb.p_sec_cback(BTA_DM_SIG_STRENGTH_EVT, (tBTA_DM_SEC *)&result); + + } +} + +/******************************************************************************* +** +** Function bta_dm_rm_cback +** +** Description Role management callback from sys +** +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_rm_cback(tBTA_SYS_CONN_STATUS status, UINT8 id, UINT8 app_id, BD_ADDR peer_addr) +{ + + UINT8 j; + tBTA_PREF_ROLES role; + tBTA_DM_PEER_DEVICE *p_dev; + + p_dev = bta_dm_find_peer_device(peer_addr); + if( status == BTA_SYS_CONN_OPEN) + { + if(p_dev) + { + p_dev->conn_state = BTA_DM_CONNECTED; + + for(j=1; j<= p_bta_dm_rm_cfg[0].app_id; j++) + { + if(((p_bta_dm_rm_cfg[j].app_id == app_id) || (p_bta_dm_rm_cfg[j].app_id == BTA_ALL_APP_ID)) + && (p_bta_dm_rm_cfg[j].id == id)) + { + role = p_bta_dm_rm_cfg[j].cfg; + + if(role > p_dev->pref_role ) + p_dev->pref_role = role; + break; + } + } + + } + + } + + if((BTA_ID_AV == id)||(BTA_ID_AVK ==id)) + { + if( status == BTA_SYS_CONN_BUSY) + { + if(p_dev) + p_dev->info |= BTA_DM_DI_AV_ACTIVE; + /* AV calls bta_sys_conn_open with the A2DP stream count as app_id */ + if(BTA_ID_AV == id) + bta_dm_cb.cur_av_count = app_id; + } + else if( status == BTA_SYS_CONN_IDLE) + { + if(p_dev) + p_dev->info &= ~BTA_DM_DI_AV_ACTIVE; + /* AV calls bta_sys_conn_open with the A2DP stream count as app_id */ + if(BTA_ID_AV == id) + bta_dm_cb.cur_av_count = app_id; + } + APPL_TRACE_WARNING2("bta_dm_rm_cback:%d, status:%d", bta_dm_cb.cur_av_count, status); + } + + + bta_dm_adjust_roles(); + +} + +/******************************************************************************* +** +** Function bta_dm_adjust_roles +** +** Description Adjust roles +** +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_adjust_roles(void) +{ + + UINT8 i; + BOOLEAN set_master_role = FALSE; + + if(bta_dm_cb.device_list.count) + { + + /* the configuration is no scatternet + * or AV connection exists and there are more than one ACL link */ + if( (p_bta_dm_rm_cfg[0].cfg == BTA_DM_NO_SCATTERNET) || + (bta_dm_cb.cur_av_count && bta_dm_cb.device_list.count > 1) ) + { + + L2CA_SetDesireRole (HCI_ROLE_MASTER); + set_master_role = TRUE; + + } + + for(i=0; i<bta_dm_cb.device_list.count; i++) + { + if(bta_dm_cb.device_list.peer_device[i].conn_state == BTA_DM_CONNECTED) + { + if(!set_master_role && (bta_dm_cb.device_list.peer_device[i].pref_role != BTA_ANY_ROLE) + && (p_bta_dm_rm_cfg[0].cfg == BTA_DM_PARTIAL_SCATTERNET)) + { + L2CA_SetDesireRole (HCI_ROLE_MASTER); + set_master_role = TRUE; + } + + if((bta_dm_cb.device_list.peer_device[i].pref_role == BTA_MASTER_ROLE_ONLY) + || (bta_dm_cb.device_list.count > 1)) + { + + BTM_SwitchRole (bta_dm_cb.device_list.peer_device[i].peer_bdaddr, HCI_ROLE_MASTER, NULL); + + } + + } + } + + + if(!set_master_role) + { + + L2CA_SetDesireRole (L2CAP_DESIRED_LINK_ROLE); + + } + + } + else + { + L2CA_SetDesireRole (L2CAP_DESIRED_LINK_ROLE); + } + + +} + +/******************************************************************************* +** +** Function bta_dm_get_remname +** +** Description Returns a pointer to the remote name stored in the DM control +** block if it exists, or from the BTM memory. +** +** Returns char * - Pointer to the remote device name +*******************************************************************************/ +static char *bta_dm_get_remname(void) +{ + char *p_name = bta_dm_search_cb.peer_name; + char *p_temp; + + /* If the name isn't already stored, try retrieving from BTM */ + if (*p_name == '\0') + if ((p_temp = BTM_SecReadDevName(bta_dm_search_cb.peer_bdaddr)) != NULL) + p_name = p_temp; + + return p_name; +} + +/******************************************************************************* +** +** Function bta_dm_bond_cancel_complete_cback +** +** Description Authentication complete callback from BTM +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_bond_cancel_complete_cback(tBTM_STATUS result) +{ + + tBTA_DM_SEC sec_event; + + if (result == BTM_SUCCESS) + sec_event.bond_cancel_cmpl.result = BTA_SUCCESS; + else + sec_event.bond_cancel_cmpl.result = BTA_FAILURE; + + if(bta_dm_cb.p_sec_cback) + { + bta_dm_cb.p_sec_cback(BTA_DM_BOND_CANCEL_CMPL_EVT, &sec_event); + } +} + +#if ( BTM_EIR_SERVER_INCLUDED == TRUE ) + #if ( BTA_EIR_CANNED_UUID_LIST != TRUE )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) +/******************************************************************************* +** +** Function bta_dm_update_eir_uuid +** +** Description +** +** +*******************************************************************************/ +void bta_dm_update_eir_uuid (tBTA_DM_MSG *p_data) +{ + tBTA_DM_API_UPDATE_EIR_UUID *p_msg = (tBTA_DM_API_UPDATE_EIR_UUID *)p_data; + UINT8 xx; + UINT8 empty_slot = BTA_EIR_SERVER_NUM_CUSTOM_UUID; + UINT8 match_slot = BTA_EIR_SERVER_NUM_CUSTOM_UUID; + + for (xx = 0; xx < BTA_EIR_SERVER_NUM_CUSTOM_UUID; xx++) + { + if (bta_dm_cb.custom_uuid[xx].len == 0) + { + if (empty_slot == BTA_EIR_SERVER_NUM_CUSTOM_UUID) + empty_slot = xx; + } + else if (match_slot == BTA_EIR_SERVER_NUM_CUSTOM_UUID) + { + if (!memcmp (bta_dm_cb.custom_uuid[xx].uu.uuid128, p_msg->uuid.uu.uuid128, p_msg->uuid.len)) + { + match_slot = xx;; + } + } + } + + if (p_msg->is_add) + { + if (match_slot == BTA_EIR_SERVER_NUM_CUSTOM_UUID) + { + if (empty_slot == BTA_EIR_SERVER_NUM_CUSTOM_UUID) + { + APPL_TRACE_ERROR0("No space to add UUID for EIR"); + return; + } + else + { + memcpy (&(bta_dm_cb.custom_uuid[empty_slot]), &(p_msg->uuid), sizeof(tBT_UUID)); + } + } + else + { + APPL_TRACE_ERROR0("UUID is already added for EIR"); + return; + } + } + else + { + if (match_slot == BTA_EIR_SERVER_NUM_CUSTOM_UUID) + { + APPL_TRACE_ERROR0("UUID is not found for EIR"); + return; + } + else + { + memset (&(bta_dm_cb.custom_uuid[match_slot]), 0, sizeof(tBT_UUID)); + } + } + + bta_dm_set_eir (NULL); +} + #endif + +/******************************************************************************* +** +** Function bta_dm_set_eir_config +** +** Description +** +** +*******************************************************************************/ +void bta_dm_set_eir_config (tBTA_DM_MSG *p_data) +{ + if (p_data->set_eir_cfg.p_eir_cfg) + { + /* User defined config */ + p_bta_dm_eir_cfg = p_data->set_eir_cfg.p_eir_cfg; + } + else + { + /* Back to default config */ + p_bta_dm_eir_cfg = (tBTA_DM_EIR_CONF*)&bta_dm_eir_cfg; + } + + bta_dm_set_eir (NULL); +} + +/******************************************************************************* +** +** Function bta_dm_set_eir +** +** Description This function creates EIR tagged data and writes it to controller. +** +** Returns None +** +*******************************************************************************/ +static void bta_dm_set_eir (char *local_name) +{ + BT_HDR *p_buf; + UINT8 *p; + UINT8 *p_length; +#if (BTA_EIR_CANNED_UUID_LIST != TRUE) + UINT8 *p_type; + UINT8 max_num_uuid; +#if (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) + UINT8 custom_uuid_idx; +#endif +#endif +#if (BTM_EIR_DEFAULT_FEC_REQUIRED == FALSE) + UINT8 free_eir_length = HCI_EXT_INQ_RESPONSE_LEN; +#else + UINT8 free_eir_length = HCI_DM5_PACKET_SIZE; +#endif + UINT8 num_uuid; + UINT8 data_type; + UINT8 local_name_len; + + /* wait until complete to disable */ + if (bta_dm_cb.disable_timer.in_use) + return; + +#if ( BTA_EIR_CANNED_UUID_LIST != TRUE ) + /* wait until App is ready */ + if (bta_dm_cb.app_ready_timer.in_use) + return; + + /* if local name is not provided, get it from controller */ + if( local_name == NULL ) + { + if( BTM_ReadLocalDeviceName( &local_name ) != BTM_SUCCESS ) + { + APPL_TRACE_ERROR0("Fail to read local device name for EIR"); + } + } +#endif + + /* Allocate a buffer to hold HCI command */ + if ((p_buf = (BT_HDR *)GKI_getpoolbuf(BTM_CMD_POOL_ID)) == NULL) + { + APPL_TRACE_ERROR0("bta_dm_set_eir couldn't allocate buffer"); + return; + } + p = (UINT8 *)p_buf + BTM_HCI_EIR_OFFSET; + + memset(p, 0x00, HCI_EXT_INQ_RESPONSE_LEN ); + + APPL_TRACE_DEBUG0("BTA is generating EIR"); + + if( local_name ) + local_name_len = strlen( local_name ); + else + local_name_len = 0; + + data_type = BTM_EIR_COMPLETE_LOCAL_NAME_TYPE; + /* if local name is longer than minimum length of shortened name */ + /* check whether it needs to be shortened or not */ + if( local_name_len > p_bta_dm_eir_cfg->bta_dm_eir_min_name_len ) + { + /* get number of UUID 16-bit list */ +#if (BTA_EIR_CANNED_UUID_LIST == TRUE) + num_uuid = p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len/LEN_UUID_16; +#else + max_num_uuid = (free_eir_length - 2)/LEN_UUID_16; + data_type = BTM_GetEirSupportedServices( bta_dm_cb.eir_uuid, &p, + max_num_uuid, &num_uuid ); + p = (UINT8 *)p_buf + BTM_HCI_EIR_OFFSET; /* reset p */ +#endif + + /* if UUID doesn't fit remaing space, shorten local name */ + if ( local_name_len > (free_eir_length - 4 - num_uuid*LEN_UUID_16)) + { + APPL_TRACE_WARNING0("BTA EIR: local name is shortened"); + local_name_len = p_bta_dm_eir_cfg->bta_dm_eir_min_name_len; + data_type = BTM_EIR_SHORTENED_LOCAL_NAME_TYPE; + } + else + data_type = BTM_EIR_COMPLETE_LOCAL_NAME_TYPE; + } + + UINT8_TO_STREAM(p, local_name_len + 1); + UINT8_TO_STREAM(p, data_type); + memcpy(p, local_name, local_name_len); + p += local_name_len; + free_eir_length -= local_name_len + 2; + +#if (BTA_EIR_CANNED_UUID_LIST == TRUE) + /* if UUID list is provided as static data in configuration */ + if(( p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len > 0 ) + &&(p_bta_dm_eir_cfg->bta_dm_eir_uuid16)) + { + if( free_eir_length > LEN_UUID_16 + 2) + { + free_eir_length -= 2; + + if( free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len) + { + num_uuid = p_bta_dm_eir_cfg->bta_dm_eir_uuid16_len / LEN_UUID_16; + data_type = BTM_EIR_COMPLETE_16BITS_UUID_TYPE; + } + else /* not enough room for all UUIDs */ + { + APPL_TRACE_WARNING0("BTA EIR: UUID 16-bit list is truncated"); + num_uuid = free_eir_length / LEN_UUID_16; + data_type = BTM_EIR_MORE_16BITS_UUID_TYPE; + } + UINT8_TO_STREAM(p, num_uuid * LEN_UUID_16 + 1); + UINT8_TO_STREAM(p, data_type); + memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_uuid16, num_uuid * LEN_UUID_16 ); + p += num_uuid * LEN_UUID_16; + free_eir_length -= num_uuid * LEN_UUID_16; + } + } +#else /* (BTA_EIR_CANNED_UUID_LIST == TRUE) */ + /* if UUID list is dynamic */ + if ( free_eir_length >= 2) + { + p_length = p++; + p_type = p++; + num_uuid = 0; + + max_num_uuid = (free_eir_length - 2)/LEN_UUID_16; + data_type = BTM_GetEirSupportedServices( bta_dm_cb.eir_uuid, &p, max_num_uuid, &num_uuid ); + + if( data_type == BTM_EIR_MORE_16BITS_UUID_TYPE ) + { + APPL_TRACE_WARNING0("BTA EIR: UUID 16-bit list is truncated"); + } +#if (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) + else + { + for (custom_uuid_idx = 0; custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID; custom_uuid_idx++) + { + if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_16) + { + if ( num_uuid < max_num_uuid ) + { + UINT16_TO_STREAM(p, bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid16); + num_uuid++; + } + else + { + data_type = BTM_EIR_MORE_16BITS_UUID_TYPE; + APPL_TRACE_WARNING0("BTA EIR: UUID 16-bit list is truncated"); + break; + } + } + } + } +#endif /* (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) */ + + UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_16 + 1); + UINT8_TO_STREAM(p_type, data_type); + free_eir_length -= num_uuid * LEN_UUID_16 + 2; + } +#endif /* (BTA_EIR_CANNED_UUID_LIST == TRUE) */ + +#if ( BTA_EIR_CANNED_UUID_LIST != TRUE )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) + /* Adding 32-bit UUID list */ + if ( free_eir_length >= 2) + { + p_length = p++; + p_type = p++; + num_uuid = 0; + data_type = BTM_EIR_COMPLETE_32BITS_UUID_TYPE; + + max_num_uuid = (free_eir_length - 2)/LEN_UUID_32; + + for (custom_uuid_idx = 0; custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID; custom_uuid_idx++) + { + if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_32) + { + if ( num_uuid < max_num_uuid ) + { + UINT32_TO_STREAM(p, bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid32); + num_uuid++; + } + else + { + data_type = BTM_EIR_MORE_32BITS_UUID_TYPE; + APPL_TRACE_WARNING0("BTA EIR: UUID 32-bit list is truncated"); + break; + } + } + } + + UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_32 + 1); + UINT8_TO_STREAM(p_type, data_type); + free_eir_length -= num_uuid * LEN_UUID_32 + 2; + } + + /* Adding 128-bit UUID list */ + if ( free_eir_length >= 2) + { + p_length = p++; + p_type = p++; + num_uuid = 0; + data_type = BTM_EIR_COMPLETE_128BITS_UUID_TYPE; + + max_num_uuid = (free_eir_length - 2)/LEN_UUID_128; + + for (custom_uuid_idx = 0; custom_uuid_idx < BTA_EIR_SERVER_NUM_CUSTOM_UUID; custom_uuid_idx++) + { + if (bta_dm_cb.custom_uuid[custom_uuid_idx].len == LEN_UUID_128) + { + if ( num_uuid < max_num_uuid ) + { + ARRAY16_TO_STREAM(p, bta_dm_cb.custom_uuid[custom_uuid_idx].uu.uuid128); + num_uuid++; + } + else + { + data_type = BTM_EIR_MORE_128BITS_UUID_TYPE; + APPL_TRACE_WARNING0("BTA EIR: UUID 128-bit list is truncated"); + break; + } + } + } + + UINT8_TO_STREAM(p_length, num_uuid * LEN_UUID_128 + 1); + UINT8_TO_STREAM(p_type, data_type); + free_eir_length -= num_uuid * LEN_UUID_128 + 2; + } +#endif /* ( BTA_EIR_CANNED_UUID_LIST != TRUE )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) */ + + /* if Flags are provided in configuration */ + if(( p_bta_dm_eir_cfg->bta_dm_eir_flag_len > 0 ) + &&( p_bta_dm_eir_cfg->bta_dm_eir_flags ) + &&( free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_flag_len + 2 )) + { + UINT8_TO_STREAM(p, p_bta_dm_eir_cfg->bta_dm_eir_flag_len + 1); + UINT8_TO_STREAM(p, BTM_EIR_FLAGS_TYPE); + memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_flags, + p_bta_dm_eir_cfg->bta_dm_eir_flag_len); + p += p_bta_dm_eir_cfg->bta_dm_eir_flag_len; + free_eir_length -= p_bta_dm_eir_cfg->bta_dm_eir_flag_len + 2; + } + + /* if Inquiry Tx Resp Power compiled */ + if((p_bta_dm_eir_cfg->bta_dm_eir_inq_tx_power) && + (free_eir_length >= 3)) + { + UINT8_TO_STREAM(p, 2); /* Length field */ + UINT8_TO_STREAM(p, BTM_EIR_TX_POWER_LEVEL_TYPE); + UINT8_TO_STREAM(p, *(p_bta_dm_eir_cfg->bta_dm_eir_inq_tx_power)); + free_eir_length -= 3; + } + + /* if Manufacturer Specific are provided in configuration */ + if(( p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len > 0 ) + &&( p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec ) + &&( free_eir_length >= p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 2 )) + { + p_length = p; + + UINT8_TO_STREAM(p, p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 1); + UINT8_TO_STREAM(p, BTM_EIR_MANUFACTURER_SPECIFIC_TYPE); + memcpy(p, p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec, + p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len); + p += p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len; + free_eir_length -= p_bta_dm_eir_cfg->bta_dm_eir_manufac_spec_len + 2; + + } + else + { + p_length = NULL; + } + + if( free_eir_length ) + UINT8_TO_STREAM(p, 0); /* terminator of significant part */ + + BTM_WriteEIR( p_buf ); + +} +#endif + +#if ( BTM_EIR_CLIENT_INCLUDED == TRUE ) +/******************************************************************************* +** +** Function bta_dm_eir_search_services +** +** Description This function searches services in received EIR +** +** Returns None +** +*******************************************************************************/ +static void bta_dm_eir_search_services( tBTM_INQ_RESULTS *p_result, + tBTA_SERVICE_MASK *p_services_to_search, + tBTA_SERVICE_MASK *p_services_found) +{ + tBTA_SERVICE_MASK service_index = 0; + tBTM_EIR_SEARCH_RESULT result; + + APPL_TRACE_DEBUG6("BTA searching services in EIR of BDA:0x%02X%02X%02X%02X%02X%02X", + p_result->remote_bd_addr[0],p_result->remote_bd_addr[1], + p_result->remote_bd_addr[2],p_result->remote_bd_addr[3], + p_result->remote_bd_addr[4],p_result->remote_bd_addr[5]); + + APPL_TRACE_DEBUG1(" with services_to_search=0x%08X", *p_services_to_search); + +#if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE + /* always do GATT based service discovery by SDP instead of from EIR */ + /* if GATT based service is also to be put in EIR, need to modify this */ + while (service_index < (BTA_MAX_SERVICE_ID - 1)) +#else + while(service_index < BTA_MAX_SERVICE_ID) +#endif + { + if( *p_services_to_search + & (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index))) + { + result = BTM_HasInquiryEirService( p_result, + bta_service_id_to_uuid_lkup_tbl[service_index] ); + + /* Searching for HSP v1.2 only device */ + if ((result != BTM_EIR_FOUND) && + (bta_service_id_to_uuid_lkup_tbl[service_index] == UUID_SERVCLASS_HEADSET)) + { + result = BTM_HasInquiryEirService (p_result, UUID_SERVCLASS_HEADSET_HS); + } + + if( result == BTM_EIR_FOUND ) + { + /* If Plug and Play service record, need to check to see if Broadcom stack */ + /* However, EIR data doesn't have EXT_BRCM_VERSION so just skip it */ + if( bta_service_id_to_uuid_lkup_tbl[service_index] + != UUID_SERVCLASS_PNP_INFORMATION ) + { + + *p_services_found |= + (tBTA_SERVICE_MASK)(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index)); + /* remove the service from services to be searched */ + *p_services_to_search &= + (tBTA_SERVICE_MASK)(~(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index))); + } + } + else if( result == BTM_EIR_NOT_FOUND ) + { + /* remove the service from services to be searched */ + *p_services_to_search &= + (tBTA_SERVICE_MASK)(~(BTA_SERVICE_ID_TO_SERVICE_MASK(service_index))); + } + } + + service_index++; + } + + APPL_TRACE_ERROR2("BTA EIR search result, services_to_search=0x%08X, services_found=0x%08X", + *p_services_to_search, *p_services_found); +} +#endif + +#if ( BTM_EIR_SERVER_INCLUDED == TRUE )&&(BTA_EIR_CANNED_UUID_LIST != TRUE) +/******************************************************************************* +** +** Function bta_dm_eir_update_uuid +** +** Description This function adds or removes service UUID in EIR database. +** +** Returns None +** +*******************************************************************************/ +void bta_dm_eir_update_uuid(UINT16 uuid16, BOOLEAN adding) +{ + /* if this UUID is not advertised in EIR */ + if( !BTM_HasEirService( p_bta_dm_eir_cfg->uuid_mask, uuid16 )) + return; + + if( adding ) + { + APPL_TRACE_EVENT1("Adding UUID=0x%04X into EIR", uuid16); + + BTM_AddEirService( bta_dm_cb.eir_uuid, uuid16 ); + } + else + { + APPL_TRACE_EVENT1("Removing UUID=0x%04X from EIR", uuid16); + + BTM_RemoveEirService( bta_dm_cb.eir_uuid, uuid16 ); + } + + bta_dm_set_eir (NULL); + + APPL_TRACE_EVENT2("bta_dm_eir_update_uuid UUID bit mask=0x%08X %08X", + bta_dm_cb.eir_uuid[1], bta_dm_cb.eir_uuid[0] ); +} +#endif + +/******************************************************************************* +** +** Function bta_dm_enable_test_mode +** +** Description enable test mode +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_enable_test_mode(tBTA_DM_MSG *p_data) +{ + BTM_EnableTestMode(); +} + +/******************************************************************************* +** +** Function bta_dm_disable_test_mode +** +** Description disable test mode +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_disable_test_mode(tBTA_DM_MSG *p_data) +{ + BTM_DeviceReset(NULL); +} + +/******************************************************************************* +** +** Function bta_dm_execute_callback +** +** Description Just execute a generic call back in the context of the BTU/BTA tack +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_execute_callback(tBTA_DM_MSG *p_data) +{ + /* sanity check */ + if(p_data->exec_cback.p_exec_cback == NULL) + { + return; + } + + p_data->exec_cback.p_exec_cback(p_data->exec_cback.p_param); +} +/******************************************************************************* +** +** Function bta_dm_encrypt_cback +** +** Description link encryption complete callback. +** +** Returns None +** +*******************************************************************************/ +void bta_dm_encrypt_cback(BD_ADDR bd_addr, void *p_ref_data, tBTM_STATUS result) +{ + tBTA_STATUS bta_status = BTA_SUCCESS; + tBTA_DM_ENCRYPT_CBACK *p_callback = bta_dm_cb.p_encrypt_cback; + + bta_dm_cb.p_encrypt_cback = NULL; + switch (result) + { + case BTM_SUCCESS: + break; + case BTM_WRONG_MODE: + bta_status = BTA_WRONG_MODE; + break; + case BTM_NO_RESOURCES: + bta_status = BTA_NO_RESOURCES; + break; + case BTM_BUSY: + bta_status = BTA_BUSY; + break; + default: + bta_status = BTA_FAILURE; + break; + } + + APPL_TRACE_DEBUG2("bta_dm_encrypt_cback status =%d p_callback=0x%x", bta_status, p_callback); + + if (p_callback) + { + (*p_callback)(bd_addr, bta_status); + } +} +/******************************************************************************* +** +** Function bta_dm_set_encryption +** +** Description This function to encrypt the link +** +** Returns None +** +*******************************************************************************/ +void bta_dm_set_encryption (tBTA_DM_MSG *p_data) +{ + + APPL_TRACE_DEBUG0("bta_dm_set_encryption"); //todo + if (!p_data->set_encryption.p_callback) + { + APPL_TRACE_ERROR0("bta_dm_set_encryption callback is not provided"); + return; + } + + if (bta_dm_cb.p_encrypt_cback) + { + (*p_data->set_encryption.p_callback)(p_data->set_encryption.bd_addr, BTA_BUSY); + return; + } + + + bta_dm_cb.p_encrypt_cback = p_data->set_encryption.p_callback; + bta_dm_cb.sec_act = p_data->set_encryption.sec_act; + BTM_SetEncryption(p_data->set_encryption.bd_addr, bta_dm_encrypt_cback, &bta_dm_cb.sec_act); +} + +/******************************************************************************* +** +** Function bta_dm_set_afh_channels +** +** Description set afh channels +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_set_afh_channels(tBTA_DM_MSG * p_data) +{ + + BTM_SetAfhChannels(p_data->set_afhchannels.first,p_data->set_afhchannels.last); +} + +/******************************************************************************* +** +** Function bta_dm_set_afh_channel_assesment +** +** Description set afh channel assesment +** +** +** Returns void +** +*******************************************************************************/ + +void bta_dm_set_afh_channel_assesment (tBTA_DM_MSG * p_data) +{ + BTM_SetAfhChannelAssessment(p_data->set_afh_channel_assessment.enable_or_disable); +} + +#if (BLE_INCLUDED == TRUE) +#if (SMP_INCLUDED == TRUE) +/******************************************************************************* +** +** Function bta_dm_ble_smp_cback +** +** Description Callback for BLE SMP +** +** +** Returns void +** +*******************************************************************************/ +static UINT8 bta_dm_ble_smp_cback (tBTM_LE_EVT event, BD_ADDR bda, tBTM_LE_EVT_DATA *p_data) +{ + tBTM_STATUS status = BTM_SUCCESS; + tBTA_DM_SEC sec_event; + + if (!bta_dm_cb.p_sec_cback) + return BTM_NOT_AUTHORIZED; + + memset(&sec_event, 0, sizeof(tBTA_DM_SEC)); + switch (event) + { + case BTM_LE_IO_REQ_EVT: +#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE) + + bta_dm_co_ble_io_req(bda, + &p_data->io_req.io_cap, + &p_data->io_req.oob_data, + &p_data->io_req.auth_req, + &p_data->io_req.max_key_size, + &p_data->io_req.init_keys, + &p_data->io_req.resp_keys); +#endif +#if BTM_OOB_INCLUDED == FALSE + status = BTM_SUCCESS; +#endif + APPL_TRACE_EVENT2("io mitm: %d oob_data:%d", p_data->io_req.auth_req, p_data->io_req.oob_data); + + break; + + case BTM_LE_SEC_REQUEST_EVT: + bdcpy(sec_event.ble_req.bd_addr, bda); + BCM_STRNCPY_S((char*)sec_event.ble_req.bd_name, sizeof(BD_NAME), bta_dm_get_remname(), (BD_NAME_LEN-1)); + bta_dm_cb.p_sec_cback(BTA_DM_BLE_SEC_REQ_EVT, &sec_event); + break; + + case BTM_LE_KEY_NOTIF_EVT: + bdcpy(sec_event.key_notif.bd_addr, bda); + BCM_STRNCPY_S((char*)sec_event.key_notif.bd_name, sizeof(BD_NAME), bta_dm_get_remname(), (BD_NAME_LEN-1)); + sec_event.key_notif.passkey = p_data->key_notif; + bta_dm_cb.p_sec_cback(BTA_DM_BLE_PASSKEY_NOTIF_EVT, &sec_event); + break; + + case BTM_LE_KEY_REQ_EVT: + bdcpy(sec_event.ble_req.bd_addr, bda); + bta_dm_cb.p_sec_cback(BTA_DM_BLE_PASSKEY_REQ_EVT, &sec_event); + break; + + case BTM_LE_OOB_REQ_EVT: + bdcpy(sec_event.ble_req.bd_addr, bda); + bta_dm_cb.p_sec_cback(BTA_DM_BLE_OOB_REQ_EVT, &sec_event); + break; + + case BTM_LE_KEY_EVT: + bdcpy(sec_event.ble_key.bd_addr, bda); + sec_event.ble_key.key_type = p_data->key.key_type; + memcpy(&sec_event.ble_key.key_value, p_data->key.p_key_value, sizeof(tBTM_LE_KEY_VALUE)); + bta_dm_cb.p_sec_cback(BTA_DM_BLE_KEY_EVT, &sec_event); + break; + + case BTM_LE_COMPLT_EVT: + bdcpy(sec_event.auth_cmpl.bd_addr, bda); + if (p_data->complt.reason != 0) + sec_event.auth_cmpl.fail_reason = BTA_DM_AUTH_CONVERT_SMP_CODE(((UINT8)p_data->complt.reason)); + else + sec_event.auth_cmpl.success = TRUE; + + if (bta_dm_cb.p_sec_cback) + { + //bta_dm_cb.p_sec_cback(BTA_DM_AUTH_CMPL_EVT, &sec_event); + bta_dm_cb.p_sec_cback(BTA_DM_BLE_AUTH_CMPL_EVT, &sec_event); + } + + break; + + default: + status = BTM_NOT_AUTHORIZED; + break; + } + return status; +} +#endif /* SMP_INCLUDED == TRUE */ + +/******************************************************************************* +** +** Function bta_dm_ble_id_key_cback +** +** Description Callback for BLE local ID keys +** +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_ble_id_key_cback (UINT8 key_type, tBTM_BLE_LOCAL_KEYS *p_key) +{ + UINT8 evt; + tBTA_DM_SEC dm_key; + + switch (key_type) + { + case BTM_BLE_KEY_TYPE_ID: + case BTM_BLE_KEY_TYPE_ER: + if (bta_dm_cb.p_sec_cback) + { + memcpy(&dm_key.ble_id_keys, p_key, sizeof(tBTM_BLE_LOCAL_KEYS)); + + evt = (key_type == BTM_BLE_KEY_TYPE_ID) ? BTA_DM_BLE_LOCAL_IR_EVT :\ + BTA_DM_BLE_LOCAL_ER_EVT; + bta_dm_cb.p_sec_cback(evt, &dm_key); + } + break; + + default: + APPL_TRACE_DEBUG1("Unknown key type %d", key_type); + break; + } + return; + +} + +/******************************************************************************* +** +** Function bta_dm_add_blekey +** +** Description This function adds an BLE Key to an security database entry. +** This function shall only be called AFTER BTA_DmAddBleDevice has been called. +** It is normally called during host startup to restore all required information +** stored in the NVRAM. +** +** Parameters: +** +*******************************************************************************/ +void bta_dm_add_blekey (tBTA_DM_MSG *p_data) +{ + if (!BTM_SecAddBleKey (p_data->add_ble_key.bd_addr, + (tBTM_LE_KEY_VALUE *)&p_data->add_ble_key.blekey, + p_data->add_ble_key.key_type)) + { + APPL_TRACE_ERROR2 ("BTA_DM: Error adding BLE Key for device %08x%04x", + (p_data->add_ble_key.bd_addr[0]<<24)+(p_data->add_ble_key.bd_addr[1]<<16)+\ + (p_data->add_ble_key.bd_addr[2]<<8)+p_data->add_ble_key.bd_addr[3], + (p_data->add_ble_key.bd_addr[4]<<8)+p_data->add_ble_key.bd_addr[5]); + } +} + +/******************************************************************************* +** +** Function bta_dm_add_ble_device +** +** Description This function adds an BLE device to an security database entry. +** It is normally called during host startup to restore all required information +** stored in the NVRAM. +** +** Parameters: +** +*******************************************************************************/ +void bta_dm_add_ble_device (tBTA_DM_MSG *p_data) +{ + if (!BTM_SecAddBleDevice (p_data->add_ble_device.bd_addr, NULL, + p_data->add_ble_device.dev_type , + p_data->add_ble_device.addr_type)) + { + APPL_TRACE_ERROR2 ("BTA_DM: Error adding BLE Device for device %08x%04x", + (p_data->add_ble_device.bd_addr[0]<<24)+(p_data->add_ble_device.bd_addr[1]<<16)+ \ + (p_data->add_ble_device.bd_addr[2]<<8)+p_data->add_ble_device.bd_addr[3], + (p_data->add_ble_device.bd_addr[4]<<8)+p_data->add_ble_device.bd_addr[5]); + } +} + +/******************************************************************************* +** +** Function bta_dm_add_ble_device +** +** Description This function adds an BLE device to an security database entry. +** It is normally called during host startup to restore all required information +** stored in the NVRAM. +** +** Parameters: +** +*******************************************************************************/ +void bta_dm_ble_passkey_reply (tBTA_DM_MSG *p_data) +{ + if (p_data->pin_reply.accept) + { + + BTM_BlePasskeyReply(p_data->ble_passkey_reply.bd_addr, BTM_SUCCESS, p_data->ble_passkey_reply.passkey); + } + else + { + BTM_BlePasskeyReply(p_data->ble_passkey_reply.bd_addr, BTM_NOT_AUTHORIZED, p_data->ble_passkey_reply.passkey); + } + +} + +/******************************************************************************* +** +** Function bta_dm_security_grant +** +** Description This function grant SMP security request access. +** +** Parameters: +** +*******************************************************************************/ +void bta_dm_security_grant (tBTA_DM_MSG *p_data) +{ + BTM_SecurityGrant(p_data->ble_sec_grant.bd_addr, p_data->ble_sec_grant.res); +} + +/******************************************************************************* +** +** Function bta_dm_ble_set_bg_conn_type +** +** Description This function set the BLE background connection type +** +** Parameters: +** +*******************************************************************************/ +void bta_dm_ble_set_bg_conn_type (tBTA_DM_MSG *p_data) +{ + BTM_BleSetBgConnType(p_data->ble_set_bd_conn_type.bg_conn_type, + p_data->ble_set_bd_conn_type.p_select_cback); +} + +/******************************************************************************* +** +** Function bta_dm_ble_set_conn_params +** +** Description This function set the preferred connection parameters. +** +** Parameters: +** +*******************************************************************************/ +void bta_dm_ble_set_conn_params (tBTA_DM_MSG *p_data) +{ + BTM_BleSetPrefConnParams(p_data->ble_set_conn_params.peer_bda, + p_data->ble_set_conn_params.conn_int_min, + p_data->ble_set_conn_params.conn_int_max, + p_data->ble_set_conn_params.slave_latency, + p_data->ble_set_conn_params.supervision_tout); +} + +/******************************************************************************* +** +** Function bta_dm_ble_set_scan_params +** +** Description This function set the preferred connection scan parameters. +** +** Parameters: +** +*******************************************************************************/ +void bta_dm_ble_set_scan_params (tBTA_DM_MSG *p_data) +{ + BTM_BleSetConnScanParams(p_data->ble_set_scan_params.scan_int, + p_data->ble_set_scan_params.scan_window); +} + +#if ((defined BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)) + +/******************************************************************************* +** +** Function btm_dm_start_disc_gatt_services +** +** Description This function starts a GATT service search request. +** +** Parameters: +** +*******************************************************************************/ +static void btm_dm_start_disc_gatt_services (UINT16 conn_id) +{ + tBT_UUID *p_uuid = bta_dm_search_cb.p_srvc_uuid + + bta_dm_search_cb.num_uuid - bta_dm_search_cb.uuid_to_search; + + p_uuid = bta_dm_search_cb.p_srvc_uuid + + bta_dm_search_cb.num_uuid - bta_dm_search_cb.uuid_to_search; + + /* always search for all services */ + BTA_GATTC_ServiceSearchRequest(conn_id, p_uuid); +} + +/******************************************************************************* +** +** Function bta_dm_gatt_disc_result +** +** Description This function process the GATT service search result. +** +** Parameters: +** +*******************************************************************************/ +static void bta_dm_gatt_disc_result(tBTA_GATT_ID service_id) +{ + tBTA_DM_SEARCH result; + + /* + * This logic will not work for gatt case. We are checking against the bluetooth profiles here + * just copy the GATTID in raw data field and send it across. + */ + + + if ( bta_dm_search_cb.ble_raw_used + sizeof(tBTA_GATT_ID) < bta_dm_search_cb.ble_raw_size ) + { + APPL_TRACE_DEBUG3("ADDING BLE SERVICE uuid=0x%x, ble_ptr = 0x%x, ble_raw_used = 0x%x", service_id.uuid.uu.uuid16,bta_dm_search_cb.p_ble_rawdata,bta_dm_search_cb.ble_raw_used); + + memcpy((bta_dm_search_cb.p_ble_rawdata + bta_dm_search_cb.ble_raw_used), &service_id, + sizeof(service_id) ); + + bta_dm_search_cb.ble_raw_used += sizeof(service_id); + + } + else + { + APPL_TRACE_ERROR3("%s out of room to accomodate more service ids ble_raw_size = %d ble_raw_used = %d", __FUNCTION__,bta_dm_search_cb.ble_raw_size, bta_dm_search_cb.ble_raw_used ); + } + + APPL_TRACE_ERROR1("bta_dm_gatt_disc_result serivce_id len=%d ", service_id.uuid.len); + if ( bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) + { + + /* send result back to app now, one by one */ + bdcpy (result.disc_ble_res.bd_addr, bta_dm_search_cb.peer_bdaddr); + BCM_STRNCPY_S((char*)result.disc_ble_res.bd_name, sizeof(BD_NAME), bta_dm_get_remname(), (BD_NAME_LEN-1)); + memcpy(&result.disc_ble_res.service, &service_id.uuid, sizeof(tBT_UUID)); + + bta_dm_search_cb.p_search_cback(BTA_DM_DISC_BLE_RES_EVT, &result); + } +} + +/******************************************************************************* +** +** Function bta_dm_gatt_disc_complete +** +** Description This function process the GATT service search complete. +** +** Parameters: +** +*******************************************************************************/ +static void bta_dm_gatt_disc_complete(UINT16 conn_id, tBTA_GATT_STATUS status) +{ + tBTA_DM_MSG *p_msg; + + APPL_TRACE_DEBUG1("bta_dm_gatt_disc_complete conn_id = %d",conn_id); + + if (bta_dm_search_cb.uuid_to_search > 0) bta_dm_search_cb.uuid_to_search --; + + if (status == BTA_GATT_OK && bta_dm_search_cb.uuid_to_search > 0) + { + btm_dm_start_disc_gatt_services(conn_id); + } + else + { + bta_dm_search_cb.uuid_to_search = 0; + + /* no more services to be discovered */ + if ((p_msg = (tBTA_DM_MSG *) GKI_getbuf(sizeof(tBTA_DM_MSG))) != NULL) + { + p_msg->hdr.event = BTA_DM_DISCOVERY_RESULT_EVT; + p_msg->disc_result.result.disc_res.result = (status == BTA_GATT_OK) ? BTA_SUCCESS :BTA_FAILURE; + p_msg->disc_result.result.disc_res.services = bta_dm_search_cb.services_found; + bdcpy (p_msg->disc_result.result.disc_res.bd_addr, bta_dm_search_cb.peer_bdaddr); + BCM_STRNCPY_S((char*)p_msg->disc_result.result.disc_res.bd_name, sizeof(BD_NAME), + bta_dm_get_remname(), (BD_NAME_LEN-1)); + + /* make sure the string is terminated */ + p_msg->disc_result.result.disc_res.bd_name[BD_NAME_LEN-1] = 0; + + p_msg->disc_result.result.disc_res.device_type = BT_DEVICE_TYPE_BLE; + if ( bta_dm_search_cb.ble_raw_used > 0 ) + { + p_msg->disc_result.result.disc_res.p_raw_data = GKI_getbuf(bta_dm_search_cb.ble_raw_used); + + memcpy( p_msg->disc_result.result.disc_res.p_raw_data, + bta_dm_search_cb.p_ble_rawdata, + bta_dm_search_cb.ble_raw_used ); + + p_msg->disc_result.result.disc_res.raw_data_size = bta_dm_search_cb.ble_raw_used; + } + else + { + p_msg->disc_result.result.disc_res.p_raw_data = NULL; + bta_dm_search_cb.p_ble_rawdata = 0; + } + + bta_sys_sendmsg(p_msg); + } + if (conn_id != BTA_GATT_INVALID_CONN_ID) + { + BTA_GATTC_Close(conn_id); + } + bta_dm_search_cb.conn_id = BTA_GATT_INVALID_CONN_ID; + bta_dm_search_cb.gatt_disc_active = FALSE; + } +} + +/******************************************************************************* +** +** Function btm_dm_start_gatt_discovery +** +** Description This is GATT initiate the service search by open a GATT connection +** first. +** +** Parameters: +** +*******************************************************************************/ +void btm_dm_start_gatt_discovery (BD_ADDR bd_addr) +{ + bta_dm_search_cb.gatt_disc_active = TRUE; + BTA_GATTC_Open(bta_dm_search_cb.client_if, bd_addr, TRUE); +} + +/******************************************************************************* +** +** Function bta_dm_cancel_gatt_discovery +** +** Description This is GATT cancel the GATT service search. +** +** Parameters: +** +*******************************************************************************/ +static void bta_dm_cancel_gatt_discovery(BD_ADDR bd_addr) +{ + if (bta_dm_search_cb.conn_id == BTA_GATT_INVALID_CONN_ID) + { + BTA_GATTC_CancelOpen(bta_dm_search_cb.client_if, bd_addr, TRUE); + } + + bta_dm_gatt_disc_complete(bta_dm_search_cb.conn_id, (tBTA_GATT_STATUS) BTA_GATT_ERROR); +} + +/******************************************************************************* +** +** Function bta_dm_proc_open_evt +** +** Description process BTA_GATTC_OPEN_EVT in DM. +** +** Parameters: +** +*******************************************************************************/ +void bta_dm_proc_open_evt(tBTA_GATTC_OPEN *p_data) +{ + UINT8 *p1; + UINT8 *p2; + + p1 = bta_dm_search_cb.peer_bdaddr; + p2 = p_data->remote_bda; + + APPL_TRACE_DEBUG5("DM Search state= %d search_cb.peer_dbaddr: [%08x%04x] connected_bda= [%08x%04x] ", + bta_dm_search_cb.state, + ((p1[0])<<24)+((p1[1])<<16)+((p1[2])<<8)+(p1[3]), + ((p1[4])<<8)+ p1[5], + ((p2[0])<<24)+((p2[1])<<16)+((p2[2])<<8)+(p2[3]), + ((p2[4])<<8)+ p2[5]); + + APPL_TRACE_DEBUG3("BTA_GATTC_OPEN_EVT conn_id = %d client_if=%d status = %d" , + p_data->conn_id, + p_data->client_if, + p_data->status); + + bta_dm_search_cb.conn_id = p_data->conn_id; + + if (p_data->status == BTA_GATT_OK) + { + btm_dm_start_disc_gatt_services(p_data->conn_id); + } + else + { + bta_dm_gatt_disc_complete(BTA_GATT_INVALID_CONN_ID, p_data->status); + } +} + +/******************************************************************************* +** +** Function bta_dm_gattc_callback +** +** Description This is GATT client callback function used in DM. +** +** Parameters: +** +*******************************************************************************/ +static void bta_dm_gattc_callback(tBTA_GATTC_EVT event, tBTA_GATTC *p_data) +{ + APPL_TRACE_DEBUG1("bta_dm_gattc_callback event = %d", event); + + switch (event) + { + case BTA_GATTC_REG_EVT: + APPL_TRACE_DEBUG1("BTA_GATTC_REG_EVT client_if = %d", p_data->reg_oper.client_if); + if (p_data->reg_oper.status == BTA_GATT_OK) + bta_dm_search_cb.client_if = p_data->reg_oper.client_if; + else + bta_dm_search_cb.client_if = BTA_GATTS_INVALID_IF; + break; + + case BTA_GATTC_OPEN_EVT: + bta_dm_proc_open_evt(&p_data->open); + break; + + case BTA_GATTC_SEARCH_RES_EVT: + bta_dm_gatt_disc_result(p_data->srvc_res.service_uuid.id); + break; + + case BTA_GATTC_SEARCH_CMPL_EVT: + if ( bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) + bta_dm_gatt_disc_complete(p_data->search_cmpl.conn_id, p_data->search_cmpl.status); + break; + + case BTA_GATTC_CLOSE_EVT: + APPL_TRACE_DEBUG1("BTA_GATTC_CLOSE_EVT reason = %d", p_data->close.reason); + /* in case of disconnect before search is completed */ + if ( (bta_dm_search_cb.state != BTA_DM_SEARCH_IDLE) && + !memcmp(p_data->close.remote_bda, bta_dm_search_cb.peer_bdaddr, BD_ADDR_LEN)) + { + bta_dm_gatt_disc_complete((UINT16)BTA_GATT_INVALID_CONN_ID, (tBTA_GATT_STATUS) BTA_GATT_ERROR); + } + break; + + default: + break; + } +} + +#endif /* BTA_GATT_INCLUDED */ + +#endif /* BLE_INCLUDED */ diff --git a/bta/dm/bta_dm_api.c b/bta/dm/bta_dm_api.c new file mode 100644 index 0000000..97e4777 --- /dev/null +++ b/bta/dm/bta_dm_api.c @@ -0,0 +1,1602 @@ +/***************************************************************************** +** +** Name: bta_dm_api.c +** +** Description: This is the API implementation file for the BTA +** device manager. +** +** Copyright (c) 2003-2011, Broadcom Corp., All Rights Reserved. +** Broadcom Bluetooth Core. Proprietary and confidential. +** +*****************************************************************************/ + +#include "gki.h" +#include "bd.h" +#include "bta_sys.h" +#include "bta_api.h" +#include "bta_dm_int.h" +#include "bta_sys_int.h" +#include "btm_api.h" +#include "btm_int.h" +#include <string.h> + +/***************************************************************************** +** Constants +*****************************************************************************/ + +static const tBTA_SYS_REG bta_dm_reg = +{ + bta_dm_sm_execute, + bta_dm_sm_disable +}; + +static const tBTA_SYS_REG bta_dm_search_reg = +{ + bta_dm_search_sm_execute, + bta_dm_search_sm_disable +}; + +/******************************************************************************* +** +** Function BTA_EnableBluetooth +** +** Description Enables bluetooth service. This function must be +** called before any other functions in the BTA API are called. +** +** +** Returns tBTA_STATUS +** +*******************************************************************************/ +tBTA_STATUS BTA_EnableBluetooth(tBTA_DM_SEC_CBACK *p_cback) +{ + + tBTA_DM_API_ENABLE *p_msg; + + /* Bluetooth disabling is in progress */ + if (bta_dm_cb.disabling) + return BTA_FAILURE; + + memset(&bta_dm_cb, 0, sizeof(bta_dm_cb)); + + GKI_sched_lock(); + bta_sys_register (BTA_ID_DM, &bta_dm_reg ); + bta_sys_register (BTA_ID_DM_SEARCH, &bta_dm_search_reg ); + + /* if UUID list is not provided as static data */ + bta_sys_eir_register(bta_dm_eir_update_uuid); + + GKI_sched_unlock(); + + if ((p_msg = (tBTA_DM_API_ENABLE *) GKI_getbuf(sizeof(tBTA_DM_API_ENABLE))) != NULL) + { + p_msg->hdr.event = BTA_DM_API_ENABLE_EVT; + p_msg->p_sec_cback = p_cback; + bta_sys_sendmsg(p_msg); + return BTA_SUCCESS; + } + return BTA_FAILURE; + +} + +/******************************************************************************* +** +** Function BTA_DisableBluetooth +** +** Description Disables bluetooth service. This function is called when +** the application no longer needs bluetooth service +** +** Returns void +** +*******************************************************************************/ +tBTA_STATUS BTA_DisableBluetooth(void) +{ + + BT_HDR *p_msg; + + if ((p_msg = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL) + { + p_msg->event = BTA_DM_API_DISABLE_EVT; + bta_sys_sendmsg(p_msg); + } + else + { + return BTA_FAILURE; + } + + return BTA_SUCCESS; +} + +/******************************************************************************* +** +** Function BTA_EnableTestMode +** +** Description Enables bluetooth device under test mode +** +** +** Returns tBTA_STATUS +** +*******************************************************************************/ +tBTA_STATUS BTA_EnableTestMode(void) +{ + BT_HDR *p_msg; + + APPL_TRACE_API0("BTA_EnableTestMode"); + + if ((p_msg = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL) + { + p_msg->event = BTA_DM_API_ENABLE_TEST_MODE_EVT; + bta_sys_sendmsg(p_msg); + return BTA_SUCCESS; + } + return BTA_FAILURE; +} + +/******************************************************************************* +** +** Function BTA_DisableTestMode +** +** Description Disable bluetooth device under test mode +** +** +** Returns None +** +*******************************************************************************/ +void BTA_DisableTestMode(void) +{ + BT_HDR *p_msg; + + APPL_TRACE_API0("BTA_DisableTestMode"); + + if ((p_msg = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL) + { + p_msg->event = BTA_DM_API_DISABLE_TEST_MODE_EVT; + bta_sys_sendmsg(p_msg); + } +} + +/******************************************************************************* +** +** Function BTA_DmIsDeviceUp +** +** Description Called during startup to check whether the bluetooth module +** is up and ready +** +** Returns BOOLEAN +** +*******************************************************************************/ +BOOLEAN BTA_DmIsDeviceUp(void) +{ + + BOOLEAN status; + + GKI_sched_lock(); + status = BTM_IsDeviceUp(); + GKI_sched_unlock(); + return status; + +} + +/******************************************************************************* +** +** Function BTA_DmSetDeviceName +** +** Description This function sets the Bluetooth name of local device +** +** +** Returns void +** +*******************************************************************************/ +void BTA_DmSetDeviceName(char *p_name) +{ + + tBTA_DM_API_SET_NAME *p_msg; + + if ((p_msg = (tBTA_DM_API_SET_NAME *) GKI_getbuf(sizeof(tBTA_DM_API_SET_NAME))) != NULL) + { + p_msg->hdr.event = BTA_DM_API_SET_NAME_EVT; + /* truncate the name if needed */ + BCM_STRNCPY_S(p_msg->name, sizeof(p_msg->name), p_name, BD_NAME_LEN-1); + p_msg->name[BD_NAME_LEN-1]=0; + + bta_sys_sendmsg(p_msg); + } + + +} + +/******************************************************************************* +** +** Function BTA_DmSetVisibility +** +** Description This function sets the Bluetooth connectable, +** discoverable, pairable and conn paired only modes of local device +** +** +** Returns void +** +*******************************************************************************/ +void BTA_DmSetVisibility(tBTA_DM_DISC disc_mode, tBTA_DM_CONN conn_mode, UINT8 pairable_mode, UINT8 conn_filter ) +{ + + tBTA_DM_API_SET_VISIBILITY *p_msg; + + if ((p_msg = (tBTA_DM_API_SET_VISIBILITY *) GKI_getbuf(sizeof(tBTA_DM_MSG))) != NULL) + { + p_msg->hdr.event = BTA_DM_API_SET_VISIBILITY_EVT; + p_msg->disc_mode = disc_mode; + p_msg->conn_mode = conn_mode; + p_msg->pair_mode = pairable_mode; + p_msg->conn_paired_only = conn_filter; + + + bta_sys_sendmsg(p_msg); + } + + +} + +/******************************************************************************* +** +** Function BTA_DmSetScanParam +** +** Description This function sets the parameters for page scan and +** inquiry scan. +** +** +** Returns void +** +*******************************************************************************/ +void BTA_DmSetScanParam (UINT16 page_scan_interval, UINT16 page_scan_window, + UINT16 inquiry_scan_interval, UINT16 inquiry_scan_window) +{ + APPL_TRACE_API4 ("BTA_DmSetScanParam: %d, %d, %d, %d", + page_scan_interval, page_scan_window, + inquiry_scan_interval, inquiry_scan_window); + + bta_dm_cb.page_scan_interval = page_scan_interval; + bta_dm_cb.page_scan_window = page_scan_window; + bta_dm_cb.inquiry_scan_interval = inquiry_scan_interval; + bta_dm_cb.inquiry_scan_window = inquiry_scan_window; +} + +/******************************************************************************* +** +** Function BTA_DmSetAfhChannels +** +** Description This function sets the AFH first and +** last disable channel, so channels within +** that range are disabled. +** +** Returns void +** +*******************************************************************************/ +void BTA_DmSetAfhChannels(UINT8 first, UINT8 last) +{ + + tBTA_DM_API_SET_AFH_CHANNELS_EVT *p_msg; + + if ((p_msg = (tBTA_DM_API_SET_AFH_CHANNELS_EVT *) GKI_getbuf(sizeof(tBTA_DM_MSG))) != NULL) + { + p_msg->hdr.event = BTA_DM_API_SET_AFH_CHANNELS_EVT; + p_msg->first = first; + p_msg->last = last; + bta_sys_sendmsg(p_msg); + } + + +} + +/******************************************************************************* +** +** Function BTA_SetAfhChannelAssessment +** +** Description This function is called to set the channel assessment mode on or off +** +** Returns status +** +*******************************************************************************/ +void BTA_DmSetAfhChannelAssessment (BOOLEAN enable_or_disable) +{ + tBTA_DM_API_SET_AFH_CHANNEL_ASSESSMENT *p_msg; + + if ((p_msg = (tBTA_DM_API_SET_AFH_CHANNEL_ASSESSMENT *) GKI_getbuf(sizeof(tBTA_DM_API_SET_AFH_CHANNEL_ASSESSMENT))) != NULL) + { + p_msg->hdr.event = BTA_DM_API_SET_AFH_CHANNEL_ASSESMENT_EVT; + p_msg->enable_or_disable = enable_or_disable; + bta_sys_sendmsg(p_msg); + } +} + +/******************************************************************************* +** +** Function BTA_DmVendorSpecificCommand +** +** Description This function sends the vendor specific command +** to the controller +** +** +** Returns tBTA_STATUS +** +*******************************************************************************/ +tBTA_STATUS BTA_DmVendorSpecificCommand (UINT16 opcode, UINT8 param_len, + UINT8 *p_param_buf, + tBTA_VENDOR_CMPL_CBACK *p_cback) +{ + + tBTA_DM_API_VENDOR_SPECIFIC_COMMAND *p_msg; + UINT16 size; + + /* If p_cback is NULL, Notify application */ + if (p_cback == NULL) + { + return (BTA_FAILURE); + } + else + { + size = sizeof (tBTA_DM_API_VENDOR_SPECIFIC_COMMAND) + param_len; + if ((p_msg = (tBTA_DM_API_VENDOR_SPECIFIC_COMMAND *) GKI_getbuf(size)) != NULL) + { + p_msg->hdr.event = BTA_DM_API_VENDOR_SPECIFIC_COMMAND_EVT; + p_msg->opcode = opcode; + p_msg->param_len = param_len; + p_msg->p_param_buf = (UINT8 *)(p_msg + 1); + p_msg->p_cback = p_cback; + + memcpy (p_msg->p_param_buf, p_param_buf, param_len); + + bta_sys_sendmsg(p_msg); + } + return (BTA_SUCCESS); + } +} +/******************************************************************************* +** +** Function BTA_DmSearch +** +** Description This function searches for peer Bluetooth devices. It performs +** an inquiry and gets the remote name for devices. Service +** discovery is done if services is non zero +** +** +** Returns void +** +*******************************************************************************/ +void BTA_DmSearch(tBTA_DM_INQ *p_dm_inq, tBTA_SERVICE_MASK services, tBTA_DM_SEARCH_CBACK *p_cback) +{ + + tBTA_DM_API_SEARCH *p_msg; + + if ((p_msg = (tBTA_DM_API_SEARCH *) GKI_getbuf(sizeof(tBTA_DM_API_SEARCH))) != NULL) + { + memset(p_msg, 0, sizeof(tBTA_DM_API_SEARCH)); + + p_msg->hdr.event = BTA_DM_API_SEARCH_EVT; + memcpy(&p_msg->inq_params, p_dm_inq, sizeof(tBTA_DM_INQ)); + p_msg->services = services; + p_msg->p_cback = p_cback; + p_msg->rs_res = BTA_DM_RS_NONE; + bta_sys_sendmsg(p_msg); + } + +} + + +/******************************************************************************* +** +** Function BTA_DmSearchCancel +** +** Description This function cancels a search initiated by BTA_DmSearch +** +** +** Returns void +** +*******************************************************************************/ +void BTA_DmSearchCancel(void) +{ + BT_HDR *p_msg; + + if ((p_msg = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL) + { + p_msg->event = BTA_DM_API_SEARCH_CANCEL_EVT; + bta_sys_sendmsg(p_msg); + } + +} + +/******************************************************************************* +** +** Function BTA_DmDiscover +** +** Description This function does service discovery for services of a +** peer device +** +** +** Returns void +** +*******************************************************************************/ +void BTA_DmDiscover(BD_ADDR bd_addr, tBTA_SERVICE_MASK services, + tBTA_DM_SEARCH_CBACK *p_cback, BOOLEAN sdp_search) +{ + tBTA_DM_API_DISCOVER *p_msg; + + if ((p_msg = (tBTA_DM_API_DISCOVER *) GKI_getbuf(sizeof(tBTA_DM_API_DISCOVER))) != NULL) + { + memset(p_msg, 0, sizeof(tBTA_DM_API_DISCOVER)); + + p_msg->hdr.event = BTA_DM_API_DISCOVER_EVT; + bdcpy(p_msg->bd_addr, bd_addr); + p_msg->services = services; + p_msg->p_cback = p_cback; + p_msg->sdp_search = sdp_search; + bta_sys_sendmsg(p_msg); + } + +} + +/******************************************************************************* +** +** Function BTA_DmDiscoverUUID +** +** Description This function does service discovery for services of a +** peer device +** +** +** Returns void +** +*******************************************************************************/ +void BTA_DmDiscoverUUID(BD_ADDR bd_addr, tSDP_UUID *uuid, + tBTA_DM_SEARCH_CBACK *p_cback, BOOLEAN sdp_search) +{ + tBTA_DM_API_DISCOVER *p_msg; + + if ((p_msg = (tBTA_DM_API_DISCOVER *) GKI_getbuf(sizeof(tBTA_DM_API_DISCOVER))) != NULL) + { + p_msg->hdr.event = BTA_DM_API_DISCOVER_EVT; + bdcpy(p_msg->bd_addr, bd_addr); + p_msg->services = BTA_USER_SERVICE_MASK; //Not exposed at API level + p_msg->p_cback = p_cback; + p_msg->sdp_search = sdp_search; + +#if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE + p_msg->num_uuid = 0; + p_msg->p_uuid = NULL; +#endif + memcpy( &p_msg->uuid, uuid, sizeof(tSDP_UUID) ); + bta_sys_sendmsg(p_msg); + } + +} +/******************************************************************************* +** +** Function BTA_DmIsMaster +** +** Description This function checks if the local device is the master of +** the link to the given device +** +** Returns TRUE if master. +** FALSE if not. +** +*******************************************************************************/ +BOOLEAN BTA_DmIsMaster(BD_ADDR bd_addr) +{ + BOOLEAN is_master = FALSE; + UINT8 link_role; + + BTM_GetRole(bd_addr, &link_role); + APPL_TRACE_API1("BTA_DmIsMaster role:x%x", link_role); + if(link_role == BTM_ROLE_MASTER) + { + is_master = TRUE; + } + return is_master; +} + +/******************************************************************************* +** +** Function BTA_DmBond +** +** Description This function initiates a bonding procedure with a peer +** device +** +** +** Returns void +** +*******************************************************************************/ +void BTA_DmBond(BD_ADDR bd_addr) +{ + tBTA_DM_API_BOND *p_msg; + + if ((p_msg = (tBTA_DM_API_BOND *) GKI_getbuf(sizeof(tBTA_DM_API_BOND))) != NULL) + { + p_msg->hdr.event = BTA_DM_API_BOND_EVT; + bdcpy(p_msg->bd_addr, bd_addr); + bta_sys_sendmsg(p_msg); + } + + +} + +/******************************************************************************* +** +** Function BTA_DmBondCancel +** +** Description This function cancels the bonding procedure with a peer +** device +** +** +** Returns void +** +*******************************************************************************/ +void BTA_DmBondCancel(BD_ADDR bd_addr) +{ + tBTA_DM_API_BOND_CANCEL *p_msg; + + if ((p_msg = (tBTA_DM_API_BOND_CANCEL *) GKI_getbuf(sizeof(tBTA_DM_API_BOND_CANCEL))) != NULL) + { + p_msg->hdr.event = BTA_DM_API_BOND_CANCEL_EVT; + bdcpy(p_msg->bd_addr, bd_addr); + bta_sys_sendmsg(p_msg); + } + + +} + +/******************************************************************************* +** +** Function BTA_DmPinReply +** +** Description This function provides a pincode for a remote device when +** one is requested by DM through BTA_DM_PIN_REQ_EVT +** +** +** Returns void +** +*******************************************************************************/ +void BTA_DmPinReply(BD_ADDR bd_addr, BOOLEAN accept, UINT8 pin_len, UINT8 *p_pin) + +{ + tBTA_DM_API_PIN_REPLY *p_msg; + + if ((p_msg = (tBTA_DM_API_PIN_REPLY *) GKI_getbuf(sizeof(tBTA_DM_API_PIN_REPLY))) != NULL) + { + p_msg->hdr.event = BTA_DM_API_PIN_REPLY_EVT; + bdcpy(p_msg->bd_addr, bd_addr); + p_msg->accept = accept; + if(accept) + { + p_msg->pin_len = pin_len; + memcpy(p_msg->p_pin, p_pin, pin_len); + } + bta_sys_sendmsg(p_msg); + } + +} + +/******************************************************************************* +** +** Function BTA_DmLinkPolicy +** +** Description This function sets/clears the link policy mask to the given +** bd_addr. +** If clearing the sniff or park mode mask, the link is put +** in active mode. +** +** Returns void +** +*******************************************************************************/ +void BTA_DmLinkPolicy(BD_ADDR bd_addr, tBTA_DM_LP_MASK policy_mask, + BOOLEAN set) +{ + tBTA_DM_API_LINK_POLICY *p_msg; + + if ((p_msg = (tBTA_DM_API_LINK_POLICY *) GKI_getbuf(sizeof(tBTA_DM_API_LINK_POLICY))) != NULL) + { + p_msg->hdr.event = BTA_DM_API_LINK_POLICY_EVT; + bdcpy(p_msg->bd_addr, bd_addr); + p_msg->policy_mask = policy_mask; + p_msg->set = set; + bta_sys_sendmsg(p_msg); + } +} + + +#if (BTM_OOB_INCLUDED == TRUE) +/******************************************************************************* +** +** Function BTA_DmLocalOob +** +** Description This function retrieves the OOB data from local controller. +** The result is reported by bta_dm_co_loc_oob(). +** +** Returns void +** +*******************************************************************************/ +void BTA_DmLocalOob(void) +{ + tBTA_DM_API_LOC_OOB *p_msg; + + if ((p_msg = (tBTA_DM_API_LOC_OOB *) GKI_getbuf(sizeof(tBTA_DM_API_LOC_OOB))) != NULL) + { + p_msg->hdr.event = BTA_DM_API_LOC_OOB_EVT; + bta_sys_sendmsg(p_msg); + } +} +#endif /* BTM_OOB_INCLUDED */ +/******************************************************************************* +** +** Function BTA_DmConfirm +** +** Description This function accepts or rejects the numerical value of the +** Simple Pairing process on BTA_DM_SP_CFM_REQ_EVT +** +** Returns void +** +*******************************************************************************/ +void BTA_DmConfirm(BD_ADDR bd_addr, BOOLEAN accept) +{ + tBTA_DM_API_CONFIRM *p_msg; + + if ((p_msg = (tBTA_DM_API_CONFIRM *) GKI_getbuf(sizeof(tBTA_DM_API_CONFIRM))) != NULL) + { + p_msg->hdr.event = BTA_DM_API_CONFIRM_EVT; + bdcpy(p_msg->bd_addr, bd_addr); + p_msg->accept = accept; + bta_sys_sendmsg(p_msg); + } +} + +/******************************************************************************* +** +** Function BTA_DmPasskeyCancel +** +** Description This function is called to cancel the simple pairing process +** reported by BTA_DM_SP_KEY_NOTIF_EVT +** +** Returns void +** +*******************************************************************************/ +#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE) +void BTA_DmPasskeyCancel(BD_ADDR bd_addr) +{ + tBTA_DM_API_PASKY_CANCEL *p_msg; + + if ((p_msg = (tBTA_DM_API_PASKY_CANCEL *) \ + GKI_getbuf(sizeof(tBTA_DM_API_PASKY_CANCEL))) != NULL) + { + p_msg->hdr.event = BTA_DM_API_PASKY_CANCEL_EVT; + bdcpy(p_msg->bd_addr, bd_addr); + bta_sys_sendmsg(p_msg); + } +} +#endif + + +/******************************************************************************* +** +** Function BTA_DmAddDevice +** +** Description This function adds a device to the security database list of +** peer device +** +** +** Returns void +** +*******************************************************************************/ +void BTA_DmAddDevice(BD_ADDR bd_addr, DEV_CLASS dev_class, LINK_KEY link_key, + tBTA_SERVICE_MASK trusted_mask, BOOLEAN is_trusted, + UINT8 key_type, tBTA_IO_CAP io_cap) +{ + + tBTA_DM_API_ADD_DEVICE *p_msg; + + if ((p_msg = (tBTA_DM_API_ADD_DEVICE *) GKI_getbuf(sizeof(tBTA_DM_API_ADD_DEVICE))) != NULL) + { + memset (p_msg, 0, sizeof(tBTA_DM_API_ADD_DEVICE)); + + p_msg->hdr.event = BTA_DM_API_ADD_DEVICE_EVT; + bdcpy(p_msg->bd_addr, bd_addr); + p_msg->tm = trusted_mask; + p_msg->is_trusted = is_trusted; + p_msg->io_cap = io_cap; + + if (link_key) + { + p_msg->link_key_known = TRUE; + p_msg->key_type = key_type; + memcpy(p_msg->link_key, link_key, LINK_KEY_LEN); + } + + /* Load device class if specified */ + if (dev_class) + { + p_msg->dc_known = TRUE; + memcpy (p_msg->dc, dev_class, DEV_CLASS_LEN); + } + + memset (p_msg->bd_name, 0, BD_NAME_LEN); + memset (p_msg->features, 0, BD_FEATURES_LEN); + + bta_sys_sendmsg(p_msg); + } +} + + +/******************************************************************************* +** +** Function BTA_DmRemoveDevice +** +** Description This function removes a device fromthe security database list of +** peer device +** +** +** Returns void +** +*******************************************************************************/ +tBTA_STATUS BTA_DmRemoveDevice(BD_ADDR bd_addr) +{ + + BOOLEAN device_ok_to_remove = TRUE; + + GKI_sched_lock(); + + if (BTM_IsAclConnectionUp(bd_addr)) + { + /* Don't allow deletion while connection is active */ + device_ok_to_remove = FALSE; + } + else /* Ok to remove the device in application layer */ + { + BTM_SecDeleteDevice(bd_addr); + } + GKI_sched_unlock(); + + if(device_ok_to_remove) + return BTA_SUCCESS; + else + return BTA_FAILURE; +} + +/******************************************************************************* +** +** Function BTA_DmAddDevWithName +** +** Description This function is newer version of BTA_DmAddDevice() +** which added bd_name and features as input parameters. +** +** +** Returns void +** +*******************************************************************************/ +void BTA_DmAddDevWithName (BD_ADDR bd_addr, DEV_CLASS dev_class, + BD_NAME bd_name, BD_FEATURES features, + LINK_KEY link_key, tBTA_SERVICE_MASK trusted_mask, + BOOLEAN is_trusted, UINT8 key_type, tBTA_IO_CAP io_cap) +{ + tBTA_DM_API_ADD_DEVICE *p_msg; + + if ((p_msg = (tBTA_DM_API_ADD_DEVICE *) GKI_getbuf(sizeof(tBTA_DM_API_ADD_DEVICE))) != NULL) + { + memset (p_msg, 0, sizeof(tBTA_DM_API_ADD_DEVICE)); + + p_msg->hdr.event = BTA_DM_API_ADD_DEVICE_EVT; + bdcpy(p_msg->bd_addr, bd_addr); + p_msg->tm = trusted_mask; + p_msg->is_trusted = is_trusted; + p_msg->io_cap = io_cap; + + if (link_key) + { + p_msg->link_key_known = TRUE; + p_msg->key_type = key_type; + memcpy(p_msg->link_key, link_key, LINK_KEY_LEN); + } + + /* Load device class if specified */ + if (dev_class) + { + p_msg->dc_known = TRUE; + memcpy (p_msg->dc, dev_class, DEV_CLASS_LEN); + } + + if (bd_name) + memcpy(p_msg->bd_name, bd_name, BD_NAME_LEN); + + if (features) + memcpy(p_msg->features, features, BD_FEATURES_LEN); + + bta_sys_sendmsg(p_msg); + } +} + +/******************************************************************************* +** +** Function BTA_DmAuthorizeReply +** +** Description This function provides an authorization reply when authorization +** is requested by BTA through BTA_DM_AUTHORIZE_EVT +** +** +** Returns tBTA_STATUS +** +*******************************************************************************/ +void BTA_DmAuthorizeReply(BD_ADDR bd_addr, tBTA_SERVICE_ID service, tBTA_AUTH_RESP response) +{ + + tBTA_DM_API_AUTH_REPLY *p_msg; + + if ((p_msg = (tBTA_DM_API_AUTH_REPLY *) GKI_getbuf(sizeof(tBTA_DM_API_AUTH_REPLY))) != NULL) + { + p_msg->hdr.event = BTA_DM_API_AUTH_REPLY_EVT; + bdcpy(p_msg->bd_addr, bd_addr); + p_msg->service = service; + p_msg->response = response; + + bta_sys_sendmsg(p_msg); + } + +} + +/******************************************************************************* +** +** Function BTA_DmSignalStrength +** +** Description This function initiates RSSI and channnel quality +** measurments. BTA_DM_SIG_STRENGTH_EVT is sent to +** application with the values of RSSI and channel +** quality +** +** +** Returns void +** +*******************************************************************************/ +void BTA_DmSignalStrength(tBTA_SIG_STRENGTH_MASK mask, UINT16 period, BOOLEAN start) +{ + + tBTA_API_DM_SIG_STRENGTH *p_msg; + + if ((p_msg = (tBTA_API_DM_SIG_STRENGTH *) GKI_getbuf(sizeof(tBTA_API_DM_SIG_STRENGTH))) != NULL) + { + p_msg->hdr.event = BTA_API_DM_SIG_STRENGTH_EVT; + p_msg->mask = mask; + p_msg->period = period; + p_msg->start = start; + + bta_sys_sendmsg(p_msg); + } + + +} + +/******************************************************************************* +** +** Function BTA_DmWriteInqTxPower +** +** Description This command is used to write the inquiry transmit power level +** used to transmit the inquiry (ID) data packets. +** +** Parameters tx_power - tx inquiry power to use, valid value is -70 ~ 20 + +** Returns void +** +*******************************************************************************/ +void BTA_DmWriteInqTxPower(INT8 tx_power) +{ + + tBTA_API_DM_TX_INQPWR *p_msg; + + if ((p_msg = (tBTA_API_DM_TX_INQPWR *) GKI_getbuf(sizeof(tBTA_API_DM_TX_INQPWR))) != NULL) + { + p_msg->hdr.event = BTA_DM_API_TX_INQPWR_EVT; + p_msg->tx_power = tx_power; + + bta_sys_sendmsg(p_msg); + } +} + + +/******************************************************************************* +** +** Function BTA_DmEirAddUUID +** +** Description This function is called to add UUID into EIR. +** +** Parameters tBT_UUID - UUID +** +** Returns None +** +*******************************************************************************/ +void BTA_DmEirAddUUID (tBT_UUID *p_uuid) +{ +#if ( BTM_EIR_SERVER_INCLUDED == TRUE )&&( BTA_EIR_CANNED_UUID_LIST != TRUE )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) + tBTA_DM_API_UPDATE_EIR_UUID *p_msg; + + if ((p_msg = (tBTA_DM_API_UPDATE_EIR_UUID *) GKI_getbuf(sizeof(tBTA_DM_API_UPDATE_EIR_UUID))) != NULL) + { + p_msg->hdr.event = BTA_DM_API_UPDATE_EIR_UUID_EVT; + p_msg->is_add = TRUE; + memcpy (&(p_msg->uuid), p_uuid, sizeof(tBT_UUID)); + + bta_sys_sendmsg(p_msg); + } +#endif +} + +/******************************************************************************* +** +** Function BTA_DmEirRemoveUUID +** +** Description This function is called to remove UUID from EIR. +** +** Parameters tBT_UUID - UUID +** +** Returns None +** +*******************************************************************************/ +void BTA_DmEirRemoveUUID (tBT_UUID *p_uuid) +{ +#if ( BTM_EIR_SERVER_INCLUDED == TRUE )&&( BTA_EIR_CANNED_UUID_LIST != TRUE )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) + tBTA_DM_API_UPDATE_EIR_UUID *p_msg; + + if ((p_msg = (tBTA_DM_API_UPDATE_EIR_UUID *) GKI_getbuf(sizeof(tBTA_DM_API_UPDATE_EIR_UUID))) != NULL) + { + p_msg->hdr.event = BTA_DM_API_UPDATE_EIR_UUID_EVT; + p_msg->is_add = FALSE; + memcpy (&(p_msg->uuid), p_uuid, sizeof(tBT_UUID)); + + bta_sys_sendmsg(p_msg); + } +#endif +} + +/******************************************************************************* +** +** Function BTA_DmSetEIRConfig +** +** Description This function is called to override the BTA default EIR parameters. +** This funciton is only valid in a system where BTU & App task +** are in the same memory space. +** +** Parameters Pointer to User defined EIR config +** +** Returns None +** +*******************************************************************************/ +void BTA_DmSetEIRConfig (tBTA_DM_EIR_CONF *p_eir_cfg) +{ +#if (BTM_EIR_SERVER_INCLUDED == TRUE) + tBTA_DM_API_SET_EIR_CONFIG *p_msg; + + if ((p_msg = (tBTA_DM_API_SET_EIR_CONFIG *) GKI_getbuf(sizeof(tBTA_DM_API_SET_EIR_CONFIG))) != NULL) + { + p_msg->hdr.event = BTA_DM_API_SET_EIR_CONFIG_EVT; + p_msg->p_eir_cfg = p_eir_cfg; + + bta_sys_sendmsg(p_msg); + } +#endif +} + +/******************************************************************************* +** +** Function BTA_CheckEirData +** +** Description This function is called to get EIR data from significant part. +** +** Parameters p_eir - pointer of EIR significant part +** type - finding EIR data type +** p_length - return the length of EIR data +** +** Returns pointer of EIR data +** +*******************************************************************************/ +UINT8 *BTA_CheckEirData( UINT8 *p_eir, UINT8 type, UINT8 *p_length ) +{ +#if ( BTM_EIR_CLIENT_INCLUDED == TRUE ) + return BTM_CheckEirData( p_eir, type, p_length ); +#else + return NULL; +#endif +} + +/******************************************************************************* +** +** Function BTA_GetEirService +** +** Description This function is called to get BTA service mask from EIR. +** +** Parameters p_eir - pointer of EIR significant part +** p_services - return the BTA service mask +** +** Returns None +** +*******************************************************************************/ +extern const UINT16 bta_service_id_to_uuid_lkup_tbl []; +void BTA_GetEirService( UINT8 *p_eir, tBTA_SERVICE_MASK *p_services ) +{ +#if ( BTM_EIR_CLIENT_INCLUDED == TRUE ) + UINT8 xx, yy; + UINT8 num_uuid, max_num_uuid = 32; + UINT8 uuid_list[32*LEN_UUID_16]; + UINT16 *p_uuid16 = (UINT16 *)uuid_list; + tBTA_SERVICE_MASK mask; + + BTM_GetEirUuidList( p_eir, LEN_UUID_16, &num_uuid, uuid_list, max_num_uuid); + for( xx = 0; xx < num_uuid; xx++ ) + { + mask = 1; + for( yy = 0; yy < BTA_MAX_SERVICE_ID; yy++ ) + { + if( *(p_uuid16 + xx) == bta_service_id_to_uuid_lkup_tbl[yy] ) + { + *p_services |= mask; + break; + } + mask <<= 1; + } + + /* for HSP v1.2 only device */ + if (*(p_uuid16 + xx) == UUID_SERVCLASS_HEADSET_HS) + *p_services |= BTA_HSP_SERVICE_MASK; + } +#endif +} + +/******************************************************************************* +** +** Function BTA_DmUseSsr +** +** Description This function is called to check if the connected peer device +** supports SSR or not. +** +** Returns TRUE, if SSR is supported +** +*******************************************************************************/ +BOOLEAN BTA_DmUseSsr( BD_ADDR bd_addr ) +{ + BOOLEAN use_ssr = FALSE; + tBTA_DM_PEER_DEVICE * p_dev = bta_dm_find_peer_device(bd_addr); + if(p_dev && (p_dev->info & BTA_DM_DI_USE_SSR) ) + use_ssr = TRUE; + return use_ssr; +} + +/******************************************************************************* +** Device Identification (DI) Server Functions +*******************************************************************************/ +/******************************************************************************* +** +** Function BTA_DmSetLocalDiRecord +** +** Description This function adds a DI record to the local SDP database. +** +** Returns BTA_SUCCESS if record set sucessfully, otherwise error code. +** +*******************************************************************************/ +tBTA_STATUS BTA_DmSetLocalDiRecord( tBTA_DI_RECORD *p_device_info, + UINT32 *p_handle ) +{ + tBTA_STATUS status = BTA_FAILURE; + + if(bta_dm_di_cb.di_num < BTA_DI_NUM_MAX) + { + if(SDP_SetLocalDiRecord((tSDP_DI_RECORD *)p_device_info, p_handle) == SDP_SUCCESS) + { + if(!p_device_info->primary_record) + { + bta_dm_di_cb.di_handle[bta_dm_di_cb.di_num] = *p_handle; + bta_dm_di_cb.di_num ++; + } + + bta_sys_add_uuid(UUID_SERVCLASS_PNP_INFORMATION); + status = BTA_SUCCESS; + } + } + + return status; +} + +/******************************************************************************* +** +** Function BTA_DmGetLocalDiRecord +** +** Description Get a specified DI record to the local SDP database. If no +** record handle is provided, the primary DI record will be +** returned. +** +** Fills in the device information of the record +** p_handle - if p_handle == 0, the primary record is returned +** +** Returns BTA_SUCCESS if record set sucessfully, otherwise error code. +** +*******************************************************************************/ +tBTA_STATUS BTA_DmGetLocalDiRecord( tBTA_DI_GET_RECORD *p_device_info, + UINT32 *p_handle ) +{ + UINT16 status; + + status = SDP_GetLocalDiRecord(p_device_info, p_handle); + + if (status == SDP_SUCCESS) + return BTA_SUCCESS; + else + return BTA_FAILURE; + +} + +/******************************************************************************* +** Device Identification (DI) Client Functions +*******************************************************************************/ +/******************************************************************************* +** +** Function BTA_DmDiDiscover +** +** Description This function queries a remote device for DI information. +** +** +** Returns None. +** +*******************************************************************************/ +void BTA_DmDiDiscover( BD_ADDR remote_device, tBTA_DISCOVERY_DB *p_db, + UINT32 len, tBTA_DM_SEARCH_CBACK *p_cback ) +{ + tBTA_DM_API_DI_DISC *p_msg; + + if ((p_msg = (tBTA_DM_API_DI_DISC *) GKI_getbuf(sizeof(tBTA_DM_API_DI_DISC))) != NULL) + { + bdcpy(p_msg->bd_addr, remote_device); + p_msg->hdr.event = BTA_DM_API_DI_DISCOVER_EVT; + p_msg->p_sdp_db = p_db; + p_msg->len = len; + p_msg->p_cback = p_cback; + + bta_sys_sendmsg(p_msg); + } +} + +/******************************************************************************* +** +** Function BTA_DmGetDiRecord +** +** Description This function retrieves a remote device's DI record from +** the specified database. +** +** Returns BTA_SUCCESS if Get DI record is succeed. +** BTA_FAILURE if Get DI record failed. +** +*******************************************************************************/ +tBTA_STATUS BTA_DmGetDiRecord( UINT8 get_record_index, tBTA_DI_GET_RECORD *p_device_info, + tBTA_DISCOVERY_DB *p_db ) +{ + if (SDP_GetDiRecord(get_record_index, p_device_info, p_db) != SDP_SUCCESS) + return BTA_FAILURE; + else + return BTA_SUCCESS; +} + +/******************************************************************************* +** +** Function BTA_SysFeatures +** +** Description This function is called to set system features. +** +** Returns void +** +*******************************************************************************/ +void BTA_SysFeatures (UINT16 sys_features) +{ + bta_sys_cb.sys_features = sys_features; + + APPL_TRACE_API1("BTA_SysFeatures: sys_features = %d", sys_features); +} + +/******************************************************************************* +** +** Function bta_dmexecutecallback +** +** Description This function will request BTA to execute a call back in the context of BTU task +** This API was named in lower case because it is only intended +** for the internal customers(like BSA). +** +** Returns void +** +*******************************************************************************/ +void bta_dmexecutecallback (tBTA_DM_EXEC_CBACK* p_callback, void * p_param) +{ + tBTA_DM_API_EXECUTE_CBACK *p_msg; + + if ((p_msg = (tBTA_DM_API_EXECUTE_CBACK *) GKI_getbuf(sizeof(tBTA_DM_MSG))) != NULL) + { + p_msg->hdr.event = BTA_DM_API_EXECUTE_CBACK_EVT; + p_msg->p_param= p_param; + p_msg->p_exec_cback= p_callback; + bta_sys_sendmsg(p_msg); + } +} + +/******************************************************************************* +** +** Function BTA_DmAddBleKey +** +** Description Add/modify LE device information. This function will be +** normally called during host startup to restore all required +** information stored in the NVRAM. +** +** Parameters: bd_addr - BD address of the peer +** p_le_key - LE key values. +** key_type - LE SMP key type. +** +** Returns void +** +*******************************************************************************/ +void BTA_DmAddBleKey (BD_ADDR bd_addr, tBTA_LE_KEY_VALUE *p_le_key, tBTA_LE_KEY_TYPE key_type) +{ +#if BLE_INCLUDED == TRUE + + tBTA_DM_API_ADD_BLEKEY *p_msg; + + if ((p_msg = (tBTA_DM_API_ADD_BLEKEY *) GKI_getbuf(sizeof(tBTA_DM_API_ADD_BLEKEY))) != NULL) + { + memset (p_msg, 0, sizeof(tBTA_DM_API_ADD_BLEKEY)); + + p_msg->hdr.event = BTA_DM_API_ADD_BLEKEY_EVT; + p_msg->key_type = key_type; + bdcpy(p_msg->bd_addr, bd_addr); + memcpy(&p_msg->blekey, p_le_key, sizeof(tBTA_LE_KEY_VALUE)); + + bta_sys_sendmsg(p_msg); + } + +#endif +} + +/******************************************************************************* +** +** Function BTA_DmAddBleDevice +** +** Description Add a BLE device. This function will be normally called +** during host startup to restore all required information +** for a LE device stored in the NVRAM. +** +** Parameters: bd_addr - BD address of the peer +** dev_type - Remote device's device type. +** addr_type - LE device address type. +** +** Returns void +** +*******************************************************************************/ +void BTA_DmAddBleDevice(BD_ADDR bd_addr, tBLE_ADDR_TYPE addr_type, tBT_DEVICE_TYPE dev_type) +{ +#if BLE_INCLUDED == TRUE + tBTA_DM_API_ADD_BLE_DEVICE *p_msg; + + if ((p_msg = (tBTA_DM_API_ADD_BLE_DEVICE *) GKI_getbuf(sizeof(tBTA_DM_API_ADD_BLE_DEVICE))) != NULL) + { + memset (p_msg, 0, sizeof(tBTA_DM_API_ADD_BLE_DEVICE)); + + p_msg->hdr.event = BTA_DM_API_ADD_BLEDEVICE_EVT; + bdcpy(p_msg->bd_addr, bd_addr); + p_msg->addr_type = addr_type; + p_msg->dev_type = dev_type; + + bta_sys_sendmsg(p_msg); + } +#endif +} +/******************************************************************************* +** +** Function BTA_DmBlePasskeyReply +** +** Description Send BLE SMP passkey reply. +** +** Parameters: bd_addr - BD address of the peer +** accept - passkey entry sucessful or declined. +** passkey - passkey value, must be a 6 digit number, +** can be lead by 0. +** +** Returns void +** +*******************************************************************************/ +void BTA_DmBlePasskeyReply(BD_ADDR bd_addr, BOOLEAN accept, UINT32 passkey) +{ +#if BLE_INCLUDED == TRUE + tBTA_DM_API_PASSKEY_REPLY *p_msg; + + if ((p_msg = (tBTA_DM_API_PASSKEY_REPLY *) GKI_getbuf(sizeof(tBTA_DM_API_PASSKEY_REPLY))) != NULL) + { + memset(p_msg, 0, sizeof(tBTA_DM_API_PASSKEY_REPLY)); + + p_msg->hdr.event = BTA_DM_API_BLE_PASSKEY_REPLY_EVT; + bdcpy(p_msg->bd_addr, bd_addr); + p_msg->accept = accept; + + if(accept) + { + p_msg->passkey = passkey; + } + bta_sys_sendmsg(p_msg); + } +#endif +} +/******************************************************************************* +** +** Function BTA_DmBleSecurityGrant +** +** Description Grant security request access. +** +** Parameters: bd_addr - BD address of the peer +** res - security grant status. +** +** Returns void +** +*******************************************************************************/ +void BTA_DmBleSecurityGrant(BD_ADDR bd_addr, tBTA_DM_BLE_SEC_GRANT res) +{ +#if BLE_INCLUDED == TRUE + tBTA_DM_API_BLE_SEC_GRANT *p_msg; + + if ((p_msg = (tBTA_DM_API_BLE_SEC_GRANT *) GKI_getbuf(sizeof(tBTA_DM_API_BLE_SEC_GRANT))) != NULL) + { + memset(p_msg, 0, sizeof(tBTA_DM_API_BLE_SEC_GRANT)); + + p_msg->hdr.event = BTA_DM_API_BLE_SEC_GRANT_EVT; + bdcpy(p_msg->bd_addr, bd_addr); + p_msg->res = res; + + bta_sys_sendmsg(p_msg); + } +#endif +} +/******************************************************************************* +** +** Function BTA_DmSetBlePrefConnParams +** +** Description This function is called to set the preferred connection +** parameters when default connection parameter is not desired. +** +** Parameters: bd_addr - BD address of the peripheral +** scan_interval - scan interval +** scan_window - scan window +** min_conn_int - minimum preferred connection interval +** max_conn_int - maximum preferred connection interval +** slave_latency - preferred slave latency +** supervision_tout - preferred supervision timeout +** +** +** Returns void +** +*******************************************************************************/ +void BTA_DmSetBlePrefConnParams(BD_ADDR bd_addr, + UINT16 min_conn_int, UINT16 max_conn_int, + UINT16 slave_latency, UINT16 supervision_tout ) +{ +#if BLE_INCLUDED == TRUE + tBTA_DM_API_BLE_CONN_PARAMS *p_msg; + + if ((p_msg = (tBTA_DM_API_BLE_CONN_PARAMS *) GKI_getbuf(sizeof(tBTA_DM_API_BLE_CONN_PARAMS))) != NULL) + { + memset(p_msg, 0, sizeof(tBTA_DM_API_BLE_CONN_PARAMS)); + + p_msg->hdr.event = BTA_DM_API_BLE_CONN_PARAM_EVT; + + memcpy(p_msg->peer_bda, bd_addr, BD_ADDR_LEN); + + p_msg->conn_int_max = max_conn_int; + p_msg->conn_int_min = min_conn_int; + p_msg->slave_latency = slave_latency; + p_msg->supervision_tout = supervision_tout; + + bta_sys_sendmsg(p_msg); + } +#endif +} + +/******************************************************************************* +** +** Function BTA_DmSetBleConnScanParams +** +** Description This function is called to set scan parameters used in +** BLE connection request +** +** Parameters: scan_interval - scan interval +** scan_window - scan window +** +** Returns void +** +*******************************************************************************/ +void BTA_DmSetBleConnScanParams(UINT16 scan_interval, UINT16 scan_window ) +{ +#if BLE_INCLUDED == TRUE + tBTA_DM_API_BLE_SCAN_PARAMS *p_msg; + + if ((p_msg = (tBTA_DM_API_BLE_SCAN_PARAMS *) GKI_getbuf(sizeof(tBTA_DM_API_BLE_SCAN_PARAMS))) != NULL) + { + memset(p_msg, 0, sizeof(tBTA_DM_API_BLE_SCAN_PARAMS)); + + p_msg->hdr.event = BTA_DM_API_BLE_SCAN_PARAM_EVT; + + p_msg->scan_int = scan_interval; + p_msg->scan_window = scan_window; + + bta_sys_sendmsg(p_msg); + } +#endif +} + +/******************************************************************************* +** +** Function BTA_DmBleSetBgConnType +** +** Description This function is called to set BLE connectable mode for a +** peripheral device. +** +** Parameters bg_conn_type: it can be auto connection, or selective connection. +** p_select_cback: callback function when selective connection procedure +** is being used. +** +** Returns void +** +*******************************************************************************/ +void BTA_DmBleSetBgConnType(tBTA_DM_BLE_CONN_TYPE bg_conn_type, tBTA_DM_BLE_SEL_CBACK *p_select_cback) +{ +#if BLE_INCLUDED == TRUE + tBTA_DM_API_BLE_SET_BG_CONN_TYPE *p_msg; + + if ((p_msg = (tBTA_DM_API_BLE_SET_BG_CONN_TYPE *) GKI_getbuf(sizeof(tBTA_DM_API_BLE_SET_BG_CONN_TYPE))) != NULL) + { + memset(p_msg, 0, sizeof(tBTA_DM_API_BLE_SET_BG_CONN_TYPE)); + + p_msg->hdr.event = BTA_DM_API_BLE_SET_BG_CONN_TYPE; + p_msg->bg_conn_type = bg_conn_type; + p_msg->p_select_cback = p_select_cback; + + bta_sys_sendmsg(p_msg); + } +#endif +} +/******************************************************************************* +** +** Function BTA_DmDiscoverExt +** +** Description This function does service discovery for services of a +** peer device. When services.num_uuid is 0, it indicates all +** GATT based services are to be searched; other wise a list of +** UUID of interested services should be provided through +** p_services->p_uuid. +** +** +** +** Returns void +** +*******************************************************************************/ +void BTA_DmDiscoverExt(BD_ADDR bd_addr, tBTA_SERVICE_MASK_EXT *p_services, + tBTA_DM_SEARCH_CBACK *p_cback, BOOLEAN sdp_search) +{ +#if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE + tBTA_DM_API_DISCOVER *p_msg; + UINT16 len = p_services ? (sizeof(tBTA_DM_API_DISCOVER) + sizeof(tBT_UUID) * p_services->num_uuid) : + sizeof(tBTA_DM_API_DISCOVER); + + if ((p_msg = (tBTA_DM_API_DISCOVER *) GKI_getbuf(len)) != NULL) + { + memset(p_msg, 0, len); + + p_msg->hdr.event = BTA_DM_API_DISCOVER_EVT; + bdcpy(p_msg->bd_addr, bd_addr); + p_msg->p_cback = p_cback; + p_msg->sdp_search = sdp_search; + + if (p_services != NULL) + { + p_msg->services = p_services->srvc_mask; + p_msg->num_uuid = p_services->num_uuid; + + if (p_services->num_uuid != 0) + { + p_msg->p_uuid = (tBT_UUID *)(p_msg + 1); + memcpy(p_msg->p_uuid, p_services->p_uuid, sizeof(tBT_UUID) * p_services->num_uuid); + } + } + + bta_sys_sendmsg(p_msg); + } +#endif + +} + +/******************************************************************************* +** +** Function BTA_DmSearchExt +** +** Description This function searches for peer Bluetooth devices. It performs +** an inquiry and gets the remote name for devices. Service +** discovery is done if services is non zero +** +** Parameters p_dm_inq: inquiry conditions +** p_services: if service is not empty, service discovery will be done. +** for all GATT based service condition, put num_uuid, and +** p_uuid is the pointer to the list of UUID values. +** p_cback: callback functino when search is completed. +** +** +** +** Returns void +** +*******************************************************************************/ +void BTA_DmSearchExt(tBTA_DM_INQ *p_dm_inq, tBTA_SERVICE_MASK_EXT *p_services, tBTA_DM_SEARCH_CBACK *p_cback) +{ +#if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE + tBTA_DM_API_SEARCH *p_msg; + UINT16 len = p_services ? (sizeof(tBTA_DM_API_SEARCH) + sizeof(tBT_UUID) * p_services->num_uuid) : + sizeof(tBTA_DM_API_SEARCH); + + if ((p_msg = (tBTA_DM_API_SEARCH *) GKI_getbuf(len)) != NULL) + { + memset(p_msg, 0, len); + + p_msg->hdr.event = BTA_DM_API_SEARCH_EVT; + memcpy(&p_msg->inq_params, p_dm_inq, sizeof(tBTA_DM_INQ)); + p_msg->p_cback = p_cback; + p_msg->rs_res = BTA_DM_RS_NONE; + + + if (p_services != NULL) + { + p_msg->services = p_services->srvc_mask; + p_msg->num_uuid = p_services->num_uuid; + + if (p_services->num_uuid != 0) + { + p_msg->p_uuid = (tBT_UUID *)(p_msg + 1); + memcpy(p_msg->p_uuid, p_services->p_uuid, sizeof(tBT_UUID) * p_services->num_uuid); + } + else + p_msg->p_uuid = NULL; + } + + bta_sys_sendmsg(p_msg); + } +#endif +} + + +/******************************************************************************* +** +** Function BTA_DmSetEncryption +** +** Description This function is called to ensure that connection is +** encrypted. Should be called only on an open connection. +** Typically only needed for connections that first want to +** bring up unencrypted links, then later encrypt them. +** +** Parameters: bd_addr - Address of the peer device +** p_callback - Pointer to callback function to indicat the +** link encryption status +** sec_act - This is the security action to indicate +** what knid of BLE security level is required for +** the BLE link if the BLE is supported +** Note: This parameter is ignored for the BR/EDR link +** or the BLE is not supported +** +** Returns void +** +*******************************************************************************/ +void BTA_DmSetEncryption(BD_ADDR bd_addr, tBTA_DM_ENCRYPT_CBACK *p_callback, + tBTA_DM_BLE_SEC_ACT sec_act) +{ + tBTA_DM_API_SET_ENCRYPTION *p_msg; + + APPL_TRACE_API0("BTA_DmSetEncryption"); //todo + if ((p_msg = (tBTA_DM_API_SET_ENCRYPTION *) GKI_getbuf(sizeof(tBTA_DM_API_SET_ENCRYPTION))) != NULL) + { + memset(p_msg, 0, sizeof(tBTA_DM_API_SET_ENCRYPTION)); + + p_msg->hdr.event = BTA_DM_API_SET_ENCRYPTION_EVT; + + memcpy(p_msg->bd_addr, bd_addr, BD_ADDR_LEN); + p_msg->p_callback = p_callback; + p_msg->sec_act = sec_act; + + bta_sys_sendmsg(p_msg); + } +} + diff --git a/bta/dm/bta_dm_cfg.c b/bta/dm/bta_dm_cfg.c new file mode 100644 index 0000000..e6a76af --- /dev/null +++ b/bta/dm/bta_dm_cfg.c @@ -0,0 +1,424 @@ +/***************************************************************************** +** +** Name: bta_dm_cfg.c +** +** Description: This file contains compile-time configurable constants +** for the device manager. +** +** Copyright (c) 2003-2011, Broadcom Corp, All Rights Reserved. +** Broadcom Bluetooth Core. Proprietary and confidential. +** +*****************************************************************************/ + +#include "bt_target.h" +#include "bta_sys.h" +#include "bta_api.h" +#include "bta_dm_int.h" + +#ifndef BTA_DM_COD + +/* + * major : COD_MAJOR_PHONE + * minor : COD_MINOR_SMART_PHONE + * svc : None, will be set as each profile registers + * + */ + +#define BTA_DM_COD {0x40, BTM_COD_MAJOR_PHONE, BTM_COD_MINOR_SMART_PHONE} +#endif + + +#ifndef BTA_DM_LINK_POLICY_SETTINGS +#define BTA_DM_LINK_POLICY_SETTINGS (HCI_ENABLE_MASTER_SLAVE_SWITCH | HCI_ENABLE_HOLD_MODE | HCI_ENABLE_SNIFF_MODE | HCI_ENABLE_PARK_MODE) +#endif + +/* page timeout in 625uS */ +#ifndef BTA_DM_PAGE_TIMEOUT +#define BTA_DM_PAGE_TIMEOUT 8192 +#endif + +/* link supervision timeout in 625uS (5 secs) */ +#ifndef BTA_DM_LINK_TIMEOUT +#define BTA_DM_LINK_TIMEOUT 8000 +#endif + +/* For Insight, PM cfg lookup tables are runtime configurable (to allow tweaking of params for power consumption measurements) */ +#ifndef BTE_SIM_APP +#define tBTA_DM_PM_TYPE_QUALIFIER const +#else +#define tBTA_DM_PM_TYPE_QUALIFIER +#endif + + +const tBTA_DM_CFG bta_dm_cfg = +{ + /* mobile phone COD */ + BTA_DM_COD, + /* link policy settings */ + BTA_DM_LINK_POLICY_SETTINGS, + /* page timeout in 625uS */ + BTA_DM_PAGE_TIMEOUT, + /* link supervision timeout in 625uS*/ + BTA_DM_LINK_TIMEOUT, + /* TRUE to avoid scatternet when av is streaming (be the master) */ + TRUE +}; + +#ifndef BTA_DM_SCATTERNET +/* By default, allow partial scatternet */ +#define BTA_DM_SCATTERNET BTA_DM_PARTIAL_SCATTERNET +#endif + +#ifndef BTA_HH_ROLE +/* By default, do not specify HH role (backward compatibility) */ +#define BTA_HH_ROLE BTA_ANY_ROLE +#endif + +#ifndef BTA_AV_ROLE +/* By default, AV role (backward BTA_MASTER_ROLE_PREF) */ +#define BTA_AV_ROLE BTA_MASTER_ROLE_PREF +#endif + +#define BTA_DM_NUM_RM_ENTRY 4 + +/* appids for PAN used by insight sample application + these have to be same as defined in btui_int.h */ +#define BTUI_PAN_ID_PANU 0 +#define BTUI_PAN_ID_NAP 1 +#define BTUI_PAN_ID_GN 2 + +/* First element is always for SYS: + app_id = # of entries table, cfg is + device scatternet support */ +const tBTA_DM_RM bta_dm_rm_cfg[] = +{ + {BTA_ID_SYS, BTA_DM_NUM_RM_ENTRY, BTA_DM_SCATTERNET}, + {BTA_ID_PAN, BTUI_PAN_ID_NAP, BTA_MASTER_ROLE_ONLY}, + {BTA_ID_PAN, BTUI_PAN_ID_GN, BTA_MASTER_ROLE_ONLY}, + {BTA_ID_HH, BTA_ALL_APP_ID, BTA_HH_ROLE}, + {BTA_ID_AV, BTA_ALL_APP_ID, BTA_AV_ROLE} +}; + + +tBTA_DM_CFG *p_bta_dm_cfg = (tBTA_DM_CFG *)&bta_dm_cfg; + +tBTA_DM_RM *p_bta_dm_rm_cfg = (tBTA_DM_RM *)&bta_dm_rm_cfg; + + +#define BTA_DM_NUM_PM_ENTRY (15+BTA_DM_NUM_JV_ID) /* number of entries in bta_dm_pm_cfg except the first */ + +tBTA_DM_PM_TYPE_QUALIFIER tBTA_DM_PM_CFG bta_dm_pm_cfg[] = +{ + {BTA_ID_SYS, BTA_DM_NUM_PM_ENTRY, 0}, + {BTA_ID_AG, BTA_ALL_APP_ID, 0}, /* ag uses first spec table for app id 0 */ + {BTA_ID_CT, 1, 1}, /* ct (BTA_ID_CT,APP ID=1) spec table */ + {BTA_ID_CG, BTA_ALL_APP_ID, 1}, /* cg resue ct spec table */ + {BTA_ID_DG, BTA_ALL_APP_ID, 2}, /* dg spec table */ + {BTA_ID_AV, BTA_ALL_APP_ID, 4}, /* av spec table */ + {BTA_ID_FTC, BTA_ALL_APP_ID, 6}, /* ftc spec table */ + {BTA_ID_FTS, BTA_ALL_APP_ID, 7}, /* fts spec table */ + {BTA_ID_HD, BTA_ALL_APP_ID, 3}, /* hd spec table */ + {BTA_ID_HH, BTA_ALL_APP_ID, 5}, /* hh spec table */ + {BTA_ID_PBC, BTA_ALL_APP_ID, 2}, /* reuse dg spec table */ + {BTA_ID_PBS, BTA_ALL_APP_ID, 7}, /* reuse fts spec table */ + {BTA_ID_OPC, BTA_ALL_APP_ID, 6}, /* reuse ftc spec table */ + {BTA_ID_OPS, BTA_ALL_APP_ID, 7}, /* reuse fts spec table */ + {BTA_ID_MSE, BTA_ALL_APP_ID, 7}, /* reuse fts spec table */ + {BTA_ID_JV1, BTA_ALL_APP_ID, 7}, /* reuse fts spec table */ + {BTA_ID_JV2, BTA_ALL_APP_ID, 7}, /* reuse fts spec table */ + {BTA_ID_HL, BTA_ALL_APP_ID, 8} /* reuse fts spec table */ +}; + + +#ifdef BTE_SIM_APP /* For Insight builds only, see the detail below */ +#define BTA_DM_NUM_PM_SPEC (9 + 2) /* additional two */ +#else +#define BTA_DM_NUM_PM_SPEC 9 /* additional JV*/ +#endif + +tBTA_DM_PM_TYPE_QUALIFIER tBTA_DM_PM_SPEC bta_dm_pm_spec[BTA_DM_NUM_PM_SPEC] = +{ + /* AG */ + { + (BTA_DM_PM_SNIFF | BTA_DM_PM_PARK), /* allow park & sniff */ +#if (BTM_SSR_INCLUDED == TRUE) + (BTA_DM_PM_SSR2), /* the SSR entry */ +#endif + { + {{BTA_DM_PM_SNIFF, 5000}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open sniff */ + {{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */ + {{BTA_DM_PM_SNIFF3, 5000}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open, active */ + {{BTA_DM_PM_SNIFF, 5000}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close sniff */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */ + {{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */ + {{BTA_DM_PM_RETRY, 5000}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */ + } + }, + + /* CT */ + { + (BTA_DM_PM_SNIFF | BTA_DM_PM_PARK), /* allow park & sniff */ +#if (BTM_SSR_INCLUDED == TRUE) + (BTA_DM_PM_SSR2), /* the SSR entry */ +#endif + { + {{BTA_DM_PM_PARK, 5000}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open park */ + {{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */ + {{BTA_DM_PM_SNIFF, 5000}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open sniff */ + {{BTA_DM_PM_PARK, 5000}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close park */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */ + {{BTA_DM_PM_RETRY, 5000}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */ + } + }, + + /* DG */ + { + (BTA_DM_PM_ACTIVE), /* no power saving mode allowed */ +#if (BTM_SSR_INCLUDED == TRUE) + (BTA_DM_PM_SSR2), /* the SSR entry */ +#endif + { + {{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open active */ + {{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */ + {{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */ + } + }, + + /* HD */ + { + (BTA_DM_PM_SNIFF | BTA_DM_PM_PARK), /* allow park & sniff */ +#if (BTM_SSR_INCLUDED == TRUE) + (BTA_DM_PM_SSR3), /* the SSR entry */ +#endif + { + {{BTA_DM_PM_SNIFF4, 5000}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open sniff */ + {{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close */ + {{BTA_DM_PM_SNIFF2, 5000}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */ + {{BTA_DM_PM_SNIFF4, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */ + } + }, + + /* AV */ + { + (BTA_DM_PM_SNIFF), /* allow sniff */ +#if (BTM_SSR_INCLUDED == TRUE) + (BTA_DM_PM_SSR2), /* the SSR entry */ +#endif + { + {{BTA_DM_PM_SNIFF, 5000}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open sniff */ + {{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close */ + {{BTA_DM_PM_SNIFF, 5000}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */ + {{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */ + } + }, + + /* HH */ + { + (BTA_DM_PM_SNIFF | BTA_DM_PM_PARK), /* allow park & sniff */ +#if (BTM_SSR_INCLUDED == TRUE) + (BTA_DM_PM_SSR1), /* the SSR entry */ +#endif + { + {{BTA_DM_PM_SNIFF2, 7000}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open sniff */ + {{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close, used for HH suspend */ + {{BTA_DM_PM_SNIFF2, 7000}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */ + {{BTA_DM_PM_SNIFF2, 7000}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */ + } + }, + + /* FTC, OPC */ + { + (BTA_DM_PM_SNIFF), /* allow sniff */ +#if (BTM_SSR_INCLUDED == TRUE) + (BTA_DM_PM_SSR2), /* the SSR entry */ +#endif + { + {{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open active */ + {{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */ + {{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close */ + {{BTA_DM_PM_SNIFF, 5000}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */ + {{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */ + } + }, + + /* FTS, OPS */ + { + (BTA_DM_PM_SNIFF), /* allow sniff */ +#if (BTM_SSR_INCLUDED == TRUE) + (BTA_DM_PM_SSR2), /* the SSR entry */ +#endif + { + {{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open active */ + {{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */ + {{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close */ + {{BTA_DM_PM_SNIFF, 7000}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */ + {{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */ + } + }, + + /* HL */ + { + (BTA_DM_PM_SNIFF), /* allow sniff */ +#if (BTM_SSR_INCLUDED == TRUE) + (BTA_DM_PM_SSR2), /* the SSR entry */ +#endif + { + {{BTA_DM_PM_SNIFF, 5000}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn open sniff */ + {{BTA_DM_PM_NO_PREF, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* conn close */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app open */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* app close */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco open, active */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* sco close sniff */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* idle */ + {{BTA_DM_PM_ACTIVE, 0}, {BTA_DM_PM_NO_ACTION, 0}}, /* busy */ + {{BTA_DM_PM_NO_ACTION, 0}, {BTA_DM_PM_NO_ACTION, 0}} /* mode change retry */ + } + } + +#ifdef BTE_SIM_APP /* For Insight builds only */ + /* Entries at the end of the pm_spec table are user-defined (runtime configurable), + for power consumption experiments. + Insight finds the first user-defined entry by looking for the first BTA_DM_PM_NO_PREF. + The number of user_defined specs is defined by BTA_SWRAP_UD_PM_SPEC_COUNT */ + , + {BTA_DM_PM_NO_PREF}, /* pm_spec USER_DEFINED_0 */ + {BTA_DM_PM_NO_PREF} /* pm_spec USER_DEFINED_1 */ +#endif /* BTE_SIM_APP */ +}; + +tBTA_DM_PM_TYPE_QUALIFIER tBTM_PM_PWR_MD bta_dm_pm_md[] = +{ +/* more sniff parameter entries can be added for BTA_DM_PM_SNIFF3 - BTA_DM_PM_SNIFF7, if needed +When entries are added or removed, BTA_DM_PM_PARK_IDX needs to be updated to reflect the actual index +BTA_DM_PM_PARK_IDX is defined in bta_api.h and can be override by the buildcfg.h settings. +The SNIFF table entries must be in the order from highest latency (biggest interval) to lowest latency. +If there's a conflict among the connected services, the setting with lowest latency wins. +*/ +/* sniff modes: max interval, min interval, attempt, timeout */ + {800, 400, 4, 1, BTM_PM_MD_SNIFF}, /*for BTA_DM_PM_SNIFF - A2DP */ + {400, 200, 4, 1, BTM_PM_MD_SNIFF}, /*for BTA_DM_PM_SNIFF1 */ + {180, 150, 4, 1, BTM_PM_MD_SNIFF}, /*for BTA_DM_PM_SNIFF2- HD idle */ + {150, 50, 4, 1, BTM_PM_MD_SNIFF}, /*for BTA_DM_PM_SNIFF3- SCO open */ + { 54, 30, 4, 1, BTM_PM_MD_SNIFF}, /*for BTA_DM_PM_SNIFF4- HD active*/ + {800, 400, 0, 0, BTM_PM_MD_PARK} + +#ifdef BTE_SIM_APP /* For Insight builds only */ + /* Entries at the end of the bta_dm_pm_md table are user-defined (runtime configurable), + for power consumption experiments. + Insight finds the first user-defined entry by looking for the first 'max=0'. + The number of user_defined specs is defined by BTA_SWRAP_UD_PM_DM_COUNT */ + , + {0}, /* CONN_OPEN/SCO_CLOSE power mode settings for pm_spec USER_DEFINED_0 */ + {0}, /* SCO_OPEN power mode settings for pm_spec USER_DEFINED_0 */ + + {0}, /* CONN_OPEN/SCO_CLOSE power mode settings for pm_spec USER_DEFINED_1 */ + {0} /* SCO_OPEN power mode settings for pm_spec USER_DEFINED_1 */ +#endif /* BTE_SIM_APP */ +}; + +/* 0=max_lat -> no SSR */ +/* the smaller of the SSR max latency wins. + * the entries in this table must be from highest latency (biggest interval) to lowest latency */ +#if (BTM_SSR_INCLUDED == TRUE) +tBTA_DM_SSR_SPEC bta_dm_ssr_spec[] = +{ + /*max_lat, min_rmt_to, min_loc_to*/ + {0, 0, 0}, /* BTA_DM_PM_SSR0 - do not use SSR */ + {1600, 2, 2}, /* BTA_DM_PM_SSR1 - HH */ + {1200, 2, 2}, /* BTA_DM_PM_SSR2 - others (as long as sniff is allowed)*/ + {360, 160, 2} /* BTA_DM_PM_SSR3 - HD */ +}; + +tBTA_DM_SSR_SPEC *p_bta_dm_ssr_spec = (tBTA_DM_SSR_SPEC *)&bta_dm_ssr_spec; +#endif + +tBTA_DM_PM_CFG *p_bta_dm_pm_cfg = (tBTA_DM_PM_CFG *)&bta_dm_pm_cfg; +tBTA_DM_PM_SPEC *p_bta_dm_pm_spec = (tBTA_DM_PM_SPEC *)&bta_dm_pm_spec; +tBTM_PM_PWR_MD *p_bta_dm_pm_md = (tBTM_PM_PWR_MD *)&bta_dm_pm_md; + +/* The performance impact of EIR packet size +** +** When BTM_EIR_DEFAULT_FEC_REQUIRED is TRUE, +** 1 to 17 bytes, DM1 is used and most robust. +** 18 to 121 bytes, DM3 is used but impacts inquiry scan time with large number +** of devices.(almost double with 150 users) +** 122 to 224 bytes, DM5 is used but cause quite big performance loss even with +** small number of users. so it is not recommended. +** 225 to 240 bytes, DH5 is used without FEC but it not recommended. +** (same reason of DM5) +** +** When BTM_EIR_DEFAULT_FEC_REQUIRED is FALSE, +** 1 to 27 bytes, DH1 is used but only robust at short range. +** 28 to 183 bytes, DH3 is used but only robust at short range and impacts inquiry +** scan time with large number of devices. +** 184 to 240 bytes, DH5 is used but it not recommended. +*/ + +#if ( BTM_EIR_SERVER_INCLUDED == TRUE ) +#if (BTA_EIR_CANNED_UUID_LIST == TRUE) + /* for example */ +const UINT8 bta_dm_eir_uuid16_list[] = { 0x08, 0x11, /* Headset */ + 0x1E, 0x11, /* Handsfree */ + 0x0E, 0x11, /* AV Remote Control */ + 0x0B, 0x11, /* Audio Sink */ +}; +#endif + +/* Extended Inquiry Response */ +const tBTA_DM_EIR_CONF bta_dm_eir_cfg = +{ + 50, /* minimum length of local name when it is shortened */ + /* if length of local name is longer than this and EIR has not enough */ + /* room for all UUID list then local name is shortened to this length */ +#if (BTA_EIR_CANNED_UUID_LIST == TRUE) + 8, + (UINT8 *)bta_dm_eir_uuid16_list, +#else + { /* mask of UUID list in EIR */ + 0xFFFFFFFF, /* LSB is the first UUID of the first 32 UUIDs in BTM_EIR_UUID_LKUP_TBL */ + 0xFFFFFFFF /* LSB is the first UUID of the next 32 UUIDs in BTM_EIR_UUID_LKUP_TBL */ + /* BTM_EIR_UUID_LKUP_TBL can be overrided */ + }, +#endif + NULL, /* Inquiry TX power */ + 0, /* length of flags in bytes */ + NULL, /* flags for EIR */ + 0, /* length of manufacturer specific in bytes */ + NULL, /* manufacturer specific */ +}; +tBTA_DM_EIR_CONF *p_bta_dm_eir_cfg = (tBTA_DM_EIR_CONF*)&bta_dm_eir_cfg; +#endif diff --git a/bta/dm/bta_dm_ci.c b/bta/dm/bta_dm_ci.c new file mode 100644 index 0000000..b003cbe --- /dev/null +++ b/bta/dm/bta_dm_ci.c @@ -0,0 +1,106 @@ +/***************************************************************************** +** +** Name: bta_dm_ci.c +** +** Description: This is the API implementation file for the BTA +** device manager. +** +** Copyright (c) 2003-2009, Broadcom Corp., All Rights Reserved. +** Widcomm Bluetooth Core. Proprietary and confidential. +** +*****************************************************************************/ + +#include "gki.h" +#include "bd.h" +#include "bta_sys.h" +#include "bta_api.h" +#include "bta_dm_int.h" +#include <string.h> +#include "bta_dm_ci.h" + + +#if (BTM_OOB_INCLUDED == TRUE) +/******************************************************************************* +** +** Function bta_dm_ci_io_req +** +** Description This function must be called in response to function +** bta_dm_co_io_req(), if *p_oob_data to BTA_OOB_UNKNOWN +** by bta_dm_co_io_req(). +** +** Returns void +** +*******************************************************************************/ +void bta_dm_ci_io_req(BD_ADDR bd_addr, tBTA_IO_CAP io_cap, tBTA_OOB_DATA oob_data, + tBTA_AUTH_REQ auth_req) + +{ + tBTA_DM_CI_IO_REQ *p_msg; + + if ((p_msg = (tBTA_DM_CI_IO_REQ *) GKI_getbuf(sizeof(tBTA_DM_CI_IO_REQ))) != NULL) + { + p_msg->hdr.event = BTA_DM_CI_IO_REQ_EVT; + bdcpy(p_msg->bd_addr, bd_addr); + p_msg->io_cap = io_cap; + p_msg->oob_data = oob_data; + p_msg->auth_req = auth_req; + bta_sys_sendmsg(p_msg); + } +} + +/******************************************************************************* +** +** Function bta_dm_ci_rmt_oob +** +** Description This function must be called in response to function +** bta_dm_co_rmt_oob() to provide the OOB data associated +** with the remote device. +** +** Returns void +** +*******************************************************************************/ +void bta_dm_ci_rmt_oob(BOOLEAN accept, BD_ADDR bd_addr, BT_OCTET16 c, BT_OCTET16 r) +{ + tBTA_DM_CI_RMT_OOB *p_msg; + + if ((p_msg = (tBTA_DM_CI_RMT_OOB *) GKI_getbuf(sizeof(tBTA_DM_CI_RMT_OOB))) != NULL) + { + p_msg->hdr.event = BTA_DM_CI_RMT_OOB_EVT; + bdcpy(p_msg->bd_addr, bd_addr); + p_msg->accept = accept; + memcpy(p_msg->c, c, BT_OCTET16_LEN); + memcpy(p_msg->r, r, BT_OCTET16_LEN); + bta_sys_sendmsg(p_msg); + } +} +#endif /* BTM_OOB_INCLUDED */ + +#if (BTM_SCO_HCI_INCLUDED == TRUE) +/******************************************************************************* +** +** Function bta_dm_sco_ci_data_ready +** +** Description This function sends an event to indicating that the phone +** has SCO data ready. +** +** Parameters event: is obtained from bta_dm_sco_co_open() function, which +** is the BTA event we want to send back to BTA module +** when there is encoded data ready. +** sco_handle: is the BTA sco handle which indicate a specific +** SCO connection. +** Returns void +** +*******************************************************************************/ +void bta_dm_sco_ci_data_ready(UINT16 event, UINT16 sco_handle) +{ + BT_HDR *p_buf; + + if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL) + { + p_buf->event = event; + p_buf->layer_specific = sco_handle; + + bta_sys_sendmsg(p_buf); + } +} +#endif diff --git a/bta/dm/bta_dm_int.h b/bta/dm/bta_dm_int.h new file mode 100644 index 0000000..5f1da2a --- /dev/null +++ b/bta/dm/bta_dm_int.h @@ -0,0 +1,956 @@ +/***************************************************************************** +** +** Name: bta_dm_int.h +** +** Description: This is the private interface file for the BTA device +** manager. +** +** Copyright (c) 2003-2012, Broadcom Corp., All Rights Reserved. +** Broadcom Bluetooth Core. Proprietary and confidential. +** +*****************************************************************************/ +#ifndef BTA_DM_INT_H +#define BTA_DM_INT_H + +#include "bt_target.h" + +#if (BLE_INCLUDED == TRUE && (defined BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)) + #include "bta_gatt_api.h" +#endif + + + +/***************************************************************************** +** Constants and data types +*****************************************************************************/ + + +#define BTA_COPY_DEVICE_CLASS(coddst, codsrc) {((UINT8 *)(coddst))[0] = ((UINT8 *)(codsrc))[0]; \ + ((UINT8 *)(coddst))[1] = ((UINT8 *)(codsrc))[1]; \ + ((UINT8 *)(coddst))[2] = ((UINT8 *)(codsrc))[2];} + + +#define BTA_DM_MSG_LEN 50 + +#define BTA_SERVICE_ID_TO_SERVICE_MASK(id) (1 << (id)) + +/* DM events */ +enum +{ + /* device manager local device API events */ + BTA_DM_API_ENABLE_EVT = BTA_SYS_EVT_START(BTA_ID_DM), + BTA_DM_API_DISABLE_EVT, + BTA_DM_API_SET_NAME_EVT, + BTA_DM_API_SET_VISIBILITY_EVT, + BTA_DM_API_SET_AFH_CHANNELS_EVT, + BTA_API_DM_SIG_STRENGTH_EVT, + BTA_DM_API_VENDOR_SPECIFIC_COMMAND_EVT, + BTA_DM_API_TX_INQPWR_EVT, + BTA_DM_ACL_CHANGE_EVT, + BTA_DM_API_ADD_DEVICE_EVT, + + /* security API events */ + BTA_DM_API_BOND_EVT, + BTA_DM_API_BOND_CANCEL_EVT, + BTA_DM_API_PIN_REPLY_EVT, + BTA_DM_API_LINK_POLICY_EVT, + BTA_DM_API_AUTH_REPLY_EVT, + + /* power manger events */ + BTA_DM_PM_BTM_STATUS_EVT, + BTA_DM_PM_TIMER_EVT, + + /* simple pairing events */ + BTA_DM_API_CONFIRM_EVT, + + BTA_DM_API_SET_ENCRYPTION_EVT, + +#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE) + BTA_DM_API_PASKY_CANCEL_EVT, +#endif +#if (BTM_OOB_INCLUDED == TRUE) + BTA_DM_API_LOC_OOB_EVT, + BTA_DM_CI_IO_REQ_EVT, + BTA_DM_CI_RMT_OOB_EVT, +#endif /* BTM_OOB_INCLUDED */ + +#if BLE_INCLUDED == TRUE + BTA_DM_API_ADD_BLEKEY_EVT, + BTA_DM_API_ADD_BLEDEVICE_EVT, + BTA_DM_API_BLE_PASSKEY_REPLY_EVT, + BTA_DM_API_BLE_SEC_GRANT_EVT, + BTA_DM_API_BLE_SET_BG_CONN_TYPE, + BTA_DM_API_BLE_CONN_PARAM_EVT, + BTA_DM_API_BLE_SCAN_PARAM_EVT, +#endif + +#if ( BTM_EIR_SERVER_INCLUDED == TRUE )&&( BTA_EIR_CANNED_UUID_LIST != TRUE )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) + BTA_DM_API_UPDATE_EIR_UUID_EVT, +#endif +#if (BTM_EIR_SERVER_INCLUDED == TRUE) + BTA_DM_API_SET_EIR_CONFIG_EVT, +#endif + + BTA_DM_API_ENABLE_TEST_MODE_EVT, + BTA_DM_API_DISABLE_TEST_MODE_EVT, + BTA_DM_API_EXECUTE_CBACK_EVT, + BTA_DM_API_SET_AFH_CHANNEL_ASSESMENT_EVT, + BTA_DM_MAX_EVT +}; + + +/* DM search events */ +enum +{ + /* DM search API events */ + BTA_DM_API_SEARCH_EVT = BTA_SYS_EVT_START(BTA_ID_DM_SEARCH), + BTA_DM_API_SEARCH_CANCEL_EVT, + BTA_DM_API_DISCOVER_EVT, + BTA_DM_INQUIRY_CMPL_EVT, + BTA_DM_REMT_NAME_EVT, + BTA_DM_SDP_RESULT_EVT, + BTA_DM_SEARCH_CMPL_EVT, + BTA_DM_DISCOVERY_RESULT_EVT, + BTA_DM_API_DI_DISCOVER_EVT + +}; + +/* data type for BTA_DM_API_ENABLE_EVT */ +typedef struct +{ + BT_HDR hdr; + tBTA_DM_SEC_CBACK *p_sec_cback; +} tBTA_DM_API_ENABLE; + +/* data type for BTA_DM_API_SET_NAME_EVT */ +typedef struct +{ + BT_HDR hdr; + char name[BD_NAME_LEN]; +} tBTA_DM_API_SET_NAME; + +/* data type for BTA_DM_API_SET_VISIBILITY_EVT */ +typedef struct +{ + BT_HDR hdr; + tBTA_DM_DISC disc_mode; + tBTA_DM_CONN conn_mode; + UINT8 pair_mode; + UINT8 conn_paired_only; +} tBTA_DM_API_SET_VISIBILITY; + +/* data type for BTA_DM_API_SET_AFH_CHANNELS_EVT */ +typedef struct +{ + BT_HDR hdr; + UINT8 first; + UINT8 last; +} tBTA_DM_API_SET_AFH_CHANNELS_EVT; + +/* data type for BTA_DM_API_VENDOR_SPECIFIC_COMMAND_EVT */ +typedef struct +{ + BT_HDR hdr; + UINT16 opcode; + UINT8 param_len; + UINT8 *p_param_buf; + tBTA_VENDOR_CMPL_CBACK *p_cback; + +} tBTA_DM_API_VENDOR_SPECIFIC_COMMAND; + +enum +{ + BTA_DM_RS_NONE, /* straight API call */ + BTA_DM_RS_OK, /* the role switch result - successful */ + BTA_DM_RS_FAIL /* the role switch result - failed */ +}; +typedef UINT8 tBTA_DM_RS_RES; + +/* data type for BTA_DM_API_SEARCH_EVT */ +typedef struct +{ + BT_HDR hdr; + tBTA_DM_INQ inq_params; + tBTA_SERVICE_MASK services; + tBTA_DM_SEARCH_CBACK * p_cback; + tBTA_DM_RS_RES rs_res; +#if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE + UINT8 num_uuid; + tBT_UUID *p_uuid; +#endif +} tBTA_DM_API_SEARCH; + +/* data type for BTA_DM_API_DISCOVER_EVT */ +typedef struct +{ + BT_HDR hdr; + BD_ADDR bd_addr; + tBTA_SERVICE_MASK services; + tBTA_DM_SEARCH_CBACK * p_cback; + BOOLEAN sdp_search; +#if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE + UINT8 num_uuid; + tBT_UUID *p_uuid; +#endif + tSDP_UUID uuid; +} tBTA_DM_API_DISCOVER; + +/* data type for BTA_DM_API_DI_DISC_EVT */ +typedef struct +{ + BT_HDR hdr; + BD_ADDR bd_addr; + tBTA_DISCOVERY_DB *p_sdp_db; + UINT32 len; + tBTA_DM_SEARCH_CBACK * p_cback; +}tBTA_DM_API_DI_DISC; + +/* data type for BTA_DM_API_BOND_EVT */ +typedef struct +{ + BT_HDR hdr; + BD_ADDR bd_addr; +} tBTA_DM_API_BOND; + +/* data type for BTA_DM_API_BOND_CANCEL_EVT */ +typedef struct +{ + BT_HDR hdr; + BD_ADDR bd_addr; +} tBTA_DM_API_BOND_CANCEL; + +/* data type for BTA_DM_API_PIN_REPLY_EVT */ +typedef struct +{ + BT_HDR hdr; + BD_ADDR bd_addr; + BOOLEAN accept; + UINT8 pin_len; + UINT8 p_pin[PIN_CODE_LEN]; +} tBTA_DM_API_PIN_REPLY; + +/* data type for BTA_DM_API_LINK_POLICY_EVT */ +typedef struct +{ + BT_HDR hdr; + BD_ADDR bd_addr; + UINT16 policy_mask; + BOOLEAN set; +} tBTA_DM_API_LINK_POLICY; + +/* data type for BTA_DM_API_AUTH_REPLY_EVT */ +typedef struct +{ + BT_HDR hdr; + BD_ADDR bd_addr; + tBTA_SERVICE_ID service; + tBTA_AUTH_RESP response; +} tBTA_DM_API_AUTH_REPLY; + +/* data type for BTA_DM_API_LOC_OOB_EVT */ +typedef struct +{ + BT_HDR hdr; +} tBTA_DM_API_LOC_OOB; + +/* data type for BTA_DM_API_CONFIRM_EVT */ +typedef struct +{ + BT_HDR hdr; + BD_ADDR bd_addr; + BOOLEAN accept; +} tBTA_DM_API_CONFIRM; + +/* data type for BTA_DM_API_PASKY_CANCEL_EVT*/ +typedef struct +{ + BT_HDR hdr; + BD_ADDR bd_addr; +} tBTA_DM_API_PASKY_CANCEL; + +/* data type for BTA_DM_CI_IO_REQ_EVT */ +typedef struct +{ + BT_HDR hdr; + BD_ADDR bd_addr; + tBTA_IO_CAP io_cap; + tBTA_OOB_DATA oob_data; + tBTA_AUTH_REQ auth_req; +} tBTA_DM_CI_IO_REQ; + +/* data type for BTA_DM_CI_RMT_OOB_EVT */ +typedef struct +{ + BT_HDR hdr; + BD_ADDR bd_addr; + BT_OCTET16 c; + BT_OCTET16 r; + BOOLEAN accept; +} tBTA_DM_CI_RMT_OOB; + +/* data type for BTA_DM_REMT_NAME_EVT */ +typedef struct +{ + BT_HDR hdr; + tBTA_DM_SEARCH result; +} tBTA_DM_REM_NAME; + +/* data type for tBTA_DM_DISC_RESULT */ +typedef struct +{ + BT_HDR hdr; + tBTA_DM_SEARCH result; +} tBTA_DM_DISC_RESULT; + + +/* data type for BTA_DM_INQUIRY_CMPL_EVT */ +typedef struct +{ + BT_HDR hdr; + UINT8 num; +} tBTA_DM_INQUIRY_CMPL; + +/* data type for BTA_DM_SDP_RESULT_EVT */ +typedef struct +{ + BT_HDR hdr; + UINT16 sdp_result; +} tBTA_DM_SDP_RESULT; + +/* data type for BTA_API_DM_SIG_STRENGTH_EVT */ +typedef struct +{ + BT_HDR hdr; + tBTA_SIG_STRENGTH_MASK mask; + UINT16 period; + BOOLEAN start; +} tBTA_API_DM_SIG_STRENGTH; + +/* data type for tBTA_API_DM_TX_INQPWR */ +typedef struct +{ + BT_HDR hdr; + INT8 tx_power; +}tBTA_API_DM_TX_INQPWR; + +/* data type for BTA_DM_ACL_CHANGE_EVT */ +typedef struct +{ + BT_HDR hdr; + tBTM_BL_EVENT event; + UINT8 busy_level; + BOOLEAN is_new; + UINT8 new_role; + BD_ADDR bd_addr; + UINT8 hci_status; +} tBTA_DM_ACL_CHANGE; + +/* data type for BTA_DM_PM_BTM_STATUS_EVT */ +typedef struct +{ + + BT_HDR hdr; + BD_ADDR bd_addr; + tBTM_PM_STATUS status; + UINT16 value; + UINT8 hci_status; + +} tBTA_DM_PM_BTM_STATUS; + +/* data type for BTA_DM_PM_TIMER_EVT */ +typedef struct +{ + BT_HDR hdr; + BD_ADDR bd_addr; + +} tBTA_DM_PM_TIMER; + + +/* data type for BTA_DM_API_ADD_DEVICE_EVT */ +typedef struct +{ + BT_HDR hdr; + BD_ADDR bd_addr; + DEV_CLASS dc; + LINK_KEY link_key; + tBTA_SERVICE_MASK tm; + BOOLEAN is_trusted; + UINT8 key_type; + tBTA_IO_CAP io_cap; + BOOLEAN link_key_known; + BOOLEAN dc_known; + BD_NAME bd_name; + BD_FEATURES features; +} tBTA_DM_API_ADD_DEVICE; + +/* data type for BTA_DM_API_EXECUTE_CBACK_EVT */ +typedef struct +{ + BT_HDR hdr; + void * p_param; + tBTA_DM_EXEC_CBACK *p_exec_cback; +} tBTA_DM_API_EXECUTE_CBACK; + +/* data type for tBTA_DM_API_SET_ENCRYPTION */ +typedef struct +{ + BT_HDR hdr; + tBTA_DM_ENCRYPT_CBACK *p_callback; + tBTA_DM_BLE_SEC_ACT sec_act; + BD_ADDR bd_addr; +} tBTA_DM_API_SET_ENCRYPTION; + +#if BLE_INCLUDED == TRUE +typedef struct +{ + BT_HDR hdr; + BD_ADDR bd_addr; + tBTA_LE_KEY_VALUE blekey; + tBTA_LE_KEY_TYPE key_type; + +}tBTA_DM_API_ADD_BLEKEY; + +typedef struct +{ + BT_HDR hdr; + BD_ADDR bd_addr; + tBT_DEVICE_TYPE dev_type ; + tBLE_ADDR_TYPE addr_type; + +}tBTA_DM_API_ADD_BLE_DEVICE; + +typedef struct +{ + BT_HDR hdr; + BD_ADDR bd_addr; + BOOLEAN accept; + UINT32 passkey; +}tBTA_DM_API_PASSKEY_REPLY; + +typedef struct +{ + BT_HDR hdr; + BD_ADDR bd_addr; + tBTA_DM_BLE_SEC_GRANT res; +}tBTA_DM_API_BLE_SEC_GRANT; + + +typedef struct +{ + BT_HDR hdr; + tBTA_DM_BLE_CONN_TYPE bg_conn_type; + tBTA_DM_BLE_SEL_CBACK *p_select_cback; +}tBTA_DM_API_BLE_SET_BG_CONN_TYPE; + +/* set prefered BLE connection parameters for a device */ +typedef struct +{ + BT_HDR hdr; + BD_ADDR peer_bda; + UINT16 conn_int_min; + UINT16 conn_int_max; + UINT16 supervision_tout; + UINT16 slave_latency; + +}tBTA_DM_API_BLE_CONN_PARAMS; + +/* set scan parameter for BLE connections */ +typedef struct +{ + BT_HDR hdr; + UINT16 scan_int; + UINT16 scan_window; +}tBTA_DM_API_BLE_SCAN_PARAMS; + +#endif + +typedef struct +{ + BT_HDR hdr; + BOOLEAN enable_or_disable; +}tBTA_DM_API_SET_AFH_CHANNEL_ASSESSMENT; + +#if ( BTM_EIR_SERVER_INCLUDED == TRUE )&&( BTA_EIR_CANNED_UUID_LIST != TRUE )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) +/* data type for BTA_DM_API_UPDATE_EIR_UUID_EVT */ +typedef struct +{ + BT_HDR hdr; + BOOLEAN is_add; + tBT_UUID uuid; +}tBTA_DM_API_UPDATE_EIR_UUID; +#endif + +#if (BTM_EIR_SERVER_INCLUDED == TRUE) +/* data type for BTA_DM_API_SET_EIR_CONFIG_EVT */ +typedef struct +{ + BT_HDR hdr; + tBTA_DM_EIR_CONF *p_eir_cfg; +}tBTA_DM_API_SET_EIR_CONFIG; +#endif + +/* union of all data types */ +typedef union +{ + /* GKI event buffer header */ + BT_HDR hdr; + tBTA_DM_API_ENABLE enable; + + tBTA_DM_API_SET_NAME set_name; + + tBTA_DM_API_SET_VISIBILITY set_visibility; + + tBTA_DM_API_SET_AFH_CHANNELS_EVT set_afhchannels; + + tBTA_DM_API_VENDOR_SPECIFIC_COMMAND vendor_command; + + tBTA_DM_API_ADD_DEVICE add_dev; + + tBTA_DM_API_SEARCH search; + + tBTA_DM_API_DISCOVER discover; + + tBTA_DM_API_BOND bond; + + tBTA_DM_API_BOND_CANCEL bond_cancel; + + tBTA_DM_API_PIN_REPLY pin_reply; + tBTA_DM_API_LINK_POLICY link_policy; + + tBTA_DM_API_LOC_OOB loc_oob; + tBTA_DM_API_CONFIRM confirm; + tBTA_DM_API_PASKY_CANCEL passkey_cancel; + tBTA_DM_CI_IO_REQ ci_io_req; + tBTA_DM_CI_RMT_OOB ci_rmt_oob; + + tBTA_DM_API_AUTH_REPLY auth_reply; + + tBTA_DM_REM_NAME rem_name; + + tBTA_DM_DISC_RESULT disc_result; + + tBTA_DM_INQUIRY_CMPL inq_cmpl; + + tBTA_DM_SDP_RESULT sdp_event; + + tBTA_API_DM_SIG_STRENGTH sig_strength; + + tBTA_API_DM_TX_INQPWR tx_inq_pwr; + + tBTA_DM_ACL_CHANGE acl_change; + + tBTA_DM_PM_BTM_STATUS pm_status; + + tBTA_DM_PM_TIMER pm_timer; + + tBTA_DM_API_DI_DISC di_disc; + + tBTA_DM_API_EXECUTE_CBACK exec_cback; + + tBTA_DM_API_SET_ENCRYPTION set_encryption; + +#if BLE_INCLUDED == TRUE + tBTA_DM_API_ADD_BLEKEY add_ble_key; + tBTA_DM_API_ADD_BLE_DEVICE add_ble_device; + tBTA_DM_API_PASSKEY_REPLY ble_passkey_reply; + tBTA_DM_API_BLE_SEC_GRANT ble_sec_grant; + tBTA_DM_API_BLE_SET_BG_CONN_TYPE ble_set_bd_conn_type; + tBTA_DM_API_BLE_CONN_PARAMS ble_set_conn_params; + tBTA_DM_API_BLE_SCAN_PARAMS ble_set_scan_params; +#endif + + tBTA_DM_API_SET_AFH_CHANNEL_ASSESSMENT set_afh_channel_assessment; + +#if ( BTM_EIR_SERVER_INCLUDED == TRUE )&&( BTA_EIR_CANNED_UUID_LIST != TRUE )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) + tBTA_DM_API_UPDATE_EIR_UUID update_eir_uuid; +#endif +#if (BTM_EIR_SERVER_INCLUDED == TRUE) + tBTA_DM_API_SET_EIR_CONFIG set_eir_cfg; +#endif + +} tBTA_DM_MSG; + + +#define BTA_DM_NUM_PEER_DEVICE 7 + +#define BTA_DM_NOT_CONNECTED 0 +#define BTA_DM_CONNECTED 1 + +typedef UINT8 tBTA_DM_CONN_STATE; + + +#define BTA_DM_DI_NONE 0x00 /* nothing special */ +#define BTA_DM_DI_USE_SSR 0x10 /* set this bit if ssr is supported for this link */ +#define BTA_DM_DI_AV_ACTIVE 0x20 /* set this bit if AV is active for this link */ +#define BTA_DM_DI_SET_SNIFF 0x01 /* set this bit if call BTM_SetPowerMode(sniff) */ +#define BTA_DM_DI_INT_SNIFF 0x02 /* set this bit if call BTM_SetPowerMode(sniff) & enter sniff mode */ +#define BTA_DM_DI_ACP_SNIFF 0x04 /* set this bit if peer init sniff */ +typedef UINT8 tBTA_DM_DEV_INFO; + +typedef struct +{ + BD_ADDR peer_bdaddr; + UINT16 link_policy; + tBTA_DM_CONN_STATE conn_state; + tBTA_PREF_ROLES pref_role; + BOOLEAN in_use; + tBTA_DM_DEV_INFO info; +#if (BTM_SSR_INCLUDED == TRUE) + tBTM_PM_STATUS prev_low; /* previous low power mode used */ +#endif + tBTA_DM_PM_ACTTION pm_mode_attempted; + tBTA_DM_PM_ACTTION pm_mode_failed; + +} tBTA_DM_PEER_DEVICE; + + + +/* structure to store list of + active connections */ +typedef struct +{ + tBTA_DM_PEER_DEVICE peer_device[BTA_DM_NUM_PEER_DEVICE]; + UINT8 count; + +} tBTA_DM_ACTIVE_LINK; + + +typedef struct +{ + BD_ADDR peer_bdaddr; + tBTA_SYS_ID id; + UINT8 app_id; + tBTA_SYS_CONN_STATUS state; + + +} tBTA_DM_SRVCS; + +#define BTA_DM_NUM_CONN_SRVS 5 + +typedef struct +{ + + UINT8 count; + tBTA_DM_SRVCS conn_srvc[BTA_DM_NUM_CONN_SRVS]; + +} tBTA_DM_CONNECTED_SRVCS; + +typedef struct +{ + TIMER_LIST_ENT timer; + BD_ADDR peer_bdaddr; + BOOLEAN in_use; + +} tBTA_PM_TIMER; + +extern tBTA_DM_CONNECTED_SRVCS bta_dm_conn_srvcs; + +#define BTA_DM_NUM_PM_TIMER 3 + +/* DM control block */ +typedef struct +{ + BOOLEAN is_bta_dm_active; + tBTA_DM_ACTIVE_LINK device_list; + tBTA_DM_SEC_CBACK *p_sec_cback; + TIMER_LIST_ENT signal_strength_timer; + tBTA_SIG_STRENGTH_MASK signal_strength_mask; + UINT16 state; + UINT16 signal_strength_period; + BOOLEAN disabling; + TIMER_LIST_ENT disable_timer; + UINT32 wbt_sdp_handle; /* WIDCOMM Extensions SDP record handle */ + UINT8 wbt_scn; /* WIDCOMM Extensions SCN */ + UINT8 num_master_only; + UINT8 pm_id; + tBTA_PM_TIMER pm_timer[BTA_DM_NUM_PM_TIMER]; + UINT32 role_policy_mask; /* the bits set indicates the modules that wants to remove role switch from the default link policy */ + UINT16 cur_policy; /* current default link policy */ + UINT16 rs_event; /* the event waiting for role switch */ + UINT8 cur_av_count; /* current AV connecions */ + BOOLEAN disable_pair_mode; /* disable pair mode or not */ + BOOLEAN conn_paired_only; /* allow connectable to paired device only or not */ + tBTA_DM_API_SEARCH search_msg; + UINT16 page_scan_interval; + UINT16 page_scan_window; + UINT16 inquiry_scan_interval; + UINT16 inquiry_scan_window; + + /* Storage for pin code request parameters */ + BD_ADDR pin_bd_addr; + DEV_CLASS pin_dev_class; + tBTA_DM_SEC_EVT pin_evt; + UINT32 num_val; /* the numeric value for comparison. If just_works, do not show this number to UI */ + BOOLEAN just_works; /* TRUE, if "Just Works" association model */ +#if ( BTM_EIR_SERVER_INCLUDED == TRUE )&&( BTA_EIR_CANNED_UUID_LIST != TRUE ) + /* store UUID list for EIR */ + TIMER_LIST_ENT app_ready_timer; + UINT32 eir_uuid[BTM_EIR_SERVICE_ARRAY_SIZE]; +#if (BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) + tBT_UUID custom_uuid[BTA_EIR_SERVER_NUM_CUSTOM_UUID]; +#endif + +#endif + + tBTA_DM_ENCRYPT_CBACK *p_encrypt_cback; + tBTA_DM_BLE_SEC_ACT sec_act; + +} tBTA_DM_CB; + +#ifndef BTA_DM_SDP_DB_SIZE +#define BTA_DM_SDP_DB_SIZE 250 +#endif + +/* DM search control block */ +typedef struct +{ + + tBTA_DM_SEARCH_CBACK * p_search_cback; + tBTM_INQ_INFO * p_btm_inq_info; + tBTA_SERVICE_MASK services; + tBTA_SERVICE_MASK services_to_search; + tBTA_SERVICE_MASK services_found; + tSDP_DISCOVERY_DB * p_sdp_db; + UINT16 state; + BD_ADDR peer_bdaddr; + BOOLEAN name_discover_done; + char peer_name[BD_NAME_LEN]; + TIMER_LIST_ENT search_timer; + UINT8 service_index; + tBTA_DM_MSG * p_search_queue; /* search or discover commands during search cancel stored here */ + BOOLEAN wait_disc; + BOOLEAN sdp_results; + tSDP_UUID uuid; + UINT8 peer_scn; + BOOLEAN sdp_search; + +#if ((defined BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) +#if ((defined BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)) + tBTA_GATTC_IF client_if; + UINT8 num_uuid; + tBT_UUID *p_srvc_uuid; + UINT8 uuid_to_search; + BOOLEAN gatt_disc_active; + UINT16 conn_id; + UINT8 * p_ble_rawdata; + UINT32 ble_raw_size; + UINT32 ble_raw_used; +#endif +#endif + + +} tBTA_DM_SEARCH_CB; + +/* DI control block */ +typedef struct +{ + tSDP_DISCOVERY_DB * p_di_db; /* pointer to the DI discovery database */ + UINT8 di_num; /* total local DI record number */ + UINT32 di_handle[BTA_DI_NUM_MAX]; /* local DI record handle, the first one is primary record */ +}tBTA_DM_DI_CB; + +/* DM search state */ +enum +{ + + BTA_DM_SEARCH_IDLE, + BTA_DM_SEARCH_ACTIVE, + BTA_DM_SEARCH_CANCELLING, + BTA_DM_DISCOVER_ACTIVE + +}; + + + +typedef struct +{ + DEV_CLASS dev_class; /* local device class */ + UINT16 policy_settings; /* link policy setting hold, sniff, park, MS switch */ + UINT16 page_timeout; /* timeout for page in slots */ + UINT16 link_timeout; /* link supervision timeout in slots */ + BOOLEAN avoid_scatter; /* TRUE to avoid scatternet when av is streaming (be the master) */ + +} tBTA_DM_CFG; + +extern const UINT32 bta_service_id_to_btm_srv_id_lkup_tbl[]; + +extern const tBTA_DM_CFG bta_dm_cfg; + + + +#define BTA_ALL_APP_ID 0xff + +typedef struct +{ + UINT8 id; + UINT8 app_id; + UINT8 cfg; + +} tBTA_DM_RM ; + +extern tBTA_DM_CFG *p_bta_dm_cfg; +extern tBTA_DM_RM *p_bta_dm_rm_cfg; + +typedef struct +{ + + UINT8 id; + UINT8 app_id; + UINT8 spec_idx; /* index of spec table to use */ + +} tBTA_DM_PM_CFG; + + +typedef struct +{ + + tBTA_DM_PM_ACTTION power_mode; + UINT16 timeout; + +} tBTA_DM_PM_ACTN; + +typedef struct +{ + + UINT8 allow_mask; /* mask of sniff/hold/park modes to allow */ +#if (BTM_SSR_INCLUDED == TRUE) + UINT8 ssr; /* set SSR on conn open/unpark */ +#endif + tBTA_DM_PM_ACTN actn_tbl [BTA_DM_PM_NUM_EVTS][2]; + +} tBTA_DM_PM_SPEC; + +typedef struct +{ + UINT16 max_lat; + UINT16 min_rmt_to; + UINT16 min_loc_to; +} tBTA_DM_SSR_SPEC; + +extern tBTA_DM_PM_CFG *p_bta_dm_pm_cfg; +extern tBTA_DM_PM_SPEC *p_bta_dm_pm_spec; +extern tBTM_PM_PWR_MD *p_bta_dm_pm_md; +#if (BTM_SSR_INCLUDED == TRUE) +extern tBTA_DM_SSR_SPEC *p_bta_dm_ssr_spec; +#endif + +#if ( BTM_EIR_SERVER_INCLUDED == TRUE ) +/* update dynamic BRCM Aware EIR data */ +extern const tBTA_DM_EIR_CONF bta_dm_eir_cfg; +extern tBTA_DM_EIR_CONF *p_bta_dm_eir_cfg; +#endif + +/* DM control block */ +#if BTA_DYNAMIC_MEMORY == FALSE +extern tBTA_DM_CB bta_dm_cb; +#else +extern tBTA_DM_CB *bta_dm_cb_ptr; +#define bta_dm_cb (*bta_dm_cb_ptr) +#endif + +/* DM search control block */ +#if BTA_DYNAMIC_MEMORY == FALSE +extern tBTA_DM_SEARCH_CB bta_dm_search_cb; +#else +extern tBTA_DM_SEARCH_CB *bta_dm_search_cb_ptr; +#define bta_dm_search_cb (*bta_dm_search_cb_ptr) +#endif + +/* DI control block */ +#if BTA_DYNAMIC_MEMORY == FALSE +extern tBTA_DM_DI_CB bta_dm_di_cb; +#else +extern tBTA_DM_DI_CB *bta_dm_di_cb_ptr; +#define bta_dm_di_cb (*bta_dm_di_cb_ptr) +#endif + +extern BOOLEAN bta_dm_sm_execute(BT_HDR *p_msg); +extern void bta_dm_sm_disable( void ); +extern BOOLEAN bta_dm_search_sm_execute(BT_HDR *p_msg); +extern void bta_dm_search_sm_disable( void ); + + +extern void bta_dm_enable (tBTA_DM_MSG *p_data); +extern void bta_dm_disable (tBTA_DM_MSG *p_data); +extern void bta_dm_set_dev_name (tBTA_DM_MSG *p_data); +extern void bta_dm_set_visibility (tBTA_DM_MSG *p_data); +extern void bta_dm_set_afhchannels (tBTA_DM_MSG *p_data); +extern void bta_dm_vendor_spec_command(tBTA_DM_MSG *p_data); +extern void bta_dm_bond (tBTA_DM_MSG *p_data); +extern void bta_dm_bond_cancel (tBTA_DM_MSG *p_data); +extern void bta_dm_pin_reply (tBTA_DM_MSG *p_data); +extern void bta_dm_link_policy (tBTA_DM_MSG *p_data); +extern void bta_dm_auth_reply (tBTA_DM_MSG *p_data); +extern void bta_dm_signal_strength(tBTA_DM_MSG *p_data); +extern void bta_dm_tx_inqpower(tBTA_DM_MSG *p_data); +extern void bta_dm_acl_change(tBTA_DM_MSG *p_data); +extern void bta_dm_add_device (tBTA_DM_MSG *p_data); + +extern void bta_dm_pm_btm_status(tBTA_DM_MSG *p_data); +extern void bta_dm_pm_timer(tBTA_DM_MSG *p_data); +extern void bta_dm_add_ampkey (tBTA_DM_MSG *p_data); + +#if BLE_INCLUDED == TRUE +extern void bta_dm_add_blekey (tBTA_DM_MSG *p_data); +extern void bta_dm_add_ble_device (tBTA_DM_MSG *p_data); +extern void bta_dm_ble_passkey_reply (tBTA_DM_MSG *p_data); +extern void bta_dm_security_grant (tBTA_DM_MSG *p_data); +extern void bta_dm_ble_set_bg_conn_type (tBTA_DM_MSG *p_data); +extern void bta_dm_ble_set_conn_params (tBTA_DM_MSG *p_data); +extern void bta_dm_ble_set_scan_params (tBTA_DM_MSG *p_data); +#endif +extern void bta_dm_set_encryption(tBTA_DM_MSG *p_data); +extern void bta_dm_confirm(tBTA_DM_MSG *p_data); +extern void bta_dm_passkey_cancel(tBTA_DM_MSG *p_data); +#if (BTM_OOB_INCLUDED == TRUE) +extern void bta_dm_loc_oob(tBTA_DM_MSG *p_data); +extern void bta_dm_ci_io_req_act(tBTA_DM_MSG *p_data); +extern void bta_dm_ci_rmt_oob_act(tBTA_DM_MSG *p_data); +#endif /* BTM_OOB_INCLUDED */ + +extern void bta_dm_init_pm(void); +extern void bta_dm_disable_pm(void); + +extern void bta_dm_search_start (tBTA_DM_MSG *p_data); +extern void bta_dm_search_cancel (tBTA_DM_MSG *p_data); +extern void bta_dm_discover (tBTA_DM_MSG *p_data); +extern void bta_dm_di_disc (tBTA_DM_MSG *p_data); +extern void bta_dm_inq_cmpl (tBTA_DM_MSG *p_data); +extern void bta_dm_rmt_name (tBTA_DM_MSG *p_data); +extern void bta_dm_sdp_result (tBTA_DM_MSG *p_data); +extern void bta_dm_search_cmpl (tBTA_DM_MSG *p_data); +extern void bta_dm_free_sdp_db (tBTA_DM_MSG *p_data); +extern void bta_dm_disc_result (tBTA_DM_MSG *p_data); +extern void bta_dm_search_result (tBTA_DM_MSG *p_data); +extern void bta_dm_discovery_cmpl (tBTA_DM_MSG *p_data); +extern void bta_dm_queue_search (tBTA_DM_MSG *p_data); +extern void bta_dm_queue_disc (tBTA_DM_MSG *p_data); +extern void bta_dm_search_clear_queue (tBTA_DM_MSG *p_data); +extern void bta_dm_search_cancel_cmpl (tBTA_DM_MSG *p_data); +extern void bta_dm_search_cancel_notify (tBTA_DM_MSG *p_data); +extern void bta_dm_search_cancel_transac_cmpl(tBTA_DM_MSG *p_data); +extern void bta_dm_disc_rmt_name (tBTA_DM_MSG *p_data); +extern tBTA_DM_PEER_DEVICE * bta_dm_find_peer_device(BD_ADDR peer_addr); + +extern void bta_dm_pm_active(BD_ADDR peer_addr); + +#if ( BTM_EIR_SERVER_INCLUDED == TRUE ) +void bta_dm_eir_update_uuid(UINT16 uuid16, BOOLEAN adding); +#else +#define bta_dm_eir_update_uuid(x, y) +#endif + +#if ( BTM_EIR_SERVER_INCLUDED == TRUE )&&( BTA_EIR_CANNED_UUID_LIST != TRUE )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) +extern void bta_dm_update_eir_uuid (tBTA_DM_MSG *p_data); +#endif +#if (BTM_EIR_SERVER_INCLUDED == TRUE) +extern void bta_dm_set_eir_config (tBTA_DM_MSG *p_data); +#endif +extern void bta_dm_enable_test_mode(tBTA_DM_MSG *p_data); +extern void bta_dm_disable_test_mode(tBTA_DM_MSG *p_data); +extern void bta_dm_execute_callback(tBTA_DM_MSG *p_data); + +extern void bta_dm_set_afh_channel_assesment(tBTA_DM_MSG *p_data); + +#endif /* BTA_DM_INT_H */ + diff --git a/bta/dm/bta_dm_main.c b/bta/dm/bta_dm_main.c new file mode 100644 index 0000000..a3dd167 --- /dev/null +++ b/bta/dm/bta_dm_main.c @@ -0,0 +1,331 @@ +/***************************************************************************** +** +** Name: bta_dm_main.c +** +** Description: This is the main implementation file for the BTA +** device manager. +** +** Copyright (c) 2003-2011, Broadcom Corp., All Rights Reserved. +** Broadcom Bluetooth Core. Proprietary and confidential. +** +*****************************************************************************/ + +#include "bta_api.h" +#include "bta_sys.h" +#include "bta_dm_int.h" + + +/***************************************************************************** +** Constants and types +*****************************************************************************/ + +#if BTA_DYNAMIC_MEMORY == FALSE +tBTA_DM_CB bta_dm_cb; +tBTA_DM_SEARCH_CB bta_dm_search_cb; +tBTA_DM_DI_CB bta_dm_di_cb; +#endif + + +#define BTA_DM_NUM_ACTIONS (BTA_DM_MAX_EVT & 0x00ff) + +/* type for action functions */ +typedef void (*tBTA_DM_ACTION)(tBTA_DM_MSG *p_data); + +/* action function list */ +const tBTA_DM_ACTION bta_dm_action[] = +{ + + /* device manager local device API events */ + bta_dm_enable, /* 0 BTA_DM_API_ENABLE_EVT */ + bta_dm_disable, /* 1 BTA_DM_API_DISABLE_EVT */ + bta_dm_set_dev_name, /* 2 BTA_DM_API_SET_NAME_EVT */ + bta_dm_set_visibility, /* 3 BTA_DM_API_SET_VISIBILITY_EVT */ + bta_dm_set_afhchannels, /* 4 BTA_DM_API_SET_AFH_CHANNELS_EVT */ + bta_dm_signal_strength, /* 5 BTA_API_DM_SIG_STRENGTH_EVT */ + bta_dm_vendor_spec_command,/* 6 BTA_DM_API_VENDOR_SPECIFIC_COMMAND_EVT */ + bta_dm_tx_inqpower, /* 7 BTA_DM_API_SIG_STRENGTH_EVT */ + bta_dm_acl_change, /* 8 BTA_DM_ACL_CHANGE_EVT */ + bta_dm_add_device, /* 9 BTA_DM_API_ADD_DEVICE_EVT */ + + /* security API events */ + bta_dm_bond, /* 10 BTA_DM_API_BOND_EVT */ + bta_dm_bond_cancel, /* 11 BTA_DM_API_BOND_CANCEL_EVT */ + bta_dm_pin_reply, /* 12 BTA_DM_API_PIN_REPLY_EVT */ + bta_dm_link_policy, /* 13 BTA_DM_API_LINK_POLICY_EVT */ + bta_dm_auth_reply, /* 14 BTA_DM_API_AUTH_REPLY_EVT */ + + /* power manger events */ + bta_dm_pm_btm_status, /* 15 BTA_DM_PM_BTM_STATUS_EVT */ + bta_dm_pm_timer, /* 16 BTA_DM_PM_TIMER_EVT*/ + + /* simple pairing events */ + bta_dm_confirm, /* 17 BTA_DM_API_CONFIRM_EVT */ + + bta_dm_set_encryption, /* BTA_DM_API_SET_ENCRYPTION_EVT */ + +#if (BTM_LOCAL_IO_CAPS != BTM_IO_CAP_NONE) + bta_dm_passkey_cancel, /* 19 BTA_DM_API_PASKY_CANCEL_EVT */ +#endif +#if (BTM_OOB_INCLUDED == TRUE) + bta_dm_loc_oob, /* 20 BTA_DM_API_LOC_OOB_EVT */ + bta_dm_ci_io_req_act, /* 21 BTA_DM_CI_IO_REQ_EVT */ + bta_dm_ci_rmt_oob_act, /* 22 BTA_DM_CI_RMT_OOB_EVT */ +#endif /* BTM_OOB_INCLUDED */ + +#if BLE_INCLUDED == TRUE + bta_dm_add_blekey, /* BTA_DM_API_ADD_BLEKEY_EVT */ + bta_dm_add_ble_device, /* BTA_DM_API_ADD_BLEDEVICE_EVT */ + bta_dm_ble_passkey_reply, /* BTA_DM_API_BLE_PASSKEY_REPLY_EVT */ + bta_dm_security_grant, + bta_dm_ble_set_bg_conn_type, + bta_dm_ble_set_conn_params, /* BTA_DM_API_BLE_CONN_PARAM_EVT */ + bta_dm_ble_set_scan_params, /* BTA_DM_API_BLE_SCAN_PARAM_EVT */ +#endif + +#if ( BTM_EIR_SERVER_INCLUDED == TRUE )&&( BTA_EIR_CANNED_UUID_LIST != TRUE )&&(BTA_EIR_SERVER_NUM_CUSTOM_UUID > 0) + bta_dm_update_eir_uuid, /* BTA_DM_API_UPDATE_EIR_UUID_EVT */ +#endif +#if (BTM_EIR_SERVER_INCLUDED == TRUE) + bta_dm_set_eir_config, /* BTA_DM_API_SET_EIR_CONFIG_EVT */ +#endif + + bta_dm_enable_test_mode, /* BTA_DM_API_ENABLE_TEST_MODE_EVT */ + bta_dm_disable_test_mode, /* BTA_DM_API_DISABLE_TEST_MODE_EVT */ + bta_dm_execute_callback, /* BTA_DM_API_EXECUTE_CBACK_EVT */ + bta_dm_set_afh_channel_assesment /* BTA_DM_API_SET_AFH_CHANNEL_ASSESMENT_EVT */ +}; + + + +/* state machine action enumeration list */ +enum +{ + BTA_DM_API_SEARCH, /* 0 bta_dm_search_start */ + BTA_DM_API_SEARCH_CANCEL, /* 1 bta_dm_search_cancel */ + BTA_DM_API_DISCOVER, /* 2 bta_dm_discover */ + BTA_DM_INQUIRY_CMPL, /* 3 bta_dm_inq_cmpl */ + BTA_DM_REMT_NAME, /* 4 bta_dm_rmt_name */ + BTA_DM_SDP_RESULT, /* 5 bta_dm_sdp_result */ + BTA_DM_SEARCH_CMPL, /* 6 bta_dm_search_cmpl*/ + BTA_DM_FREE_SDP_DB, /* 7 bta_dm_free_sdp_db */ + BTA_DM_DISC_RESULT, /* 8 bta_dm_disc_result */ + BTA_DM_SEARCH_RESULT, /* 9 bta_dm_search_result */ + BTA_DM_QUEUE_SEARCH, /* 10 bta_dm_queue_search */ + BTA_DM_QUEUE_DISC, /* 11 bta_dm_queue_disc */ + BTA_DM_SEARCH_CLEAR_QUEUE, /* 12 bta_dm_search_clear_queue */ + BTA_DM_SEARCH_CANCEL_CMPL, /* 13 bta_dm_search_cancel_cmpl */ + BTA_DM_SEARCH_CANCEL_NOTIFY, /* 14 bta_dm_search_cancel_notify */ + BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL, /* 15 bta_dm_search_cancel_transac_cmpl */ + BTA_DM_DISC_RMT_NAME, /* 16 bta_dm_disc_rmt_name */ + BTA_DM_API_DI_DISCOVER, /* 17 bta_dm_di_disc */ + BTA_DM_SEARCH_NUM_ACTIONS /* 18 */ +}; + + +/* action function list */ +const tBTA_DM_ACTION bta_dm_search_action[] = +{ + + bta_dm_search_start, /* 0 BTA_DM_API_SEARCH */ + bta_dm_search_cancel, /* 1 BTA_DM_API_SEARCH_CANCEL */ + bta_dm_discover, /* 2 BTA_DM_API_DISCOVER */ + bta_dm_inq_cmpl, /* 3 BTA_DM_INQUIRY_CMPL */ + bta_dm_rmt_name, /* 4 BTA_DM_REMT_NAME */ + bta_dm_sdp_result, /* 5 BTA_DM_SDP_RESULT */ + bta_dm_search_cmpl, /* 6 BTA_DM_SEARCH_CMPL */ + bta_dm_free_sdp_db, /* 7 BTA_DM_FREE_SDP_DB */ + bta_dm_disc_result, /* 8 BTA_DM_DISC_RESULT */ + bta_dm_search_result, /* 9 BTA_DM_SEARCH_RESULT */ + bta_dm_queue_search, /* 10 BTA_DM_QUEUE_SEARCH */ + bta_dm_queue_disc, /* 11 BTA_DM_QUEUE_DISC */ + bta_dm_search_clear_queue, /* 12 BTA_DM_SEARCH_CLEAR_QUEUE */ + bta_dm_search_cancel_cmpl, /* 13 BTA_DM_SEARCH_CANCEL_CMPL */ + bta_dm_search_cancel_notify, /* 14 BTA_DM_SEARCH_CANCEL_NOTIFY */ + bta_dm_search_cancel_transac_cmpl, /* 15 BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL */ + bta_dm_disc_rmt_name, /* 16 BTA_DM_DISC_RMT_NAME */ + bta_dm_di_disc /* 17 BTA_DM_API_DI_DISCOVER */ +}; + +#define BTA_DM_SEARCH_IGNORE BTA_DM_SEARCH_NUM_ACTIONS +/* state table information */ +#define BTA_DM_SEARCH_ACTIONS 2 /* number of actions */ +#define BTA_DM_SEARCH_NEXT_STATE 2 /* position of next state */ +#define BTA_DM_SEARCH_NUM_COLS 3 /* number of columns in state tables */ + + + +/* state table for listen state */ +const UINT8 bta_dm_search_idle_st_table[][BTA_DM_SEARCH_NUM_COLS] = +{ + +/* Event Action 1 Action 2 Next State */ +/* API_SEARCH */ {BTA_DM_API_SEARCH, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE}, +/* API_SEARCH_CANCEL */ {BTA_DM_SEARCH_CANCEL_NOTIFY, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}, +/* API_SEARCH_DISC */ {BTA_DM_API_DISCOVER, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE}, +/* INQUIRY_CMPL */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}, +/* REMT_NAME_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}, +/* SDP_RESULT_EVT */ {BTA_DM_FREE_SDP_DB, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}, +/* SEARCH_CMPL_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}, +/* DISCV_RES_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}, +/* API_DI_DISCOVER_EVT */ {BTA_DM_API_DI_DISCOVER, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE} + +}; +const UINT8 bta_dm_search_search_active_st_table[][BTA_DM_SEARCH_NUM_COLS] = +{ + +/* Event Action 1 Action 2 Next State */ +/* API_SEARCH */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE}, +/* API_SEARCH_CANCEL */ {BTA_DM_API_SEARCH_CANCEL, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_CANCELLING}, +/* API_SEARCH_DISC */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE}, +/* INQUIRY_CMPL */ {BTA_DM_INQUIRY_CMPL, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE}, +/* REMT_NAME_EVT */ {BTA_DM_REMT_NAME, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE}, +/* SDP_RESULT_EVT */ {BTA_DM_SDP_RESULT, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE}, +/* SEARCH_CMPL_EVT */ {BTA_DM_SEARCH_CMPL, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}, +/* DISCV_RES_EVT */ {BTA_DM_SEARCH_RESULT, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE}, +/* API_DI_DISCOVER_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_ACTIVE} + + +}; + +const UINT8 bta_dm_search_search_cancelling_st_table[][BTA_DM_SEARCH_NUM_COLS] = +{ + +/* Event Action 1 Action 2 Next State */ +/* API_SEARCH */ {BTA_DM_QUEUE_SEARCH, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_CANCELLING}, +/* API_SEARCH_CANCEL */ {BTA_DM_SEARCH_CLEAR_QUEUE, BTA_DM_SEARCH_CANCEL_NOTIFY, BTA_DM_SEARCH_CANCELLING}, +/* API_SEARCH_DISC */ {BTA_DM_QUEUE_DISC, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_CANCELLING}, +/* INQUIRY_CMPL */ {BTA_DM_SEARCH_CANCEL_CMPL, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}, +/* REMT_NAME_EVT */ {BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL, BTA_DM_SEARCH_CANCEL_CMPL, BTA_DM_SEARCH_IDLE}, +/* SDP_RESULT_EVT */ {BTA_DM_SEARCH_CANCEL_TRANSAC_CMPL, BTA_DM_SEARCH_CANCEL_CMPL, BTA_DM_SEARCH_IDLE}, +/* SEARCH_CMPL_EVT */ {BTA_DM_SEARCH_CANCEL_CMPL, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}, +/* DISCV_RES_EVT */ {BTA_DM_SEARCH_CANCEL_CMPL, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}, +/* API_DI_DISCOVER_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_CANCELLING} + + +}; + +const UINT8 bta_dm_search_disc_active_st_table[][BTA_DM_SEARCH_NUM_COLS] = +{ + +/* Event Action 1 Action 2 Next State */ +/* API_SEARCH */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE}, +/* API_SEARCH_CANCEL */ {BTA_DM_SEARCH_CANCEL_NOTIFY, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_CANCELLING}, +/* API_SEARCH_DISC */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE}, +/* INQUIRY_CMPL */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE}, +/* REMT_NAME_EVT */ {BTA_DM_DISC_RMT_NAME, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE}, +/* SDP_RESULT_EVT */ {BTA_DM_SDP_RESULT, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE}, +/* SEARCH_CMPL_EVT */ {BTA_DM_SEARCH_CMPL, BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IDLE}, +/* DISCV_RES_EVT */ {BTA_DM_DISC_RESULT, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE}, +/* API_DI_DISCOVER_EVT */ {BTA_DM_SEARCH_IGNORE, BTA_DM_SEARCH_IGNORE, BTA_DM_DISCOVER_ACTIVE} + +}; + +typedef const UINT8 (*tBTA_DM_ST_TBL)[BTA_DM_SEARCH_NUM_COLS]; + +/* state table */ +const tBTA_DM_ST_TBL bta_dm_search_st_tbl[] = { + bta_dm_search_idle_st_table, + bta_dm_search_search_active_st_table, + bta_dm_search_search_cancelling_st_table, + bta_dm_search_disc_active_st_table +}; + + +/******************************************************************************* +** +** Function bta_dm_sm_disable +** +** Description unregister BTA DM +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_sm_disable( ) +{ + bta_sys_deregister( BTA_ID_DM ); +} + + +/******************************************************************************* +** +** Function bta_dm_sm_execute +** +** Description State machine event handling function for DM +** +** +** Returns void +** +*******************************************************************************/ +BOOLEAN bta_dm_sm_execute(BT_HDR *p_msg) +{ + UINT16 event = p_msg->event & 0x00ff; + + APPL_TRACE_EVENT1("bta_dm_sm_execute event:0x%x", event); + + /* execute action functions */ + if(event < BTA_DM_NUM_ACTIONS) + { + (*bta_dm_action[event])( (tBTA_DM_MSG*) p_msg); + } + + return TRUE; +} + +/******************************************************************************* +** +** Function bta_dm_sm_search_disable +** +** Description unregister BTA SEARCH DM +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_search_sm_disable( ) +{ + bta_sys_deregister( BTA_ID_DM_SEARCH ); + +} + + +/******************************************************************************* +** +** Function bta_dm_search_sm_execute +** +** Description State machine event handling function for DM +** +** +** Returns void +** +*******************************************************************************/ +BOOLEAN bta_dm_search_sm_execute(BT_HDR *p_msg) +{ + tBTA_DM_ST_TBL state_table; + UINT8 action; + int i; + + APPL_TRACE_EVENT2("bta_dm_search_sm_execute state:%d, event:0x%x", + bta_dm_search_cb.state, p_msg->event); + + /* look up the state table for the current state */ + state_table = bta_dm_search_st_tbl[bta_dm_search_cb.state]; + + bta_dm_search_cb.state = state_table[p_msg->event & 0x00ff][BTA_DM_SEARCH_NEXT_STATE]; + + + /* execute action functions */ + for (i = 0; i < BTA_DM_SEARCH_ACTIONS; i++) + { + if ((action = state_table[p_msg->event & 0x00ff][i]) != BTA_DM_SEARCH_IGNORE) + { + (*bta_dm_search_action[action])( (tBTA_DM_MSG*) p_msg); + } + else + { + break; + } + } + return TRUE; +} + diff --git a/bta/dm/bta_dm_pm.c b/bta/dm/bta_dm_pm.c new file mode 100644 index 0000000..eb0dfe4 --- /dev/null +++ b/bta/dm/bta_dm_pm.c @@ -0,0 +1,980 @@ +/***************************************************************************** +** +** Name: bta_dm_act.c +** +** Description: This file contains the action functions for device manager +** state machine. +** +** Copyright (c) 2003-2011, Broadcom Corp., All Rights Reserved. +** Broadcom Bluetooth Core. Proprietary and confidential. +** +*****************************************************************************/ + +#include "gki.h" +#include "bd.h" +#include "bta_sys.h" +#include "bta_api.h" +#include "bta_dm_int.h" +#include "btm_api.h" + +#include <string.h> + + +static void bta_dm_pm_cback(tBTA_SYS_CONN_STATUS status, UINT8 id, UINT8 app_id, BD_ADDR peer_addr); +static void bta_dm_pm_set_mode(BD_ADDR peer_addr, BOOLEAN timed_out ); +static void bta_dm_pm_timer_cback(void *p_tle); +static void bta_dm_pm_btm_cback(BD_ADDR bd_addr, tBTM_PM_STATUS status, UINT16 value, UINT8 hci_status); +static BOOLEAN bta_dm_pm_park(BD_ADDR peer_addr); +static BOOLEAN bta_dm_pm_sniff(tBTA_DM_PEER_DEVICE *p_peer_dev, UINT8 index); +static BOOLEAN bta_dm_pm_is_sco_active (); +static void bta_dm_pm_hid_check(BOOLEAN bScoActive); +static void bta_dm_pm_set_sniff_policy(tBTA_DM_PEER_DEVICE *p_dev, BOOLEAN bDisable); +#if (BTM_SSR_INCLUDED == TRUE) +static void bta_dm_pm_ssr(BD_ADDR peer_addr); +static void bta_dm_ssr_cfg_cback(UINT8 id, UINT8 app_id, UINT16 max_lat, UINT16 min_rmt_to); +#endif + +tBTA_DM_CONNECTED_SRVCS bta_dm_conn_srvcs; + + +/******************************************************************************* +** +** Function bta_dm_init_pm +** +** Description Initialises the BT low power manager +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_init_pm(void) +{ + + memset(&bta_dm_conn_srvcs, 0x00, sizeof(bta_dm_conn_srvcs)); + + /* if there are no power manger entries, so not register */ + if(p_bta_dm_pm_cfg[0].app_id != 0) + { + bta_sys_pm_register((tBTA_SYS_CONN_CBACK*)bta_dm_pm_cback); + +#if (BTM_SSR_INCLUDED == TRUE) + bta_sys_ssr_cfg_register((tBTA_SYS_SSR_CFG_CBACK*)bta_dm_ssr_cfg_cback); +#endif + BTM_PmRegister((BTM_PM_REG_SET | BTM_PM_REG_NOTIF), &bta_dm_cb.pm_id, + bta_dm_pm_btm_cback); + } + + +} + + +/******************************************************************************* +** +** Function bta_dm_disable_pm +** +** Description Disable PM +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_disable_pm(void) +{ + UINT8 i; + + BTM_PmRegister( BTM_PM_DEREG, &bta_dm_cb.pm_id, NULL); + + /* Need to stop all active timers. */ + for(i=0; i<BTA_DM_NUM_PM_TIMER; i++) + { + if(bta_dm_cb.pm_timer[i].in_use) + { + APPL_TRACE_WARNING1("stop dm_pm_timer:%d", i); + bta_sys_stop_timer(&bta_dm_cb.pm_timer[i].timer); + bta_dm_cb.pm_timer[i].in_use = FALSE; + } + } +} + +/******************************************************************************* +** +** Function bta_dm_pm_stop_timer +** +** Description stop a PM timer +** +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_pm_stop_timer(BD_ADDR peer_addr) +{ + UINT8 i; + + for(i=0; i<BTA_DM_NUM_PM_TIMER; i++) + { + + if(bta_dm_cb.pm_timer[i].in_use && !bdcmp(bta_dm_cb.pm_timer[i].peer_bdaddr, peer_addr)) + { + APPL_TRACE_WARNING1("stop dm_pm_timer:%d", i); + bta_sys_stop_timer(&bta_dm_cb.pm_timer[i].timer); + bta_dm_cb.pm_timer[i].in_use = FALSE; + break; + } + + } +} + +/******************************************************************************* +** +** Function bta_dm_pm_cback +** +** Description Conn change callback from sys for low power management +** +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_pm_cback(tBTA_SYS_CONN_STATUS status, UINT8 id, UINT8 app_id, BD_ADDR peer_addr) +{ + + UINT8 i,j; + UINT16 policy_setting; + tBTM_STATUS btm_status; + tBTM_VERSION_INFO vers; +#if (BTM_SSR_INCLUDED == TRUE) + int index = BTA_DM_PM_SSR0; +#endif + tBTA_DM_PEER_DEVICE *p_dev; + + APPL_TRACE_DEBUG3("bta_dm_pm_cback: st(%d), id(%d), app(%d)", status, id, app_id); + + btm_status = BTM_ReadLocalVersion (&vers); + p_dev = bta_dm_find_peer_device(peer_addr); + + /* Disable/Enable sniff policy on the SCO link if sco Up/Down. Will be removed in 2.2*/ + if ((btm_status == BTM_SUCCESS) && + (vers.manufacturer == LMP_COMPID_BROADCOM) && + (vers.hci_version < HCI_PROTO_VERSION_2_0) && + ((status == BTA_SYS_SCO_OPEN) || (status == BTA_SYS_SCO_CLOSE)) ) + { + bta_dm_pm_set_sniff_policy(p_dev, (status == BTA_SYS_SCO_OPEN)); + } + + /* find if there is an power mode entry for the service */ + for(i=1; i<=p_bta_dm_pm_cfg[0].app_id; i++) + { + + if((p_bta_dm_pm_cfg[i].id == id) + && ((p_bta_dm_pm_cfg[i].app_id == BTA_ALL_APP_ID ) || (p_bta_dm_pm_cfg[i].app_id == app_id ))) + break; + + } + + /* if no entries are there for the app_id and subystem in p_bta_dm_pm_spec*/ + if(i> p_bta_dm_pm_cfg[0].app_id) + return; + + bta_dm_pm_stop_timer(peer_addr); + /*p_dev = bta_dm_find_peer_device(peer_addr);*/ + +#if (BTM_SSR_INCLUDED == TRUE) + /* set SSR parameters on SYS CONN OPEN */ + if((BTA_SYS_CONN_OPEN == status) && p_dev && (p_dev->info & BTA_DM_DI_USE_SSR)) + { + index = p_bta_dm_pm_spec[p_bta_dm_pm_cfg[i].spec_idx].ssr; + } +#endif + + /* if no action for the event */ + if(p_bta_dm_pm_spec[p_bta_dm_pm_cfg[i].spec_idx].actn_tbl[status][0].power_mode == BTA_DM_PM_NO_ACTION) + { +#if (BTM_SSR_INCLUDED == TRUE) + if(BTA_DM_PM_SSR0 == index) /* and do not need to set SSR, return. */ +#endif + return; + } + + for(j=0; j<bta_dm_conn_srvcs.count ; j++) + { + /* check if an entry already present */ + if((bta_dm_conn_srvcs.conn_srvc[j].id == id) + && (bta_dm_conn_srvcs.conn_srvc[j].app_id == app_id ) + && !bdcmp(bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr, peer_addr)) + break; + + } + + /* if subsystem has no more preference on the power mode remove + the cb */ + if(p_bta_dm_pm_spec[p_bta_dm_pm_cfg[i].spec_idx].actn_tbl[status][0].power_mode == BTA_DM_PM_NO_PREF) + { + + if(j != bta_dm_conn_srvcs.count) + { + bta_dm_conn_srvcs.count--; + + for(; j<bta_dm_conn_srvcs.count ; j++) + { + + memcpy(&bta_dm_conn_srvcs.conn_srvc[j], &bta_dm_conn_srvcs.conn_srvc[j+1], sizeof(bta_dm_conn_srvcs.conn_srvc[j])); + + } + } + else + { + APPL_TRACE_WARNING0("bta_dm_act no entry for connected service cbs"); + return; + } + } + else if(j == bta_dm_conn_srvcs.count ) + { + /* check if we have more connected service that cbs */ + if(bta_dm_conn_srvcs.count == BTA_DM_NUM_CONN_SRVS) + { + APPL_TRACE_WARNING0("bta_dm_act no more connected service cbs"); + return; + } + + /* fill in a new cb */ + bta_dm_conn_srvcs.conn_srvc[j].id = id; + bta_dm_conn_srvcs.conn_srvc[j].app_id = app_id; + bdcpy(bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr, peer_addr); + + APPL_TRACE_WARNING2("new conn_srvc id:%d, app_id:%d", id, app_id); + + bta_dm_conn_srvcs.count++; + bta_dm_conn_srvcs.conn_srvc[j].state = status; + } + else + { + /* no service is added or removed. only updating status. */ + bta_dm_conn_srvcs.conn_srvc[j].state = status; + } + + if(p_dev) + { + p_dev->pm_mode_attempted = 0; + p_dev->pm_mode_failed = 0; + } + +#if (BTM_SSR_INCLUDED == TRUE) + if(p_bta_dm_ssr_spec[index].max_lat) + { + bta_dm_pm_ssr(peer_addr); + } +#endif + + bta_dm_pm_set_mode(peer_addr, FALSE); + + /* perform the HID link workaround if needed + ** 1. If SCO up/down event is received OR + ** 2. If HID connection open is received and SCO is already active. + ** This will handle the case where HID connects when SCO already active + */ + if ( (btm_status == BTM_SUCCESS) && + ( ((status == BTA_SYS_SCO_OPEN) || (status == BTA_SYS_SCO_CLOSE)) || + ((status == BTA_SYS_CONN_OPEN) && (id == BTA_ID_HH) && bta_dm_pm_is_sco_active()) ) ) + { + BOOLEAN bScoActive; + if (status == BTA_SYS_CONN_OPEN) + bScoActive = TRUE; + else + bScoActive = (status == BTA_SYS_SCO_OPEN); + + bta_dm_pm_hid_check(bScoActive); + } + +} + + +/******************************************************************************* +** +** Function bta_dm_pm_set_mode +** +** Description Set the power mode for the device +** +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_pm_set_mode(BD_ADDR peer_addr, BOOLEAN timed_out ) +{ + + tBTA_DM_PM_ACTTION pm_action = BTA_DM_PM_NO_ACTION; + UINT16 timeout = 0; + UINT8 i,j; + tBTA_DM_PM_ACTTION failed_pm = 0; + tBTA_DM_PEER_DEVICE *p_peer_device = NULL; + tBTA_DM_PM_ACTTION allowed_modes = 0; + tBTA_DM_PM_ACTTION pref_modes = 0; + tBTA_DM_PM_CFG *p_pm_cfg; + tBTA_DM_PM_SPEC *p_pm_spec; + tBTA_DM_PM_ACTN *p_act0, *p_act1; + tBTA_DM_SRVCS *p_srvcs; + + + if(!bta_dm_cb.device_list.count) + return; + + /* see if any attempt to put device in low power mode failed */ + p_peer_device = bta_dm_find_peer_device(peer_addr); + /* if no peer device found return */ + if (p_peer_device == NULL) + return; + + failed_pm = p_peer_device->pm_mode_failed; + + for(i=0; i<bta_dm_conn_srvcs.count ; i++) + { + + p_srvcs = &bta_dm_conn_srvcs.conn_srvc[i]; + if(!bdcmp(p_srvcs->peer_bdaddr, peer_addr)) + { + + /* p_bta_dm_pm_cfg[0].app_id is the number of entries */ + for(j=1; j<=p_bta_dm_pm_cfg[0].app_id; j++) + { + if((p_bta_dm_pm_cfg[j].id == p_srvcs->id) + && ((p_bta_dm_pm_cfg[j].app_id == BTA_ALL_APP_ID ) || + (p_bta_dm_pm_cfg[j].app_id == p_srvcs->app_id))) + break; + } + + p_pm_cfg = &p_bta_dm_pm_cfg[j]; + p_pm_spec = &p_bta_dm_pm_spec[p_pm_cfg->spec_idx]; + p_act0 = &p_pm_spec->actn_tbl[p_srvcs->state][0]; + p_act1 = &p_pm_spec->actn_tbl[p_srvcs->state][1]; + + APPL_TRACE_DEBUG3("bta_dm_pm_set_mode: srvcsid: %d, state: %d, j: %d", p_srvcs->id, p_srvcs->state, j); + allowed_modes |= p_pm_spec->allow_mask; + + /* PM actions are in the order of strictness */ + + /* first check if the first preference is ok */ + if(!(failed_pm & p_act0->power_mode)) + { + pref_modes |= p_act0->power_mode; + + if(p_act0->power_mode > pm_action) + { + pm_action = p_act0->power_mode; + timeout = p_act0->timeout; + + } + } + /* if first preference has already failed, try second preference */ + else if(!(failed_pm & p_act1->power_mode)) + { + pref_modes |= p_act1->power_mode; + + if(p_act1->power_mode > pm_action) + { + pm_action = p_act1->power_mode; + timeout = p_act1->timeout; + + } + } + } + } + + if(pm_action & (BTA_DM_PM_PARK | BTA_DM_PM_SNIFF)) + { + + /* some service don't like the mode */ + if(!(allowed_modes & pm_action)) + { + + /* select the other mode if its allowed and preferred, otherwise 0 which is BTA_DM_PM_NO_ACTION */ + pm_action = (allowed_modes & (BTA_DM_PM_PARK | BTA_DM_PM_SNIFF) & pref_modes); + + /* no timeout needed if no action is required */ + if(pm_action == BTA_DM_PM_NO_ACTION) + { + timeout = 0; + } + + } + + + } + + if(!timed_out && timeout) + { + + for(i=0; i<BTA_DM_NUM_PM_TIMER; i++) + { + + if(!bta_dm_cb.pm_timer[i].in_use) + { + bta_dm_cb.pm_timer[i].in_use = TRUE; + bdcpy(bta_dm_cb.pm_timer[i].peer_bdaddr, peer_addr); + bta_dm_cb.pm_timer[i].timer.p_cback = bta_dm_pm_timer_cback; + bta_sys_start_timer(&bta_dm_cb.pm_timer[i].timer, 0, timeout); + APPL_TRACE_WARNING2("start dm_pm_timer:%d, %d", i, timeout); + return; + + } + + } + + /* no more timers */ + if(i==BTA_DM_NUM_PM_TIMER) + { + APPL_TRACE_WARNING0("bta_dm_act dm_pm_timer no more"); + return; + } + } + + if(pm_action == BTA_DM_PM_NO_ACTION) + { + + + } + else if(pm_action == BTA_DM_PM_PARK) + { + p_peer_device->pm_mode_attempted = BTA_DM_PM_PARK; + bta_dm_pm_park(peer_addr); + + } + else if(pm_action & BTA_DM_PM_SNIFF) + { + /* dont initiate SNIFF, if link_policy has it disabled */ + if (p_peer_device->link_policy & HCI_ENABLE_SNIFF_MODE) + { + p_peer_device->pm_mode_attempted = BTA_DM_PM_SNIFF; + bta_dm_pm_sniff(p_peer_device, (UINT8)(pm_action & 0x0F) ); + } + else + { + APPL_TRACE_DEBUG0("bta_dm_pm_set_mode: Link policy disallows SNIFF, ignore request"); + } + } + else if(pm_action == BTA_DM_PM_ACTIVE) + { + + bta_dm_pm_active(peer_addr); + + } + + +} + + +/******************************************************************************* +** +** Function bta_ag_pm_park +** +** Description Switch to park mode. +** +** +** Returns TRUE if park attempted, FALSE otherwise. +** +*******************************************************************************/ +static BOOLEAN bta_dm_pm_park(BD_ADDR peer_addr) +{ + + tBTM_PM_MODE mode = BTM_PM_STS_ACTIVE; + + /* if not in park mode, switch to park */ + BTM_ReadPowerMode(peer_addr, &mode); + + if(mode != BTM_PM_MD_PARK) + { + BTM_SetPowerMode (bta_dm_cb.pm_id, peer_addr, &p_bta_dm_pm_md[BTA_DM_PM_PARK_IDX]); + } + return TRUE; + +} + + +/******************************************************************************* +** +** Function bta_ag_pm_sniff +** +** Description Switch to sniff mode. +** +** +** Returns TRUE if sniff attempted, FALSE otherwise. +** +*******************************************************************************/ +static BOOLEAN bta_dm_pm_sniff(tBTA_DM_PEER_DEVICE *p_peer_dev, UINT8 index) +{ + tBTM_PM_MODE mode = BTM_PM_STS_ACTIVE; + tBTM_PM_PWR_MD pwr_md; + tBTM_STATUS status; + + BTM_ReadPowerMode(p_peer_dev->peer_bdaddr, &mode); + +#if (BTM_SSR_INCLUDED == TRUE) + APPL_TRACE_DEBUG3("bta_dm_pm_sniff cur:%d, idx:%d, info:x%x", mode, index, p_peer_dev->info); + if (mode != BTM_PM_MD_SNIFF || + (HCI_SNIFF_SUB_RATE_SUPPORTED(BTM_ReadLocalFeatures ()) && + HCI_SNIFF_SUB_RATE_SUPPORTED(BTM_ReadRemoteFeatures (p_peer_dev->peer_bdaddr)) && + !(p_peer_dev->info & BTA_DM_DI_USE_SSR))) +#else + APPL_TRACE_DEBUG2("bta_dm_pm_sniff cur:%d, idx:%d", mode, index); + if(mode != BTM_PM_MD_SNIFF) +#endif + { + /* if the current mode is not sniff, issue the sniff command. + * If sniff, but SSR is not used in this link, still issue the command */ + memcpy(&pwr_md, &p_bta_dm_pm_md[index], sizeof (tBTM_PM_PWR_MD)); + if (p_peer_dev->info & BTA_DM_DI_INT_SNIFF) + { + pwr_md.mode |= BTM_PM_MD_FORCE; + } + status = BTM_SetPowerMode (bta_dm_cb.pm_id, p_peer_dev->peer_bdaddr, &pwr_md); + if (status == BTM_CMD_STORED|| status == BTM_CMD_STARTED) + { + p_peer_dev->info &= ~(BTA_DM_DI_INT_SNIFF|BTA_DM_DI_ACP_SNIFF); + p_peer_dev->info |= BTA_DM_DI_SET_SNIFF; + } + else if (status == BTM_SUCCESS) + { + APPL_TRACE_DEBUG0("bta_dm_pm_sniff BTM_SetPowerMode() returns BTM_SUCCESS"); + p_peer_dev->info &= ~(BTA_DM_DI_INT_SNIFF|BTA_DM_DI_ACP_SNIFF|BTA_DM_DI_SET_SNIFF); + } + else /* error */ + { + APPL_TRACE_ERROR1("bta_dm_pm_sniff BTM_SetPowerMode() returns ERROR status=%d", status); + p_peer_dev->info &= ~(BTA_DM_DI_INT_SNIFF|BTA_DM_DI_ACP_SNIFF|BTA_DM_DI_SET_SNIFF); + } + } + /* else already in sniff and is using SSR, do nothing */ + + return TRUE; + +} + +/******************************************************************************* +** +** Function bta_dm_pm_ssr +** +** Description checks and sends SSR parameters +** +** Returns void +** +*******************************************************************************/ +#if (BTM_SSR_INCLUDED == TRUE) +static void bta_dm_pm_ssr(BD_ADDR peer_addr) +{ + tBTA_DM_SSR_SPEC *p_spec, *p_spec_cur; + UINT8 i,j; + int ssr = BTA_DM_PM_SSR0; + + /* go through the connected services */ + for(i=0; i<bta_dm_conn_srvcs.count ; i++) + { + if(!bdcmp(bta_dm_conn_srvcs.conn_srvc[i].peer_bdaddr, peer_addr)) + { + /* p_bta_dm_pm_cfg[0].app_id is the number of entries */ + for(j=1; j<=p_bta_dm_pm_cfg[0].app_id; j++) + { + /* find the associated p_bta_dm_pm_cfg */ + if((p_bta_dm_pm_cfg[j].id == bta_dm_conn_srvcs.conn_srvc[i].id) + && ((p_bta_dm_pm_cfg[j].app_id == BTA_ALL_APP_ID ) + || (p_bta_dm_pm_cfg[j].app_id == bta_dm_conn_srvcs.conn_srvc[i].app_id))) + { + APPL_TRACE_WARNING2("bta_dm_pm_ssr conn_srvc id:%d, app_id:%d", + bta_dm_conn_srvcs.conn_srvc[i].id, bta_dm_conn_srvcs.conn_srvc[i].app_id); + break; + } + } + + /* find the ssr index with the smallest max latency. */ + p_spec_cur = &p_bta_dm_ssr_spec[p_bta_dm_pm_spec[p_bta_dm_pm_cfg[j].spec_idx].ssr]; + p_spec = &p_bta_dm_ssr_spec[ssr]; + + if (p_spec_cur->max_lat < p_spec->max_lat || + (ssr == BTA_DM_PM_SSR0 && p_bta_dm_pm_spec[p_bta_dm_pm_cfg[j].spec_idx].ssr != BTA_DM_PM_SSR0)) + { + ssr = p_bta_dm_pm_spec[p_bta_dm_pm_cfg[j].spec_idx].ssr; + } + + } + } + + p_spec = &p_bta_dm_ssr_spec[ssr]; + APPL_TRACE_WARNING2("bta_dm_pm_ssr:%d, lat:%d", ssr, p_spec->max_lat); + if(p_spec->max_lat) + { + /* set the SSR parameters. */ + BTM_SetSsrParams (peer_addr, p_spec->max_lat, + p_spec->min_rmt_to, p_spec->min_loc_to); + } +} +/******************************************************************************* +** +** Function bta_dm_ssr_cfg_cback +** +** Description Conn change callback from sys for low power management +** +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_ssr_cfg_cback(UINT8 id, UINT8 app_id, + UINT16 max_lat, UINT16 min_rmt_to) +{ + tBTA_DM_SSR_SPEC *p_spec; + UINT8 i, index; + /* find if there is an power mode entry for the service */ + for(i=1; i<=p_bta_dm_pm_cfg[0].app_id; i++) + { + + if((p_bta_dm_pm_cfg[i].id == id) + && ((p_bta_dm_pm_cfg[i].app_id == BTA_ALL_APP_ID ) || (p_bta_dm_pm_cfg[i].app_id == app_id ))) + break; + + } + /* if no entries are there for the app_id and subystem in p_bta_dm_pm_spec*/ + if(i> p_bta_dm_pm_cfg[0].app_id) + return; + + index = p_bta_dm_pm_spec[p_bta_dm_pm_cfg[i].spec_idx].ssr; + + APPL_TRACE_DEBUG2("SSR parameter changed to: max_latency: %d min_tout: %d", max_lat, min_rmt_to); + + p_spec = &p_bta_dm_ssr_spec[index]; + p_spec->max_lat = max_lat; + p_spec->min_rmt_to = min_rmt_to; +} + + +#endif +/******************************************************************************* +** +** Function bta_dm_pm_active +** +** Description Brings connection to active mode +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_pm_active(BD_ADDR peer_addr) +{ + tBTM_PM_PWR_MD pm; + + memset( (void*)&pm, 0, sizeof(pm)); + + /* switch to active mode */ + pm.mode = BTM_PM_MD_ACTIVE; + BTM_SetPowerMode (bta_dm_cb.pm_id, peer_addr, &pm); + + +} + + +/******************************************************************************* +** +** Function bta_dm_pm_btm_cback +** +** Description BTM power manager callback. +** +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_pm_btm_cback(BD_ADDR bd_addr, tBTM_PM_STATUS status, UINT16 value, UINT8 hci_status) +{ + tBTA_DM_PM_BTM_STATUS *p_buf; + + if ((p_buf = (tBTA_DM_PM_BTM_STATUS *) GKI_getbuf(sizeof(tBTA_DM_PM_BTM_STATUS))) != NULL) + { + p_buf->hdr.event = BTA_DM_PM_BTM_STATUS_EVT; + p_buf->status = status; + p_buf->value = value; + p_buf->hci_status = hci_status; + bdcpy(p_buf->bd_addr, bd_addr); + bta_sys_sendmsg(p_buf); + } +} + +/******************************************************************************* +** +** Function bta_dm_pm_timer_cback +** +** Description Power management timer callback. +** +** +** Returns void +** +*******************************************************************************/ +static void bta_dm_pm_timer_cback(void *p_tle) +{ + tBTA_DM_PM_TIMER *p_buf; + UINT8 i; + + APPL_TRACE_WARNING0("dm_pm_timer expires"); + + for(i=0; i<BTA_DM_NUM_PM_TIMER; i++) + { + + if(bta_dm_cb.pm_timer[i].in_use) + { + + if(&bta_dm_cb.pm_timer[i].timer == (TIMER_LIST_ENT*) p_tle) + { + APPL_TRACE_WARNING1("dm_pm_timer expires %d", i); + bta_dm_cb.pm_timer[i].in_use = FALSE; + break; + } + + } + + } + + + /* no more timers */ + if(i==BTA_DM_NUM_PM_TIMER) + { + return; + } + + if ((p_buf = (tBTA_DM_PM_TIMER *) GKI_getbuf(sizeof(tBTA_DM_PM_TIMER))) != NULL) + { + p_buf->hdr.event = BTA_DM_PM_TIMER_EVT; + bdcpy(p_buf->bd_addr, bta_dm_cb.pm_timer[i].peer_bdaddr); + bta_sys_sendmsg(p_buf); + } +} + +/******************************************************************************* +** +** Function bta_dm_pm_btm_status +** +** Description Process pm status event from btm +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_pm_btm_status(tBTA_DM_MSG *p_data) +{ + + tBTA_DM_PEER_DEVICE *p_dev; + tBTA_DM_DEV_INFO info; + + APPL_TRACE_DEBUG1("bta_dm_pm_btm_status:%d", p_data->pm_status.status); + p_dev = bta_dm_find_peer_device(p_data->pm_status.bd_addr); + if(NULL == p_dev) + return; + + info = p_dev->info; + /* check new mode */ + switch (p_data->pm_status.status) + { + case BTM_PM_STS_ACTIVE: + /* if our sniff or park attempt failed + we should not try it again*/ + if (p_data->pm_status.hci_status != 0) + { + APPL_TRACE_ERROR1("bta_dm_pm_btm_status hci_status=%d", p_data->pm_status.hci_status); + p_dev->info &= ~(BTA_DM_DI_INT_SNIFF|BTA_DM_DI_ACP_SNIFF|BTA_DM_DI_SET_SNIFF); + + if(p_dev->pm_mode_attempted &(BTA_DM_PM_PARK | BTA_DM_PM_SNIFF)) + { + p_dev->pm_mode_failed + |= ((BTA_DM_PM_PARK | BTA_DM_PM_SNIFF) & p_dev->pm_mode_attempted); + bta_dm_pm_stop_timer(p_data->pm_status.bd_addr); + bta_dm_pm_set_mode(p_data->pm_status.bd_addr, FALSE); + } + } + else + { +#if (BTM_SSR_INCLUDED == TRUE) + if(p_dev->prev_low) + { + /* need to send the SSR paramaters to controller again */ + bta_dm_pm_ssr(p_dev->peer_bdaddr); + } + p_dev->prev_low = BTM_PM_STS_ACTIVE; +#endif + bta_dm_pm_stop_timer(p_data->pm_status.bd_addr); + bta_dm_pm_set_mode(p_data->pm_status.bd_addr, FALSE); + } + break; + +#if (BTM_SSR_INCLUDED == TRUE) + case BTM_PM_STS_PARK: + case BTM_PM_STS_HOLD: + /* save the previous low power mode - for SSR. + * SSR parameters are sent to controller on "conn open". + * the numbers stay good until park/hold/detach */ + if(p_dev->info & BTA_DM_DI_USE_SSR) + p_dev->prev_low = p_data->pm_status.status; + break; + + case BTM_PM_STS_SSR: + if(p_data->pm_status.value) + p_dev->info |= BTA_DM_DI_USE_SSR; + else + p_dev->info &= ~BTA_DM_DI_USE_SSR; + break; +#endif + case BTM_PM_STS_SNIFF: + p_dev->info &= ~(BTA_DM_DI_SET_SNIFF|BTA_DM_DI_INT_SNIFF|BTA_DM_DI_ACP_SNIFF); + if (info & BTA_DM_DI_SET_SNIFF) + p_dev->info |= BTA_DM_DI_INT_SNIFF; + else + p_dev->info |= BTA_DM_DI_ACP_SNIFF; + break; + + case BTM_PM_STS_ERROR: + p_dev->info &= ~BTA_DM_DI_SET_SNIFF; + break; + + default: + break; + } + + + +} + +/******************************************************************************* +** +** Function bta_dm_pm_timer +** +** Description Process pm timer event from btm +** +** +** Returns void +** +*******************************************************************************/ +void bta_dm_pm_timer(tBTA_DM_MSG *p_data) +{ + + APPL_TRACE_WARNING0("proc dm_pm_timer expires"); + bta_dm_pm_set_mode(p_data->pm_status.bd_addr, TRUE); + + +} + +/******************************************************************************* +** +** Function bta_dm_find_peer_device +** +** Description Given an address, find the associated control block. +** +** Returns tBTA_DM_PEER_DEVICE +** +*******************************************************************************/ +tBTA_DM_PEER_DEVICE * bta_dm_find_peer_device(BD_ADDR peer_addr) +{ + tBTA_DM_PEER_DEVICE *p_dev = NULL; + int i; + + for(i=0; i<bta_dm_cb.device_list.count; i++) + { + if(!bdcmp( bta_dm_cb.device_list.peer_device[i].peer_bdaddr, peer_addr)) + { + p_dev = &bta_dm_cb.device_list.peer_device[i]; + break; + } + + } + return p_dev; +} + +/******************************************************************************* +** +** Function bta_dm_is_sco_active +** +** Description Loop through connected services for HFP+State=SCO +** +** Returns BOOLEAN. TRUE if SCO active, else FALSE +** +*******************************************************************************/ +static BOOLEAN bta_dm_pm_is_sco_active () +{ + int j; + BOOLEAN bScoActive = FALSE; + + for(j=0; j<bta_dm_conn_srvcs.count ; j++) + { + /* check if an entry already present */ + if ( (bta_dm_conn_srvcs.conn_srvc[j].id == BTA_ID_AG ) && (bta_dm_conn_srvcs.conn_srvc[j].state == BTA_SYS_SCO_OPEN) ) + { + bScoActive = TRUE; + break; + } + } + + APPL_TRACE_DEBUG1("bta_dm_is_sco_active: SCO active: %d", bScoActive); + return bScoActive; +} + + +/******************************************************************************* +** +** Function bta_dm_pm_hid_check +** +** Description Disables/Enables sniff in link policy based on SCO Up/Down +** +** Returns None +** +*******************************************************************************/ + +static void bta_dm_pm_hid_check(BOOLEAN bScoActive) +{ + int j; + + /* if HID is active, disable the link policy */ + for(j=0; j<bta_dm_conn_srvcs.count ; j++) + { + /* check if an entry already present */ + if(bta_dm_conn_srvcs.conn_srvc[j].id == BTA_ID_HH ) + { + APPL_TRACE_DEBUG2 ("SCO status change(Active: %d), modify HID link policy. state: %d", + bScoActive, bta_dm_conn_srvcs.conn_srvc[j].state); + bta_dm_pm_set_sniff_policy( bta_dm_find_peer_device(bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr), bScoActive); + + /* if we had disabled link policy, seems like the hid device stop retrying SNIFF after a few tries. force sniff if needed */ + if (!bScoActive) + bta_dm_pm_set_mode(bta_dm_conn_srvcs.conn_srvc[j].peer_bdaddr, FALSE); + } + } + +} + +/******************************************************************************* +** +** Function bta_dm_pm_set_sniff_policy +** +** Description Disables/Enables sniff in link policy for the give device +** +** Returns None +** +*******************************************************************************/ +static void bta_dm_pm_set_sniff_policy(tBTA_DM_PEER_DEVICE *p_dev, BOOLEAN bDisable) +{ + UINT16 policy_setting; + + if (!p_dev) + return; + + if (bDisable) + { + policy_setting = bta_dm_cb.cur_policy & + (HCI_ENABLE_MASTER_SLAVE_SWITCH | + HCI_ENABLE_HOLD_MODE | + HCI_ENABLE_PARK_MODE); + + } + else + { + /* allow sniff after sco is closed */ + policy_setting= bta_dm_cb.cur_policy; + } + + /* if disabling SNIFF, make sure link is Active */ + if (bDisable) + bta_dm_pm_active(p_dev->peer_bdaddr); + + /* update device record and set link policy */ + p_dev->link_policy = policy_setting; + BTM_SetLinkPolicy(p_dev->peer_bdaddr, &policy_setting); + +} diff --git a/bta/dm/bta_dm_sco.c b/bta/dm/bta_dm_sco.c new file mode 100644 index 0000000..7ad840a --- /dev/null +++ b/bta/dm/bta_dm_sco.c @@ -0,0 +1,684 @@ +/***************************************************************************** +** +** Name: bta_dm_sco.c +** +** Description: This file contains the down sampling utility to convert PCM +** samples in 16k/32k/48k/44.1k/22050/11025 sampling rate into +** 8K/16bits samples required for SCO channel format. One API +** function isprovided and only possible to be used when +** transmitting SCO data is sent via HCI interface. +** +** Original code took from BTW/CE. +** +** Copyright (c) 2003-2007, Broadcom Corp., All Rights Reserved. +** Widcomm Bluetooth Core. Proprietary and confidential. +** +*****************************************************************************/ +#include <string.h> +#include "bta_api.h" +#include "bta_sys.h" + +#if (BTM_SCO_HCI_INCLUDED == TRUE) + +#ifndef BTA_DM_SCO_DEBUG +#define BTA_DM_SCO_DEBUG FALSE +#endif +/***************************************************************************** +** Constants +*****************************************************************************/ + +#define BTA_DM_PCM_OVERLAP_SIZE 48 + +#define BTA_DM_PCM_SMPL_RATE_44100 44100 +#define BTA_DM_PCM_SMPL_RATE_22050 22050 +#define BTA_DM_PCM_SMPL_RATE_11025 11025 + +/***************************************************************************** +** Data types for PCM Resampling utility +*****************************************************************************/ + +typedef INT32 (*PCONVERT_TO_BT_FILTERED) (UINT8 *pSrc, void *pDst, UINT32 dwSrcSamples, + UINT32 dwSrcSps,INT32 *pLastCurPos, UINT8 *pOverlapArea); +typedef INT32 (*PCONVERT_TO_BT_NOFILTER) (void *pSrc, void *pDst, UINT32 dwSrcSamples, + UINT32 dwSrcSps); +typedef struct +{ + UINT8 overlap_area[BTA_DM_PCM_OVERLAP_SIZE * 4]; + UINT32 cur_pos; /* current position */ + UINT32 src_sps; /* samples per second (source audio data) */ + PCONVERT_TO_BT_FILTERED filter; /* the action function to do the + conversion 44100, 22050, 11025*/ + PCONVERT_TO_BT_NOFILTER nofilter; /* the action function to do + the conversion 48000, 32000, 16000*/ + UINT32 bits; /* number of bits per pcm sample */ + UINT32 n_channels; /* number of channels (i.e. mono(1), stereo(2)...) */ + UINT32 sample_size; + UINT32 can_be_filtered; + UINT32 divisor; +} tBTA_DM_PCM_RESAMPLE_CB; + +tBTA_DM_PCM_RESAMPLE_CB bta_dm_pcm_cb; + +/***************************************************************************** +** Macro Definition +*****************************************************************************/ + + +#define CHECK_SATURATION16(x) \ + if (x > 32767) \ + x = 32767; \ + else if (x < -32768) \ + x = -32768; + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +#define CONVERT_44100_TO_BLUETOOTH(pStart, pEnd) \ + { \ + INT32 out1, out2, out3, out4, out5; \ + SRC_TYPE *pS = (SRC_TYPE *)pStart; \ + SRC_TYPE *pSEnd = (SRC_TYPE *)pEnd; \ + \ + while (pS < pSEnd) \ + { \ + CurrentPos -= 8000; \ + \ + if (CurrentPos >= 0) \ + { \ + pS += SRC_CHANNELS; \ + continue; \ + } \ + CurrentPos += dwSrcSps; \ + \ + out1 = (SRC_SAMPLE(0) * 1587) \ + + ((SRC_SAMPLE(1) + SRC_SAMPLE(-1)) * 1522) \ + + ((SRC_SAMPLE(2) + SRC_SAMPLE(-2)) * 1337) \ + + ((SRC_SAMPLE(3) + SRC_SAMPLE(-3)) * 1058); \ + \ + out1 = out1 / 30000; \ + \ + out2 = ((SRC_SAMPLE(4) + SRC_SAMPLE(-4)) * 725) \ + + ((SRC_SAMPLE(5) + SRC_SAMPLE(-5)) * 384) \ + + ((SRC_SAMPLE(6) + SRC_SAMPLE(-6)) * 79); \ + \ + out2 = out2 / 30000; \ + \ + out3 = ((SRC_SAMPLE(7) + SRC_SAMPLE(-7)) * 156) \ + + ((SRC_SAMPLE(8) + SRC_SAMPLE(-8)) * 298) \ + + ((SRC_SAMPLE(9) + SRC_SAMPLE(-9)) * 345); \ + \ + out3 = out3 / 30000; \ + \ + out4 = ((SRC_SAMPLE(10) + SRC_SAMPLE(-10)) * 306) \ + + ((SRC_SAMPLE(11) + SRC_SAMPLE(-11)) * 207) \ + + ((SRC_SAMPLE(12) + SRC_SAMPLE(-12)) * 78); \ + \ + out4 = out4 / 30000; \ + \ + out5 = out1 + out2 - out3 - out4; \ + \ + CHECK_SATURATION16(out5); \ + *psBtOut++ = (INT16)out5; \ + \ + pS += SRC_CHANNELS; \ + } \ + } + + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +#define CONVERT_22050_TO_BLUETOOTH(pStart, pEnd) \ + { \ + INT32 out1, out2, out3, out4, out5; \ + SRC_TYPE *pS = (SRC_TYPE *)pStart; \ + SRC_TYPE *pSEnd = (SRC_TYPE *)pEnd; \ + \ + while (pS < pSEnd) \ + { \ + CurrentPos -= 8000; \ + \ + if (CurrentPos >= 0) \ + { \ + pS += SRC_CHANNELS; \ + continue; \ + } \ + CurrentPos += dwSrcSps; \ + \ + out1 = (SRC_SAMPLE(0) * 2993) \ + + ((SRC_SAMPLE(1) + SRC_SAMPLE(-1)) * 2568) \ + + ((SRC_SAMPLE(2) + SRC_SAMPLE(-2)) * 1509) \ + + ((SRC_SAMPLE(3) + SRC_SAMPLE(-3)) * 331); \ + \ + out1 = out1 / 30000; \ + \ + out2 = ((SRC_SAMPLE(4) + SRC_SAMPLE(-4)) * 454) \ + + ((SRC_SAMPLE(5) + SRC_SAMPLE(-5)) * 620) \ + + ((SRC_SAMPLE(6) + SRC_SAMPLE(-6)) * 305); \ + \ + out2 = out2 / 30000; \ + \ + out3 = ((SRC_SAMPLE(7) + SRC_SAMPLE(-7)) * 127) \ + + ((SRC_SAMPLE(8) + SRC_SAMPLE(-8)) * 350) \ + + ((SRC_SAMPLE(9) + SRC_SAMPLE(-9)) * 265) \ + + ((SRC_SAMPLE(10) + SRC_SAMPLE(-10)) * 6); \ + \ + out3 = out3 / 30000; \ + \ + out4 = ((SRC_SAMPLE(11) + SRC_SAMPLE(-11)) * 201); \ + \ + out4 = out4 / 30000; \ + \ + out5 = out1 - out2 + out3 - out4; \ + \ + CHECK_SATURATION16(out5); \ + *psBtOut++ = (INT16)out5; \ + \ + pS += SRC_CHANNELS; \ + } \ + } + + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +#define CONVERT_11025_TO_BLUETOOTH(pStart, pEnd) \ + { \ + INT32 out1; \ + SRC_TYPE *pS = (SRC_TYPE *)pStart; \ + SRC_TYPE *pSEnd = (SRC_TYPE *)pEnd; \ + \ + while (pS < pSEnd) \ + { \ + CurrentPos -= 8000; \ + \ + if (CurrentPos >= 0) \ + { \ + pS += SRC_CHANNELS; \ + continue; \ + } \ + CurrentPos += dwSrcSps; \ + \ + out1 = (SRC_SAMPLE(0) * 6349) \ + + ((SRC_SAMPLE(1) + SRC_SAMPLE(-1)) * 2874) \ + - ((SRC_SAMPLE(2) + SRC_SAMPLE(-2)) * 1148) \ + - ((SRC_SAMPLE(3) + SRC_SAMPLE(-3)) * 287) \ + + ((SRC_SAMPLE(4) + SRC_SAMPLE(-4)) * 675) \ + - ((SRC_SAMPLE(5) + SRC_SAMPLE(-5)) * 258) \ + - ((SRC_SAMPLE(6) + SRC_SAMPLE(-6)) * 206) \ + + ((SRC_SAMPLE(7) + SRC_SAMPLE(-7)) * 266); \ + \ + out1 = out1 / 30000; \ + \ + CHECK_SATURATION16(out1); \ + *psBtOut++ = (INT16)out1; \ + \ + pS += SRC_CHANNELS; \ + } \ + } + + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +#undef SRC_CHANNELS +#undef SRC_SAMPLE +#undef SRC_TYPE + +#define SRC_TYPE UINT8 +#define SRC_CHANNELS 1 +#define SRC_SAMPLE(x) ((pS[x] - 0x80) << 8) + +/***************************************************************************** +** Local Function +*****************************************************************************/ +INT32 Convert_8M_ToBT_Filtered (UINT8 *pSrc, void *pDst, UINT32 dwSrcSamples, + UINT32 dwSrcSps, INT32 *pLastCurPos, UINT8 *pOverlapArea) +{ + INT32 CurrentPos = *pLastCurPos; + SRC_TYPE *pIn, *pInEnd; + SRC_TYPE *pOv, *pOvEnd; + INT16 *psBtOut = (INT16 *)pDst; +#if BTA_DM_SCO_DEBUG + APPL_TRACE_DEBUG1("Convert_8M_ToBT_Filtered, CurrentPos %d\n", CurrentPos); +#endif + memcpy (pOverlapArea + (BTA_DM_PCM_OVERLAP_SIZE * 2), pSrc, BTA_DM_PCM_OVERLAP_SIZE * 2); + + pOv = (SRC_TYPE *)(pOverlapArea + BTA_DM_PCM_OVERLAP_SIZE); + pOvEnd = (SRC_TYPE *)(pOverlapArea + (BTA_DM_PCM_OVERLAP_SIZE * 3)); + + pIn = (SRC_TYPE *)(pSrc + BTA_DM_PCM_OVERLAP_SIZE); + pInEnd = (SRC_TYPE *)(pSrc + (dwSrcSamples * SRC_CHANNELS * sizeof (SRC_TYPE)) - \ + BTA_DM_PCM_OVERLAP_SIZE); + + if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_44100) + { + CONVERT_44100_TO_BLUETOOTH(pOv, pOvEnd); + CONVERT_44100_TO_BLUETOOTH(pIn, pInEnd); + } + else if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_22050) + { + CONVERT_22050_TO_BLUETOOTH(pOv, pOvEnd); + CONVERT_22050_TO_BLUETOOTH(pIn, pInEnd); + } + else if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_11025) + { + CONVERT_11025_TO_BLUETOOTH(pOv, pOvEnd); + CONVERT_11025_TO_BLUETOOTH(pIn, pInEnd); + } + + memcpy (pOverlapArea, pSrc + (dwSrcSamples * SRC_CHANNELS * sizeof (SRC_TYPE)) - \ + (BTA_DM_PCM_OVERLAP_SIZE * 2), BTA_DM_PCM_OVERLAP_SIZE * 2); + + *pLastCurPos = CurrentPos; + + return (psBtOut - (INT16 *)pDst); +} + +INT32 Convert_8M_ToBT_NoFilter (void *pSrc, void *pDst, UINT32 dwSrcSamples, UINT32 dwSrcSps) +{ + INT32 CurrentPos; + UINT8 *pbSrc = (UINT8 *)pSrc; + INT16 *psDst = (INT16 *)pDst; + INT16 sWorker; + + // start at dwSpsSrc / 2, decrement by 8000 + // + CurrentPos = (dwSrcSps >> 1); + + while (dwSrcSamples--) + { + CurrentPos -= 8000; + + if (CurrentPos >= 0) + pbSrc++; + else + { + sWorker = *pbSrc++; + sWorker -= 0x80; + sWorker <<= 8; + + *psDst++ = sWorker; + + CurrentPos += dwSrcSps; + } + } + + return (psDst - (INT16 *)pDst); +} + + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +#undef SRC_CHANNELS +#undef SRC_SAMPLE +#undef SRC_TYPE + +#define SRC_TYPE INT16 +#define SRC_CHANNELS 1 +#define SRC_SAMPLE(x) pS[x] + +INT32 Convert_16M_ToBT_Filtered (UINT8 *pSrc, void *pDst, UINT32 dwSrcSamples, + UINT32 dwSrcSps, INT32 *pLastCurPos, UINT8 *pOverlapArea) +{ + INT32 CurrentPos = *pLastCurPos; + SRC_TYPE *pIn, *pInEnd; + SRC_TYPE *pOv, *pOvEnd; + INT16 *psBtOut = (INT16 *)pDst; + + memcpy (pOverlapArea + (BTA_DM_PCM_OVERLAP_SIZE * 2), pSrc, BTA_DM_PCM_OVERLAP_SIZE * 2); + + pOv = (SRC_TYPE *)(pOverlapArea + BTA_DM_PCM_OVERLAP_SIZE); + pOvEnd = (SRC_TYPE *)(pOverlapArea + (BTA_DM_PCM_OVERLAP_SIZE * 3)); + + pIn = (SRC_TYPE *)(pSrc + BTA_DM_PCM_OVERLAP_SIZE); + pInEnd = (SRC_TYPE *)(pSrc + (dwSrcSamples * SRC_CHANNELS * sizeof (SRC_TYPE)) - BTA_DM_PCM_OVERLAP_SIZE); + + if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_44100) + { + CONVERT_44100_TO_BLUETOOTH(pOv, pOvEnd); + CONVERT_44100_TO_BLUETOOTH(pIn, pInEnd); + } + else if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_22050) + { + CONVERT_22050_TO_BLUETOOTH(pOv, pOvEnd); + CONVERT_22050_TO_BLUETOOTH(pIn, pInEnd); + } + else if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_11025) + { + CONVERT_11025_TO_BLUETOOTH(pOv, pOvEnd); + CONVERT_11025_TO_BLUETOOTH(pIn, pInEnd); + } + + memcpy (pOverlapArea, pSrc + (dwSrcSamples * SRC_CHANNELS * sizeof (SRC_TYPE)) - \ + (BTA_DM_PCM_OVERLAP_SIZE * 2), BTA_DM_PCM_OVERLAP_SIZE * 2); + + *pLastCurPos = CurrentPos; + + return (psBtOut - (INT16 *)pDst); +} + +INT32 Convert_16M_ToBT_NoFilter (void *pSrc, void *pDst, UINT32 dwSrcSamples, UINT32 dwSrcSps) +{ + INT32 CurrentPos; + INT16 *psSrc = (INT16 *)pSrc; + INT16 *psDst = (INT16 *)pDst; + + // start at dwSpsSrc / 2, decrement by 8000 + // + CurrentPos = (dwSrcSps >> 1); + + while (dwSrcSamples--) + { + CurrentPos -= 8000; + + if (CurrentPos >= 0) + psSrc++; + else + { + *psDst++ = *psSrc++; + + CurrentPos += dwSrcSps; + } + } + + return (psDst - (INT16 *)pDst); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +#undef SRC_CHANNELS +#undef SRC_SAMPLE +#undef SRC_TYPE + +#define SRC_TYPE UINT8 +#define SRC_CHANNELS 2 +#define SRC_SAMPLE(x) ((((pS[x * 2] - 0x80) << 8) + ((pS[(x * 2) + 1] - 0x80) << 8)) >> 1) + +INT32 Convert_8S_ToBT_Filtered (UINT8 *pSrc, void *pDst, UINT32 dwSrcSamples, + UINT32 dwSrcSps, INT32 *pLastCurPos, UINT8 *pOverlapArea) +{ + INT32 CurrentPos = *pLastCurPos; + SRC_TYPE *pIn, *pInEnd; + SRC_TYPE *pOv, *pOvEnd; + INT16 *psBtOut = (INT16 *)pDst; + +#if BTA_DM_SCO_DEBUG + APPL_TRACE_DEBUG5("Convert_8S_ToBT_Filtered CurrentPos %d, SRC_TYPE %d, SRC_CHANNELS %d, \ + dwSrcSamples %d, dwSrcSps %d", CurrentPos, sizeof (SRC_TYPE), SRC_CHANNELS, \ + dwSrcSamples, dwSrcSps); +#endif + memcpy (pOverlapArea + (BTA_DM_PCM_OVERLAP_SIZE * 2), pSrc, BTA_DM_PCM_OVERLAP_SIZE * 2); + + pOv = (SRC_TYPE *)(pOverlapArea + BTA_DM_PCM_OVERLAP_SIZE); + pOvEnd = (SRC_TYPE *)(pOverlapArea + (BTA_DM_PCM_OVERLAP_SIZE * 3)); + + pIn = (SRC_TYPE *)(pSrc + BTA_DM_PCM_OVERLAP_SIZE); + pInEnd = (SRC_TYPE *)(pSrc + (dwSrcSamples * SRC_CHANNELS * sizeof (SRC_TYPE)) - BTA_DM_PCM_OVERLAP_SIZE); + + if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_44100) + { + CONVERT_44100_TO_BLUETOOTH(pOv, pOvEnd); + CONVERT_44100_TO_BLUETOOTH(pIn, pInEnd); + } + else if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_22050) + { + CONVERT_22050_TO_BLUETOOTH(pOv, pOvEnd); + CONVERT_22050_TO_BLUETOOTH(pIn, pInEnd); + } + else if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_11025) + { + CONVERT_11025_TO_BLUETOOTH(pOv, pOvEnd); + CONVERT_11025_TO_BLUETOOTH(pIn, pInEnd); + } + + memcpy (pOverlapArea, pSrc + (dwSrcSamples * SRC_CHANNELS * sizeof (SRC_TYPE)) - \ + (BTA_DM_PCM_OVERLAP_SIZE * 2), BTA_DM_PCM_OVERLAP_SIZE * 2); + + *pLastCurPos = CurrentPos; + + return (psBtOut - (INT16 *)pDst); +} + +INT32 Convert_8S_ToBT_NoFilter (void *pSrc, void *pDst, UINT32 dwSrcSamples, UINT32 dwSrcSps) +{ + INT32 CurrentPos; + UINT8 *pbSrc = (UINT8 *)pSrc; + INT16 *psDst = (INT16 *)pDst; + INT16 sWorker, sWorker2; + + // start at dwSpsSrc / 2, decrement by 8000 + // + CurrentPos = (dwSrcSps >> 1); + + while (dwSrcSamples--) + { + CurrentPos -= 8000; + + if (CurrentPos >= 0) + pbSrc += 2; + else + { + sWorker = *(unsigned char *)pbSrc; + sWorker -= 0x80; + sWorker <<= 8; + pbSrc++; + + sWorker2 = *(unsigned char *)pbSrc; + sWorker2 -= 0x80; + sWorker2 <<= 8; + pbSrc++; + + sWorker += sWorker2; + sWorker >>= 1; + + *psDst++ = sWorker; + + CurrentPos += dwSrcSps; + } + } + + return (psDst - (INT16 *)pDst); +} + + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +#undef SRC_CHANNELS +#undef SRC_SAMPLE +#undef SRC_TYPE + +#define SRC_TYPE INT16 +#define SRC_CHANNELS 2 +#define SRC_SAMPLE(x) ((pS[x * 2] + pS[(x * 2) + 1]) >> 1) + +INT32 Convert_16S_ToBT_Filtered (UINT8 *pSrc, void *pDst, UINT32 dwSrcSamples, + UINT32 dwSrcSps, INT32 *pLastCurPos, UINT8 *pOverlapArea) +{ + INT32 CurrentPos = *pLastCurPos; + SRC_TYPE *pIn, *pInEnd; + SRC_TYPE *pOv, *pOvEnd; + INT16 *psBtOut = (INT16 *)pDst; + + memcpy (pOverlapArea + (BTA_DM_PCM_OVERLAP_SIZE * 2), pSrc, BTA_DM_PCM_OVERLAP_SIZE * 2); + + pOv = (SRC_TYPE *)(pOverlapArea + BTA_DM_PCM_OVERLAP_SIZE); + pOvEnd = (SRC_TYPE *)(pOverlapArea + (BTA_DM_PCM_OVERLAP_SIZE * 3)); + + pIn = (SRC_TYPE *)(pSrc + BTA_DM_PCM_OVERLAP_SIZE); + pInEnd = (SRC_TYPE *)(pSrc + (dwSrcSamples * SRC_CHANNELS * sizeof (SRC_TYPE)) - BTA_DM_PCM_OVERLAP_SIZE); + + if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_44100) + { + CONVERT_44100_TO_BLUETOOTH(pOv, pOvEnd); + CONVERT_44100_TO_BLUETOOTH(pIn, pInEnd); + } + else if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_22050) + { + CONVERT_22050_TO_BLUETOOTH(pOv, pOvEnd); + CONVERT_22050_TO_BLUETOOTH(pIn, pInEnd); + } + else if (dwSrcSps == BTA_DM_PCM_SMPL_RATE_11025) + { + CONVERT_11025_TO_BLUETOOTH(pOv, pOvEnd); + CONVERT_11025_TO_BLUETOOTH(pIn, pInEnd); + } + + memcpy (pOverlapArea, pSrc + (dwSrcSamples * SRC_CHANNELS * sizeof (SRC_TYPE)) - \ + (BTA_DM_PCM_OVERLAP_SIZE * 2), BTA_DM_PCM_OVERLAP_SIZE * 2); + + *pLastCurPos = CurrentPos; + + return (psBtOut - (INT16 *)pDst); +} + +INT32 Convert_16S_ToBT_NoFilter (void *pSrc, void *pDst, UINT32 dwSrcSamples, UINT32 dwSrcSps) +{ + INT32 CurrentPos; + INT16 *psSrc = (INT16 *)pSrc; + INT16 *psDst = (INT16 *)pDst; + INT16 sWorker; + + // start at dwSpsSrc / 2, decrement by 8000 + // + CurrentPos = (dwSrcSps >> 1); + + while (dwSrcSamples--) + { + CurrentPos -= 8000; + + if (CurrentPos >= 0) + psSrc += 2; + else + { + /* CR 82894, to avoid overflow, divide before add */ + sWorker = ((*psSrc) >> 1 ); + psSrc++; + sWorker += ((*psSrc) >> 1 ); + psSrc++; + + *psDst++ = sWorker; + + CurrentPos += dwSrcSps; + } + } + + return (psDst - (INT16 *)pDst); +} + +/******************************************************************************* +** +** Function BTA_DmPcmInitSamples +** +** Description initialize the down sample converter. +** +** src_sps: original samples per second (source audio data) +** (ex. 44100, 48000) +** bits: number of bits per pcm sample (16) +** n_channels: number of channels (i.e. mono(1), stereo(2)...) +** +** Returns none +** +*******************************************************************************/ +void BTA_DmPcmInitSamples (UINT32 src_sps, UINT32 bits, UINT32 n_channels) +{ + tBTA_DM_PCM_RESAMPLE_CB *p_cb = &bta_dm_pcm_cb; + + p_cb->cur_pos = src_sps / 2; + p_cb->src_sps = src_sps; + p_cb->bits = bits; + p_cb->n_channels = n_channels; + p_cb->sample_size = 2; + p_cb->divisor = 2; + + memset(p_cb->overlap_area, 0, sizeof(p_cb->overlap_area) ); + + if ((src_sps == BTA_DM_PCM_SMPL_RATE_44100) || + (src_sps == BTA_DM_PCM_SMPL_RATE_22050) || + (src_sps == BTA_DM_PCM_SMPL_RATE_11025)) + p_cb->can_be_filtered = 1; + else + p_cb->can_be_filtered = 0; + +#if BTA_DM_SCO_DEBUG + APPL_TRACE_DEBUG2("bta_dm_pcm_init_samples: n_channels = %d bits = %d", n_channels, bits); +#endif + if(n_channels == 1) + { + /* mono */ + if(bits == 8) + { + p_cb->filter = (PCONVERT_TO_BT_FILTERED) Convert_8M_ToBT_Filtered; + p_cb->nofilter = (PCONVERT_TO_BT_NOFILTER) Convert_8M_ToBT_NoFilter; + p_cb->divisor = 1; + } + else + { + p_cb->filter = (PCONVERT_TO_BT_FILTERED) Convert_16M_ToBT_Filtered; + p_cb->nofilter = (PCONVERT_TO_BT_NOFILTER) Convert_16M_ToBT_NoFilter; + } + } + else + { + /* stereo */ + if(bits == 8) + { + p_cb->filter = (PCONVERT_TO_BT_FILTERED) Convert_8S_ToBT_Filtered; + p_cb->nofilter = (PCONVERT_TO_BT_NOFILTER) Convert_8S_ToBT_NoFilter; + } + else + { + p_cb->filter = (PCONVERT_TO_BT_FILTERED) Convert_16S_ToBT_Filtered; + p_cb->nofilter = (PCONVERT_TO_BT_NOFILTER) Convert_16S_ToBT_NoFilter; + p_cb->divisor = 4; + } + } + +#if BTA_DM_SCO_DEBUG + APPL_TRACE_DEBUG2("bta_pcm_init_dwn_sample: cur_pos %d, src_sps %d", \ + p_cb->cur_pos, p_cb->src_sps); + APPL_TRACE_DEBUG3("bta_pcm_init_dwn_sample: bits %d, n_channels %d, sample_size %d, ", \ + p_cb->bits, p_cb->n_channels, p_cb->sample_size); + APPL_TRACE_DEBUG3("bta_pcm_init_dwn_sample: can_be_filtered %d, n_channels: %d, \ + divisor %d", p_cb->can_be_filtered, p_cb->n_channels, p_cb->divisor); +#endif + +} + +/************************************************************************************** +** Function BTA_DmPcmResample +** +** Description Down sampling utility to convert higher sampling rate into 8K/16bits +** PCM samples. +** +** Parameters p_src: pointer to the buffer where the original sampling PCM +** are stored. +** in_bytes: Length of the input PCM sample buffer in byte. +** p_dst: pointer to the buffer which is to be used to store +** the converted PCM samples. +** +** +** Returns INT32: number of samples converted. +** +**************************************************************************************/ +INT32 BTA_DmPcmResample (void *p_src, UINT32 in_bytes, void *p_dst) +{ + UINT32 out_sample; + +#if BTA_DM_SCO_DEBUG + APPL_TRACE_DEBUG1("bta_pcm_resample : insamples %d", (in_bytes / bta_dm_pcm_cb.divisor)); +#endif + if(bta_dm_pcm_cb.can_be_filtered) + { + out_sample = (*bta_dm_pcm_cb.filter) (p_src, p_dst, (in_bytes / bta_dm_pcm_cb.divisor), + bta_dm_pcm_cb.src_sps, (INT32 *) &bta_dm_pcm_cb.cur_pos, bta_dm_pcm_cb.overlap_area); + } + else + { + out_sample = (*bta_dm_pcm_cb.nofilter) (p_src, p_dst, + (in_bytes / bta_dm_pcm_cb.divisor), bta_dm_pcm_cb.src_sps); + } + +#if BTA_DM_SCO_DEBUG + APPL_TRACE_DEBUG1("bta_pcm_resample : outsamples %d", out_sample); +#endif + + return (out_sample * bta_dm_pcm_cb.sample_size); +} +#endif |